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