8 #include <QtMaemo5/QMaemo5InformationBox>
11 #include "librarydialog.h"
13 #include "sortedlibrary.h"
15 #include "infodialog.h"
17 LibraryDialog::LibraryDialog(QWidget *parent):
18 QDialog(parent, Qt::Dialog | Qt::WindowTitleHint |
19 Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint)
21 setWindowTitle(tr("Library"));
22 list = new QListView(this);
23 sortedLibrary = new SortedLibrary(this);
24 list->setModel(sortedLibrary);
25 list->setSelectionMode(QAbstractItemView::SingleSelection);
27 setSizeGripEnabled(true);
31 Book *current = library->current();
32 if (library->size() && current) {
33 list->setItemSelected(list->item(library->find(current)), true);
37 QHBoxLayout *horizontalLayout = new QHBoxLayout(this);
38 horizontalLayout->addWidget(list);
40 QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical);
42 detailsButton = new QPushButton(tr("Details"), this);
43 readButton = new QPushButton(tr("Read"), this);
44 removeButton = new QPushButton(tr("Delete"), this);
46 addButton = new QPushButton(tr("Add"), this);
49 buttonBox->addButton(detailsButton, QDialogButtonBox::ActionRole);
50 buttonBox->addButton(readButton, QDialogButtonBox::AcceptRole);
51 buttonBox->addButton(removeButton, QDialogButtonBox::ActionRole);
52 #endif // Q_WS_MAEMO_5
53 buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
55 horizontalLayout->addWidget(buttonBox);
58 connect(library, SIGNAL(currentBookChanged()),
59 this, SLOT(onCurrentBookChanged()));
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()));
69 connect(addButton, SIGNAL(clicked()), this, SLOT(onAdd()));
71 connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
72 this, SLOT(onItemActivated(QListWidgetItem *)));
76 onItemSelectionChanged();
80 void LibraryDialog::onAdd()
82 Library *library = Library::instance();
85 if (library->size()) {
86 QFileInfo info(library->at(library->size() - 1)->path());
87 lastDir = info.absolutePath();
91 lastDir = QDir::homePath();
93 QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
94 lastDir, "Books (*.epub)");
95 qDebug() << "LibraryDialog::onAdd" << path;
100 lastDir = QFileInfo(path).absolutePath();
101 if (library->find(path) != -1) {
103 QMaemo5InformationBox::information(this,
104 tr("This book is already in the library"),
105 QMaemo5InformationBox::DefaultTimeout);
107 // FIXME: Select existing book
114 void LibraryDialog::onBookAdded()
117 Library *library = Library::instance();
118 int index = library->size() - 1;
119 Book *book = library->at(index);
120 QListWidgetItem *item = new QListWidgetItem(book->cover,
121 createItemText(book));
123 list->setCurrentItem(item);
129 void LibraryDialog::onRemove()
132 qDebug() << "LibraryDialog::onRemove";
133 if (list->selectedItems().isEmpty()) {
136 QListWidgetItem *item = list->selectedItems()[0];
137 int row = list->row(item);
138 QString title = Library::instance()->at(row)->title;
139 if (QMessageBox::Yes ==
140 QMessageBox::question(this, "Delete book",
141 "Delete book \"" + title + "\"?", QMessageBox::Yes
147 Library::instance()->remove(row);
152 void LibraryDialog::onRead()
154 qDebug() << "LibraryDialog::onRead";
156 if (list->selectedItems().isEmpty()) {
159 QListWidgetItem *item = list->selectedItems()[0];
160 int row = list->row(item);
161 Library::instance()->setCurrent(row);
165 void LibraryDialog::onDetails()
168 onItemActivated(list->selectedItems()[0]);
172 #endif // Q_WS_MAEMO_5
174 void LibraryDialog::onItemActivated(const QModelIndex &index)
176 qDebug() << "LibraryDialog::onItemActivated";
178 int row = list->row(item);
179 Book *book = Library::instance()->at(row);
180 InfoDialog *info = new InfoDialog(book, this);
185 QString LibraryDialog::createItemText(const Book *book)
187 QString text = book->title + "\n";
188 if (book->creators.size()) {
189 text += book->creators[0];
190 for (int i = 1; i < book->creators.size(); i++) {
191 text += ", " + book->creators[i];
199 void LibraryDialog::onItemSelectionChanged()
202 bool enable = list->selectedItems().size();
203 qDebug() << "LibraryDialog::onItemSelectionChanged" << enable;
204 readButton->setEnabled(enable);
205 qDebug() << " readButton";
206 detailsButton->setEnabled(enable);
207 qDebug() << " detailsButton";
208 removeButton->setEnabled(enable);
209 qDebug() << " removeButton";
213 #endif // Q_WS_MAEMO_5
215 void LibraryDialog::onCurrentBookChanged()