New version. Have and overlap between current/next page.
[dorian] / bookmarksdialog.cpp
index 498ec07..f125ed9 100644 (file)
@@ -3,70 +3,55 @@
 #include "bookmarksdialog.h"
 #include "book.h"
 #include "bookmarkinfodialog.h"
+#include "trace.h"
 
 BookmarksDialog::BookmarksDialog(Book *book_, QWidget *parent):
-    QMainWindow(parent), book(book_)
+    ListWindow(tr("(No bookmarks)\n"), parent), book(book_)
 {
-#ifdef Q_WS_MAEMO_5
-    setAttribute(Qt::WA_Maemo5StackedWindow, true);
-#endif
     setWindowTitle(tr("Bookmarks"));
+    if (!book) {
+        return;
+    }
 
-    QFrame *frame = new QFrame(this);
-    setCentralWidget(frame);
-    QHBoxLayout *horizontalLayout = new QHBoxLayout(frame);
-    frame->setLayout(horizontalLayout);
-
-    list = new QListWidget(this);
-    list->setSelectionMode(QAbstractItemView::SingleSelection);
+    // Build and set bookmark model
+    // FIXME: Localize me
     foreach (Book::Bookmark bookmark, book_->bookmarks()) {
-        QString contentId = book_->toc[bookmark.part];
-        QString contentTitle = book_->content[contentId].name;
-        (void)new QListWidgetItem(QIcon(":icons/bookmark.png"), contentTitle +
-            "\nAt " + QString::number((int)(bookmark.pos*100)) + "%", list);
+        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);
 
-    horizontalLayout->addWidget(list);
-
-#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()));
-
-    QPushButton *addButton = new QPushButton(tr("Add"), this);
-    buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
-    connect(addButton, SIGNAL(clicked()), this, SLOT(onAdd()));
+    addButton(tr("Add bookmark"), this, SLOT(onAdd()), "add");
 
-    QPushButton *deleteButton = new QPushButton(tr("Delete"), this);
-    buttonBox->addButton(deleteButton, QDialogButtonBox::DestructiveRole);
-    connect(deleteButton, SIGNAL(clicked()), this, SLOT(onDelete()));
-
-    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 *)));
+    connect(this, SIGNAL(activated(const QModelIndex &)),
+            this, SLOT(onItemActivated(const QModelIndex &)));
 }
 
 void BookmarksDialog::onGo()
 {
-    if (!list->selectedItems().isEmpty()) {
-        QListWidgetItem *item = list->selectedItems()[0];
-        emit goToBookmark(list->row(item));
+    TRACE;
+    QModelIndex current = currentItem();
+    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;
@@ -80,38 +65,29 @@ void BookmarksDialog::onItemActivated(QListWidgetItem *item)
 
 void BookmarksDialog::onAdd()
 {
-    emit addBookmark();
-    close();
-}
-
-void BookmarksDialog::onClose()
-{
-    close();
+    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)
 {
-    if (list->selectedItems().isEmpty()) {
+    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)) {
+                tr("Delete bookmark?"), QMessageBox::Yes | QMessageBox::No)) {
             return;
         }
     }
-    QListWidgetItem *item = list->selectedItems()[0];
-    int row = list->row(item);
+    int row = current.row();
+    model()->removeRow(row);
     book->deleteBookmark(row);
-    delete item;
-}
-
-void BookmarksDialog::closeEvent(QCloseEvent *event)
-{
-#ifdef Q_WS_MAEMO_5
-    menuBar()->clear();
-#endif
-    event->accept();
 }