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();
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()));
58 connect(Library::instance(),
59 SIGNAL(rowsInserted(const QModelIndex &, int, int)),
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();
84 // Figure out directory to show
85 QString lastDir = Settings::instance()->value("lastdir").toString();
87 lastDir = QDir::homePath();
91 QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
92 lastDir, "Books (*.epub)");
97 // Save directory selected
98 Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
100 // Add book to library
101 QModelIndex index = library->find(path);
102 if (index.isValid()) {
104 QMaemo5InformationBox::information(this,
105 tr("This book is already in the library"),
106 QMaemo5InformationBox::DefaultTimeout);
108 (void)QMessageBox::information(this, tr("Dorian"),
109 tr("This book is already in the library"), QMessageBox::Ok);
110 #endif // Q_WS_MAEMO_5
118 void LibraryDialog::onBookAdded()
120 Library *library = Library::instance();
121 select(library->index(library->rowCount() - 1));
126 void LibraryDialog::onRemove()
128 qDebug() << "LibraryDialog::onRemove";
129 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
130 if (current.isValid()) {
131 Book *currentBook = Library::instance()->book(current);
132 QString title = currentBook->name();
133 if (QMessageBox::Yes ==
134 QMessageBox::question(this, "Delete book",
135 "Delete book \"" + title + "\"?",
141 Library::instance()->remove(current);
146 void LibraryDialog::onRead()
148 qDebug() << "LibraryDialog::onRead";
149 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
150 if (current.isValid()) {
151 Library::instance()->setNowReading(current);
155 void LibraryDialog::onDetails()
158 onItemActivated(list->selectedItems()[0]);
162 #endif // Q_WS_MAEMO_5
164 void LibraryDialog::onItemActivated(const QModelIndex &index)
166 qDebug() << "LibraryDialog::onItemActivated";
168 int row = list->row(item);
169 Book *book = Library::instance()->at(row);
170 InfoDialog *info = new InfoDialog(book, this);
175 QString LibraryDialog::createItemText(const Book *book)
177 QString text = book->title + "\n";
178 if (book->creators.size()) {
179 text += book->creators[0];
180 for (int i = 1; i < book->creators.size(); i++) {
181 text += ", " + book->creators[i];
189 void LibraryDialog::onItemSelectionChanged()
192 bool enable = list->selectedItems().size();
193 qDebug() << "LibraryDialog::onItemSelectionChanged" << enable;
194 readButton->setEnabled(enable);
195 qDebug() << " readButton";
196 detailsButton->setEnabled(enable);
197 qDebug() << " detailsButton";
198 removeButton->setEnabled(enable);
199 qDebug() << " removeButton";
203 #endif // Q_WS_MAEMO_5
205 void LibraryDialog::onCurrentBookChanged()
210 void LibraryDialog::select(const QModelIndex &libraryIndex)
212 QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
213 list->selectionModel()->clearSelection();
214 if (sortedIndex.isValid()) {
215 list->selectionModel()->select(sortedIndex,
216 QItemSelectionModel::Select);
217 list->setCurrentIndex(sortedIndex);