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);
67 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
68 "view-fullscreen", tr("Full screen"));
71 chaptersAction = addToolBarAction(this, SLOT(showChapters()),
72 "chapters", tr("Chapters"));
73 bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
74 "bookmarks", tr("Bookmarks"));
75 infoAction = addToolBarAction(this, SLOT(showInfo()),
76 "info", tr("Book info"));
77 libraryAction = addToolBarAction(this, SLOT(showLibrary()),
78 "library", tr("Library"));
81 settingsAction = menuBar()->addAction(tr("Settings"));
82 connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
83 devToolsAction = menuBar()->addAction(tr("Developer"));
84 connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
85 QAction *aboutAction = menuBar()->addAction(tr("About"));
86 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
88 settingsAction = addToolBarAction(this, SLOT(showSettings()),
89 "preferences-system", tr("Settings"));
90 devToolsAction = addToolBarAction(this, SLOT(showDevTools()),
91 "developer", tr("Developer"));
92 addToolBarAction(this, SLOT(about()), "about", tr("About"));
93 #endif // Q_WS_MAEMO_5
97 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
98 "view-fullscreen", tr("Full screen"));
100 (void)addToolBarAction(this, SLOT(close()), "", tr("Exit"));
103 // Buttons on top of the book view
104 previousButton = new TranslucentButton("back", this);
105 nextButton = new TranslucentButton("forward", this);
107 // Handle model changes
108 connect(Library::instance(), SIGNAL(nowReadingChanged()),
109 this, SLOT(onCurrentBookChanged()));
111 // Load library, upgrade it if needed
112 libraryProgress = new ProgressDialog(tr("Upgrading library"), this);
113 Library *library = Library::instance();
114 connect(library, SIGNAL(beginUpgrade(int)), this, SLOT(onBeginUpgrade(int)));
115 connect(library, SIGNAL(upgrading(const QString &)),
116 this, SLOT(onUpgrading(const QString &)));
117 connect(library, SIGNAL(endUpgrade()), this, SLOT(onEndUpgrade()));
119 // Handle loading book parts
120 connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
121 connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
124 connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
126 // Shadow window for full screen reading
127 fullScreenWindow = new FullScreenWindow(this);
128 connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
130 // Handle settings changes
131 connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
132 this, SLOT(onSettingsChanged(const QString &)));
134 // Handle book view buttons
135 connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
136 connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
138 // Adopt view, show window
141 #ifdef DORIAN_TEST_MODEL
142 (void)new ModelTest(Library::instance(), this);
146 void MainWindow::initialize()
149 Library *library = Library::instance();
151 // Upgrade library if needed, then load it
155 // Load book on command line, or load last read book, or load default book
156 if (QCoreApplication::arguments().size() == 2) {
157 QString path = QCoreApplication::arguments()[1];
159 QModelIndex index = library->find(path);
160 if (index.isValid()) {
161 library->setNowReading(index);
164 QModelIndex index = library->nowReading();
165 if (index.isValid()) {
166 library->setNowReading(index);
168 if (!library->rowCount()) {
169 library->add(":/books/2BR02B.epub");
171 library->setNowReading(library->index(0));
176 void MainWindow::onCurrentBookChanged()
179 setCurrentBook(Library::instance()->nowReading());
182 void MainWindow::showRegular()
186 // Re-parent children
187 fullScreenWindow->leaveChildren();
188 QList<QWidget *> otherChildren;
189 otherChildren << progress << previousButton << nextButton;
190 takeChildren(view, otherChildren);
192 // Adjust geometry of decorations
193 QRect geo = geometry();
194 progress->setGeometry(0, 0, geo.width(), DORIAN_PROGRESS_HEIGHT);
195 #if defined(Q_WS_MAEMO_5)
196 previousButton->setGeometry(0,
197 geo.height() - toolBar->height() - TranslucentButton::pixels,
198 TranslucentButton::pixels, TranslucentButton::pixels);
199 nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
200 TranslucentButton::pixels, TranslucentButton::pixels);
201 #elif defined(Q_OS_SYMBIAN)
202 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
203 TranslucentButton::pixels, TranslucentButton::pixels);
204 nextButton->setGeometry(geo.width() - TranslucentButton::pixels,
205 0, TranslucentButton::pixels, TranslucentButton::pixels);
207 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
208 TranslucentButton::pixels, TranslucentButton::pixels);
209 nextButton->setGeometry(geo.width() - TranslucentButton::pixels - 25,
210 toolBar->height(), TranslucentButton::pixels,
211 TranslucentButton::pixels);
212 #endif // Q_WS_MAEMO_5
213 qDebug() << "previousButton geometry" << previousButton->geometry();
215 fullScreenWindow->hide();
217 #if defined(Q_OS_SYMBIAN)
222 previousButton->flash();
225 void MainWindow::showBig()
229 // Re-parent children
231 QList<QWidget *> otherChildren;
232 otherChildren << progress << nextButton << previousButton;
233 fullScreenWindow->takeChildren(view, otherChildren);
235 // Adjust geometry of decorations
236 QRect screen = QApplication::desktop()->screenGeometry();
237 progress->setGeometry(0, 0, screen.width(), DORIAN_PROGRESS_HEIGHT);
238 #if defined(Q_WS_MAEMO_5)
239 nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
240 TranslucentButton::pixels, TranslucentButton::pixels);
242 nextButton->setGeometry(screen.width() - TranslucentButton::pixels - 25, 0,
243 TranslucentButton::pixels, TranslucentButton::pixels);
244 #endif // Q_WS_MAEMO_5
245 previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
246 TranslucentButton::pixels, TranslucentButton::pixels);
251 fullScreenWindow->showFullScreen();
253 fullScreenWindow->activateWindow();
257 previousButton->flash();
260 void MainWindow::setCurrentBook(const QModelIndex ¤t)
263 Book *book = Library::instance()->book(current);
265 setWindowTitle(book? book->shortName(): tr("Dorian"));
268 void MainWindow::showLibrary()
270 (new LibraryDialog(this))->show();
273 void MainWindow::showSettings()
275 (new SettingsWindow(this))->show();
278 void MainWindow::showInfo()
280 if (mCurrent.isValid()) {
281 (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
286 void MainWindow::showDevTools()
288 (new DevTools())->exec();
291 void MainWindow::showBookmarks()
293 Book *book = Library::instance()->book(mCurrent);
295 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
296 bookmarks->setWindowModality(Qt::WindowModal);
297 connect(bookmarks, SIGNAL(addBookmark(const QString &)),
298 this, SLOT(onAddBookmark(const QString &)));
299 connect(bookmarks, SIGNAL(goToBookmark(int)),
300 this, SLOT(onGoToBookmark(int)));
305 void MainWindow::closeEvent(QCloseEvent *event)
308 view->setLastBookmark();
312 void MainWindow::onSettingsChanged(const QString &key)
314 #if defined(Q_WS_MAEMO_5)
315 if (key == "orientation") {
316 QString value = Settings::instance()->value(key,
317 Platform::instance()->defaultOrientation()).toString();
318 qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
319 if (value == "portrait") {
320 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
321 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
323 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
324 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
326 } else if (key == "lightson") {
327 bool enable = Settings::instance()->value(key, false).toBool();
328 qDebug() << "MainWindow::onSettingsChanged: lightson" << enable;
329 killTimer(preventBlankingTimer);
331 preventBlankingTimer = startTimer(29 * 1000);
336 #endif // Q_WS_MAEMO_5
339 void MainWindow::onPartLoadStart()
342 Platform::instance()->showBusy(this, true);
345 void MainWindow::onPartLoadEnd(int index)
348 bool enablePrevious = false;
349 bool enableNext = false;
350 Book *book = Library::instance()->book(mCurrent);
353 enablePrevious = true;
355 if (index < (book->parts.size() - 1)) {
359 Platform::instance()->showBusy(this, false);
362 void MainWindow::onAddBookmark(const QString ¬e)
365 view->addBookmark(note);
366 Platform::instance()->information(tr("Bookmarked current position"), this);
369 void MainWindow::onGoToBookmark(int index)
372 Book *book = Library::instance()->book(mCurrent);
373 view->goToBookmark(book->bookmarks()[index]);
376 void MainWindow::showChapters()
378 Book *book = Library::instance()->book(mCurrent);
380 ChaptersDialog *chapters = new ChaptersDialog(book, this);
381 chapters->setWindowModality(Qt::WindowModal);
382 connect(chapters, SIGNAL(goToChapter(int)),
383 this, SLOT(onGoToChapter(int)));
388 void MainWindow::onGoToChapter(int index)
392 Book *book = Library::instance()->book(mCurrent);
395 int partIndex = book->partFromChapter(index, fragment);
396 if (partIndex != -1) {
397 view->goToPart(partIndex, fragment);
402 void MainWindow::timerEvent(QTimerEvent *event)
404 if (event->timerId() == preventBlankingTimer) {
406 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
407 MCE_REQUEST_IF, QDBusConnection::systemBus());
408 mce.call(MCE_PREVENT_BLANK_REQ);
409 #endif // Q_WS_MAEMO_5
410 qDebug() << "MainWindow::timerEvent: Prevent display blanking";
412 AdopterWindow::timerEvent(event);
415 void MainWindow::resizeEvent(QResizeEvent *e)
418 progress->setGeometry(QRect(0, 0, e->size().width(), DORIAN_PROGRESS_HEIGHT));
419 #if defined(Q_WS_MAEMO_5)
420 previousButton->setGeometry(0,
421 e->size().height() - toolBar->height() - TranslucentButton::pixels,
422 TranslucentButton::pixels, TranslucentButton::pixels);
423 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
424 TranslucentButton::pixels, TranslucentButton::pixels);
425 #elif defined(Q_OS_SYMBIAN)
426 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
427 TranslucentButton::pixels, TranslucentButton::pixels);
428 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
429 0, TranslucentButton::pixels, TranslucentButton::pixels);
431 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
432 TranslucentButton::pixels, TranslucentButton::pixels);
433 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels - 25,
434 toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
435 #endif // Q_WS_MAEMO_5
436 qDebug() << "previousButton geometry" << previousButton->geometry();
439 // This is needed on Maemo, in order not to lose current reading position
440 // after orientation change
441 QTimer::singleShot(250, view, SLOT(restoreLastBookmark()));
443 previousButton->flash();
445 QMainWindow::resizeEvent(e);
448 void MainWindow::about()
450 Dyalog *aboutDialog = new Dyalog(this, false);
451 aboutDialog->setWindowTitle(tr("About Dorian"));
452 QString version = Platform::instance()->version();
453 QLabel *label = new QLabel(aboutDialog);
454 label->setTextFormat(Qt::RichText);
455 label->setOpenExternalLinks(true);
456 label->setWordWrap(true);
457 label->setText(tr("<b>Dorian %1</b><br><br>Copyright © 2010 "
458 "Akos Polster <akos@pipacs.com><br>"
459 "Licensed under GNU General Public License, Version 3<br>"
460 "Source code:<br><a href='https://garage.maemo.org/projects/dorian/'>"
461 "garage.maemo.org/projects/dorian</a>").arg(version));
462 aboutDialog->addWidget(label);
463 aboutDialog->addStretch();
467 void MainWindow::goToNextPage()
470 previousButton->flash();
474 void MainWindow::goToPreviousPage()
477 previousButton->flash();
478 view->goPreviousPage();
481 void MainWindow::onBeginUpgrade(int total)
483 libraryProgress->setVisible(total > 0);
484 libraryProgress->setWindowTitle(tr("Upgrading library"));
485 libraryProgress->setMaximum(total);
488 void MainWindow::onUpgrading(const QString &path)
490 libraryProgress->setLabelText(tr("Upgrading %1").
491 arg(QFileInfo(path).fileName()));
492 libraryProgress->setValue(libraryProgress->value() + 1);
495 void MainWindow::onEndUpgrade()
497 libraryProgress->hide();
498 libraryProgress->reset();
501 void MainWindow::onBeginLoad(int total)
503 libraryProgress->setVisible(total > 0);
504 libraryProgress->setWindowTitle(tr("Loading library"));
505 libraryProgress->setMaximum(total);
508 void MainWindow::onLoading(const QString &path)
510 libraryProgress->setLabelText(tr("Loading %1").
511 arg(QFileInfo(path).fileName()));
512 libraryProgress->setValue(libraryProgress->value() + 1);
515 void MainWindow::onEndLoad()
517 libraryProgress->hide();
518 libraryProgress->reset();