Improve initialization order.
[dorian] / adopterwindow.cpp
1 #include <QtGui>
2
3 #if defined(Q_WS_MAEMO_5)
4 #   include <QtGui/QX11Info>
5 #   include <X11/Xlib.h>
6 #   include <X11/Xatom.h>
7 #   include <QAbstractKineticScroller>
8 #endif
9
10 #include "adopterwindow.h"
11 #include "trace.h"
12 #include "bookview.h"
13 #include "platform.h"
14 #include "settings.h"
15
16 AdopterWindow::AdopterWindow(QWidget *parent):
17     QMainWindow(parent), bookView(0), grabbingVolumeKeys(false)
18 {
19     TRACE;
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 #ifdef Q_OS_SYMBIAN
32     QAction *closeAction = new QAction(parent? tr("Back"): tr("Exit"), this);
33     closeAction->setSoftKeyRole(QAction::NegativeSoftKey);
34     connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
35     QMainWindow::addAction(closeAction);
36 #else
37     // Tool bar
38     setUnifiedTitleAndToolBarOnMac(true);
39     toolBar = addToolBar("controls");
40     toolBar->setMovable(false);
41     toolBar->setFloatable(false);
42     toolBar->toggleViewAction()->setVisible(false);
43 #if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)
44     toolBar->setIconSize(QSize(42, 42));
45 #endif
46 #endif // Q_OS_SYMBIAN
47
48     // Monitor settings
49     connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
50             this, SLOT(onSettingsChanged(const QString &)));
51 }
52
53 void AdopterWindow::takeChildren(BookView *view, const QList<QWidget *> &others)
54 {
55     TRACE;
56     leaveChildren();
57     if (view) {
58         bookView = view;
59         bookView->setParent(centralWidget());
60         centralWidget()->layout()->addWidget(bookView);
61         bookView->show();
62     }
63     foreach (QWidget *child, others) {
64         if (child) {
65             child->setParent(this);
66         }
67     }
68 }
69
70 void AdopterWindow::leaveChildren()
71 {
72     TRACE;
73     if (bookView) {
74         centralWidget()->layout()->removeWidget(bookView);
75         bookView = 0;
76     }
77 }
78
79 void AdopterWindow::show()
80 {
81 #ifdef Q_OS_SYMBIAN
82     foreach (QWidget *w, QApplication::allWidgets()) {
83         w->setContextMenuPolicy(Qt::NoContextMenu);
84     }
85     showMaximized();
86     raise();
87 #else
88     QMainWindow::show();
89 #endif
90 }
91
92 QAction *AdopterWindow::addToolBarAction(QObject *receiver,
93                                          const char *member,
94                                          const QString &iconName,
95                                          const QString &text)
96 {
97     TRACE;
98     qDebug() << "icon" << iconName << "text" << text;
99 #ifndef Q_OS_SYMBIAN
100     return toolBar->addAction(QIcon(Platform::instance()->icon(iconName)),
101                               text, receiver, member);
102 #else
103     Q_UNUSED(iconName);
104     QAction *action = new QAction(text, this);
105     menuBar()->addAction(action);
106     connect(action, SIGNAL(triggered()), receiver, member);
107     return action;
108 #endif
109 }
110
111 void AdopterWindow::addToolBarSpace()
112 {
113 #ifndef Q_OS_SYMBIAN
114     QFrame *frame = new QFrame(toolBar);
115     frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
116     toolBar->addWidget(frame);
117 #endif
118 }
119
120 void AdopterWindow::grabVolumeKeys(bool grab)
121 {
122     TRACE;
123     grabbingVolumeKeys = grab;
124 #ifdef Q_WS_MAEMO_5
125     doGrabVolumeKeys(grab);
126 #endif
127 }
128
129 #ifdef Q_WS_MAEMO_5
130
131 void AdopterWindow::doGrabVolumeKeys(bool grab)
132 {
133     TRACE;
134     if (!isVisible()) {
135         qDebug() << "Not visible - skipping";
136         return;
137     }
138     if (!winId()) {
139         qDebug() << "Could not get window ID - skipping";
140         return;
141     }
142     unsigned long val = grab? 1: 0;
143     Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
144     if (!atom) {
145         qCritical() << "Unable to obtain _HILDON_ZOOM_KEY_ATOM";
146         return;
147     }
148     XChangeProperty(QX11Info::display(),
149         winId(),
150         atom,
151         XA_INTEGER,
152         32,
153         PropModeReplace,
154         reinterpret_cast<unsigned char *>(&val),
155         1);
156     qDebug() << "Grabbed volume keys";
157 }
158
159 #endif // Q_WS_MAEMO_5
160
161 #ifdef Q_WS_MAEMO_5
162
163 void AdopterWindow::showEvent(QShowEvent *e)
164 {
165     TRACE;
166     doGrabVolumeKeys(grabbingVolumeKeys);
167     QMainWindow::showEvent(e);
168 }
169
170 #endif // Q_WS_MAEMO_5
171
172 void AdopterWindow::keyPressEvent(QKeyEvent *event)
173 {
174     TRACE;
175     if (bookView && grabbingVolumeKeys) {
176         switch (event->key()) {
177 #ifdef Q_WS_MAEMO_5
178         case Qt::Key_F7:
179             qDebug() << "F7";
180             bookView->goNextPage();
181             event->accept();
182             break;
183         case Qt::Key_F8:
184             qDebug() << "F8";
185             bookView->goPreviousPage();
186             event->accept();
187             break;
188 #endif // Q_WS_MAEMO_5
189         case Qt::Key_PageUp:
190             bookView->goPreviousPage();
191             event->accept();
192             break;
193         case Qt::Key_PageDown:
194             bookView->goNextPage();
195             event->accept();
196             break;
197         default:
198             ;
199         }
200     }
201     QMainWindow::keyPressEvent(event);
202 }
203
204 void AdopterWindow::onSettingsChanged(const QString &key)
205 {
206     if (key == "usevolumekeys") {
207         bool grab = Settings::instance()->value(key, false).toBool();
208         qDebug() << "AdopterWindow::onSettingsChanged: usevolumekeys" << grab;
209         grabVolumeKeys(grab);
210     }
211 }
212