Add back splash screen.
[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):
19     QMainWindow(parent), bookView(0), grabbingVolumeKeys(false), toolBar(0),
20     progress(0), previousButton(0), nextButton(0)
21 {
22     TRACE;
23
24 #ifdef Q_WS_MAEMO_5
25     setAttribute(Qt::WA_Maemo5StackedWindow, true);
26 #endif
27
28     QFrame *frame = new QFrame(this);
29     QVBoxLayout *layout = new QVBoxLayout(frame);
30     layout->setMargin(0);
31     frame->setLayout(layout);
32     //frame->show();
33     setCentralWidget(frame);
34
35 #ifdef Q_OS_SYMBIAN
36     QAction *closeAction = new QAction(parent? tr("Back"): tr("Exit"), this);
37     closeAction->setSoftKeyRole(QAction::NegativeSoftKey);
38     connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
39     QMainWindow::addAction(closeAction);
40 #else
41     // Tool bar
42     setUnifiedTitleAndToolBarOnMac(true);
43     toolBar = addToolBar("");
44     toolBar->setMovable(false);
45     toolBar->setFloatable(false);
46     toolBar->toggleViewAction()->setVisible(false);
47 #if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)
48     toolBar->setIconSize(QSize(42, 42));
49 #endif
50 #endif // Q_OS_SYMBIAN
51
52     // Monitor settings
53     connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
54             this, SLOT(onSettingsChanged(const QString &)));
55 }
56
57 void AdopterWindow::takeBookView(BookView *view,
58                                  Progress *prog,
59                                  TranslucentButton *previous,
60                                  TranslucentButton *next)
61 {
62     TRACE;
63
64     Q_ASSERT(view);
65     Q_ASSERT(prog);
66     Q_ASSERT(previous);
67     Q_ASSERT(next);
68
69     leaveBookView();
70
71     bookView = view;
72     bookView->setParent(this);
73     centralWidget()->layout()->addWidget(bookView);
74     bookView->show();
75
76     progress = prog;
77     previousButton = previous;
78     nextButton = next;
79     progress->setParent(this);
80     previousButton->setParent(this);
81     nextButton->setParent(this);
82 }
83
84 void AdopterWindow::leaveBookView()
85 {
86     TRACE;
87     if (bookView) {
88         bookView->hide();
89         centralWidget()->layout()->removeWidget(bookView);
90     }
91     bookView = 0;
92     progress = 0;
93     nextButton = 0;
94     previousButton = 0;
95 }
96
97 bool AdopterWindow::hasBookView()
98 {
99     return bookView != 0;
100 }
101
102 void AdopterWindow::show()
103 {
104     Trace t("AdopterWindow::show");
105 #ifdef Q_OS_SYMBIAN
106     foreach (QWidget *w, QApplication::allWidgets()) {
107         w->setContextMenuPolicy(Qt::NoContextMenu);
108     }
109     showMaximized();
110 #else
111     QMainWindow::show();
112 #endif
113 }
114
115 QAction *AdopterWindow::addToolBarAction(QObject *receiver,
116                                          const char *member,
117                                          const QString &iconName,
118                                          const QString &text,
119                                          bool important)
120 {
121     TRACE;
122     qDebug() << "icon" << iconName << "text" << text;
123     QAction *action;
124 #ifndef Q_OS_SYMBIAN
125     Q_UNUSED(important);
126     action = toolBar->addAction(QIcon(Platform::instance()->icon(iconName)),
127                                 text, receiver, member);
128 #else
129     if (!toolBar && important) {
130         // Create tool bar if needed
131         toolBar = new QToolBar("", this);
132         // toolBar->setFixedHeight(63);
133         toolBar->setStyleSheet("margin:0; border:0; padding:0");
134         toolBar->setSizePolicy(QSizePolicy::MinimumExpanding,
135                                QSizePolicy::Maximum);
136         addToolBar(Qt::BottomToolBarArea, toolBar);
137     }
138     if (important) {
139         // Add tool bar action
140         QPushButton *button = new QPushButton(this);
141         button->setIconSize(QSize(60, 60));
142         button->setFixedSize(89, 60);
143         button->setIcon(QIcon(Platform::instance()->icon(iconName)));
144         button->setSizePolicy(QSizePolicy::MinimumExpanding,
145                               QSizePolicy::Maximum);
146         connect(button, SIGNAL(clicked()), receiver, member);
147         toolBar->addWidget(button);
148     }
149     // Add menu action, too
150     action = new QAction(text, this);
151     menuBar()->addAction(action);
152     connect(action, SIGNAL(triggered()), receiver, member);
153 #endif
154
155 #if defined Q_WS_MAEMO_5
156     action->setText("");
157     action->setToolTip("");
158 #endif
159
160     return action;
161 }
162
163 void AdopterWindow::addToolBarSpace()
164 {
165 #ifndef Q_OS_SYMBIAN
166     QFrame *frame = new QFrame(toolBar);
167     frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
168     toolBar->addWidget(frame);
169 #endif
170 }
171
172 void AdopterWindow::grabVolumeKeys(bool grab)
173 {
174     TRACE;
175     grabbingVolumeKeys = grab;
176 #ifdef Q_WS_MAEMO_5
177     doGrabVolumeKeys(grab);
178 #endif
179 }
180
181 #ifdef Q_WS_MAEMO_5
182
183 void AdopterWindow::doGrabVolumeKeys(bool grab)
184 {
185     TRACE;
186     if (!isVisible()) {
187         qDebug() << "Not visible - skipping";
188         return;
189     }
190     if (!winId()) {
191         qDebug() << "Could not get window ID - skipping";
192         return;
193     }
194     unsigned long val = grab? 1: 0;
195     Atom atom =
196             XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
197     if (!atom) {
198         qCritical() << "Unable to obtain _HILDON_ZOOM_KEY_ATOM";
199         return;
200     }
201     XChangeProperty(QX11Info::display(),
202         winId(),
203         atom,
204         XA_INTEGER,
205         32,
206         PropModeReplace,
207         reinterpret_cast<unsigned char *>(&val),
208         1);
209     qDebug() << "Grabbed volume keys";
210 }
211
212 #endif // Q_WS_MAEMO_5
213
214 #ifdef Q_OS_SYMBIAN
215
216 void AdopterWindow::updateToolBar()
217 {
218     TRACE;
219     if (toolBar) {
220         QRect geometry = QApplication::desktop()->geometry();
221         bool isPortrait = geometry.width() < geometry.height();
222         bool isToolBarHidden = toolBar->isHidden();
223         if (isPortrait && isToolBarHidden) {
224             qDebug() << "Show tool bar";
225             toolBar->setVisible(true);
226         } else if (!isPortrait && !isToolBarHidden) {
227             qDebug() << "Hide tool bar";
228             toolBar->setVisible(false);
229         }
230     }
231 }
232
233 bool AdopterWindow::portrait()
234 {
235     QRect geometry = QApplication::desktop()->geometry();
236     return geometry.width() < geometry.height();
237 }
238
239 #endif // Q_OS_SYMBIAN
240
241 void AdopterWindow::showEvent(QShowEvent *e)
242 {
243     Trace t("AdopterWindow::showEvent");
244
245 #ifdef Q_OS_SYMBIAN
246     updateToolBar();
247 #endif
248     QMainWindow::showEvent(e);
249 #if defined(Q_WS_MAEMO_5)
250     doGrabVolumeKeys(grabbingVolumeKeys);
251 #endif
252     placeDecorations();
253 }
254
255 void AdopterWindow::resizeEvent(QResizeEvent *event)
256 {
257     Trace t("AdopterWindow::resizeEvent");
258 #ifdef Q_OS_SYMBIAN
259     updateToolBar();
260 #endif
261     QMainWindow::resizeEvent(event);
262     placeDecorations();
263 }
264
265 void AdopterWindow::keyPressEvent(QKeyEvent *event)
266 {
267     TRACE;
268     if (bookView && grabbingVolumeKeys) {
269         switch (event->key()) {
270 #ifdef Q_WS_MAEMO_5
271         case Qt::Key_F7:
272             qDebug() << "F7";
273             bookView->goNextPage();
274             event->accept();
275             break;
276         case Qt::Key_F8:
277             qDebug() << "F8";
278             bookView->goPreviousPage();
279             event->accept();
280             break;
281 #endif // Q_WS_MAEMO_5
282         case Qt::Key_PageUp:
283             bookView->goPreviousPage();
284             event->accept();
285             break;
286         case Qt::Key_PageDown:
287             bookView->goNextPage();
288             event->accept();
289             break;
290         default:
291             ;
292         }
293     }
294     QMainWindow::keyPressEvent(event);
295 }
296
297 void AdopterWindow::onSettingsChanged(const QString &key)
298 {
299     if (key == "usevolumekeys") {
300         bool grab = Settings::instance()->value(key, false).toBool();
301         qDebug() << "AdopterWindow::onSettingsChanged: usevolumekeys" << grab;
302         grabVolumeKeys(grab);
303     }
304 }
305
306 void AdopterWindow::placeDecorations()
307 {
308     Trace t("AdopterWindow::placeDecorations");
309
310     if (!hasBookView()) {
311         return;
312     }
313
314     int toolBarHeight = 0;
315
316     QRect geo = bookView->geometry();
317     qDebug() << "bookView:" << geo;
318
319 #ifdef Q_OS_SYMBIAN
320     // Work around Symbian bug: If tool bar is hidden, increase bottom
321     // decorator widgets' Y coordinates by the tool bar's height
322     if (toolBar && toolBar->isHidden()) {
323         toolBarHeight = toolBar->height();
324     }
325
326     // Work around another Symbian bug: When returning from full screen mode
327     // in landscape, the book view widget's height is miscalculated.
328     // My apologies for this kludge
329     if (geo.height() == 288) {
330         qDebug() << "Adjusting bottom Y";
331         toolBarHeight -= 288 - 223;
332     }
333 #endif // Q_OS_SYMBIAN
334
335     progress->setGeometry(geo.x(),
336         geo.y() + geo.height() - progress->thickness() + toolBarHeight,
337         geo.width(), progress->thickness());
338     previousButton->setGeometry(geo.x(),
339         geo.y() + geo.height() - TranslucentButton::pixels + toolBarHeight,
340         TranslucentButton::pixels, TranslucentButton::pixels);
341     nextButton->setGeometry(
342         geo.x() + geo.width() - TranslucentButton::pixels,
343         geo.y(), TranslucentButton::pixels, TranslucentButton::pixels);
344     progress->flash();
345     previousButton->flash();
346     nextButton->flash();
347     qDebug() << "progress:" << progress->geometry();
348 }