Clean up book view decorations code.
authorAkos Polster <akos@pipacs.com>
Sun, 28 Nov 2010 01:50:02 +0000 (02:50 +0100)
committerAkos Polster <akos@pipacs.com>
Sun, 28 Nov 2010 01:50:02 +0000 (02:50 +0100)
adopterwindow.cpp
adopterwindow.h
fullscreenwindow.cpp
fullscreenwindow.h
mainwindow.cpp
mainwindow.h
platform.cpp

index d5bc82b..3299af1 100644 (file)
 #include "bookview.h"
 #include "platform.h"
 #include "settings.h"
+#include "progress.h"
+#include "translucentbutton.h"
 
 AdopterWindow::AdopterWindow(QWidget *parent):
-    QMainWindow(parent), bookView(0), grabbingVolumeKeys(false), toolBar(0)
+    QMainWindow(parent), bookView(0), grabbingVolumeKeys(false), toolBar(0),
+    progress(0), previousButton(0), nextButton(0)
 {
     TRACE;
 
@@ -51,40 +54,48 @@ AdopterWindow::AdopterWindow(QWidget *parent):
             this, SLOT(onSettingsChanged(const QString &)));
 }
 
-void AdopterWindow::takeChildren(BookView *view, const QList<QWidget *> &others)
+void AdopterWindow::takeBookView(BookView *view,
+                                 Progress *prog,
+                                 TranslucentButton *previous,
+                                 TranslucentButton *next)
 {
     TRACE;
-    leaveChildren();
-    if (view) {
-        bookView = view;
-        bookView->setParent(this);
-        bookView->show();
-        QVBoxLayout *layout =
-                qobject_cast<QVBoxLayout *>(centralWidget()->layout());
-        layout->addWidget(bookView, 1);
-    }
-    foreach (QWidget *child, others) {
-        if (child) {
-            child->setParent(this);
-        }
-    }
+
+    Q_ASSERT(view);
+    Q_ASSERT(prog);
+    Q_ASSERT(previous);
+    Q_ASSERT(next);
+
+    leaveBookView();
+
+    bookView = view;
+    bookView->setParent(this);
+    bookView->show();
+    centralWidget()->layout()->addWidget(bookView);
+
+    progress = prog;
+    previousButton = previous;
+    nextButton = next;
+    progress->setParent(this);
+    previousButton->setParent(this);
+    nextButton->setParent(this);
 }
 
-void AdopterWindow::leaveChildren()
+void AdopterWindow::leaveBookView()
 {
     TRACE;
     if (bookView) {
         centralWidget()->layout()->removeWidget(bookView);
-        bookView = 0;
     }
+    bookView = 0;
+    progress = 0;
+    nextButton = 0;
+    previousButton = 0;
 }
 
-bool AdopterWindow::hasChild(QWidget *child)
+bool AdopterWindow::hasBookView()
 {
-    if (child == bookView) {
-        return true;
-    }
-    return this == child->parent();
+    return bookView != 0;
 }
 
 void AdopterWindow::show()
