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()));
121 connect(library, SIGNAL(beginLoad(int)), this, SLOT(onBeginLoad(int)));
122 connect(library, SIGNAL(loading(const QString &)),
123 this, SLOT(onLoading(const QString &)));
124 connect(library, SIGNAL(endLoad()), this, SLOT(onEndLoad()));
128 // Load book on command line, or load last read book, or load default book
129 if (QCoreApplication::arguments().size() == 2) {
130 QString path = QCoreApplication::arguments()[1];
132 QModelIndex index = library->find(path);
133 if (index.isValid()) {
134 library->setNowReading(index);
137 QModelIndex index = library->nowReading();
138 if (index.isValid()) {
139 library->setNowReading(index);
141 if (!library->rowCount()) {
142 library->add(":/books/2BR02B.epub");
144 library->setNowReading(library->index(0));
148 // Handle loading book parts
149 connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
150 connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
153 connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
155 // Shadow window for full screen reading
156 fullScreenWindow = new FullScreenWindow(this);
157 connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
159 // Handle settings changes
160 Settings *settings = Settings::instance();
161 connect(settings, SIGNAL(valueChanged(const QString &)),
162 this, SLOT(onSettingsChanged(const QString &)));
163 settings->setValue("orientation", settings->value("orientation"));
164 settings->setValue("lightson", settings->value("lightson"));
166 // Handle book view buttons
167 connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
168 connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
170 // Adopt view, show window
173 #ifdef DORIAN_TEST_MODEL
174 (void)new ModelTest(Library::instance(), this);
178 void MainWindow::onCurrentBookChanged()
180 setCurrentBook(Library::instance()->nowReading());
183 void MainWindow::showRegular()
187 // Re-parent children
188 fullScreenWindow->leaveChildren();
189 QList<QWidget *> otherChildren;
190 otherChildren << progress << previousButton << nextButton;
191 takeChildren(view, otherChildren);
193 // Adjust geometry of decorations
194 QRect geo = geometry();
195 progress->setGeometry(0, 0, geo.width(), DORIAN_PROGRESS_HEIGHT);
196 #if defined(Q_WS_MAEMO_5)
197 previousButton->setGeometry(0,
198 geo.height() - toolBar->height() - TranslucentButton::pixels,
199 TranslucentButton::pixels, TranslucentButton::pixels);
200 nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
201 TranslucentButton::pixels, TranslucentButton::pixels);
202 #elif defined(Q_OS_SYMBIAN)
203 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
204 TranslucentButton::pixels, TranslucentButton::pixels);
205 nextButton->setGeometry(geo.width() - TranslucentButton::pixels,
206 0, TranslucentButton::pixels, TranslucentButton::pixels);
208 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
209 TranslucentButton::pixels, TranslucentButton::pixels);
210 nextButton->setGeometry(geo.width() - TranslucentButton::pixels - 25,
211 toolBar->height(), TranslucentButton::pixels,
212 TranslucentButton::pixels);
213 #endif // Q_WS_MAEMO_5
214 qDebug() << "previousButton geometry" << previousButton->geometry();
216 fullScreenWindow->hide();
218 #if defined(Q_OS_SYMBIAN)
220 #elif defined(Q_WS_MAEMO_5)
221 // FIXME: This is ugly.
222 view->restoreLastBookmark();
226 previousButton->flash();
229 void MainWindow::showBig()
233 // Re-parent children
235 QList<QWidget *> otherChildren;
236 otherChildren << progress << nextButton << previousButton;
237 fullScreenWindow->takeChildren(view, otherChildren);
239 // Adjust geometry of decorations
240 QRect screen = QApplication::desktop()->screenGeometry();
241 progress->setGeometry(0, 0, screen.width(), DORIAN_PROGRESS_HEIGHT);
242 #if defined(Q_WS_MAEMO_5)
243 nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
244 TranslucentButton::pixels, TranslucentButton::pixels);
246 nextButton->setGeometry(screen.width() - TranslucentButton::pixels - 25, 0,
247 TranslucentButton::pixels, TranslucentButton::pixels);
248 #endif // Q_WS_MAEMO_5
249 previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
250 TranslucentButton::pixels, TranslucentButton::pixels);
255 fullScreenWindow->showFullScreen();
257 fullScreenWindow->activateWindow();
261 previousButton->flash();
264 void MainWindow::setCurrentBook(const QModelIndex ¤t)
267 Book *book = Library::instance()->book(current);
269 setWindowTitle(book? book->shortName(): tr("Dorian"));
272 void MainWindow::showLibrary()
274 (new LibraryDialog(this))->show();
277 void MainWindow::showSettings()
279 (new SettingsWindow(this))->show();
282 void MainWindow::showInfo()
284 if (mCurrent.isValid()) {
285 (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
290 void MainWindow::showDevTools()
292 (new DevTools())->exec();
295 void MainWindow::showBookmarks()
297 Book *book = Library::instance()->book(mCurrent);
299 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
300 bookmarks->setWindowModality(Qt::WindowModal);
301 connect(bookmarks, SIGNAL(addBookmark(const QString &)),
302 this, SLOT(onAddBookmark(const QString &)));
303 connect(bookmarks, SIGNAL(goToBookmark(int)),
304 this, SLOT(onGoToBookmark(int)));
309 void MainWindow::closeEvent(QCloseEvent *event)
312 view->setLastBookmark();
316 void MainWindow::onSettingsChanged(const QString &key)
320 #if defined(Q_WS_MAEMO_5)
321 if (key == "orientation") {
322 QString value = Settings::instance()->value(key).toString();
323 if (value == "portrait") {
324 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
325 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
327 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
328 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
330 } else if (key == "lightson") {
331 bool enable = Settings::instance()->value(key, false).toBool();
332 killTimer(preventBlankingTimer);
334 preventBlankingTimer = startTimer(29 * 1000);
337 #endif // Q_WS_MAEMO_5
340 void MainWindow::onPartLoadStart()
343 Platform::showBusy(this, true);
346 void MainWindow::onPartLoadEnd(int index)
349 bool enablePrevious = false;
350 bool enableNext = false;
351 Book *book = Library::instance()->book(mCurrent);
354 enablePrevious = true;
356 if (index < (book->parts.size() - 1)) {
360 Platform::showBusy(this, false);
363 void MainWindow::onAddBookmark(const QString ¬e)
366 view->addBookmark(note);
367 Platform::information(tr("Bookmarked current position"), this);
370 void MainWindow::onGoToBookmark(int index)
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)
393 Book *book = Library::instance()->book(mCurrent);
396 int partIndex = book->partFromChapter(index, fragment);
397 if (partIndex != -1) {
398 view->goToPart(partIndex, fragment);
403 void MainWindow::timerEvent(QTimerEvent *event)
405 if (event->timerId() == preventBlankingTimer) {
407 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
408 MCE_REQUEST_IF, QDBusConnection::systemBus());
409 mce.call(MCE_PREVENT_BLANK_REQ);
410 #endif // Q_WS_MAEMO_5
411 qDebug() << "MainWindow::timerEvent: Prevent display blanking";
413 AdopterWindow::timerEvent(event);
416 void MainWindow::resizeEvent(QResizeEvent *e)
419 progress->setGeometry(QRect(0, 0, e->size().width(), DORIAN_PROGRESS_HEIGHT));
420 #if defined(Q_WS_MAEMO_5)
421 previousButton->setGeometry(0,
422 e->size().height() - toolBar->height() - TranslucentButton::pixels,
423 TranslucentButton::pixels, TranslucentButton::pixels);
424 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
425 TranslucentButton::pixels, TranslucentButton::pixels);
426 #elif defined(Q_OS_SYMBIAN)
427 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
428 TranslucentButton::pixels, TranslucentButton::pixels);
429 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
430 0, TranslucentButton::pixels, TranslucentButton::pixels);
432 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
433 TranslucentButton::pixels, TranslucentButton::pixels);
434 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels - 25,
435 toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
436 #endif // Q_WS_MAEMO_5
437 qDebug() << "previousButton geometry" << previousButton->geometry();
438 previousButton->flash();
440 QMainWindow::resizeEvent(e);
443 void MainWindow::about()
445 Dyalog *aboutDialog = new Dyalog(this, false);
446 aboutDialog->setWindowTitle(tr("About Dorian"));
447 QLabel *label = new QLabel(aboutDialog);
448 label->setTextFormat(Qt::RichText);
449 label->setOpenExternalLinks(true);
450 label->setWordWrap(true);
451 label->setText(tr("<b>Dorian %1</b><br><br>Copyright © 2010 "
452 "Akos Polster <akos@pipacs.com><br>"
453 "Licensed under GNU General Public License, Version 3<br>"
454 "Source code:<br><a href='https://garage.maemo.org/projects/dorian/'>"
455 "garage.maemo.org/projects/dorian</a>").arg(Platform::version()));
456 aboutDialog->addWidget(label);
457 aboutDialog->addStretch();
461 void MainWindow::goToNextPage()
464 previousButton->flash();
468 void MainWindow::goToPreviousPage()
471 previousButton->flash();
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();
495 void MainWindow::onBeginLoad(int total)
497 libraryProgress->setVisible(total > 0);
498 libraryProgress->setWindowTitle(tr("Loading library"));
499 libraryProgress->setMaximum(total);
502 void MainWindow::onLoading(const QString &path)
504 libraryProgress->setLabelText(tr("Loading %1").
505 arg(QFileInfo(path).fileName()));
506 libraryProgress->setValue(libraryProgress->value() + 1);
509 void MainWindow::onEndLoad()
511 libraryProgress->hide();
512 libraryProgress->reset();