From bb18722c28645b4301f76d367a21a035627bab4a Mon Sep 17 00:00:00 2001 From: Akos Polster Date: Sun, 5 Dec 2010 02:24:47 +0100 Subject: [PATCH] AdopterWindow to inherit from MainBase. --- adopterwindow.cpp | 162 +++++--------------------------------------------- adopterwindow.h | 38 +----------- widgets/mainbase.cpp | 70 ++++++++++++---------- widgets/mainbase.h | 29 +++++---- 4 files changed, 72 insertions(+), 227 deletions(-) diff --git a/adopterwindow.cpp b/adopterwindow.cpp index 8d8e6d0..0ed8670 100644 --- a/adopterwindow.cpp +++ b/adopterwindow.cpp @@ -15,40 +15,11 @@ #include "progress.h" #include "translucentbutton.h" -AdopterWindow::AdopterWindow(QWidget *parent): - QMainWindow(parent), bookView(0), grabbingVolumeKeys(false), toolBar(0), - progress(0), previousButton(0), nextButton(0) +AdopterWindow::AdopterWindow(QWidget *parent): MainBase(parent), bookView(0), + grabbingVolumeKeys(false), progress(0), previousButton(0), nextButton(0) { TRACE; -#ifdef Q_WS_MAEMO_5 - setAttribute(Qt::WA_Maemo5StackedWindow, true); -#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 - QAction *closeAction = new QAction(parent? tr("Back"): tr("Exit"), this); - closeAction->setSoftKeyRole(QAction::NegativeSoftKey); - connect(closeAction, SIGNAL(triggered()), this, SLOT(close())); - QMainWindow::addAction(closeAction); -#else - // Tool bar - setUnifiedTitleAndToolBarOnMac(true); - toolBar = addToolBar(""); - toolBar->setMovable(false); - toolBar->setFloatable(false); - toolBar->toggleViewAction()->setVisible(false); -#if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5) - toolBar->setIconSize(QSize(42, 42)); -#endif -#endif // Q_OS_SYMBIAN - // Monitor settings connect(Settings::instance(), SIGNAL(valueChanged(const QString &)), this, SLOT(onSettingsChanged(const QString &))); @@ -108,76 +79,6 @@ 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(); -#else - QMainWindow::show(); -#endif -} - -QAction *AdopterWindow::addToolBarAction(QObject *receiver, - const char *member, - const QString &iconName, - const QString &text, - bool important) -{ - TRACE; - qDebug() << "icon" << iconName << "text" << text; - QAction *action; -#ifndef Q_OS_SYMBIAN - Q_UNUSED(important); - action = toolBar->addAction(QIcon(Platform::instance()->icon(iconName)), - text, receiver, member); -#else - 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); -#endif - -#if defined Q_WS_MAEMO_5 - action->setText(""); - action->setToolTip(""); -#endif - - return action; -} - -void AdopterWindow::addToolBarSpace() -{ -#ifndef Q_OS_SYMBIAN - QFrame *frame = new QFrame(toolBar); - frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); - toolBar->addWidget(frame); -#endif -} - void AdopterWindow::grabVolumeKeys(bool grab) { TRACE; @@ -220,41 +121,11 @@ 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); + MainBase::showEvent(e); #if defined(Q_WS_MAEMO_5) doGrabVolumeKeys(grabbingVolumeKeys); #endif @@ -264,10 +135,7 @@ void AdopterWindow::showEvent(QShowEvent *e) void AdopterWindow::resizeEvent(QResizeEvent *event) { Trace t("AdopterWindow::resizeEvent"); -#ifdef Q_OS_SYMBIAN - updateToolBar(); -#endif - QMainWindow::resizeEvent(event); + MainBase::resizeEvent(event); placeDecorations(); if (bookView) { QTimer::singleShot(110, bookView, SLOT(restoreLastBookmark())); @@ -280,7 +148,7 @@ void AdopterWindow::closeEvent(QCloseEvent *event) if (bookView) { bookView->setLastBookmark(); } - QMainWindow::closeEvent(event); + MainBase::closeEvent(event); } void AdopterWindow::leaveEvent(QEvent *event) @@ -289,7 +157,7 @@ void AdopterWindow::leaveEvent(QEvent *event) if (bookView) { bookView->setLastBookmark(); } - QMainWindow::leaveEvent(event); + MainBase::leaveEvent(event); } void AdopterWindow::keyPressEvent(QKeyEvent *event) @@ -313,7 +181,7 @@ void AdopterWindow::keyPressEvent(QKeyEvent *event) default: ; } - QMainWindow::keyPressEvent(event); + MainBase::keyPressEvent(event); } void AdopterWindow::onSettingsChanged(const QString &key) @@ -333,7 +201,7 @@ void AdopterWindow::placeDecorations() return; } - int toolBarHeight = 0; + int extraHeight = 0; QRect geo = bookView->geometry(); qDebug() << "bookView:" << geo; @@ -341,8 +209,8 @@ 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 (toolBar && toolBar->isHidden()) { - toolBarHeight = toolBar->height(); + if (isToolBarHidden()) { + extraHeight = toolBarHeight(); } // Work around another Symbian bug: When returning from full screen mode @@ -350,18 +218,17 @@ void AdopterWindow::placeDecorations() // My apologies for this kludge if (geo.height() == 288) { qDebug() << "Adjusting bottom Y"; - toolBarHeight -= 288 - 223; + extraHeight -= 288 - 223; } #endif // Q_OS_SYMBIAN progress->setGeometry(geo.x(), - geo.y() + geo.height() - progress->thickness() + toolBarHeight, + geo.y() + geo.height() - progress->thickness() + extraHeight, geo.width(), progress->thickness()); previousButton->setGeometry(geo.x(), - geo.y() + geo.height() - TranslucentButton::pixels + toolBarHeight, + geo.y() + geo.height() - TranslucentButton::pixels + extraHeight, TranslucentButton::pixels, TranslucentButton::pixels); - nextButton->setGeometry( - geo.x() + geo.width() - TranslucentButton::pixels, + nextButton->setGeometry(geo.x() + geo.width() - TranslucentButton::pixels, geo.y(), TranslucentButton::pixels, TranslucentButton::pixels); progress->flash(); previousButton->flash(); @@ -386,4 +253,3 @@ void AdopterWindow::onPageDown() setEnabled(true); } } - diff --git a/adopterwindow.h b/adopterwindow.h index eca7669..7420d2c 100644 --- a/adopterwindow.h +++ b/adopterwindow.h @@ -2,13 +2,11 @@ #define ADOPTERWINDOW_H #include -#include "mainwindow.h" +#include "mainbase.h" class QWidget; -class QToolBar; class QAction; class BookView; -class QVBoxLayout; class Progress; class TranslucentButton; @@ -16,7 +14,7 @@ class TranslucentButton; * A toplevel window that can adopt a BookView and other children. * On Maemo, it can also grab the volume keys. */ -class AdopterWindow: public MainWindow +class AdopterWindow: public MainBase { Q_OBJECT @@ -33,26 +31,6 @@ public: /** Return true if the book view is currently adopted. */ bool hasBookView(); - /** - * Add action that is visible on the tool bar. - * @param receiver Object receiving "activated" signal. - * @param slot Slot receiving "activated" signal. - * @param iconName Base name of tool bar icon in resource file. - * @param text Tool bar item text. - * @param important On Symbian, only "important" actions are added to - * the tool bar. All actions are added to the Options - * menu though. - */ - QAction *addToolBarAction(QObject *receiver, const char *slot, - const QString &iconName, const QString &text, - bool important = false); - - /** Add spacing to tool bar. */ - void addToolBarSpace(); - - /** Show window. */ - void show(); - /** If grab is true, volume keys will navigate the book view. */ void grabVolumeKeys(bool grab); @@ -86,22 +64,11 @@ protected: /** Handle leave event: Save reading position. */ void leaveEvent(QEvent *event); -#ifdef Q_OS_SYMBIAN - /** Update toolbar visibility. */ - void updateToolBar(); - - /** Return true in portrait mode. */ - bool portrait(); -#endif // Q_OS_SYMBIAN - #ifdef Q_WS_MAEMO_5 /** Actually grab the volume keys. */ void doGrabVolumeKeys(bool grab); #endif // Q_WS_MAEMO_5 - /** Hide tool bar if visible. */ - void hideToolBar(); - protected slots: void placeDecorations(); void onPageDown(); @@ -110,7 +77,6 @@ protected slots: private: BookView *bookView; /**< Book view widget. */ bool grabbingVolumeKeys;/**< True, if volume keys should be grabbed. */ - QToolBar *toolBar; /**< Tool bar. */ Progress *progress; /**< Reading progress indicator. */ TranslucentButton *previousButton; /**< Previous page indicator. */ TranslucentButton *nextButton; /**< Next page indicator. */ diff --git a/widgets/mainbase.cpp b/widgets/mainbase.cpp index f3d176d..1869d14 100755 --- a/widgets/mainbase.cpp +++ b/widgets/mainbase.cpp @@ -1,10 +1,10 @@ #include -#include "mainwindow.h" +#include "mainbase.h" #include "trace.h" #include "platform.h" -MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), toolBar(0) +MainBase::MainBase(QWidget *parent): QMainWindow(parent), toolBar(0) { TRACE; @@ -37,11 +37,11 @@ MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), toolBar(0) #endif // Q_OS_SYMBIAN } -QAction *MainWindow::addToolBarAction(QObject *receiver, - const char *member, - const QString &iconName, - const QString &text, - bool important) +QAction *MainBase::addToolBarAction(QObject *receiver, + const char *member, + const QString &iconName, + const QString &text, + bool important) { TRACE; qDebug() << "icon" << iconName << "text" << text; @@ -83,7 +83,7 @@ QAction *MainWindow::addToolBarAction(QObject *receiver, return action; } -void MainWindow::addToolBarSpace() +void MainBase::addToolBarSpace() { #ifndef Q_OS_SYMBIAN QFrame *frame = new QFrame(toolBar); @@ -92,10 +92,9 @@ void MainWindow::addToolBarSpace() #endif } -#ifdef Q_OS_SYMBIAN - -void MainWindow::updateToolBar() +void MainBase::updateToolBar() { +#ifdef Q_OS_SYMBIAN TRACE; if (toolBar) { QRect geometry = QApplication::desktop()->geometry(); @@ -109,38 +108,49 @@ void MainWindow::updateToolBar() toolBar->setVisible(false); } } -} - -bool MainWindow::portrait() -{ - QRect geometry = QApplication::desktop()->geometry(); - return geometry.width() < geometry.height(); -} - #endif // Q_OS_SYMBIAN +} -void MainWindow::showEvent(QShowEvent *e) +void MainBase::showEvent(QShowEvent *event) { - Trace t("MainWindow::showEvent"); - -#ifdef Q_OS_SYMBIAN + Trace t("MainBase::showEvent"); updateToolBar(); -#endif - QMainWindow::showEvent(e); + QMainWindow::showEvent(event); } -void MainWindow::resizeEvent(QResizeEvent *event) +void MainBase::resizeEvent(QResizeEvent *event) { - Trace t("MainWindow::resizeEvent"); -#ifdef Q_OS_SYMBIAN + Trace t("MainBase::resizeEvent"); updateToolBar(); -#endif QMainWindow::resizeEvent(event); } -void MainWindow::hideToolBar() +void MainBase::hideToolBar() { if (toolBar) { toolBar->hide(); } } + +bool MainBase::isToolBarHidden() +{ + return toolBar && toolBar->isHidden(); +} + +int MainBase::toolBarHeight() +{ + return toolBar? toolBar->height(): 0; +} + +void MainBase::show() +{ + Trace t("MainBase::show"); +#ifdef Q_OS_SYMBIAN + foreach (QWidget *w, QApplication::allWidgets()) { + w->setContextMenuPolicy(Qt::NoContextMenu); + } + showMaximized(); +#else + QMainWindow::show(); +#endif +} diff --git a/widgets/mainbase.h b/widgets/mainbase.h index 62b1ba3..3c72999 100755 --- a/widgets/mainbase.h +++ b/widgets/mainbase.h @@ -1,5 +1,5 @@ -#ifndef MAINWINDOW_H -#define MAINWINDOW_H +#ifndef MAINBASE_H +#define MAINBASE_H #include @@ -8,12 +8,12 @@ class QToolBar; /** * Main window with a toolbar. */ -class MainWindow: public QMainWindow +class MainBase: public QMainWindow { Q_OBJECT public: - explicit MainWindow(QWidget *parent = 0); + explicit MainBase(QWidget *parent = 0); /** * Add action that is visible on the tool bar. @@ -32,9 +32,8 @@ public: /** Add spacing to tool bar. */ void addToolBarSpace(); -signals: - public slots: + void show(); protected: /** Handle resize event: Manage tool bar visibility. */ @@ -43,19 +42,23 @@ protected: /** Handle show event: Manage tool bar visibility. */ void showEvent(QShowEvent *event); -#ifdef Q_OS_SYMBIAN - /** Update toolbar visibility. */ + /** + * Update toolb ar visibility. + * On Symbian, the tool bar is not visible in landscape mode. + */ void updateToolBar(); - /** Return true in portrait mode. */ - bool portrait(); -#endif // Q_OS_SYMBIAN - /** Hide tool bar if visible. */ void hideToolBar(); + /** Return true if the tool bar is present and hidden. */ + bool isToolBarHidden(); + + /** Return the height of the tool bar (or 0 if there is no tool bar). */ + int toolBarHeight(); + private: QToolBar *toolBar; /**< Tool bar. */ }; -#endif // MAINWINDOW_H +#endif // MAINBASE_H -- 1.7.9.5