#include "bookmarksdialog.h"
#include "book.h"
+#include "bookmarkinfodialog.h"
+#include "trace.h"
BookmarksDialog::BookmarksDialog(Book *book_, QWidget *parent):
- QDialog(parent, Qt::Dialog | Qt::WindowTitleHint |
- Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint)
+ ListWindow(tr("(No bookmarks)\n"), parent), book(book_)
{
setWindowTitle(tr("Bookmarks"));
-#ifndef Q_WS_MAEMO_5
- setSizeGripEnabled(true);
-#endif
+ if (!book) {
+ return;
+ }
- list = new QListWidget(this);
- list->setSelectionMode(QAbstractItemView::SingleSelection);
+ // Build and set bookmark model
+ // FIXME: Localize me
foreach (Book::Bookmark bookmark, book_->bookmarks()) {
- list->addItem("Chapter " + QString::number(bookmark.chapter) + ", at " +
- QString::number((int)(bookmark.pos * 100)) + "%");
+ QString label("At ");
+ label += QString::number((int)(100 * book_->
+ getProgress(bookmark.part, bookmark.pos))) + "%";
+ if (!bookmark.note.isEmpty()) {
+ label += ": " + bookmark.note;
+ }
+ label += "\n";
+ int chapterIndex = book_->chapterFromPart(bookmark.part);
+ if (chapterIndex != -1) {
+ QString chapterId = book_->chapters[chapterIndex];
+ label += "In \"" + book_->content[chapterId].name + "\"";
+ }
+ data.append(label);
}
+ QStringListModel *model = new QStringListModel(data, this);
+ setModel(model);
+
+ addButton(tr("Add bookmark"), this, SLOT(onAdd()), "add");
- QHBoxLayout *horizontalLayout = new QHBoxLayout(this);
- horizontalLayout->addWidget(list);
-
- QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical);
-#ifndef Q_WS_MAEMO_5
- QPushButton *goButton = new QPushButton(tr("Go"), this);
- buttonBox->addButton(goButton, QDialogButtonBox::AcceptRole);
- connect(goButton, SIGNAL(clicked()), this, SLOT(onGo()));
-#endif
- QPushButton *addButton = new QPushButton(tr("Add"), this);
- buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
- connect(addButton, SIGNAL(clicked()), this, SLOT(onAdd()));
-
- horizontalLayout->addWidget(buttonBox);
-
-#ifdef Q_WS_MAEMO_5
- connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
- this, SLOT(onItemActivated(QListWidgetItem *)));
-#endif
+ connect(this, SIGNAL(activated(const QModelIndex &)),
+ this, SLOT(onItemActivated(const QModelIndex &)));
}
void BookmarksDialog::onGo()
{
- if (list->selectedItems().isEmpty()) {
- return;
+ TRACE;
+ QModelIndex current = currentItem();
+ if (current.isValid()) {
+ emit goToBookmark(current.row());
+ close();
}
- QListWidgetItem *item = list->selectedItems()[0];
- int index = list->row(item) + 1;
- done(index);
}
-void BookmarksDialog::onItemActivated(QListWidgetItem *item)
+void BookmarksDialog::onItemActivated(const QModelIndex &index)
{
- done(list->row(item) + 1);
+ switch ((new BookmarkInfoDialog(book, index.row(), this))->exec()) {
+ case BookmarkInfoDialog::GoTo:
+ onGo();
+ break;
+ case BookmarkInfoDialog::Delete:
+ onDelete(true);
+ break;
+ default:
+ ;
+ }
}
void BookmarksDialog::onAdd()
{
- done(-1);
+ bool ok;
+ QString text = QInputDialog::getText(this, tr("Add bookmark"),
+ tr("Note (optional):"), QLineEdit::Normal, QString(), &ok);
+ if (ok) {
+ emit addBookmark(text);
+ close();
+ }
+}
+
+void BookmarksDialog::onDelete(bool really)
+{
+ QModelIndex current = currentItem();
+ if (!current.isValid()) {
+ return;
+ }
+ if (!really) {
+ if (QMessageBox::Yes !=
+ QMessageBox::question(this, tr("Delete bookmark"),
+ tr("Delete bookmark?"), QMessageBox::Yes | QMessageBox::No)) {
+ return;
+ }
+ }
+ int row = current.row();
+ model()->removeRow(row);
+ book->deleteBookmark(row);
}