6 # include <QtGui/QX11Info>
8 # include <X11/Xatom.h>
9 # include <mce/mode-names.h>
10 # include <mce/dbus-names.h>
11 #endif // Q_WS_MAEMO_5
16 #include "infodialog.h"
17 #include "librarydialog.h"
19 #include "mainwindow.h"
20 #include "settingswindow.h"
21 #include "bookmarksdialog.h"
23 #include "chaptersdialog.h"
24 #include "fullscreenwindow.h"
26 #include "bookfinder.h"
29 #include "translucentbutton.h"
31 #include "progressdialog.h"
32 #include "sortedlibrary.h"
34 #ifdef DORIAN_TEST_MODEL
35 # include "modeltest.h"
38 MainWindow::MainWindow(QWidget *parent):
39 AdopterWindow(parent), view(0), preventBlankingTimer(-1)
43 setAttribute(Qt::WA_Maemo5StackedWindow, true);
45 setWindowTitle("Dorian");
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);
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);
63 central->setLayout(layout);
64 setCentralWidget(central);
67 view = new BookView(this);
69 layout->addWidget(view);
72 progress = new Progress(this);
77 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
78 "view-fullscreen", tr("Full screen"));
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);
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()));
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
107 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
108 "view-fullscreen", tr("Full screen"));
110 (void)addToolBarAction(this, SLOT(close()), "", tr("Exit"));
113 // Buttons on top of the book view
114 previousButton = new TranslucentButton("back", this);
115 nextButton = new TranslucentButton("forward", this);
117 // Handle model changes
118 connect(Library::instance(), SIGNAL(nowReadingChanged()),
119 this, SLOT(onCurrentBookChanged()));
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()));
129 // Handle loading book parts
130 connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
131 connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
134 connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
136 // Shadow window for full screen reading
137 fullScreenWindow = new FullScreenWindow(this);
138 connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
140 // Handle settings changes
141 connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
142 this, SLOT(onSettingsChanged(const QString &)));
144 // Handle book view buttons
145 connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
146 connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
148 // Adopt view, show window
151 #ifdef DORIAN_TEST_MODEL
152 (void)new ModelTest(Library::instance(), this);
156 void MainWindow::initialize()
159 Library *library = Library::instance();
161 // Upgrade library if needed, then load it
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];
169 QModelIndex index = library->find(path);
170 if (index.isValid()) {
171 library->setNowReading(index);
174 QModelIndex index = library->nowReading();
175 if (index.isValid()) {
176 library->setNowReading(index);
178 if (!library->rowCount()) {
179 library->add(":/books/2BR02B.epub");
181 SortedLibrary sorted;
182 library->setNowReading(sorted.mapToSource(sorted.index(0, 0)));
187 void MainWindow::onCurrentBookChanged()
190 setCurrentBook(Library::instance()->nowReading());
193 void MainWindow::showRegular()
197 // Re-parent children
198 fullScreenWindow->leaveChildren();
199 QList<QWidget *> otherChildren;
200 otherChildren << progress << previousButton << nextButton;
201 takeChildren(view, otherChildren);
205 // Adjust geometry of decorations
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)
215 (QApplication::desktop()->width() < QApplication::desktop()->height());
216 qDebug() << (hasToolBar? "Portrait": "Landscape");
219 y -= toolBar->height();
222 progress->setGeometry(0, y, geo.width(), y + progress->thickness());
224 #if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
225 y = geo.height() - TranslucentButton::pixels;
227 y -= toolBar->height();
229 previousButton->setGeometry(0, y, TranslucentButton::pixels,
230 TranslucentButton::pixels);
231 nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
232 TranslucentButton::pixels, TranslucentButton::pixels);
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();
244 fullScreenWindow->hide();
246 #if defined(Q_OS_SYMBIAN)
251 void MainWindow::showBig()
255 // Re-parent children
257 fullScreenWindow->takeChildren(view, progress, previousButton, nextButton);
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);
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);
277 // #ifdef Q_OS_SYMBIAN
281 fullScreenWindow->showFullScreen();
283 fullScreenWindow->activateWindow();
287 void MainWindow::setCurrentBook(const QModelIndex ¤t)
290 Book *book = Library::instance()->book(current);
292 setWindowTitle(book? book->shortName(): tr("Dorian"));
295 void MainWindow::showLibrary()
297 (new LibraryDialog(this))->show();
300 void MainWindow::showSettings()
302 (new SettingsWindow(this))->show();
305 void MainWindow::showInfo()
307 if (mCurrent.isValid()) {
308 (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
313 void MainWindow::showDevTools()
315 (new DevTools())->exec();
318 void MainWindow::showBookmarks()
320 Book *book = Library::instance()->book(mCurrent);
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)));
331 void MainWindow::closeEvent(QCloseEvent *event)
334 view->setLastBookmark();
335 AdopterWindow::closeEvent(event);
338 void MainWindow::onSettingsChanged(const QString &key)
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,
350 fullScreenWindow->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
352 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
353 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
354 fullScreenWindow->setAttribute(Qt::WA_Maemo5PortraitOrientation,
356 fullScreenWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,
359 } else if (key == "lightson") {
360 bool enable = Settings::instance()->value(key, false).toBool();
361 qDebug() << "MainWindow::onSettingsChanged: lightson" << enable;
362 killTimer(preventBlankingTimer);
364 preventBlankingTimer = startTimer(29 * 1000);
369 #endif // Q_WS_MAEMO_5
372 void MainWindow::onPartLoadStart()
375 Platform::instance()->showBusy(this, true);
378 void MainWindow::onPartLoadEnd(int index)
381 bool enablePrevious = false;
382 bool enableNext = false;
383 Book *book = Library::instance()->book(mCurrent);
386 enablePrevious = true;
388 if (index < (book->parts.size() - 1)) {
392 Platform::instance()->showBusy(this, false);
395 void MainWindow::onAddBookmark(const QString ¬e)
398 view->addBookmark(note);
399 Platform::instance()->information(tr("Bookmarked current position"), this);
402 void MainWindow::onGoToBookmark(int index)
405 Book *book = Library::instance()->book(mCurrent);
406 view->goToBookmark(book->bookmarks()[index]);
409 void MainWindow::showChapters()
411 Book *book = Library::instance()->book(mCurrent);
413 ChaptersDialog *chapters = new ChaptersDialog(book, this);
414 connect(chapters, SIGNAL(goToChapter(int)),
415 this, SLOT(onGoToChapter(int)));
420 void MainWindow::onGoToChapter(int index)
424 Book *book = Library::instance()->book(mCurrent);
427 int partIndex = book->partFromChapter(index, fragment);
428 if (partIndex != -1) {
429 view->goToPart(partIndex, fragment);
434 void MainWindow::timerEvent(QTimerEvent *event)
436 if (event->timerId() == preventBlankingTimer) {
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";
444 AdopterWindow::timerEvent(event);
447 void MainWindow::resizeEvent(QResizeEvent *e)
449 Trace t("MainWindow::resizeEvent");
451 // Tool bar is only useful in portrait mode
453 (QApplication::desktop()->width() < QApplication::desktop()->height());
454 toolBar->setVisible(isPortrait);
456 QTimer::singleShot(100, this, SLOT(placeChildren()));
457 AdopterWindow::resizeEvent(e);
460 void MainWindow::placeChildren()
462 Trace t("MainWindow::placeChildren");
464 int toolBarHeight = 0;
467 // Tool bar is only useful in portrait mode
469 (QApplication::desktop()->width() < QApplication::desktop()->height());
470 // toolBar->setVisible(isPortrait);
472 // Work around Symbian bug: If there is no tool bar, increase decorator
473 // widgets' Y coordinates
475 toolBarHeight = toolBar->height();
477 #endif // Q_OS_SYMBIAN
479 if (hasChild(view)) {
480 QRect geo = centralWidget()->geometry();
481 qDebug() << "centralWidget (MainWindow::resizeEvent)" << geo;
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) {
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);
500 previousButton->flash();
502 qDebug() << "Progress (MainWindow::resizeEvent)"
503 << progress->geometry();
508 void MainWindow::about()
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 © 2010 "
518 "Akos Polster <akos@pipacs.com><br>"
519 "Licensed under GNU General Public License, Version 3<br>"
520 "Source code:<br><a href='http://dorian.garage.maemo.org/'>"
521 "dorian.garage.maemo.org</a>").arg(version));
522 aboutDialog->addWidget(label);
523 aboutDialog->addStretch();
527 void MainWindow::goToNextPage()
530 previousButton->flash();
534 void MainWindow::goToPreviousPage()
537 previousButton->flash();
538 view->goPreviousPage();
541 void MainWindow::onBeginUpgrade(int total)
543 libraryProgress->setVisible(total > 0);
544 libraryProgress->setWindowTitle(tr("Upgrading library"));
545 libraryProgress->setMaximum(total);
548 void MainWindow::onUpgrading(const QString &path)
550 libraryProgress->setLabelText(tr("Upgrading %1").
551 arg(QFileInfo(path).fileName()));
552 libraryProgress->setValue(libraryProgress->value() + 1);
555 void MainWindow::onEndUpgrade()
557 libraryProgress->hide();
558 libraryProgress->reset();
561 void MainWindow::onBeginLoad(int total)
563 libraryProgress->setVisible(total > 0);
564 libraryProgress->setWindowTitle(tr("Loading library"));
565 libraryProgress->setMaximum(total);
568 void MainWindow::onLoading(const QString &path)
570 libraryProgress->setLabelText(tr("Loading %1").
571 arg(QFileInfo(path).fileName()));
572 libraryProgress->setValue(libraryProgress->value() + 1);
575 void MainWindow::onEndLoad()
577 libraryProgress->hide();
578 libraryProgress->reset();