Updated library dialog works.
[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)"), 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     // FIXME
39     // connect(list, SIGNAL(activated(const QModelIndex &)),
40     //         this, SLOT(onItemActivated(const QModelIndex &)));
41 }
42
43 void BookmarksDialog::onGo()
44 {
45     TRACE;
46     // FIXME
47     // QModelIndex current = list->currentIndex();
48     // if (current.isValid()) {
49     //     emit goToBookmark(current.row());
50     //     close();
51     // }
52 }
53
54 void BookmarksDialog::onItemActivated(const QModelIndex &index)
55 {
56     switch ((new BookmarkInfoDialog(book, index.row(), this))->exec()) {
57     case BookmarkInfoDialog::GoTo:
58         onGo();
59         break;
60     case BookmarkInfoDialog::Delete:
61         onDelete(true);
62         break;
63     default:
64         ;
65     }
66 }
67
68 void BookmarksDialog::onAdd()
69 {
70     bool ok;
71     QString text = QInputDialog::getText(this, tr("Add bookmark"),
72         tr("Note (optional):"), QLineEdit::Normal, QString(), &ok);
73     if (ok) {
74         emit addBookmark(text);
75         close();
76     }
77 }
78
79 void BookmarksDialog::onDelete(bool really)
80 {
81 #if 0
82     // FIXME
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 #endif
98 }