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