4 #include <QApplication>
11 # include <QtMaemo5/QMaemo5InformationBox>
13 # include <QtGui/QX11Info>
14 # include <X11/Xlib.h>
15 # include <X11/Xatom.h>
16 # include <mce/mode-names.h>
17 # include <mce/dbus-names.h>
18 #endif // Q_WS_MAEMO_5
23 #include "infodialog.h"
24 #include "librarydialog.h"
26 #include "mainwindow.h"
27 #include "settingswindow.h"
28 #include "bookmarksdialog.h"
30 #include "chaptersdialog.h"
31 #include "fullscreenwindow.h"
33 #include "bookfinder.h"
36 #include "translucentbutton.h"
38 #ifdef DORIAN_TEST_MODEL
39 # include "modeltest.h"
42 const int PROGRESS_HEIGHT = 17;
43 static const char *DORIAN_VERSION =
44 #include "pkg/version.txt"
47 MainWindow::MainWindow(QWidget *parent):
48 AdopterWindow(parent), view(0), preventBlankingTimer(-1)
50 Trace t("MainWindow::MainWindow");
52 setAttribute(Qt::WA_Maemo5StackedWindow, true);
54 setWindowTitle("Dorian");
56 // Central widget. Must be an intermediate, because the book view widget
57 // can be re-parented later
58 QFrame *central = new QFrame(this);
59 QVBoxLayout *layout = new QVBoxLayout(central);
61 central->setLayout(layout);
62 setCentralWidget(central);
65 view = new BookView(central);
67 layout->addWidget(view);
70 progress = new Progress(central);
73 settings = new QDialog(this);
77 chaptersAction = addToolBarAction(this, SLOT(showChapters()),
78 "chapters", tr("Chapters"));
79 bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
80 "bookmarks", tr("Bookmarks"));
81 infoAction = addToolBarAction(this, SLOT(showInfo()),
82 "info", tr("Book info"));
83 libraryAction = addToolBarAction(this, SLOT(showLibrary()),
84 "library", tr("Library"));
87 settingsAction = menuBar()->addAction(tr("Settings"));
88 connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
89 devToolsAction = menuBar()->addAction(tr("Developer"));
90 connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
91 QAction *aboutAction = menuBar()->addAction(tr("About"));
92 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
94 settingsAction = addToolBarAction(this, SLOT(showSettings()),
95 "preferences-system", tr("Settings"));
96 devToolsAction = addToolBarAction(this, SLOT(showDevTools()),
97 "developer", tr("Developer"));
98 addToolBarAction(this, SLOT(about()), "about", tr("About"));
99 #endif // Q_WS_MAEMO_5
102 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
103 "view-fullscreen", tr("Full screen"));
105 // Buttons on top of the book view
106 previousButton = new TranslucentButton("back", this);
107 nextButton = new TranslucentButton("forward", this);
109 // Handle model changes
110 connect(Library::instance(), SIGNAL(nowReadingChanged()),
111 this, SLOT(onCurrentBookChanged()));
113 // Load book on command line, or load last read book, or load default book
114 Library *library = Library::instance();
115 if (QCoreApplication::arguments().size() == 2) {
116 QString path = QCoreApplication::arguments()[1];
118 QModelIndex index = library->find(path);
119 if (index.isValid()) {
120 library->setNowReading(index);
124 QModelIndex index = library->nowReading();
125 if (index.isValid()) {
126 library->setNowReading(index);
129 if (!library->rowCount()) {
130 library->add(":/books/2BR02B.epub");
132 library->setNowReading(library->index(0));
136 // Handle loading book parts
137 connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
138 connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
141 connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
143 // Shadow window for full screen reading
144 fullScreenWindow = new FullScreenWindow(this);
145 connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
147 // Handle settings changes
148 Settings *settings = Settings::instance();
149 connect(settings, SIGNAL(valueChanged(const QString &)),
150 this, SLOT(onSettingsChanged(const QString &)));
151 settings->setValue("orientation", settings->value("orientation"));
152 settings->setValue("lightson", settings->value("lightson"));
153 settings->setValue("usevolumekeys", settings->value("usevolumekeys"));
155 // Handle book view buttons
156 connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
157 connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
159 #ifdef DORIAN_TEST_MODEL
160 (void)new ModelTest(Library::instance(), this);
164 MainWindow::~MainWindow()
168 void MainWindow::onCurrentBookChanged()
170 setCurrentBook(Library::instance()->nowReading());
173 void MainWindow::showRegular()
175 Trace t("MainWindow::showRegular");
176 fullScreenWindow->hide();
177 fullScreenWindow->leaveChildren();
179 QList<QWidget *> otherChildren;
180 otherChildren << progress << previousButton << nextButton;
181 takeChildren(view, otherChildren);
182 QRect geo = geometry();
183 progress->setGeometry(0, 0, geo.width(), PROGRESS_HEIGHT);
184 #if defined(Q_WS_MAEMO_5)
185 previousButton->setGeometry(0,
186 geo.height() - toolBar->height() - TranslucentButton::pixels,
187 TranslucentButton::pixels, TranslucentButton::pixels);
188 nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
189 TranslucentButton::pixels, TranslucentButton::pixels);
190 #elif defined(Q_OS_SYMBIAN)
191 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
192 TranslucentButton::pixels, TranslucentButton::pixels);
193 nextButton->setGeometry(geo.width() - TranslucentButton::pixels,
194 0, TranslucentButton::pixels, TranslucentButton::pixels);
196 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
197 TranslucentButton::pixels, TranslucentButton::pixels);
198 nextButton->setGeometry(geo.width() - TranslucentButton::pixels - 25,
199 toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
200 #endif // Q_WS_MAEMO_5
201 qDebug() << "previousButton geometry" << previousButton->geometry();
204 previousButton->show();
205 nextButton->flash(1500);
206 previousButton->flash(1500);
209 void MainWindow::showBig()
211 Trace t("MainWindow::showBig");
213 QList<QWidget *> otherChildren;
214 otherChildren << progress << nextButton << previousButton;
215 QRect screen = QApplication::desktop()->screenGeometry();
216 progress->setGeometry(0, 0, screen.width(), PROGRESS_HEIGHT);
217 #if defined(Q_WS_MAEMO_5)
218 nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
219 TranslucentButton::pixels, TranslucentButton::pixels);
221 nextButton->setGeometry(screen.width() - TranslucentButton::pixels - 25, 0,
222 TranslucentButton::pixels, TranslucentButton::pixels);
223 #endif // Q_WS_MAEMO_5
224 previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
225 TranslucentButton::pixels, TranslucentButton::pixels);
227 fullScreenWindow->takeChildren(view, otherChildren);
228 fullScreenWindow->showFullScreen();
230 nextButton->flash(1500);
231 previousButton->flash(1500);
234 void MainWindow::setCurrentBook(const QModelIndex ¤t)
237 Book *book = Library::instance()->book(current);
239 setWindowTitle(book? book->shortName(): tr("Dorian"));
242 void MainWindow::showLibrary()
244 (new LibraryDialog(this))->show();
247 void MainWindow::showSettings()
249 (new SettingsWindow(this))->show();
252 void MainWindow::showInfo()
254 if (mCurrent.isValid()) {
255 (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
260 void MainWindow::showDevTools()
262 (new DevTools())->exec();
265 void MainWindow::showBookmarks()
267 Book *book = Library::instance()->book(mCurrent);
269 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
270 bookmarks->setWindowModality(Qt::WindowModal);
271 connect(bookmarks, SIGNAL(addBookmark()), this, SLOT(onAddBookmark()));
272 connect(bookmarks, SIGNAL(goToBookmark(int)),
273 this, SLOT(onGoToBookmark(int)));
278 void MainWindow::closeEvent(QCloseEvent *event)
280 Trace t("MainWindow::closeEvent");
281 view->setLastBookmark();
285 void MainWindow::onSettingsChanged(const QString &key)
288 if (key == "orientation") {
289 QString value = Settings::instance()->value(key).toString();
290 qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
291 if (value == "portrait") {
292 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
293 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
296 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
297 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
299 } else if (key == "lightson") {
300 bool enable = Settings::instance()->value(key, false).toBool();
301 qDebug() << "MainWindow::onSettingsChanged: lightson:" << enable;
302 killTimer(preventBlankingTimer);
304 preventBlankingTimer = startTimer(29 * 1000);
306 } else if (key == "usevolumekeys") {
307 bool value = Settings::instance()->value(key).toBool();
308 qDebug() << "MainWindow::onSettingsChanged: usevolumekeys" << value;
310 fullScreenWindow->grabZoomKeys(value);
314 #endif // Q_WS_MAEMO_5
317 void MainWindow::onPartLoadStart()
319 Trace t("MainWindow::onPartLoadStart");
321 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
325 void MainWindow::onPartLoadEnd(int index)
327 Trace t("MainWindow::onPartLoadEnd");
328 bool enablePrevious = false;
329 bool enableNext = false;
330 Book *book = Library::instance()->book(mCurrent);
333 enablePrevious = true;
335 if (index < (book->parts.size() - 1)) {
340 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
341 #endif // Q_WS_MAEMO_5
344 void MainWindow::onAddBookmark()
346 Trace t("MainWindow:onAddBookmark");
350 void MainWindow::onGoToBookmark(int index)
352 Trace t("MainWindow::onGoToBookmark");
353 Book *book = Library::instance()->book(mCurrent);
354 view->goToBookmark(book->bookmarks()[index]);
357 void MainWindow::showChapters()
359 Book *book = Library::instance()->book(mCurrent);
361 ChaptersDialog *chapters = new ChaptersDialog(book, this);
362 chapters->setWindowModality(Qt::WindowModal);
363 connect(chapters, SIGNAL(goToChapter(int)),
364 this, SLOT(onGoToChapter(int)));
369 void MainWindow::onGoToChapter(int index)
371 Trace t("MainWindow::onGoToChapter");
373 Book *book = Library::instance()->book(mCurrent);
375 int partIndex = book->partFromChapter(index);
376 if (partIndex != -1) {
377 view->goToBookmark(Book::Bookmark(partIndex, 0));
382 void MainWindow::timerEvent(QTimerEvent *event)
384 if (event->timerId() == preventBlankingTimer) {
386 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
387 MCE_REQUEST_IF, QDBusConnection::systemBus());
388 mce.call(MCE_PREVENT_BLANK_REQ);
389 #endif // Q_WS_MAEMO_5
390 qDebug() << "MainWindow::timerEvent: Prevent display blanking";
394 void MainWindow::resizeEvent(QResizeEvent *e)
396 Trace t("MainWindow::resizeEvent");
397 progress->setGeometry(QRect(0, 0, e->size().width(), PROGRESS_HEIGHT));
398 #if defined(Q_WS_MAEMO_5)
399 previousButton->setGeometry(0,
400 e->size().height() - toolBar->height() - TranslucentButton::pixels,
401 TranslucentButton::pixels, TranslucentButton::pixels);
402 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
403 TranslucentButton::pixels, TranslucentButton::pixels);
404 #elif defined(Q_OS_SYMBIAN)
405 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
406 TranslucentButton::pixels, TranslucentButton::pixels);
407 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
408 0, TranslucentButton::pixels, TranslucentButton::pixels);
410 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
411 TranslucentButton::pixels, TranslucentButton::pixels);
412 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels - 25,
413 toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
414 #endif // Q_WS_MAEMO_5
415 qDebug() << "previousButton geometry" << previousButton->geometry();
416 previousButton->flash(1500);
417 nextButton->flash(1500);
418 QMainWindow::resizeEvent(e);
421 void MainWindow::about()
423 Dyalog *aboutDialog = new Dyalog(this);
424 aboutDialog->setWindowTitle(tr("About Dorian"));
425 QLabel *label = new QLabel(aboutDialog);
426 label->setTextFormat(Qt::RichText);
427 label->setOpenExternalLinks(true);
428 label->setWordWrap(true);
429 label->setText(tr("<b>Dorian %1</b><br><br>Copyright © 2010 "
430 "Akos Polster <akos@pipacs.com><br>"
431 "Licensed under GNU General Public License, Version 3<br>"
432 "Source code: <a href='https://garage.maemo.org/projects/dorian/'>"
433 "garage.maemo.org/projects/dorian</a>").arg(DORIAN_VERSION));
434 aboutDialog->addWidget(label);
435 aboutDialog->addStretch();
440 void MainWindow::goToNextPage()
442 nextButton->flash(1500);
443 previousButton->flash(1500);
447 void MainWindow::goToPreviousPage()
449 nextButton->flash(1500);
450 previousButton->flash(1500);
451 view->goPreviousPage();