Facelift bookmarks dialog. Add Maemo-friendly dialog box class.
authorAkos Polster <polster@marzipan.pipacs.com>
Sun, 25 Jul 2010 16:46:42 +0000 (18:46 +0200)
committerAkos Polster <polster@marzipan.pipacs.com>
Sun, 25 Jul 2010 16:46:42 +0000 (18:46 +0200)
17 files changed:
book.cpp
book.h
bookmarkinfodialog.cpp [new file with mode: 0644]
bookmarkinfodialog.h [new file with mode: 0644]
bookmarksdialog.cpp
bookmarksdialog.h
dialog.cpp [new file with mode: 0644]
dialog.h [new file with mode: 0644]
dorian.pro
info.cpp [deleted file]
info.h [deleted file]
infodialog.cpp
infodialog.h
mainwindow.cpp
mainwindow.h
pkg/changelog
pkg/version.txt

index 489e087..52727ac 100644 (file)
--- a/book.cpp
+++ b/book.cpp
@@ -289,6 +289,12 @@ void Book::addBookmark(int chapter, qreal position)
     save();
 }
 
+void Book::deleteBookmark(int index)
+{
+    mBookmarks.removeAt(index);
+    save();
+}
+
 QList<Book::Bookmark> Book::bookmarks() const
 {
     return mBookmarks;
diff --git a/book.h b/book.h
index 6449112..048280c 100644 (file)
--- a/book.h
+++ b/book.h
@@ -77,6 +77,9 @@ public:
     /** Add bookmark. */
     void addBookmark(int chapter, qreal position);
 
+    /** Delete bookmark. */
+    void deleteBookmark(int index);
+
     /** List bookmarks. */
     QList<Bookmark> bookmarks() const;
 
diff --git a/bookmarkinfodialog.cpp b/bookmarkinfodialog.cpp
new file mode 100644 (file)
index 0000000..83b6568
--- /dev/null
@@ -0,0 +1,37 @@
+#include <QtGui>
+
+#include "bookmarkinfodialog.h"
+#include "book.h"
+
+BookmarkInfoDialog::BookmarkInfoDialog(Book *b, int i, QWidget *parent):
+    Dialog(parent),
+    book(b),
+    index(i)
+{
+    setWindowTitle(tr("Bookmark Details"));
+
+    Book::Bookmark bookmark = book->bookmarks()[index];
+    QString contentId = book->toc[bookmark.chapter];
+    QString contentTitle = book->content[contentId].name;
+    QLabel *info = new QLabel(contentTitle + "\nAt " +
+        QString::number((int)(bookmark.pos*100)) + "%", this);
+    addWidget(info);
+    addStretch();
+
+    QPushButton *read = new QPushButton(tr("Go to"), this);
+    QPushButton *remove = new QPushButton(tr("Delete"), this);
+    connect(read, SIGNAL(clicked()), this, SLOT(onRead()));
+    connect(remove, SIGNAL(clicked()), this, SLOT(onRemove()));
+    addButton(read, QDialogButtonBox::ActionRole);
+    addButton(remove, QDialogButtonBox::ActionRole);
+}
+
+void BookmarkInfoDialog::onRead()
+{
+    done(GoTo);
+}
+
+void BookmarkInfoDialog::onRemove()
+{
+    done(Delete);
+}
diff --git a/bookmarkinfodialog.h b/bookmarkinfodialog.h
new file mode 100644 (file)
index 0000000..7530db8
--- /dev/null
@@ -0,0 +1,26 @@
+#ifndef BOOKMARKINFODIALOG_H
+#define BOOKMARKINFODIALOG_H
+
+#include "dialog.h"
+
+class Book;
+
+class BookmarkInfoDialog: public Dialog
+{
+    Q_OBJECT
+
+public:
+    enum {GoTo = 1, Delete};
+    explicit BookmarkInfoDialog(Book *b, int i, QWidget *parent);
+
+public slots:
+    void onRead();
+    void onRemove();
+
+protected:
+    Book *book;
+    int index;
+};
+
+
+#endif // BOOKMARKINFODIALOG_H
index 14b3b3e..c0d1f1d 100644 (file)
 
 #include "bookmarksdialog.h"
 #include "book.h"
+#include "bookmarkinfodialog.h"
 
 BookmarksDialog::BookmarksDialog(Book *book_, QWidget *parent):
-    QDialog(parent, Qt::Dialog | Qt::WindowTitleHint |
-            Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint)
+    QMainWindow(parent), book(book_)
 {
-    setWindowTitle(tr("Bookmarks"));
-#ifndef Q_WS_MAEMO_5
-    setSizeGripEnabled(true);
+#ifdef Q_WS_MAEMO_5
+    setAttribute(Qt::WA_Maemo5StackedWindow, true);
 #endif
+    setWindowTitle(tr("Bookmarks"));
+
+    QFrame *frame = new QFrame(this);
+    setCentralWidget(frame);
+    QHBoxLayout *horizontalLayout = new QHBoxLayout(this);
+    frame->setLayout(horizontalLayout);
 
     list = new QListWidget(this);
     list->setSelectionMode(QAbstractItemView::SingleSelection);
     foreach (Book::Bookmark bookmark, book_->bookmarks()) {
         QString contentId = book_->toc[bookmark.chapter];
         QString contentTitle = book_->content[contentId].name;
-        list->addItem(contentTitle + ", at " +
-                      QString::number((int)(bookmark.pos * 100)) + "%");
+        (void)new QListWidgetItem(QIcon(":icons/bookmark.png"), contentTitle +
+            "\nAt " + QString::number((int)(bookmark.pos*100)) + "%", list);
     }
 
-    QHBoxLayout *horizontalLayout = new QHBoxLayout(this);
     horizontalLayout->addWidget(list);
 
-    QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical);
 #ifndef Q_WS_MAEMO_5
+    QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical);
+
     QPushButton *goButton = new QPushButton(tr("Go"), this);
