Improve icons. Move some menu items to the toolbar. Don't show buttons in current...
[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 += "\nIn \"" + 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     emit addBookmark();
73     close();
74 }
75
76 void BookmarksDialog::onDelete(bool really)
77 {
78     QModelIndex current = list->currentIndex();
79     if (!current.isValid()) {
80         return;
81     }
82     if (!really) {
83         if (QMessageBox::Yes !=
84             QMessageBox::question(this, tr("Delete bookmark"),
85                 tr("Delete bookmark?"), QMessageBox::Yes | QMessageBox::No)) {
86             return;
87         }
88     }
89     int row = current.row();
90     list->model()->removeRow(row);
91     book->deleteBookmark(row);
92 }