4 # include <QtMaemo5/QMaemo5InformationBox>
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(central);
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"));
104 // Buttons on top of the book view
105 previousButton = new TranslucentButton("back", this);
106 nextButton = new TranslucentButton("forward", this);
108 // Handle model changes
109 connect(Library::instance(), SIGNAL(nowReadingChanged()),
110 this, SLOT(onCurrentBookChanged()));
112 // Load library, upgrade it if needed
113 libraryProgress = new ProgressDialog(tr("Upgrading library"), this);
114 Library *library = Library::instance();
115 connect(library, SIGNAL(beginUpgrade(int)), this, SLOT(onBeginUpgrade(int)));
116 connect(library, SIGNAL(upgrading(const QString &)),
117 this, SLOT(onUpgrading(const QString &)));
118 connect(library, SIGNAL(endUpgrade()), this, SLOT(onEndUpgrade()));
119 connect(library, SIGNAL(beginLoad(int)), this, SLOT(onBeginLoad(int)));
120 connect(library, SIGNAL(loading(const QString &)),
121 this, SLOT(onLoading(const QString &)));
122 connect(library, SIGNAL(endLoad()), this, SLOT(onEndLoad()));
126 // Load book on command line, or load last read book, or load default book
127 if (QCoreApplication::arguments().size() == 2) {
128 QString path = QCoreApplication::arguments()[1];
130 QModelIndex index = library->find(path);
131 if (index.isValid()) {
132 library->setNowReading(index);
135 QModelIndex index = library->nowReading();
136 if (index.isValid()) {
137 library->setNowReading(index);
139 if (!library->rowCount()) {
140 library->add(":/books/2BR02B.epub");
142 library->setNowReading(library->index(0));
146 // Handle loading book parts
147 connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
148 connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
151 connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
153 // Shadow window for full screen reading
154 fullScreenWindow = new FullScreenWindow(this);
155 connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
157 // Handle settings changes
158 Settings *settings = Settings::instance();
159 connect(settings, SIGNAL(valueChanged(const QString &)),
160 this, SLOT(onSettingsChanged(const QString &)));
161 settings->setValue("orientation", settings->value("orientation"));
162 settings->setValue("lightson", settings->value("lightson"));
163 settings->setValue("usevolumekeys", settings->value("usevolumekeys"));
165 // Handle book view buttons
166 connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
167 connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
169 #ifdef DORIAN_TEST_MODEL
170 (void)new ModelTest(Library::instance(), this);
174 MainWindow::~MainWindow()
178 void MainWindow::onCurrentBookChanged()
180 setCurrentBook(Library::instance()->nowReading());
183 void MainWindow::showRegular()
187 // Re-parent children
188 fullScreenWindow->leaveChildren();
189 QList<QWidget *> otherChildren;
190 otherChildren << progress << previousButton << nextButton;
191 takeChildren(view, otherChildren);
193 // Adjust geometry of decorations
194 QRect geo = geometry();
195 progress->setGeometry(0, 0, geo.width(), DORIAN_PROGRESS_HEIGHT);
196 #if defined(Q_WS_MAEMO_5)
197 previousButton->setGeometry(0,
198 geo.height() - toolBar->height() - TranslucentButton::pixels,
199 TranslucentButton::pixels, TranslucentButton::pixels);
200 nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
201 TranslucentButton::pixels, TranslucentButton::pixels);
202 #elif defined(Q_OS_SYMBIAN)
203 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
204 TranslucentButton::pixels, TranslucentButton::pixels);
205 nextButton->setGeometry(geo.width() - TranslucentButton::pixels,
206 0, TranslucentButton::pixels, TranslucentButton::pixels);
208 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
209 TranslucentButton::pixels, TranslucentButton::pixels);
210 nextButton->setGeometry(geo.width() - TranslucentButton::pixels - 25,
211 toolBar->height(), TranslucentButton::pixels,
212 TranslucentButton::pixels);
213 #endif // Q_WS_MAEMO_5
214 qDebug() << "previousButton geometry" << previousButton->geometry();
216 fullScreenWindow->hide();
218 #if defined(Q_OS_SYMBIAN)
220 #elif defined(Q_WS_MAEMO_5)
221 // FIXME: This is ugly.
222 view->restoreLastBookmark();
225 nextButton->flash(1500);
226 previousButton->flash(1500);
229 void MainWindow::showBig()
233 // Re-parent children
235 QList<QWidget *> otherChildren;
236 otherChildren << progress << nextButton << previousButton;
237 fullScreenWindow->takeChildren(view, otherChildren);
239 // Adjust geometry of decorations
240 QRect screen = QApplication::desktop()->screenGeometry();
241 progress->setGeometry(0, 0, screen.width(), DORIAN_PROGRESS_HEIGHT);
242 #if defined(Q_WS_MAEMO_5)
243 nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
244 TranslucentButton::pixels, TranslucentButton::pixels);
246 nextButton->setGeometry(screen.width() - TranslucentButton::pixels - 25, 0,
247 TranslucentButton::pixels, TranslucentButton::pixels);
248 #endif // Q_WS_MAEMO_5
249 previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
250 TranslucentButton::pixels, TranslucentButton::pixels);
255 fullScreenWindow->showFullScreen();
257 fullScreenWindow->activateWindow();
260 nextButton->flash(1500);
261 previousButton->flash(1500);
264 void MainWindow::setCurrentBook(const QModelIndex ¤t)
267 Book *book = Library::instance()->book(current);
269 setWindowTitle(book? book->shortName(): tr("Dorian"));
272 void MainWindow::showLibrary()
274 (new LibraryDialog(this))->show();
277 void MainWindow::showSettings()
279 (new SettingsWindow(this))->show();
282 void MainWindow::showInfo()
284 if (mCurrent.isValid()) {
285 (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
290 void MainWindow::showDevTools()
292 (new DevTools())->exec();
295 void MainWindow::showBookmarks()
297 Book *book = Library::instance()->book(mCurrent);
299 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
300 bookmarks->setWindowModality(Qt::WindowModal);
301 connect(bookmarks, SIGNAL(addBookmark(const QString &)),
302 this, SLOT(onAddBookmark(const QString &)));
303 connect(bookmarks, SIGNAL(goToBookmark(int)),
304 this, SLOT(onGoToBookmark(int)));
309 void MainWindow::closeEvent(QCloseEvent *event)
312 view->setLastBookmark();
316 void MainWindow::onSettingsChanged(const QString &key)
319 if (key == "orientation") {
320 QString value = Settings::instance()->value(key).toString();
321 qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
322 if (value == "portrait") {
323 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
324 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
326 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
327 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
329 } else if (key == "lightson") {
330 bool enable = Settings::instance()->value(key, false).toBool();
331 qDebug() << "MainWindow::onSettingsChanged: lightson:" << enable;
332 killTimer(preventBlankingTimer);
334 preventBlankingTimer = startTimer(29 * 1000);
336 } else if (key == "usevolumekeys") {
337 bool value = Settings::instance()->value(key).toBool();
338 qDebug() << "MainWindow::onSettingsChanged: usevolumekeys" << value;
340 fullScreenWindow->grabZoomKeys(value);
344 #endif // Q_WS_MAEMO_5
347 void MainWindow::onPartLoadStart()
351 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
355 void MainWindow::onPartLoadEnd(int index)
358 bool enablePrevious = false;
359 bool enableNext = false;
360 Book *book = Library::instance()->book(mCurrent);
363 enablePrevious = true;
365 if (index < (book->parts.size() - 1)) {
370 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
371 #endif // Q_WS_MAEMO_5
374 void MainWindow::onAddBookmark(const QString ¬e)
377 view->addBookmark(note);
380 void MainWindow::onGoToBookmark(int index)
383 Book *book = Library::instance()->book(mCurrent);
384 view->goToBookmark(book->bookmarks()[index]);
387 void MainWindow::showChapters()
389 Book *book = Library::instance()->book(mCurrent);
391 ChaptersDialog *chapters = new ChaptersDialog(book, this);
392 chapters->setWindowModality(Qt::WindowModal);
393 connect(chapters, SIGNAL(goToChapter(int)),
394 this, SLOT(onGoToChapter(int)));
399 void MainWindow::onGoToChapter(int index)
403 Book *book = Library::instance()->book(mCurrent);
405 int partIndex = book->partFromChapter(index);
406 if (partIndex != -1) {
407 view->goToBookmark(Book::Bookmark(partIndex, 0));
412 void MainWindow::timerEvent(QTimerEvent *event)
414 if (event->timerId() == preventBlankingTimer) {
416 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
417 MCE_REQUEST_IF, QDBusConnection::systemBus());
418 mce.call(MCE_PREVENT_BLANK_REQ);
419 #endif // Q_WS_MAEMO_5
420 qDebug() << "MainWindow::timerEvent: Prevent display blanking";
422 AdopterWindow::timerEvent(event);
425 void MainWindow::resizeEvent(QResizeEvent *e)
428 progress->setGeometry(QRect(0, 0, e->size().width(), DORIAN_PROGRESS_HEIGHT));
429 #if defined(Q_WS_MAEMO_5)
430 previousButton->setGeometry(0,
431 e->size().height() - toolBar->height() - TranslucentButton::pixels,
432 TranslucentButton::pixels, TranslucentButton::pixels);
433 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
434 TranslucentButton::pixels, TranslucentButton::pixels);
435 #elif defined(Q_OS_SYMBIAN)
436 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
437 TranslucentButton::pixels, TranslucentButton::pixels);
438 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
439 0, TranslucentButton::pixels, TranslucentButton::pixels);
441 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
442 TranslucentButton::pixels, TranslucentButton::pixels);
443 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels - 25,
444 toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
445 #endif // Q_WS_MAEMO_5
446 qDebug() << "previousButton geometry" << previousButton->geometry();
447 previousButton->flash(1500);
448 nextButton->flash(1500);
449 QMainWindow::resizeEvent(e);
452 void MainWindow::about()
454 Dyalog *aboutDialog = new Dyalog(this, false);
455 aboutDialog->setWindowTitle(tr("About Dorian"));
456 QLabel *label = new QLabel(aboutDialog);
457 label->setTextFormat(Qt::RichText);
458 label->setOpenExternalLinks(true);
459 label->setWordWrap(true);
460 label->setText(tr("<b>Dorian %1</b><br><br>Copyright © 2010 "
461 "Akos Polster <akos@pipacs.com><br>"
462 "Licensed under GNU General Public License, Version 3<br>"
463 "Source code: <a href='https://garage.maemo.org/projects/dorian/'>"
464 "garage.maemo.org/projects/dorian</a>").arg(Platform::version()));
465 aboutDialog->addWidget(label);
466 aboutDialog->addStretch();
471 void MainWindow::goToNextPage()
473 nextButton->flash(1500);
474 previousButton->flash(1500);
478 void MainWindow::goToPreviousPage()
480 nextButton->flash(1500);
481 previousButton->flash(1500);
482 view->goPreviousPage();
485 void MainWindow::onBeginUpgrade(int total)
487 libraryProgress->setVisible(total > 0);
488 libraryProgress->setWindowTitle(tr("Upgrading library"));
489 libraryProgress->setMaximum(total);
492 void MainWindow::onUpgrading(const QString &path)
494 libraryProgress->setLabelText(tr("Upgrading %1").
495 arg(QFileInfo(path).fileName()));
496 libraryProgress->setValue(libraryProgress->value() + 1);
499 void MainWindow::onEndUpgrade()
501 libraryProgress->hide();
502 libraryProgress->reset();
506 void MainWindow::onBeginLoad(int total)
508 libraryProgress->setVisible(total > 0);
509 libraryProgress->setWindowTitle(tr("Loading library"));
510 libraryProgress->setMaximum(total);
513 void MainWindow::onLoading(const QString &path)
515 libraryProgress->setLabelText(tr("Loading %1").
516 arg(QFileInfo(path).fileName()));
517 libraryProgress->setValue(libraryProgress->value() + 1);
520 void MainWindow::onEndLoad()
522 libraryProgress->hide();
523 libraryProgress->reset();