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"
32 #include "sortedlibrary.h"
34 #ifdef DORIAN_TEST_MODEL
35 # include "modeltest.h"
38 MainWindow::MainWindow(QWidget *parent):
39 AdopterWindow(parent), view(0), preventBlankingTimer(-1)
43 setAttribute(Qt::WA_Maemo5StackedWindow, true);
45 setWindowTitle("Dorian");
49 toolBar = new QToolBar("", this /*frame*/);
50 toolBar->setFixedWidth(QApplication::desktop()->
51 availableGeometry().width());
52 toolBar->setFixedHeight(65);
53 toolBar->setStyleSheet("margin:0;border:0;padding:0");
54 toolBar->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Maximum);
55 addToolBar(Qt::BottomToolBarArea, toolBar);
58 // Central widget. Must be an intermediate, because the book view widget
59 // can be re-parented later
60 QFrame *central = new QFrame(this);
61 QVBoxLayout *layout = new QVBoxLayout(central);
63 central->setLayout(layout);
64 setCentralWidget(central);
67 view = new BookView(central);
69 layout->addWidget(view);
72 progress = new Progress(this);
77 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
78 "view-fullscreen", tr("Full screen"));
81 chaptersAction = addToolBarAction(this, SLOT(showChapters()),
82 "chapters", tr("Chapters"), true);
83 bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
84 "bookmarks", tr("Bookmarks"), true);
85 infoAction = addToolBarAction(this, SLOT(showInfo()),
86 "info", tr("Book info"), true);
87 libraryAction = addToolBarAction(this, SLOT(showLibrary()),
88 "library", tr("Library"), true);
91 settingsAction = menuBar()->addAction(tr("Settings"));
92 connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
93 devToolsAction = menuBar()->addAction(tr("Developer"));
94 connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
95 QAction *aboutAction = menuBar()->addAction(tr("About"));
96 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
98 settingsAction = addToolBarAction(this, SLOT(showSettings()),
99 "preferences-system", tr("Settings"));
100 devToolsAction = addToolBarAction(this, SLOT(showDevTools()),
101 "developer", tr("Developer"));
102 addToolBarAction(this, SLOT(about()), "about", tr("About"));
103 #endif // Q_WS_MAEMO_5
107 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
108 "view-fullscreen", tr("Full screen"));
110 (void)addToolBarAction(this, SLOT(close()), "", tr("Exit"));
113 // Buttons on top of the book view
114 previousButton = new TranslucentButton("back", this);
115 nextButton = new TranslucentButton("forward", this);
117 // Handle model changes
118 connect(Library::instance(), SIGNAL(nowReadingChanged()),
119 this, SLOT(onCurrentBookChanged()));
121 // Load library, upgrade it if needed
122 libraryProgress = new ProgressDialog(tr("Upgrading library"), this);
123 Library *library = Library::instance();
124 connect(library, SIGNAL(beginUpgrade(int)), this, SLOT(onBeginUpgrade(int)));
125 connect(library, SIGNAL(upgrading(const QString &)),
126 this, SLOT(onUpgrading(const QString &)));
127 connect(library, SIGNAL(endUpgrade()), this, SLOT(onEndUpgrade()));
129 // Handle loading book parts
130 connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
131 connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
134 connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
136 // Shadow window for full screen reading
137 fullScreenWindow = new FullScreenWindow(this);
138 connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
140 // Handle settings changes
141 connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
142 this, SLOT(onSettingsChanged(const QString &)));
144 // Handle book view buttons
145 connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
146 connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
148 // Adopt view, show window
151 #ifdef DORIAN_TEST_MODEL
152 (void)new ModelTest(Library::instance(), this);
156 void MainWindow::initialize()
159 Library *library = Library::instance();
161 // Upgrade library if needed, then load it
165 // Load book on command line, or load last read book, or load default book
166 if (QCoreApplication::arguments().size() == 2) {
167 QString path = QCoreApplication::arguments()[1];
169 QModelIndex index = library->find(path);
170 if (index.isValid()) {
171 library->setNowReading(index);
174 QModelIndex index = library->nowReading();
175 if (index.isValid()) {
176 library->setNowReading(index);
178 if (!library->rowCount()) {
179 library->add(":/books/2BR02B.epub");
181 SortedLibrary sorted;
182 library->setNowReading(sorted.mapToSource(sorted.index(0, 0)));
187 void MainWindow::onCurrentBookChanged()
190 setCurrentBook(Library::instance()->nowReading());
193 void MainWindow::showRegular()
197 // Re-parent children
198 fullScreenWindow->leaveChildren();
199 QList<QWidget *> otherChildren;
200 otherChildren << progress << previousButton << nextButton;
201 takeChildren(view, otherChildren);
203 // Adjust geometry of decorations
205 QRect geo = geometry();
207 int y = geo.height() - progress->thickness();
208 #if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
209 bool hasToolBar = false;
210 # if defined(Q_OS_SYMBIAN)
212 (QApplication::desktop()->width() < QApplication::desktop()->height());
213 qDebug() << (hasToolBar? "Portrait": "Landscape");
216 y -= toolBar->height();
219 progress->setGeometry(0, y, geo.width(), y + progress->thickness());
221 #if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
222 y = geo.height() - TranslucentButton::pixels;
224 y -= toolBar->height();
226 previousButton->setGeometry(0, y, TranslucentButton::pixels,
227 TranslucentButton::pixels);
228 nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
229 TranslucentButton::pixels, TranslucentButton::pixels);
231 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
232 TranslucentButton::pixels, TranslucentButton::pixels);
233 nextButton->setGeometry(geo.width() - TranslucentButton::pixels - 25,
234 toolBar->height(), TranslucentButton::pixels,
235 TranslucentButton::pixels);
236 #endif // Q_WS_MAEMO_5
237 qDebug() << "previousButton geometry" << previousButton->geometry();
239 fullScreenWindow->hide();
241 #if defined(Q_OS_SYMBIAN)
246 previousButton->flash();
249 void MainWindow::showBig()
253 // Re-parent children
255 fullScreenWindow->takeChildren(view, progress, previousButton, nextButton);
257 // Adjust geometry of decorations
258 QRect screen = QApplication::desktop()->screenGeometry();
259 int y = screen.height() - progress->thickness();
260 progress->setGeometry(0, y, screen.width(), y + progress->thickness());
261 #if defined(Q_WS_MAEMO_5)
262 nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
263 TranslucentButton::pixels, TranslucentButton::pixels);
265 nextButton->setGeometry(screen.width() - TranslucentButton::pixels - 25, 0,
266 TranslucentButton::pixels, TranslucentButton::pixels);
267 #endif // Q_WS_MAEMO_5
268 previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
269 TranslucentButton::pixels, TranslucentButton::pixels);
274 fullScreenWindow->showFullScreen();
276 fullScreenWindow->activateWindow();
280 previousButton->flash();
283 void MainWindow::setCurrentBook(const QModelIndex ¤t)
286 Book *book = Library::instance()->book(current);
288 setWindowTitle(book? book->shortName(): tr("Dorian"));
291 void MainWindow::showLibrary()
293 (new LibraryDialog(this))->show();
296 void MainWindow::showSettings()
298 (new SettingsWindow(this))->show();
301 void MainWindow::showInfo()
303 if (mCurrent.isValid()) {
304 (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
309 void MainWindow::showDevTools()
311 (new DevTools())->exec();
314 void MainWindow::showBookmarks()
316 Book *book = Library::instance()->book(mCurrent);
318 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
319 connect(bookmarks, SIGNAL(addBookmark(const QString &)),
320 this, SLOT(onAddBookmark(const QString &)));
321 connect(bookmarks, SIGNAL(goToBookmark(int)),
322 this, SLOT(onGoToBookmark(int)));
327 void MainWindow::closeEvent(QCloseEvent *event)
330 view->setLastBookmark();
331 AdopterWindow::closeEvent(event);
334 void MainWindow::onSettingsChanged(const QString &key)
336 #if defined(Q_WS_MAEMO_5)
337 if (key == "orientation") {
338 QString value = Settings::instance()->value(key,
339 Platform::instance()->defaultOrientation()).toString();
340 qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
341 if (value == "portrait") {
342 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
343 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
344 fullScreenWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,
346 fullScreenWindow->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
348 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
349 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
350 fullScreenWindow->setAttribute(Qt::WA_Maemo5PortraitOrientation,
352 fullScreenWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,
355 } else if (key == "lightson") {
356 bool enable = Settings::instance()->value(key, false).toBool();
357 qDebug() << "MainWindow::onSettingsChanged: lightson" << enable;
358 killTimer(preventBlankingTimer);
360 preventBlankingTimer = startTimer(29 * 1000);
365 #endif // Q_WS_MAEMO_5
368 void MainWindow::onPartLoadStart()
371 Platform::instance()->showBusy(this, true);
374 void MainWindow::onPartLoadEnd(int index)
377 bool enablePrevious = false;
378 bool enableNext = false;
379 Book *book = Library::instance()->book(mCurrent);
382 enablePrevious = true;
384 if (index < (book->parts.size() - 1)) {
388 Platform::instance()->showBusy(this, false);
391 void MainWindow::onAddBookmark(const QString ¬e)
394 view->addBookmark(note);
395 Platform::instance()->information(tr("Bookmarked current position"), this);
398 void MainWindow::onGoToBookmark(int index)
401 Book *book = Library::instance()->book(mCurrent);
402 view->goToBookmark(book->bookmarks()[index]);
405 void MainWindow::showChapters()
407 Book *book = Library::instance()->book(mCurrent);
409 ChaptersDialog *chapters = new ChaptersDialog(book, this);
410 connect(chapters, SIGNAL(goToChapter(int)),
411 this, SLOT(onGoToChapter(int)));
416 void MainWindow::onGoToChapter(int index)
420 Book *book = Library::instance()->book(mCurrent);
423 int partIndex = book->partFromChapter(index, fragment);
424 if (partIndex != -1) {
425 view->goToPart(partIndex, fragment);
430 void MainWindow::timerEvent(QTimerEvent *event)
432 if (event->timerId() == preventBlankingTimer) {
434 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
435 MCE_REQUEST_IF, QDBusConnection::systemBus());
436 mce.call(MCE_PREVENT_BLANK_REQ);
437 #endif // Q_WS_MAEMO_5
438 qDebug() << "MainWindow::timerEvent: Prevent display blanking";
440 AdopterWindow::timerEvent(event);
443 void MainWindow::resizeEvent(QResizeEvent *e)
447 int toolBarHeight = 0;
450 // Tool bar is only useful in portrait mode
451 bool isPortrait = (e->size().width() < e->size().height());
452 toolBar->setVisible(isPortrait);
454 toolBarHeight = toolBar->height();
456 #endif // Q_OS_SYMBIAN
459 qDebug() << "BookView geometry" << bookView->geometry();
460 QRect geo = bookView->geometry();
461 progress->setGeometry(geo.x(),
462 geo.y() + geo.height() - progress->thickness() + toolBarHeight,
463 geo.width(), progress->thickness());
464 previousButton->setGeometry(geo.x(),
465 geo.y() + geo.height() - TranslucentButton::pixels + toolBarHeight,
466 TranslucentButton::pixels, TranslucentButton::pixels);
467 nextButton->setGeometry(
468 geo.x() + geo.width() - TranslucentButton::pixels,
469 geo.y(), TranslucentButton::pixels, TranslucentButton::pixels);
471 QMainWindow::resizeEvent(e);
474 void MainWindow::about()
476 Dyalog *aboutDialog = new Dyalog(this, false);
477 aboutDialog->setWindowTitle(tr("About Dorian"));
478 QString version = Platform::instance()->version();
479 QLabel *label = new QLabel(aboutDialog);
480 label->setTextFormat(Qt::RichText);
481 label->setOpenExternalLinks(true);
482 label->setWordWrap(true);
483 label->setText(tr("<b>Dorian %1</b><br><br>Copyright © 2010 "
484 "Akos Polster <akos@pipacs.com><br>"
485 "Licensed under GNU General Public License, Version 3<br>"
486 "Source code:<br><a href='https://garage.maemo.org/projects/dorian/'>"
487 "garage.maemo.org/projects/dorian</a>").arg(version));
488 aboutDialog->addWidget(label);
489 aboutDialog->addStretch();
493 void MainWindow::goToNextPage()
496 previousButton->flash();
500 void MainWindow::goToPreviousPage()
503 previousButton->flash();
504 view->goPreviousPage();
507 void MainWindow::onBeginUpgrade(int total)
509 libraryProgress->setVisible(total > 0);
510 libraryProgress->setWindowTitle(tr("Upgrading library"));
511 libraryProgress->setMaximum(total);
514 void MainWindow::onUpgrading(const QString &path)
516 libraryProgress->setLabelText(tr("Upgrading %1").
517 arg(QFileInfo(path).fileName()));
518 libraryProgress->setValue(libraryProgress->value() + 1);
521 void MainWindow::onEndUpgrade()
523 libraryProgress->hide();
524 libraryProgress->reset();
527 void MainWindow::onBeginLoad(int total)
529 libraryProgress->setVisible(total > 0);
530 libraryProgress->setWindowTitle(tr("Loading library"));
531 libraryProgress->setMaximum(total);
534 void MainWindow::onLoading(const QString &path)
536 libraryProgress->setLabelText(tr("Loading %1").
537 arg(QFileInfo(path).fileName()));
538 libraryProgress->setValue(libraryProgress->value() + 1);
541 void MainWindow::onEndLoad()
543 libraryProgress->hide();
544 libraryProgress->reset();