Fix popup button geometries.
[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     // 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     // Dialogs
61     progress = new Progress(this);
62
63     // Tool bar actions
64
65 #ifdef Q_OS_SYMBIAN
66     fullScreenAction = addToolBarAction(this, SLOT(showBig()),
67                                         "view-fullscreen", tr("Full screen"));
68 #endif
69
70     chaptersAction = addToolBarAction(this, SLOT(showChapters()),
71                                       "chapters", tr("Chapters"), true);
72     bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
73                                        "bookmarks", tr("Bookmarks"), true);
74     infoAction = addToolBarAction(this, SLOT(showInfo()),
75                                   "info", tr("Book info"), true);
76     libraryAction = addToolBarAction(this, SLOT(showLibrary()),
77                                      "library", tr("Library"), true);
78
79 #ifdef Q_WS_MAEMO_5
80     settingsAction = menuBar()->addAction(tr("Settings"));
81     connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
82     devToolsAction = menuBar()->addAction(tr("Developer"));
83     connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
84     QAction *aboutAction = menuBar()->addAction(tr("About"));
85     connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
86 #else
87     settingsAction = addToolBarAction(this, SLOT(showSettings()),
88                                       "preferences-system", tr("Settings"));
89     devToolsAction = addToolBarAction(this, SLOT(showDevTools()),
90                                       "developer", tr("Developer"));
91     addToolBarAction(this, SLOT(about()), "about", tr("About"));
92 #endif // Q_WS_MAEMO_5
93
94 #ifndef Q_OS_SYMBIAN
95     addToolBarSpace();
96     fullScreenAction = addToolBarAction(this, SLOT(showBig()),
97                                         "view-fullscreen", tr("Full screen"));
98 #else
99     (void)addToolBarAction(this, SLOT(close()), "", tr("Exit"));
100 #endif
101
102     // Buttons on top of the book view
103     previousButton = new TranslucentButton("back", this);
104     nextButton = new TranslucentButton("forward", this);
105
106     // Handle model changes
107     connect(Library::instance(), SIGNAL(nowReadingChanged()),
108             this, SLOT(onCurrentBookChanged()));
109
110     // Load library, upgrade it if needed
111     libraryProgress = new ProgressDialog(tr("Upgrading library"), this);
112     Library *library = Library::instance();
113     connect(library, SIGNAL(beginUpgrade(int)), this, SLOT(onBeginUpgrade(int)));
114     connect(library, SIGNAL(upgrading(const QString &)),
115             this, SLOT(onUpgrading(const QString &)));
116     connect(library, SIGNAL(endUpgrade()), this, SLOT(onEndUpgrade()));
117
118     // Handle loading book parts
119     connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
120     connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
121
122     // Handle progress
123     connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
124
125     // Shadow window for full screen reading
126     fullScreenWindow = new FullScreenWindow(this);
127     connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
128
129     // Handle settings changes
130     connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
131             this, SLOT(onSettingsChanged(const QString &)));
132
133     // Handle book view buttons
134     connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
135     connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
136
137     // Adopt view, show window
138     showRegular();
139
140 #ifdef DORIAN_TEST_MODEL
141     (void)new ModelTest(Library::instance(), this);
142 #endif
143 }
144
145 void MainWindow::initialize()
146 {
147     TRACE;
148     Library *library = Library::instance();
149
150     // Upgrade library if needed, then load it
151     library->upgrade();
152     library->load();
153
154     // Load book on command line, or load last read book, or load default book
155     if (QCoreApplication::arguments().size() == 2) {
156         QString path = QCoreApplication::arguments()[1];
157         library->add(path);
158         QModelIndex index = library->find(path);
159         if (index.isValid()) {
160             library->setNowReading(index);
161         }
162     } else {
163         QModelIndex index = library->nowReading();
164         if (index.isValid()) {
165             library->setNowReading(index);
166         } else {
167             if (!library->rowCount()) {
168                 library->add(":/books/2BR02B.epub");
169             }
170             SortedLibrary sorted;
171             library->setNowReading(sorted.mapToSource(sorted.index(0, 0)));
172         }
173     }
174 }
175
176 void MainWindow::onCurrentBookChanged()
177 {
178     TRACE;
179     setCurrentBook(Library::instance()->nowReading());
180 }
181
182 void MainWindow::showRegular()
183 {
184     TRACE;
185
186     // Re-parent children
187     fullScreenWindow->leaveChildren();
188     QList<QWidget *> otherChildren;
189     otherChildren << progress << previousButton << nextButton;
190     takeChildren(view, otherChildren);
191
192     // Adjust geometry of decorations
193
194     QRect geo = geometry();
195     qDebug() << geo;
196     int y = geo.height() - progress->thickness();
197 #if defined(Q_WS_MAEMO_5)
198     y -= toolBar->height();
199 #endif
200     progress->setGeometry(0, y, geo.width(), y + progress->thickness());
201
202 #if defined(Q_WS_MAEMO_5)
203     previousButton->setGeometry(0,
204         geo.height() - toolBar->height() - TranslucentButton::pixels,
205         TranslucentButton::pixels, TranslucentButton::pixels);
206     nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
207         TranslucentButton::pixels, TranslucentButton::pixels);
208 #elif defined(Q_OS_SYMBIAN)
209     previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
210         TranslucentButton::pixels, TranslucentButton::pixels);
211     nextButton->setGeometry(geo.width() - TranslucentButton::pixels,
212         0, TranslucentButton::pixels, TranslucentButton::pixels);
213 #else
214     previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
215         TranslucentButton::pixels, TranslucentButton::pixels);
216     nextButton->setGeometry(geo.width() - TranslucentButton::pixels - 25,
217         toolBar->height(), TranslucentButton::pixels,
218         TranslucentButton::pixels);
219 #endif // Q_WS_MAEMO_5
220     qDebug() << "previousButton geometry" << previousButton->geometry();
221
222     fullScreenWindow->hide();
223     show();
224 #if defined(Q_OS_SYMBIAN)
225     activateWindow();
226 #endif
227     progress->flash();
228     nextButton->flash();
229     previousButton->flash();
230 }
231
232 void MainWindow::showBig()
233 {
234     TRACE;
235
236     // Re-parent children
237     leaveChildren();
238     QList<QWidget *> otherChildren;
239     otherChildren << progress << nextButton << previousButton;
240     fullScreenWindow->takeChildren(view, otherChildren);
241
242     // Adjust geometry of decorations
243     QRect screen = QApplication::desktop()->screenGeometry();
244     int y = screen.height() - progress->thickness();
245     progress->setGeometry(0, y, screen.width(), y + progress->thickness());
246 #if defined(Q_WS_MAEMO_5)
247     nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
248         TranslucentButton::pixels, TranslucentButton::pixels);
249 #else
250     nextButton->setGeometry(screen.width() - TranslucentButton::pixels - 25, 0,
251         TranslucentButton::pixels, TranslucentButton::pixels);
252 #endif // Q_WS_MAEMO_5
253     previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
254         TranslucentButton::pixels, TranslucentButton::pixels);
255
256 #ifdef Q_OS_SYMBIAN
257     hide();
258 #endif
259     fullScreenWindow->showFullScreen();
260 #ifdef Q_OS_SYMBIAN
261     fullScreenWindow->activateWindow();
262 #endif
263     progress->flash();
264     nextButton->flash();
265     previousButton->flash();
266 }
267
268 void MainWindow::setCurrentBook(const QModelIndex &current)
269 {
270     mCurrent = current;
271     Book *book = Library::instance()->book(current);
272     view->setBook(book);
273     setWindowTitle(book? book->shortName(): tr("Dorian"));
274 }
275
276 void MainWindow::showLibrary()
277 {
278     (new LibraryDialog(this))->show();
279 }
280
281 void MainWindow::showSettings()
282 {
283     (new SettingsWindow(this))->show();
284 }
285
286 void MainWindow::showInfo()
287 {
288     if (mCurrent.isValid()) {
289         (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
290                 exec();
291     }
292 }
293
294 void MainWindow::showDevTools()
295 {
296     (new DevTools())->exec();
297 }
298
299 void MainWindow::showBookmarks()
300 {
301     Book *book = Library::instance()->book(mCurrent);
302     if (book) {
303         BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
304         connect(bookmarks, SIGNAL(addBookmark(const QString &)),
305                 this, SLOT(onAddBookmark(const QString &)));
306         connect(bookmarks, SIGNAL(goToBookmark(int)),
307                 this, SLOT(onGoToBookmark(int)));
308         bookmarks->show();
309     }
310 }
311
312 void MainWindow::closeEvent(QCloseEvent *event)
313 {
314     TRACE;
315     view->setLastBookmark();
316     AdopterWindow::closeEvent(event);
317 }
318
319 void MainWindow::onSettingsChanged(const QString &key)
320 {
321 #if defined(Q_WS_MAEMO_5)
322     if (key == "orientation") {
323         QString value = Settings::instance()->value(key,
324             Platform::instance()->defaultOrientation()).toString();
325         qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
326         if (value == "portrait") {
327             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
328             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
329             fullScreenWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,
330                                            false);
331             fullScreenWindow->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
332         } else {
333             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
334             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
335             fullScreenWindow->setAttribute(Qt::WA_Maemo5PortraitOrientation,
336                                            false);
337             fullScreenWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,
338                                            true);
339         }
340     } else if (key == "lightson") {
341         bool enable = Settings::instance()->value(key, false).toBool();
342         qDebug() << "MainWindow::onSettingsChanged: lightson" << enable;
343         killTimer(preventBlankingTimer);
344         if (enable) {
345             preventBlankingTimer = startTimer(29 * 1000);
346         }
347     }
348 #else
349     Q_UNUSED(key);
350 #endif // Q_WS_MAEMO_5
351 }
352
353 void MainWindow::onPartLoadStart()
354 {
355     TRACE;
356     Platform::instance()->showBusy(this, true);
357 }
358
359 void MainWindow::onPartLoadEnd(int index)
360 {
361     TRACE;
362     bool enablePrevious = false;
363     bool enableNext = false;
364     Book *book = Library::instance()->book(mCurrent);
365     if (book) {
366         if (index > 0) {
367             enablePrevious = true;
368         }
369         if (index < (book->parts.size() - 1)) {
370             enableNext = true;
371         }
372     }
373     Platform::instance()->showBusy(this, false);
374 }
375
376 void MainWindow::onAddBookmark(const QString &note)
377 {
378     TRACE;
379     view->addBookmark(note);
380     Platform::instance()->information(tr("Bookmarked current position"), this);
381 }
382
383 void MainWindow::onGoToBookmark(int index)
384 {
385     TRACE;
386     Book *book = Library::instance()->book(mCurrent);
387     view->goToBookmark(book->bookmarks()[index]);
388 }
389
390 void MainWindow::showChapters()
391 {
392     Book *book = Library::instance()->book(mCurrent);
393     if (book) {
394         ChaptersDialog *chapters = new ChaptersDialog(book, this);
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
432     if (bookView) {
433         qDebug() << "BookView geometry" << bookView->geometry();
434         QRect geo = bookView->geometry();
435         progress->setGeometry(geo.x(),
436             geo.y() + geo.height() - progress->thickness(), geo.width(),
437             progress->thickness());
438         previousButton->setGeometry(geo.x(),
439             geo.y() + geo.height() - TranslucentButton::pixels,
440             TranslucentButton::pixels, TranslucentButton::pixels);
441         nextButton->setGeometry(
442             geo.x() + geo.width() - TranslucentButton::pixels,
443             geo.y(), TranslucentButton::pixels, TranslucentButton::pixels);
444     }
445     QMainWindow::resizeEvent(e);
446 }
447
448 void MainWindow::about()
449 {
450     Dyalog *aboutDialog = new Dyalog(this, false);
451     aboutDialog->setWindowTitle(tr("About Dorian"));
452     QString version = Platform::instance()->version();
453     QLabel *label = new QLabel(aboutDialog);
454     label->setTextFormat(Qt::RichText);
455     label->setOpenExternalLinks(true);
456     label->setWordWrap(true);
457     label->setText(tr("<b>Dorian %1</b><br><br>Copyright &copy; 2010 "
458         "Akos Polster &lt;akos@pipacs.com&gt;<br>"
459         "Licensed under GNU General Public License, Version 3<br>"
460         "Source code:<br><a href='https://garage.maemo.org/projects/dorian/'>"
461         "garage.maemo.org/projects/dorian</a>").arg(version));
462     aboutDialog->addWidget(label);
463     aboutDialog->addStretch();
464     aboutDialog->show();
465 }
466
467 void MainWindow::goToNextPage()
468 {
469     nextButton->flash();
470     previousButton->flash();
471     view->goNextPage();
472 }
473
474 void MainWindow::goToPreviousPage()
475 {
476     nextButton->flash();
477     previousButton->flash();
478     view->goPreviousPage();
479 }
480
481 void MainWindow::onBeginUpgrade(int total)
482 {
483     libraryProgress->setVisible(total > 0);
484     libraryProgress->setWindowTitle(tr("Upgrading library"));
485     libraryProgress->setMaximum(total);
486 }
487
488 void MainWindow::onUpgrading(const QString &path)
489 {
490     libraryProgress->setLabelText(tr("Upgrading %1").
491                                   arg(QFileInfo(path).fileName()));
492     libraryProgress->setValue(libraryProgress->value() + 1);
493 }
494
495 void MainWindow::onEndUpgrade()
496 {
497     libraryProgress->hide();
498     libraryProgress->reset();
499 }
500
501 void MainWindow::onBeginLoad(int total)
502 {
503     libraryProgress->setVisible(total > 0);
504     libraryProgress->setWindowTitle(tr("Loading library"));
505     libraryProgress->setMaximum(total);
506 }
507
508 void MainWindow::onLoading(const QString &path)
509 {
510     libraryProgress->setLabelText(tr("Loading %1").
511                                   arg(QFileInfo(path).fileName()));
512     libraryProgress->setValue(libraryProgress->value() + 1);
513 }
514
515 void MainWindow::onEndLoad()
516 {
517     libraryProgress->hide();
518     libraryProgress->reset();
519 }