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