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 #include "progressdialog.h"
40 #ifdef DORIAN_TEST_MODEL
41 # include "modeltest.h"
44 const int DORIAN_PROGRESS_HEIGHT = 17;
46 MainWindow::MainWindow(QWidget *parent):
47 AdopterWindow(parent), view(0), preventBlankingTimer(-1)
49 Trace t("MainWindow::MainWindow");
51 setAttribute(Qt::WA_Maemo5StackedWindow, true);
53 setWindowTitle("Dorian");
55 // Central widget. Must be an intermediate, because the book view widget
56 // can be re-parented later
57 QFrame *central = new QFrame(this);
58 QVBoxLayout *layout = new QVBoxLayout(central);
60 central->setLayout(layout);
61 setCentralWidget(central);
64 view = new BookView(central);
66 layout->addWidget(view);
69 progress = new Progress(central);
72 settings = new QDialog(this);
77 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
78 "view-fullscreen", tr("Full screen"));
81 chaptersAction = addToolBarAction(this, SLOT(showChapters()),
82 "chapters", tr("Chapters"));
83 bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
84 "bookmarks", tr("Bookmarks"));
85 infoAction = addToolBarAction(this, SLOT(showInfo()),
86 "info", tr("Book info"));
87 libraryAction = addToolBarAction(this, SLOT(showLibrary()),
88 "library", tr("Library"));
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"));
111 // Buttons on top of the book view
112 previousButton = new TranslucentButton("back", this);
113 nextButton = new TranslucentButton("forward", this);
115 // Handle model changes
116 connect(Library::instance(), SIGNAL(nowReadingChanged()),
117 this, SLOT(onCurrentBookChanged()));
119 // Load library, upgrade it if needed
120 libraryProgress = new ProgressDialog(tr("Upgrading library"), this);
121 Library *library = Library::instance();
122 connect(library, SIGNAL(beginUpgrade(int)), this, SLOT(onBeginUpgrade(int)));
123 connect(library, SIGNAL(upgrading(const QString &)),
124 this, SLOT(onUpgrading(const QString &)));
125 connect(library, SIGNAL(endUpgrade()), this, SLOT(onEndUpgrade()));
126 connect(library, SIGNAL(beginLoad(int)), this, SLOT(onBeginLoad(int)));
127 connect(library, SIGNAL(loading(const QString &)),
128 this, SLOT(onLoading(const QString &)));
129 connect(library, SIGNAL(endLoad()), this, SLOT(onEndLoad()));
133 // Load book on command line, or load last read book, or load default book
134 if (QCoreApplication::arguments().size() == 2) {
135 QString path = QCoreApplication::arguments()[1];
137 QModelIndex index = library->find(path);
138 if (index.isValid()) {
139 library->setNowReading(index);
142 QModelIndex index = library->nowReading();
143 if (index.isValid()) {
144 library->setNowReading(index);
146 if (!library->rowCount()) {
147 library->add(":/books/2BR02B.epub");
149 library->setNowReading(library->index(0));
153 // Handle loading book parts
154 connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
155 connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
158 connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
160 // Shadow window for full screen reading
161 fullScreenWindow = new FullScreenWindow(this);
162 connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
164 // Handle settings changes
165 Settings *settings = Settings::instance();
166 connect(settings, SIGNAL(valueChanged(const QString &)),
167 this, SLOT(onSettingsChanged(const QString &)));
168 settings->setValue("orientation", settings->value("orientation"));
169 settings->setValue("lightson", settings->value("lightson"));
170 settings->setValue("usevolumekeys", settings->value("usevolumekeys"));
172 // Handle book view buttons
173 connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
174 connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
176 #ifdef DORIAN_TEST_MODEL
177 (void)new ModelTest(Library::instance(), this);
181 MainWindow::~MainWindow()
185 void MainWindow::onCurrentBookChanged()
187 setCurrentBook(Library::instance()->nowReading());
190 void MainWindow::showRegular()
192 Trace t("MainWindow::showRegular");
193 fullScreenWindow->hide();
194 fullScreenWindow->leaveChildren();
196 QList<QWidget *> otherChildren;
197 otherChildren << progress << previousButton << nextButton;
198 takeChildren(view, otherChildren);
199 QRect geo = geometry();
200 progress->setGeometry(0, 0, geo.width(), DORIAN_PROGRESS_HEIGHT);
201 #if defined(Q_WS_MAEMO_5)
202 previousButton->setGeometry(0,
203 geo.height() - toolBar->height() - TranslucentButton::pixels,
204 TranslucentButton::pixels, TranslucentButton::pixels);
205 nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
206 TranslucentButton::pixels, TranslucentButton::pixels);
207 #elif defined(Q_OS_SYMBIAN)
208 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
209 TranslucentButton::pixels, TranslucentButton::pixels);
210 nextButton->setGeometry(geo.width() - TranslucentButton::pixels,
211 0, TranslucentButton::pixels, TranslucentButton::pixels);
213 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
214 TranslucentButton::pixels, TranslucentButton::pixels);
215 nextButton->setGeometry(geo.width() - TranslucentButton::pixels - 25,
216 toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
217 #endif // Q_WS_MAEMO_5
218 qDebug() << "previousButton geometry" << previousButton->geometry();
221 previousButton->show();
222 nextButton->flash(1500);
223 previousButton->flash(1500);
226 void MainWindow::showBig()
228 Trace t("MainWindow::showBig");
230 QList<QWidget *> otherChildren;
231 otherChildren << progress << nextButton << previousButton;
232 QRect screen = QApplication::desktop()->screenGeometry();
233 progress->setGeometry(0, 0, screen.width(), DORIAN_PROGRESS_HEIGHT);
234 #if defined(Q_WS_MAEMO_5)
235 nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
236 TranslucentButton::pixels, TranslucentButton::pixels);
238 nextButton->setGeometry(screen.width() - TranslucentButton::pixels - 25, 0,
239 TranslucentButton::pixels, TranslucentButton::pixels);
240 #endif // Q_WS_MAEMO_5
241 previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
242 TranslucentButton::pixels, TranslucentButton::pixels);
244 fullScreenWindow->takeChildren(view, otherChildren);
245 fullScreenWindow->showFullScreen();
247 nextButton->flash(1500);
248 previousButton->flash(1500);
251 void MainWindow::setCurrentBook(const QModelIndex ¤t)
254 Book *book = Library::instance()->book(current);
256 setWindowTitle(book? book->shortName(): tr("Dorian"));
259 void MainWindow::showLibrary()
261 (new LibraryDialog(this))->show();
264 void MainWindow::showSettings()
266 (new SettingsWindow(this))->show();
269 void MainWindow::showInfo()
271 if (mCurrent.isValid()) {
272 (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
277 void MainWindow::showDevTools()
279 (new DevTools())->exec();
282 void MainWindow::showBookmarks()
284 Book *book = Library::instance()->book(mCurrent);
286 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
287 bookmarks->setWindowModality(Qt::WindowModal);
288 connect(bookmarks, SIGNAL(addBookmark(const QString &)),
289 this, SLOT(onAddBookmark(const QString &)));
290 connect(bookmarks, SIGNAL(goToBookmark(int)),
291 this, SLOT(onGoToBookmark(int)));
296 void MainWindow::closeEvent(QCloseEvent *event)
298 Trace t("MainWindow::closeEvent");
299 view->setLastBookmark();
303 void MainWindow::onSettingsChanged(const QString &key)
306 if (key == "orientation") {
307 QString value = Settings::instance()->value(key).toString();
308 qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
309 if (value == "portrait") {
310 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
311 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
313 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
314 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
316 } else if (key == "lightson") {
317 bool enable = Settings::instance()->value(key, false).toBool();
318 qDebug() << "MainWindow::onSettingsChanged: lightson:" << enable;
319 killTimer(preventBlankingTimer);
321 preventBlankingTimer = startTimer(29 * 1000);
323 } else if (key == "usevolumekeys") {
324 bool value = Settings::instance()->value(key).toBool();
325 qDebug() << "MainWindow::onSettingsChanged: usevolumekeys" << value;
327 fullScreenWindow->grabZoomKeys(value);
331 #endif // Q_WS_MAEMO_5
334 void MainWindow::onPartLoadStart()
336 Trace t("MainWindow::onPartLoadStart");
338 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
342 void MainWindow::onPartLoadEnd(int index)
344 Trace t("MainWindow::onPartLoadEnd");
345 bool enablePrevious = false;
346 bool enableNext = false;
347 Book *book = Library::instance()->book(mCurrent);
350 enablePrevious = true;
352 if (index < (book->parts.size() - 1)) {
357 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
358 #endif // Q_WS_MAEMO_5
361 void MainWindow::onAddBookmark(const QString ¬e)
363 Trace t("MainWindow:onAddBookmark");
364 view->addBookmark(note);
367 void MainWindow::onGoToBookmark(int index)
369 Trace t("MainWindow::onGoToBookmark");
370 Book *book = Library::instance()->book(mCurrent);
371 view->goToBookmark(book->bookmarks()[index]);
374 void MainWindow::showChapters()
376 Book *book = Library::instance()->book(mCurrent);
378 ChaptersDialog *chapters = new ChaptersDialog(book, this);
379 chapters->setWindowModality(Qt::WindowModal);
380 connect(chapters, SIGNAL(goToChapter(int)),
381 this, SLOT(onGoToChapter(int)));
386 void MainWindow::onGoToChapter(int index)
388 Trace t("MainWindow::onGoToChapter");
390 Book *book = Library::instance()->book(mCurrent);
392 int partIndex = book->partFromChapter(index);
393 if (partIndex != -1) {
394 view->goToBookmark(Book::Bookmark(partIndex, 0));
399 void MainWindow::timerEvent(QTimerEvent *event)
401 if (event->timerId() == preventBlankingTimer) {
403 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
404 MCE_REQUEST_IF, QDBusConnection::systemBus());
405 mce.call(MCE_PREVENT_BLANK_REQ);
406 #endif // Q_WS_MAEMO_5
407 qDebug() << "MainWindow::timerEvent: Prevent display blanking";
409 AdopterWindow::timerEvent(event);
412 void MainWindow::resizeEvent(QResizeEvent *e)
414 Trace t("MainWindow::resizeEvent");
415 progress->setGeometry(QRect(0, 0, e->size().width(), DORIAN_PROGRESS_HEIGHT));
416 #if defined(Q_WS_MAEMO_5)
417 previousButton->setGeometry(0,
418 e->size().height() - toolBar->height() - TranslucentButton::pixels,
419 TranslucentButton::pixels, TranslucentButton::pixels);
420 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
421 TranslucentButton::pixels, TranslucentButton::pixels);
422 #elif defined(Q_OS_SYMBIAN)
423 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
424 TranslucentButton::pixels, TranslucentButton::pixels);
425 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
426 0, TranslucentButton::pixels, TranslucentButton::pixels);
428 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
429 TranslucentButton::pixels, TranslucentButton::pixels);
430 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels - 25,
431 toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
432 #endif // Q_WS_MAEMO_5
433 qDebug() << "previousButton geometry" << previousButton->geometry();
434 previousButton->flash(1500);
435 nextButton->flash(1500);
436 QMainWindow::resizeEvent(e);
439 void MainWindow::about()
441 Dyalog *aboutDialog = new Dyalog(this, false);
442 aboutDialog->setWindowTitle(tr("About Dorian"));
443 QLabel *label = new QLabel(aboutDialog);
444 label->setTextFormat(Qt::RichText);
445 label->setOpenExternalLinks(true);
446 label->setWordWrap(true);
447 label->setText(tr("<b>Dorian %1</b><br><br>Copyright © 2010 "
448 "Akos Polster <akos@pipacs.com><br>"
449 "Licensed under GNU General Public License, Version 3<br>"
450 "Source code: <a href='https://garage.maemo.org/projects/dorian/'>"
451 "garage.maemo.org/projects/dorian</a>").arg(Platform::version()));
452 aboutDialog->addWidget(label);
453 aboutDialog->addStretch();
458 void MainWindow::goToNextPage()
460 nextButton->flash(1500);
461 previousButton->flash(1500);
465 void MainWindow::goToPreviousPage()
467 nextButton->flash(1500);
468 previousButton->flash(1500);
469 view->goPreviousPage();
472 void MainWindow::onBeginUpgrade(int total)
474 libraryProgress->setVisible(total > 0);
475 libraryProgress->setWindowTitle(tr("Upgrading library"));
476 libraryProgress->setMaximum(total);
479 void MainWindow::onUpgrading(const QString &path)
481 libraryProgress->setLabelText(tr("Upgrading %1").
482 arg(QFileInfo(path).fileName()));
483 libraryProgress->setValue(libraryProgress->value() + 1);
486 void MainWindow::onEndUpgrade()
488 libraryProgress->hide();
489 libraryProgress->reset();
493 void MainWindow::onBeginLoad(int total)
495 libraryProgress->setVisible(total > 0);
496 libraryProgress->setWindowTitle(tr("Loading library"));
497 libraryProgress->setMaximum(total);
500 void MainWindow::onLoading(const QString &path)
502 libraryProgress->setLabelText(tr("Loading %1").
503 arg(QFileInfo(path).fileName()));
504 libraryProgress->setValue(libraryProgress->value() + 1);
507 void MainWindow::onEndLoad()
509 libraryProgress->hide();
510 libraryProgress->reset();