Add tool bars to list windows on Symbian.
[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("Delete bookmark"), this, SLOT(onDelete()), "remove");
38
39     connect(this, SIGNAL(activated(const QModelIndex &)),
40             this, SLOT(onItemActivated(const QModelIndex &)));
41 }
42
43 void BookmarksDialog::onGo()
44 {
45     TRACE;
46     QModelIndex current = currentItem();
47     if (current.isValid()) {
48         emit goToBookmark(current.row());
49         close();
50     }
51 }
52
53 void BookmarksDialog::onItemActivated(const QModelIndex &index)
54 {
55     switch ((new BookmarkInfoDialog(book, index.row(), this))->exec()) {
56     case BookmarkInfoDialog::GoTo:
57         onGo();
58         break;
59     case BookmarkInfoDialog::Delete:
60         onDelete(true);
61         break;
62     default:
63         ;
64     }
65 }
66
67 void BookmarksDialog::onAdd()
68 {
69     bool ok;
70     QString text = QInputDialog::getText(this, tr("Add bookmark"),
71         tr("Note (optional):"), QLineEdit::Normal, QString(), &ok);
72     if (ok) {
73         emit addBookmark(text);
74         close();
75     }
76 }
77
78 void BookmarksDialog::onDelete(bool really)
79 {
80     QModelIndex current = currentItem();
81     if (!current.isValid()) {
82         return;
83     }
84     if (!really) {
85         if (QMessageBox::Yes !=
86             QMessageBox::question(this, tr("Delete bookmark"),
87                 tr("Delete bookmark?"), QMessageBox::Yes | QMessageBox::No)) {
88             return;
89         }
90     }
91     int row = current.row();
92     model()->removeRow(row);
93     book->deleteBookmark(row);
94 }