f3d176daeb2c47ba714bfb1dc4d4100501d1f340
[dorian] / widgets / mainwindow.cpp
1 #include <QtGui>\r
2 \r
3 #include "mainwindow.h"\r
4 #include "trace.h"\r
5 #include "platform.h"\r
6 \r
7 MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), toolBar(0)\r
8 {\r
9     TRACE;\r
10 \r
11 #ifdef Q_WS_MAEMO_5\r
12     setAttribute(Qt::WA_Maemo5StackedWindow, true);\r
13 #endif\r
14 \r
15     QFrame *frame = new QFrame(this);\r
16     QVBoxLayout *layout = new QVBoxLayout(frame);\r
17     layout->setMargin(0);\r
18     frame->setLayout(layout);\r
19     //frame->show();\r
20     setCentralWidget(frame);\r
21 \r
22 #ifdef Q_OS_SYMBIAN\r
23     QAction *closeAction = new QAction(parent? tr("Back"): tr("Exit"), this);\r
24     closeAction->setSoftKeyRole(QAction::NegativeSoftKey);\r
25     connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));\r
26     QMainWindow::addAction(closeAction);\r
27 #else\r
28     // Tool bar\r
29     setUnifiedTitleAndToolBarOnMac(true);\r
30     toolBar = addToolBar("");\r
31     toolBar->setMovable(false);\r
32     toolBar->setFloatable(false);\r
33     toolBar->toggleViewAction()->setVisible(false);\r
34 #if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)\r
35     toolBar->setIconSize(QSize(42, 42));\r
36 #endif\r
37 #endif // Q_OS_SYMBIAN\r
38 }\r
39 \r
40 QAction *MainWindow::addToolBarAction(QObject *receiver,\r
41                                       const char *member,\r
42                                       const QString &iconName,\r
43                                       const QString &text,\r
44                                       bool important)\r
45 {\r
46     TRACE;\r
47     qDebug() << "icon" << iconName << "text" << text;\r
48     QAction *action;\r
49 #ifndef Q_OS_SYMBIAN\r
50     Q_UNUSED(important);\r
51     action = toolBar->addAction(QIcon(Platform::instance()->icon(iconName)),\r
52                                 text, receiver, member);\r
53 #else\r
54     if (!toolBar && important) {\r
55         // Create tool bar if needed\r
56         toolBar = new QToolBar("", this);\r
57         // toolBar->setFixedHeight(63);\r
58         toolBar->setStyleSheet("margin:0; border:0; padding:0");\r
59         toolBar->setSizePolicy(QSizePolicy::MinimumExpanding,\r
60                                QSizePolicy::Maximum);\r
61         addToolBar(Qt::BottomToolBarArea, toolBar);\r
62     }\r
63     if (important) {\r
64         // Add tool bar action\r
65         QPushButton *button = new QPushButton(this);\r
66         button->setIconSize(QSize(60, 60));\r
67         button->setFixedSize(89, 60);\r
68         button->setIcon(QIcon(Platform::instance()->icon(iconName)));\r
69         button->setSizePolicy(QSizePolicy::MinimumExpanding,\r
70                               QSizePolicy::Maximum);\r
71         connect(button, SIGNAL(clicked()), receiver, member);\r
72         toolBar->addWidget(button);\r
73     }\r
74     // Add menu action, too\r
75     action = new QAction(text, this);\r
76     menuBar()->addAction(action);\r
77     connect(action, SIGNAL(triggered()), receiver, member);\r
78 #endif\r
79 \r
80     action->setText("");\r
81     action->setToolTip("");\r
82 \r
83     return action;\r
84 }\r
85 \r
86 void MainWindow::addToolBarSpace()\r
87 {\r
88 #ifndef Q_OS_SYMBIAN\r
89     QFrame *frame = new QFrame(toolBar);\r
90     frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);\r
91     toolBar->addWidget(frame);\r
92 #endif\r
93 }\r
94 \r
95 #ifdef Q_OS_SYMBIAN\r
96 \r
97 void MainWindow::updateToolBar()\r
98 {\r
99     TRACE;\r
100     if (toolBar) {\r
101         QRect geometry = QApplication::desktop()->geometry();\r
102         bool isPortrait = geometry.width() < geometry.height();\r
103         bool isToolBarHidden = toolBar->isHidden();\r
104         if (isPortrait && isToolBarHidden) {\r
105             qDebug() << "Show tool bar";\r
106             toolBar->setVisible(true);\r
107         } else if (!isPortrait && !isToolBarHidden) {\r
108             qDebug() << "Hide tool bar";\r
109             toolBar->setVisible(false);\r
110         }\r
111     }\r
112 }\r
113 \r
114 bool MainWindow::portrait()\r
115 {\r
116     QRect geometry = QApplication::desktop()->geometry();\r
117     return geometry.width() < geometry.height();\r
118 }\r
119 \r
120 #endif // Q_OS_SYMBIAN\r
121 \r
122 void MainWindow::showEvent(QShowEvent *e)\r
123 {\r
124     Trace t("MainWindow::showEvent");\r
125 \r
126 #ifdef Q_OS_SYMBIAN\r
127     updateToolBar();\r
128 #endif\r
129     QMainWindow::showEvent(e);\r
130 }\r
131 \r
132 void MainWindow::resizeEvent(QResizeEvent *event)\r
133 {\r
134     Trace t("MainWindow::resizeEvent");\r
135 #ifdef Q_OS_SYMBIAN\r
136     updateToolBar();\r
137 #endif\r
138     QMainWindow::resizeEvent(event);\r
139 }\r
140 \r
141 void MainWindow::hideToolBar()\r
142 {\r
143     if (toolBar) {\r
144         toolBar->hide();\r
145     }\r
146 }\r