Improve traces. Simplify folder management.
[dorian] / widgets / bookwindow.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 "bookwindow.h"
10 #include "trace.h"
11
12 BookWindow::BookWindow(QWidget *parent):
13         QMainWindow(parent), grabbingZoomKeys(false), mainChild(0)
14 {
15 #ifdef Q_WS_MAEMO_5
16     setAttribute(Qt::WA_Maemo5StackedWindow, true);
17 #endif // Q_WS_MAEMO_5
18
19     QFrame *frame = new QFrame(this);
20     QVBoxLayout *layout = new QVBoxLayout(frame);
21     layout->setMargin(0);
22     frame->setLayout(layout);
23     setCentralWidget(frame);
24 }
25
26 void BookWindow::takeChildren(QWidget *main, const QList<QWidget *> &others)
27 {
28     Trace t("BookWindow::takeChildren");
29     leaveChildren();
30     if (main) {
31         mainChild = main;
32         mainChild->setParent(centralWidget());
33         centralWidget()->layout()->addWidget(mainChild);
34         mainChild->show();
35     }
36     foreach (QWidget *child, others) {
37         if (child) {
38             child->setParent(this);
39         }
40     }
41 }
42
43 void BookWindow::leaveChildren()
44 {
45     Trace t("BookWindow::leaveChildren");
46     if (mainChild) {
47         centralWidget()->layout()->removeWidget(mainChild);
48         mainChild = 0;
49     }
50 }
51
52 void BookWindow::grabZoomKeys(bool grab)
53 {
54     Trace t("BookWindow::grabZoomKeys");
55     grabbingZoomKeys = grab;
56     doGrabZoomKeys(grab);
57 }
58
59 void BookWindow::showEvent(QShowEvent *e)
60 {
61     Trace t("BookWindow::showEvent");
62     doGrabZoomKeys(grabbingZoomKeys);
63     QMainWindow::showEvent(e);
64 }
65
66 void BookWindow::doGrabZoomKeys(bool grab)
67 {
68     Trace t("BookWindow::doGrabZoomKeys");
69 #ifdef Q_WS_MAEMO_5
70     if (!isVisible()) {
71         qDebug() << "Not visible - skipping";
72     }
73     if (!winId()) {
74         qDebug() << "Could not get window ID - skipping";
75         return;
76     }
77     unsigned long val = grab? 1: 0;
78     Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
79     if (!atom) {
80         qCritical() << "Unable to obtain _HILDON_ZOOM_KEY_ATOM";
81         return;
82     }
83     XChangeProperty(QX11Info::display(),
84         winId(),
85         atom,
86         XA_INTEGER,
87         32,
88         PropModeReplace,
89         reinterpret_cast<unsigned char *>(&val),
90         1);
91 #else
92     Q_UNUSED(grab);
93 #endif // Q_WS_MAEMO_5
94 }