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