-    buttonBox->addButton(goButton, QDialogButtonBox::AcceptRole);
+    buttonBox->addButton(goButton, QDialogButtonBox::ActionRole);
     connect(goButton, SIGNAL(clicked()), this, SLOT(onGo()));
-#endif
+
+    QPushButton *closeButton = new QPushButton(tr("Close"), this);
+    buttonBox->addButton(closeButton, QDialogButtonBox::AcceptRole);
+    connect(closeButton, SIGNAL(clicked()), this, SLOT(onClose()));
+
     QPushButton *addButton = new QPushButton(tr("Add"), this);
     buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
     connect(addButton, SIGNAL(clicked()), this, SLOT(onAdd()));
 
-    horizontalLayout->addWidget(buttonBox);
+    QPushButton *deleteButton = new QPushButton(tr("Delete"), this);
+    buttonBox->addButton(deleteButton, QDialogButtonBox::DestructiveRole);
+    connect(deleteButton, SIGNAL(clicked()), this, SLOT(onDelete()));
 
-#ifdef Q_WS_MAEMO_5
+    horizontalLayout->addWidget(buttonBox);
+#else
+    QAction *addBookmarkAction = menuBar()->addAction(tr("Add bookmark"));
+    connect(addBookmarkAction, SIGNAL(triggered()), this, SLOT(onAdd()));
+#endif // Q_WS_MAEMO_5
     connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
             this, SLOT(onItemActivated(QListWidgetItem *)));
