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 bookmarks->setWindowModality(Qt::WindowModal);
304 connect(bookmarks, SIGNAL(addBookmark(const QString &)),
305 this, SLOT(onAddBookmark(const QString &)));
306 connect(bookmarks, SIGNAL(goToBookmark(int)),
307 this, SLOT(onGoToBookmark(int)));
312 void MainWindow::closeEvent(QCloseEvent *event)
315 view->setLastBookmark();
319 void MainWindow::onSettingsChanged(const QString &key)
321 #if defined(Q_WS_MAEMO_5)
322 if (key == "orientation") {
323 QString value = Settings::instance()->value(key,
324 Platform::instance()->defaultOrientation()).toString();
325 qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
326 if (value == "portrait") {
327 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
328 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
329 fullScreenWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,
331 fullScreenWindow->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
333 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
334 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
335 fullScreenWindow->setAttribute(Qt::WA_Maemo5PortraitOrientation,
337 fullScreenWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,
340 } else if (key == "lightson") {
341 bool enable = Settings::instance()->value(key, false).toBool();
342 qDebug() << "MainWindow::onSettingsChanged: lightson" << enable;
343 killTimer(preventBlankingTimer);
345 preventBlankingTimer = startTimer(29 * 1000);
350 #endif // Q_WS_MAEMO_5
353 void MainWindow::onPartLoadStart()
356 Platform::instance()->showBusy(this, true);
359 void MainWindow::onPartLoadEnd(int index)
362 bool enablePrevious = false;
363 bool enableNext = false;
364 Book *book = Library::instance()->book(mCurrent);
367 enablePrevious = true;
369 if (index < (book->parts.size() - 1)) {
373 Platform::instance()->showBusy(this, false);
376 void MainWindow::onAddBookmark(const QString ¬e)
379 view->addBookmark(note);
380 Platform::instance()->information(tr("Bookmarked current position"), this);
383 void MainWindow::onGoToBookmark(int index)
386 Book *book = Library::instance()->book(mCurrent);
387 view->goToBookmark(book->bookmarks()[index]);
390 void MainWindow::showChapters()
392 Book *book = Library::instance()->book(mCurrent);
394 ChaptersDialog *chapters = new ChaptersDialog(book, this);
395 chapters->setWindowModality(Qt::WindowModal);
396 connect(chapters, SIGNAL(goToChapter(int)),
397 this, SLOT(onGoToChapter(int)));
402 void MainWindow::onGoToChapter(int index)
406 Book *book = Library::instance()->book(mCurrent);
409 int partIndex = book->partFromChapter(index, fragment);
410 if (partIndex != -1) {
411 view->goToPart(partIndex, fragment);
416 void MainWindow::timerEvent(QTimerEvent *event)
418 if (event->timerId() == preventBlankingTimer) {
420 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
421 MCE_REQUEST_IF, QDBusConnection::systemBus());
422 mce.call(MCE_PREVENT_BLANK_REQ);
423 #endif // Q_WS_MAEMO_5
424 qDebug() << "MainWindow::timerEvent: Prevent display blanking";
426 AdopterWindow::timerEvent(event);
429 void MainWindow::resizeEvent(QResizeEvent *e)
433 if (hasChild(progress)) {
434 qDebug() << "To" << e->size();
435 int y = e->size().height() - progress->thickness();
436 #if defined(Q_WS_MAEMO_5)
437 y -= toolBar->height();
439 progress->setGeometry(0, y, e->size().width(), y + progress->thickness());
441 #if defined(Q_WS_MAEMO_5)
442 previousButton->setGeometry(0,
443 e->size().height() - toolBar->height() - TranslucentButton::pixels,
444 TranslucentButton::pixels, TranslucentButton::pixels);
445 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
446 TranslucentButton::pixels, TranslucentButton::pixels);
447 #elif defined(Q_OS_SYMBIAN)
448 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
449 TranslucentButton::pixels, TranslucentButton::pixels);
450 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
451 0, TranslucentButton::pixels, TranslucentButton::pixels);
453 previousButton->setGeometry(0,
454 e->size().height() - TranslucentButton::pixels,
455 TranslucentButton::pixels, TranslucentButton::pixels);
456 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels - 25,
457 toolBar->height(), TranslucentButton::pixels,
458 TranslucentButton::pixels);
459 #endif // Q_WS_MAEMO_5
462 // This is needed on Maemo, in order not to lose current reading position
463 // after orientation change
464 QTimer::singleShot(250, view, SLOT(restoreLastBookmark()));
466 previousButton->flash();
469 QMainWindow::resizeEvent(e);
472 void MainWindow::about()
474 Dyalog *aboutDialog = new Dyalog(this, false);
475 aboutDialog->setWindowTitle(tr("About Dorian"));
476 QString version = Platform::instance()->version();
477 QLabel *label = new QLabel(aboutDialog);
478 label->setTextFormat(Qt::RichText);
479 label->setOpenExternalLinks(true);
480 label->setWordWrap(true);
481 label->setText(tr("<b>Dorian %1</b><br><br>Copyright © 2010 "
482 "Akos Polster <akos@pipacs.com><br>"
483 "Licensed under GNU General Public License, Version 3<br>"
484 "Source code:<br><a href='https://garage.maemo.org/projects/dorian/'>"
485 "garage.maemo.org/projects/dorian</a>").arg(version));
486 aboutDialog->addWidget(label);
487 aboutDialog->addStretch();
491 void MainWindow::goToNextPage()
494 previousButton->flash();
498 void MainWindow::goToPreviousPage()
501 previousButton->flash();
502 view->goPreviousPage();
505 void MainWindow::onBeginUpgrade(int total)
507 libraryProgress->setVisible(total > 0);
508 libraryProgress->setWindowTitle(tr("Upgrading library"));
509 libraryProgress->setMaximum(total);
512 void MainWindow::onUpgrading(const QString &path)
514 libraryProgress->setLabelText(tr("Upgrading %1").
515 arg(QFileInfo(path).fileName()));
516 libraryProgress->setValue(libraryProgress->value() + 1);
519 void MainWindow::onEndUpgrade()
521 libraryProgress->hide();
522 libraryProgress->reset();
525 void MainWindow::onBeginLoad(int total)
527 libraryProgress->setVisible(total > 0);
528 libraryProgress->setWindowTitle(tr("Loading library"));
529 libraryProgress->setMaximum(total);
532 void MainWindow::onLoading(const QString &path)
534 libraryProgress->setLabelText(tr("Loading %1").
535 arg(QFileInfo(path).fileName()));
536 libraryProgress->setValue(libraryProgress->value() + 1);
539 void MainWindow::onEndLoad()
541 libraryProgress->hide();
542 libraryProgress->reset();