Parse NCX directory for chapter titles. Show chapter titles for bookmarks.
[dorian] / bookmarksdialog.cpp
1 #include <QtGui>
2
3 #include "bookmarksdialog.h"
4 #include "book.h"
5
6 BookmarksDialog::BookmarksDialog(Book *book_, QWidget *parent):
7     QDialog(parent, Qt::Dialog | Qt::WindowTitleHint |
8             Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint)
9 {
10     setWindowTitle(tr("Bookmarks"));
11 #ifndef Q_WS_MAEMO_5
12     setSizeGripEnabled(true);
13 #endif
14
15     list = new QListWidget(this);
16     list->setSelectionMode(QAbstractItemView::SingleSelection);
17     foreach (Book::Bookmark bookmark, book_->bookmarks()) {
18         QString contentId = book_->toc[bookmark.chapter];
19         QString contentTitle = book_->content[contentId].name;
20         list->addItem(contentTitle + ", at " +
21                       QString::number((int)(bookmark.pos * 100)) + "%");
22     }
23
24     QHBoxLayout *horizontalLayout = new QHBoxLayout(this);
25     horizontalLayout->addWidget(list);
26
27     QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical);
28 #ifndef Q_WS_MAEMO_5
29     QPushButton *goButton = new QPushButton(tr("Go"), this);
30     buttonBox->addButton(goButton, QDialogButtonBox::AcceptRole);
31     connect(goButton, SIGNAL(clicked()), this, SLOT(onGo()));
32 #endif
33     QPushButton *addButton = new QPushButton(tr("Add"), this);
34     buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
35     connect(addButton, SIGNAL(clicked()), this, SLOT(onAdd()));
36
37     horizontalLayout->addWidget(buttonBox);
38
39 #ifdef Q_WS_MAEMO_5
40     connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
41             this, SLOT(onItemActivated(QListWidgetItem *)));
42 #endif
43 }
44
45 void BookmarksDialog::onGo()
46 {
47     if (list->selectedItems().isEmpty()) {
48         return;
49     }
50     QListWidgetItem *item = list->selectedItems()[0];
51     int index = list->row(item) + 1;
52     done(index);
53 }
54
55 void BookmarksDialog::onItemActivated(QListWidgetItem *item)
56 {
57     done(list->row(item) + 1);
58 }
59
60 void BookmarksDialog::onAdd()
61 {
62     done(-1);
63 }