8 #include <QtMaemo5/QMaemo5InformationBox>
11 #include "librarydialog.h"
13 #include "sortedlibrary.h"
15 #include "infodialog.h"
18 LibraryDialog::LibraryDialog(QWidget *parent):
19 QDialog(parent, Qt::Dialog | Qt::WindowTitleHint |
20 Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint)
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);
29 setSizeGripEnabled(true);
32 Library *library = Library::instance();
33 QModelIndex current = library->nowReading();
34 list->setCurrentIndex(current);
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::instance(), SIGNAL(nowReadingChanged()),
57 this, SLOT(onCurrentBookChanged()));
59 connect(list, SIGNAL(itemSelectionChanged()),
60 this, SLOT(onItemSelectionChanged()));
61 connect(detailsButton, SIGNAL(clicked()), this, SLOT(onDetails()));
62 connect(readButton, SIGNAL(clicked()), this, SLOT(onRead()));
63 connect(removeButton, SIGNAL(clicked()), this, SLOT(onRemove()));
65 connect(addButton, SIGNAL(clicked()), this, SLOT(onAdd()));
67 connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
68 this, SLOT(onItemActivated(QListWidgetItem *)));
72 onItemSelectionChanged();
76 void LibraryDialog::onAdd()
78 Library *library = Library::instance();
80 // Figure out directory to show
81 QString lastDir = Settings::instance()->value("lastdir").toString();
83 lastDir = QDir::homePath();
87 QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
88 lastDir, "Books (*.epub)");
93 // Save directory selected
94 Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
96 // Add book to library
97 if (library->find(path).isValid()) {
99 QMaemo5InformationBox::information(this,
100 tr("This book is already in the library"),
101 QMaemo5InformationBox::DefaultTimeout);
103 (void)QMessageBox::information(this, tr("Dorian"),
104 tr("This book is already in the library"), QMessageBox::Ok);
105 #endif // Q_WS_MAEMO_5
106 // FIXME: Select existing book
113 void LibraryDialog::onBookAdded()
116 Library *library = Library::instance();
117 int index = library->size() - 1;
118 Book *book = library->at(index);
119 QListWidgetItem *item = new QListWidgetItem(book->cover,
120 createItemText(book));
122 list->setCurrentItem(item);
128 void LibraryDialog::onRemove()
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 + "\"?",
143 Library::instance()->remove(current);
148 void LibraryDialog::onRead()
150 qDebug() << "LibraryDialog::onRead";
151 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
152 if (current.isValid()) {
153 Library::instance()->setNowReading(current);
157 void LibraryDialog::onDetails()
160 onItemActivated(list->selectedItems()[0]);
164 #endif // Q_WS_MAEMO_5
166 void LibraryDialog::onItemActivated(const QModelIndex &index)
168 qDebug() << "LibraryDialog::onItemActivated";
170 int row = list->row(item);
171 Book *book = Library::instance()->at(row);
172 InfoDialog *info = new InfoDialog(book, this);
177 QString LibraryDialog::createItemText(const Book *book)
179 QString text = book->title + "\n";
180 if (book->creators.size()) {
181 text += book->creators[0];
182 for (int i = 1; i < book->creators.size(); i++) {
183 text += ", " + book->creators[i];
191 void LibraryDialog::onItemSelectionChanged()
194 bool enable = list->selectedItems().size();
195 qDebug() << "LibraryDialog::onItemSelectionChanged" << enable;
196 readButton->setEnabled(enable);
197 qDebug() << " readButton";
198 detailsButton->setEnabled(enable);
199 qDebug() << " detailsButton";
200 removeButton->setEnabled(enable);
201 qDebug() << " removeButton";
205 #endif // Q_WS_MAEMO_5
207 void LibraryDialog::onCurrentBookChanged()