Speed up paging through the book. Clean up code for saving/restoring
[dorian] / adopterwindow.cpp
index e51c506..29e8331 100644 (file)
@@ -52,6 +52,7 @@ AdopterWindow::AdopterWindow(QWidget *parent):
     // Monitor settings
     connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
             this, SLOT(onSettingsChanged(const QString &)));
+
 }
 
 void AdopterWindow::takeBookView(BookView *view,
@@ -79,6 +80,12 @@ void AdopterWindow::takeBookView(BookView *view,
     progress->setParent(this);
     previousButton->setParent(this);
     nextButton->setParent(this);
+
+    // Handle page and/or volume keys
+    connect(this, SIGNAL(pageUp()), this, SLOT(onPageUp()),
+            Qt::QueuedConnection);
+    connect(this, SIGNAL(pageDown()), this, SLOT(onPageDown()),
+            Qt::QueuedConnection);
 }
 
 void AdopterWindow::leaveBookView()
@@ -92,6 +99,8 @@ void AdopterWindow::leaveBookView()
     progress = 0;
     nextButton = 0;
     previousButton = 0;
+    disconnect(this, SLOT(onPageUp()));
+    disconnect(this, SLOT(onPageDown()));
 }
 
 bool AdopterWindow::hasBookView()
@@ -129,9 +138,7 @@ QAction *AdopterWindow::addToolBarAction(QObject *receiver,
     if (!toolBar && important) {
         // Create tool bar if needed
         toolBar = new QToolBar("", this);
-        // toolBar->setFixedWidth(QApplication::desktop()->
-        //                        availableGeometry().width());
-        toolBar->setFixedHeight(65);
+        // toolBar->setFixedHeight(63);
         toolBar->setStyleSheet("margin:0; border:0; padding:0");
         toolBar->setSizePolicy(QSizePolicy::MinimumExpanding,
                                QSizePolicy::Maximum);
@@ -194,7 +201,8 @@ void AdopterWindow::doGrabVolumeKeys(bool grab)
         return;
     }
     unsigned long val = grab? 1: 0;
-    Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
+    Atom atom =
+            XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
     if (!atom) {
         qCritical() << "Unable to obtain _HILDON_ZOOM_KEY_ATOM";
         return;
@@ -212,52 +220,98 @@ void AdopterWindow::doGrabVolumeKeys(bool grab)
 
 #endif // Q_WS_MAEMO_5
 
+#ifdef Q_OS_SYMBIAN
+
+void AdopterWindow::updateToolBar()
+{
+    TRACE;
+    if (toolBar) {
+        QRect geometry = QApplication::desktop()->geometry();
+        bool isPortrait = geometry.width() < geometry.height();
+        bool isToolBarHidden = toolBar->isHidden();
+        if (isPortrait && isToolBarHidden) {
+            qDebug() << "Show tool bar";
+            toolBar->setVisible(true);
+        } else if (!isPortrait && !isToolBarHidden) {
+            qDebug() << "Hide tool bar";
+            toolBar->setVisible(false);
+        }
+    }
+}
+
+bool AdopterWindow::portrait()
+{
+    QRect geometry = QApplication::desktop()->geometry();
+    return geometry.width() < geometry.height();
+}
+
+#endif // Q_OS_SYMBIAN
+
 void AdopterWindow::showEvent(QShowEvent *e)
 {
     Trace t("AdopterWindow::showEvent");
 
+#ifdef Q_OS_SYMBIAN
+    updateToolBar();
+#endif
     QMainWindow::showEvent(e);
 #if defined(Q_WS_MAEMO_5)
     doGrabVolumeKeys(grabbingVolumeKeys);
-#endif // Q_WS_MAEMO_5
+#endif
     placeDecorations();
 }
 
 void AdopterWindow::resizeEvent(QResizeEvent *event)
 {
     Trace t("AdopterWindow::resizeEvent");
+#ifdef Q_OS_SYMBIAN
+    updateToolBar();
+#endif
     QMainWindow::resizeEvent(event);
     placeDecorations();
+    if (bookView) {
+        QTimer::singleShot(110, bookView, SLOT(restoreLastBookmark()));
+    }
+}
+
+void AdopterWindow::closeEvent(QCloseEvent *event)
+{
+    Trace t("AdopterWindow::closeEvent");
+    if (bookView) {
+        bookView->setLastBookmark();
+    }
+    QMainWindow::closeEvent(event);
+}
+
+void AdopterWindow::leaveEvent(QEvent *event)
+{
+    Trace t("AdopterWindow::leaveEvent");
+    if (bookView) {
+        bookView->setLastBookmark();
+    }
+    QMainWindow::leaveEvent(event);
 }
 
 void AdopterWindow::keyPressEvent(QKeyEvent *event)
 {
     TRACE;
-    if (bookView && grabbingVolumeKeys) {
-        switch (event->key()) {
+    switch (event->key()) {
+    case Qt::Key_PageDown:
 #ifdef Q_WS_MAEMO_5
-        case Qt::Key_F7:
-            qDebug() << "F7";
-            bookView->goNextPage();
-            event->accept();
-            break;
-        case Qt::Key_F8:
-            qDebug() << "F8";
-            bookView->goPreviousPage();
-            event->accept();
-            break;
-#endif // Q_WS_MAEMO_5
-        case Qt::Key_PageUp:
-            bookView->goPreviousPage();
-            event->accept();
-            break;
-        case Qt::Key_PageDown:
-            bookView->goNextPage();
-            event->accept();
-            break;
-        default:
-            ;
-        }
+    case Qt::Key_F7:
+#endif
+        emit pageDown();
+        event->accept();
+        break;
+    case Qt::Key_PageUp:
+#ifdef Q_WS_MAEMO_5
+    case Qt::Key_F8:
+#endif
+        emit pageUp();
+        event->accept();
+        break;
+    default:
+        ;
     }
     QMainWindow::keyPressEvent(event);
 }
@@ -271,28 +325,10 @@ 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");
 
-#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()) {
         return;
     }
@@ -305,7 +341,7 @@ void AdopterWindow::placeDecorations()
 #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) {
+    if (toolBar && toolBar->isHidden()) {
         toolBarHeight = toolBar->height();
     }
 
@@ -332,3 +368,28 @@ void AdopterWindow::placeDecorations()
     nextButton->flash();
     qDebug() << "progress:" << progress->geometry();
 }
+
+void AdopterWindow::onPageUp()
+{
+    if (bookView && grabbingVolumeKeys) {
+        setEnabled(false);
+        bookView->goPreviousPage();
+        setEnabled(true);
+    }
+}
+
+void AdopterWindow::onPageDown()
+{
+    if (bookView && grabbingVolumeKeys) {
+        setEnabled(false);
+        bookView->goNextPage();
+        setEnabled(true);
+    }
+}
+
+void AdopterWindow::hideToolBar()
+{
+    if (toolBar) {
+        toolBar->hide();
+    }
+}