More Symbian tunings.
[dorian] / adopterwindow.cpp
index d553a5c..5e9ef64 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)
+AdopterWindow::AdopterWindow(QWidget *parent):
+    QMainWindow(parent), bookView(0), grabbingVolumeKeys(false), toolBar(0),
+    progress(0), previousButton(0), nextButton(0)
 {
     TRACE;
 
 #ifdef Q_WS_MAEMO_5
     setAttribute(Qt::WA_Maemo5StackedWindow, true);
-#endif // Q_WS_MAEMO_5
+#endif
 
     QFrame *frame = new QFrame(this);
     QVBoxLayout *layout = new QVBoxLayout(frame);
     layout->setMargin(0);
     frame->setLayout(layout);
+    //frame->show();
     setCentralWidget(frame);
 
 #ifdef Q_OS_SYMBIAN
@@ -35,7 +40,7 @@ AdopterWindow::AdopterWindow(QWidget *parent): QMainWindow(parent), bookView(0)
 #else
     // Tool bar
     setUnifiedTitleAndToolBarOnMac(true);
-    toolBar = addToolBar("controls");
+    toolBar = addToolBar("");
     toolBar->setMovable(false);
     toolBar->setFloatable(false);
     toolBar->toggleViewAction()->setVisible(false);
@@ -45,46 +50,63 @@ AdopterWindow::AdopterWindow(QWidget *parent): QMainWindow(parent), bookView(0)
 #endif // Q_OS_SYMBIAN
 
     // Monitor settings
-    Settings *settings = Settings::instance();
-    connect(settings, SIGNAL(valueChanged(const QString &)),
+    connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
             this, SLOT(onSettingsChanged(const QString &)));
-    settings->setValue("usevolumekeys", settings->value("usevolumekeys"));
 }
 
-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(centralWidget());
-        centralWidget()->layout()->addWidget(bookView);
-        bookView->show();
-    }
-    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);
+    centralWidget()->layout()->addWidget(bookView);
+    bookView->show();
+
+    progress = prog;
+    previousButton = previous;
+    nextButton = next;
+    progress->setParent(this);
+    previousButton->setParent(this);
+    nextButton->setParent(this);
 }
 
-void AdopterWindow::leaveChildren()
+void AdopterWindow::leaveBookView()
 {
     TRACE;
     if (bookView) {
+        bookView->hide();
         centralWidget()->layout()->removeWidget(bookView);
-        bookView = 0;
     }
+    bookView = 0;
+    progress = 0;
+    nextButton = 0;
+    previousButton = 0;
+}
+
+bool AdopterWindow::hasBookView()
+{
+    return bookView != 0;
 }
 
 void AdopterWindow::show()
 {
+    Trace t("AdopterWindow::show");
 #ifdef Q_OS_SYMBIAN
     foreach (QWidget *w, QApplication::allWidgets()) {
         w->setContextMenuPolicy(Qt::NoContextMenu);
     }
     showMaximized();
-    raise();
 #else
     QMainWindow::show();
 #endif
@@ -93,20 +115,49 @@ void AdopterWindow::show()
 QAction *AdopterWindow::addToolBarAction(QObject *receiver,
                                          const char *member,
                                          const QString &iconName,
-                                         const QString &text)
+                                         const QString &text,
+                                         bool important)
 {
     TRACE;
     qDebug() << "icon" << iconName << "text" << text;
+    QAction *action;
 #ifndef Q_OS_SYMBIAN
-    return toolBar->addAction(QIcon(Platform::icon(iconName)),
-                              text, receiver, member);
+    Q_UNUSED(important);
+    action = toolBar->addAction(QIcon(Platform::instance()->icon(iconName)),
+                                text, receiver, member);
 #else
-    Q_UNUSED(iconName);
-    QAction *action = new QAction(text, this);
+    if (!toolBar && important) {
+        // Create tool bar if needed
+        toolBar = new QToolBar("", this);
+        // toolBar->setFixedHeight(63);
+        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);
+        button->setIcon(QIcon(Platform::instance()->icon(iconName)));
+        button->setSizePolicy(QSizePolicy::MinimumExpanding,
+                              QSizePolicy::Maximum);
+        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);
-    return action;
 #endif
+
+#if defined Q_WS_MAEMO_5
+    action->setText("");
+    action->setToolTip("");
+#endif
+
+    return action;
 }
 
 void AdopterWindow::addToolBarSpace()
@@ -141,7 +192,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;
@@ -159,16 +211,56 @@ void AdopterWindow::doGrabVolumeKeys(bool grab)
 
 #endif // Q_WS_MAEMO_5
 
-#ifdef Q_WS_MAEMO_5
+#ifdef Q_OS_SYMBIAN
 
-void AdopterWindow::showEvent(QShowEvent *e)
+void AdopterWindow::updateToolBar()
 {
     TRACE;
-    doGrabVolumeKeys(grabbingVolumeKeys);
+    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
+    placeDecorations();
 }
 
-#endif // Q_WS_MAEMO_5
+void AdopterWindow::resizeEvent(QResizeEvent *event)
+{
+    Trace t("AdopterWindow::resizeEvent");
+#ifdef Q_OS_SYMBIAN
+    updateToolBar();
+#endif
+    QMainWindow::resizeEvent(event);
+    placeDecorations();
+}
 
 void AdopterWindow::keyPressEvent(QKeyEvent *event)
 {
@@ -204,10 +296,53 @@ void AdopterWindow::keyPressEvent(QKeyEvent *event)
 
 void AdopterWindow::onSettingsChanged(const QString &key)
 {
-    TRACE;
     if (key == "usevolumekeys") {
-        qDebug() << key;
-        grabVolumeKeys(Settings::instance()->value(key).toBool());
+        bool grab = Settings::instance()->value(key, false).toBool();
+        qDebug() << "AdopterWindow::onSettingsChanged: usevolumekeys" << grab;
+        grabVolumeKeys(grab);
     }
 }
 
+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 (toolBar && toolBar->isHidden()) {
+        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();
+}