Handle selection changes.
[dorian] / librarydialog.cpp
1 #include <QtGui>
2 #include <QDebug>
3 #include <QFileInfo>
4 #include <QDir>
5 #include <QModelIndex>
6
7 #ifdef Q_WS_MAEMO_5
8 #include <QtMaemo5/QMaemo5InformationBox>
9 #endif
10
11 #include "librarydialog.h"
12 #include "library.h"
13 #include "sortedlibrary.h"
14 #include "book.h"
15 #include "infodialog.h"
16 #include "settings.h"
17
18 LibraryDialog::LibraryDialog(QWidget *parent):
19         QDialog(parent, Qt::Dialog | Qt::WindowTitleHint |
20                 Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint)
21 {
22     setWindowTitle(tr("Library"));
23     list = new QListView(this);
24     sortedLibrary = new SortedLibrary(this);
25     list->setModel(sortedLibrary);
26     list->setSelectionMode(QAbstractItemView::SingleSelection);
27     list->setUniformItemSizes(true);
28 #ifndef Q_WS_MAEMO_5
29     setSizeGripEnabled(true);
30 #endif
31
32     Library *library = Library::instance();
33     QModelIndex current = library->nowReading();
34     setSelected(current);
35
36     QHBoxLayout *horizontalLayout = new QHBoxLayout(this);
37     horizontalLayout->addWidget(list);
38
39     QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical);
40 #ifndef Q_WS_MAEMO_5
41     detailsButton = new QPushButton(tr("Details"), this);
42     readButton = new QPushButton(tr("Read"), this);
43     removeButton = new QPushButton(tr("Delete"), this);
44 #endif
45     addButton = new QPushButton(tr("Add"), this);
46
47 #ifndef Q_WS_MAEMO_5
48     buttonBox->addButton(detailsButton, QDialogButtonBox::ActionRole);
49     buttonBox->addButton(readButton, QDialogButtonBox::AcceptRole);
50     buttonBox->addButton(removeButton, QDialogButtonBox::ActionRole);
51 #endif // Q_WS_MAEMO_5
52     buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
53
54     horizontalLayout->addWidget(buttonBox);
55
56     connect(Library::instance(), SIGNAL(nowReadingChanged()),
57             this, SLOT(onCurrentBookChanged()));
58     connect(Library::instance(),
59             SIGNAL(rowsInserted(const QModelIndex &, int, int)),
60             this,
61             SLOT(onBookAdded()));
62     connect(addButton, SIGNAL(clicked()), this, SLOT(onAdd()));
63 #ifdef Q_WS_MAEMO_5
64     connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
65             this, SLOT(onItemActivated(QListWidgetItem *)));
66 #else
67     connect(list, SIGNAL(itemSelectionChanged()),
68             this, SLOT(onItemSelectionChanged()));
69     connect(detailsButton, SIGNAL(clicked()), this, SLOT(onDetails()));
70     connect(readButton, SIGNAL(clicked()), this, SLOT(onRead()));
71     connect(removeButton, SIGNAL(clicked()), this, SLOT(onRemove()));
72     connect(list, SIGNAL(doubleClicked(const QModelIndex &)),
73             this, SLOT(onItemActivated(const QModelIndex &)));
74     connect(list->selectionModel(),
75             SIGNAL(selectionChanged(const QItemSelection &,
76                                     const QItemSelection &)),
77             this, SLOT(onItemSelectionChanged()));
78     onItemSelectionChanged();
79 #endif // Q_WS_MAEMO_5
80 }
81
82 void LibraryDialog::onAdd()
83 {
84     Library *library = Library::instance();
85
86     // Figure out directory to show
87     QString lastDir = Settings::instance()->value("lastdir").toString();
88     if (lastDir == "") {
89         lastDir = QDir::homePath();
90     }
91
92     // Get book file name
93     QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
94                                                 lastDir, "Books (*.epub)");
95     if (path == "") {
96         return;
97     }
98
99     // Save directory selected
100     Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
101
102     // Add book to library
103     QModelIndex index = library->find(path);
104     if (index.isValid()) {
105 #ifdef Q_WS_MAEMO_5
106         QMaemo5InformationBox::information(this,
107             tr("This book is already in the library"),
108             QMaemo5InformationBox::DefaultTimeout);
109 #else
110         (void)QMessageBox::information(this, tr("Dorian"),
111             tr("This book is already in the library"), QMessageBox::Ok);
112 #endif // Q_WS_MAEMO_5
113         setSelected(index);
114     }
115     else {
116         library->add(path);
117     }
118 }
119
120 void LibraryDialog::onBookAdded()
121 {
122     Library *library = Library::instance();
123     setSelected(library->index(library->rowCount() - 1));
124 }
125
126 #ifndef Q_WS_MAEMO_5
127
128 void LibraryDialog::onRemove()
129 {
130     qDebug() << "LibraryDialog::onRemove";
131     QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
132     if (current.isValid()) {
133         Book *currentBook = Library::instance()->book(current);
134         QString title = currentBook->name();
135         if (QMessageBox::Yes ==
136             QMessageBox::question(this, "Delete book",
137                                   "Delete book \"" + title + "\"?",
138                                   QMessageBox::Yes
139 #ifndef Q_WS_MAEMO_5
140                                   , QMessageBox::No
141 #endif
142                                   )) {
143             Library::instance()->remove(current);
144         }
145     }
146 }
147
148 void LibraryDialog::onRead()
149 {
150     qDebug() << "LibraryDialog::onRead";
151     QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
152     if (current.isValid()) {
153         Library::instance()->setNowReading(current);
154     }
155 }
156
157 void LibraryDialog::onDetails()
158 {
159     onItemActivated(list->currentIndex());
160 }
161
162 #endif // Q_WS_MAEMO_5
163
164 void LibraryDialog::onItemActivated(const QModelIndex &index)
165 {
166     qDebug() << "LibraryDialog::onItemActivated";
167     QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
168     Book *book = Library::instance()->book(libraryIndex);
169     (new InfoDialog(book, this))->exec();
170 }
171
172 QString LibraryDialog::createItemText(const Book *book)
173 {
174     QString text = book->title + "\n";
175     if (book->creators.size()) {
176         text += book->creators[0];
177         for (int i = 1; i < book->creators.size(); i++) {
178             text += ", " + book->creators[i];
179         }
180     }
181     return text;
182 }
183
184 #ifndef Q_WS_MAEMO_5
185
186 void LibraryDialog::onItemSelectionChanged()
187 {
188     bool enable = selected().isValid();
189     qDebug() << "LibraryDialog::onItemSelectionChanged" << enable;
190     readButton->setEnabled(enable);
191     qDebug() << " readButton";
192     detailsButton->setEnabled(enable);
193     qDebug() << " detailsButton";
194     removeButton->setEnabled(enable);
195     qDebug() << " removeButton";
196 }
197
198 #endif // Q_WS_MAEMO_5
199
200 void LibraryDialog::onCurrentBookChanged()
201 {
202     close();
203 }
204
205 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
206 {
207     QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
208     list->selectionModel()->clearSelection();
209     if (sortedIndex.isValid()) {
210         list->selectionModel()->select(sortedIndex,
211                                        QItemSelectionModel::Select);
212         list->setCurrentIndex(sortedIndex);
213     }
214 }
215
216 QModelIndex LibraryDialog::selected() const
217 {
218     QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
219     if (selectedItems.size()) {
220         return sortedLibrary->mapToSource(selectedItems[0]);
221     }
222     return QModelIndex();
223 }