2cdf4dd042f24cc4ac6c60ef4cdc3e6bcc5da3db
[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
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     leaveBookView();
42
43     bookView = view;
44     bookView->setParent(this);
45     centralWidget()->layout()->addWidget(bookView);
46     bookView->show();
47
48     progress = prog;
49     previousButton = previous;
50     nextButton = next;
51     progress->setParent(this);
52     previousButton->setParent(this);
53     nextButton->setParent(this);
54
55     // Handle page and/or volume keys
56     connect(this, SIGNAL(pageUp()), this, SLOT(onPageUp()),
57             Qt::QueuedConnection);
58     connect(this, SIGNAL(pageDown()), this, SLOT(onPageDown()),
59             Qt::QueuedConnection);
60 }
61
62 void AdopterWindow::leaveBookView()
63 {
64     TRACE;
65     if (bookView) {
66         bookView->hide();
67         centralWidget()->layout()->removeWidget(bookView);
68     }
69     bookView = 0;
70     progress = 0;
71     nextButton = 0;
72     previousButton = 0;
73     disconnect(this, SLOT(onPageUp()));
74     disconnect(this, SLOT(onPageDown()));
75 }
76
77 bool AdopterWindow::hasBookView()
78 {
79     return bookView != 0;
80 }
81
82 void AdopterWindow::grabVolumeKeys(bool grab)
83 {
84     TRACE;
85     grabbingVolumeKeys = grab;
86 #ifdef Q_WS_MAEMO_5
87     doGrabVolumeKeys(grab);
88 #endif
89 }
90
91 #ifdef Q_WS_MAEMO_5
92
93 void AdopterWindow::doGrabVolumeKeys(bool grab)
94 {
95     TRACE;
96     if (!isVisible()) {
97         qDebug() << "Not visible - skipping";
98         return;
99     }
100     if (!winId()) {
101         qDebug() << "Could not get window ID - skipping";
102         return;
103     }
104     unsigned long val = grab? 1: 0;
105     Atom atom =
106             XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
107     if (!atom) {
108         qCritical() << "Unable to obtain _HILDON_ZOOM_KEY_ATOM";
109         return;
110     }
111     XChangeProperty(QX11Info::display(),
112         winId(),
113         atom,
114         XA_INTEGER,
115         32,
116         PropModeReplace,
117         reinterpret_cast<unsigned char *>(&val),
118         1);
119     qDebug() << "Grabbed volume keys";
120 }
121
122 #endif // Q_WS_MAEMO_5
123
124 void AdopterWindow::showEvent(QShowEvent *e)
125 {
126     Trace t("AdopterWindow::showEvent");
127
128     MainBase::showEvent(e);
129 #if defined(Q_WS_MAEMO_5)
130     doGrabVolumeKeys(grabbingVolumeKeys);
131 #endif
132     placeDecorations();
133 }
134
135 void AdopterWindow::resizeEvent(QResizeEvent *event)
136 {
137     Trace t("AdopterWindow::resizeEvent");
138 #if defined(Q_OS_SYMBIAN)
139     if (bookView) {
140         bookView->setLastBookmark();
141     }
142 #endif
143     MainBase::resizeEvent(event);
144     placeDecorations();
145 #if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
146     if (bookView) {
147         QTimer::singleShot(110, bookView, SLOT(restoreLastBookmark()));
148     }
149 #endif
150 }
151
152 void AdopterWindow::closeEvent(QCloseEvent *event)
153 {
154     Trace t("AdopterWindow::closeEvent");
155     if (bookView) {
156         bookView->setLastBookmark();
157     }
158     MainBase::closeEvent(event);
159 }
160
161 void AdopterWindow::leaveEvent(QEvent *event)
162 {
163     Trace t("AdopterWindow::leaveEvent");
164     if (bookView) {
165         bookView->setLastBookmark();
166     }
167     MainBase::leaveEvent(event);
168 }
169
170 void AdopterWindow::keyPressEvent(QKeyEvent *event)
171 {
172     TRACE;
173     switch (event->key()) {
174     case Qt::Key_PageDown:
175 #ifdef Q_WS_MAEMO_5
176     case Qt::Key_F7:
177 #endif
178         emit pageDown();
179         event->accept();
180         break;
181     case Qt::Key_PageUp:
182 #ifdef Q_WS_MAEMO_5
183     case Qt::Key_F8:
184 #endif
185         emit pageUp();
186         event->accept();
187         break;
188     default:
189         ;
190     }
191     MainBase::keyPressEvent(event);
192 }
193
194 void AdopterWindow::onSettingsChanged(const QString &key)
195 {
196     if (key == "usevolumekeys") {
197         bool grab = Settings::instance()->value(key, false).toBool();
198         qDebug() << "AdopterWindow::onSettingsChanged: usevolumekeys" << grab;
199         grabVolumeKeys(grab);
200     }
201 }
202
203 void AdopterWindow::placeDecorations()
204 {
205     Trace t("AdopterWindow::placeDecorations");
206
207     if (!hasBookView()) {
208         return;
209     }
210
211     int extraHeight = 0;
212
213     QRect geo = bookView->geometry();
214     qDebug() << "bookView:" << geo;
215
216 #ifdef Q_OS_SYMBIAN
217     // Work around Symbian bug: If tool bar is hidden, increase bottom
218     // decorator widgets' Y coordinates by the tool bar's height
219     if (isToolBarHidden()) {
220         extraHeight = toolBarHeight();
221     }
222
223     // Work around another Symbian bug: When returning from full screen mode
224     // in landscape, the book view widget's height is miscalculated.
225     // My apologies for this kludge
226     if (geo.height() == 288) {
227         qDebug() << "Adjusting bottom Y";
228         extraHeight -= 288 - 223;
229     }
230 #endif // Q_OS_SYMBIAN
231
232     progress->setGeometry(geo.x(),
233         geo.y() + geo.height() - progress->thickness() + extraHeight,
234         geo.width(), progress->thickness());
235     previousButton->setGeometry(geo.x(),
236         geo.y() + geo.height() - TranslucentButton::pixels + extraHeight,
237         TranslucentButton::pixels, TranslucentButton::pixels);
238     nextButton->setGeometry(geo.x() + geo.width() - TranslucentButton::pixels,
239         geo.y(), TranslucentButton::pixels, TranslucentButton::pixels);
240     progress->flash();
241     previousButton->flash();
242     nextButton->flash();
243     qDebug() << "progress:" << progress->geometry();
244 }
245
246 void AdopterWindow::onPageUp()
247 {
248     if (bookView && grabbingVolumeKeys) {
249         setEnabled(false);
250         bookView->goPreviousPage();
251         setEnabled(true);
252     }
253 }
254
255 void AdopterWindow::onPageDown()
256 {
257     if (bookView && grabbingVolumeKeys) {
258         setEnabled(false);
259         bookView->goNextPage();
260         setEnabled(true);
261     }
262 }