@@ -114,7 +125,19 @@ QAction *AdopterWindow::addToolBarAction(QObject *receiver,
     action = toolBar->addAction(QIcon(Platform::instance()->icon(iconName)),
                                 text, receiver, member);
 #else
-    if (toolBar && important) {
+    if (!toolBar && important) {
+        // Create tool bar if needed
+        toolBar = new QToolBar("", this);
+        // toolBar->setFixedWidth(QApplication::desktop()->
+        //                        availableGeometry().width());
+        toolBar->setFixedHeight(65);
+        toolBar->setStyleSheet("margin:0; border:0; padding:0");
+        toolBar->setSizePolicy(QSizePolicy::MinimumExpanding,
+                               QSizePolicy::Maximum);
+        addToolBar(Qt::BottomToolBarArea, toolBar);
+    }
+    if (important) {
+        // Add tool bar action
         QPushButton *button = new QPushButton(this);
         button->setIconSize(QSize(60, 60));
         button->setFixedSize(89, 60);
@@ -124,6 +147,7 @@ QAction *AdopterWindow::addToolBarAction(QObject *receiver,
         connect(button, SIGNAL(clicked()), receiver, member);
         toolBar->addWidget(button);
     }
+    // Add menu action, too
     action = new QAction(text, this);
     menuBar()->addAction(action);
     connect(action, SIGNAL(triggered()), receiver, member);
@@ -187,16 +211,37 @@ void AdopterWindow::doGrabVolumeKeys(bool grab)
 
 #endif // Q_WS_MAEMO_5
 
-#ifdef Q_WS_MAEMO_5
-
 void AdopterWindow::showEvent(QShowEvent *e)
 {
-    TRACE;
+    Trace t("AdopterWindow::showEvent");
+
+#if defined(Q_WS_MAEMO_5)
     doGrabVolumeKeys(grabbingVolumeKeys);
+#endif // Q_WS_MAEMO_5
     QMainWindow::showEvent(e);
 }
 
-#endif // Q_WS_MAEMO_5
+void AdopterWindow::resizeEvent(QResizeEvent *event)
+{
+    Trace t("AdopterWindow::resizeEvent");
+
+#ifdef Q_OS_SYMBIAN
+    if (toolBar) {
+        if (portrait()) {
+            qDebug() << "Show tool bar";
+            toolBar->setVisible(true);
+        } else {
+            qDebug() << "Hide tool bar";
+            toolBar->setVisible(false);
+        }
+    }
+#endif // Q_OS_SYMBIAN
+
+    if (hasBookView()) {
+        QTimer::singleShot(100, this, SLOT(placeDecorations()));
+    }
+    QMainWindow::resizeEvent(event);
+}
 
 void AdopterWindow::keyPressEvent(QKeyEvent *event)
 {
@@ -239,3 +284,52 @@ void AdopterWindow::onSettingsChanged(const QString &key)
     }
 }
 
+bool AdopterWindow::portrait()
+{
+    QRect geometry = QApplication::desktop()->geometry();
+    return geometry.width() < geometry.height();
+}
+
+void AdopterWindow::placeDecorations()
+{
+    Trace t("AdopterWindow::placeDecorations");
+
+    if (!hasBookView()) {
+        return;
+    }
+
+    int toolBarHeight = 0;
+
+    QRect geo = bookView->geometry();
+    qDebug() << "bookView:" << geo;
+
+#ifdef Q_OS_SYMBIAN
+    // Work around Symbian bug: If tool bar is hidden, increase bottom
+    // decorator widgets' Y coordinates by the tool bar's height
+    if (!portrait() && toolBar) {
+        toolBarHeight = toolBar->height();
+    }
+
+    // Work around another Symbian bug: When returning from full screen mode
+    // in landscape, the book view widget's height is miscalculated.
+    // My apologies for this kludge
+    if (geo.height() == 288) {
+        qDebug() << "Adjusting bottom Y";
+        toolBarHeight -= 288 - 223;
+    }
+#endif // Q_OS_SYMBIAN
+
+    progress->setGeometry(geo.x(),
+        geo.y() + geo.height() - progress->thickness() + toolBarHeight,
+        geo.width(), progress->thickness());
+    previousButton->setGeometry(geo.x(),
+        geo.y() + geo.height() - TranslucentButton::pixels + toolBarHeight,
+        TranslucentButton::pixels, TranslucentButton::pixels);
+    nextButton->setGeometry(
+        geo.x() + geo.width() - TranslucentButton::pixels,
+        geo.y(), TranslucentButton::pixels, TranslucentButton::pixels);
+    progress->flash();
+    previousButton->flash();
+    nextButton->flash();
+    qDebug() << "progress:" << progress->geometry();
+}
index d4631f4..eb30d53 100644 (file)
@@ -9,6 +9,8 @@ class QToolBar;
 class QAction;
 class BookView;
 class QVBoxLayout;
+class Progress;
+class TranslucentButton;
 
 /**
  * A toplevel window that can adopt a BookView and other children.
@@ -21,14 +23,15 @@ class AdopterWindow: public QMainWindow
 public:
     explicit AdopterWindow(QWidget *parent = 0);
 
-    /** Adopt children "bookView" and "others". */
-    void takeChildren(BookView *bookView, const QList<QWidget *> &others);
+    /** Adopt book view and decorations. */
+    void takeBookView(BookView *bookView, Progress *prog,
+                      TranslucentButton *prev, TranslucentButton *next);
 
-    /** Release current children (adopted in @see takeChildren). */
-    void leaveChildren();
+    /** Release book view and decorations. */
+    void leaveBookView();
 
-    /** Return true if a child is currently adopted. */
-    bool hasChild(QWidget *child);
+    /** Return true if the book view is currently adopted. */
+    bool hasBookView();
 
     /**
      * Add action that is visible on the tool bar.
@@ -50,7 +53,7 @@ public:
     /** Show window. */
     void show();
 
-    /** If grab is true, volume keys will generate pageUp/Down key events. */
+    /** If grab is true, volume keys will navigate the book view. */
     void grabVolumeKeys(bool grab);
 
 public slots:
@@ -58,14 +61,36 @@ public slots:
     void onSettingsChanged(const QString &key);
 
 protected:
+    /** Return true, if we are in portrait mode. */
+    bool portrait();
+
+    /** Handle key press events. */
     void keyPressEvent(QKeyEvent *event);
-#ifdef Q_WS_MAEMO_5
+
+    /**
+     * Handle show events.
+     * On Symbian, volume keys can only be grabbed, if the window is shown.
+     */
     void showEvent(QShowEvent *event);
+
+    /** Handle resize events. */
+    void resizeEvent(QResizeEvent *event);
+
+#ifdef Q_WS_MAEMO_5
+    /** Actually grab the volume keys. */
     void doGrabVolumeKeys(bool grab);
 #endif
-    BookView *bookView;
+
+protected slots:
+    void placeDecorations();
+
+private:
+    BookView *bookView;     /**< Book view widget. */
     bool grabbingVolumeKeys;/**< True, if volume keys should be grabbed. */
-    QToolBar *toolBar;
+    QToolBar *toolBar;      /**< Tool bar. */
+    Progress *progress;     /**< Reading progress indicator. */
+    TranslucentButton *previousButton;  /**< Previous page indicator. */
+    TranslucentButton *nextButton;      /**< Next page indicator. */
 };
 
 #endif // ADOPTERWINDOW_H
index 07bf291..047c01a 100644 (file)
@@ -9,8 +9,7 @@
 
 static const int MARGIN = 9;
 
-FullScreenWindow::FullScreenWindow(QWidget *parent):
-        AdopterWindow(parent), progress(0), previousButton(0), nextButton(0)
+FullScreenWindow::FullScreenWindow(QWidget *parent): AdopterWindow(parent)
 {
     TRACE;
     Q_ASSERT(parent);
@@ -50,20 +49,6 @@ void FullScreenWindow::resizeEvent(QResizeEvent *e)
     AdopterWindow::resizeEvent(e);
 }
 
-void FullScreenWindow::takeChildren(BookView *view,
-                                    Progress *prog,
-                                    TranslucentButton *previous,
-                                    TranslucentButton *next)
-{
-    TRACE;
-    progress = prog;
-    previousButton = previous;
-    nextButton = next;
-    QList<QWidget *> otherChildren;
-    otherChildren << progress << previousButton << nextButton;
-    AdopterWindow::takeChildren(view, otherChildren);
-}
-
 void FullScreenWindow::placeChildren()
 {
     Trace t("FullScreenWindow::placeChildren");
@@ -89,28 +74,5 @@ void FullScreenWindow::placeChildren()
         h - TranslucentButton::pixels - MARGIN,
         TranslucentButton::pixels,
         TranslucentButton::pixels);
-
-    if (hasChild(progress)) {
-        progress->setGeometry(0, h - progress->thickness(),
-                              w, progress->thickness());
-        qDebug() << "Screen (FullScreenWindow::resizeEvent)" << w << "x" << h;
-        qDebug() << "Progress (FullScreenWindow::resizeEvent)"
-                << progress->geometry();
-    }
-    if (hasChild(previousButton)) {
-        previousButton->setGeometry(
-                MARGIN,
-                h - TranslucentButton::pixels - MARGIN,
-                TranslucentButton::pixels,
-                TranslucentButton::pixels);
-    }
-    if (hasChild(nextButton)) {
-        nextButton->setGeometry(
-        w - TranslucentButton::pixels - MARGIN,
-        MARGIN,
-        TranslucentButton::pixels,
-        TranslucentButton::pixels);
-    }
-
     restoreButton->flash(3000);
 }
