Fix popup widgets' positions. Add tool bar on Symbian.
[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 #include "sortedlibrary.h"
33
34 #ifdef DORIAN_TEST_MODEL
35 #   include "modeltest.h"
36 #endif
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 #ifdef Q_OS_SYMBIAN
48     // Tool bar
49     toolBar = new QToolBar("", this /*frame*/);
50     toolBar->setFixedWidth(QApplication::desktop()->
51                            availableGeometry().width());
52     toolBar->setFixedHeight(65);
53     toolBar->setStyleSheet("margin:0;border:0;padding:0");
54     toolBar->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Maximum);
55     addToolBar(Qt::BottomToolBarArea, toolBar);
56 #endif
57
58     // Central widget. Must be an intermediate, because the book view widget
59     // can be re-parented later
60     QFrame *central = new QFrame(this);
61     QVBoxLayout *layout = new QVBoxLayout(central);
62     layout->setMargin(0);
63     central->setLayout(layout);
64     setCentralWidget(central);
65
66     // Book view
67     view = new BookView(this);
68     view->show();
69     layout->addWidget(view);
70
71     // Dialogs
72     progress = new Progress(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"), true);
83     bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
84                                        "bookmarks", tr("Bookmarks"), true);
85     infoAction = addToolBarAction(this, SLOT(showInfo()),
86                                   "info", tr("Book info"), true);
87     libraryAction = addToolBarAction(this, SLOT(showLibrary()),
88                                      "library", tr("Library"), true);
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 #else
110     (void)addToolBarAction(this, SLOT(close()), "", tr("Exit"));
111 #endif
112
113     // Buttons on top of the book view
114     previousButton = new TranslucentButton("back", this);
115     nextButton = new TranslucentButton("forward", this);
116
117     // Handle model changes
118     connect(Library::instance(), SIGNAL(nowReadingChanged()),
119             this, SLOT(onCurrentBookChanged()));
120
121     // Load library, upgrade it if needed
122     libraryProgress = new ProgressDialog(tr("Upgrading library"), this);
123     Library *library = Library::instance();
124     connect(library, SIGNAL(beginUpgrade(int)), this, SLOT(onBeginUpgrade(int)));
125     connect(library, SIGNAL(upgrading(const QString &)),
126             this, SLOT(onUpgrading(const QString &)));
127     connect(library, SIGNAL(endUpgrade()), this, SLOT(onEndUpgrade()));
128
129     // Handle loading book parts
130     connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
131     connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
132
133     // Handle progress
134     connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
135
136     // Shadow window for full screen reading
137     fullScreenWindow = new FullScreenWindow(this);
138     connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
139
140     // Handle settings changes
141     connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
142             this, SLOT(onSettingsChanged(const QString &)));
143
144     // Handle book view buttons
145     connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
146     connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
147
148     // Adopt view, show window
149     showRegular();
150
151 #ifdef DORIAN_TEST_MODEL
152     (void)new ModelTest(Library::instance(), this);
153 #endif
154 }
155
156 void MainWindow::initialize()
157 {
158     TRACE;
159     Library *library = Library::instance();
160
161     // Upgrade library if needed, then load it
162     library->upgrade();
163     library->load();
164
165     // Load book on command line, or load last read book, or load default book
166     if (QCoreApplication::arguments().size() == 2) {
167         QString path = QCoreApplication::arguments()[1];
168         library->add(path);
169         QModelIndex index = library->find(path);
170         if (index.isValid()) {
171             library->setNowReading(index);
172         }
173     } else {
174         QModelIndex index = library->nowReading();
175         if (index.isValid()) {
176             library->setNowReading(index);
177         } else {
178             if (!library->rowCount()) {
179                 library->add(":/books/2BR02B.epub");
180             }
181             SortedLibrary sorted;
182             library->setNowReading(sorted.mapToSource(sorted.index(0, 0)));
183         }
184     }
185 }
186
187 void MainWindow::onCurrentBookChanged()
188 {
189     TRACE;
190     setCurrentBook(Library::instance()->nowReading());
191 }
192
193 void MainWindow::showRegular()
194 {
195     TRACE;
196
197     // Re-parent children
198     fullScreenWindow->leaveChildren();
199     QList<QWidget *> otherChildren;
200     otherChildren << progress << previousButton << nextButton;
201     takeChildren(view, otherChildren);
202
203 #if 0
204
205     // Adjust geometry of decorations
206
207     QRect geo = geometry();
208     qDebug() << "MainWindow (MainWindow::showRegular)" << geo;
209     qDebug() << "BookView (MainWindow::showRegular)" << view->geometry();
210     int y = geo.height() - progress->thickness();
211 #if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
212     bool hasToolBar = false;
213 #   if defined(Q_OS_SYMBIAN)
214     hasToolBar =
215         (QApplication::desktop()->width() < QApplication::desktop()->height());
216     qDebug() << (hasToolBar? "Portrait": "Landscape");
217 #   endif
218     if (!hasToolBar) {
219         y -= toolBar->height();
220     }
221 #endif
222     progress->setGeometry(0, y, geo.width(), y + progress->thickness());
223
224 #if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
225     y = geo.height() - TranslucentButton::pixels;
226     if (!hasToolBar) {
227         y -= toolBar->height();
228     }
229     previousButton->setGeometry(0, y, TranslucentButton::pixels,
230                                 TranslucentButton::pixels);
231     nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
232         TranslucentButton::pixels, TranslucentButton::pixels);
233 #else
234     previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
235         TranslucentButton::pixels, TranslucentButton::pixels);
236     nextButton->setGeometry(geo.width() - TranslucentButton::pixels - 25,
237         toolBar->height(), TranslucentButton::pixels,
238         TranslucentButton::pixels);
239 #endif // Q_WS_MAEMO_5
240     qDebug() << "previousButton geometry" << previousButton->geometry();
241
242 #endif
243
244     fullScreenWindow->hide();
245     show();
246 #if defined(Q_OS_SYMBIAN)
247     activateWindow();
248 #endif
249 }
250
251 void MainWindow::showBig()
252 {
253     TRACE;
254
255     // Re-parent children
256     leaveChildren();
257     fullScreenWindow->takeChildren(view, progress, previousButton, nextButton);
258
259 #if 0
260
261     // Adjust geometry of decorations
262     QRect screen = QApplication::desktop()->screenGeometry();
263     int y = screen.height() - progress->thickness();
264     progress->setGeometry(0, y, screen.width(), y + progress->thickness());
265 #if defined(Q_WS_MAEMO_5)
266     nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
267         TranslucentButton::pixels, TranslucentButton::pixels);
268 #else
269     nextButton->setGeometry(screen.width() - TranslucentButton::pixels - 25, 0,
270         TranslucentButton::pixels, TranslucentButton::pixels);
271 #endif // Q_WS_MAEMO_5
272     previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
273         TranslucentButton::pixels, TranslucentButton::pixels);
274
275 #endif
276
277 // #ifdef Q_OS_SYMBIAN
278     hide();
279 // #endif
280
281     fullScreenWindow->showFullScreen();
282 #ifdef Q_OS_SYMBIAN
283     fullScreenWindow->activateWindow();
284 #endif
285 }
286
287 void MainWindow::setCurrentBook(const QModelIndex &current)
288 {
289     mCurrent = current;
290     Book *book = Library::instance()->book(current);
291     view->setBook(book);
292     setWindowTitle(book? book->shortName(): tr("Dorian"));
293 }
294
295 void MainWindow::showLibrary()
296 {
297     (new LibraryDialog(this))->show();
298 }
299
300 void MainWindow::showSettings()
301 {
302     (new SettingsWindow(this))->show();
303 }
304
305 void MainWindow::showInfo()
306 {
307     if (mCurrent.isValid()) {
308         (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
309                 exec();
310     }
311 }
312
313 void MainWindow::showDevTools()
314 {
315     (new DevTools())->exec();
316 }
317
318 void MainWindow::showBookmarks()
319 {
320     Book *book = Library::instance()->book(mCurrent);
321     if (book) {
322         BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
323         connect(bookmarks, SIGNAL(addBookmark(const QString &)),
324                 this, SLOT(onAddBookmark(const QString &)));
325         connect(bookmarks, SIGNAL(goToBookmark(int)),
326                 this, SLOT(onGoToBookmark(int)));
327         bookmarks->show();
328     }
329 }
330
331 void MainWindow::closeEvent(QCloseEvent *event)
332 {
333     TRACE;
334     view->setLastBookmark();
335     AdopterWindow::closeEvent(event);
336 }
337
338 void MainWindow::onSettingsChanged(const QString &key)
339 {
340 #if defined(Q_WS_MAEMO_5)
341     if (key == "orientation") {
342         QString value = Settings::instance()->value(key,
343             Platform::instance()->defaultOrientation()).toString();
344         qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
345         if (value == "portrait") {
346             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
347             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
348             fullScreenWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,
349                                            false);
350             fullScreenWindow->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
351         } else {
352             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
353             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
354             fullScreenWindow->setAttribute(Qt::WA_Maemo5PortraitOrientation,
355                                            false);
356             fullScreenWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,
357                                            true);
358         }
359     } else if (key == "lightson") {
360         bool enable = Settings::instance()->value(key, false).toBool();
361         qDebug() << "MainWindow::onSettingsChanged: lightson" << enable;
362         killTimer(preventBlankingTimer);
363         if (enable) {
364             preventBlankingTimer = startTimer(29 * 1000);
365         }
366     }
367 #else
368     Q_UNUSED(key);
369 #endif // Q_WS_MAEMO_5
370 }
371
372 void MainWindow::onPartLoadStart()
373 {
374     TRACE;
375     Platform::instance()->showBusy(this, true);
376 }
377
378 void MainWindow::onPartLoadEnd(int index)
379 {
380     TRACE;
381     bool enablePrevious = false;
382     bool enableNext = false;
383     Book *book = Library::instance()->book(mCurrent);
384     if (book) {
385         if (index > 0) {
386             enablePrevious = true;
387         }
388         if (index < (book->parts.size() - 1)) {
389             enableNext = true;
390         }
391     }
392     Platform::instance()->showBusy(this, false);
393 }
394
395 void MainWindow::onAddBookmark(const QString &note)
396 {
397     TRACE;
398     view->addBookmark(note);
399     Platform::instance()->information(tr("Bookmarked current position"), this);
400 }
401
402 void MainWindow::onGoToBookmark(int index)
403 {
404     TRACE;
405     Book *book = Library::instance()->book(mCurrent);
406     view->goToBookmark(book->bookmarks()[index]);
407 }
408
409 void MainWindow::showChapters()
410 {
411     Book *book = Library::instance()->book(mCurrent);
412     if (book) {
413         ChaptersDialog *chapters = new ChaptersDialog(book, this);
414         connect(chapters, SIGNAL(goToChapter(int)),
415                 this, SLOT(onGoToChapter(int)));
416         chapters->show();
417     }
418 }
419
420 void MainWindow::onGoToChapter(int index)
421 {
422     TRACE;
423
424     Book *book = Library::instance()->book(mCurrent);
425     if (book) {
426         QString fragment;
427         int partIndex = book->partFromChapter(index, fragment);
428         if (partIndex != -1) {
429             view->goToPart(partIndex, fragment);
430         }
431     }
432 }
433
434 void MainWindow::timerEvent(QTimerEvent *event)
435 {
436     if (event->timerId() == preventBlankingTimer) {
437 #ifdef Q_WS_MAEMO_5
438         QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
439                            MCE_REQUEST_IF, QDBusConnection::systemBus());
440         mce.call(MCE_PREVENT_BLANK_REQ);
441 #endif // Q_WS_MAEMO_5
442         qDebug() << "MainWindow::timerEvent: Prevent display blanking";
443     }
444     AdopterWindow::timerEvent(event);
445 }
446
447 void MainWindow::resizeEvent(QResizeEvent *e)
448 {
449     Trace t("MainWindow::resizeEvent");
450 #ifdef Q_OS_SYMBIAN
451     // Tool bar is only useful in portrait mode
452     bool isPortrait =
453         (QApplication::desktop()->width() < QApplication::desktop()->height());
454     toolBar->setVisible(isPortrait);
455 #endif
456     QTimer::singleShot(100, this, SLOT(placeChildren()));
457     AdopterWindow::resizeEvent(e);
458 }
459
460 void MainWindow::placeChildren()
461 {
462     Trace t("MainWindow::placeChildren");
463
464     int toolBarHeight = 0;
465
466 #ifdef Q_OS_SYMBIAN
467     // Tool bar is only useful in portrait mode
468     bool isPortrait =
469         (QApplication::desktop()->width() < QApplication::desktop()->height());
470     // toolBar->setVisible(isPortrait);
471
472     // Work around Symbian bug: If there is no tool bar, increase decorator
473     // widgets' Y coordinates
474     if (!isPortrait) {
475         toolBarHeight = toolBar->height();
476     }
477 #endif // Q_OS_SYMBIAN
478
479     if (hasChild(view)) {
480         QRect geo = centralWidget()->geometry();
481         qDebug() << "centralWidget (MainWindow::resizeEvent)" << geo;
482 #ifdef Q_OS_SYMBIAN
483         // FIXME: When returning from full screen in landscape mode,
484         // the central widget's height is miscalculated on Symbian.
485         // My apologies for this kludge
486         if (geo.height() == 288) {
487             geo.setHeight(223);
488         }
489 #endif // Q_OS_SYMBIAN
490         progress->setGeometry(geo.x(),
491             geo.y() + geo.height() - progress->thickness() + toolBarHeight,
492             geo.width(), progress->thickness());
493         previousButton->setGeometry(geo.x(),
494             geo.y() + geo.height() - TranslucentButton::pixels + toolBarHeight,
495             TranslucentButton::pixels, TranslucentButton::pixels);
496         nextButton->setGeometry(
497             geo.x() + geo.width() - TranslucentButton::pixels,
498             geo.y(), TranslucentButton::pixels, TranslucentButton::pixels);
499         progress->flash();
500         previousButton->flash();
501         nextButton->flash();
502         qDebug() << "Progress (MainWindow::resizeEvent)"
503                 << progress->geometry();
504     }
505
506 }
507
508 void MainWindow::about()
509 {
510     Dyalog *aboutDialog = new Dyalog(this, false);
511     aboutDialog->setWindowTitle(tr("About Dorian"));
512     QString version = Platform::instance()->version();
513     QLabel *label = new QLabel(aboutDialog);
514     label->setTextFormat(Qt::RichText);
515     label->setOpenExternalLinks(true);
516     label->setWordWrap(true);
517     label->setText(tr("<b>Dorian %1</b><br><br>Copyright &copy; 2010 "
518         "Akos Polster &lt;akos@pipacs.com&gt;<br>"
519         "Licensed under GNU General Public License, Version 3<br>"
520         "Source code:<br><a href='https://garage.maemo.org/projects/dorian/'>"
521         "garage.maemo.org/projects/dorian</a>").arg(version));
522     aboutDialog->addWidget(label);
523     aboutDialog->addStretch();
524     aboutDialog->show();
525 }
526
527 void MainWindow::goToNextPage()
528 {
529     nextButton->flash();
530     previousButton->flash();
531     view->goNextPage();
532 }
533
534 void MainWindow::goToPreviousPage()
535 {
536     nextButton->flash();
537     previousButton->flash();
538     view->goPreviousPage();
539 }
540
541 void MainWindow::onBeginUpgrade(int total)
542 {
543     libraryProgress->setVisible(total > 0);
544     libraryProgress->setWindowTitle(tr("Upgrading library"));
545     libraryProgress->setMaximum(total);
546 }
547
548 void MainWindow::onUpgrading(const QString &path)
549 {
550     libraryProgress->setLabelText(tr("Upgrading %1").
551                                   arg(QFileInfo(path).fileName()));
552     libraryProgress->setValue(libraryProgress->value() + 1);
553 }
554
555 void MainWindow::onEndUpgrade()
556 {
557     libraryProgress->hide();
558     libraryProgress->reset();
559 }
560
561 void MainWindow::onBeginLoad(int total)
562 {
563     libraryProgress->setVisible(total > 0);
564     libraryProgress->setWindowTitle(tr("Loading library"));
565     libraryProgress->setMaximum(total);
566 }
567
568 void MainWindow::onLoading(const QString &path)
569 {
570     libraryProgress->setLabelText(tr("Loading %1").
571                                   arg(QFileInfo(path).fileName()));
572     libraryProgress->setValue(libraryProgress->value() + 1);
573 }
574
575 void MainWindow::onEndLoad()
576 {
577     libraryProgress->hide();
578     libraryProgress->reset();
579 }