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);
44 #endif // Q_WS_MAEMO_5
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)),
62 connect(addButton, SIGNAL(clicked()), this, SLOT(onAdd()));
63 connect(list, SIGNAL(activated(const QModelIndex &)),
64 this, SLOT(onItemActivated(const QModelIndex &)));
66 connect(list, SIGNAL(itemSelectionChanged()),
67 this, SLOT(onItemSelectionChanged()));
68 connect(detailsButton, SIGNAL(clicked()), this, SLOT(onDetails()));
69 connect(readButton, SIGNAL(clicked()), this, SLOT(onRead()));
70 connect(removeButton, SIGNAL(clicked()), this, SLOT(onRemove()));
71 connect(list->selectionModel(),
72 SIGNAL(selectionChanged(const QItemSelection &,
73 const QItemSelection &)),
74 this, SLOT(onItemSelectionChanged()));
75 onItemSelectionChanged();
76 #endif // !Q_WS_MAEMO_5
79 void LibraryDialog::onAdd()
81 Library *library = Library::instance();
83 // Figure out directory to show
84 QString lastDir = Settings::instance()->value("lastdir").toString();
86 lastDir = QDir::homePath();
90 QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
91 lastDir, "Books (*.epub)");
96 // Save directory selected
97 Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
99 // Add book to library
100 QModelIndex index = library->find(path);
101 if (index.isValid()) {
103 QMaemo5InformationBox::information(this,
104 tr("This book is already in the library"),
105 QMaemo5InformationBox::DefaultTimeout);
107 (void)QMessageBox::information(this, tr("Dorian"),
108 tr("This book is already in the library"), QMessageBox::Ok);
109 #endif // Q_WS_MAEMO_5
117 void LibraryDialog::onBookAdded()
119 Library *library = Library::instance();
120 setSelected(library->index(library->rowCount() - 1));
125 void LibraryDialog::onRemove()
127 qDebug() << "LibraryDialog::onRemove";
128 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
129 if (current.isValid()) {
130 Book *currentBook = Library::instance()->book(current);
131 QString title = currentBook->name();
132 if (QMessageBox::Yes ==
133 QMessageBox::question(this, "Delete book",
134 "Delete book \"" + title + "\"?",
135 QMessageBox::Yes, QMessageBox::No)) {
136 Library::instance()->remove(current);
141 void LibraryDialog::onRead()
143 qDebug() << "LibraryDialog::onRead";
144 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
145 if (current.isValid()) {
146 Library::instance()->setNowReading(current);
150 void LibraryDialog::onDetails()
152 onItemActivated(list->currentIndex());
155 #endif // Q_WS_MAEMO_5
157 void LibraryDialog::onItemActivated(const QModelIndex &index)
159 qDebug() << "LibraryDialog::onItemActivated";
160 QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
161 Book *book = Library::instance()->book(libraryIndex);
162 (new InfoDialog(book, this))->exec();
165 QString LibraryDialog::createItemText(const Book *book)
167 QString text = book->title + "\n";
168 if (book->creators.size()) {
169 text += book->creators[0];
170 for (int i = 1; i < book->creators.size(); i++) {
171 text += ", " + book->creators[i];
179 void LibraryDialog::onItemSelectionChanged()
181 bool enable = selected().isValid();
182 qDebug() << "LibraryDialog::onItemSelectionChanged" << enable;
183 readButton->setEnabled(enable);
184 qDebug() << " readButton";
185 detailsButton->setEnabled(enable);
186 qDebug() << " detailsButton";
187 removeButton->setEnabled(enable);
188 qDebug() << " removeButton";
191 #endif // Q_WS_MAEMO_5
193 void LibraryDialog::onCurrentBookChanged()
198 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
200 QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
201 list->selectionModel()->clearSelection();
202 if (sortedIndex.isValid()) {
203 list->selectionModel()->select(sortedIndex,
204 QItemSelectionModel::Select);
205 list->setCurrentIndex(sortedIndex);
209 QModelIndex LibraryDialog::selected() const
211 QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
212 if (selectedItems.size()) {
213 return sortedLibrary->mapToSource(selectedItems[0]);
215 return QModelIndex();