More Symbian fixes.
[dorian] / widgets / adopterwindow.cpp
1 #include <QtGui>
2
3 #ifdef Q_WS_MAEMO_5
4 #   include <QtGui/QX11Info>
5 #   include <X11/Xlib.h>
6 #   include <X11/Xatom.h>
7 #endif // Q_WS_MAEMO_5
8
9 #include "adopterwindow.h"
10 #include "trace.h"
11
12 #ifdef Q_WS_MAC
13 #   define ICON_PREFIX ":/icons/mac/"
14 #else
15 #   define ICON_PREFIX ":/icons/"
16 #endif
17
18 AdopterWindow::AdopterWindow(QWidget *parent):
19         QMainWindow(parent), grabbingZoomKeys(false), mainChild(0)
20 {
21     Trace t("AdopterWindow::AdopterWindow");
22
23 #ifdef Q_WS_MAEMO_5
24     setAttribute(Qt::WA_Maemo5StackedWindow, true);
25 #endif // Q_WS_MAEMO_5
26
27     QFrame *frame = new QFrame(this);
28     QVBoxLayout *layout = new QVBoxLayout(frame);
29     layout->setMargin(0);
30     frame->setLayout(layout);
31     setCentralWidget(frame);
32
33 #ifdef Q_OS_SYMBIAN
34     QAction *closeAction = new QAction(parent? tr("Back"): tr("Exit"), this);
35     closeAction->setSoftKeyRole(QAction::NegativeSoftKey);
36     connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
37 #else
38     // Tool bar
39     setUnifiedTitleAndToolBarOnMac(true);
40     toolBar = addToolBar("controls");
41     toolBar->setMovable(false);
42     toolBar->setFloatable(false);
43     toolBar->toggleViewAction()->setVisible(false);
44 #if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)
45     toolBar->setIconSize(QSize(42, 42));
46 #endif
47 #endif // Q_OS_SYMBIAN
48 }
49
50 void AdopterWindow::takeChildren(QWidget *main, const QList<QWidget *> &others)
51 {
52     Trace t("AdopterWindow::takeChildren");
53     leaveChildren();
54     if (main) {
55         mainChild = main;
56         mainChild->setParent(centralWidget());
57         centralWidget()->layout()->addWidget(mainChild);
58         mainChild->show();
59     }
60     foreach (QWidget *child, others) {
61         if (child) {
62             child->setParent(this);
63         }
64     }
65 }
66
67 void AdopterWindow::leaveChildren()
68 {
69     Trace t("AdopterWindow::leaveChildren");
70     if (mainChild) {
71         centralWidget()->layout()->removeWidget(mainChild);
72         mainChild = 0;
73     }
74 }
75
76 void AdopterWindow::grabZoomKeys(bool grab)
77 {
78     Trace t("AdopterWindow::grabZoomKeys");
79     grabbingZoomKeys = grab;
80     doGrabZoomKeys(grab);
81 }
82
83 void AdopterWindow::showEvent(QShowEvent *e)
84 {
85     Trace t("AdopterWindow::showEvent");
86     doGrabZoomKeys(grabbingZoomKeys);
87     QMainWindow::showEvent(e);
88 }
89
90 void AdopterWindow::doGrabZoomKeys(bool grab)
91 {
92     Trace t("AdopterWindow::doGrabZoomKeys");
93 #ifdef Q_WS_MAEMO_5
94     if (!isVisible()) {
95         qDebug() << "Not visible - skipping";
96     }
97     if (!winId()) {
98         qDebug() << "Could not get window ID - skipping";
99         return;
100     }
101     unsigned long val = grab? 1: 0;
102     Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
103     if (!atom) {
104         qCritical() << "Unable to obtain _HILDON_ZOOM_KEY_ATOM";
105         return;
106     }
107     XChangeProperty(QX11Info::display(),
108         winId(),
109         atom,
110         XA_INTEGER,
111         32,
112         PropModeReplace,
113         reinterpret_cast<unsigned char *>(&val),
114         1);
115 #else
116     Q_UNUSED(grab);
117 #endif // Q_WS_MAEMO_5
118 }
119
120 void AdopterWindow::show()
121 {
122 #ifdef Q_OS_SYMBIAN
123     showMaximized();
124 #else
125     QMainWindow::show();
126 #endif
127 }
128
129 QAction *AdopterWindow::addToolBarAction(QObject *receiver,
130                                          const char *member,
131                                          const QString &iconName,
132                                          const QString &text)
133 {
134     Trace t("AdopterWindow::addToolBarAction");
135     qDebug() << "icon" << iconName << "text" << text;
136 #ifndef Q_OS_SYMBIAN
137     return toolBar->addAction(QIcon(ICON_PREFIX + iconName + ".png"), text,
138                               receiver, member);
139 #else
140     Q_UNUSED(iconName);
141     QAction *action = new QAction(text, this);
142     menuBar()->addAction(action);
143     connect(action, SIGNAL(triggered()), receiver, member);
144     return action;
145 #endif
146 }
147
148 void AdopterWindow::addToolBarSpace()
149 {
150 #ifndef Q_OS_SYMBIAN
151     QFrame *frame = new QFrame(toolBar);
152     frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
153     toolBar->addWidget(frame);
154 #endif
155 }