index df2496c..bd93a10 100644 (file)
@@ -45,9 +45,6 @@ protected slots:
 
 private:
     TranslucentButton *restoreButton;
-    Progress *progress;
-    TranslucentButton *previousButton;
-    TranslucentButton *nextButton;
 };
 
 #endif // FULLSCREENWINDOW_H
index 074b33f..54c892b 100755 (executable)
@@ -44,17 +44,6 @@ MainWindow::MainWindow(QWidget *parent):
 #endif
     setWindowTitle("Dorian");
 
-#ifdef Q_OS_SYMBIAN
-    // Tool bar
-    toolBar = new QToolBar("", this /*frame*/);
-    toolBar->setFixedWidth(QApplication::desktop()->
-                           availableGeometry().width());
-    toolBar->setFixedHeight(65);
-    toolBar->setStyleSheet("margin:0;border:0;padding:0");
-    toolBar->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Maximum);
-    addToolBar(Qt::BottomToolBarArea, toolBar);
-#endif
-
     // Central widget. Must be an intermediate, because the book view widget
     // can be re-parented later
     QFrame *central = new QFrame(this);
@@ -66,10 +55,6 @@ MainWindow::MainWindow(QWidget *parent):
     // Book view
     view = new BookView(this);
     view->show();
-    layout->addWidget(view);
-
-    // Dialogs
-    progress = new Progress(this);
 
     // Tool bar actions
 
