Symbian fixes: full screen mode, dialog softkeys.
[dorian] / mainwindow.cpp
1 #include <QtGui>
2 #include <QtDebug>
3 #include <QDir>
4 #include <QApplication>
5 #include <QFileInfo>
6 #include <QStringList>
7 #include <QWebView>
8 #include <QWebFrame>
9
10 #ifdef Q_WS_MAEMO_5
11 #   include <QtMaemo5/QMaemo5InformationBox>
12 #   include <QtDBus>
13 #   include <QtGui/QX11Info>
14 #   include <X11/Xlib.h>
15 #   include <X11/Xatom.h>
16 #   include <mce/mode-names.h>
17 #   include <mce/dbus-names.h>
18 #endif // Q_WS_MAEMO_5
19
20 #include "bookview.h"
21 #include "book.h"
22 #include "library.h"
23 #include "infodialog.h"
24 #include "librarydialog.h"
25 #include "devtools.h"
26 #include "mainwindow.h"
27 #include "settingswindow.h"
28 #include "bookmarksdialog.h"
29 #include "settings.h"
30 #include "chaptersdialog.h"
31 #include "fullscreenwindow.h"
32 #include "trace.h"
33 #include "bookfinder.h"
34 #include "progress.h"
35 #include "dyalog.h"
36 #include "translucentbutton.h"
37 #include "platform.h"
38 #include "progressdialog.h"
39
40 #ifdef DORIAN_TEST_MODEL
41 #   include "modeltest.h"
42 #endif
43
44 const int DORIAN_PROGRESS_HEIGHT = 17;
45
46 MainWindow::MainWindow(QWidget *parent):
47     AdopterWindow(parent), view(0), preventBlankingTimer(-1)
48 {
49     Trace t("MainWindow::MainWindow");
50 #ifdef Q_WS_MAEMO_5
51     setAttribute(Qt::WA_Maemo5StackedWindow, true);
52 #endif
53     setWindowTitle("Dorian");
54
55     // Central widget. Must be an intermediate, because the book view widget
56     // can be re-parented later
57     QFrame *central = new QFrame(this);
58     QVBoxLayout *layout = new QVBoxLayout(central);
59     layout->setMargin(0);
60     central->setLayout(layout);
61     setCentralWidget(central);
62
63     // Book view
64     view = new BookView(central);
65     view->show();
66     layout->addWidget(view);
67
68     // Progress
69     progress = new Progress(central);
70
71     // Settings dialog
72     settings = new QDialog(this);
73
74     // Tool bar actions
75
76 #ifdef Q_OS_SYMBIAN
77     fullScreenAction = addToolBarAction(this, SLOT(showBig()),
78                                         "view-fullscreen", tr("Full screen"));
79 #endif
80
81     chaptersAction = addToolBarAction(this, SLOT(showChapters()),
82                                       "chapters", tr("Chapters"));
83     bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
84                                        "bookmarks", tr("Bookmarks"));
85     infoAction = addToolBarAction(this, SLOT(showInfo()),
86                                   "info", tr("Book info"));
87     libraryAction = addToolBarAction(this, SLOT(showLibrary()),
88                                      "library", tr("Library"));
89
90 #ifdef Q_WS_MAEMO_5
91     settingsAction = menuBar()->addAction(tr("Settings"));
92     connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
93     devToolsAction = menuBar()->addAction(tr("Developer"));
94     connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
95     QAction *aboutAction = menuBar()->addAction(tr("About"));
96     connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
97 #else
98     settingsAction = addToolBarAction(this, SLOT(showSettings()),
99                                       "preferences-system", tr("Settings"));
100     devToolsAction = addToolBarAction(this, SLOT(showDevTools()),
101                                       "developer", tr("Developer"));
102     addToolBarAction(this, SLOT(about()), "about", tr("About"));
103 #endif // Q_WS_MAEMO_5
104
105 #ifndef Q_OS_SYMBIAN
106     addToolBarSpace();
107     fullScreenAction = addToolBarAction(this, SLOT(showBig()),
108                                         "view-fullscreen", tr("Full screen"));
109 #endif
110
111     // Buttons on top of the book view
112     previousButton = new TranslucentButton("back", this);
113     nextButton = new TranslucentButton("forward", this);
114
115     // Handle model changes
116     connect(Library::instance(), SIGNAL(nowReadingChanged()),
117             this, SLOT(onCurrentBookChanged()));
118
119     // Load library, upgrade it if needed
120     libraryProgress = new ProgressDialog(tr("Upgrading library"), this);
121     Library *library = Library::instance();
122     connect(library, SIGNAL(beginUpgrade(int)), this, SLOT(onBeginUpgrade(int)));
123     connect(library, SIGNAL(upgrading(const QString &)),
124             this, SLOT(onUpgrading(const QString &)));
125     connect(library, SIGNAL(endUpgrade()), this, SLOT(onEndUpgrade()));
126     connect(library, SIGNAL(beginLoad(int)), this, SLOT(onBeginLoad(int)));
127     connect(library, SIGNAL(loading(const QString &)),
128             this, SLOT(onLoading(const QString &)));
129     connect(library, SIGNAL(endLoad()), this, SLOT(onEndLoad()));
130     library->upgrade();
131     library->load();
132
133     // Load book on command line, or load last read book, or load default book
134     if (QCoreApplication::arguments().size() == 2) {
135         QString path = QCoreApplication::arguments()[1];
136         library->add(path);
137         QModelIndex index = library->find(path);
138         if (index.isValid()) {
139             library->setNowReading(index);
140         }
141     } else {
142         QModelIndex index = library->nowReading();
143         if (index.isValid()) {
144             library->setNowReading(index);
145         } else {
146             if (!library->rowCount()) {
147                 library->add(":/books/2BR02B.epub");
148             }
149             library->setNowReading(library->index(0));
150         }
151     }
152
153     // Handle loading book parts
154     connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
155     connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
156
157     // Handle progress
158     connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
159
160     // Shadow window for full screen reading
161     fullScreenWindow = new FullScreenWindow(this);
162     connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
163
164     // Handle settings changes
165     Settings *settings = Settings::instance();
166     connect(settings, SIGNAL(valueChanged(const QString &)),
167             this, SLOT(onSettingsChanged(const QString &)));
168     settings->setValue("orientation", settings->value("orientation"));
169     settings->setValue("lightson", settings->value("lightson"));
170     settings->setValue("usevolumekeys", settings->value("usevolumekeys"));
171
172     // Handle book view buttons
173     connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
174     connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
175
176 #ifdef DORIAN_TEST_MODEL
177     (void)new ModelTest(Library::instance(), this);
178 #endif
179 }
180
181 MainWindow::~MainWindow()
182 {
183 }
184
185 void MainWindow::onCurrentBookChanged()
186 {
187     setCurrentBook(Library::instance()->nowReading());
188 }
189
190 void MainWindow::showRegular()
191 {
192     Trace t("MainWindow::showRegular");
193
194     // Re-parent children
195     fullScreenWindow->leaveChildren();
196     QList<QWidget *> otherChildren;
197     otherChildren << progress << previousButton << nextButton;
198     takeChildren(view, otherChildren);
199
200     // Adjust geometry of decorations
201     QRect geo = geometry();
202     progress->setGeometry(0, 0, geo.width(), DORIAN_PROGRESS_HEIGHT);
203 #if defined(Q_WS_MAEMO_5)
204     previousButton->setGeometry(0,
205         geo.height() - toolBar->height() - TranslucentButton::pixels,
206         TranslucentButton::pixels, TranslucentButton::pixels);
207     nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
208         TranslucentButton::pixels, TranslucentButton::pixels);
209 #elif defined(Q_OS_SYMBIAN)
210     previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
211         TranslucentButton::pixels, TranslucentButton::pixels);
212     nextButton->setGeometry(geo.width() - TranslucentButton::pixels,
213         0, TranslucentButton::pixels, TranslucentButton::pixels);
214 #else
215     previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
216         TranslucentButton::pixels, TranslucentButton::pixels);
217     nextButton->setGeometry(geo.width() - TranslucentButton::pixels - 25,
218         toolBar->height(), TranslucentButton::pixels,
219         TranslucentButton::pixels);
220 #endif // Q_WS_MAEMO_5
221     qDebug() << "previousButton geometry" << previousButton->geometry();
222
223     fullScreenWindow->hide();
224     show();
225     activateWindow();
226     progress->flash();
227     nextButton->flash(1500);
228     previousButton->flash(1500);
229 }
230
231 void MainWindow::showBig()
232 {
233     Trace t("MainWindow::showBig");
234
235     // Re-parent children
236     leaveChildren();
237     QList<QWidget *> otherChildren;
238     otherChildren << progress << nextButton << previousButton;
239     fullScreenWindow->takeChildren(view, otherChildren);
240
241     // Adjust geometry of decorations
242     QRect screen = QApplication::desktop()->screenGeometry();
243     progress->setGeometry(0, 0, screen.width(), DORIAN_PROGRESS_HEIGHT);
244 #if defined(Q_WS_MAEMO_5)
245     nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
246         TranslucentButton::pixels, TranslucentButton::pixels);
247 #else
248     nextButton->setGeometry(screen.width() - TranslucentButton::pixels - 25, 0,
249         TranslucentButton::pixels, TranslucentButton::pixels);
250 #endif // Q_WS_MAEMO_5
251     previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
252         TranslucentButton::pixels, TranslucentButton::pixels);
253
254     hide();
255     fullScreenWindow->showFullScreen();
256     fullScreenWindow->activateWindow();
257     progress->flash();
258     nextButton->flash(1500);
259     previousButton->flash(1500);
260 }
261
262 void MainWindow::setCurrentBook(const QModelIndex &current)
263 {
264     mCurrent = current;
265     Book *book = Library::instance()->book(current);
266     view->setBook(book);
267     setWindowTitle(book? book->shortName(): tr("Dorian"));
268 }
269
270 void MainWindow::showLibrary()
271 {
272     (new LibraryDialog(this))->show();
273 }
274
275 void MainWindow::showSettings()
276 {
277     (new SettingsWindow(this))->show();
278 }
279
280 void MainWindow::showInfo()
281 {
282     if (mCurrent.isValid()) {
283         (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
284                 exec();
285     }
286 }
287
288 void MainWindow::showDevTools()
289 {
290     (new DevTools())->exec();
291 }
292
293 void MainWindow::showBookmarks()
294 {
295     Book *book = Library::instance()->book(mCurrent);
296     if (book) {
297         BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
298         bookmarks->setWindowModality(Qt::WindowModal);
299         connect(bookmarks, SIGNAL(addBookmark(const QString &)),
300                 this, SLOT(onAddBookmark(const QString &)));
301         connect(bookmarks, SIGNAL(goToBookmark(int)),
302                 this, SLOT(onGoToBookmark(int)));
303         bookmarks->show();
304     }
305 }
306
307 void MainWindow::closeEvent(QCloseEvent *event)
308 {
309     Trace t("MainWindow::closeEvent");
310     view->setLastBookmark();
311     event->accept();
312 }
313
314 void MainWindow::onSettingsChanged(const QString &key)
315 {
316 #ifdef Q_WS_MAEMO_5
317     if (key == "orientation") {
318         QString value = Settings::instance()->value(key).toString();
319         qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
320         if (value == "portrait") {
321             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
322             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
323         } else {
324             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
325             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
326         }
327     } else if (key == "lightson") {
328         bool enable = Settings::instance()->value(key, false).toBool();
329         qDebug() << "MainWindow::onSettingsChanged: lightson:" << enable;
330         killTimer(preventBlankingTimer);
331         if (enable) {
332             preventBlankingTimer = startTimer(29 * 1000);
333         }
334     } else if (key == "usevolumekeys") {
335         bool value = Settings::instance()->value(key).toBool();
336         qDebug() << "MainWindow::onSettingsChanged: usevolumekeys" << value;
337         grabZoomKeys(value);
338         fullScreenWindow->grabZoomKeys(value);
339     }
340 #else
341     Q_UNUSED(key);
342 #endif // Q_WS_MAEMO_5
343 }
344
345 void MainWindow::onPartLoadStart()
346 {
347     Trace t("MainWindow::onPartLoadStart");
348 #ifdef Q_WS_MAEMO_5
349     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
350 #endif
351 }
352
353 void MainWindow::onPartLoadEnd(int index)
354 {
355     Trace t("MainWindow::onPartLoadEnd");
356     bool enablePrevious = false;
357     bool enableNext = false;
358     Book *book = Library::instance()->book(mCurrent);
359     if (book) {
360         if (index > 0) {
361             enablePrevious = true;
362         }
363         if (index < (book->parts.size() - 1)) {
364             enableNext = true;
365         }
366     }
367 #ifdef Q_WS_MAEMO_5
368     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
369 #endif // Q_WS_MAEMO_5
370 }
371
372 void MainWindow::onAddBookmark(const QString &note)
373 {
374     Trace t("MainWindow:onAddBookmark");
375     view->addBookmark(note);
376 }
377
378 void MainWindow::onGoToBookmark(int index)
379 {
380     Trace t("MainWindow::onGoToBookmark");
381     Book *book = Library::instance()->book(mCurrent);
382     view->goToBookmark(book->bookmarks()[index]);
383 }
384
385 void MainWindow::showChapters()
386 {
387     Book *book = Library::instance()->book(mCurrent);
388     if (book) {
389         ChaptersDialog *chapters = new ChaptersDialog(book, this);
390         chapters->setWindowModality(Qt::WindowModal);
391         connect(chapters, SIGNAL(goToChapter(int)),
392                 this, SLOT(onGoToChapter(int)));
393         chapters->show();
394     }
395 }
396
397 void MainWindow::onGoToChapter(int index)
398 {
399     Trace t("MainWindow::onGoToChapter");
400
401     Book *book = Library::instance()->book(mCurrent);
402     if (book) {
403         int partIndex = book->partFromChapter(index);
404         if (partIndex != -1) {
405             view->goToBookmark(Book::Bookmark(partIndex, 0));
406         }
407     }
408 }
409
410 void MainWindow::timerEvent(QTimerEvent *event)
411 {
412     if (event->timerId() == preventBlankingTimer) {
413 #ifdef Q_WS_MAEMO_5
414         QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
415                            MCE_REQUEST_IF, QDBusConnection::systemBus());
416         mce.call(MCE_PREVENT_BLANK_REQ);
417 #endif // Q_WS_MAEMO_5
418         qDebug() << "MainWindow::timerEvent: Prevent display blanking";
419     }
420     AdopterWindow::timerEvent(event);
421 }
422
423 void MainWindow::resizeEvent(QResizeEvent *e)
424 {
425     Trace t("MainWindow::resizeEvent");
426     progress->setGeometry(QRect(0, 0, e->size().width(), DORIAN_PROGRESS_HEIGHT));
427 #if defined(Q_WS_MAEMO_5)
428     previousButton->setGeometry(0,
429         e->size().height() - toolBar->height() - TranslucentButton::pixels,
430         TranslucentButton::pixels, TranslucentButton::pixels);
431     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
432         TranslucentButton::pixels, TranslucentButton::pixels);
433 #elif defined(Q_OS_SYMBIAN)
434     previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
435         TranslucentButton::pixels, TranslucentButton::pixels);
436     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
437         0, TranslucentButton::pixels, TranslucentButton::pixels);
438 #else
439     previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
440         TranslucentButton::pixels, TranslucentButton::pixels);
441     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels - 25,
442         toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
443 #endif // Q_WS_MAEMO_5
444     qDebug() << "previousButton geometry" << previousButton->geometry();
445     previousButton->flash(1500);
446     nextButton->flash(1500);
447     QMainWindow::resizeEvent(e);
448 }
449
450 void MainWindow::about()
451 {
452     Dyalog *aboutDialog = new Dyalog(this, false);
453     aboutDialog->setWindowTitle(tr("About Dorian"));
454     QLabel *label = new QLabel(aboutDialog);
455     label->setTextFormat(Qt::RichText);
456     label->setOpenExternalLinks(true);
457     label->setWordWrap(true);
458     label->setText(tr("<b>Dorian %1</b><br><br>Copyright &copy; 2010 "
459         "Akos Polster &lt;akos@pipacs.com&gt;<br>"
460         "Licensed under GNU General Public License, Version 3<br>"
461         "Source code: <a href='https://garage.maemo.org/projects/dorian/'>"
462         "garage.maemo.org/projects/dorian</a>").arg(Platform::version()));
463     aboutDialog->addWidget(label);
464     aboutDialog->addStretch();
465     aboutDialog->show();
466 }
467
468
469 void MainWindow::goToNextPage()
470 {
471     nextButton->flash(1500);
472     previousButton->flash(1500);
473     view->goNextPage();
474 }
475
476 void MainWindow::goToPreviousPage()
477 {
478     nextButton->flash(1500);
479     previousButton->flash(1500);
480     view->goPreviousPage();
481 }
482
483 void MainWindow::onBeginUpgrade(int total)
484 {
485     libraryProgress->setVisible(total > 0);
486     libraryProgress->setWindowTitle(tr("Upgrading library"));
487     libraryProgress->setMaximum(total);
488 }
489
490 void MainWindow::onUpgrading(const QString &path)
491 {
492     libraryProgress->setLabelText(tr("Upgrading %1").
493                                   arg(QFileInfo(path).fileName()));
494     libraryProgress->setValue(libraryProgress->value() + 1);
495 }
496
497 void MainWindow::onEndUpgrade()
498 {
499     libraryProgress->hide();
500     libraryProgress->reset();
501 }
502
503
504 void MainWindow::onBeginLoad(int total)
505 {
506     libraryProgress->setVisible(total > 0);
507     libraryProgress->setWindowTitle(tr("Loading library"));
508     libraryProgress->setMaximum(total);
509 }
510
511 void MainWindow::onLoading(const QString &path)
512 {
513     libraryProgress->setLabelText(tr("Loading %1").
514                                   arg(QFileInfo(path).fileName()));
515     libraryProgress->setValue(libraryProgress->value() + 1);
516 }
517
518 void MainWindow::onEndLoad()
519 {
520     libraryProgress->hide();
521     libraryProgress->reset();
522 }
523