Get rid of unused Options menu items on Symbian.
[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()), "add");
17
18     // Build bookmark list
19     // FIXME: Localize me
20     foreach (Book::Bookmark bookmark, book_->bookmarks()) {
21         QString label("At ");
22         label += QString::number((int)(100 * book_->
23             getProgress(bookmark.part, bookmark.pos))) + "%";
24         if (!bookmark.note.isEmpty()) {
25             label += ": " + bookmark.note;
26         }
27         label += "\n";
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         data.append(label);
34     }
35
36     // Create bookmark list view
37     QStringListModel *model = new QStringListModel(data, this);
38     list = new ListView;
39     list->setSelectionMode(QAbstractItemView::SingleSelection);
40     list->setModel(model);
41     addList(list);
42     connect(list, SIGNAL(activated(const QModelIndex &)),
43             this, SLOT(onItemActivated(const QModelIndex &)));
44     addList(list);
45 }
46
47 void BookmarksDialog::onGo()
48 {
49     QModelIndex current = list->currentIndex();
50     if (current.isValid()) {
51         emit goToBookmark(current.row());
52         close();
53     }
54 }
55
56 void BookmarksDialog::onItemActivated(const QModelIndex &index)
57 {
58     switch ((new BookmarkInfoDialog(book, index.row(), this))->exec()) {
59     case BookmarkInfoDialog::GoTo:
60         onGo();
61         break;
62     case BookmarkInfoDialog::Delete:
63         onDelete(true);
64         break;
65     default:
66         ;
67     }
68 }
69
70 void BookmarksDialog::onAdd()
71 {
72     bool ok;
73     QString text = QInputDialog::getText(this, tr("Add bookmark"),
74         tr("Note (optional):"), QLineEdit::Normal, QString(), &ok);
75     if (ok) {
76         emit addBookmark(text);
77         close();
78     }
79 }
80
81 void BookmarksDialog::onDelete(bool really)
82 {
83     QModelIndex current = list->currentIndex();
84     if (!current.isValid()) {
85         return;
86     }
87     if (!really) {
88         if (QMessageBox::Yes !=
89             QMessageBox::question(this, tr("Delete bookmark"),
90                 tr("Delete bookmark?"), QMessageBox::Yes | QMessageBox::No)) {
91             return;
92         }
93     }
94     int row = current.row();
95     list->model()->removeRow(row);
96     book->deleteBookmark(row);
97 }