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()), this, SLOT(onAddBookmark()));
294 connect(bookmarks, SIGNAL(goToBookmark(int)),
295 this, SLOT(onGoToBookmark(int)));
300 void MainWindow::closeEvent(QCloseEvent *event)
302 Trace t("MainWindow::closeEvent");
303 view->setLastBookmark();
307 void MainWindow::onSettingsChanged(const QString &key)
310 if (key == "orientation") {
311 QString value = Settings::instance()->value(key).toString();
312 qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
313 if (value == "portrait") {
314 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
315 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
317 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
318 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
320 } else if (key == "lightson") {
321 bool enable = Settings::instance()->value(key, false).toBool();
322 qDebug() << "MainWindow::onSettingsChanged: lightson:" << enable;
323 killTimer(preventBlankingTimer);
325 preventBlankingTimer = startTimer(29 * 1000);
327 } else if (key == "usevolumekeys") {
328 bool value = Settings::instance()->value(key).toBool();
329 qDebug() << "MainWindow::onSettingsChanged: usevolumekeys" << value;
331 fullScreenWindow->grabZoomKeys(value);
335 #endif // Q_WS_MAEMO_5
338 void MainWindow::onPartLoadStart()
340 Trace t("MainWindow::onPartLoadStart");
342 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
346 void MainWindow::onPartLoadEnd(int index)
348 Trace t("MainWindow::onPartLoadEnd");
349 bool enablePrevious = false;
350 bool enableNext = false;
351 Book *book = Library::instance()->book(mCurrent);
354 enablePrevious = true;
356 if (index < (book->parts.size() - 1)) {
361 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
362 #endif // Q_WS_MAEMO_5
365 void MainWindow::onAddBookmark()
367 Trace t("MainWindow:onAddBookmark");
371 void MainWindow::onGoToBookmark(int index)
373 Trace t("MainWindow::onGoToBookmark");
374 Book *book = Library::instance()->book(mCurrent);
375 view->goToBookmark(book->bookmarks()[index]);
378 void MainWindow::showChapters()
380 Book *book = Library::instance()->book(mCurrent);
382 ChaptersDialog *chapters = new ChaptersDialog(book, this);
383 chapters->setWindowModality(Qt::WindowModal);
384 connect(chapters, SIGNAL(goToChapter(int)),
385 this, SLOT(onGoToChapter(int)));
390 void MainWindow::onGoToChapter(int index)
392 Trace t("MainWindow::onGoToChapter");
394 Book *book = Library::instance()->book(mCurrent);
396 int partIndex = book->partFromChapter(index);
397 if (partIndex != -1) {
398 view->goToBookmark(Book::Bookmark(partIndex, 0));
403 void MainWindow::timerEvent(QTimerEvent *event)
405 if (event->timerId() == preventBlankingTimer) {
407 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
408 MCE_REQUEST_IF, QDBusConnection::systemBus());
409 mce.call(MCE_PREVENT_BLANK_REQ);
410 #endif // Q_WS_MAEMO_5
411 qDebug() << "MainWindow::timerEvent: Prevent display blanking";
413 AdopterWindow::timerEvent(event);
416 void MainWindow::resizeEvent(QResizeEvent *e)
418 Trace t("MainWindow::resizeEvent");
419 progress->setGeometry(QRect(0, 0, e->size().width(), PROGRESS_HEIGHT));
420 #if defined(Q_WS_MAEMO_5)
421 previousButton->setGeometry(0,
422 e->size().height() - toolBar->height() - TranslucentButton::pixels,
423 TranslucentButton::pixels, TranslucentButton::pixels);
424 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
425 TranslucentButton::pixels, TranslucentButton::pixels);
426 #elif defined(Q_OS_SYMBIAN)
427 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
428 TranslucentButton::pixels, TranslucentButton::pixels);
429 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
430 0, TranslucentButton::pixels, TranslucentButton::pixels);
432 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
433 TranslucentButton::pixels, TranslucentButton::pixels);
434 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels - 25,
435 toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
436 #endif // Q_WS_MAEMO_5
437 qDebug() << "previousButton geometry" << previousButton->geometry();
438 previousButton->flash(1500);
439 nextButton->flash(1500);
440 QMainWindow::resizeEvent(e);
443 void MainWindow::about()
445 Dyalog *aboutDialog = new Dyalog(this, false);
446 aboutDialog->setWindowTitle(tr("About Dorian"));
447 QLabel *label = new QLabel(aboutDialog);
448 label->setTextFormat(Qt::RichText);
449 label->setOpenExternalLinks(true);
450 label->setWordWrap(true);
451 label->setText(tr("<b>Dorian %1</b><br><br>Copyright © 2010 "
452 "Akos Polster <akos@pipacs.com><br>"
453 "Licensed under GNU General Public License, Version 3<br>"
454 "Source code: <a href='https://garage.maemo.org/projects/dorian/'>"
455 "garage.maemo.org/projects/dorian</a>").arg(DORIAN_VERSION));
456 aboutDialog->addWidget(label);
457 aboutDialog->addStretch();
462 void MainWindow::goToNextPage()
464 nextButton->flash(1500);
465 previousButton->flash(1500);
469 void MainWindow::goToPreviousPage()
471 nextButton->flash(1500);
472 previousButton->flash(1500);
473 view->goPreviousPage();
476 void MainWindow::onBeginUpgrade(int total)
478 libraryProgress->setVisible(total > 0);
479 libraryProgress->setWindowTitle(tr("Upgrading library"));
480 libraryProgress->setMaximum(total);
483 void MainWindow::onUpgrading(const QString &path)
485 libraryProgress->setLabelText(tr("Upgrading %1").
486 arg(QFileInfo(path).fileName()));
487 libraryProgress->setValue(libraryProgress->value() + 1);
490 void MainWindow::onEndUpgrade()
492 libraryProgress->hide();
493 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();