Better dialogs on Mac.
[dorian] / librarydialog.cpp
1 #include <QtGui>
2 #include <QDebug>
3 #include <QFileInfo>
4 #include <QDir>
5
6 #ifdef Q_WS_MAEMO_5
7 #include <QtMaemo5/QMaemo5InformationBox>
8 #endif
9
10 #include "librarydialog.h"
11 #include "library.h"
12 #include "book.h"
13 #include "infodialog.h"
14
15 LibraryDialog::LibraryDialog(QWidget *parent):
16         QDialog(parent, Qt::Dialog | Qt::WindowTitleHint |
17                 Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint)
18 {
19     setWindowTitle(tr("Library"));
20     list = new QListWidget(this);
21     list->setSelectionMode(QAbstractItemView::SingleSelection);
22 #ifndef Q_WS_MAEMO_5
23     setSizeGripEnabled(true);
24 #endif
25
26     Library *library = Library::instance();
27     for (int i = 0; i < library->size(); i++) {
28         Book *book = library->at(i);
29         (void)new QListWidgetItem(book->cover, createItemText(book), list);
30     }
31     Book *current = library->current();
32     if (library->size() && current) {
33         list->setItemSelected(list->item(library->find(current)), true);
34     }
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, SIGNAL(bookAdded()), this, SLOT(onBookAdded()));
57     connect(library, SIGNAL(currentBookChanged()),
58             this, SLOT(onCurrentBookChanged()));
59 #ifndef Q_WS_MAEMO_5
60     connect(list, SIGNAL(itemSelectionChanged()),
61             this, SLOT(onItemSelectionChanged()));
62     connect(detailsButton, SIGNAL(clicked()), this, SLOT(onDetails()));
63     connect(readButton, SIGNAL(clicked()), this, SLOT(onRead()));
64     connect(removeButton, SIGNAL(clicked()), this, SLOT(onRemove()));
65 #endif
66     connect(addButton, SIGNAL(clicked()), this, SLOT(onAdd()));
67 #ifdef Q_WS_MAEMO_5
68     connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
69             this, SLOT(onItemActivated(QListWidgetItem *)));
70 #endif
71
72 #ifndef Q_WS_MAEMO_5
73     onItemSelectionChanged();
74 #endif
75 }
76
77 void LibraryDialog::onAdd()
78 {
79     Library *library = Library::instance();
80
81     if (lastDir == "") {
82         if (library->size()) {
83             QFileInfo info(library->at(library->size() - 1)->path());
84             lastDir = info.absolutePath();
85         }
86     }
87     if (lastDir == "") {
88         lastDir = QDir::homePath();
89     }
90     QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
91                                                 lastDir, "Books (*.epub)");
92     qDebug() << "LibraryDialog::onAdd" << path;
93     if (path == "") {
94         return;
95     }
96
97     lastDir = QFileInfo(path).absolutePath();
98     if (library->find(path) != -1) {
99 #ifdef Q_WS_MAEMO_5
100         QMaemo5InformationBox::information(this,
101             tr("Book is alreadButtony in the library"),
102             QMaemo5InformationBox::DefaultTimeout);
103 #endif
104         // FIXME: Select existing book
105     }
106     else {
107         library->add(path);
108     }
109 }
110
111 void LibraryDialog::onBookAdded()
112 {
113     Library *library = Library::instance();
114     int index = library->size() - 1;
115     Book *book = library->at(index);
116     QListWidgetItem *item = new QListWidgetItem(book->cover,
117                                                 createItemText(book));
118     list->addItem(item);
119     list->setCurrentItem(item);
120 }
121
122 #ifndef Q_WS_MAEMO_5
123
124 void LibraryDialog::onRemove()
125 {
126     qDebug() << "LibraryDialog::onRemove";
127     if (list->selectedItems().isEmpty()) {
128         return;
129     }
130     QListWidgetItem *item = list->selectedItems()[0];
131     int row = list->row(item);
132     QString title = Library::instance()->at(row)->title;
133     if (QMessageBox::Yes ==
134         QMessageBox::question(this, "Delete book",
135                               "Delete book \"" + title + "\"?", QMessageBox::Yes
136 #ifndef Q_WS_MAEMO_5
137                               , QMessageBox::No
138 #endif
139                               )) {
140         list->takeItem(row);
141         Library::instance()->remove(row);
142     }
143 }
144
145 void LibraryDialog::onRead()
146 {
147     qDebug() << "LibraryDialog::onRead";
148     if (list->selectedItems().isEmpty()) {
149         return;
150     }
151     QListWidgetItem *item = list->selectedItems()[0];
152     int row = list->row(item);
153     Library::instance()->setCurrent(row);
154 }
155
156 void LibraryDialog::onDetails()
157 {
158     onItemActivated(list->selectedItems()[0]);
159 }
160
161 #endif // Q_WS_MAEMO_5
162
163 void LibraryDialog::onItemActivated(QListWidgetItem *item)
164 {
165     qDebug() << "LibraryDialog::onItemActivated";
166     int row = list->row(item);
167     Book *book = Library::instance()->at(row);
168     InfoDialog *info = new InfoDialog(book, this);
169     info->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 = list->selectedItems().size();
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 }