7 #include <QtMaemo5/QMaemo5InformationBox>
10 #include "librarydialog.h"
13 #include "infodialog.h"
15 LibraryDialog::LibraryDialog(QWidget *parent):
16 QDialog(parent, Qt::Dialog | Qt::WindowTitleHint |
17 Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint)
19 setWindowTitle(tr("Library"));
20 list = new QListWidget(this);
21 list->setSelectionMode(QAbstractItemView::SingleSelection);
23 setSizeGripEnabled(true);
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);
31 Book *current = library->current();
32 if (library->size() && current) {
33 list->setItemSelected(list->item(library->find(current)), true);
36 QHBoxLayout *horizontalLayout = new QHBoxLayout(this);
37 horizontalLayout->addWidget(list);
39 QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical);
41 detailsButton = new QPushButton(tr("Details"), this);
42 readButton = new QPushButton(tr("Read"), this);
43 removeButton = new QPushButton(tr("Delete"), this);
45 addButton = new QPushButton(tr("Add"), this);
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);
54 horizontalLayout->addWidget(buttonBox);
56 connect(library, SIGNAL(bookAdded()), this, SLOT(onBookAdded()));
57 connect(library, SIGNAL(currentBookChanged()),
58 this, SLOT(onCurrentBookChanged()));
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()));
66 connect(addButton, SIGNAL(clicked()), this, SLOT(onAdd()));
68 connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
69 this, SLOT(onItemActivated(QListWidgetItem *)));
73 onItemSelectionChanged();
77 void LibraryDialog::onAdd()
79 Library *library = Library::instance();
82 if (library->size()) {
83 QFileInfo info(library->at(library->size() - 1)->path());
84 lastDir = info.absolutePath();
88 lastDir = QDir::homePath();
90 QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
91 lastDir, "Books (*.epub)");
92 qDebug() << "LibraryDialog::onAdd" << path;
97 lastDir = QFileInfo(path).absolutePath();
98 if (library->find(path) != -1) {
100 QMaemo5InformationBox::information(this,
101 tr("Book is alreadButtony in the library"),
102 QMaemo5InformationBox::DefaultTimeout);
104 // FIXME: Select existing book
111 void LibraryDialog::onBookAdded()
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));
119 list->setCurrentItem(item);
124 void LibraryDialog::onRemove()
126 qDebug() << "LibraryDialog::onRemove";
127 if (list->selectedItems().isEmpty()) {
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
141 Library::instance()->remove(row);
145 void LibraryDialog::onRead()
147 qDebug() << "LibraryDialog::onRead";
148 if (list->selectedItems().isEmpty()) {
151 QListWidgetItem *item = list->selectedItems()[0];
152 int row = list->row(item);
153 Library::instance()->setCurrent(row);
156 void LibraryDialog::onDetails()
158 onItemActivated(list->selectedItems()[0]);
161 #endif // Q_WS_MAEMO_5
163 void LibraryDialog::onItemActivated(QListWidgetItem *item)
165 qDebug() << "LibraryDialog::onItemActivated";
166 int row = list->row(item);
167 Book *book = Library::instance()->at(row);
168 InfoDialog *info = new InfoDialog(book, this);
172 QString LibraryDialog::createItemText(const Book *book)
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];
186 void LibraryDialog::onItemSelectionChanged()
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";
198 #endif // Q_WS_MAEMO_5
200 void LibraryDialog::onCurrentBookChanged()