-#endif
 }
 
 void BookmarksDialog::onGo()
 {
-    if (list->selectedItems().isEmpty()) {
-        return;
+    if (!list->selectedItems().isEmpty()) {
+        QListWidgetItem *item = list->selectedItems()[0];
+        emit goToBookmark(list->row(item));
+        close();
     }
-    QListWidgetItem *item = list->selectedItems()[0];
-    int index = list->row(item) + 1;
-    done(index);
 }
 
 void BookmarksDialog::onItemActivated(QListWidgetItem *item)
 {
-    done(list->row(item) + 1);
+    switch ((new BookmarkInfoDialog(book, list->row(item), this))->exec()) {
+    case BookmarkInfoDialog::GoTo:
+        onGo();
+        break;
+    case BookmarkInfoDialog::Delete:
+        onDelete();
+        break;
+    default:
+        ;
+    }
 }
 
 void BookmarksDialog::onAdd()
 {
-    done(-1);
+    emit addBookmark();
+    close();
+}
+
+void BookmarksDialog::onClose()
+{
+    close();
+}
+
+void BookmarksDialog::onDelete()
+{
+    if (!list->selectedItems().isEmpty()) {
+        QListWidgetItem *item = list->selectedItems()[0];
+        int row = list->row(item);
+        book->deleteBookmark(row);
+        delete item;
+    }
+}
+
+void BookmarksDialog::closeEvent(QCloseEvent *event)
+{
+#ifdef Q_WS_MAEMO_5
+    menuBar()->clear();
+#endif
+    event->accept();
 }
index 9d07e47..6bfab27 100644 (file)
@@ -1,14 +1,15 @@
 #ifndef BOOKMARKSDIALOG_H
 #define BOOKMARKSDIALOG_H
 
-#include <QDialog>
+#include <QMainWindow>
 
+class QCloseEvent;
 class Book;
 class QListWidget;
 class QListWidgetItem;
 
 /** Dialog box managing bookmarks. */
-class BookmarksDialog: public QDialog
+class BookmarksDialog: public QMainWindow
 {
     Q_OBJECT
 
@@ -16,13 +17,18 @@ public:
     explicit BookmarksDialog(Book *book, QWidget *parent = 0);
 
 signals:
+    void goToBookmark(int index);
+    void addBookmark();
 
 public slots:
     void onGo();
     void onAdd();
     void onItemActivated(QListWidgetItem *);
+    void onClose();
+    void onDelete();
 
 protected:
+    void closeEvent(QCloseEvent *e);
     Book *book;
     QListWidget *list;
 };
diff --git a/dialog.cpp b/dialog.cpp
new file mode 100644 (file)
index 0000000..e148ea6
--- /dev/null
@@ -0,0 +1,49 @@
+#include <QtGui>
+
+#include "dialog.h"
+
+Dialog::Dialog(QWidget *parent) :
+    QDialog(parent, Qt::Dialog | Qt::WindowTitleHint |
+                    Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint)
+{
+    scroller = new QScrollArea(this);
+
+#ifdef Q_WS_MAEMO_5
+    scroller->setProperty("FingerScrollable", true);
+    scroller->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+#else
+    scroller->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
+#endif
+    scroller->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+    scroller->setFrameStyle(QFrame::NoFrame);
+
+    content = new QWidget(scroller);
+    contentLayout = new QVBoxLayout(content);
+    contentLayout->setMargin(0);
+    content->setLayout(contentLayout);
+
+    buttonBox = new QDialogButtonBox(Qt::Vertical, this);
+    QHBoxLayout *horizontalLayout = new QHBoxLayout(this);
+    horizontalLayout->addWidget(scroller);
+    horizontalLayout->addWidget(buttonBox);
+    setLayout(horizontalLayout);
+
+    scroller->setWidget(content);
+    content->show();
+    scroller->setWidgetResizable(true);
+}
+
+void Dialog::addWidget(QWidget *widget)
+{
+    contentLayout->addWidget(widget);
+}
+
+void Dialog::addStretch(int stretch)
+{
+    contentLayout->addStretch(stretch);
+}
+
+void Dialog::addButton(QPushButton *button, QDialogButtonBox::ButtonRole role)
+{
+    buttonBox->addButton(button, role);
+}
diff --git a/dialog.h b/dialog.h
new file mode 100644 (file)
index 0000000..6f7a160
--- /dev/null
+++ b/dialog.h
@@ -0,0 +1,37 @@
+#ifndef DIALOG_H
+#define DIALOG_H
+
+#include <QDialog>
+#include <QDialogButtonBox>
+
+class QWidget;
+class QPushButton;
+class QScrollArea;
+class QVBoxLayout;
+
+/** Maemo- and Mac-friendly dialog box. */
+class Dialog: public QDialog
+{
+    Q_OBJECT
+
+public:
+    explicit Dialog(QWidget *parent = 0);
+
+    /** Add widget to the scrollable content pane. */
+    void addWidget(QWidget *widget);
+
+    /** Add stretch to the scrollable content pane. */
+    void addStretch(int stretch = 0);
+
+    /** Add button to the dialog button box. */
+    void addButton(QPushButton *button,
+        QDialogButtonBox::ButtonRole role = QDialogButtonBox::AcceptRole);
+
+protected:
+    QScrollArea *scroller;
+    QWidget *content;
+    QVBoxLayout *contentLayout;
+    QDialogButtonBox *buttonBox;
+};
+
+#endif // DIALOG_H
index 0caa0af..18cd168 100644 (file)
@@ -9,7 +9,6 @@ SOURCES += \
     extractzip.cpp \
     library.cpp \
     book.cpp \
-    info.cpp \
     librarydialog.cpp \
     devtools.cpp \
     infodialog.cpp \
@@ -17,7 +16,9 @@ SOURCES += \
     settingswindow.cpp \
     settings.cpp \
     bookmarksdialog.cpp \
-    sortedlibrary.cpp
+    sortedlibrary.cpp \
+    bookmarkinfodialog.cpp \
+    dialog.cpp
 
 HEADERS += \
     mainwindow.h \
@@ -29,7 +30,6 @@ HEADERS += \
     extractzip.h \
     library.h \
     book.h \
-    info.h \
     librarydialog.h \
     devtools.h \
     infodialog.h \
@@ -40,7 +40,9 @@ HEADERS += \
     xmlerrorhandler.h \
     containerhandler.h \
     sortedlibrary.h \
-    ncxhandler.h
+    ncxhandler.h \
+    bookmarkinfodialog.h \
+    dialog.h
 
 RESOURCES += \
     dorian.qrc
diff --git a/info.cpp b/info.cpp
deleted file mode 100644 (file)
index 2c0e123..0000000
--- a/info.cpp
+++ /dev/null
@@ -1,60 +0,0 @@
-#include <QtGui>
-#include <QDebug>
-
-#include "info.h"
-#include "book.h"
-
-Info::Info(Book *book, QWidget *parent): QScrollArea(parent)
-{
-#ifdef Q_WS_MAEMO_5
-    setProperty("FingerScrollable", true);
-    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
-#else
-    setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
-#endif
-    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
-    setFrameStyle(QFrame::NoFrame);
-
-    QWidget *contents = new QWidget(this);
-    QVBoxLayout *layout = new QVBoxLayout(contents);
-    layout->setMargin(0);
-    contents->setLayout(layout);
-
-    if (book) {
-        QLabel *title = new QLabel(book->title, contents);
-        layout->addWidget(title);
-        if (book->subject != "") {
-            QLabel *subject = new QLabel(book->subject, contents);
-            layout->addWidget(subject);
-        }
-        if (book->creators.size()) {
-            QLabel *creators = new QLabel(contents);
-            QString c = "By " + book->creators[0];
-            for (int i = 1; i < book->creators.size(); i++) {
-                c += ", " + book->creators[i];
-            }
-            creators->setText(c);
-            layout->addWidget(creators);
-        }
-        QLabel *path = new QLabel("File: " + book->path(), contents);
-        layout->addWidget(path);
-        if (book->publisher != "") {
-            QLabel *publisher =
-                    new QLabel("Published by " + book->publisher, contents);
-            layout->addWidget(publisher);
-        }
-        if (book->source != "") {
-            QLabel *source = new QLabel("Source: " + book->source, contents);
-            layout->addWidget(source);
-        }
-        if (book->rights != "") {
-            QLabel *rights = new QLabel(book->rights, contents);
-            layout->addWidget(rights);
-        }
-    }
-
-    layout->addStretch();
-    setWidget(contents);
-    contents->show();
-    setWidgetResizable(true);
-}
diff --git a/info.h b/info.h
deleted file mode 100644 (file)
index 7a17914..0000000
--- a/info.h
+++ /dev/null
@@ -1,22 +0,0 @@
-#ifndef INFO_H
-#define INFO_H
-
-#include <QScrollArea>
-
-class QLabel;
-class Book;
-
-class Info: public QScrollArea
-{
-    Q_OBJECT
-
-public:
-    explicit Info(Book *book, QWidget *parent = 0);
-
-private:
-    QLabel *title;
-    QLabel *path;
-    QLabel *creators;
-};
-
-#endif // INFO_H
index 7deb3c5..202d906 100644 (file)
@@ -1,29 +1,53 @@
 #include <QtGui>
 
 #include "infodialog.h"
-#include "info.h"
 #include "book.h"
 #include "library.h"
 
-InfoDialog::InfoDialog(Book *book_, QWidget *parent):
-        QDialog(parent, Qt::Dialog | Qt::WindowTitleHint |
-                Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint),
-        book(book_)
+InfoDialog::InfoDialog(Book *b, QWidget *parent): Dialog(parent), book(b)
 {
     setWindowTitle(tr("Book Details"));
-    Info *info = new Info(book);
-    QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical, this);
+
+    if (book) {
+        QLabel *title = new QLabel(book->title, this);
+        addWidget(title);
+        if (book->subject != "") {
+            QLabel *subject = new QLabel(book->subject, this);
+            addWidget(subject);
+        }
+        if (book->creators.size()) {
+            QLabel *creators = new QLabel(this);
+            QString c = "By " + book->creators[0];
+            for (int i = 1; i < book->creators.size(); i++) {
+                c += ", " + book->creators[i];
+            }
+            creators->setText(c);
+            addWidget(creators);
+        }
+        QLabel *path = new QLabel("File: " + book->path(), this);
+        addWidget(path);
+        if (book->publisher != "") {
+            QLabel *publisher =
+                    new QLabel("Published by " + book->publisher, this);
+            addWidget(publisher);
+        }
+        if (book->source != "") {
+            QLabel *source = new QLabel("Source: " + book->source, this);
+            addWidget(source);
+        }
+        if (book->rights != "") {
+            QLabel *rights = new QLabel(book->rights, this);
+            addWidget(rights);
+        }
+        addStretch();
+    }
+
     QPushButton *read = new QPushButton(tr("Read"), this);
     QPushButton *remove = new QPushButton(tr("Delete"), this);
     connect(read, SIGNAL(clicked()), this, SLOT(onReadBook()));
     connect(remove, SIGNAL(clicked()), this, SLOT(onRemoveBook()));
-    buttonBox->addButton(read, QDialogButtonBox::ActionRole);
-    buttonBox->addButton(remove, QDialogButtonBox::ActionRole);
-
-    QHBoxLayout *horizontalLayout = new QHBoxLayout(this);
-    horizontalLayout->addWidget(info);
-    horizontalLayout->addWidget(buttonBox);
-    setLayout(horizontalLayout);
+    addButton(read, QDialogButtonBox::ActionRole);
+    addButton(remove, QDialogButtonBox::ActionRole);
 }
 
 void InfoDialog::onReadBook()
