Simplify in/out traces.
[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 "platform.h"
11 #include "trace.h"
12
13 AdopterWindow::AdopterWindow(QWidget *parent):
14         QMainWindow(parent), grabbingZoomKeys(false), mainChild(0)
15 {
16     TRACE;
17
18 #ifdef Q_WS_MAEMO_5
19     setAttribute(Qt::WA_Maemo5StackedWindow, true);
20 #endif // Q_WS_MAEMO_5
21
22     QFrame *frame = new QFrame(this);
23     QVBoxLayout *layout = new QVBoxLayout(frame);
24     layout->setMargin(0);
25     frame->setLayout(layout);
26     setCentralWidget(frame);
27
28 #ifdef Q_OS_SYMBIAN
29     QAction *closeAction = new QAction(parent? tr("Back"): tr("Exit"), this);
30     closeAction->setSoftKeyRole(QAction::NegativeSoftKey);
31     connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
32     QMainWindow::addAction(closeAction);
33 #else
34     // Tool bar
35     setUnifiedTitleAndToolBarOnMac(true);
36     toolBar = addToolBar("controls");
37     toolBar->setMovable(false);
38     toolBar->setFloatable(false);
39     toolBar->toggleViewAction()->setVisible(false);
40 #if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)
41     toolBar->setIconSize(QSize(42, 42));
42 #endif
43 #endif // Q_OS_SYMBIAN
44 }
45
46 void AdopterWindow::takeChildren(QWidget *main, const QList<QWidget *> &others)
47 {
48     TRACE;
49     leaveChildren();
50     if (main) {
51         mainChild = main;
52         mainChild->setParent(centralWidget());
53         centralWidget()->layout()->addWidget(mainChild);
54         mainChild->show();
55     }
56     foreach (QWidget *child, others) {
57         if (child) {
58             child->setParent(this);
59         }
60     }
61 }
62
63 void AdopterWindow::leaveChildren()
64 {
65     TRACE;
66     if (mainChild) {
67         centralWidget()->layout()->removeWidget(mainChild);
68         mainChild = 0;
69     }
70 }
71
72 void AdopterWindow::grabZoomKeys(bool grab)
73 {
74     TRACE;
75     grabbingZoomKeys = grab;
76     doGrabZoomKeys(grab);
77 }
78
79 void AdopterWindow::showEvent(QShowEvent *e)
80 {
81     TRACE;
82     doGrabZoomKeys(grabbingZoomKeys);
83     QMainWindow::showEvent(e);
84 }
85
86 void AdopterWindow::doGrabZoomKeys(bool grab)
87 {
88     TRACE;
89 #ifdef Q_WS_MAEMO_5
90     if (!isVisible()) {
91         qDebug() << "Not visible - skipping";
92     }
93     if (!winId()) {
94         qDebug() << "Could not get window ID - skipping";
95         return;
96     }
97     unsigned long val = grab? 1: 0;
98     Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
99     if (!atom) {
100         qCritical() << "Unable to obtain _HILDON_ZOOM_KEY_ATOM";
101         return;
102     }
103     XChangeProperty(QX11Info::display(),
104         winId(),
105         atom,
106         XA_INTEGER,
107         32,
108         PropModeReplace,
109         reinterpret_cast<unsigned char *>(&val),
110         1);
111 #else
112     Q_UNUSED(grab);
113 #endif // Q_WS_MAEMO_5
114 }
115
116 void AdopterWindow::show()
117 {
118 #ifdef Q_OS_SYMBIAN
119     foreach (QWidget *w, QApplication::allWidgets()) {
120         w->setContextMenuPolicy(Qt::NoContextMenu);
121     }
122     showMaximized();
123     raise();
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;
135     qDebug() << "icon" << iconName << "text" << text;
136 #ifndef Q_OS_SYMBIAN
137     return toolBar->addAction(QIcon(Platform::icon(iconName)),
138                               text, 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 }