173ed8b6b36d59ee038d831e312a2955fbdf4265
[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     MainBase::resizeEvent(event);
139     placeDecorations();
140
141 #if defined(Q_WS_MAEMO_5)
142     // Restore previous reading position
143     if (!bookView) {
144         return;
145     }
146     QTimer::singleShot(110, bookView, SLOT(restoreLastBookmark()));
147 #endif // Q_WS_MAEMO_5
148
149 #if defined(Q_OS_SYMBIAN)
150     // Adjust reading position after orientation change
151     if (!bookView) {
152         return;
153     }
154     if (event->oldSize().width() == -1) {
155         return;
156     }
157     bool oldPortrait = event->oldSize().width() < event->oldSize().height();
158     bool portrait = event->size().width() < event->size().height();
159     if (oldPortrait == portrait) {
160         return;
161     }
162     QTimer::singleShot(990, bookView, SLOT(adjustPosition()));
163 #endif // Q_OS_SYMBIAN
164 }
165
166 void AdopterWindow::closeEvent(QCloseEvent *event)
167 {
168     Trace t("AdopterWindow::closeEvent");
169     if (bookView) {
170         bookView->setLastBookmark();
171     }
172     MainBase::closeEvent(event);
173 }
174
175 void AdopterWindow::leaveEvent(QEvent *event)
176 {
177     Trace t("AdopterWindow::leaveEvent");
178     if (bookView) {
179         bookView->setLastBookmark();
180     }
181     MainBase::leaveEvent(event);
182 }
183
184 void AdopterWindow::keyPressEvent(QKeyEvent *event)
185 {
186     TRACE;
187     switch (event->key()) {
188     case Qt::Key_PageDown:
189 #ifdef Q_WS_MAEMO_5
190     case Qt::Key_F7:
191 #endif
192         emit pageDown();
193         event->accept();
194         break;
195     case Qt::Key_PageUp:
196 #ifdef Q_WS_MAEMO_5
197     case Qt::Key_F8:
198 #endif
199         emit pageUp();
200         event->accept();
201         break;
202     default:
203         ;
204     }
205     MainBase::keyPressEvent(event);
206 }
207
208 void AdopterWindow::onSettingsChanged(const QString &key)
209 {
210     if (key == "usevolumekeys") {
211         bool grab = Settings::instance()->value(key, false).toBool();
212         qDebug() << "AdopterWindow::onSettingsChanged: usevolumekeys" << grab;
213         grabVolumeKeys(grab);
214     }
215 }
216
217 void AdopterWindow::placeDecorations()
218 {
219     Trace t("AdopterWindow::placeDecorations");
220
221     if (!hasBookView()) {
222         return;
223     }
224
225     int extraHeight = 0;
226
227     QRect geo = bookView->geometry();
228     qDebug() << "bookView:" << geo;
229
230 #ifdef Q_OS_SYMBIAN
231     // Work around Symbian bug: If tool bar is hidden, increase bottom
232     // decorator widgets' Y coordinates by the tool bar's height
233     if (isToolBarHidden()) {
234         extraHeight = toolBarHeight();
235     }
236
237     // Work around another Symbian bug: When returning from full screen mode
238     // in landscape, the book view widget's height is miscalculated.
239     // My apologies for this kludge
240     if (geo.height() == 288) {
241         qDebug() << "Adjusting bottom Y";
242         extraHeight -= 288 - 223;
243     }
244 #endif // Q_OS_SYMBIAN
245
246     progress->setGeometry(geo.x(),
247         geo.y() + geo.height() - progress->thickness() + extraHeight,
248         geo.width(), progress->thickness());
249     previousButton->setGeometry(geo.x(),
250         geo.y() + geo.height() - TranslucentButton::pixels + extraHeight,
251         TranslucentButton::pixels, TranslucentButton::pixels);
252     nextButton->setGeometry(geo.x() + geo.width() - TranslucentButton::pixels,
253         geo.y(), TranslucentButton::pixels, TranslucentButton::pixels);
254     progress->flash();
255     previousButton->flash();
256     nextButton->flash();
257     qDebug() << "progress:" << progress->geometry();
258 }
259
260 void AdopterWindow::onPageUp()
261 {
262     if (bookView && grabbingVolumeKeys) {
263         setEnabled(false);
264         bookView->goPreviousPage();
265         setEnabled(true);
266     }
267 }
268
269 void AdopterWindow::onPageDown()
270 {
271     if (bookView && grabbingVolumeKeys) {
272         setEnabled(false);
273         bookView->goNextPage();
274         setEnabled(true);
275     }
276 }