@@ -110,9 +95,10 @@ MainWindow::MainWindow(QWidget *parent):
     (void)addToolBarAction(this, SLOT(close()), "", tr("Exit"));
 #endif
 
-    // Buttons on top of the book view
-    previousButton = new TranslucentButton("back", this);
-    nextButton = new TranslucentButton("forward", this);
+    // Decorations
+    prev = new TranslucentButton("back", this);
+    next = new TranslucentButton("forward", this);
+    prog = new Progress(this);
 
     // Handle model changes
     connect(Library::instance(), SIGNAL(nowReadingChanged()),
@@ -131,7 +117,7 @@ MainWindow::MainWindow(QWidget *parent):
     connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
 
     // Handle progress
-    connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
+    connect(view, SIGNAL(progress(qreal)), prog, SLOT(setProgress(qreal)));
 
     // Shadow window for full screen reading
     fullScreenWindow = new FullScreenWindow(this);
@@ -142,8 +128,8 @@ MainWindow::MainWindow(QWidget *parent):
             this, SLOT(onSettingsChanged(const QString &)));
 
     // Handle book view buttons
-    connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
-    connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
+    connect(next, SIGNAL(triggered()), this, SLOT(goToNextPage()));
+    connect(prev, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
 
     // Adopt view, show window
     showRegular();
@@ -195,51 +181,8 @@ void MainWindow::showRegular()
     TRACE;
 
     // Re-parent children
-    fullScreenWindow->leaveChildren();
-    QList<QWidget *> otherChildren;
-    otherChildren << progress << previousButton << nextButton;
-    takeChildren(view, otherChildren);
-
-#if 0
-
-    // Adjust geometry of decorations
-
-    QRect geo = geometry();
-    qDebug() << "MainWindow (MainWindow::showRegular)" << geo;
-    qDebug() << "BookView (MainWindow::showRegular)" << view->geometry();
-    int y = geo.height() - progress->thickness();
-#if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
-    bool hasToolBar = false;
-#   if defined(Q_OS_SYMBIAN)
-    hasToolBar =
-        (QApplication::desktop()->width() < QApplication::desktop()->height());
-    qDebug() << (hasToolBar? "Portrait": "Landscape");
-#   endif
-    if (!hasToolBar) {
-        y -= toolBar->height();
-    }
-#endif
-    progress->setGeometry(0, y, geo.width(), y + progress->thickness());
-
-#if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
-    y = geo.height() - TranslucentButton::pixels;
-    if (!hasToolBar) {
-        y -= toolBar->height();
-    }
-    previousButton->setGeometry(0, y, TranslucentButton::pixels,
-                                TranslucentButton::pixels);
-    nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
-        TranslucentButton::pixels, TranslucentButton::pixels);
-#else
-    previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
-        TranslucentButton::pixels, TranslucentButton::pixels);
-    nextButton->setGeometry(geo.width() - TranslucentButton::pixels - 25,
-        toolBar->height(), TranslucentButton::pixels,
-        TranslucentButton::pixels);
-#endif // Q_WS_MAEMO_5
-    qDebug() << "previousButton geometry" << previousButton->geometry();
-
-#endif
+    fullScreenWindow->leaveBookView();
+    takeBookView(view, prog, prev, next);
 
     fullScreenWindow->hide();
     show();
@@ -253,26 +196,8 @@ void MainWindow::showBig()
     TRACE;
 
     // Re-parent children
-    leaveChildren();
-    fullScreenWindow->takeChildren(view, progress, previousButton, nextButton);
-
-#if 0
-
-    // Adjust geometry of decorations
-    QRect screen = QApplication::desktop()->screenGeometry();
-    int y = screen.height() - progress->thickness();
-    progress->setGeometry(0, y, screen.width(), y + progress->thickness());
-#if defined(Q_WS_MAEMO_5)
-    nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
-        TranslucentButton::pixels, TranslucentButton::pixels);
-#else
-    nextButton->setGeometry(screen.width() - TranslucentButton::pixels - 25, 0,
-        TranslucentButton::pixels, TranslucentButton::pixels);
-#endif // Q_WS_MAEMO_5
-    previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
-        TranslucentButton::pixels, TranslucentButton::pixels);
-
-#endif
+    leaveBookView();
+    fullScreenWindow->takeBookView(view, prog, prev, next);
 
 // #ifdef Q_OS_SYMBIAN
     hide();
