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);
77 chaptersAction = addToolBarAction(this, SLOT(showChapters()),
78 "chapters", tr("Chapters"));
79 bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
80 "bookmarks", tr("Bookmarks"));
81 infoAction = addToolBarAction(this, SLOT(showInfo()),
82 "info", tr("Book info"));
83 libraryAction = addToolBarAction(this, SLOT(showLibrary()),
84 "library", tr("Library"));
87 settingsAction = menuBar()->addAction(tr("Settings"));
88 connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
89 devToolsAction = menuBar()->addAction(tr("Developer"));
90 connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
91 QAction *aboutAction = menuBar()->addAction(tr("About"));
92 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
94 settingsAction = addToolBarAction(this, SLOT(showSettings()),
95 "preferences-system", tr("Settings"));
96 devToolsAction = addToolBarAction(this, SLOT(showDevTools()),
97 "developer", tr("Developer"));
98 addToolBarAction(this, SLOT(about()), "about", tr("About"));
99 #endif // Q_WS_MAEMO_5
102 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
103 "view-fullscreen", tr("Full screen"));
105 // Buttons on top of the book view
106 previousButton = new TranslucentButton("back", this);
107 nextButton = new TranslucentButton("forward", this);
109 // Handle model changes
110 connect(Library::instance(), SIGNAL(nowReadingChanged()),
111 this, SLOT(onCurrentBookChanged()));
113 // Load library, upgrade it if needed
114 libraryProgress = new QProgressDialog(tr("Upgrading library"), "", 0, 0, this);
115 libraryProgress->reset();
116 libraryProgress->setMinimumDuration(0);
117 libraryProgress->setWindowModality(Qt::WindowModal);
118 libraryProgress->setCancelButton(0);
119 Library *library = Library::instance();
120 connect(library, SIGNAL(beginUpgrade(int)), this, SLOT(onBeginUpgrade(int)));
121 connect(library, SIGNAL(upgrading(const QString &)),
122 this, SLOT(onUpgrading(const QString &)));
123 connect(library, SIGNAL(endUpgrade()), this, SLOT(onEndUpgrade()));
124 connect(library, SIGNAL(beginLoad(int)), this, SLOT(onBeginLoad(int)));
125 connect(library, SIGNAL(loading(const QString &)),
126 this, SLOT(onLoading(const QString &)));
127 connect(library, SIGNAL(endLoad()), this, SLOT(onEndLoad()));
131 // Load book on command line, or load last read book, or load default book
132 if (QCoreApplication::arguments().size() == 2) {
133 QString path = QCoreApplication::arguments()[1];
135 QModelIndex index = library->find(path);
136 if (index.isValid()) {
137 library->setNowReading(index);
140 QModelIndex index = library->nowReading();
141 if (index.isValid()) {
142 library->setNowReading(index);
144 if (!library->rowCount()) {
145 library->add(":/books/2BR02B.epub");
147 library->setNowReading(library->index(0));
151 // Handle loading book parts
152 connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
153 connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
156 connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
158 // Shadow window for full screen reading
159 fullScreenWindow = new FullScreenWindow(this);
160 connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
162 // Handle settings changes
163 Settings *settings = Settings::instance();
164 connect(settings, SIGNAL(valueChanged(const QString &)),
165 this, SLOT(onSettingsChanged(const QString &)));
166 settings->setValue("orientation", settings->value("orientation"));
167 settings->setValue("lightson", settings->value("lightson"));
168 settings->setValue("usevolumekeys", settings->value("usevolumekeys"));
170 // Handle book view buttons
171 connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
172 connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
174 #ifdef DORIAN_TEST_MODEL
175 (void)new ModelTest(Library::instance(), this);
179 MainWindow::~MainWindow()
183 void MainWindow::onCurrentBookChanged()
185 setCurrentBook(Library::instance()->nowReading());
188 void MainWindow::showRegular()
190 Trace t("MainWindow::showRegular");
191 fullScreenWindow->hide();
192 fullScreenWindow->leaveChildren();
194 QList<QWidget *> otherChildren;
195 otherChildren << progress << previousButton << nextButton;
196 takeChildren(view, otherChildren);
197 QRect geo = geometry();
198 progress->setGeometry(0, 0, geo.width(), PROGRESS_HEIGHT);
199 #if defined(Q_WS_MAEMO_5)
200 previousButton->setGeometry(0,
201 geo.height() - toolBar->height() - TranslucentButton::pixels,
202 TranslucentButton::pixels, TranslucentButton::pixels);
203 nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
204 TranslucentButton::pixels, TranslucentButton::pixels);
205 #elif defined(Q_OS_SYMBIAN)
206 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
207 TranslucentButton::pixels, TranslucentButton::pixels);
208 nextButton->setGeometry(geo.width() - TranslucentButton::pixels,
209 0, TranslucentButton::pixels, TranslucentButton::pixels);
211 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
212 TranslucentButton::pixels, TranslucentButton::pixels);
213 nextButton->setGeometry(geo.width() - TranslucentButton::pixels - 25,
214 toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
215 #endif // Q_WS_MAEMO_5
216 qDebug() << "previousButton geometry" << previousButton->geometry();
219 previousButton->show();
220 nextButton->flash(1500);
221 previousButton->flash(1500);
224 void MainWindow::showBig()
226 Trace t("MainWindow::showBig");
228 QList<QWidget *> otherChildren;
229 otherChildren << progress << nextButton << previousButton;
230 QRect screen = QApplication::desktop()->screenGeometry();
231 progress->setGeometry(0, 0, screen.width(), PROGRESS_HEIGHT);
232 #if defined(Q_WS_MAEMO_5)
233 nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
234 TranslucentButton::pixels, TranslucentButton::pixels);
236 nextButton->setGeometry(screen.width() - TranslucentButton::pixels - 25, 0,
237 TranslucentButton::pixels, TranslucentButton::pixels);
238 #endif // Q_WS_MAEMO_5
239 previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
240 TranslucentButton::pixels, TranslucentButton::pixels);
242 fullScreenWindow->takeChildren(view, otherChildren);
243 fullScreenWindow->showFullScreen();
245 nextButton->flash(1500);
246 previousButton->flash(1500);
249 void MainWindow::setCurrentBook(const QModelIndex ¤t)
252 Book *book = Library::instance()->book(current);
254 setWindowTitle(book? book->shortName(): tr("Dorian"));
257 void MainWindow::showLibrary()
259 (new LibraryDialog(this))->show();
262 void MainWindow::showSettings()
264 (new SettingsWindow(this))->show();
267 void MainWindow::showInfo()
269 if (mCurrent.isValid()) {
270 (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
275 void MainWindow::showDevTools()
277 (new DevTools())->exec();
280 void MainWindow::showBookmarks()
282 Book *book = Library::instance()->book(mCurrent);
284 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
285 bookmarks->setWindowModality(Qt::WindowModal);
286 connect(bookmarks, SIGNAL(addBookmark()), this, SLOT(onAddBookmark()));
287 connect(bookmarks, SIGNAL(goToBookmark(int)),
288 this, SLOT(onGoToBookmark(int)));
293 void MainWindow::closeEvent(QCloseEvent *event)
295 Trace t("MainWindow::closeEvent");
296 view->setLastBookmark();
300 void MainWindow::onSettingsChanged(const QString &key)
303 if (key == "orientation") {
304 QString value = Settings::instance()->value(key).toString();
305 qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
306 if (value == "portrait") {
307 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
308 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
310 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
311 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
313 } else if (key == "lightson") {
314 bool enable = Settings::instance()->value(key, false).toBool();
315 qDebug() << "MainWindow::onSettingsChanged: lightson:" << enable;
316 killTimer(preventBlankingTimer);
318 preventBlankingTimer = startTimer(29 * 1000);
320 } else if (key == "usevolumekeys") {
321 bool value = Settings::instance()->value(key).toBool();
322 qDebug() << "MainWindow::onSettingsChanged: usevolumekeys" << value;
324 fullScreenWindow->grabZoomKeys(value);
328 #endif // Q_WS_MAEMO_5
331 void MainWindow::onPartLoadStart()
333 Trace t("MainWindow::onPartLoadStart");
335 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
339 void MainWindow::onPartLoadEnd(int index)
341 Trace t("MainWindow::onPartLoadEnd");
342 bool enablePrevious = false;
343 bool enableNext = false;
344 Book *book = Library::instance()->book(mCurrent);
347 enablePrevious = true;
349 if (index < (book->parts.size() - 1)) {
354 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
355 #endif // Q_WS_MAEMO_5
358 void MainWindow::onAddBookmark()
360 Trace t("MainWindow:onAddBookmark");
364 void MainWindow::onGoToBookmark(int index)
366 Trace t("MainWindow::onGoToBookmark");
367 Book *book = Library::instance()->book(mCurrent);
368 view->goToBookmark(book->bookmarks()[index]);
371 void MainWindow::showChapters()
373 Book *book = Library::instance()->book(mCurrent);
375 ChaptersDialog *chapters = new ChaptersDialog(book, this);
376 chapters->setWindowModality(Qt::WindowModal);
377 connect(chapters, SIGNAL(goToChapter(int)),
378 this, SLOT(onGoToChapter(int)));
383 void MainWindow::onGoToChapter(int index)
385 Trace t("MainWindow::onGoToChapter");
387 Book *book = Library::instance()->book(mCurrent);
389 int partIndex = book->partFromChapter(index);
390 if (partIndex != -1) {
391 view->goToBookmark(Book::Bookmark(partIndex, 0));
396 void MainWindow::timerEvent(QTimerEvent *event)
398 if (event->timerId() == preventBlankingTimer) {
400 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
401 MCE_REQUEST_IF, QDBusConnection::systemBus());
402 mce.call(MCE_PREVENT_BLANK_REQ);
403 #endif // Q_WS_MAEMO_5
404 qDebug() << "MainWindow::timerEvent: Prevent display blanking";
408 void MainWindow::resizeEvent(QResizeEvent *e)
410 Trace t("MainWindow::resizeEvent");
411 progress->setGeometry(QRect(0, 0, e->size().width(), PROGRESS_HEIGHT));
412 #if defined(Q_WS_MAEMO_5)
413 previousButton->setGeometry(0,
414 e->size().height() - toolBar->height() - TranslucentButton::pixels,
415 TranslucentButton::pixels, TranslucentButton::pixels);
416 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
417 TranslucentButton::pixels, TranslucentButton::pixels);
418 #elif defined(Q_OS_SYMBIAN)
419 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
420 TranslucentButton::pixels, TranslucentButton::pixels);
421 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
422 0, TranslucentButton::pixels, TranslucentButton::pixels);
424 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
425 TranslucentButton::pixels, TranslucentButton::pixels);
426 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels - 25,
427 toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
428 #endif // Q_WS_MAEMO_5
429 qDebug() << "previousButton geometry" << previousButton->geometry();
430 previousButton->flash(1500);
431 nextButton->flash(1500);
432 QMainWindow::resizeEvent(e);
435 void MainWindow::about()
437 Dyalog *aboutDialog = new Dyalog(this, false);
438 aboutDialog->setWindowTitle(tr("About Dorian"));
439 QLabel *label = new QLabel(aboutDialog);
440 label->setTextFormat(Qt::RichText);
441 label->setOpenExternalLinks(true);
442 label->setWordWrap(true);
443 label->setText(tr("<b>Dorian %1</b><br><br>Copyright © 2010 "
444 "Akos Polster <akos@pipacs.com><br>"
445 "Licensed under GNU General Public License, Version 3<br>"
446 "Source code: <a href='https://garage.maemo.org/projects/dorian/'>"
447 "garage.maemo.org/projects/dorian</a>").arg(DORIAN_VERSION));
448 aboutDialog->addWidget(label);
449 aboutDialog->addStretch();
454 void MainWindow::goToNextPage()
456 nextButton->flash(1500);
457 previousButton->flash(1500);
461 void MainWindow::goToPreviousPage()
463 nextButton->flash(1500);
464 previousButton->flash(1500);
465 view->goPreviousPage();
468 void MainWindow::onBeginUpgrade(int total)
470 libraryProgress->setVisible(total > 0);
471 libraryProgress->setWindowTitle(tr("Upgrading library"));
472 libraryProgress->setMaximum(total);
475 void MainWindow::onUpgrading(const QString &path)
477 libraryProgress->setLabelText(tr("Upgrading %1").
478 arg(QFileInfo(path).fileName()));
479 libraryProgress->setValue(libraryProgress->value() + 1);
482 void MainWindow::onEndUpgrade()
484 libraryProgress->hide();
485 libraryProgress->reset();
489 void MainWindow::onBeginLoad(int total)
491 libraryProgress->setVisible(total > 0);
492 libraryProgress->setWindowTitle(tr("Loading library"));
493 libraryProgress->setMaximum(total);
496 void MainWindow::onLoading(const QString &path)
498 libraryProgress->setLabelText(tr("Loading %1").
499 arg(QFileInfo(path).fileName()));
500 libraryProgress->setValue(libraryProgress->value() + 1);
503 void MainWindow::onEndLoad()
505 libraryProgress->hide();
506 libraryProgress->reset();