Fix forward navigation control on Linux.
[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 t("AdopterWindow::keyPressEvent");
176
177     switch (event->key()) {
178     case Qt::Key_PageDown:
179 #ifdef Q_WS_MAEMO_5
180     case Qt::Key_F7:
181 #endif
182         emit pageDown();
183         event->accept();
184         break;
185     case Qt::Key_PageUp:
186 #ifdef Q_WS_MAEMO_5
187     case Qt::Key_F8:
188 #endif
189         emit pageUp();
190         event->accept();
191         break;
192     default:
193         ;
194     }
195     MainBase::keyPressEvent(event);
196 }
197
198 void AdopterWindow::onSettingsChanged(const QString &key)
199 {
200     if (key == "usevolumekeys") {
201         bool grab = Settings::instance()->value(key, false).toBool();
202         qDebug() << "AdopterWindow::onSettingsChanged: usevolumekeys" << grab;
203         grabVolumeKeys(grab);
204     }
205 }
206
207 void AdopterWindow::placeDecorations()
208 {
209     Trace t("AdopterWindow::placeDecorations");
210
211     if (!hasBookView()) {
212         qDebug() << "Doesn't have the book view";
213         return;
214     }
215
216     qDebug() << "Has the book view";
217     int extraHeight = 0;
218
219 #if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)
220     extraHeight += toolBarHeight();
221 #endif
222
223     QRect geo = bookView->geometry();
224     qDebug() << "bookView:" << geo;
225
226     progress->setGeometry(geo.x(),
227         geo.y() + geo.height() - progress->thickness() + extraHeight,
228         geo.width(), progress->thickness());
229     previousButton->setGeometry(geo.x(),
230         geo.y() + geo.height() - TranslucentButton::pixels + extraHeight,
231         TranslucentButton::pixels, TranslucentButton::pixels);
232     nextButton->setGeometry(
233         geo.x() + geo.width() - TranslucentButton::pixels,
234         geo.y() + extraHeight, TranslucentButton::pixels,
235         TranslucentButton::pixels);
236     progress->flash();
237     previousButton->flash();
238     nextButton->flash();
239     qDebug() << "progress:" << progress->geometry();
240 }
241
242 void AdopterWindow::onPageUp()
243 {
244     if (bookView && grabbingVolumeKeys) {
245         setEnabled(false);
246         bookView->goPreviousPage();
247         setEnabled(true);
248     }
249 }
250
251 void AdopterWindow::onPageDown()
252 {
253     if (bookView && grabbingVolumeKeys) {
254         setEnabled(false);
255         bookView->goNextPage();
256         setEnabled(true);
257     }
258 }