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))->exec();
258 void MainWindow::showDevTools()
260 (new DevTools())->exec();
263 void MainWindow::showBookmarks()
265 Book *book = Library::instance()->book(mCurrent);
267 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
268 bookmarks->setWindowModality(Qt::WindowModal);
269 connect(bookmarks, SIGNAL(addBookmark()), this, SLOT(onAddBookmark()));
270 connect(bookmarks, SIGNAL(goToBookmark(int)),
271 this, SLOT(onGoToBookmark(int)));
276 void MainWindow::closeEvent(QCloseEvent *event)
278 Trace t("MainWindow::closeEvent");
279 view->setLastBookmark();
283 void MainWindow::onSettingsChanged(const QString &key)
286 if (key == "orientation") {
287 QString value = Settings::instance()->value(key).toString();
288 qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
289 if (value == "portrait") {
290 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
291 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
294 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
295 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
297 } else if (key == "lightson") {
298 bool enable = Settings::instance()->value(key, false).toBool();
299 qDebug() << "MainWindow::onSettingsChanged: lightson:" << enable;
300 killTimer(preventBlankingTimer);
302 preventBlankingTimer = startTimer(29 * 1000);
304 } else if (key == "usevolumekeys") {
305 bool value = Settings::instance()->value(key).toBool();
306 qDebug() << "MainWindow::onSettingsChanged: usevolumekeys" << value;
308 fullScreenWindow->grabZoomKeys(value);
312 #endif // Q_WS_MAEMO_5
315 void MainWindow::onPartLoadStart()
317 Trace t("MainWindow::onPartLoadStart");
319 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
323 void MainWindow::onPartLoadEnd(int index)
325 Trace t("MainWindow::onPartLoadEnd");
326 bool enablePrevious = false;
327 bool enableNext = false;
328 Book *book = Library::instance()->book(mCurrent);
331 enablePrevious = true;
333 if (index < (book->parts.size() - 1)) {
338 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
339 #endif // Q_WS_MAEMO_5
342 void MainWindow::onAddBookmark()
344 Trace t("MainWindow:onAddBookmark");
348 void MainWindow::onGoToBookmark(int index)
350 Trace t("MainWindow::onGoToBookmark");
351 Book *book = Library::instance()->book(mCurrent);
352 view->goToBookmark(book->bookmarks()[index]);
355 void MainWindow::showChapters()
357 Book *book = Library::instance()->book(mCurrent);
359 ChaptersDialog *chapters = new ChaptersDialog(book, this);
360 chapters->setWindowModality(Qt::WindowModal);
361 connect(chapters, SIGNAL(goToChapter(int)),
362 this, SLOT(onGoToChapter(int)));
367 void MainWindow::onGoToChapter(int index)
369 Trace t("MainWindow::onGoToChapter");
371 Book *book = Library::instance()->book(mCurrent);
373 int partIndex = book->partFromChapter(index);
374 if (partIndex != -1) {
375 view->goToBookmark(Book::Bookmark(partIndex, 0));
380 void MainWindow::timerEvent(QTimerEvent *event)
382 if (event->timerId() == preventBlankingTimer) {
384 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
385 MCE_REQUEST_IF, QDBusConnection::systemBus());
386 mce.call(MCE_PREVENT_BLANK_REQ);
387 #endif // Q_WS_MAEMO_5
388 qDebug() << "MainWindow::timerEvent: Prevent display blanking";
392 void MainWindow::resizeEvent(QResizeEvent *e)
394 Trace t("MainWindow::resizeEvent");
395 progress->setGeometry(QRect(0, 0, e->size().width(), PROGRESS_HEIGHT));
396 #if defined(Q_WS_MAEMO_5)
397 previousButton->setGeometry(0,
398 e->size().height() - toolBar->height() - TranslucentButton::pixels,
399 TranslucentButton::pixels, TranslucentButton::pixels);
400 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
401 TranslucentButton::pixels, TranslucentButton::pixels);
402 #elif defined(Q_OS_SYMBIAN)
403 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
404 TranslucentButton::pixels, TranslucentButton::pixels);
405 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
406 0, TranslucentButton::pixels, TranslucentButton::pixels);
408 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
409 TranslucentButton::pixels, TranslucentButton::pixels);
410 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
411 toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
412 #endif // Q_WS_MAEMO_5
413 qDebug() << "previousButton geometry" << previousButton->geometry();
414 previousButton->flash(1500);
415 nextButton->flash(1500);
416 QMainWindow::resizeEvent(e);
419 void MainWindow::about()
421 Dyalog *aboutDialog = new Dyalog(this);
422 aboutDialog->setWindowTitle(tr("About Dorian"));
423 QLabel *label = new QLabel(aboutDialog);
424 label->setTextFormat(Qt::RichText);
425 label->setOpenExternalLinks(true);
426 label->setText(tr("<b>Dorian %1</b><br><br>Copyright © 2010 "
427 "Akos Polster <akos@pipacs.com><br>"
428 "Licensed under GNU General Public License, Version 3<br>"
429 "Source code: <a href='https://garage.maemo.org/projects/dorian/'>"
430 "garage.maemo.org/projects/dorian</a>").arg(DORIAN_VERSION));
431 aboutDialog->addWidget(label);
436 void MainWindow::goToNextPage()
438 nextButton->flash(1500);
439 previousButton->flash(1500);
443 void MainWindow::goToPreviousPage()
445 nextButton->flash(1500);
446 previousButton->flash(1500);
447 view->goPreviousPage();