0c734c0e27bb1343a95595c5a3280a80df763f76
[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     QMainWindow::addAction(closeAction);
38 #else
39     // Tool bar
40     setUnifiedTitleAndToolBarOnMac(true);
41     toolBar = addToolBar("controls");
42     toolBar->setMovable(false);
43     toolBar->setFloatable(false);
44     toolBar->toggleViewAction()->setVisible(false);
45 #if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)
46     toolBar->setIconSize(QSize(42, 42));
47 #endif
48 #endif // Q_OS_SYMBIAN
49 }
50
51 void AdopterWindow::takeChildren(QWidget *main, const QList<QWidget *> &others)
52 {
53     Trace t("AdopterWindow::takeChildren");
54     leaveChildren();
55     if (main) {
56         mainChild = main;
57         mainChild->setParent(centralWidget());
58         centralWidget()->layout()->addWidget(mainChild);
59         mainChild->show();
60     }
61     foreach (QWidget *child, others) {
62         if (child) {
63             child->setParent(this);
64         }
65     }
66 }
67
68 void AdopterWindow::leaveChildren()
69 {
70     Trace t("AdopterWindow::leaveChildren");
71     if (mainChild) {
72         centralWidget()->layout()->removeWidget(mainChild);
73         mainChild = 0;
74     }
75 }
76
77 void AdopterWindow::grabZoomKeys(bool grab)
78 {
79     Trace t("AdopterWindow::grabZoomKeys");
80     grabbingZoomKeys = grab;
81     doGrabZoomKeys(grab);
82 }
83
84 void AdopterWindow::showEvent(QShowEvent *e)
85 {
86     Trace t("AdopterWindow::showEvent");
87     doGrabZoomKeys(grabbingZoomKeys);
88     QMainWindow::showEvent(e);
89 }
90
91 void AdopterWindow::doGrabZoomKeys(bool grab)
92 {
93     Trace t("AdopterWindow::doGrabZoomKeys");
94 #ifdef Q_WS_MAEMO_5
95     if (!isVisible()) {
96         qDebug() << "Not visible - skipping";
97     }
98     if (!winId()) {
99         qDebug() << "Could not get window ID - skipping";
100         return;
101     }
102     unsigned long val = grab? 1: 0;
103     Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
104     if (!atom) {
105         qCritical() << "Unable to obtain _HILDON_ZOOM_KEY_ATOM";
106         return;
107     }
108     XChangeProperty(QX11Info::display(),
109         winId(),
110         atom,
111         XA_INTEGER,
112         32,
113         PropModeReplace,
114         reinterpret_cast<unsigned char *>(&val),
115         1);
116 #else
117     Q_UNUSED(grab);
118 #endif // Q_WS_MAEMO_5
119 }
120
121 void AdopterWindow::show()
122 {
123 #ifdef Q_OS_SYMBIAN
124     foreach (QWidget *w, QApplication::allWidgets()) {
125         w->setContextMenuPolicy(Qt::NoContextMenu);
126     }
127     showMaximized();
128 #else
129     QMainWindow::show();
130 #endif
131 }
132
133 QAction *AdopterWindow::addToolBarAction(QObject *receiver,
134                                          const char *member,
135                                          const QString &iconName,
136                                          const QString &text)
137 {
138     Trace t("AdopterWindow::addToolBarAction");
139     qDebug() << "icon" << iconName << "text" << text;
140 #ifndef Q_OS_SYMBIAN
141     return toolBar->addAction(QIcon(ICON_PREFIX + iconName + ".png"), text,
142                               receiver, member);
143 #else
144     Q_UNUSED(iconName);
145     QAction *action = new QAction(text, this);
146     menuBar()->addAction(action);
147     connect(action, SIGNAL(triggered()), receiver, member);
148     return action;
149 #endif
150 }
151
152 void AdopterWindow::addToolBarSpace()
153 {
154 #ifndef Q_OS_SYMBIAN
155     QFrame *frame = new QFrame(toolBar);
156     frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
157     toolBar->addWidget(frame);
158 #endif
159 }