@@ -444,67 +369,6 @@ void MainWindow::timerEvent(QTimerEvent *event)
     AdopterWindow::timerEvent(event);
 }
 
-void MainWindow::resizeEvent(QResizeEvent *e)
-{
-    Trace t("MainWindow::resizeEvent");
-#ifdef Q_OS_SYMBIAN
-    // Tool bar is only useful in portrait mode
-    bool isPortrait =
-        (QApplication::desktop()->width() < QApplication::desktop()->height());
-    toolBar->setVisible(isPortrait);
-#endif
-    QTimer::singleShot(100, this, SLOT(placeChildren()));
-    AdopterWindow::resizeEvent(e);
-}
-
-void MainWindow::placeChildren()
-{
-    Trace t("MainWindow::placeChildren");
-
-    int toolBarHeight = 0;
-
-#ifdef Q_OS_SYMBIAN
-    // Tool bar is only useful in portrait mode
-    bool isPortrait =
-        (QApplication::desktop()->width() < QApplication::desktop()->height());
-    // toolBar->setVisible(isPortrait);
-
-    // Work around Symbian bug: If there is no tool bar, increase decorator
-    // widgets' Y coordinates
-    if (!isPortrait) {
-        toolBarHeight = toolBar->height();
-    }
-#endif // Q_OS_SYMBIAN
-
-    if (hasChild(view)) {
-        QRect geo = centralWidget()->geometry();
-        qDebug() << "centralWidget (MainWindow::resizeEvent)" << geo;
-#ifdef Q_OS_SYMBIAN
-        // FIXME: When returning from full screen in landscape mode,
-        // the central widget's height is miscalculated on Symbian.
-        // My apologies for this kludge
-        if (geo.height() == 288) {
-            geo.setHeight(223);
-        }
-#endif // Q_OS_SYMBIAN
-        progress->setGeometry(geo.x(),
-            geo.y() + geo.height() - progress->thickness() + toolBarHeight,
-            geo.width(), progress->thickness());
-        previousButton->setGeometry(geo.x(),
-            geo.y() + geo.height() - TranslucentButton::pixels + toolBarHeight,
-            TranslucentButton::pixels, TranslucentButton::pixels);
-        nextButton->setGeometry(
-            geo.x() + geo.width() - TranslucentButton::pixels,
-            geo.y(), TranslucentButton::pixels, TranslucentButton::pixels);
-        progress->flash();
-        previousButton->flash();
-        nextButton->flash();
-        qDebug() << "Progress (MainWindow::resizeEvent)"
-                << progress->geometry();
-    }
-
-}
-
 void MainWindow::about()
 {
     Dyalog *aboutDialog = new Dyalog(this, false);
@@ -526,15 +390,15 @@ void MainWindow::about()
 
 void MainWindow::goToNextPage()
 {
-    nextButton->flash();
-    previousButton->flash();
+    next->flash();
+    prev->flash();
     view->goNextPage();
 }
 
 void MainWindow::goToPreviousPage()
 {
-    nextButton->flash();
-    previousButton->flash();
+    next->flash();
+    prev->flash();
     view->goPreviousPage();
 }
 
index 4f499d2..6e70740 100755 (executable)
@@ -53,10 +53,6 @@ public slots:
 protected:
     void closeEvent(QCloseEvent *event);
     void timerEvent(QTimerEvent *event);
-    void resizeEvent(QResizeEvent *event);
-
-protected slots:
-    void placeChildren();
 
 private:
     void setCurrentBook(const QModelIndex &current);
@@ -75,9 +71,9 @@ private:
     QModelIndex mCurrent;
     FullScreenWindow *fullScreenWindow;
     int preventBlankingTimer;
-    Progress *progress;
-    TranslucentButton *previousButton;
-    TranslucentButton *nextButton;
+    Progress *prog;
+    TranslucentButton *prev;
+    TranslucentButton *next;
     ProgressDialog *libraryProgress;
 };
 
index 291a4b5..4fc6dee 100644 (file)
@@ -1,5 +1,6 @@
 #include <QtGlobal>
 #include <QDir>
+#include <QtGui>
 
 #if defined(Q_OS_UNIX) && !defined(Q_OS_SYMBIAN)
 #   include <unistd.h>