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");
47 // Central widget. Must be an intermediate, because the book view widget
48 // can be re-parented later
49 QFrame *central = new QFrame(this);
50 QVBoxLayout *layout = new QVBoxLayout(central);
52 central->setLayout(layout);
53 setCentralWidget(central);
56 view = new BookView(central);
58 layout->addWidget(view);
61 progress = new Progress(this);
66 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
67 "view-fullscreen", tr("Full screen"));
70 chaptersAction = addToolBarAction(this, SLOT(showChapters()),
71 "chapters", tr("Chapters"));
72 bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
73 "bookmarks", tr("Bookmarks"));
74 infoAction = addToolBarAction(this, SLOT(showInfo()),
75 "info", tr("Book info"));
76 libraryAction = addToolBarAction(this, SLOT(showLibrary()),
77 "library", tr("Library"));
80 settingsAction = menuBar()->addAction(tr("Settings"));
81 connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
82 devToolsAction = menuBar()->addAction(tr("Developer"));
83 connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
84 QAction *aboutAction = menuBar()->addAction(tr("About"));
85 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
87 settingsAction = addToolBarAction(this, SLOT(showSettings()),
88 "preferences-system", tr("Settings"));
89 devToolsAction = addToolBarAction(this, SLOT(showDevTools()),
90 "developer", tr("Developer"));
91 addToolBarAction(this, SLOT(about()), "about", tr("About"));
92 #endif // Q_WS_MAEMO_5
96 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
97 "view-fullscreen", tr("Full screen"));
99 (void)addToolBarAction(this, SLOT(close()), "", tr("Exit"));
102 // Buttons on top of the book view
103 previousButton = new TranslucentButton("back", this);
104 nextButton = new TranslucentButton("forward", this);
106 // Handle model changes
107 connect(Library::instance(), SIGNAL(nowReadingChanged()),
108 this, SLOT(onCurrentBookChanged()));
110 // Load library, upgrade it if needed
111 libraryProgress = new ProgressDialog(tr("Upgrading library"), this);
112 Library *library = Library::instance();
113 connect(library, SIGNAL(beginUpgrade(int)), this, SLOT(onBeginUpgrade(int)));
114 connect(library, SIGNAL(upgrading(const QString &)),
115 this, SLOT(onUpgrading(const QString &)));
116 connect(library, SIGNAL(endUpgrade()), this, SLOT(onEndUpgrade()));
118 // Handle loading book parts
119 connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
120 connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
123 connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
125 // Shadow window for full screen reading
126 fullScreenWindow = new FullScreenWindow(this);
127 connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
129 // Handle settings changes
130 connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
131 this, SLOT(onSettingsChanged(const QString &)));
133 // Handle book view buttons
134 connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
135 connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
137 // Adopt view, show window
140 #ifdef DORIAN_TEST_MODEL
141 (void)new ModelTest(Library::instance(), this);
145 void MainWindow::initialize()
148 Library *library = Library::instance();
150 // Upgrade library if needed, then load it
154 // Load book on command line, or load last read book, or load default book
155 if (QCoreApplication::arguments().size() == 2) {
156 QString path = QCoreApplication::arguments()[1];
158 QModelIndex index = library->find(path);
159 if (index.isValid()) {
160 library->setNowReading(index);
163 QModelIndex index = library->nowReading();
164 if (index.isValid()) {
165 library->setNowReading(index);
167 if (!library->rowCount()) {
168 library->add(":/books/2BR02B.epub");
170 SortedLibrary sorted;
171 library->setNowReading(sorted.mapToSource(sorted.index(0, 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
194 QRect geo = geometry();
195 int y = geo.height() - progress->thickness();
196 #if defined(Q_WS_MAEMO_5)
197 y -= toolBar->height();
199 progress->setGeometry(0, y, geo.width(), y + progress->thickness());
201 #if defined(Q_WS_MAEMO_5)
202 previousButton->setGeometry(0,
203 geo.height() - toolBar->height() - TranslucentButton::pixels,
204 TranslucentButton::pixels, TranslucentButton::pixels);
205 nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
206 TranslucentButton::pixels, TranslucentButton::pixels);
207 #elif defined(Q_OS_SYMBIAN)
208 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
209 TranslucentButton::pixels, TranslucentButton::pixels);
210 nextButton->setGeometry(geo.width() - TranslucentButton::pixels,
211 0, TranslucentButton::pixels, TranslucentButton::pixels);
213 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
214 TranslucentButton::pixels, TranslucentButton::pixels);
215 nextButton->setGeometry(geo.width() - TranslucentButton::pixels - 25,
216 toolBar->height(), TranslucentButton::pixels,
217 TranslucentButton::pixels);
218 #endif // Q_WS_MAEMO_5
219 qDebug() << "previousButton geometry" << previousButton->geometry();
221 fullScreenWindow->hide();
223 #if defined(Q_OS_SYMBIAN)
228 previousButton->flash();
231 void MainWindow::showBig()
235 // Re-parent children
237 QList<QWidget *> otherChildren;
238 otherChildren << progress << nextButton << previousButton;
239 fullScreenWindow->takeChildren(view, otherChildren);
241 // Adjust geometry of decorations
242 QRect screen = QApplication::desktop()->screenGeometry();
243 int y = screen.height() - progress->thickness();
244 progress->setGeometry(0, y, screen.width(), y + progress->thickness());
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 connect(bookmarks, SIGNAL(addBookmark(const QString &)),
304 this, SLOT(onAddBookmark(const QString &)));
305 connect(bookmarks, SIGNAL(goToBookmark(int)),
306 this, SLOT(onGoToBookmark(int)));
311 void MainWindow::closeEvent(QCloseEvent *event)
314 view->setLastBookmark();
318 void MainWindow::onSettingsChanged(const QString &key)
320 #if defined(Q_WS_MAEMO_5)
321 if (key == "orientation") {
322 QString value = Settings::instance()->value(key,
323 Platform::instance()->defaultOrientation()).toString();
324 qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
325 if (value == "portrait") {
326 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
327 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
328 fullScreenWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,
330 fullScreenWindow->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
332 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
333 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
334 fullScreenWindow->setAttribute(Qt::WA_Maemo5PortraitOrientation,
336 fullScreenWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,
339 } else if (key == "lightson") {
340 bool enable = Settings::instance()->value(key, false).toBool();
341 qDebug() << "MainWindow::onSettingsChanged: lightson" << enable;
342 killTimer(preventBlankingTimer);
344 preventBlankingTimer = startTimer(29 * 1000);
349 #endif // Q_WS_MAEMO_5
352 void MainWindow::onPartLoadStart()
355 Platform::instance()->showBusy(this, true);
358 void MainWindow::onPartLoadEnd(int index)
361 bool enablePrevious = false;
362 bool enableNext = false;
363 Book *book = Library::instance()->book(mCurrent);
366 enablePrevious = true;
368 if (index < (book->parts.size() - 1)) {
372 Platform::instance()->showBusy(this, false);
375 void MainWindow::onAddBookmark(const QString ¬e)
378 view->addBookmark(note);
379 Platform::instance()->information(tr("Bookmarked current position"), this);
382 void MainWindow::onGoToBookmark(int index)
385 Book *book = Library::instance()->book(mCurrent);
386 view->goToBookmark(book->bookmarks()[index]);
389 void MainWindow::showChapters()
391 Book *book = Library::instance()->book(mCurrent);
393 ChaptersDialog *chapters = new ChaptersDialog(book, this);
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();