Re-design ListWindow.
[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(parent), book(book_)
10 {
11     setWindowTitle(tr("Bookmarks"));
12     if (!book) {
13         return;
14     }
15
16     addButton(tr("Add bookmark"), this, SLOT(onAdd()), "add");
17
18     // Build and set bookmark model
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     QStringListModel *model = new QStringListModel(data, this);
36     setModel(model);
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     QModelIndex current = list->currentIndex();
82     if (!current.isValid()) {
83         return;
84     }
85     if (!really) {
86         if (QMessageBox::Yes !=
87             QMessageBox::question(this, tr("Delete bookmark"),
88                 tr("Delete bookmark?"), QMessageBox::Yes | QMessageBox::No)) {
89             return;
90         }
91     }
92     int row = current.row();
93     list->model()->removeRow(row);
94     book->deleteBookmark(row);
95 }