Initial steps to keep library in a database.
[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 t("AdopterWindow::AdopterWindow");
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 t("AdopterWindow::takeChildren");
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 t("AdopterWindow::leaveChildren");
66     if (mainChild) {
67         centralWidget()->layout()->removeWidget(mainChild);
68         mainChild = 0;
69     }
70 }
71
72 void AdopterWindow::grabZoomKeys(bool grab)
73 {
74     Trace t("AdopterWindow::grabZoomKeys");
75     grabbingZoomKeys = grab;
76     doGrabZoomKeys(grab);
77 }
78
79 void AdopterWindow::showEvent(QShowEvent *e)
80 {
81     Trace t("AdopterWindow::showEvent");
82     doGrabZoomKeys(grabbingZoomKeys);
83     QMainWindow::showEvent(e);
84 }
85
86 void AdopterWindow::doGrabZoomKeys(bool grab)
87 {
88     Trace t("AdopterWindow::doGrabZoomKeys");
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 #else
124     QMainWindow::show();
125 #endif
126 }
127
128 QAction *AdopterWindow::addToolBarAction(QObject *receiver,
129                                          const char *member,
130                                          const QString &iconName,
131                                          const QString &text)
132 {
133     Trace t("AdopterWindow::addToolBarAction");
134     qDebug() << "icon" << iconName << "text" << text;
135 #ifndef Q_OS_SYMBIAN
136     return toolBar->addAction(QIcon(Platform::icon(iconName)),
137                               text, receiver, member);
138 #else
139     Q_UNUSED(iconName);
140     QAction *action = new QAction(text, this);
141     menuBar()->addAction(action);
142     connect(action, SIGNAL(triggered()), receiver, member);
143     return action;
144 #endif
145 }
146
147 void AdopterWindow::addToolBarSpace()
148 {
149 #ifndef Q_OS_SYMBIAN
150     QFrame *frame = new QFrame(toolBar);
151     frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
152     toolBar->addWidget(frame);
153 #endif
154 }