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 connect(library, SIGNAL(beginLoad(int)), this, SLOT(onBeginLoad(int)));
123 connect(library, SIGNAL(loading(const QString &)),
124 this, SLOT(onLoading(const QString &)));
125 connect(library, SIGNAL(endLoad()), this, SLOT(onEndLoad()));
130 // Load book on command line, or load last read book, or load default book
131 if (QCoreApplication::arguments().size() == 2) {
132 QString path = QCoreApplication::arguments()[1];
134 QModelIndex index = library->find(path);
135 if (index.isValid()) {
136 library->setNowReading(index);
139 QModelIndex index = library->nowReading();
140 if (index.isValid()) {
141 library->setNowReading(index);
143 if (!library->rowCount()) {
144 library->add(":/books/2BR02B.epub");
146 library->setNowReading(library->index(0));
150 // Handle loading book parts
151 connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
152 connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
155 connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
157 // Shadow window for full screen reading
158 fullScreenWindow = new FullScreenWindow(this);
159 connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
161 // Handle settings changes
162 Settings *settings = Settings::instance();
163 connect(settings, SIGNAL(valueChanged(const QString &)),
164 this, SLOT(onSettingsChanged(const QString &)));
165 settings->setValue("orientation", settings->value("orientation"));
166 settings->setValue("lightson", settings->value("lightson"));
168 // Handle book view buttons
169 connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
170 connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
172 // Adopt view, show window
175 #ifdef DORIAN_TEST_MODEL
176 (void)new ModelTest(Library::instance(), this);
180 void MainWindow::onCurrentBookChanged()
183 setCurrentBook(Library::instance()->nowReading());
186 void MainWindow::showRegular()
190 // Re-parent children
191 fullScreenWindow->leaveChildren();
192 QList<QWidget *> otherChildren;
193 otherChildren << progress << previousButton << nextButton;
194 takeChildren(view, otherChildren);
196 // Adjust geometry of decorations
197 QRect geo = geometry();
198 progress->setGeometry(0, 0, geo.width(), DORIAN_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,
215 TranslucentButton::pixels);
216 #endif // Q_WS_MAEMO_5
217 qDebug() << "previousButton geometry" << previousButton->geometry();
219 fullScreenWindow->hide();
221 #if defined(Q_OS_SYMBIAN)
223 #elif defined(Q_WS_MAEMO_5)
224 // FIXME: This is ugly.
225 // view->restoreLastBookmark();
229 previousButton->flash();
232 void MainWindow::showBig()
236 // Re-parent children
238 QList<QWidget *> otherChildren;
239 otherChildren << progress << nextButton << previousButton;
240 fullScreenWindow->takeChildren(view, otherChildren);
242 // Adjust geometry of decorations
243 QRect screen = QApplication::desktop()->screenGeometry();
244 progress->setGeometry(0, 0, screen.width(), DORIAN_PROGRESS_HEIGHT);
245 #if defined(Q_WS_MAEMO_5)
246 nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
247 TranslucentButton::pixels, TranslucentButton::pixels);
249 nextButton->setGeometry(screen.width() - TranslucentButton::pixels - 25, 0,
250 TranslucentButton::pixels, TranslucentButton::pixels);
251 #endif // Q_WS_MAEMO_5
252 previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
253 TranslucentButton::pixels, TranslucentButton::pixels);
258 fullScreenWindow->showFullScreen();
260 fullScreenWindow->activateWindow();
264 previousButton->flash();
267 void MainWindow::setCurrentBook(const QModelIndex ¤t)
270 Book *book = Library::instance()->book(current);
272 setWindowTitle(book? book->shortName(): tr("Dorian"));
275 void MainWindow::showLibrary()
277 (new LibraryDialog(this))->show();
280 void MainWindow::showSettings()
282 (new SettingsWindow(this))->show();
285 void MainWindow::showInfo()
287 if (mCurrent.isValid()) {
288 (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
293 void MainWindow::showDevTools()
295 (new DevTools())->exec();
298 void MainWindow::showBookmarks()
300 Book *book = Library::instance()->book(mCurrent);
302 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
303 bookmarks->setWindowModality(Qt::WindowModal);
304 connect(bookmarks, SIGNAL(addBookmark(const QString &)),
305 this, SLOT(onAddBookmark(const QString &)));
306 connect(bookmarks, SIGNAL(goToBookmark(int)),
307 this, SLOT(onGoToBookmark(int)));
312 void MainWindow::closeEvent(QCloseEvent *event)
315 view->setLastBookmark();
319 void MainWindow::onSettingsChanged(const QString &key)
323 #if defined(Q_WS_MAEMO_5)
324 if (key == "orientation") {
325 QString value = Settings::instance()->value(key).toString();
326 if (value == "portrait") {
327 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
328 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
330 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
331 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
333 } else if (key == "lightson") {
334 bool enable = Settings::instance()->value(key, false).toBool();
335 killTimer(preventBlankingTimer);
337 preventBlankingTimer = startTimer(29 * 1000);
340 #endif // Q_WS_MAEMO_5
343 void MainWindow::onPartLoadStart()
346 Platform::instance()->showBusy(this, true);
349 void MainWindow::onPartLoadEnd(int index)
352 bool enablePrevious = false;
353 bool enableNext = false;
354 Book *book = Library::instance()->book(mCurrent);
357 enablePrevious = true;
359 if (index < (book->parts.size() - 1)) {
363 Platform::instance()->showBusy(this, false);
366 void MainWindow::onAddBookmark(const QString ¬e)
369 view->addBookmark(note);
370 Platform::instance()->information(tr("Bookmarked current position"), this);
373 void MainWindow::onGoToBookmark(int index)
376 Book *book = Library::instance()->book(mCurrent);
377 view->goToBookmark(book->bookmarks()[index]);
380 void MainWindow::showChapters()
382 Book *book = Library::instance()->book(mCurrent);
384 ChaptersDialog *chapters = new ChaptersDialog(book, this);
385 chapters->setWindowModality(Qt::WindowModal);
386 connect(chapters, SIGNAL(goToChapter(int)),
387 this, SLOT(onGoToChapter(int)));
392 void MainWindow::onGoToChapter(int index)
396 Book *book = Library::instance()->book(mCurrent);
399 int partIndex = book->partFromChapter(index, fragment);
400 if (partIndex != -1) {
401 view->goToPart(partIndex, fragment);
406 void MainWindow::timerEvent(QTimerEvent *event)
408 if (event->timerId() == preventBlankingTimer) {
410 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
411 MCE_REQUEST_IF, QDBusConnection::systemBus());
412 mce.call(MCE_PREVENT_BLANK_REQ);
413 #endif // Q_WS_MAEMO_5
414 qDebug() << "MainWindow::timerEvent: Prevent display blanking";
416 AdopterWindow::timerEvent(event);
419 void MainWindow::resizeEvent(QResizeEvent *e)
422 progress->setGeometry(QRect(0, 0, e->size().width(), DORIAN_PROGRESS_HEIGHT));
423 #if defined(Q_WS_MAEMO_5)
424 previousButton->setGeometry(0,
425 e->size().height() - toolBar->height() - TranslucentButton::pixels,
426 TranslucentButton::pixels, TranslucentButton::pixels);
427 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
428 TranslucentButton::pixels, TranslucentButton::pixels);
429 #elif defined(Q_OS_SYMBIAN)
430 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
431 TranslucentButton::pixels, TranslucentButton::pixels);
432 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
433 0, TranslucentButton::pixels, TranslucentButton::pixels);
435 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
436 TranslucentButton::pixels, TranslucentButton::pixels);
437 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels - 25,
438 toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
439 #endif // Q_WS_MAEMO_5
440 qDebug() << "previousButton geometry" << previousButton->geometry();
441 previousButton->flash();
443 QMainWindow::resizeEvent(e);
446 void MainWindow::about()
448 Dyalog *aboutDialog = new Dyalog(this, false);
449 aboutDialog->setWindowTitle(tr("About Dorian"));
450 QString version = Platform::instance()->version();
451 QLabel *label = new QLabel(aboutDialog);
452 label->setTextFormat(Qt::RichText);
453 label->setOpenExternalLinks(true);
454 label->setWordWrap(true);
455 label->setText(tr("<b>Dorian %1</b><br><br>Copyright © 2010 "
456 "Akos Polster <akos@pipacs.com><br>"
457 "Licensed under GNU General Public License, Version 3<br>"
458 "Source code:<br><a href='https://garage.maemo.org/projects/dorian/'>"
459 "garage.maemo.org/projects/dorian</a>").arg(version));
460 aboutDialog->addWidget(label);
461 aboutDialog->addStretch();
465 void MainWindow::goToNextPage()
468 previousButton->flash();
472 void MainWindow::goToPreviousPage()
475 previousButton->flash();
476 view->goPreviousPage();
479 void MainWindow::onBeginUpgrade(int total)
481 libraryProgress->setVisible(total > 0);
482 libraryProgress->setWindowTitle(tr("Upgrading library"));
483 libraryProgress->setMaximum(total);
486 void MainWindow::onUpgrading(const QString &path)
488 libraryProgress->setLabelText(tr("Upgrading %1").
489 arg(QFileInfo(path).fileName()));
490 libraryProgress->setValue(libraryProgress->value() + 1);
493 void MainWindow::onEndUpgrade()
495 libraryProgress->hide();
496 libraryProgress->reset();
499 void MainWindow::onBeginLoad(int total)
501 libraryProgress->setVisible(total > 0);
502 libraryProgress->setWindowTitle(tr("Loading library"));
503 libraryProgress->setMaximum(total);
506 void MainWindow::onLoading(const QString &path)
508 libraryProgress->setLabelText(tr("Loading %1").
509 arg(QFileInfo(path).fileName()));
510 libraryProgress->setValue(libraryProgress->value() + 1);
513 void MainWindow::onEndLoad()
515 libraryProgress->hide();
516 libraryProgress->reset();