3 #include "bookmarksdialog.h"
5 #include "bookmarkinfodialog.h"
8 BookmarksDialog::BookmarksDialog(Book *book_, QWidget *parent):
9 ListWindow(tr("(No bookmarks)\n"), parent), book(book_)
11 setWindowTitle(tr("Bookmarks"));
16 // Build and set bookmark model
17 foreach (Book::Bookmark bookmark, book_->bookmarks()) {
18 data.append(bookmarkToText(bookmark));
20 QStringListModel *model = new QStringListModel(data, this);
23 addButton(tr("Add bookmark"), this, SLOT(onAdd()), "add");
24 addItemButton(tr("Go to bookmark"), this, SLOT(onGo()), "goto");
25 addItemButton(tr("Edit bookmark"), this, SLOT(onEdit()), "edit");
26 addItemButton(tr("Delete bookmark"), this, SLOT(onDelete()), "delete");
28 connect(this, SIGNAL(activated(const QModelIndex &)),
29 this, SLOT(onItemActivated(const QModelIndex &)));
32 void BookmarksDialog::onGo()
35 QModelIndex current = currentItem();
36 if (current.isValid()) {
37 emit goToBookmark(current.row());
42 void BookmarksDialog::onItemActivated(const QModelIndex &index)
46 switch ((new BookmarkInfoDialog(book, index.row(), this))->exec()) {
47 case BookmarkInfoDialog::GoTo:
50 case BookmarkInfoDialog::Delete:
61 void BookmarksDialog::onAdd()
65 QString text = QInputDialog::getText(this, tr("Add bookmark"),
66 tr("Note (optional):"), QLineEdit::Normal, QString(), &ok);
68 emit addBookmark(text);
73 void BookmarksDialog::onDelete()
76 if (!currentItem().isValid()) {
79 if (QMessageBox::Yes !=
80 QMessageBox::question(this, tr("Delete bookmark"),
81 tr("Delete bookmark?"), QMessageBox::Yes | QMessageBox::No)) {
87 void BookmarksDialog::reallyDelete()
90 QModelIndex current = currentItem();
91 if (!current.isValid()) {
94 int row = current.row();
95 model()->removeRow(row);
96 book->deleteBookmark(row);
99 void BookmarksDialog::onEdit()
102 QModelIndex current = currentItem();
103 if (!current.isValid()) {
106 int row = current.row();
107 Book::Bookmark b = book->bookmarks()[row];
109 QString text = QInputDialog::getText(this, tr("Edit bookmark"),
110 tr("Note:"), QLineEdit::Normal, b.note, &ok);
115 book->setBookmarkNote(row, text);
116 QStringListModel *m = qobject_cast<QStringListModel *>(model());
118 m->setData(current, bookmarkToText(b), Qt::DisplayRole);
122 QString BookmarksDialog::bookmarkToText(const Book::Bookmark &bookmark)
124 // FIXME: Localize me
125 QString label("At ");
126 label += QString::number((int)(100 * book->
127 getProgress(bookmark.part, bookmark.pos))) + "%";
128 if (!bookmark.note.isEmpty()) {
129 label += ": " + bookmark.note;
132 int chapterIndex = book->chapterFromPart(bookmark.part);
133 if (chapterIndex != -1) {
134 QString chapterId = book->chapters[chapterIndex];
135 label += "In \"" + book->content[chapterId].name + "\"";