Symbian fixes.
[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 #include "progress.h"
16 #include "translucentbutton.h"
17
18 AdopterWindow::AdopterWindow(QWidget *parent): MainBase(parent), bookView(0),
19     grabbingVolumeKeys(false), progress(0), previousButton(0), nextButton(0)
20 {
21     TRACE;
22
23     // Monitor settings changes
24     connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
25             this, SLOT(onSettingsChanged(const QString &)));
26
27 }
28
29 void AdopterWindow::takeBookView(BookView *view,
30                                  Progress *prog,
31                                  TranslucentButton *previous,
32                                  TranslucentButton *next)
33 {
34     TRACE;
35
36     Q_ASSERT(view);
37     Q_ASSERT(prog);
38     Q_ASSERT(previous);
39     Q_ASSERT(next);
40
41     if (bookView) {
42         return;
43     }
44
45     bookView = view;
46     bookView->setParent(this);
47     centralWidget()->layout()->addWidget(bookView);
48     // bookView->show();
49
50     progress = prog;
51     previousButton = previous;
52     nextButton = next;
53     progress->setParent(this);
54     previousButton->setParent(this);
55     nextButton->setParent(this);
56
57     // Handle page and/or volume keys
58     connect(this, SIGNAL(pageUp()), this, SLOT(onPageUp()),
59             Qt::QueuedConnection);
60     connect(this, SIGNAL(pageDown()), this, SLOT(onPageDown()),
61             Qt::QueuedConnection);
62 }
63
64 void AdopterWindow::leaveBookView()
65 {
66     TRACE;
67
68     if (!bookView) {
69         return;
70     }
71
72     // bookView->hide();
73     centralWidget()->layout()->removeWidget(bookView);
74     bookView = 0;
75     progress = 0;
76     nextButton = 0;
77     previousButton = 0;
78     disconnect(this, SLOT(onPageUp()));
79     disconnect(this, SLOT(onPageDown()));
80 }
81
82 bool AdopterWindow::hasBookView()
83 {
84     return bookView != 0;
85 }
86
87 void AdopterWindow::grabVolumeKeys(bool grab)
88 {
89     TRACE;
90     grabbingVolumeKeys = grab;
91 #ifdef Q_WS_MAEMO_5
92     doGrabVolumeKeys(grab);
93 #endif
94 }
95
96 #ifdef Q_WS_MAEMO_5
97
98 void AdopterWindow::doGrabVolumeKeys(bool grab)
99 {
100     TRACE;
101     if (!isVisible()) {
102         qDebug() << "Not visible - skipping";
103         return;
104     }
105     if (!winId()) {
106         qDebug() << "Could not get window ID - skipping";
107         return;
108     }
109     unsigned long val = grab? 1: 0;
110     Atom atom =
111             XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
112     if (!atom) {
113         qCritical() << "Unable to obtain _HILDON_ZOOM_KEY_ATOM";
114         return;
115     }
116     XChangeProperty(QX11Info::display(),
117         winId(),
118         atom,
119         XA_INTEGER,
120         32,
121         PropModeReplace,
122         reinterpret_cast<unsigned char *>(&val),
123         1);
124     qDebug() << "Grabbed volume keys";
125 }
126
127 #endif // Q_WS_MAEMO_5
128
129 void AdopterWindow::showEvent(QShowEvent *event)
130 {
131     Trace t("AdopterWindow::showEvent");
132
133     MainBase::showEvent(event);
134 #if defined(Q_WS_MAEMO_5)
135     doGrabVolumeKeys(grabbingVolumeKeys);
136 #endif
137     placeDecorations();
138 }
139
140 void AdopterWindow::resizeEvent(QResizeEvent *event)
141 {
142     Trace t("AdopterWindow::resizeEvent");
143
144     MainBase::resizeEvent(event);
145     placeDecorations();
146 #if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
147     // Restore previous reading position
148     if (bookView) {
149         bookView->scheduleRestoreLastBookmark();
150     }
151 #endif // defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
152 }
153
154 void AdopterWindow::closeEvent(QCloseEvent *event)
155 {
156     Trace t("AdopterWindow::closeEvent");
157     if (bookView) {
158         bookView->setLastBookmark();
159     }
160     hide();
161     MainBase::closeEvent(event);
162 }
163
164 void AdopterWindow::leaveEvent(QEvent *event)
165 {
166     Trace t("AdopterWindow::leaveEvent");
167     if (bookView) {
168         bookView->setLastBookmark();
169     }
170     MainBase::leaveEvent(event);
171 }
172
173 void AdopterWindow::keyPressEvent(QKeyEvent *event)
174 {
175     TRACE;
176     switch (event->key()) {
177     case Qt::Key_PageDown:
178 #ifdef Q_WS_MAEMO_5
179     case Qt::Key_F7:
180 #endif
181         emit pageDown();
182         event->accept();
183         break;
184     case Qt::Key_PageUp:
185 #ifdef Q_WS_MAEMO_5
186     case Qt::Key_F8:
187 #endif
188         emit pageUp();
189         event->accept();
190         break;
191     default:
192         ;
193     }
194     MainBase::keyPressEvent(event);
195 }
196
197 void AdopterWindow::onSettingsChanged(const QString &key)
198 {
199     if (key == "usevolumekeys") {
200         bool grab = Settings::instance()->value(key, false).toBool();
201         qDebug() << "AdopterWindow::onSettingsChanged: usevolumekeys" << grab;
202         grabVolumeKeys(grab);
203     }
204 }
205
206 void AdopterWindow::placeDecorations()
207 {
208     Trace t("AdopterWindow::placeDecorations");
209
210     if (!hasBookView()) {
211         qDebug() << "Doesn't have the book view";
212         return;
213     }
214
215     qDebug() << "Has the book view";
216     int extraHeight = 0;
217
218     QRect geo = bookView->geometry();
219     qDebug() << "bookView:" << geo;
220
221     progress->setGeometry(geo.x(),
222         geo.y() + geo.height() - progress->thickness() + extraHeight,
223         geo.width(), progress->thickness());
224     previousButton->setGeometry(geo.x(),
225         geo.y() + geo.height() - TranslucentButton::pixels + extraHeight,
226         TranslucentButton::pixels, TranslucentButton::pixels);
227     nextButton->setGeometry(geo.x() + geo.width() - TranslucentButton::pixels,
228         geo.y(), TranslucentButton::pixels, TranslucentButton::pixels);
229     progress->flash();
230     previousButton->flash();
231     nextButton->flash();
232     qDebug() << "progress:" << progress->geometry();
233 }
234
235 void AdopterWindow::onPageUp()
236 {
237     if (bookView && grabbingVolumeKeys) {
238         setEnabled(false);
239         bookView->goPreviousPage();
240         setEnabled(true);
241     }
242 }
243
244 void AdopterWindow::onPageDown()
245 {
246     if (bookView && grabbingVolumeKeys) {
247         setEnabled(false);
248         bookView->goNextPage();
249         setEnabled(true);
250     }
251 }