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 MainWindow::MainWindow(QWidget *parent):
38 AdopterWindow(parent), view(0), preventBlankingTimer(-1)
42 setAttribute(Qt::WA_Maemo5StackedWindow, true);
44 setWindowTitle("Dorian");
46 // Central widget. Must be an intermediate, because the book view widget
47 // can be re-parented later
48 QFrame *central = new QFrame(this);
49 QVBoxLayout *layout = new QVBoxLayout(central);
51 central->setLayout(layout);
52 setCentralWidget(central);
55 view = new BookView(central);
57 layout->addWidget(view);
60 progress = new Progress(this);
65 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
66 "view-fullscreen", tr("Full screen"));
69 chaptersAction = addToolBarAction(this, SLOT(showChapters()),
70 "chapters", tr("Chapters"));
71 bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
72 "bookmarks", tr("Bookmarks"));
73 infoAction = addToolBarAction(this, SLOT(showInfo()),
74 "info", tr("Book info"));
75 libraryAction = addToolBarAction(this, SLOT(showLibrary()),
76 "library", tr("Library"));
79 settingsAction = menuBar()->addAction(tr("Settings"));
80 connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
81 devToolsAction = menuBar()->addAction(tr("Developer"));
82 connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
83 QAction *aboutAction = menuBar()->addAction(tr("About"));
84 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
86 settingsAction = addToolBarAction(this, SLOT(showSettings()),
87 "preferences-system", tr("Settings"));
88 devToolsAction = addToolBarAction(this, SLOT(showDevTools()),
89 "developer", tr("Developer"));
90 addToolBarAction(this, SLOT(about()), "about", tr("About"));
91 #endif // Q_WS_MAEMO_5
95 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
96 "view-fullscreen", tr("Full screen"));
98 (void)addToolBarAction(this, SLOT(close()), "", tr("Exit"));
101 // Buttons on top of the book view
102 previousButton = new TranslucentButton("back", this);
103 nextButton = new TranslucentButton("forward", this);
105 // Handle model changes
106 connect(Library::instance(), SIGNAL(nowReadingChanged()),
107 this, SLOT(onCurrentBookChanged()));
109 // Load library, upgrade it if needed
110 libraryProgress = new ProgressDialog(tr("Upgrading library"), this);
111 Library *library = Library::instance();
112 connect(library, SIGNAL(beginUpgrade(int)), this, SLOT(onBeginUpgrade(int)));
113 connect(library, SIGNAL(upgrading(const QString &)),
114 this, SLOT(onUpgrading(const QString &)));
115 connect(library, SIGNAL(endUpgrade()), this, SLOT(onEndUpgrade()));
117 // Handle loading book parts
118 connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
119 connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
122 connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
124 // Shadow window for full screen reading
125 fullScreenWindow = new FullScreenWindow(this);
126 connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
128 // Handle settings changes
129 connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
130 this, SLOT(onSettingsChanged(const QString &)));
132 // Handle book view buttons
133 connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
134 connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
136 // Adopt view, show window
139 #ifdef DORIAN_TEST_MODEL
140 (void)new ModelTest(Library::instance(), this);
144 void MainWindow::initialize()
147 Library *library = Library::instance();
149 // Upgrade library if needed, then load it
153 // Load book on command line, or load last read book, or load default book
154 if (QCoreApplication::arguments().size() == 2) {
155 QString path = QCoreApplication::arguments()[1];
157 QModelIndex index = library->find(path);
158 if (index.isValid()) {
159 library->setNowReading(index);
162 QModelIndex index = library->nowReading();
163 if (index.isValid()) {
164 library->setNowReading(index);
166 if (!library->rowCount()) {
167 library->add(":/books/2BR02B.epub");
169 library->setNowReading(library->index(0));
174 void MainWindow::onCurrentBookChanged()
177 setCurrentBook(Library::instance()->nowReading());
180 void MainWindow::showRegular()
184 // Re-parent children
185 fullScreenWindow->leaveChildren();
186 QList<QWidget *> otherChildren;
187 otherChildren << progress << previousButton << nextButton;
188 takeChildren(view, otherChildren);
190 // Adjust geometry of decorations
192 QRect geo = geometry();
193 int y = geo.height() - progress->thickness();
194 #if defined(Q_WS_MAEMO_5)
195 y -= toolBar->height();
197 progress->setGeometry(0, y, geo.width(), y + progress->thickness());
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)
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 int y = screen.height() - progress->thickness();
242 progress->setGeometry(0, y, screen.width(), y + progress->thickness());
243 #if defined(Q_WS_MAEMO_5)
244 nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
245 TranslucentButton::pixels, TranslucentButton::pixels);
247 nextButton->setGeometry(screen.width() - TranslucentButton::pixels - 25, 0,
248 TranslucentButton::pixels, TranslucentButton::pixels);
249 #endif // Q_WS_MAEMO_5
250 previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
251 TranslucentButton::pixels, TranslucentButton::pixels);
256 fullScreenWindow->showFullScreen();
258 fullScreenWindow->activateWindow();
262 previousButton->flash();
265 void MainWindow::setCurrentBook(const QModelIndex ¤t)
268 Book *book = Library::instance()->book(current);
270 setWindowTitle(book? book->shortName(): tr("Dorian"));
273 void MainWindow::showLibrary()
275 (new LibraryDialog(this))->show();
278 void MainWindow::showSettings()
280 (new SettingsWindow(this))->show();
283 void MainWindow::showInfo()
285 if (mCurrent.isValid()) {
286 (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
291 void MainWindow::showDevTools()
293 (new DevTools())->exec();
296 void MainWindow::showBookmarks()
298 Book *book = Library::instance()->book(mCurrent);
300 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
301 bookmarks->setWindowModality(Qt::WindowModal);
302 connect(bookmarks, SIGNAL(addBookmark(const QString &)),
303 this, SLOT(onAddBookmark(const QString &)));
304 connect(bookmarks, SIGNAL(goToBookmark(int)),
305 this, SLOT(onGoToBookmark(int)));
310 void MainWindow::closeEvent(QCloseEvent *event)
313 view->setLastBookmark();
317 void MainWindow::onSettingsChanged(const QString &key)
319 #if defined(Q_WS_MAEMO_5)
320 if (key == "orientation") {
321 QString value = Settings::instance()->value(key,
322 Platform::instance()->defaultOrientation()).toString();
323 qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
324 if (value == "portrait") {
325 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
326 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
327 fullScreenWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,
329 fullScreenWindow->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
331 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
332 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
333 fullScreenWindow->setAttribute(Qt::WA_Maemo5PortraitOrientation,
335 fullScreenWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,
338 } else if (key == "lightson") {
339 bool enable = Settings::instance()->value(key, false).toBool();
340 qDebug() << "MainWindow::onSettingsChanged: lightson" << enable;
341 killTimer(preventBlankingTimer);
343 preventBlankingTimer = startTimer(29 * 1000);
348 #endif // Q_WS_MAEMO_5
351 void MainWindow::onPartLoadStart()
354 Platform::instance()->showBusy(this, true);
357 void MainWindow::onPartLoadEnd(int index)
360 bool enablePrevious = false;
361 bool enableNext = false;
362 Book *book = Library::instance()->book(mCurrent);
365 enablePrevious = true;
367 if (index < (book->parts.size() - 1)) {
371 Platform::instance()->showBusy(this, false);
374 void MainWindow::onAddBookmark(const QString ¬e)
377 view->addBookmark(note);
378 Platform::instance()->information(tr("Bookmarked current position"), this);
381 void MainWindow::onGoToBookmark(int index)
384 Book *book = Library::instance()->book(mCurrent);
385 view->goToBookmark(book->bookmarks()[index]);
388 void MainWindow::showChapters()
390 Book *book = Library::instance()->book(mCurrent);
392 ChaptersDialog *chapters = new ChaptersDialog(book, this);
393 chapters->setWindowModality(Qt::WindowModal);
394 connect(chapters, SIGNAL(goToChapter(int)),
395 this, SLOT(onGoToChapter(int)));
400 void MainWindow::onGoToChapter(int index)
404 Book *book = Library::instance()->book(mCurrent);
407 int partIndex = book->partFromChapter(index, fragment);
408 if (partIndex != -1) {
409 view->goToPart(partIndex, fragment);
414 void MainWindow::timerEvent(QTimerEvent *event)
416 if (event->timerId() == preventBlankingTimer) {
418 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
419 MCE_REQUEST_IF, QDBusConnection::systemBus());
420 mce.call(MCE_PREVENT_BLANK_REQ);
421 #endif // Q_WS_MAEMO_5
422 qDebug() << "MainWindow::timerEvent: Prevent display blanking";
424 AdopterWindow::timerEvent(event);
427 void MainWindow::resizeEvent(QResizeEvent *e)
431 if (hasChild(progress)) {
432 qDebug() << "To" << e->size();
433 int y = e->size().height() - progress->thickness();
434 #if defined(Q_WS_MAEMO_5)
435 y -= toolBar->height();
437 progress->setGeometry(0, y, e->size().width(), y + progress->thickness());
439 #if defined(Q_WS_MAEMO_5)
440 previousButton->setGeometry(0,
441 e->size().height() - toolBar->height() - TranslucentButton::pixels,
442 TranslucentButton::pixels, TranslucentButton::pixels);
443 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
444 TranslucentButton::pixels, TranslucentButton::pixels);
445 #elif defined(Q_OS_SYMBIAN)
446 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
447 TranslucentButton::pixels, TranslucentButton::pixels);
448 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
449 0, TranslucentButton::pixels, TranslucentButton::pixels);
451 previousButton->setGeometry(0,
452 e->size().height() - TranslucentButton::pixels,
453 TranslucentButton::pixels, TranslucentButton::pixels);
454 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels - 25,
455 toolBar->height(), TranslucentButton::pixels,
456 TranslucentButton::pixels);
457 #endif // Q_WS_MAEMO_5
460 // This is needed on Maemo, in order not to lose current reading position
461 // after orientation change
462 QTimer::singleShot(250, view, SLOT(restoreLastBookmark()));
464 previousButton->flash();
467 QMainWindow::resizeEvent(e);
470 void MainWindow::about()
472 Dyalog *aboutDialog = new Dyalog(this, false);
473 aboutDialog->setWindowTitle(tr("About Dorian"));
474 QString version = Platform::instance()->version();
475 QLabel *label = new QLabel(aboutDialog);
476 label->setTextFormat(Qt::RichText);
477 label->setOpenExternalLinks(true);
478 label->setWordWrap(true);
479 label->setText(tr("<b>Dorian %1</b><br><br>Copyright © 2010 "
480 "Akos Polster <akos@pipacs.com><br>"
481 "Licensed under GNU General Public License, Version 3<br>"
482 "Source code:<br><a href='https://garage.maemo.org/projects/dorian/'>"
483 "garage.maemo.org/projects/dorian</a>").arg(version));
484 aboutDialog->addWidget(label);
485 aboutDialog->addStretch();
489 void MainWindow::goToNextPage()
492 previousButton->flash();
496 void MainWindow::goToPreviousPage()
499 previousButton->flash();
500 view->goPreviousPage();
503 void MainWindow::onBeginUpgrade(int total)
505 libraryProgress->setVisible(total > 0);
506 libraryProgress->setWindowTitle(tr("Upgrading library"));
507 libraryProgress->setMaximum(total);
510 void MainWindow::onUpgrading(const QString &path)
512 libraryProgress->setLabelText(tr("Upgrading %1").
513 arg(QFileInfo(path).fileName()));
514 libraryProgress->setValue(libraryProgress->value() + 1);
517 void MainWindow::onEndUpgrade()
519 libraryProgress->hide();
520 libraryProgress->reset();
523 void MainWindow::onBeginLoad(int total)
525 libraryProgress->setVisible(total > 0);
526 libraryProgress->setWindowTitle(tr("Loading library"));
527 libraryProgress->setMaximum(total);
530 void MainWindow::onLoading(const QString &path)
532 libraryProgress->setLabelText(tr("Loading %1").
533 arg(QFileInfo(path).fileName()));
534 libraryProgress->setValue(libraryProgress->value() + 1);
537 void MainWindow::onEndLoad()
539 libraryProgress->hide();
540 libraryProgress->reset();