Bookmarks with notes, first attempt.
[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         int chapterIndex = book_->chapterFromPart(bookmark.part);
29         if (chapterIndex != -1) {
30             QString chapterId = book_->chapters[chapterIndex];
31             label += ", in \"" + book_->content[chapterId].name + "\"";
32         }
33         if (!bookmark.note.isEmpty()) {
34             label += "\n" + bookmark.note;
35         }
36         data.append(label);
37     }
38
39     // Create bookmark list view
40     QStringListModel *model = new QStringListModel(data, this);
41     list = new ListView;
42     list->setSelectionMode(QAbstractItemView::SingleSelection);
43     list->setModel(model);
44     addList(list);
45     connect(list, SIGNAL(activated(const QModelIndex &)),
46             this, SLOT(onItemActivated(const QModelIndex &)));
47     addList(list);
48 }
49
50 void BookmarksDialog::onGo()
51 {
52     QModelIndex current = list->currentIndex();
53     if (current.isValid()) {
54         emit goToBookmark(current.row());
55         close();
56     }
57 }
58
59 void BookmarksDialog::onItemActivated(const QModelIndex &index)
60 {
61     switch ((new BookmarkInfoDialog(book, index.row(), this))->exec()) {
62     case BookmarkInfoDialog::GoTo:
63         onGo();
64         break;
65     case BookmarkInfoDialog::Delete:
66         onDelete(true);
67         break;
68     default:
69         ;
70     }
71 }
72
73 void BookmarksDialog::onAdd()
74 {
75     bool ok;
76     QString text = QInputDialog::getText(this, tr("Add bookmark"),
77                                          tr("Note:"), QLineEdit::Normal,
78                                          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 }