8 #include <QtMaemo5/QMaemo5InformationBox>
11 #include "librarydialog.h"
13 #include "sortedlibrary.h"
15 #include "infodialog.h"
18 LibraryDialog::LibraryDialog(QWidget *parent): QMainWindow(parent)
21 setAttribute(Qt::WA_Maemo5StackedWindow, true);
23 setWindowTitle(tr("Library"));
25 QFrame *frame = new QFrame(this);
26 setCentralWidget(frame);
27 QHBoxLayout *horizontalLayout = new QHBoxLayout(frame);
28 frame->setLayout(horizontalLayout);
30 list = new QListView(this);
31 sortedLibrary = new SortedLibrary(this);
32 list->setModel(sortedLibrary);
33 list->setSelectionMode(QAbstractItemView::SingleSelection);
35 list->setUniformItemSizes(true);
37 Library *library = Library::instance();
38 QModelIndex current = library->nowReading();
40 horizontalLayout->addWidget(list);
43 QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical);
44 detailsButton = new QPushButton(tr("Details"), this);
45 readButton = new QPushButton(tr("Read"), this);
46 removeButton = new QPushButton(tr("Delete"), this);
47 addButton = new QPushButton(tr("Add"), this);
49 buttonBox->addButton(detailsButton, QDialogButtonBox::ActionRole);
50 buttonBox->addButton(readButton, QDialogButtonBox::AcceptRole);
51 buttonBox->addButton(removeButton, QDialogButtonBox::ActionRole);
52 buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
53 horizontalLayout->addWidget(buttonBox);
55 QAction *addBookAction = menuBar()->addAction(tr("Add book"));
56 #endif // Q_WS_MAEMO_5
58 connect(Library::instance(), SIGNAL(nowReadingChanged()),
59 this, SLOT(onCurrentBookChanged()));
60 connect(Library::instance(),
61 SIGNAL(rowsInserted(const QModelIndex &, int, int)),
64 connect(list, SIGNAL(activated(const QModelIndex &)),
65 this, SLOT(onItemActivated(const QModelIndex &)));
67 connect(addButton, SIGNAL(clicked()), this, SLOT(onAdd()));
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();
77 connect(addBookAction, SIGNAL(triggered()), this, SLOT(onAdd()));
78 #endif // !Q_WS_MAEMO_5
81 void LibraryDialog::onAdd()
83 Library *library = Library::instance();
85 // Figure out directory to show
86 QString lastDir = Settings::instance()->value("lastdir").toString();
88 lastDir = QDir::homePath();
92 QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
93 lastDir, "Books (*.epub)");
98 // Save directory selected
99 Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
101 // Add book to library
102 QModelIndex index = library->find(path);
103 if (index.isValid()) {
105 QMaemo5InformationBox::information(this,
106 tr("This book is already in the library"),
107 QMaemo5InformationBox::DefaultTimeout);
109 (void)QMessageBox::information(this, tr("Dorian"),
110 tr("This book is already in the library"), QMessageBox::Ok);
111 #endif // Q_WS_MAEMO_5
119 void LibraryDialog::onBookAdded()
121 Library *library = Library::instance();
122 setSelected(library->index(library->rowCount() - 1));
127 void 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 + "\"?",
136 QMessageBox::Yes | QMessageBox::No)) {
137 Library::instance()->remove(current);
142 void LibraryDialog::onRead()
144 qDebug() << "LibraryDialog::onRead";
145 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
146 if (current.isValid()) {
147 Library::instance()->setNowReading(current);
151 void LibraryDialog::onDetails()
153 onItemActivated(list->currentIndex());
156 #endif // Q_WS_MAEMO_5
158 void LibraryDialog::onItemActivated(const QModelIndex &index)
160 qDebug() << "LibraryDialog::onItemActivated";
161 QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
162 Book *book = Library::instance()->book(libraryIndex);
163 (new InfoDialog(book, this))->exec();
166 QString LibraryDialog::createItemText(const Book *book)
168 QString text = book->title + "\n";
169 if (book->creators.size()) {
170 text += book->creators[0];
171 for (int i = 1; i < book->creators.size(); i++) {
172 text += ", " + book->creators[i];
180 void LibraryDialog::onItemSelectionChanged()
182 bool enable = selected().isValid();
183 readButton->setEnabled(enable);
184 detailsButton->setEnabled(enable);
185 removeButton->setEnabled(enable);
188 #endif // Q_WS_MAEMO_5
190 void LibraryDialog::onCurrentBookChanged()
195 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
197 QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
198 list->selectionModel()->clearSelection();
199 if (sortedIndex.isValid()) {
200 list->selectionModel()->select(sortedIndex,
201 QItemSelectionModel::Select);
202 list->setCurrentIndex(sortedIndex);
206 QModelIndex LibraryDialog::selected() const
208 QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
209 if (selectedItems.size()) {
210 return sortedLibrary->mapToSource(selectedItems[0]);
212 return QModelIndex();
215 void LibraryDialog::closeEvent(QCloseEvent *event)