Make selection handling work.
[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     select(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 #ifndef Q_WS_MAEMO_5
63     connect(list, SIGNAL(itemSelectionChanged()),
64             this, SLOT(onItemSelectionChanged()));
65     connect(detailsButton, SIGNAL(clicked()), this, SLOT(onDetails()));
66     connect(readButton, SIGNAL(clicked()), this, SLOT(onRead()));
67     connect(removeButton, SIGNAL(clicked()), this, SLOT(onRemove()));
68 #endif
69     connect(addButton, SIGNAL(clicked()), this, SLOT(onAdd()));
70 #ifdef Q_WS_MAEMO_5
71     connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
72             this, SLOT(onItemActivated(QListWidgetItem *)));
73 #endif
74
75 #ifndef Q_WS_MAEMO_5
76     onItemSelectionChanged();
77 #endif
78 }
79
80 void LibraryDialog::onAdd()
81 {
82     Library *library = Library::instance();
83
84     // Figure out directory to show
85     QString lastDir = Settings::instance()->value("lastdir").toString();
86     if (lastDir == "") {
87         lastDir = QDir::homePath();
88     }
89
90     // Get book file name
91     QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
92                                                 lastDir, "Books (*.epub)");
93     if (path == "") {
94         return;
95     }
96
97     // Save directory selected
98     Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
99
100     // Add book to library
101     QModelIndex index = library->find(path);
102     if (index.isValid()) {
103 #ifdef Q_WS_MAEMO_5
104         QMaemo5InformationBox::information(this,
105             tr("This book is already in the library"),
106             QMaemo5InformationBox::DefaultTimeout);
107 #else
108         (void)QMessageBox::information(this, tr("Dorian"),
109             tr("This book is already in the library"), QMessageBox::Ok);
110 #endif // Q_WS_MAEMO_5
111         select(index);
112     }
113     else {
114         library->add(path);
115     }
116 }
117
118 void LibraryDialog::onBookAdded()
119 {
120     Library *library = Library::instance();
121     select(library->index(library->rowCount() - 1));
122 }
123
124 #ifndef Q_WS_MAEMO_5
125
126 void LibraryDialog::onRemove()
127 {
128     qDebug() << "LibraryDialog::onRemove";
129     QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
130     if (current.isValid()) {
131         Book *currentBook = Library::instance()->book(current);
132         QString title = currentBook->name();
133         if (QMessageBox::Yes ==
134             QMessageBox::question(this, "Delete book",
135                                   "Delete book \"" + title + "\"?",
136                                   QMessageBox::Yes
137 #ifndef Q_WS_MAEMO_5
138                                   , QMessageBox::No
139 #endif
140                                   )) {
141             Library::instance()->remove(current);
142         }
143     }
144 }
145
146 void LibraryDialog::onRead()
147 {
148     qDebug() << "LibraryDialog::onRead";
149     QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
150     if (current.isValid()) {
151         Library::instance()->setNowReading(current);
152     }
153 }
154
155 void LibraryDialog::onDetails()
156 {
157 #if 0 // FIXME
158     onItemActivated(list->selectedItems()[0]);
159 #endif
160 }
161
162 #endif // Q_WS_MAEMO_5
163
164 void LibraryDialog::onItemActivated(const QModelIndex &index)
165 {
166     qDebug() << "LibraryDialog::onItemActivated";
167 #if 0 // FIXME
168     int row = list->row(item);
169     Book *book = Library::instance()->at(row);
170     InfoDialog *info = new InfoDialog(book, this);
171     info->exec();
172 #endif
173 }
174
175 QString LibraryDialog::createItemText(const Book *book)
176 {
177     QString text = book->title + "\n";
178     if (book->creators.size()) {
179         text += book->creators[0];
180         for (int i = 1; i < book->creators.size(); i++) {
181             text += ", " + book->creators[i];
182         }
183     }
184     return text;
185 }
186
187 #ifndef Q_WS_MAEMO_5
188
189 void LibraryDialog::onItemSelectionChanged()
190 {
191 #if 0 // FIXME
192     bool enable = list->selectedItems().size();
193     qDebug() << "LibraryDialog::onItemSelectionChanged" << enable;
194     readButton->setEnabled(enable);
195     qDebug() << " readButton";
196     detailsButton->setEnabled(enable);
197     qDebug() << " detailsButton";
198     removeButton->setEnabled(enable);
199     qDebug() << " removeButton";
200 #endif
201 }
202
203 #endif // Q_WS_MAEMO_5
204
205 void LibraryDialog::onCurrentBookChanged()
206 {
207     close();
208 }
209
210 void LibraryDialog::select(const QModelIndex &libraryIndex)
211 {
212     QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
213     list->selectionModel()->clearSelection();
214     if (sortedIndex.isValid()) {
215         list->selectionModel()->select(sortedIndex,
216                                        QItemSelectionModel::Select);
217         list->setCurrentIndex(sortedIndex);
218     }
219 }