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");
194 // Re-parent children
195 fullScreenWindow->leaveChildren();
196 QList<QWidget *> otherChildren;
197 otherChildren << progress << previousButton << nextButton;
198 takeChildren(view, otherChildren);
200 // Adjust geometry of decorations
201 QRect geo = geometry();
202 progress->setGeometry(0, 0, geo.width(), DORIAN_PROGRESS_HEIGHT);
203 #if defined(Q_WS_MAEMO_5)
204 previousButton->setGeometry(0,
205 geo.height() - toolBar->height() - TranslucentButton::pixels,
206 TranslucentButton::pixels, TranslucentButton::pixels);
207 nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
208 TranslucentButton::pixels, TranslucentButton::pixels);
209 #elif defined(Q_OS_SYMBIAN)
210 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
211 TranslucentButton::pixels, TranslucentButton::pixels);
212 nextButton->setGeometry(geo.width() - TranslucentButton::pixels,
213 0, TranslucentButton::pixels, TranslucentButton::pixels);
215 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
216 TranslucentButton::pixels, TranslucentButton::pixels);
217 nextButton->setGeometry(geo.width() - TranslucentButton::pixels - 25,
218 toolBar->height(), TranslucentButton::pixels,
219 TranslucentButton::pixels);
220 #endif // Q_WS_MAEMO_5
221 qDebug() << "previousButton geometry" << previousButton->geometry();
223 fullScreenWindow->hide();
227 nextButton->flash(1500);
228 previousButton->flash(1500);
231 void MainWindow::showBig()
233 Trace t("MainWindow::showBig");
235 // Re-parent children
237 QList<QWidget *> otherChildren;
238 otherChildren << progress << nextButton << previousButton;
239 fullScreenWindow->takeChildren(view, otherChildren);
241 // Adjust geometry of decorations
242 QRect screen = QApplication::desktop()->screenGeometry();
243 progress->setGeometry(0, 0, screen.width(), DORIAN_PROGRESS_HEIGHT);
244 #if defined(Q_WS_MAEMO_5)
245 nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
246 TranslucentButton::pixels, TranslucentButton::pixels);
248 nextButton->setGeometry(screen.width() - TranslucentButton::pixels - 25, 0,
249 TranslucentButton::pixels, TranslucentButton::pixels);
250 #endif // Q_WS_MAEMO_5
251 previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
252 TranslucentButton::pixels, TranslucentButton::pixels);
255 fullScreenWindow->showFullScreen();
256 fullScreenWindow->activateWindow();
258 nextButton->flash(1500);
259 previousButton->flash(1500);
262 void MainWindow::setCurrentBook(const QModelIndex ¤t)
265 Book *book = Library::instance()->book(current);
267 setWindowTitle(book? book->shortName(): tr("Dorian"));
270 void MainWindow::showLibrary()
272 (new LibraryDialog(this))->show();
275 void MainWindow::showSettings()
277 (new SettingsWindow(this))->show();
280 void MainWindow::showInfo()
282 if (mCurrent.isValid()) {
283 (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
288 void MainWindow::showDevTools()
290 (new DevTools())->exec();
293 void MainWindow::showBookmarks()
295 Book *book = Library::instance()->book(mCurrent);
297 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
298 bookmarks->setWindowModality(Qt::WindowModal);
299 connect(bookmarks, SIGNAL(addBookmark(const QString &)),
300 this, SLOT(onAddBookmark(const QString &)));
301 connect(bookmarks, SIGNAL(goToBookmark(int)),
302 this, SLOT(onGoToBookmark(int)));
307 void MainWindow::closeEvent(QCloseEvent *event)
309 Trace t("MainWindow::closeEvent");
310 view->setLastBookmark();
314 void MainWindow::onSettingsChanged(const QString &key)
317 if (key == "orientation") {
318 QString value = Settings::instance()->value(key).toString();
319 qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
320 if (value == "portrait") {
321 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
322 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
324 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
325 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
327 } else if (key == "lightson") {
328 bool enable = Settings::instance()->value(key, false).toBool();
329 qDebug() << "MainWindow::onSettingsChanged: lightson:" << enable;
330 killTimer(preventBlankingTimer);
332 preventBlankingTimer = startTimer(29 * 1000);
334 } else if (key == "usevolumekeys") {
335 bool value = Settings::instance()->value(key).toBool();
336 qDebug() << "MainWindow::onSettingsChanged: usevolumekeys" << value;
338 fullScreenWindow->grabZoomKeys(value);
342 #endif // Q_WS_MAEMO_5
345 void MainWindow::onPartLoadStart()
347 Trace t("MainWindow::onPartLoadStart");
349 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
353 void MainWindow::onPartLoadEnd(int index)
355 Trace t("MainWindow::onPartLoadEnd");
356 bool enablePrevious = false;
357 bool enableNext = false;
358 Book *book = Library::instance()->book(mCurrent);
361 enablePrevious = true;
363 if (index < (book->parts.size() - 1)) {
368 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
369 #endif // Q_WS_MAEMO_5
372 void MainWindow::onAddBookmark(const QString ¬e)
374 Trace t("MainWindow:onAddBookmark");
375 view->addBookmark(note);
378 void MainWindow::onGoToBookmark(int index)
380 Trace t("MainWindow::onGoToBookmark");
381 Book *book = Library::instance()->book(mCurrent);
382 view->goToBookmark(book->bookmarks()[index]);
385 void MainWindow::showChapters()
387 Book *book = Library::instance()->book(mCurrent);
389 ChaptersDialog *chapters = new ChaptersDialog(book, this);
390 chapters->setWindowModality(Qt::WindowModal);
391 connect(chapters, SIGNAL(goToChapter(int)),
392 this, SLOT(onGoToChapter(int)));
397 void MainWindow::onGoToChapter(int index)
399 Trace t("MainWindow::onGoToChapter");
401 Book *book = Library::instance()->book(mCurrent);
403 int partIndex = book->partFromChapter(index);
404 if (partIndex != -1) {
405 view->goToBookmark(Book::Bookmark(partIndex, 0));
410 void MainWindow::timerEvent(QTimerEvent *event)
412 if (event->timerId() == preventBlankingTimer) {
414 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
415 MCE_REQUEST_IF, QDBusConnection::systemBus());
416 mce.call(MCE_PREVENT_BLANK_REQ);
417 #endif // Q_WS_MAEMO_5
418 qDebug() << "MainWindow::timerEvent: Prevent display blanking";
420 AdopterWindow::timerEvent(event);
423 void MainWindow::resizeEvent(QResizeEvent *e)
425 Trace t("MainWindow::resizeEvent");
426 progress->setGeometry(QRect(0, 0, e->size().width(), DORIAN_PROGRESS_HEIGHT));
427 #if defined(Q_WS_MAEMO_5)
428 previousButton->setGeometry(0,
429 e->size().height() - toolBar->height() - TranslucentButton::pixels,
430 TranslucentButton::pixels, TranslucentButton::pixels);
431 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
432 TranslucentButton::pixels, TranslucentButton::pixels);
433 #elif defined(Q_OS_SYMBIAN)
434 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
435 TranslucentButton::pixels, TranslucentButton::pixels);
436 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
437 0, TranslucentButton::pixels, TranslucentButton::pixels);
439 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
440 TranslucentButton::pixels, TranslucentButton::pixels);
441 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels - 25,
442 toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
443 #endif // Q_WS_MAEMO_5
444 qDebug() << "previousButton geometry" << previousButton->geometry();
445 previousButton->flash(1500);
446 nextButton->flash(1500);
447 QMainWindow::resizeEvent(e);
450 void MainWindow::about()
452 Dyalog *aboutDialog = new Dyalog(this, false);
453 aboutDialog->setWindowTitle(tr("About Dorian"));
454 QLabel *label = new QLabel(aboutDialog);
455 label->setTextFormat(Qt::RichText);
456 label->setOpenExternalLinks(true);
457 label->setWordWrap(true);
458 label->setText(tr("<b>Dorian %1</b><br><br>Copyright © 2010 "
459 "Akos Polster <akos@pipacs.com><br>"
460 "Licensed under GNU General Public License, Version 3<br>"
461 "Source code: <a href='https://garage.maemo.org/projects/dorian/'>"
462 "garage.maemo.org/projects/dorian</a>").arg(Platform::version()));
463 aboutDialog->addWidget(label);
464 aboutDialog->addStretch();
469 void MainWindow::goToNextPage()
471 nextButton->flash(1500);
472 previousButton->flash(1500);
476 void MainWindow::goToPreviousPage()
478 nextButton->flash(1500);
479 previousButton->flash(1500);
480 view->goPreviousPage();
483 void MainWindow::onBeginUpgrade(int total)
485 libraryProgress->setVisible(total > 0);
486 libraryProgress->setWindowTitle(tr("Upgrading library"));
487 libraryProgress->setMaximum(total);
490 void MainWindow::onUpgrading(const QString &path)
492 libraryProgress->setLabelText(tr("Upgrading %1").
493 arg(QFileInfo(path).fileName()));
494 libraryProgress->setValue(libraryProgress->value() + 1);
497 void MainWindow::onEndUpgrade()
499 libraryProgress->hide();
500 libraryProgress->reset();
504 void MainWindow::onBeginLoad(int total)
506 libraryProgress->setVisible(total > 0);
507 libraryProgress->setWindowTitle(tr("Loading library"));
508 libraryProgress->setMaximum(total);
511 void MainWindow::onLoading(const QString &path)
513 libraryProgress->setLabelText(tr("Loading %1").
514 arg(QFileInfo(path).fileName()));
515 libraryProgress->setValue(libraryProgress->value() + 1);
518 void MainWindow::onEndLoad()
520 libraryProgress->hide();
521 libraryProgress->reset();