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"
33 #ifdef DORIAN_TEST_MODEL
34 # include "modeltest.h"
37 const int DORIAN_PROGRESS_HEIGHT = 17;
39 MainWindow::MainWindow(QWidget *parent):
40 AdopterWindow(parent), view(0), preventBlankingTimer(-1)
44 setAttribute(Qt::WA_Maemo5StackedWindow, true);
46 setWindowTitle("Dorian");
48 // Central widget. Must be an intermediate, because the book view widget
49 // can be re-parented later
50 QFrame *central = new QFrame(this);
51 QVBoxLayout *layout = new QVBoxLayout(central);
53 central->setLayout(layout);
54 setCentralWidget(central);
57 view = new BookView(central);
59 layout->addWidget(view);
62 progress = new Progress(this);
65 settings = new QDialog(this);
70 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
71 "view-fullscreen", tr("Full screen"));
74 chaptersAction = addToolBarAction(this, SLOT(showChapters()),
75 "chapters", tr("Chapters"));
76 bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
77 "bookmarks", tr("Bookmarks"));
78 infoAction = addToolBarAction(this, SLOT(showInfo()),
79 "info", tr("Book info"));
80 libraryAction = addToolBarAction(this, SLOT(showLibrary()),
81 "library", tr("Library"));
84 settingsAction = menuBar()->addAction(tr("Settings"));
85 connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
86 devToolsAction = menuBar()->addAction(tr("Developer"));
87 connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
88 QAction *aboutAction = menuBar()->addAction(tr("About"));
89 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
91 settingsAction = addToolBarAction(this, SLOT(showSettings()),
92 "preferences-system", tr("Settings"));
93 devToolsAction = addToolBarAction(this, SLOT(showDevTools()),
94 "developer", tr("Developer"));
95 addToolBarAction(this, SLOT(about()), "about", tr("About"));
96 #endif // Q_WS_MAEMO_5
100 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
101 "view-fullscreen", tr("Full screen"));
103 (void)addToolBarAction(this, SLOT(close()), "", tr("Exit"));
106 // Buttons on top of the book view
107 previousButton = new TranslucentButton("back", this);
108 nextButton = new TranslucentButton("forward", this);
110 // Handle model changes
111 connect(Library::instance(), SIGNAL(nowReadingChanged()),
112 this, SLOT(onCurrentBookChanged()));
114 // Load library, upgrade it if needed
115 libraryProgress = new ProgressDialog(tr("Upgrading library"), this);
116 Library *library = Library::instance();
117 connect(library, SIGNAL(beginUpgrade(int)), this, SLOT(onBeginUpgrade(int)));
118 connect(library, SIGNAL(upgrading(const QString &)),
119 this, SLOT(onUpgrading(const QString &)));
120 connect(library, SIGNAL(endUpgrade()), this, SLOT(onEndUpgrade()));
122 connect(library, SIGNAL(beginLoad(int)), this, SLOT(onBeginLoad(int)));
123 connect(library, SIGNAL(loading(const QString &)),
124 this, SLOT(onLoading(const QString &)));
125 connect(library, SIGNAL(endLoad()), this, SLOT(onEndLoad()));
130 // Load book on command line, or load last read book, or load default book
131 if (QCoreApplication::arguments().size() == 2) {
132 QString path = QCoreApplication::arguments()[1];
134 QModelIndex index = library->find(path);
135 if (index.isValid()) {
136 library->setNowReading(index);
139 QModelIndex index = library->nowReading();
140 if (index.isValid()) {
141 library->setNowReading(index);
143 if (!library->rowCount()) {
144 library->add(":/books/2BR02B.epub");
146 library->setNowReading(library->index(0));
150 // Handle loading book parts
151 connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
152 connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
155 connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
157 // Shadow window for full screen reading
158 fullScreenWindow = new FullScreenWindow(this);
159 connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
161 // Handle settings changes
162 Settings *settings = Settings::instance();
163 connect(settings, SIGNAL(valueChanged(const QString &)),
164 this, SLOT(onSettingsChanged(const QString &)));
165 settings->setValue("orientation", settings->value("orientation"));
166 settings->setValue("lightson", settings->value("lightson"));
168 // Handle book view buttons
169 connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
170 connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
172 // Adopt view, show window
175 #ifdef DORIAN_TEST_MODEL
176 (void)new ModelTest(Library::instance(), this);
180 void MainWindow::onCurrentBookChanged()
182 setCurrentBook(Library::instance()->nowReading());
185 void MainWindow::showRegular()
189 // Re-parent children
190 fullScreenWindow->leaveChildren();
191 QList<QWidget *> otherChildren;
192 otherChildren << progress << previousButton << nextButton;
193 takeChildren(view, otherChildren);
195 // Adjust geometry of decorations
196 QRect geo = geometry();
197 progress->setGeometry(0, 0, geo.width(), DORIAN_PROGRESS_HEIGHT);
198 #if defined(Q_WS_MAEMO_5)
199 previousButton->setGeometry(0,
200 geo.height() - toolBar->height() - TranslucentButton::pixels,
201 TranslucentButton::pixels, TranslucentButton::pixels);
202 nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
203 TranslucentButton::pixels, TranslucentButton::pixels);
204 #elif defined(Q_OS_SYMBIAN)
205 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
206 TranslucentButton::pixels, TranslucentButton::pixels);
207 nextButton->setGeometry(geo.width() - TranslucentButton::pixels,
208 0, TranslucentButton::pixels, TranslucentButton::pixels);
210 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
211 TranslucentButton::pixels, TranslucentButton::pixels);
212 nextButton->setGeometry(geo.width() - TranslucentButton::pixels - 25,
213 toolBar->height(), TranslucentButton::pixels,
214 TranslucentButton::pixels);
215 #endif // Q_WS_MAEMO_5
216 qDebug() << "previousButton geometry" << previousButton->geometry();
218 fullScreenWindow->hide();
220 #if defined(Q_OS_SYMBIAN)
222 #elif defined(Q_WS_MAEMO_5)
223 // FIXME: This is ugly.
224 view->restoreLastBookmark();
228 previousButton->flash();
231 void MainWindow::showBig()
235 // Re-parent children
237 QList<QWidget *> otherChildren;
238 otherChildren << progress << nextButton << previousButton;
239 fullScreenWindow->takeChildren(view, otherChildren);
241 // Adjust geometry of decorations
242 QRect screen = QApplication::desktop()->screenGeometry();
243 progress->setGeometry(0, 0, screen.width(), DORIAN_PROGRESS_HEIGHT);
244 #if defined(Q_WS_MAEMO_5)
245 nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
246 TranslucentButton::pixels, TranslucentButton::pixels);
248 nextButton->setGeometry(screen.width() - TranslucentButton::pixels - 25, 0,
249 TranslucentButton::pixels, TranslucentButton::pixels);
250 #endif // Q_WS_MAEMO_5
251 previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
252 TranslucentButton::pixels, TranslucentButton::pixels);
257 fullScreenWindow->showFullScreen();
259 fullScreenWindow->activateWindow();
263 previousButton->flash();
266 void MainWindow::setCurrentBook(const QModelIndex ¤t)
269 Book *book = Library::instance()->book(current);
271 setWindowTitle(book? book->shortName(): tr("Dorian"));
274 void MainWindow::showLibrary()
276 (new LibraryDialog(this))->show();
279 void MainWindow::showSettings()
281 (new SettingsWindow(this))->show();
284 void MainWindow::showInfo()
286 if (mCurrent.isValid()) {
287 (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
292 void MainWindow::showDevTools()
294 (new DevTools())->exec();
297 void MainWindow::showBookmarks()
299 Book *book = Library::instance()->book(mCurrent);
301 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
302 bookmarks->setWindowModality(Qt::WindowModal);
303 connect(bookmarks, SIGNAL(addBookmark(const QString &)),
304 this, SLOT(onAddBookmark(const QString &)));
305 connect(bookmarks, SIGNAL(goToBookmark(int)),
306 this, SLOT(onGoToBookmark(int)));
311 void MainWindow::closeEvent(QCloseEvent *event)
314 view->setLastBookmark();
318 void MainWindow::onSettingsChanged(const QString &key)
322 #if defined(Q_WS_MAEMO_5)
323 if (key == "orientation") {
324 QString value = Settings::instance()->value(key).toString();
325 if (value == "portrait") {
326 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
327 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
329 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
330 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
332 } else if (key == "lightson") {
333 bool enable = Settings::instance()->value(key, false).toBool();
334 killTimer(preventBlankingTimer);
336 preventBlankingTimer = startTimer(29 * 1000);
339 #endif // Q_WS_MAEMO_5
342 void MainWindow::onPartLoadStart()
345 Platform::showBusy(this, true);
348 void MainWindow::onPartLoadEnd(int index)
351 bool enablePrevious = false;
352 bool enableNext = false;
353 Book *book = Library::instance()->book(mCurrent);
356 enablePrevious = true;
358 if (index < (book->parts.size() - 1)) {
362 Platform::showBusy(this, false);
365 void MainWindow::onAddBookmark(const QString ¬e)
368 view->addBookmark(note);
369 Platform::information(tr("Bookmarked current position"), this);
372 void MainWindow::onGoToBookmark(int index)
375 Book *book = Library::instance()->book(mCurrent);
376 view->goToBookmark(book->bookmarks()[index]);
379 void MainWindow::showChapters()
381 Book *book = Library::instance()->book(mCurrent);
383 ChaptersDialog *chapters = new ChaptersDialog(book, this);
384 chapters->setWindowModality(Qt::WindowModal);
385 connect(chapters, SIGNAL(goToChapter(int)),
386 this, SLOT(onGoToChapter(int)));
391 void MainWindow::onGoToChapter(int index)
395 Book *book = Library::instance()->book(mCurrent);
398 int partIndex = book->partFromChapter(index, fragment);
399 if (partIndex != -1) {
400 view->goToPart(partIndex, fragment);
405 void MainWindow::timerEvent(QTimerEvent *event)
407 if (event->timerId() == preventBlankingTimer) {
409 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
410 MCE_REQUEST_IF, QDBusConnection::systemBus());
411 mce.call(MCE_PREVENT_BLANK_REQ);
412 #endif // Q_WS_MAEMO_5
413 qDebug() << "MainWindow::timerEvent: Prevent display blanking";
415 AdopterWindow::timerEvent(event);
418 void MainWindow::resizeEvent(QResizeEvent *e)
421 progress->setGeometry(QRect(0, 0, e->size().width(), DORIAN_PROGRESS_HEIGHT));
422 #if defined(Q_WS_MAEMO_5)
423 previousButton->setGeometry(0,
424 e->size().height() - toolBar->height() - TranslucentButton::pixels,
425 TranslucentButton::pixels, TranslucentButton::pixels);
426 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
427 TranslucentButton::pixels, TranslucentButton::pixels);
428 #elif defined(Q_OS_SYMBIAN)
429 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
430 TranslucentButton::pixels, TranslucentButton::pixels);
431 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
432 0, TranslucentButton::pixels, TranslucentButton::pixels);
434 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
435 TranslucentButton::pixels, TranslucentButton::pixels);
436 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels - 25,
437 toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
438 #endif // Q_WS_MAEMO_5
439 qDebug() << "previousButton geometry" << previousButton->geometry();
440 previousButton->flash();
442 QMainWindow::resizeEvent(e);
445 void MainWindow::about()
447 Dyalog *aboutDialog = new Dyalog(this, false);
448 aboutDialog->setWindowTitle(tr("About Dorian"));
449 QLabel *label = new QLabel(aboutDialog);
450 label->setTextFormat(Qt::RichText);
451 label->setOpenExternalLinks(true);
452 label->setWordWrap(true);
453 label->setText(tr("<b>Dorian %1</b><br><br>Copyright © 2010 "
454 "Akos Polster <akos@pipacs.com><br>"
455 "Licensed under GNU General Public License, Version 3<br>"
456 "Source code:<br><a href='https://garage.maemo.org/projects/dorian/'>"
457 "garage.maemo.org/projects/dorian</a>").arg(Platform::version()));
458 aboutDialog->addWidget(label);
459 aboutDialog->addStretch();
463 void MainWindow::goToNextPage()
466 previousButton->flash();
470 void MainWindow::goToPreviousPage()
473 previousButton->flash();
474 view->goPreviousPage();
477 void MainWindow::onBeginUpgrade(int total)
479 libraryProgress->setVisible(total > 0);
480 libraryProgress->setWindowTitle(tr("Upgrading library"));
481 libraryProgress->setMaximum(total);
484 void MainWindow::onUpgrading(const QString &path)
486 libraryProgress->setLabelText(tr("Upgrading %1").
487 arg(QFileInfo(path).fileName()));
488 libraryProgress->setValue(libraryProgress->value() + 1);
491 void MainWindow::onEndUpgrade()
493 libraryProgress->hide();
494 libraryProgress->reset();
497 void MainWindow::onBeginLoad(int total)
499 libraryProgress->setVisible(total > 0);
500 libraryProgress->setWindowTitle(tr("Loading library"));
501 libraryProgress->setMaximum(total);
504 void MainWindow::onLoading(const QString &path)
506 libraryProgress->setLabelText(tr("Loading %1").
507 arg(QFileInfo(path).fileName()));
508 libraryProgress->setValue(libraryProgress->value() + 1);
511 void MainWindow::onEndLoad()
513 libraryProgress->hide();
514 libraryProgress->reset();