54343b60c3e0a71bf77d4bdfee59084de0b9dcbb
[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 #ifdef Q_OS_SYMBIAN\r
50     if (important) {\r
51         if (!toolBar) {\r
52             // Create tool bar if needed\r
53             toolBar = new QToolBar("", this);\r
54             toolBar->setStyleSheet("margin:0; border:0; padding:0");\r
55             addToolBar(Qt::BottomToolBarArea, toolBar);\r
56         }\r
57         // Add tool bar action\r
58         QPushButton *button = new QPushButton(this);\r
59         button->setIconSize(QSize(60, 60));\r
60         button->setFixedSize(89, 60);\r
61         button->setIcon(QIcon(Platform::instance()->icon(iconName)));\r
62         button->setSizePolicy(QSizePolicy::MinimumExpanding,\r
63                               QSizePolicy::Maximum);\r
64         connect(button, SIGNAL(clicked()), receiver, member);\r
65         toolBar->addWidget(button);\r
66     }\r
67     // Add menu action, too\r
68     action = menuBar()->addAction(text);\r
69     connect(action, SIGNAL(triggered()), receiver, member);\r
70 #else\r
71     Q_UNUSED(important);\r
72     action = toolBar->addAction(QIcon(Platform::instance()->icon(iconName)),\r
73                                 text, receiver, member);\r
74 #endif\r
75 \r
76     action->setToolTip("");\r
77     return action;\r
78 }\r
79 \r
80 void MainBase::addToolBarSpace()\r
81 {\r
82 #ifndef Q_OS_SYMBIAN\r
83     QFrame *frame = new QFrame(toolBar);\r
84     frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);\r
85     toolBar->addWidget(frame);\r
86 #endif\r
87 }\r
88 \r
89 void MainBase::updateToolBar()\r
90 {\r
91 #ifdef Q_OS_SYMBIAN\r
92     TRACE;\r
93     if (toolBar) {\r
94         QRect geometry = QApplication::desktop()->geometry();\r
95         bool isPortrait = geometry.width() < geometry.height();\r
96         bool isToolBarHidden = toolBar->isHidden();\r
97         if (isPortrait && isToolBarHidden) {\r
98             qDebug() << "Show tool bar";\r
99             toolBar->setVisible(true);\r
100         } else if (!isPortrait && !isToolBarHidden) {\r
101             qDebug() << "Hide tool bar";\r
102             toolBar->setVisible(false);\r
103         }\r
104     }\r
105 #endif // Q_OS_SYMBIAN\r
106 }\r
107 \r
108 void MainBase::showEvent(QShowEvent *event)\r
109 {\r
110     Trace t("MainBase::showEvent");\r
111     updateToolBar();\r
112     QMainWindow::showEvent(event);\r
113 }\r
114 \r
115 void MainBase::resizeEvent(QResizeEvent *event)\r
116 {\r
117     Trace t("MainBase::resizeEvent");\r
118     updateToolBar();\r
119     QMainWindow::resizeEvent(event);\r
120 }\r
121 \r
122 void MainBase::hideToolBar()\r
123 {\r
124     if (toolBar) {\r
125         toolBar->hide();\r
126     }\r
127 }\r
128 \r
129 bool MainBase::isToolBarHidden()\r
130 {\r
131     return toolBar && toolBar->isHidden();\r
132 }\r
133 \r
134 int MainBase::toolBarHeight()\r
135 {\r
136     return toolBar? toolBar->height(): 0;\r
137 }\r
138 \r
139 void MainBase::show()\r
140 {\r
141     Trace t("MainBase::show");\r
142 #ifdef Q_OS_SYMBIAN\r
143     foreach (QWidget *w, QApplication::allWidgets()) {\r
144         w->setContextMenuPolicy(Qt::NoContextMenu);\r
145     }\r
146     showMaximized();\r
147 #else\r
148     QMainWindow::show();\r
149 #endif\r
150 }\r