Add new widget MainWindow.
authorAkos Polster <akos@pipacs.com>
Sun, 5 Dec 2010 00:36:26 +0000 (01:36 +0100)
committerAkos Polster <akos@pipacs.com>
Sun, 5 Dec 2010 00:36:26 +0000 (01:36 +0100)
adopterwindow.cpp
adopterwindow.h
dorian.pro
widgets/mainwindow.cpp [new file with mode: 0755]
widgets/mainwindow.h [new file with mode: 0755]

index 29e8331..8d8e6d0 100644 (file)
@@ -387,9 +387,3 @@ void AdopterWindow::onPageDown()
     }
 }
 
-void AdopterWindow::hideToolBar()
-{
-    if (toolBar) {
-        toolBar->hide();
-    }
-}
index c6c99cd..eca7669 100644 (file)
@@ -1,8 +1,8 @@
 #ifndef ADOPTERWINDOW_H
 #define ADOPTERWINDOW_H
 
-#include <QMainWindow>
 #include <QList>
+#include "mainwindow.h"
 
 class QWidget;
 class QToolBar;
@@ -16,7 +16,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 QMainWindow
+class AdopterWindow: public MainWindow
 {
     Q_OBJECT
 
index dbc4311..48ef01f 100644 (file)
@@ -39,7 +39,8 @@ SOURCES += \
     searchresultsdialog.cpp \\r
     searchresultinfodialog.cpp \\r
     widgets/progressdialog.cpp \\r
-    widgets/splash.cpp\r
+    widgets/splash.cpp \\r
+    widgets/mainwindow.cpp\r
 \r
 HEADERS += \\r
     mainwindow.h \\r
@@ -80,7 +81,8 @@ HEADERS += \
     searchresultsdialog.h \\r
     searchresultinfodialog.h \\r
     widgets/progressdialog.h \\r
-    widgets/splash.h\r
+    widgets/splash.h \\r
+    widgets/mainwindow.h\r
 \r
 RESOURCES += \\r
     dorian.qrc\r
diff --git a/widgets/mainwindow.cpp b/widgets/mainwindow.cpp
new file mode 100755 (executable)
index 0000000..f3d176d
--- /dev/null
@@ -0,0 +1,146 @@
+#include <QtGui>\r
+\r
+#include "mainwindow.h"\r
+#include "trace.h"\r
+#include "platform.h"\r
+\r
+MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), toolBar(0)\r
+{\r
+    TRACE;\r
+\r
+#ifdef Q_WS_MAEMO_5\r
+    setAttribute(Qt::WA_Maemo5StackedWindow, true);\r
+#endif\r
+\r
+    QFrame *frame = new QFrame(this);\r
+    QVBoxLayout *layout = new QVBoxLayout(frame);\r
+    layout->setMargin(0);\r
+    frame->setLayout(layout);\r
+    //frame->show();\r
+    setCentralWidget(frame);\r
+\r
+#ifdef Q_OS_SYMBIAN\r
+    QAction *closeAction = new QAction(parent? tr("Back"): tr("Exit"), this);\r
+    closeAction->setSoftKeyRole(QAction::NegativeSoftKey);\r
+    connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));\r
+    QMainWindow::addAction(closeAction);\r
+#else\r
+    // Tool bar\r
+    setUnifiedTitleAndToolBarOnMac(true);\r
+    toolBar = addToolBar("");\r
+    toolBar->setMovable(false);\r
+    toolBar->setFloatable(false);\r
+    toolBar->toggleViewAction()->setVisible(false);\r
+#if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)\r
+    toolBar->setIconSize(QSize(42, 42));\r
+#endif\r
+#endif // Q_OS_SYMBIAN\r
+}\r
+\r
+QAction *MainWindow::addToolBarAction(QObject *receiver,\r
+                                      const char *member,\r
+                                      const QString &iconName,\r
+                                      const QString &text,\r
+                                      bool important)\r
+{\r
+    TRACE;\r
+    qDebug() << "icon" << iconName << "text" << text;\r
+    QAction *action;\r
+#ifndef Q_OS_SYMBIAN\r
+    Q_UNUSED(important);\r
+    action = toolBar->addAction(QIcon(Platform::instance()->icon(iconName)),\r
+                                text, receiver, member);\r
+#else\r
+    if (!toolBar && important) {\r
+        // Create tool bar if needed\r
+        toolBar = new QToolBar("", this);\r
+        // toolBar->setFixedHeight(63);\r
+        toolBar->setStyleSheet("margin:0; border:0; padding:0");\r
+        toolBar->setSizePolicy(QSizePolicy::MinimumExpanding,\r
+                               QSizePolicy::Maximum);\r
+        addToolBar(Qt::BottomToolBarArea, toolBar);\r
+    }\r
+    if (important) {\r
+        // Add tool bar action\r
+        QPushButton *button = new QPushButton(this);\r
+        button->setIconSize(QSize(60, 60));\r
+        button->setFixedSize(89, 60);\r
+        button->setIcon(QIcon(Platform::instance()->icon(iconName)));\r
+        button->setSizePolicy(QSizePolicy::MinimumExpanding,\r
+                              QSizePolicy::Maximum);\r
+        connect(button, SIGNAL(clicked()), receiver, member);\r
+        toolBar->addWidget(button);\r
+    }\r
+    // Add menu action, too\r
+    action = new QAction(text, this);\r
+    menuBar()->addAction(action);\r
+    connect(action, SIGNAL(triggered()), receiver, member);\r
+#endif\r
+\r
+    action->setText("");\r
+    action->setToolTip("");\r
+\r
+    return action;\r
+}\r
+\r
+void MainWindow::addToolBarSpace()\r
+{\r
+#ifndef Q_OS_SYMBIAN\r
+    QFrame *frame = new QFrame(toolBar);\r
+    frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);\r
+    toolBar->addWidget(frame);\r
+#endif\r
+}\r
+\r
+#ifdef Q_OS_SYMBIAN\r
+\r
+void MainWindow::updateToolBar()\r
+{\r
+    TRACE;\r
+    if (toolBar) {\r
+        QRect geometry = QApplication::desktop()->geometry();\r
+        bool isPortrait = geometry.width() < geometry.height();\r
+        bool isToolBarHidden = toolBar->isHidden();\r
+        if (isPortrait && isToolBarHidden) {\r
+            qDebug() << "Show tool bar";\r
+            toolBar->setVisible(true);\r
+        } else if (!isPortrait && !isToolBarHidden) {\r
+            qDebug() << "Hide tool bar";\r
+            toolBar->setVisible(false);\r
+        }\r
+    }\r
+}\r
+\r
+bool MainWindow::portrait()\r
+{\r
+    QRect geometry = QApplication::desktop()->geometry();\r
+    return geometry.width() < geometry.height();\r
+}\r
+\r
+#endif // Q_OS_SYMBIAN\r
+\r
+void MainWindow::showEvent(QShowEvent *e)\r
+{\r
+    Trace t("MainWindow::showEvent");\r
+\r
+#ifdef Q_OS_SYMBIAN\r
+    updateToolBar();\r
+#endif\r
+    QMainWindow::showEvent(e);\r
+}\r
+\r
+void MainWindow::resizeEvent(QResizeEvent *event)\r
+{\r
+    Trace t("MainWindow::resizeEvent");\r
+#ifdef Q_OS_SYMBIAN\r
+    updateToolBar();\r
+#endif\r
+    QMainWindow::resizeEvent(event);\r
+}\r
+\r
+void MainWindow::hideToolBar()\r
+{\r
+    if (toolBar) {\r
+        toolBar->hide();\r
+    }\r
+}\r
diff --git a/widgets/mainwindow.h b/widgets/mainwindow.h
new file mode 100755 (executable)
index 0000000..62b1ba3
--- /dev/null
@@ -0,0 +1,61 @@
+#ifndef MAINWINDOW_H\r
+#define MAINWINDOW_H\r
+\r
+#include <QMainWindow>\r
+\r
+class QToolBar;\r
+\r
+/**\r
+ * Main window with a toolbar.\r
+ */\r
+class MainWindow: public QMainWindow\r
+{\r
+    Q_OBJECT\r
+\r
+public:\r
+    explicit MainWindow(QWidget *parent = 0);\r
+\r
+    /**\r
+     * Add action that is visible on the tool bar.\r
+     * @param   receiver    Object receiving "activated" signal.\r
+     * @param   slot        Slot receiving "activated" signal.\r
+     * @param   iconName    Base name of tool bar icon in resource file.\r
+     * @param   text        Tool bar item text.\r
+     * @param   important   On Symbian, only "important" actions are added to\r
+     *                      the tool bar. All actions are added to the Options\r
+     *                      menu though.\r
+     */\r
+    QAction *addToolBarAction(QObject *receiver, const char *slot,\r
+                              const QString &iconName, const QString &text,\r
+                              bool important = false);\r
+\r
+    /** Add spacing to tool bar. */\r
+    void addToolBarSpace();\r
+\r
+signals:\r
+\r
+public slots:\r
+\r
+protected:\r
+    /** Handle resize event: Manage tool bar visibility. */\r
+    void resizeEvent(QResizeEvent *event);\r
+\r
+    /** Handle show event: Manage tool bar visibility. */\r
+    void showEvent(QShowEvent *event);\r
+\r
+#ifdef Q_OS_SYMBIAN\r
+    /** Update toolbar visibility. */\r
+    void updateToolBar();\r
+\r
+    /** Return true in portrait mode. */\r
+    bool portrait();\r
+#endif // Q_OS_SYMBIAN\r
+\r
+    /** Hide tool bar if visible. */\r
+    void hideToolBar();\r
+\r
+private:\r
+    QToolBar *toolBar;      /**< Tool bar. */\r
+};\r
+\r
+#endif // MAINWINDOW_H\r