Make it build on Windows. Fix traces on Windows. Abstract out platform specific actio...
[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 #ifdef Q_WS_MAEMO_5
22     setAttribute(Qt::WA_Maemo5StackedWindow, true);
23 #endif // Q_WS_MAEMO_5
24
25     QFrame *frame = new QFrame(this);
26     QVBoxLayout *layout = new QVBoxLayout(frame);
27     layout->setMargin(0);
28     frame->setLayout(layout);
29     setCentralWidget(frame);
30
31 #ifndef Q_OS_SYMBIAN
32     // Tool bar
33     setUnifiedTitleAndToolBarOnMac(true);
34     toolBar = addToolBar("controls");
35     toolBar->setMovable(false);
36     toolBar->setFloatable(false);
37     toolBar->toggleViewAction()->setVisible(false);
38 #if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)
39     toolBar->setIconSize(QSize(42, 42));
40 #endif
41 #endif // Q_OS_SYMBIAN
42 }
43
44 void AdopterWindow::takeChildren(QWidget *main, const QList<QWidget *> &others)
45 {
46     Trace t("AdopterWindow::takeChildren");
47     leaveChildren();
48     if (main) {
49         mainChild = main;
50         mainChild->setParent(centralWidget());
51         centralWidget()->layout()->addWidget(mainChild);
52         mainChild->show();
53     }
54     foreach (QWidget *child, others) {
55         if (child) {
56             child->setParent(this);
57         }
58     }
59 }
60
61 void AdopterWindow::leaveChildren()
62 {
63     Trace t("AdopterWindow::leaveChildren");
64     if (mainChild) {
65         centralWidget()->layout()->removeWidget(mainChild);
66         mainChild = 0;
67     }
68 }
69
70 void AdopterWindow::grabZoomKeys(bool grab)
71 {
72     Trace t("AdopterWindow::grabZoomKeys");
73     grabbingZoomKeys = grab;
74     doGrabZoomKeys(grab);
75 }
76
77 void AdopterWindow::showEvent(QShowEvent *e)
78 {
79     Trace t("AdopterWindow::showEvent");
80     doGrabZoomKeys(grabbingZoomKeys);
81     QMainWindow::showEvent(e);
82 }
83
84 void AdopterWindow::doGrabZoomKeys(bool grab)
85 {
86     Trace t("AdopterWindow::doGrabZoomKeys");
87 #ifdef Q_WS_MAEMO_5
88     if (!isVisible()) {
89         qDebug() << "Not visible - skipping";
90     }
91     if (!winId()) {
92         qDebug() << "Could not get window ID - skipping";
93         return;
94     }
95     unsigned long val = grab? 1: 0;
96     Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
97     if (!atom) {
98         qCritical() << "Unable to obtain _HILDON_ZOOM_KEY_ATOM";
99         return;
100     }
101     XChangeProperty(QX11Info::display(),
102         winId(),
103         atom,
104         XA_INTEGER,
105         32,
106         PropModeReplace,
107         reinterpret_cast<unsigned char *>(&val),
108         1);
109 #else
110     Q_UNUSED(grab);
111 #endif // Q_WS_MAEMO_5
112 }
113
114 void AdopterWindow::show()
115 {
116 #ifdef Q_OS_SYMBIAN
117     showMaximized();
118 #else
119     QMainWindow::show();
120 #endif
121 }
122
123 QAction *AdopterWindow::addToolBarAction(QObject *receiver,
124                                          const char *member,
125                                          const QString &iconName,
126                                          const QString &text)
127 {
128 #ifndef Q_OS_SYMBIAN
129     return toolBar->addAction(QIcon(ICON_PREFIX + iconName + ".png"), text,
130                               receiver, member);
131 #else
132     Q_UNUSED(iconName);
133     QAction *action = new QAction(text, this);
134     menuBar()->addAction(action);
135     connect(action, SIGNAL(triggered()), receiver, member);
136     return action;
137 #endif
138 }