4 #include <QApplication>
11 # include <QtMaemo5/QMaemo5InformationBox>
13 # include <QtGui/QX11Info>
14 # include <X11/Xlib.h>
15 # include <X11/Xatom.h>
16 # include <mce/mode-names.h>
17 # include <mce/dbus-names.h>
18 #endif // Q_WS_MAEMO_5
23 #include "infodialog.h"
24 #include "librarydialog.h"
26 #include "mainwindow.h"
27 #include "settingswindow.h"
28 #include "bookmarksdialog.h"
30 #include "chaptersdialog.h"
31 #include "fullscreenwindow.h"
33 #include "bookfinder.h"
36 #include "translucentbutton.h"
38 #ifdef DORIAN_TEST_MODEL
39 # include "modeltest.h"
42 const int PROGRESS_HEIGHT = 17;
43 static const char *DORIAN_VERSION =
44 #include "pkg/version.txt"
47 MainWindow::MainWindow(QWidget *parent):
48 AdopterWindow(parent), view(0), preventBlankingTimer(-1)
50 Trace t("MainWindow::MainWindow");
52 setAttribute(Qt::WA_Maemo5StackedWindow, true);
54 setWindowTitle("Dorian");
56 // Central widget. Must be an intermediate, because the book view widget
57 // can be re-parented later
58 QFrame *central = new QFrame(this);
59 QVBoxLayout *layout = new QVBoxLayout(central);
61 central->setLayout(layout);
62 setCentralWidget(central);
65 view = new BookView(central);
67 layout->addWidget(view);
70 progress = new Progress(central);
73 settings = new QDialog(this);
78 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
79 "view-fullscreen", tr("Full screen"));
82 chaptersAction = addToolBarAction(this, SLOT(showChapters()),
83 "chapters", tr("Chapters"));
84 bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
85 "bookmarks", tr("Bookmarks"));
86 infoAction = addToolBarAction(this, SLOT(showInfo()),
87 "info", tr("Book info"));
88 libraryAction = addToolBarAction(this, SLOT(showLibrary()),
89 "library", tr("Library"));
92 settingsAction = menuBar()->addAction(tr("Settings"));
93 connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
94 devToolsAction = menuBar()->addAction(tr("Developer"));
95 connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
96 QAction *aboutAction = menuBar()->addAction(tr("About"));
97 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
99 settingsAction = addToolBarAction(this, SLOT(showSettings()),
100 "preferences-system", tr("Settings"));
101 devToolsAction = addToolBarAction(this, SLOT(showDevTools()),
102 "developer", tr("Developer"));
103 addToolBarAction(this, SLOT(about()), "about", tr("About"));
104 #endif // Q_WS_MAEMO_5
108 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
109 "view-fullscreen", tr("Full screen"));
112 // Buttons on top of the book view
113 previousButton = new TranslucentButton("back", this);
114 nextButton = new TranslucentButton("forward", this);
116 // Handle model changes
117 connect(Library::instance(), SIGNAL(nowReadingChanged()),
118 this, SLOT(onCurrentBookChanged()));
120 // Load library, upgrade it if needed
121 libraryProgress = new QProgressDialog(tr("Upgrading library"), "", 0, 0, this);
122 libraryProgress->reset();
123 libraryProgress->setMinimumDuration(0);
124 libraryProgress->setWindowModality(Qt::WindowModal);
125 libraryProgress->setCancelButton(0);
126 Library *library = Library::instance();
127 connect(library, SIGNAL(beginUpgrade(int)), this, SLOT(onBeginUpgrade(int)));
128 connect(library, SIGNAL(upgrading(const QString &)),
129 this, SLOT(onUpgrading(const QString &)));
130 connect(library, SIGNAL(endUpgrade()), this, SLOT(onEndUpgrade()));
131 connect(library, SIGNAL(beginLoad(int)), this, SLOT(onBeginLoad(int)));
132 connect(library, SIGNAL(loading(const QString &)),
133 this, SLOT(onLoading(const QString &)));
134 connect(library, SIGNAL(endLoad()), this, SLOT(onEndLoad()));
138 // Load book on command line, or load last read book, or load default book
139 if (QCoreApplication::arguments().size() == 2) {
140 QString path = QCoreApplication::arguments()[1];
142 QModelIndex index = library->find(path);
143 if (index.isValid()) {
144 library->setNowReading(index);
147 QModelIndex index = library->nowReading();
148 if (index.isValid()) {
149 library->setNowReading(index);
151 if (!library->rowCount()) {
152 library->add(":/books/2BR02B.epub");
154 library->setNowReading(library->index(0));
158 // Handle loading book parts
159 connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
160 connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
163 connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
165 // Shadow window for full screen reading
166 fullScreenWindow = new FullScreenWindow(this);
167 connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
169 // Handle settings changes
170 Settings *settings = Settings::instance();
171 connect(settings, SIGNAL(valueChanged(const QString &)),
172 this, SLOT(onSettingsChanged(const QString &)));
173 settings->setValue("orientation", settings->value("orientation"));
174 settings->setValue("lightson", settings->value("lightson"));
175 settings->setValue("usevolumekeys", settings->value("usevolumekeys"));
177 // Handle book view buttons
178 connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
179 connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
181 #ifdef DORIAN_TEST_MODEL
182 (void)new ModelTest(Library::instance(), this);
186 MainWindow::~MainWindow()
190 void MainWindow::onCurrentBookChanged()
192 setCurrentBook(Library::instance()->nowReading());
195 void MainWindow::showRegular()
197 Trace t("MainWindow::showRegular");
198 fullScreenWindow->hide();
199 fullScreenWindow->leaveChildren();
201 QList<QWidget *> otherChildren;
202 otherChildren << progress << previousButton << nextButton;
203 takeChildren(view, otherChildren);
204 QRect geo = geometry();
205 progress->setGeometry(0, 0, geo.width(), PROGRESS_HEIGHT);
206 #if defined(Q_WS_MAEMO_5)
207 previousButton->setGeometry(0,
208 geo.height() - toolBar->height() - TranslucentButton::pixels,
209 TranslucentButton::pixels, TranslucentButton::pixels);
210 nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
211 TranslucentButton::pixels, TranslucentButton::pixels);
212 #elif defined(Q_OS_SYMBIAN)
213 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
214 TranslucentButton::pixels, TranslucentButton::pixels);
215 nextButton->setGeometry(geo.width() - TranslucentButton::pixels,
216 0, TranslucentButton::pixels, TranslucentButton::pixels);
218 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
219 TranslucentButton::pixels, TranslucentButton::pixels);
220 nextButton->setGeometry(geo.width() - TranslucentButton::pixels - 25,
221 toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
222 #endif // Q_WS_MAEMO_5
223 qDebug() << "previousButton geometry" << previousButton->geometry();
226 previousButton->show();
227 nextButton->flash(1500);
228 previousButton->flash(1500);
231 void MainWindow::showBig()
233 Trace t("MainWindow::showBig");
235 QList<QWidget *> otherChildren;
236 otherChildren << progress << nextButton << previousButton;
237 QRect screen = QApplication::desktop()->screenGeometry();
238 progress->setGeometry(0, 0, screen.width(), PROGRESS_HEIGHT);
239 #if defined(Q_WS_MAEMO_5)
240 nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
241 TranslucentButton::pixels, TranslucentButton::pixels);
243 nextButton->setGeometry(screen.width() - TranslucentButton::pixels - 25, 0,
244 TranslucentButton::pixels, TranslucentButton::pixels);
245 #endif // Q_WS_MAEMO_5
246 previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
247 TranslucentButton::pixels, TranslucentButton::pixels);
249 fullScreenWindow->takeChildren(view, otherChildren);
250 fullScreenWindow->showFullScreen();
252 nextButton->flash(1500);
253 previousButton->flash(1500);
256 void MainWindow::setCurrentBook(const QModelIndex ¤t)
259 Book *book = Library::instance()->book(current);
261 setWindowTitle(book? book->shortName(): tr("Dorian"));
264 void MainWindow::showLibrary()
266 (new LibraryDialog(this))->show();
269 void MainWindow::showSettings()
271 (new SettingsWindow(this))->show();
274 void MainWindow::showInfo()
276 if (mCurrent.isValid()) {
277 (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
282 void MainWindow::showDevTools()
284 (new DevTools())->exec();
287 void MainWindow::showBookmarks()
289 Book *book = Library::instance()->book(mCurrent);
291 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
292 bookmarks->setWindowModality(Qt::WindowModal);
293 connect(bookmarks, SIGNAL(addBookmark(const QString &)),
294 this, SLOT(onAddBookmark(const QString &)));
295 connect(bookmarks, SIGNAL(goToBookmark(int)),
296 this, SLOT(onGoToBookmark(int)));
301 void MainWindow::closeEvent(QCloseEvent *event)
303 Trace t("MainWindow::closeEvent");
304 view->setLastBookmark();
308 void MainWindow::onSettingsChanged(const QString &key)
311 if (key == "orientation") {
312 QString value = Settings::instance()->value(key).toString();
313 qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
314 if (value == "portrait") {
315 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
316 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
318 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
319 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
321 } else if (key == "lightson") {
322 bool enable = Settings::instance()->value(key, false).toBool();
323 qDebug() << "MainWindow::onSettingsChanged: lightson:" << enable;
324 killTimer(preventBlankingTimer);
326 preventBlankingTimer = startTimer(29 * 1000);
328 } else if (key == "usevolumekeys") {
329 bool value = Settings::instance()->value(key).toBool();
330 qDebug() << "MainWindow::onSettingsChanged: usevolumekeys" << value;
332 fullScreenWindow->grabZoomKeys(value);
336 #endif // Q_WS_MAEMO_5
339 void MainWindow::onPartLoadStart()
341 Trace t("MainWindow::onPartLoadStart");
343 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
347 void MainWindow::onPartLoadEnd(int index)
349 Trace t("MainWindow::onPartLoadEnd");
350 bool enablePrevious = false;
351 bool enableNext = false;
352 Book *book = Library::instance()->book(mCurrent);
355 enablePrevious = true;
357 if (index < (book->parts.size() - 1)) {
362 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
363 #endif // Q_WS_MAEMO_5
366 void MainWindow::onAddBookmark(const QString ¬e)
368 Trace t("MainWindow:onAddBookmark");
369 view->addBookmark(note);
372 void MainWindow::onGoToBookmark(int index)
374 Trace t("MainWindow::onGoToBookmark");
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)
393 Trace t("MainWindow::onGoToChapter");
395 Book *book = Library::instance()->book(mCurrent);
397 int partIndex = book->partFromChapter(index);
398 if (partIndex != -1) {
399 view->goToBookmark(Book::Bookmark(partIndex, 0));
404 void MainWindow::timerEvent(QTimerEvent *event)
406 if (event->timerId() == preventBlankingTimer) {
408 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
409 MCE_REQUEST_IF, QDBusConnection::systemBus());
410 mce.call(MCE_PREVENT_BLANK_REQ);
411 #endif // Q_WS_MAEMO_5
412 qDebug() << "MainWindow::timerEvent: Prevent display blanking";
414 AdopterWindow::timerEvent(event);
417 void MainWindow::resizeEvent(QResizeEvent *e)
419 Trace t("MainWindow::resizeEvent");
420 progress->setGeometry(QRect(0, 0, e->size().width(), PROGRESS_HEIGHT));
421 #if defined(Q_WS_MAEMO_5)
422 previousButton->setGeometry(0,
423 e->size().height() - toolBar->height() - TranslucentButton::pixels,
424 TranslucentButton::pixels, TranslucentButton::pixels);
425 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
426 TranslucentButton::pixels, TranslucentButton::pixels);
427 #elif defined(Q_OS_SYMBIAN)
428 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
429 TranslucentButton::pixels, TranslucentButton::pixels);
430 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
431 0, TranslucentButton::pixels, TranslucentButton::pixels);
433 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
434 TranslucentButton::pixels, TranslucentButton::pixels);
435 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels - 25,
436 toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
437 #endif // Q_WS_MAEMO_5
438 qDebug() << "previousButton geometry" << previousButton->geometry();
439 previousButton->flash(1500);
440 nextButton->flash(1500);
441 QMainWindow::resizeEvent(e);
444 void MainWindow::about()
446 Dyalog *aboutDialog = new Dyalog(this, false);
447 aboutDialog->setWindowTitle(tr("About Dorian"));
448 QLabel *label = new QLabel(aboutDialog);
449 label->setTextFormat(Qt::RichText);
450 label->setOpenExternalLinks(true);
451 label->setWordWrap(true);
452 label->setText(tr("<b>Dorian %1</b><br><br>Copyright © 2010 "
453 "Akos Polster <akos@pipacs.com><br>"
454 "Licensed under GNU General Public License, Version 3<br>"
455 "Source code: <a href='https://garage.maemo.org/projects/dorian/'>"
456 "garage.maemo.org/projects/dorian</a>").arg(DORIAN_VERSION));
457 aboutDialog->addWidget(label);
458 aboutDialog->addStretch();
463 void MainWindow::goToNextPage()
465 nextButton->flash(1500);
466 previousButton->flash(1500);
470 void MainWindow::goToPreviousPage()
472 nextButton->flash(1500);
473 previousButton->flash(1500);
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();
498 void MainWindow::onBeginLoad(int total)
500 libraryProgress->setVisible(total > 0);
501 libraryProgress->setWindowTitle(tr("Loading library"));
502 libraryProgress->setMaximum(total);
505 void MainWindow::onLoading(const QString &path)
507 libraryProgress->setLabelText(tr("Loading %1").
508 arg(QFileInfo(path).fileName()));
509 libraryProgress->setValue(libraryProgress->value() + 1);
512 void MainWindow::onEndLoad()
514 libraryProgress->hide();
515 libraryProgress->reset();