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