Clean up event handling.
[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 #if defined(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     setCentralWidget(frame);\r
20 \r
21 #if defined(Q_OS_SYMBIAN)\r
22     QAction *closeAction = new QAction(parent? tr("Back"): tr("Exit"), this);\r
23     closeAction->setSoftKeyRole(QAction::NegativeSoftKey);\r
24     connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));\r
25     QMainWindow::addAction(closeAction);\r
26 #endif // Q_OS_SYMBIAN\r
27 }\r
28 \r
29 void MainBase::addToolBar()\r
30 {\r
31     TRACE;\r
32 \r
33     if (toolBar) {\r
34         return;\r
35     }\r
36 \r
37 #if defined(Q_OS_SYMBIAN)\r
38     toolBar = new QToolBar("", this);\r
39     QMainWindow::addToolBar(Qt::BottomToolBarArea, toolBar);\r
40 #else\r
41     toolBar = QMainWindow::addToolBar("");\r
42 #endif\r
43 \r
44     setUnifiedTitleAndToolBarOnMac(true);\r
45     toolBar->setMovable(false);\r
46     toolBar->setFloatable(false);\r
47     toolBar->toggleViewAction()->setVisible(false);\r
48 \r
49 #if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)\r
50     toolBar->setIconSize(QSize(42, 42));\r
51 #endif\r
52 }\r
53 \r
54 QAction *MainBase::addToolBarAction(QObject *receiver,\r
55                                     const char *member,\r
56                                     const QString &iconName,\r
57                                     const QString &text,\r
58                                     bool important)\r
59 {\r
60     TRACE;\r
61     qDebug() << "icon" << iconName << "text" << text;\r
62     QAction *action;\r
63 #ifdef Q_OS_SYMBIAN\r
64     if (important) {\r
65         // Add tool bar action\r
66         addToolBar();\r
67         QPushButton *button = new QPushButton(this);\r
68         button->setIconSize(QSize(60, 60));\r
69         button->setFixedHeight(60);\r
70         button->setIcon(QIcon(Platform::instance()->icon(iconName)));\r
71         button->setSizePolicy(QSizePolicy::MinimumExpanding,\r
72                               QSizePolicy::Maximum);\r
73         connect(button, SIGNAL(clicked()), receiver, member);\r
74         toolBar->addWidget(button);\r
75     }\r
76     // Add menu action, too\r
77     action = menuBar()->addAction(text);\r
78     connect(action, SIGNAL(triggered()), receiver, member);\r
79 #else\r
80     Q_UNUSED(important);\r
81     addToolBar();\r
82     action = toolBar->addAction(QIcon(Platform::instance()->icon(iconName)),\r
83                                 text, receiver, member);\r
84 #endif\r
85 \r
86     action->setToolTip("");\r
87     return action;\r
88 }\r
89 \r
90 void MainBase::addToolBarSpace()\r
91 {\r
92     addToolBar();\r
93     QFrame *frame = new QFrame(toolBar);\r
94     frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);\r
95     toolBar->addWidget(frame);\r
96 }\r
97 \r
98 int MainBase::toolBarHeight()\r
99 {\r
100     return toolBar? toolBar->height(): 0;\r
101 }\r
102 \r
103 void MainBase::show()\r
104 {\r
105     Trace t("MainBase::show");\r
106 #ifdef Q_OS_SYMBIAN\r
107     foreach (QWidget *w, QApplication::allWidgets()) {\r
108         w->setContextMenuPolicy(Qt::NoContextMenu);\r
109     }\r
110     showMaximized();\r
111 #else\r
112     QMainWindow::show();\r
113 #endif\r
114 }\r