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)),
62 connect(addButton, SIGNAL(clicked()), this, SLOT(onAdd()));
64 connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
65 this, SLOT(onItemActivated(QListWidgetItem *)));
67 connect(list, SIGNAL(itemSelectionChanged()),
68 this, SLOT(onItemSelectionChanged()));
69 connect(detailsButton, SIGNAL(clicked()), this, SLOT(onDetails()));
70 connect(readButton, SIGNAL(clicked()), this, SLOT(onRead()));
71 connect(removeButton, SIGNAL(clicked()), this, SLOT(onRemove()));
72 connect(list, SIGNAL(doubleClicked(const QModelIndex &)),
73 this, SLOT(onItemActivated(const QModelIndex &)));
74 connect(list->selectionModel(),
75 SIGNAL(selectionChanged(const QItemSelection &,
76 const QItemSelection &)),
77 this, SLOT(onItemSelectionChanged()));
78 onItemSelectionChanged();
79 #endif // Q_WS_MAEMO_5
82 void LibraryDialog::onAdd()
84 Library *library = Library::instance();
86 // Figure out directory to show
87 QString lastDir = Settings::instance()->value("lastdir").toString();
89 lastDir = QDir::homePath();
93 QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
94 lastDir, "Books (*.epub)");
99 // Save directory selected
100 Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
102 // Add book to library
103 QModelIndex index = library->find(path);
104 if (index.isValid()) {
106 QMaemo5InformationBox::information(this,
107 tr("This book is already in the library"),
108 QMaemo5InformationBox::DefaultTimeout);
110 (void)QMessageBox::information(this, tr("Dorian"),
111 tr("This book is already in the library"), QMessageBox::Ok);
112 #endif // Q_WS_MAEMO_5
120 void LibraryDialog::onBookAdded()
122 Library *library = Library::instance();
123 setSelected(library->index(library->rowCount() - 1));
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()
159 onItemActivated(list->currentIndex());
162 #endif // Q_WS_MAEMO_5
164 void LibraryDialog::onItemActivated(const QModelIndex &index)
166 qDebug() << "LibraryDialog::onItemActivated";
167 QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
168 Book *book = Library::instance()->book(libraryIndex);
169 (new InfoDialog(book, this))->exec();
172 QString LibraryDialog::createItemText(const Book *book)
174 QString text = book->title + "\n";
175 if (book->creators.size()) {
176 text += book->creators[0];
177 for (int i = 1; i < book->creators.size(); i++) {
178 text += ", " + book->creators[i];
186 void LibraryDialog::onItemSelectionChanged()
188 bool enable = selected().isValid();
189 qDebug() << "LibraryDialog::onItemSelectionChanged" << enable;
190 readButton->setEnabled(enable);
191 qDebug() << " readButton";
192 detailsButton->setEnabled(enable);
193 qDebug() << " detailsButton";
194 removeButton->setEnabled(enable);
195 qDebug() << " removeButton";
198 #endif // Q_WS_MAEMO_5
200 void LibraryDialog::onCurrentBookChanged()
205 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
207 QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
208 list->selectionModel()->clearSelection();
209 if (sortedIndex.isValid()) {
210 list->selectionModel()->select(sortedIndex,
211 QItemSelectionModel::Select);
212 list->setCurrentIndex(sortedIndex);
216 QModelIndex LibraryDialog::selected() const
218 QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
219 if (selectedItems.size()) {
220 return sortedLibrary->mapToSource(selectedItems[0]);
222 return QModelIndex();