AdopterWindow to inherit from MainBase.
[dorian] / widgets / mainbase.cpp
1 #include <QtGui>\r
2 \r
3 #include "mainbase.h"\r
4 #include "trace.h"\r
5 #include "platform.h"\r
6 \r
7 MainBase::MainBase(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 *MainBase::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 MainBase::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 void MainBase::updateToolBar()\r
96 {\r
97 #ifdef Q_OS_SYMBIAN\r
98     TRACE;\r
99     if (toolBar) {\r
100         QRect geometry = QApplication::desktop()->geometry();\r
101         bool isPortrait = geometry.width() < geometry.height();\r
102         bool isToolBarHidden = toolBar->isHidden();\r
103         if (isPortrait && isToolBarHidden) {\r
104             qDebug() << "Show tool bar";\r
105             toolBar->setVisible(true);\r
106         } else if (!isPortrait && !isToolBarHidden) {\r
107             qDebug() << "Hide tool bar";\r
108             toolBar->setVisible(false);\r
109         }\r
110     }\r
111 #endif // Q_OS_SYMBIAN\r
112 }\r
113 \r
114 void MainBase::showEvent(QShowEvent *event)\r
115 {\r
116     Trace t("MainBase::showEvent");\r
117     updateToolBar();\r
118     QMainWindow::showEvent(event);\r
119 }\r
120 \r
121 void MainBase::resizeEvent(QResizeEvent *event)\r
122 {\r
123     Trace t("MainBase::resizeEvent");\r
124     updateToolBar();\r
125     QMainWindow::resizeEvent(event);\r
126 }\r
127 \r
128 void MainBase::hideToolBar()\r
129 {\r
130     if (toolBar) {\r
131         toolBar->hide();\r
132     }\r
133 }\r
134 \r
135 bool MainBase::isToolBarHidden()\r
136 {\r
137     return toolBar && toolBar->isHidden();\r
138 }\r
139 \r
140 int MainBase::toolBarHeight()\r
141 {\r
142     return toolBar? toolBar->height(): 0;\r
143 }\r
144 \r
145 void MainBase::show()\r
146 {\r
147     Trace t("MainBase::show");\r
148 #ifdef Q_OS_SYMBIAN\r
149     foreach (QWidget *w, QApplication::allWidgets()) {\r
150         w->setContextMenuPolicy(Qt::NoContextMenu);\r
151     }\r
152     showMaximized();\r
153 #else\r
154     QMainWindow::show();\r
155 #endif\r
156 }\r