#include "bookmarksdialog.h"
#include "book.h"
#include "bookmarkinfodialog.h"
-#include "listview.h"
+#include "trace.h"
BookmarksDialog::BookmarksDialog(Book *book_, QWidget *parent):
- ListWindow(parent), book(book_)
+ ListWindow(tr("(No bookmarks)\n"), parent), book(book_)
{
setWindowTitle(tr("Bookmarks"));
if (!book) {
return;
}
- addAction(tr("Add bookmark"), this, SLOT(onAdd()), ":/icons/add.png");
-#ifndef Q_WS_MAEMO_5
- addItemAction(tr("Go to"), this, SLOT(onGo()));
- addItemAction(tr("Delete"), this, SLOT(onDelete()));
-#endif // ! Q_WS_MAEMO_5
-
- // Build bookmark list
- // FIXME: Localize me
+ // Build and set bookmark model
foreach (Book::Bookmark bookmark, book_->bookmarks()) {
- QString label("At ");
- label += QString::number((int)(100 * book_->
- getProgress(bookmark.part, bookmark.pos))) + "%";
- int chapterIndex = book_->chapterFromPart(bookmark.part);
- if (chapterIndex != -1) {
- QString chapterId = book_->chapters[chapterIndex];
- label += "\nIn \"" + book_->content[chapterId].name + "\"";
- }
- data.append(label);
+ data.append(bookmarkToText(bookmark));
}
-
- // Create bookmark list view
QStringListModel *model = new QStringListModel(data, this);
- list = new ListView;
- list->setSelectionMode(QAbstractItemView::SingleSelection);
- list->setModel(model);
- addList(list);
- connect(list, SIGNAL(activated(const QModelIndex &)),
+ setModel(model);
+
+ addButton(tr("Add bookmark"), this, SLOT(onAdd()), "add");
+ addItemButton(tr("Go to bookmark"), this, SLOT(onGo()), "goto");
+ addItemButton(tr("Edit bookmark"), this, SLOT(onEdit()), "edit");
+ addItemButton(tr("Delete bookmark"), this, SLOT(onDelete()), "delete");
+
+ connect(this, SIGNAL(activated(const QModelIndex &)),
this, SLOT(onItemActivated(const QModelIndex &)));
- addList(list);
}
void BookmarksDialog::onGo()
{
- QModelIndex current = list->currentIndex();
+ TRACE;
+ QModelIndex current = currentItem();
if (current.isValid()) {
emit goToBookmark(current.row());
close();
void BookmarksDialog::onItemActivated(const QModelIndex &index)
{
+ TRACE;
+#ifdef Q_WS_MAEMO_5
switch ((new BookmarkInfoDialog(book, index.row(), this))->exec()) {
case BookmarkInfoDialog::GoTo:
onGo();
break;
case BookmarkInfoDialog::Delete:
- onDelete(true);
+ reallyDelete();
break;
default:
;
}
+#else
+ Q_UNUSED(index);
+#endif
}
void BookmarksDialog::onAdd()
{
- emit addBookmark();
- close();
+ TRACE;
+ bool ok;
+ QString text = QInputDialog::getText(this, tr("Add bookmark"),
+ tr("Note (optional):"), QLineEdit::Normal, QString(), &ok);
+ if (ok) {
+ emit addBookmark(text);
+ close();
+ }
}
-void BookmarksDialog::onDelete(bool really)
+void BookmarksDialog::onDelete()
{
- QModelIndex current = list->currentIndex();
- if (!current.isValid()) {
+ TRACE;
+ if (!currentItem().isValid()) {
return;
}
- if (!really) {
- if (QMessageBox::Yes !=
- QMessageBox::question(this, tr("Delete bookmark"),
- tr("Delete bookmark?"),
- QMessageBox::Yes | QMessageBox::No)) {
- return;
- }
+ if (QMessageBox::Yes !=
+ QMessageBox::question(this, tr("Delete bookmark"),
+ tr("Delete bookmark?"), QMessageBox::Yes | QMessageBox::No)) {
+ return;
+ }
+ reallyDelete();
+}
+
+void BookmarksDialog::reallyDelete()
+{
+ TRACE;
+ QModelIndex current = currentItem();
+ if (!current.isValid()) {
+ return;
}
int row = current.row();
- list->model()->removeRow(row);
+ model()->removeRow(row);
book->deleteBookmark(row);
}
+
+void BookmarksDialog::onEdit()
+{
+ TRACE;
+ QModelIndex current = currentItem();
+ if (!current.isValid()) {
+ return;
+ }
+ int row = current.row();
+ Book::Bookmark b = book->bookmarks()[row];
+ bool ok;
+ QString text = QInputDialog::getText(this, tr("Edit bookmark"),
+ tr("Note:"), QLineEdit::Normal, b.note, &ok);
+ if (!ok) {
+ return;
+ }
+ b.note = text;
+ book->setBookmarkNote(row, text);
+ QStringListModel *m = qobject_cast<QStringListModel *>(model());
+ if (m) {
+ m->setData(current, bookmarkToText(b), Qt::DisplayRole);
+ }
+}
+
+QString BookmarksDialog::bookmarkToText(const Book::Bookmark &bookmark)
+{
+ // FIXME: Localize me
+ QString label("At ");
+ label += QString::number((int)(100 * book->
+ getProgress(bookmark.part, bookmark.pos))) + "%";
+ if (!bookmark.note.isEmpty()) {
+ label += ": " + bookmark.note;
+ }
+ label += "\n";
+ int chapterIndex = book->chapterFromPart(bookmark.part);
+ if (chapterIndex != -1) {
+ QString chapterId = book->chapters[chapterIndex];
+ label += "In \"" + book->content[chapterId].name + "\"";
+ }
+ return label;
+}