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