Show reading progress.
[dorian] / bookmarksdialog.cpp
1 #include <QtGui>
2
3 #include "bookmarksdialog.h"
4 #include "book.h"
5 #include "bookmarkinfodialog.h"
6
7 BookmarksDialog::BookmarksDialog(Book *book_, QWidget *parent):
8     QMainWindow(parent), book(book_)
9 {
10 #ifdef Q_WS_MAEMO_5
11     setAttribute(Qt::WA_Maemo5StackedWindow, true);
12 #endif
13     setWindowTitle(tr("Bookmarks"));
14
15     QFrame *frame = new QFrame(this);
16     setCentralWidget(frame);
17     QHBoxLayout *horizontalLayout = new QHBoxLayout(frame);
18     frame->setLayout(horizontalLayout);
19
20     list = new QListWidget(this);
21     list->setSelectionMode(QAbstractItemView::SingleSelection);
22     foreach (Book::Bookmark bookmark, book_->bookmarks()) {
23         QString contentId = book_->parts[bookmark.part];
24         QString contentTitle = book_->content[contentId].name;
25         (void)new QListWidgetItem(QIcon(":icons/bookmark.png"), contentTitle +
26             "\nAt " + QString::number((int)(bookmark.pos*100)) + "%", list);
27     }
28
29     horizontalLayout->addWidget(list);
30
31 #ifndef Q_WS_MAEMO_5
32     QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical);
33
34     QPushButton *goButton = new QPushButton(tr("Go to"), this);
35     buttonBox->addButton(goButton, QDialogButtonBox::ActionRole);
36     connect(goButton, SIGNAL(clicked()), this, SLOT(onGo()));
37
38     QPushButton *closeButton = buttonBox->addButton(QDialogButtonBox::Close);
39     connect(closeButton, SIGNAL(clicked()), this, SLOT(onClose()));
40
41     QPushButton *addButton = new QPushButton(tr("Add"), this);
42     buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
43     connect(addButton, SIGNAL(clicked()), this, SLOT(onAdd()));
44
45     QPushButton *deleteButton = new QPushButton(tr("Delete"), this);
46     buttonBox->addButton(deleteButton, QDialogButtonBox::DestructiveRole);
47     connect(deleteButton, SIGNAL(clicked()), this, SLOT(onDelete()));
48
49     horizontalLayout->addWidget(buttonBox);
50 #else
51     QAction *addBookmarkAction = menuBar()->addAction(tr("Add bookmark"));
52     connect(addBookmarkAction, SIGNAL(triggered()), this, SLOT(onAdd()));
53 #endif // Q_WS_MAEMO_5
54     connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
55             this, SLOT(onItemActivated(QListWidgetItem *)));
56 }
57
58 void BookmarksDialog::onGo()
59 {
60     if (!list->selectedItems().isEmpty()) {
61         QListWidgetItem *item = list->selectedItems()[0];
62         emit goToBookmark(list->row(item));
63         close();
64     }
65 }
66
67 void BookmarksDialog::onItemActivated(QListWidgetItem *item)
68 {
69     switch ((new BookmarkInfoDialog(book, list->row(item), this))->exec()) {
70     case BookmarkInfoDialog::GoTo:
71         onGo();
72         break;
73     case BookmarkInfoDialog::Delete:
74         onDelete(true);
75         break;
76     default:
77         ;
78     }
79 }
80
81 void BookmarksDialog::onAdd()
82 {
83     emit addBookmark();
84     close();
85 }
86
87 void BookmarksDialog::onClose()
88 {
89     close();
90 }
91
92 void BookmarksDialog::onDelete(bool really)
93 {
94     if (list->selectedItems().isEmpty()) {
95         return;
96     }
97     if (!really) {
98         if (QMessageBox::Yes !=
99             QMessageBox::question(this, tr("Delete bookmark"),
100                                   tr("Delete bookmark?"),
101                                   QMessageBox::Yes | QMessageBox::No)) {
102             return;
103         }
104     }
105     QListWidgetItem *item = list->selectedItems()[0];
106     int row = list->row(item);
107     book->deleteBookmark(row);
108     delete item;
109 }
110
111 void BookmarksDialog::closeEvent(QCloseEvent *event)
112 {
113 #ifdef Q_WS_MAEMO_5
114     menuBar()->clear();
115 #endif
116     event->accept();
117 }