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