6 # include <QtGui/QX11Info>
8 # include <X11/Xatom.h>
9 # include <mce/mode-names.h>
10 # include <mce/dbus-names.h>
11 #endif // Q_WS_MAEMO_5
16 #include "infodialog.h"
17 #include "librarydialog.h"
19 #include "mainwindow.h"
20 #include "settingswindow.h"
21 #include "bookmarksdialog.h"
23 #include "chaptersdialog.h"
24 #include "fullscreenwindow.h"
26 #include "bookfinder.h"
29 #include "translucentbutton.h"
31 #include "progressdialog.h"
33 #ifdef DORIAN_TEST_MODEL
34 # include "modeltest.h"
37 const int DORIAN_PROGRESS_HEIGHT = 17;
39 MainWindow::MainWindow(QWidget *parent):
40 AdopterWindow(parent), view(0), preventBlankingTimer(-1)
44 setAttribute(Qt::WA_Maemo5StackedWindow, true);
46 setWindowTitle("Dorian");
48 // Central widget. Must be an intermediate, because the book view widget
49 // can be re-parented later
50 QFrame *central = new QFrame(this);
51 QVBoxLayout *layout = new QVBoxLayout(central);
53 central->setLayout(layout);
54 setCentralWidget(central);
57 view = new BookView(central);
59 layout->addWidget(view);
62 progress = new Progress(this);
65 settings = new QDialog(this);
70 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
71 "view-fullscreen", tr("Full screen"));
74 chaptersAction = addToolBarAction(this, SLOT(showChapters()),
75 "chapters", tr("Chapters"));
76 bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
77 "bookmarks", tr("Bookmarks"));
78 infoAction = addToolBarAction(this, SLOT(showInfo()),
79 "info", tr("Book info"));
80 libraryAction = addToolBarAction(this, SLOT(showLibrary()),
81 "library", tr("Library"));
84 settingsAction = menuBar()->addAction(tr("Settings"));
85 connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
86 devToolsAction = menuBar()->addAction(tr("Developer"));
87 connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
88 QAction *aboutAction = menuBar()->addAction(tr("About"));
89 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
91 settingsAction = addToolBarAction(this, SLOT(showSettings()),
92 "preferences-system", tr("Settings"));
93 devToolsAction = addToolBarAction(this, SLOT(showDevTools()),
94 "developer", tr("Developer"));
95 addToolBarAction(this, SLOT(about()), "about", tr("About"));
96 #endif // Q_WS_MAEMO_5
100 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
101 "view-fullscreen", tr("Full screen"));
103 (void)addToolBarAction(this, SLOT(close()), "", tr("Exit"));
106 // Buttons on top of the book view
107 previousButton = new TranslucentButton("back", this);
108 nextButton = new TranslucentButton("forward", this);
110 // Handle model changes
111 connect(Library::instance(), SIGNAL(nowReadingChanged()),
112 this, SLOT(onCurrentBookChanged()));
114 // Load library, upgrade it if needed
115 libraryProgress = new ProgressDialog(tr("Upgrading library"), this);
116 Library *library = Library::instance();
117 connect(library, SIGNAL(beginUpgrade(int)), this, SLOT(onBeginUpgrade(int)));
118 connect(library, SIGNAL(upgrading(const QString &)),
119 this, SLOT(onUpgrading(const QString &)));
120 connect(library, SIGNAL(endUpgrade()), this, SLOT(onEndUpgrade()));
122 // Handle loading book parts
123 connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
124 connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
127 connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
129 // Shadow window for full screen reading
130 fullScreenWindow = new FullScreenWindow(this);
131 connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
133 // Handle settings changes
134 connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
135 this, SLOT(onSettingsChanged(const QString &)));
137 // Handle book view buttons
138 connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
139 connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
141 // Adopt view, show window
144 #ifdef DORIAN_TEST_MODEL
145 (void)new ModelTest(Library::instance(), this);
149 void MainWindow::initialize()
152 Library *library = Library::instance();
154 // Upgrade library if needed, then load it
158 // Load book on command line, or load last read book, or load default book
159 if (QCoreApplication::arguments().size() == 2) {
160 QString path = QCoreApplication::arguments()[1];
162 QModelIndex index = library->find(path);
163 if (index.isValid()) {
164 library->setNowReading(index);
167 QModelIndex index = library->nowReading();
168 if (index.isValid()) {
169 library->setNowReading(index);
171 if (!library->rowCount()) {
172 library->add(":/books/2BR02B.epub");
174 library->setNowReading(library->index(0));
179 void MainWindow::onCurrentBookChanged()
182 setCurrentBook(Library::instance()->nowReading());
185 void MainWindow::showRegular()
189 // Re-parent children
190 fullScreenWindow->leaveChildren();
191 QList<QWidget *> otherChildren;
192 otherChildren << progress << previousButton << nextButton;
193 takeChildren(view, otherChildren);
195 // Adjust geometry of decorations
196 QRect geo = geometry();
197 progress->setGeometry(0, 0, geo.width(), DORIAN_PROGRESS_HEIGHT);
198 #if defined(Q_WS_MAEMO_5)
199 previousButton->setGeometry(0,
200 geo.height() - toolBar->height() - TranslucentButton::pixels,
201 TranslucentButton::pixels, TranslucentButton::pixels);
202 nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
203 TranslucentButton::pixels, TranslucentButton::pixels);
204 #elif defined(Q_OS_SYMBIAN)
205 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
206 TranslucentButton::pixels, TranslucentButton::pixels);
207 nextButton->setGeometry(geo.width() - TranslucentButton::pixels,
208 0, TranslucentButton::pixels, TranslucentButton::pixels);
210 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
211 TranslucentButton::pixels, TranslucentButton::pixels);
212 nextButton->setGeometry(geo.width() - TranslucentButton::pixels - 25,
213 toolBar->height(), TranslucentButton::pixels,
214 TranslucentButton::pixels);
215 #endif // Q_WS_MAEMO_5
216 qDebug() << "previousButton geometry" << previousButton->geometry();
218 fullScreenWindow->hide();
220 #if defined(Q_OS_SYMBIAN)
225 previousButton->flash();
228 void MainWindow::showBig()
232 // Re-parent children
234 QList<QWidget *> otherChildren;
235 otherChildren << progress << nextButton << previousButton;
236 fullScreenWindow->takeChildren(view, otherChildren);
238 // Adjust geometry of decorations
239 QRect screen = QApplication::desktop()->screenGeometry();
240 progress->setGeometry(0, 0, screen.width(), DORIAN_PROGRESS_HEIGHT);
241 #if defined(Q_WS_MAEMO_5)
242 nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
243 TranslucentButton::pixels, TranslucentButton::pixels);
245 nextButton->setGeometry(screen.width() - TranslucentButton::pixels - 25, 0,
246 TranslucentButton::pixels, TranslucentButton::pixels);
247 #endif // Q_WS_MAEMO_5
248 previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
249 TranslucentButton::pixels, TranslucentButton::pixels);
254 fullScreenWindow->showFullScreen();
256 fullScreenWindow->activateWindow();
260 previousButton->flash();
263 void MainWindow::setCurrentBook(const QModelIndex ¤t)
266 Book *book = Library::instance()->book(current);
268 setWindowTitle(book? book->shortName(): tr("Dorian"));
271 void MainWindow::showLibrary()
273 (new LibraryDialog(this))->show();
276 void MainWindow::showSettings()
278 (new SettingsWindow(this))->show();
281 void MainWindow::showInfo()
283 if (mCurrent.isValid()) {
284 (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
289 void MainWindow::showDevTools()
291 (new DevTools())->exec();
294 void MainWindow::showBookmarks()
296 Book *book = Library::instance()->book(mCurrent);
298 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
299 bookmarks->setWindowModality(Qt::WindowModal);
300 connect(bookmarks, SIGNAL(addBookmark(const QString &)),
301 this, SLOT(onAddBookmark(const QString &)));
302 connect(bookmarks, SIGNAL(goToBookmark(int)),
303 this, SLOT(onGoToBookmark(int)));
308 void MainWindow::closeEvent(QCloseEvent *event)
311 view->setLastBookmark();
315 void MainWindow::onSettingsChanged(const QString &key)
317 #if defined(Q_WS_MAEMO_5)
318 if (key == "orientation") {
319 QString value = Settings::instance()->value(key,
320 Platform::instance()->defaultOrientation()).toString();
321 qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
322 if (value == "portrait") {
323 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
324 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
326 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
327 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
329 } else if (key == "lightson") {
330 bool enable = Settings::instance()->value(key, false).toBool();
331 qDebug() << "MainWindow::onSettingsChanged: lightson" << enable;
332 killTimer(preventBlankingTimer);
334 preventBlankingTimer = startTimer(29 * 1000);
339 #endif // Q_WS_MAEMO_5
342 void MainWindow::onPartLoadStart()
345 Platform::instance()->showBusy(this, true);
348 void MainWindow::onPartLoadEnd(int index)
351 bool enablePrevious = false;
352 bool enableNext = false;
353 Book *book = Library::instance()->book(mCurrent);
356 enablePrevious = true;
358 if (index < (book->parts.size() - 1)) {
362 Platform::instance()->showBusy(this, false);
365 void MainWindow::onAddBookmark(const QString ¬e)
368 view->addBookmark(note);
369 Platform::instance()->information(tr("Bookmarked current position"), this);
372 void MainWindow::onGoToBookmark(int index)
375 Book *book = Library::instance()->book(mCurrent);
376 view->goToBookmark(book->bookmarks()[index]);
379 void MainWindow::showChapters()
381 Book *book = Library::instance()->book(mCurrent);
383 ChaptersDialog *chapters = new ChaptersDialog(book, this);
384 chapters->setWindowModality(Qt::WindowModal);
385 connect(chapters, SIGNAL(goToChapter(int)),
386 this, SLOT(onGoToChapter(int)));
391 void MainWindow::onGoToChapter(int index)
395 Book *book = Library::instance()->book(mCurrent);
398 int partIndex = book->partFromChapter(index, fragment);
399 if (partIndex != -1) {
400 view->goToPart(partIndex, fragment);
405 void MainWindow::timerEvent(QTimerEvent *event)
407 if (event->timerId() == preventBlankingTimer) {
409 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
410 MCE_REQUEST_IF, QDBusConnection::systemBus());
411 mce.call(MCE_PREVENT_BLANK_REQ);
412 #endif // Q_WS_MAEMO_5
413 qDebug() << "MainWindow::timerEvent: Prevent display blanking";
415 AdopterWindow::timerEvent(event);
418 void MainWindow::resizeEvent(QResizeEvent *e)
421 progress->setGeometry(QRect(0, 0, e->size().width(), DORIAN_PROGRESS_HEIGHT));
422 #if defined(Q_WS_MAEMO_5)
423 previousButton->setGeometry(0,
424 e->size().height() - toolBar->height() - TranslucentButton::pixels,
425 TranslucentButton::pixels, TranslucentButton::pixels);
426 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
427 TranslucentButton::pixels, TranslucentButton::pixels);
428 #elif defined(Q_OS_SYMBIAN)
429 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
430 TranslucentButton::pixels, TranslucentButton::pixels);
431 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
432 0, TranslucentButton::pixels, TranslucentButton::pixels);
434 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
435 TranslucentButton::pixels, TranslucentButton::pixels);
436 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels - 25,
437 toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
438 #endif // Q_WS_MAEMO_5
439 qDebug() << "previousButton geometry" << previousButton->geometry();
442 // This is needed on Maemo, in order not to lose current reading position
443 // after orientation change
444 QTimer::singleShot(250, view, SLOT(restoreLastBookmark()));
446 previousButton->flash();
448 QMainWindow::resizeEvent(e);
451 void MainWindow::about()
453 Dyalog *aboutDialog = new Dyalog(this, false);
454 aboutDialog->setWindowTitle(tr("About Dorian"));
455 QString version = Platform::instance()->version();
456 QLabel *label = new QLabel(aboutDialog);
457 label->setTextFormat(Qt::RichText);
458 label->setOpenExternalLinks(true);
459 label->setWordWrap(true);
460 label->setText(tr("<b>Dorian %1</b><br><br>Copyright © 2010 "
461 "Akos Polster <akos@pipacs.com><br>"
462 "Licensed under GNU General Public License, Version 3<br>"
463 "Source code:<br><a href='https://garage.maemo.org/projects/dorian/'>"
464 "garage.maemo.org/projects/dorian</a>").arg(version));
465 aboutDialog->addWidget(label);
466 aboutDialog->addStretch();
470 void MainWindow::goToNextPage()
473 previousButton->flash();
477 void MainWindow::goToPreviousPage()
480 previousButton->flash();
481 view->goPreviousPage();
484 void MainWindow::onBeginUpgrade(int total)
486 libraryProgress->setVisible(total > 0);
487 libraryProgress->setWindowTitle(tr("Upgrading library"));
488 libraryProgress->setMaximum(total);
491 void MainWindow::onUpgrading(const QString &path)
493 libraryProgress->setLabelText(tr("Upgrading %1").
494 arg(QFileInfo(path).fileName()));
495 libraryProgress->setValue(libraryProgress->value() + 1);
498 void MainWindow::onEndUpgrade()
500 libraryProgress->hide();
501 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();