9f81259140102f610a17e6f48b8ad199e993104a
[dorian] / bookmarksdialog.cpp
1 #include <QtGui>
2
3 #include "bookmarksdialog.h"
4 #include "book.h"
5 #include "bookmarkinfodialog.h"
6 #include "trace.h"
7
8 BookmarksDialog::BookmarksDialog(Book *book_, QWidget *parent):
9     ListWindow(tr("(No bookmarks)\n"), parent), book(book_)
10 {
11     setWindowTitle(tr("Bookmarks"));
12     if (!book) {
13         return;
14     }
15
16     // Build and set bookmark model
17     // FIXME: Localize me
18     foreach (Book::Bookmark bookmark, book_->bookmarks()) {
19         QString label("At ");
20         label += QString::number((int)(100 * book_->
21             getProgress(bookmark.part, bookmark.pos))) + "%";
22         if (!bookmark.note.isEmpty()) {
23             label += ": " + bookmark.note;
24         }
25         label += "\n";
26         int chapterIndex = book_->chapterFromPart(bookmark.part);
27         if (chapterIndex != -1) {
28             QString chapterId = book_->chapters[chapterIndex];
29             label += "In \"" + book_->content[chapterId].name + "\"";
30         }
31         data.append(label);
32     }
33     QStringListModel *model = new QStringListModel(data, this);
34     setModel(model);
35
36     addButton(tr("Add bookmark"), this, SLOT(onAdd()), "add");
37     addItemButton(tr("Go to bookmark"), this, SLOT(onGo()), "goto");
38     addItemButton(tr("Edit bookmark"), this, SLOT(onEdit()), "edit");
39     addItemButton(tr("Delete bookmark"), this, SLOT(onDelete()), "delete");
40
41     connect(this, SIGNAL(activated(const QModelIndex &)),
42             this, SLOT(onItemActivated(const QModelIndex &)));
43 }
44
45 void BookmarksDialog::onGo()
46 {
47     TRACE;
48     QModelIndex current = currentItem();
49     if (current.isValid()) {
50         emit goToBookmark(current.row());
51         close();
52     }
53 }
54
55 void BookmarksDialog::onItemActivated(const QModelIndex &index)
56 {
57     TRACE;
58 #ifdef Q_WS_MAEMO_5
59     switch ((new BookmarkInfoDialog(book, index.row(), this))->exec()) {
60     case BookmarkInfoDialog::GoTo:
61         onGo();
62         break;
63     case BookmarkInfoDialog::Delete:
64         reallyDelete();
65         break;
66     default:
67         ;
68     }
69 #else
70     Q_UNUSED(index);
71 #endif
72 }
73
74 void BookmarksDialog::onAdd()
75 {
76     TRACE;
77     bool ok;
78     QString text = QInputDialog::getText(this, tr("Add bookmark"),
79         tr("Note (optional):"), QLineEdit::Normal, QString(), &ok);
80     if (ok) {
81         emit addBookmark(text);
82         close();
83     }
84 }
85
86 void BookmarksDialog::onDelete()
87 {
88     TRACE;
89     if (!currentItem().isValid()) {
90         return;
91     }
92     if (QMessageBox::Yes !=
93         QMessageBox::question(this, tr("Delete bookmark"),
94             tr("Delete bookmark?"), QMessageBox::Yes | QMessageBox::No)) {
95         return;
96     }
97     reallyDelete();
98 }
99
100 void BookmarksDialog::reallyDelete()
101 {
102     TRACE;
103     QModelIndex current = currentItem();
104     if (!current.isValid()) {
105         return;
106     }
107     int row = current.row();
108     model()->removeRow(row);
109     book->deleteBookmark(row);
110 }
111
112 void BookmarksDialog::onEdit()
113 {
114     // FIXME: Implement me
115 }