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(this);
28 frame->setLayout(horizontalLayout);
30 list = new QListView(this);
31 sortedLibrary = new SortedLibrary(this);
32 list->setModel(sortedLibrary);
33 list->setSelectionMode(QAbstractItemView::SingleSelection);
34 list->setUniformItemSizes(true);
36 Library *library = Library::instance();
37 QModelIndex current = library->nowReading();
39 horizontalLayout->addWidget(list);
42 QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical);
43 detailsButton = new QPushButton(tr("Details"), this);
44 readButton = new QPushButton(tr("Read"), this);
45 removeButton = new QPushButton(tr("Delete"), this);
46 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 buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
52 horizontalLayout->addWidget(buttonBox);
54 QAction *addBookAction = menuBar()->addAction(tr("Add book"));
55 #endif // Q_WS_MAEMO_5
57 connect(Library::instance(), SIGNAL(nowReadingChanged()),
58 this, SLOT(onCurrentBookChanged()));
59 connect(Library::instance(),
60 SIGNAL(rowsInserted(const QModelIndex &, int, int)),
63 connect(list, SIGNAL(activated(const QModelIndex &)),
64 this, SLOT(onItemActivated(const QModelIndex &)));
66 connect(list, SIGNAL(itemSelectionChanged()),
67 this, SLOT(onItemSelectionChanged()));
68 connect(addButton, SIGNAL(clicked()), this, SLOT(onAdd()));
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->selectionModel(),
73 SIGNAL(selectionChanged(const QItemSelection &,
74 const QItemSelection &)),
75 this, SLOT(onItemSelectionChanged()));
76 onItemSelectionChanged();
78 connect(addBookAction, SIGNAL(triggered()), this, SLOT(onAdd()));
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 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
131 if (current.isValid()) {
132 Book *currentBook = Library::instance()->book(current);
133 QString title = currentBook->name();
134 if (QMessageBox::Yes ==
135 QMessageBox::question(this, "Delete book",
136 "Delete book \"" + title + "\"?",
137 QMessageBox::Yes | QMessageBox::No)) {
138 Library::instance()->remove(current);
143 void LibraryDialog::onRead()
145 qDebug() << "LibraryDialog::onRead";
146 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
147 if (current.isValid()) {
148 Library::instance()->setNowReading(current);
152 void LibraryDialog::onDetails()
154 onItemActivated(list->currentIndex());
157 #endif // Q_WS_MAEMO_5
159 void LibraryDialog::onItemActivated(const QModelIndex &index)
161 qDebug() << "LibraryDialog::onItemActivated";
162 QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
163 Book *book = Library::instance()->book(libraryIndex);
164 (new InfoDialog(book, this))->exec();
167 QString LibraryDialog::createItemText(const Book *book)
169 QString text = book->title + "\n";
170 if (book->creators.size()) {
171 text += book->creators[0];
172 for (int i = 1; i < book->creators.size(); i++) {
173 text += ", " + book->creators[i];
181 void LibraryDialog::onItemSelectionChanged()
183 bool enable = selected().isValid();
184 readButton->setEnabled(enable);
185 detailsButton->setEnabled(enable);
186 removeButton->setEnabled(enable);
189 #endif // Q_WS_MAEMO_5
191 void LibraryDialog::onCurrentBookChanged()
196 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
198 QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
199 list->selectionModel()->clearSelection();
200 if (sortedIndex.isValid()) {
201 list->selectionModel()->select(sortedIndex,
202 QItemSelectionModel::Select);
203 list->setCurrentIndex(sortedIndex);
207 QModelIndex LibraryDialog::selected() const
209 QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
210 if (selectedItems.size()) {
211 return sortedLibrary->mapToSource(selectedItems[0]);
213 return QModelIndex();
216 void LibraryDialog::closeEvent(QCloseEvent *event)