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 *)));
74 connect(list, SIGNAL(doubleClicked(const QModelIndex &)),
75 this, SLOT(onItemActivated(const QModelIndex &)));
79 onItemSelectionChanged();
83 void LibraryDialog::onAdd()
85 Library *library = Library::instance();
87 // Figure out directory to show
88 QString lastDir = Settings::instance()->value("lastdir").toString();
90 lastDir = QDir::homePath();
94 QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
95 lastDir, "Books (*.epub)");
100 // Save directory selected
101 Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
103 // Add book to library
104 QModelIndex index = library->find(path);
105 if (index.isValid()) {
107 QMaemo5InformationBox::information(this,
108 tr("This book is already in the library"),
109 QMaemo5InformationBox::DefaultTimeout);
111 (void)QMessageBox::information(this, tr("Dorian"),
112 tr("This book is already in the library"), QMessageBox::Ok);
113 #endif // Q_WS_MAEMO_5
121 void LibraryDialog::onBookAdded()
123 Library *library = Library::instance();
124 select(library->index(library->rowCount() - 1));
129 void LibraryDialog::onRemove()
131 qDebug() << "LibraryDialog::onRemove";
132 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
133 if (current.isValid()) {
134 Book *currentBook = Library::instance()->book(current);
135 QString title = currentBook->name();
136 if (QMessageBox::Yes ==
137 QMessageBox::question(this, "Delete book",
138 "Delete book \"" + title + "\"?",
144 Library::instance()->remove(current);
149 void LibraryDialog::onRead()
151 qDebug() << "LibraryDialog::onRead";
152 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
153 if (current.isValid()) {
154 Library::instance()->setNowReading(current);
158 void LibraryDialog::onDetails()
160 onItemActivated(list->currentIndex());
163 #endif // Q_WS_MAEMO_5
165 void LibraryDialog::onItemActivated(const QModelIndex &index)
167 qDebug() << "LibraryDialog::onItemActivated";
168 QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
169 Book *book = Library::instance()->book(libraryIndex);
170 InfoDialog *info = new InfoDialog(book, this);
174 QString LibraryDialog::createItemText(const Book *book)
176 QString text = book->title + "\n";
177 if (book->creators.size()) {
178 text += book->creators[0];
179 for (int i = 1; i < book->creators.size(); i++) {
180 text += ", " + book->creators[i];
188 void LibraryDialog::onItemSelectionChanged()
191 bool enable = list->selectedItems().size();
192 qDebug() << "LibraryDialog::onItemSelectionChanged" << enable;
193 readButton->setEnabled(enable);
194 qDebug() << " readButton";
195 detailsButton->setEnabled(enable);
196 qDebug() << " detailsButton";
197 removeButton->setEnabled(enable);
198 qDebug() << " removeButton";
202 #endif // Q_WS_MAEMO_5
204 void LibraryDialog::onCurrentBookChanged()
209 void LibraryDialog::select(const QModelIndex &libraryIndex)
211 QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
212 list->selectionModel()->clearSelection();
213 if (sortedIndex.isValid()) {
214 list->selectionModel()->select(sortedIndex,
215 QItemSelectionModel::Select);
216 list->setCurrentIndex(sortedIndex);