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"), true);
72 bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
73 "bookmarks", tr("Bookmarks"), true);
74 infoAction = addToolBarAction(this, SLOT(showInfo()),
75 "info", tr("Book info"), true);
76 libraryAction = addToolBarAction(this, SLOT(showLibrary()),
77 "library", tr("Library"), true);
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();
196 int y = geo.height() - progress->thickness();
197 #if defined(Q_WS_MAEMO_5)
198 y -= toolBar->height();
200 progress->setGeometry(0, y, geo.width(), y + progress->thickness());
202 #if defined(Q_WS_MAEMO_5)
203 previousButton->setGeometry(0,
204 geo.height() - toolBar->height() - TranslucentButton::pixels,
205 TranslucentButton::pixels, TranslucentButton::pixels);
206 nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
207 TranslucentButton::pixels, TranslucentButton::pixels);
208 #elif defined(Q_OS_SYMBIAN)
209 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
210 TranslucentButton::pixels, TranslucentButton::pixels);
211 nextButton->setGeometry(geo.width() - TranslucentButton::pixels,
212 0, TranslucentButton::pixels, TranslucentButton::pixels);
214 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
215 TranslucentButton::pixels, TranslucentButton::pixels);
216 nextButton->setGeometry(geo.width() - TranslucentButton::pixels - 25,
217 toolBar->height(), TranslucentButton::pixels,
218 TranslucentButton::pixels);
219 #endif // Q_WS_MAEMO_5
220 qDebug() << "previousButton geometry" << previousButton->geometry();
222 fullScreenWindow->hide();
224 #if defined(Q_OS_SYMBIAN)
229 previousButton->flash();
232 void MainWindow::showBig()
236 // Re-parent children
238 QList<QWidget *> otherChildren;
239 otherChildren << progress << nextButton << previousButton;
240 fullScreenWindow->takeChildren(view, otherChildren);
242 // Adjust geometry of decorations
243 QRect screen = QApplication::desktop()->screenGeometry();
244 int y = screen.height() - progress->thickness();
245 progress->setGeometry(0, y, screen.width(), y + progress->thickness());
246 #if defined(Q_WS_MAEMO_5)
247 nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
248 TranslucentButton::pixels, TranslucentButton::pixels);
250 nextButton->setGeometry(screen.width() - TranslucentButton::pixels - 25, 0,
251 TranslucentButton::pixels, TranslucentButton::pixels);
252 #endif // Q_WS_MAEMO_5
253 previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
254 TranslucentButton::pixels, TranslucentButton::pixels);
259 fullScreenWindow->showFullScreen();
261 fullScreenWindow->activateWindow();
265 previousButton->flash();
268 void MainWindow::setCurrentBook(const QModelIndex ¤t)
271 Book *book = Library::instance()->book(current);
273 setWindowTitle(book? book->shortName(): tr("Dorian"));
276 void MainWindow::showLibrary()
278 (new LibraryDialog(this))->show();
281 void MainWindow::showSettings()
283 (new SettingsWindow(this))->show();
286 void MainWindow::showInfo()
288 if (mCurrent.isValid()) {
289 (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
294 void MainWindow::showDevTools()
296 (new DevTools())->exec();
299 void MainWindow::showBookmarks()
301 Book *book = Library::instance()->book(mCurrent);
303 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
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();
316 AdopterWindow::closeEvent(event);
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 connect(chapters, SIGNAL(goToChapter(int)),
396 this, SLOT(onGoToChapter(int)));
401 void MainWindow::onGoToChapter(int index)
405 Book *book = Library::instance()->book(mCurrent);
408 int partIndex = book->partFromChapter(index, fragment);
409 if (partIndex != -1) {
410 view->goToPart(partIndex, fragment);
415 void MainWindow::timerEvent(QTimerEvent *event)
417 if (event->timerId() == preventBlankingTimer) {
419 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
420 MCE_REQUEST_IF, QDBusConnection::systemBus());
421 mce.call(MCE_PREVENT_BLANK_REQ);
422 #endif // Q_WS_MAEMO_5
423 qDebug() << "MainWindow::timerEvent: Prevent display blanking";
425 AdopterWindow::timerEvent(event);
428 void MainWindow::resizeEvent(QResizeEvent *e)
433 qDebug() << "BookView geometry" << bookView->geometry();
434 QRect geo = bookView->geometry();
435 progress->setGeometry(geo.x(),
436 geo.y() + geo.height() - progress->thickness(), geo.width(),
437 progress->thickness());
438 previousButton->setGeometry(geo.x(),
439 geo.y() + geo.height() - TranslucentButton::pixels,
440 TranslucentButton::pixels, TranslucentButton::pixels);
441 nextButton->setGeometry(
442 geo.x() + geo.width() - TranslucentButton::pixels,
443 geo.y(), TranslucentButton::pixels, TranslucentButton::pixels);
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();