index e76c9c7..a3f4b92 100644 (file)
@@ -1,11 +1,12 @@
 #ifndef INFODIALOG_H
 #define INFODIALOG_H
 
-#include <QDialog>
+#include "dialog.h"
 
+class QWidget;
 class Book;
 
-class InfoDialog: public QDialog
+class InfoDialog: public Dialog
 {
     Q_OBJECT
 
index ca97ee2..4dc9466 100755 (executable)
@@ -202,14 +202,11 @@ void MainWindow::showBookmarks()
     Book *book = Library::instance()->book(mCurrent);
     if (book) {
         BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
-        int ret = bookmarks->exec();
-        if (ret > 0) {
-            int index = ret - 1;
-            view->goToBookmark(book->bookmarks()[index]);
-        }
-        else if (ret < 0) {
-            view->addBookmark();
-        }
+        bookmarks->setWindowModality(Qt::WindowModal);
+        connect(bookmarks, SIGNAL(addBookmark()), this, SLOT(onAddBookmark()));
+        connect(bookmarks, SIGNAL(goToBookmark(int)),
+                this, SLOT(onGoToBookmark(int)));
+        bookmarks->show();
     }
 }
 
@@ -291,3 +288,14 @@ void MainWindow::onChapterLoadEnd(int index)
     previousAction->setEnabled(enablePrevious);
     nextAction->setEnabled(enableNext);
 }
