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"
39 #ifdef DORIAN_TEST_MODEL
40 # include "modeltest.h"
43 const int DORIAN_PROGRESS_HEIGHT = 17;
45 MainWindow::MainWindow(QWidget *parent):
46 AdopterWindow(parent), view(0), preventBlankingTimer(-1)
48 Trace t("MainWindow::MainWindow");
50 setAttribute(Qt::WA_Maemo5StackedWindow, true);
52 setWindowTitle("Dorian");
54 // Central widget. Must be an intermediate, because the book view widget
55 // can be re-parented later
56 QFrame *central = new QFrame(this);
57 QVBoxLayout *layout = new QVBoxLayout(central);
59 central->setLayout(layout);
60 setCentralWidget(central);
63 view = new BookView(central);
65 layout->addWidget(view);
68 progress = new Progress(central);
71 settings = new QDialog(this);
76 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
77 "view-fullscreen", tr("Full screen"));
80 chaptersAction = addToolBarAction(this, SLOT(showChapters()),
81 "chapters", tr("Chapters"));
82 bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
83 "bookmarks", tr("Bookmarks"));
84 infoAction = addToolBarAction(this, SLOT(showInfo()),
85 "info", tr("Book info"));
86 libraryAction = addToolBarAction(this, SLOT(showLibrary()),
87 "library", tr("Library"));
90 settingsAction = menuBar()->addAction(tr("Settings"));
91 connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
92 devToolsAction = menuBar()->addAction(tr("Developer"));
93 connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
94 QAction *aboutAction = menuBar()->addAction(tr("About"));
95 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
97 settingsAction = addToolBarAction(this, SLOT(showSettings()),
98 "preferences-system", tr("Settings"));
99 devToolsAction = addToolBarAction(this, SLOT(showDevTools()),
100 "developer", tr("Developer"));
101 addToolBarAction(this, SLOT(about()), "about", tr("About"));
102 #endif // Q_WS_MAEMO_5
106 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
107 "view-fullscreen", tr("Full screen"));
110 // Buttons on top of the book view
111 previousButton = new TranslucentButton("back", this);
112 nextButton = new TranslucentButton("forward", this);
114 // Handle model changes
115 connect(Library::instance(), SIGNAL(nowReadingChanged()),
116 this, SLOT(onCurrentBookChanged()));
118 // Load library, upgrade it if needed
119 libraryProgress = new QProgressDialog(tr("Upgrading library"), "", 0, 0, this);
120 libraryProgress->reset();
121 libraryProgress->setMinimumDuration(0);
122 libraryProgress->setWindowModality(Qt::WindowModal);
123 libraryProgress->setCancelButton(0);
124 Library *library = Library::instance();
125 connect(library, SIGNAL(beginUpgrade(int)), this, SLOT(onBeginUpgrade(int)));
126 connect(library, SIGNAL(upgrading(const QString &)),
127 this, SLOT(onUpgrading(const QString &)));
128 connect(library, SIGNAL(endUpgrade()), this, SLOT(onEndUpgrade()));
129 connect(library, SIGNAL(beginLoad(int)), this, SLOT(onBeginLoad(int)));
130 connect(library, SIGNAL(loading(const QString &)),
131 this, SLOT(onLoading(const QString &)));
132 connect(library, SIGNAL(endLoad()), this, SLOT(onEndLoad()));
136 // Load book on command line, or load last read book, or load default book
137 if (QCoreApplication::arguments().size() == 2) {
138 QString path = QCoreApplication::arguments()[1];
140 QModelIndex index = library->find(path);
141 if (index.isValid()) {
142 library->setNowReading(index);
145 QModelIndex index = library->nowReading();
146 if (index.isValid()) {
147 library->setNowReading(index);
149 if (!library->rowCount()) {
150 library->add(":/books/2BR02B.epub");
152 library->setNowReading(library->index(0));
156 // Handle loading book parts
157 connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
158 connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
161 connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
163 // Shadow window for full screen reading
164 fullScreenWindow = new FullScreenWindow(this);
165 connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
167 // Handle settings changes
168 Settings *settings = Settings::instance();
169 connect(settings, SIGNAL(valueChanged(const QString &)),
170 this, SLOT(onSettingsChanged(const QString &)));
171 settings->setValue("orientation", settings->value("orientation"));
172 settings->setValue("lightson", settings->value("lightson"));
173 settings->setValue("usevolumekeys", settings->value("usevolumekeys"));
175 // Handle book view buttons
176 connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
177 connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
179 #ifdef DORIAN_TEST_MODEL
180 (void)new ModelTest(Library::instance(), this);
184 MainWindow::~MainWindow()
188 void MainWindow::onCurrentBookChanged()
190 setCurrentBook(Library::instance()->nowReading());
193 void MainWindow::showRegular()
195 Trace t("MainWindow::showRegular");
196 fullScreenWindow->hide();
197 fullScreenWindow->leaveChildren();
199 QList<QWidget *> otherChildren;
200 otherChildren << progress << previousButton << nextButton;
201 takeChildren(view, otherChildren);
202 QRect geo = geometry();
203 progress->setGeometry(0, 0, geo.width(), DORIAN_PROGRESS_HEIGHT);
204 #if defined(Q_WS_MAEMO_5)
205 previousButton->setGeometry(0,
206 geo.height() - toolBar->height() - TranslucentButton::pixels,
207 TranslucentButton::pixels, TranslucentButton::pixels);
208 nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
209 TranslucentButton::pixels, TranslucentButton::pixels);
210 #elif defined(Q_OS_SYMBIAN)
211 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
212 TranslucentButton::pixels, TranslucentButton::pixels);
213 nextButton->setGeometry(geo.width() - TranslucentButton::pixels,
214 0, TranslucentButton::pixels, TranslucentButton::pixels);
216 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
217 TranslucentButton::pixels, TranslucentButton::pixels);
218 nextButton->setGeometry(geo.width() - TranslucentButton::pixels - 25,
219 toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
220 #endif // Q_WS_MAEMO_5
221 qDebug() << "previousButton geometry" << previousButton->geometry();
224 previousButton->show();
225 nextButton->flash(1500);
226 previousButton->flash(1500);
229 void MainWindow::showBig()
231 Trace t("MainWindow::showBig");
233 QList<QWidget *> otherChildren;
234 otherChildren << progress << nextButton << previousButton;
235 QRect screen = QApplication::desktop()->screenGeometry();
236 progress->setGeometry(0, 0, screen.width(), DORIAN_PROGRESS_HEIGHT);
237 #if defined(Q_WS_MAEMO_5)
238 nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
239 TranslucentButton::pixels, TranslucentButton::pixels);
241 nextButton->setGeometry(screen.width() - TranslucentButton::pixels - 25, 0,
242 TranslucentButton::pixels, TranslucentButton::pixels);
243 #endif // Q_WS_MAEMO_5
244 previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
245 TranslucentButton::pixels, TranslucentButton::pixels);
247 fullScreenWindow->takeChildren(view, otherChildren);
248 fullScreenWindow->showFullScreen();
250 nextButton->flash(1500);
251 previousButton->flash(1500);
254 void MainWindow::setCurrentBook(const QModelIndex ¤t)
257 Book *book = Library::instance()->book(current);
259 setWindowTitle(book? book->shortName(): tr("Dorian"));
262 void MainWindow::showLibrary()
264 (new LibraryDialog(this))->show();
267 void MainWindow::showSettings()
269 (new SettingsWindow(this))->show();
272 void MainWindow::showInfo()
274 if (mCurrent.isValid()) {
275 (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
280 void MainWindow::showDevTools()
282 (new DevTools())->exec();
285 void MainWindow::showBookmarks()
287 Book *book = Library::instance()->book(mCurrent);
289 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
290 bookmarks->setWindowModality(Qt::WindowModal);
291 connect(bookmarks, SIGNAL(addBookmark(const QString &)),
292 this, SLOT(onAddBookmark(const QString &)));
293 connect(bookmarks, SIGNAL(goToBookmark(int)),
294 this, SLOT(onGoToBookmark(int)));
299 void MainWindow::closeEvent(QCloseEvent *event)
301 Trace t("MainWindow::closeEvent");
302 view->setLastBookmark();
306 void MainWindow::onSettingsChanged(const QString &key)
309 if (key == "orientation") {
310 QString value = Settings::instance()->value(key).toString();
311 qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
312 if (value == "portrait") {
313 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
314 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
316 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
317 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
319 } else if (key == "lightson") {
320 bool enable = Settings::instance()->value(key, false).toBool();
321 qDebug() << "MainWindow::onSettingsChanged: lightson:" << enable;
322 killTimer(preventBlankingTimer);
324 preventBlankingTimer = startTimer(29 * 1000);
326 } else if (key == "usevolumekeys") {
327 bool value = Settings::instance()->value(key).toBool();
328 qDebug() << "MainWindow::onSettingsChanged: usevolumekeys" << value;
330 fullScreenWindow->grabZoomKeys(value);
334 #endif // Q_WS_MAEMO_5
337 void MainWindow::onPartLoadStart()
339 Trace t("MainWindow::onPartLoadStart");
341 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
345 void MainWindow::onPartLoadEnd(int index)
347 Trace t("MainWindow::onPartLoadEnd");
348 bool enablePrevious = false;
349 bool enableNext = false;
350 Book *book = Library::instance()->book(mCurrent);
353 enablePrevious = true;
355 if (index < (book->parts.size() - 1)) {
360 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
361 #endif // Q_WS_MAEMO_5
364 void MainWindow::onAddBookmark(const QString ¬e)
366 Trace t("MainWindow:onAddBookmark");
367 view->addBookmark(note);
370 void MainWindow::onGoToBookmark(int index)
372 Trace t("MainWindow::onGoToBookmark");
373 Book *book = Library::instance()->book(mCurrent);
374 view->goToBookmark(book->bookmarks()[index]);
377 void MainWindow::showChapters()
379 Book *book = Library::instance()->book(mCurrent);
381 ChaptersDialog *chapters = new ChaptersDialog(book, this);
382 chapters->setWindowModality(Qt::WindowModal);
383 connect(chapters, SIGNAL(goToChapter(int)),
384 this, SLOT(onGoToChapter(int)));
389 void MainWindow::onGoToChapter(int index)
391 Trace t("MainWindow::onGoToChapter");
393 Book *book = Library::instance()->book(mCurrent);
395 int partIndex = book->partFromChapter(index);
396 if (partIndex != -1) {
397 view->goToBookmark(Book::Bookmark(partIndex, 0));
402 void MainWindow::timerEvent(QTimerEvent *event)
404 if (event->timerId() == preventBlankingTimer) {
406 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
407 MCE_REQUEST_IF, QDBusConnection::systemBus());
408 mce.call(MCE_PREVENT_BLANK_REQ);
409 #endif // Q_WS_MAEMO_5
410 qDebug() << "MainWindow::timerEvent: Prevent display blanking";
412 AdopterWindow::timerEvent(event);
415 void MainWindow::resizeEvent(QResizeEvent *e)
417 Trace t("MainWindow::resizeEvent");
418 progress->setGeometry(QRect(0, 0, e->size().width(), DORIAN_PROGRESS_HEIGHT));
419 #if defined(Q_WS_MAEMO_5)
420 previousButton->setGeometry(0,
421 e->size().height() - toolBar->height() - TranslucentButton::pixels,
422 TranslucentButton::pixels, TranslucentButton::pixels);
423 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
424 TranslucentButton::pixels, TranslucentButton::pixels);
425 #elif defined(Q_OS_SYMBIAN)
426 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
427 TranslucentButton::pixels, TranslucentButton::pixels);
428 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
429 0, TranslucentButton::pixels, TranslucentButton::pixels);
431 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
432 TranslucentButton::pixels, TranslucentButton::pixels);
433 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels - 25,
434 toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
435 #endif // Q_WS_MAEMO_5
436 qDebug() << "previousButton geometry" << previousButton->geometry();
437 previousButton->flash(1500);
438 nextButton->flash(1500);
439 QMainWindow::resizeEvent(e);
442 void MainWindow::about()
444 Dyalog *aboutDialog = new Dyalog(this, false);
445 aboutDialog->setWindowTitle(tr("About Dorian"));
446 QLabel *label = new QLabel(aboutDialog);
447 label->setTextFormat(Qt::RichText);
448 label->setOpenExternalLinks(true);
449 label->setWordWrap(true);
450 label->setText(tr("<b>Dorian %1</b><br><br>Copyright © 2010 "
451 "Akos Polster <akos@pipacs.com><br>"
452 "Licensed under GNU General Public License, Version 3<br>"
453 "Source code: <a href='https://garage.maemo.org/projects/dorian/'>"
454 "garage.maemo.org/projects/dorian</a>").arg(Platform::version()));
455 aboutDialog->addWidget(label);
456 aboutDialog->addStretch();
461 void MainWindow::goToNextPage()
463 nextButton->flash(1500);
464 previousButton->flash(1500);
468 void MainWindow::goToPreviousPage()
470 nextButton->flash(1500);
471 previousButton->flash(1500);
472 view->goPreviousPage();
475 void MainWindow::onBeginUpgrade(int total)
477 libraryProgress->setVisible(total > 0);
478 libraryProgress->setWindowTitle(tr("Upgrading library"));
479 libraryProgress->setMaximum(total);
482 void MainWindow::onUpgrading(const QString &path)
484 libraryProgress->setLabelText(tr("Upgrading %1").
485 arg(QFileInfo(path).fileName()));
486 libraryProgress->setValue(libraryProgress->value() + 1);
489 void MainWindow::onEndUpgrade()
491 libraryProgress->hide();
492 libraryProgress->reset();
496 void MainWindow::onBeginLoad(int total)
498 libraryProgress->setVisible(total > 0);
499 libraryProgress->setWindowTitle(tr("Loading library"));
500 libraryProgress->setMaximum(total);
503 void MainWindow::onLoading(const QString &path)
505 libraryProgress->setLabelText(tr("Loading %1").
506 arg(QFileInfo(path).fileName()));
507 libraryProgress->setValue(libraryProgress->value() + 1);
510 void MainWindow::onEndLoad()
512 libraryProgress->hide();
513 libraryProgress->reset();