Jump to exact position on page, as identified by TOC entry. Handle
[dorian] / bookmarksdialog.cpp
1 #include <QtGui>
2
3 #include "bookmarksdialog.h"
4 #include "book.h"
5 #include "bookmarkinfodialog.h"
6 #include "listview.h"
7
8 BookmarksDialog::BookmarksDialog(Book *book_, QWidget *parent):
9     ListWindow(parent), book(book_)
10 {
11     setWindowTitle(tr("Bookmarks"));
12     if (!book) {
13         return;
14     }
15
16     addAction(tr("Add bookmark"), this, SLOT(onAdd()), ":/icons/add.png");
17 #ifndef Q_WS_MAEMO_5
18     addItemAction(tr("Go to"), this, SLOT(onGo()));
19     addItemAction(tr("Delete"), this, SLOT(onDelete()));
20 #endif // ! Q_WS_MAEMO_5
21
22     // Build bookmark list
23     // FIXME: Localize me
24     foreach (Book::Bookmark bookmark, book_->bookmarks()) {
25         QString label("At ");
26         label += QString::number((int)(100 * book_->
27             getProgress(bookmark.part, bookmark.pos))) + "%";
28         if (!bookmark.note.isEmpty()) {
29             label += ": " + bookmark.note;
30         }
31         label += "\n";
32         int chapterIndex = book_->chapterFromPart(bookmark.part);
33         if (chapterIndex != -1) {
34             QString chapterId = book_->chapters[chapterIndex];
35             label += "In \"" + book_->content[chapterId].name + "\"";
36         }
37         data.append(label);
38     }
39
40     // Create bookmark list view
41     QStringListModel *model = new QStringListModel(data, this);
42     list = new ListView;
43     list->setSelectionMode(QAbstractItemView::SingleSelection);
44     list->setModel(model);
45     addList(list);
46     connect(list, SIGNAL(activated(const QModelIndex &)),
47             this, SLOT(onItemActivated(const QModelIndex &)));
48     addList(list);
49 }
50
51 void BookmarksDialog::onGo()
52 {
53     QModelIndex current = list->currentIndex();
54     if (current.isValid()) {
55         emit goToBookmark(current.row());
56         close();
57     }
58 }
59
60 void BookmarksDialog::onItemActivated(const QModelIndex &index)
61 {
62     switch ((new BookmarkInfoDialog(book, index.row(), this))->exec()) {
63     case BookmarkInfoDialog::GoTo:
64         onGo();
65         break;
66     case BookmarkInfoDialog::Delete:
67         onDelete(true);
68         break;
69     default:
70         ;
71     }
72 }
73
74 void BookmarksDialog::onAdd()
75 {
76     bool ok;
77     QString text = QInputDialog::getText(this, tr("Add bookmark"),
78         tr("Note (optional):"), QLineEdit::Normal, QString(), &ok);
79     if (ok) {
80         emit addBookmark(text);
81         close();
82     }
83 }
84
85 void BookmarksDialog::onDelete(bool really)
86 {
87     QModelIndex current = list->currentIndex();
88     if (!current.isValid()) {
89         return;
90     }
91     if (!really) {
92         if (QMessageBox::Yes !=
93             QMessageBox::question(this, tr("Delete bookmark"),
94                 tr("Delete bookmark?"), QMessageBox::Yes | QMessageBox::No)) {
95             return;
96         }
97     }
98     int row = current.row();
99     list->model()->removeRow(row);
100     book->deleteBookmark(row);
101 }