Improve chapter navigation: work around a QUrl bug.
[dorian] / bookmarksdialog.cpp
index f7d2400..fdfed5b 100644 (file)
@@ -3,70 +3,59 @@
 #include "bookmarksdialog.h"
 #include "book.h"
 #include "bookmarkinfodialog.h"
+#include "listview.h"
 
 BookmarksDialog::BookmarksDialog(Book *book_, QWidget *parent):
-    QMainWindow(parent), book(book_)
+    ListWindow(parent), book(book_)
 {
-#ifdef Q_WS_MAEMO_5
-    setAttribute(Qt::WA_Maemo5StackedWindow, true);
-#endif
     setWindowTitle(tr("Bookmarks"));
-
-    QFrame *frame = new QFrame(this);
-    setCentralWidget(frame);
-    QHBoxLayout *horizontalLayout = new QHBoxLayout(this);
-    frame->setLayout(horizontalLayout);
-
-    list = new QListWidget(this);
-    list->setSelectionMode(QAbstractItemView::SingleSelection);
-    foreach (Book::Bookmark bookmark, book_->bookmarks()) {
-        QString contentId = book_->toc[bookmark.chapter];
-        QString contentTitle = book_->content[contentId].name;
-        (void)new QListWidgetItem(QIcon(":icons/bookmark.png"), contentTitle +
-            "\nAt " + QString::number((int)(bookmark.pos*100)) + "%", list);
+    if (!book) {
+        return;
     }
 
-    horizontalLayout->addWidget(list);
-
+    addAction(tr("Add bookmark"), this, SLOT(onAdd()), ":/icons/add.png");
 #ifndef Q_WS_MAEMO_5
-    QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical);
-
-    QPushButton *goButton = new QPushButton(tr("Go to"), this);
-    buttonBox->addButton(goButton, QDialogButtonBox::ActionRole);
-    connect(goButton, SIGNAL(clicked()), this, SLOT(onGo()));
-
-    QPushButton *closeButton = buttonBox->addButton(QDialogButtonBox::Close);
-    connect(closeButton, SIGNAL(clicked()), this, SLOT(onClose()));
+    addItemAction(tr("Go to"), this, SLOT(onGo()));
+    addItemAction(tr("Delete"), this, SLOT(onDelete()));
+#endif // ! Q_WS_MAEMO_5
 
-    QPushButton *addButton = new QPushButton(tr("Add"), this);
-    buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
-    connect(addButton, SIGNAL(clicked()), this, SLOT(onAdd()));
-
-    QPushButton *deleteButton = new QPushButton(tr("Delete"), this);
-    buttonBox->addButton(deleteButton, QDialogButtonBox::DestructiveRole);
-    connect(deleteButton, SIGNAL(clicked()), this, SLOT(onDelete()));
+    // Build bookmark list
+    // FIXME: Localize me
+    foreach (Book::Bookmark bookmark, book_->bookmarks()) {
+        QString label("At ");
+        label += QString::number((int)(100 * book_->
+            getProgress(bookmark.part, bookmark.pos))) + "%";
+        int chapterIndex = book_->chapterFromPart(bookmark.part);
+        if (chapterIndex != -1) {
+            QString chapterId = book_->chapters[chapterIndex];
+            label += "\nIn \"" + book_->content[chapterId].name + "\"";
+        }
+        data.append(label);
+    }
 
-    horizontalLayout->addWidget(buttonBox);
-#else
-    QAction *addBookmarkAction = menuBar()->addAction(tr("Add bookmark"));
-    connect(addBookmarkAction, SIGNAL(triggered()), this, SLOT(onAdd()));
-#endif // Q_WS_MAEMO_5
-    connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
-            this, SLOT(onItemActivated(QListWidgetItem *)));
+    // Create bookmark list view
+    QStringListModel *model = new QStringListModel(data, this);
+    list = new ListView;
+    list->setSelectionMode(QAbstractItemView::SingleSelection);
+    list->setModel(model);
+    addList(list);
+    connect(list, SIGNAL(activated(const QModelIndex &)),
+            this, SLOT(onItemActivated(const QModelIndex &)));
+    addList(list);
 }
 
 void BookmarksDialog::onGo()
 {
-    if (!list->selectedItems().isEmpty()) {
-        QListWidgetItem *item = list->selectedItems()[0];
-        emit goToBookmark(list->row(item));
+    QModelIndex current = list->currentIndex();
+    if (current.isValid()) {
+        emit goToBookmark(current.row());
         close();
     }
 }
 
-void BookmarksDialog::onItemActivated(QListWidgetItem *item)
+void BookmarksDialog::onItemActivated(const QModelIndex &index)
 {
-    switch ((new BookmarkInfoDialog(book, list->row(item), this))->exec()) {
+    switch ((new BookmarkInfoDialog(book, index.row(), this))->exec()) {
     case BookmarkInfoDialog::GoTo:
         onGo();
         break;
@@ -84,34 +73,20 @@ void BookmarksDialog::onAdd()
     close();
 }
 
-void BookmarksDialog::onClose()
-{
-    close();
-}
-
 void BookmarksDialog::onDelete(bool really)
 {
-    if (list->selectedItems().isEmpty()) {
+    QModelIndex current = list->currentIndex();
+    if (!current.isValid()) {
         return;
     }
     if (!really) {
         if (QMessageBox::Yes !=
             QMessageBox::question(this, tr("Delete bookmark"),
-                                  tr("Delete bookmark?"),
-                                  QMessageBox::Yes | QMessageBox::No)) {
+                tr("Delete bookmark?"), QMessageBox::Yes | QMessageBox::No)) {
             return;
         }
     }
-    QListWidgetItem *item = list->selectedItems()[0];
-    int row = list->row(item);
+    int row = current.row();
+    list->model()->removeRow(row);
     book->deleteBookmark(row);
-    delete item;
-}
-
-void BookmarksDialog::closeEvent(QCloseEvent *event)
-{
-#ifdef Q_WS_MAEMO_5
-    menuBar()->clear();
-#endif
-    event->accept();
 }