+
+void MainWindow::onAddBookmark()
+{
+    view->addBookmark();
+}
+
+void MainWindow::onGoToBookmark(int index)
+{
+    Book *book = Library::instance()->book(mCurrent);
+    view->goToBookmark(book->bookmarks()[index]);
+}
index 628fd18..3671cb2 100755 (executable)
@@ -30,6 +30,8 @@ public slots:
     void onSettingsChanged(const QString &key);
     void onChapterLoadStart();
     void onChapterLoadEnd(int index);
+    void onAddBookmark();
+    void onGoToBookmark(int index);
 
 protected:
 #ifdef Q_WS_MAEMO5
index 4f46599..4bcbc3f 100644 (file)
@@ -1,3 +1,10 @@
+dorian (0.0.11-1) unstable; urgency=low
+
+  * Facelift bookmark manager
+  * Make Maemo friendly dialog box class
+
+ -- Akos Polster <akos@pipacs.com>  Sun, 25 Jul 2010 20:00:00 +0200
+
 dorian (0.0.10-1) unstable; urgency=low
 
   * Turn library into proper model
@@ -8,7 +15,7 @@ dorian (0.0.10-1) unstable; urgency=low
   * Read chapter titles from NCX directory
   * Display chapter titles for bookmarks
 
- -- Akos Polster <akos@pipacs.com>  Fri, 16 Jul 2010 20:00:00 +0200
+ -- Akos Polster <akos@pipacs.com>  Sat, 24 Jul 2010 20:00:00 +0200
 
 dorian (0.0.9-1) unstable; urgency=low
 
index 7c1886b..2cfabea 100644 (file)
@@ -1 +1 @@
-0.0.10
+0.0.11