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);
26 list->setUniformItemSizes(true);
28 setSizeGripEnabled(true);
32 Book *current = library->current();
33 if (library->size() && current) {
34 list->setItemSelected(list->item(library->find(current)), true);
38 QHBoxLayout *horizontalLayout = new QHBoxLayout(this);
39 horizontalLayout->addWidget(list);
41 QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical);
43 detailsButton = new QPushButton(tr("Details"), this);
44 readButton = new QPushButton(tr("Read"), this);
45 removeButton = new QPushButton(tr("Delete"), this);
47 addButton = new QPushButton(tr("Add"), this);
50 buttonBox->addButton(detailsButton, QDialogButtonBox::ActionRole);
51 buttonBox->addButton(readButton, QDialogButtonBox::AcceptRole);
52 buttonBox->addButton(removeButton, QDialogButtonBox::ActionRole);
53 #endif // Q_WS_MAEMO_5
54 buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
56 horizontalLayout->addWidget(buttonBox);
58 connect(Library::instance(), SIGNAL(currentBookChanged()),
59 this, SLOT(onCurrentBookChanged()));
61 connect(list, SIGNAL(itemSelectionChanged()),
62 this, SLOT(onItemSelectionChanged()));
63 connect(detailsButton, SIGNAL(clicked()), this, SLOT(onDetails()));
64 connect(readButton, SIGNAL(clicked()), this, SLOT(onRead()));
65 connect(removeButton, SIGNAL(clicked()), this, SLOT(onRemove()));
67 connect(addButton, SIGNAL(clicked()), this, SLOT(onAdd()));
69 connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
70 this, SLOT(onItemActivated(QListWidgetItem *)));
74 onItemSelectionChanged();
78 void LibraryDialog::onAdd()
80 Library *library = Library::instance();
82 // Figure out directory to show
84 if (library->rowCount()) {
85 QModelIndex lastIndex = library->index(library->rowCount() - 1);
86 Book *lastBook = library->book(lastIndex);
87 QFileInfo info(lastBook->path());
88 lastDir = info.absolutePath();
92 lastDir = QDir::homePath();
96 QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
97 lastDir, "Books (*.epub)");
98 qDebug() << "LibraryDialog::onAdd" << path;
103 // Add book to library
104 lastDir = QFileInfo(path).absolutePath();
105 if (library->find(path).isValid()) {
107 QMaemo5InformationBox::information(this,
108 tr("This book is already in the library"),
109 QMaemo5InformationBox::DefaultTimeout);
111 // FIXME: Select existing book
118 void LibraryDialog::onBookAdded()
121 Library *library = Library::instance();
122 int index = library->size() - 1;
123 Book *book = library->at(index);
124 QListWidgetItem *item = new QListWidgetItem(book->cover,
125 createItemText(book));
127 list->setCurrentItem(item);
133 void LibraryDialog::onRemove()
135 qDebug() << "LibraryDialog::onRemove";
136 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
137 if (current.isValid()) {
138 Book *currentBook = Library::instance()->book(current);
139 QString title = currentBook->name();
140 if (QMessageBox::Yes ==
141 QMessageBox::question(this, "Delete book",
142 "Delete book \"" + title + "\"?",
148 Library::instance()->remove(current);
153 void LibraryDialog::onRead()
155 qDebug() << "LibraryDialog::onRead";
156 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
157 if (current.isValid()) {
158 Library::instance()->setNowReading(current);
162 void LibraryDialog::onDetails()
165 onItemActivated(list->selectedItems()[0]);
169 #endif // Q_WS_MAEMO_5
171 void LibraryDialog::onItemActivated(const QModelIndex &index)
173 qDebug() << "LibraryDialog::onItemActivated";
175 int row = list->row(item);
176 Book *book = Library::instance()->at(row);
177 InfoDialog *info = new InfoDialog(book, this);
182 QString LibraryDialog::createItemText(const Book *book)
184 QString text = book->title + "\n";
185 if (book->creators.size()) {
186 text += book->creators[0];
187 for (int i = 1; i < book->creators.size(); i++) {
188 text += ", " + book->creators[i];
196 void LibraryDialog::onItemSelectionChanged()
199 bool enable = list->selectedItems().size();
200 qDebug() << "LibraryDialog::onItemSelectionChanged" << enable;
201 readButton->setEnabled(enable);
202 qDebug() << " readButton";
203 detailsButton->setEnabled(enable);
204 qDebug() << " detailsButton";
205 removeButton->setEnabled(enable);
206 qDebug() << " removeButton";
210 #endif // Q_WS_MAEMO_5
212 void LibraryDialog::onCurrentBookChanged()