4 #include <QApplication>
9 # include <QtMaemo5/QMaemo5InformationBox>
11 # include <QtGui/QX11Info>
12 # include <X11/Xlib.h>
13 # include <X11/Xatom.h>
14 # include <mce/mode-names.h>
15 # include <mce/dbus-names.h>
16 #endif // Q_WS_MAEMO_5
21 #include "infodialog.h"
22 #include "librarydialog.h"
24 #include "mainwindow.h"
25 #include "settingswindow.h"
26 #include "bookmarksdialog.h"
28 #include "chaptersdialog.h"
29 #include "fullscreenwindow.h"
31 #include "bookfinder.h"
34 #include "translucentbutton.h"
36 #ifdef DORIAN_TEST_MODEL
37 # include "modeltest.h"
41 # define ICON_PREFIX ":/icons/mac/"
43 # define ICON_PREFIX ":/icons/"
46 const int PROGRESS_HEIGHT = 17;
47 static const char *DORIAN_VERSION =
48 #include "pkg/version.txt"
51 MainWindow::MainWindow(QWidget *parent):
52 AdopterWindow(parent), view(0), preventBlankingTimer(-1)
54 Trace t("MainWindow::MainWindow");
56 setAttribute(Qt::WA_Maemo5StackedWindow, true);
58 setWindowTitle("Dorian");
60 // Central widget. Must be an intermediate, because the book view widget
61 // can be re-parented later
62 QFrame *central = new QFrame(this);
63 QVBoxLayout *layout = new QVBoxLayout(central);
65 central->setLayout(layout);
66 setCentralWidget(central);
69 view = new BookView(central);
71 layout->addWidget(view);
74 progress = new Progress(central);
77 settings = new QDialog(this);
81 chaptersAction = addToolBarAction(this, SLOT(showChapters()),
82 "chapters", tr("Chapters"));
83 bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
84 "bookmarks", tr("Bookmarks"));
85 infoAction = addToolBarAction(this, SLOT(showInfo()),
86 "info", tr("Book info"));
87 libraryAction = addToolBarAction(this, SLOT(showLibrary()),
88 "library", tr("Library"));
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
106 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
107 "view-fullscreen", tr("Full screen"));
109 // Buttons on top of the book view
110 previousButton = new TranslucentButton("back", this);
111 nextButton = new TranslucentButton("forward", this);
113 // Handle model changes
114 connect(Library::instance(), SIGNAL(nowReadingChanged()),
115 this, SLOT(onCurrentBookChanged()));
117 // Load book on command line, or load last read book, or load default book
118 Library *library = Library::instance();
119 if (QCoreApplication::arguments().size() == 2) {
120 QString path = QCoreApplication::arguments()[1];
122 QModelIndex index = library->find(path);
123 if (index.isValid()) {
124 library->setNowReading(index);
128 QModelIndex index = library->nowReading();
129 if (index.isValid()) {
130 library->setNowReading(index);
133 if (!library->rowCount()) {
134 library->add(":/books/2 B R 0 2 B.epub");
136 library->setNowReading(library->index(0));
140 // Handle loading book parts
141 connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
142 connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
145 connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
147 // Shadow window for full screen reading
148 fullScreenWindow = new FullScreenWindow(this);
149 connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
151 // Handle settings changes
152 Settings *settings = Settings::instance();
153 connect(settings, SIGNAL(valueChanged(const QString &)),
154 this, SLOT(onSettingsChanged(const QString &)));
155 settings->setValue("orientation", settings->value("orientation"));
156 settings->setValue("lightson", settings->value("lightson"));
157 settings->setValue("usevolumekeys", settings->value("usevolumekeys"));
159 // Handle book view buttons
160 connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
161 connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
163 #ifdef DORIAN_TEST_MODEL
164 (void)new ModelTest(Library::instance(), this);
168 MainWindow::~MainWindow()
172 void MainWindow::onCurrentBookChanged()
174 setCurrentBook(Library::instance()->nowReading());
177 void MainWindow::showRegular()
179 Trace t("MainWindow::showRegular");
180 fullScreenWindow->hide();
181 fullScreenWindow->leaveChildren();
183 QList<QWidget *> otherChildren;
184 otherChildren << progress << previousButton << nextButton;
185 takeChildren(view, otherChildren);
186 QRect geo = geometry();
187 progress->setGeometry(0, 0, geo.width(), PROGRESS_HEIGHT);
188 #if defined(Q_WS_MAEMO_5)
189 previousButton->setGeometry(0,
190 geo.height() - toolBar->height() - TranslucentButton::pixels,
191 TranslucentButton::pixels, TranslucentButton::pixels);
192 nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
193 TranslucentButton::pixels, TranslucentButton::pixels);
194 #elif defined(Q_OS_SYMBIAN)
195 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
196 TranslucentButton::pixels, TranslucentButton::pixels);
197 nextButton->setGeometry(geo.width() - TranslucentButton::pixels,
198 0, TranslucentButton::pixels, TranslucentButton::pixels);
200 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
201 TranslucentButton::pixels, TranslucentButton::pixels);
202 nextButton->setGeometry(geo.width() - TranslucentButton::pixels,
203 toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
204 #endif // Q_WS_MAEMO_5
205 qDebug() << "previousButton geometry" << previousButton->geometry();
208 previousButton->show();
209 nextButton->flash(1500);
210 previousButton->flash(1500);
213 void MainWindow::showBig()
215 Trace t("MainWindow::showBig");
217 QList<QWidget *> otherChildren;
218 otherChildren << progress << nextButton << previousButton;
219 QRect screen = QApplication::desktop()->screenGeometry();
220 progress->setGeometry(0, 0, screen.width(), PROGRESS_HEIGHT);
221 nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
222 TranslucentButton::pixels, TranslucentButton::pixels);
223 previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
224 TranslucentButton::pixels, TranslucentButton::pixels);
226 fullScreenWindow->takeChildren(view, otherChildren);
227 fullScreenWindow->showFullScreen();
229 nextButton->flash(1500);
230 previousButton->flash(1500);
233 void MainWindow::setCurrentBook(const QModelIndex ¤t)
236 Book *book = Library::instance()->book(current);
238 setWindowTitle(book? book->shortName(): tr("Dorian"));
241 void MainWindow::showLibrary()
243 (new LibraryDialog(this))->show();
246 void MainWindow::showSettings()
248 (new SettingsWindow(this))->show();
251 void MainWindow::showInfo()
253 if (mCurrent.isValid()) {
254 (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
259 void MainWindow::showDevTools()
261 (new DevTools())->exec();
264 void MainWindow::showBookmarks()
266 Book *book = Library::instance()->book(mCurrent);
268 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
269 bookmarks->setWindowModality(Qt::WindowModal);
270 connect(bookmarks, SIGNAL(addBookmark()), this, SLOT(onAddBookmark()));
271 connect(bookmarks, SIGNAL(goToBookmark(int)),
272 this, SLOT(onGoToBookmark(int)));
277 void MainWindow::closeEvent(QCloseEvent *event)
279 Trace t("MainWindow::closeEvent");
280 view->setLastBookmark();
284 void MainWindow::onSettingsChanged(const QString &key)
287 if (key == "orientation") {
288 QString value = Settings::instance()->value(key).toString();
289 qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
290 if (value == "portrait") {
291 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
292 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
295 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
296 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
298 } else if (key == "lightson") {
299 bool enable = Settings::instance()->value(key, false).toBool();
300 qDebug() << "MainWindow::onSettingsChanged: lightson:" << enable;
301 killTimer(preventBlankingTimer);
303 preventBlankingTimer = startTimer(29 * 1000);
305 } else if (key == "usevolumekeys") {
306 bool value = Settings::instance()->value(key).toBool();
307 qDebug() << "MainWindow::onSettingsChanged: usevolumekeys" << value;
309 fullScreenWindow->grabZoomKeys(value);
313 #endif // Q_WS_MAEMO_5
316 void MainWindow::onPartLoadStart()
318 Trace t("MainWindow::onPartLoadStart");
320 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
324 void MainWindow::onPartLoadEnd(int index)
326 Trace t("MainWindow::onPartLoadEnd");
327 bool enablePrevious = false;
328 bool enableNext = false;
329 Book *book = Library::instance()->book(mCurrent);
332 enablePrevious = true;
334 if (index < (book->parts.size() - 1)) {
339 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
340 #endif // Q_WS_MAEMO_5
343 void MainWindow::onAddBookmark()
345 Trace t("MainWindow:onAddBookmark");
349 void MainWindow::onGoToBookmark(int index)
351 Trace t("MainWindow::onGoToBookmark");
352 Book *book = Library::instance()->book(mCurrent);
353 view->goToBookmark(book->bookmarks()[index]);
356 void MainWindow::showChapters()
358 Book *book = Library::instance()->book(mCurrent);
360 ChaptersDialog *chapters = new ChaptersDialog(book, this);
361 chapters->setWindowModality(Qt::WindowModal);
362 connect(chapters, SIGNAL(goToChapter(int)),
363 this, SLOT(onGoToChapter(int)));
368 void MainWindow::onGoToChapter(int index)
370 Trace t("MainWindow::onGoToChapter");
372 Book *book = Library::instance()->book(mCurrent);
374 int partIndex = book->partFromChapter(index);
375 if (partIndex != -1) {
376 view->goToBookmark(Book::Bookmark(partIndex, 0));
381 void MainWindow::timerEvent(QTimerEvent *event)
383 if (event->timerId() == preventBlankingTimer) {
385 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
386 MCE_REQUEST_IF, QDBusConnection::systemBus());
387 mce.call(MCE_PREVENT_BLANK_REQ);
388 #endif // Q_WS_MAEMO_5
389 qDebug() << "MainWindow::timerEvent: Prevent display blanking";
393 void MainWindow::resizeEvent(QResizeEvent *e)
395 Trace t("MainWindow::resizeEvent");
396 progress->setGeometry(QRect(0, 0, e->size().width(), PROGRESS_HEIGHT));
397 #if defined(Q_WS_MAEMO_5)
398 previousButton->setGeometry(0,
399 e->size().height() - toolBar->height() - TranslucentButton::pixels,
400 TranslucentButton::pixels, TranslucentButton::pixels);
401 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
402 TranslucentButton::pixels, TranslucentButton::pixels);
403 #elif defined(Q_OS_SYMBIAN)
404 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
405 TranslucentButton::pixels, TranslucentButton::pixels);
406 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
407 0, TranslucentButton::pixels, TranslucentButton::pixels);
409 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
410 TranslucentButton::pixels, TranslucentButton::pixels);
411 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
412 toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
413 #endif // Q_WS_MAEMO_5
414 qDebug() << "previousButton geometry" << previousButton->geometry();
415 previousButton->flash(1500);
416 nextButton->flash(1500);
417 QMainWindow::resizeEvent(e);
420 void MainWindow::about()
422 Dyalog *aboutDialog = new Dyalog(this);
423 aboutDialog->setWindowTitle(tr("About Dorian"));
424 QLabel *label = new QLabel(aboutDialog);
425 label->setTextFormat(Qt::RichText);
426 label->setOpenExternalLinks(true);
427 label->setWordWrap(true);
428 label->setText(tr("<b>Dorian %1</b><br><br>Copyright © 2010 "
429 "Akos Polster <akos@pipacs.com><br>"
430 "Licensed under GNU General Public License, Version 3<br>"
431 "Source code: <a href='https://garage.maemo.org/projects/dorian/'>"
432 "garage.maemo.org/projects/dorian</a>").arg(DORIAN_VERSION));
433 aboutDialog->addWidget(label);
434 aboutDialog->addStretch();
439 void MainWindow::goToNextPage()
441 nextButton->flash(1500);
442 previousButton->flash(1500);
446 void MainWindow::goToPreviousPage()
448 nextButton->flash(1500);
449 previousButton->flash(1500);
450 view->goPreviousPage();