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 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);
78 setUnifiedTitleAndToolBarOnMac(true);
79 settings = new QDialog(this);
80 toolBar = addToolBar("controls");
81 toolBar->setMovable(false);
82 toolBar->setFloatable(false);
83 toolBar->toggleViewAction()->setVisible(false);
84 #if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)
85 toolBar->setIconSize(QSize(42, 42));
88 chaptersAction = addToolBarAction(this, SLOT(showChapters()), "chapters");
89 bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()), "bookmarks");
90 infoAction = addToolBarAction(this, SLOT(showInfo()), "info");
91 libraryAction = addToolBarAction(this, SLOT(showLibrary()), "library");
94 settingsAction = menuBar()->addAction(tr("Settings"));
95 connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
96 devToolsAction = menuBar()->addAction(tr("Developer"));
97 connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
98 QAction *aboutAction = menuBar()->addAction(tr("About"));
99 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
101 settingsAction = addToolBarAction(this, SLOT(showSettings()),
102 "preferences-system");
103 devToolsAction = addToolBarAction(this, SLOT(showDevTools()), "developer");
104 addToolBarAction(this, SLOT(about()), "about");
105 #endif // Q_WS_MAEMO_5
107 QFrame *frame = new QFrame(toolBar);
108 frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
109 toolBar->addWidget(frame);
111 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
114 // Buttons on top of the book view
115 previousButton = new TranslucentButton("back", this);
116 nextButton = new TranslucentButton("forward", this);
118 // Handle model changes
119 connect(Library::instance(), SIGNAL(nowReadingChanged()),
120 this, SLOT(onCurrentBookChanged()));
122 // Load book on command line, or load last read book, or load default book
123 Library *library = Library::instance();
124 if (QCoreApplication::arguments().size() == 2) {
125 QString path = QCoreApplication::arguments()[1];
127 QModelIndex index = library->find(path);
128 if (index.isValid()) {
129 library->setNowReading(index);
133 QModelIndex index = library->nowReading();
134 if (index.isValid()) {
135 library->setNowReading(index);
138 if (!library->rowCount()) {
139 library->add(":/books/2 B R 0 2 B.epub");
141 library->setNowReading(library->index(0));
145 // Handle loading book parts
146 connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
147 connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
150 connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
152 // Shadow window for full screen reading
153 fullScreenWindow = new FullScreenWindow(this);
154 connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
156 // Handle settings changes
157 Settings *settings = Settings::instance();
158 connect(settings, SIGNAL(valueChanged(const QString &)),
159 this, SLOT(onSettingsChanged(const QString &)));
160 settings->setValue("orientation", settings->value("orientation"));
161 settings->setValue("lightson", settings->value("lightson"));
162 settings->setValue("usevolumekeys", settings->value("usevolumekeys"));
164 // Handle book view buttons
165 connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
166 connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
168 #ifdef DORIAN_TEST_MODEL
169 (void)new ModelTest(Library::instance(), this);
173 MainWindow::~MainWindow()
177 void MainWindow::onCurrentBookChanged()
179 setCurrentBook(Library::instance()->nowReading());
182 void MainWindow::showRegular()
184 Trace t("MainWindow::showRegular");
185 fullScreenWindow->hide();
186 fullScreenWindow->leaveChildren();
188 QList<QWidget *> otherChildren;
189 otherChildren << progress << previousButton << nextButton;
190 takeChildren(view, otherChildren);
191 QRect geo = geometry();
192 qDebug() << "Geometry" << geo << "toolbar" << toolBar->height();
193 progress->setGeometry(0, 0, geo.width(), PROGRESS_HEIGHT);
195 previousButton->setGeometry(0,
196 geo.height() - toolBar->height() - TranslucentButton::pixels,
197 TranslucentButton::pixels, TranslucentButton::pixels);
198 nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
199 TranslucentButton::pixels, TranslucentButton::pixels);
201 previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
202 TranslucentButton::pixels, TranslucentButton::pixels);
203 nextButton->setGeometry(geo.width() - TranslucentButton::pixels,
204 toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
205 #endif // Q_WS_MAEMO_5
206 qDebug() << "previousButton geometry" << previousButton->geometry();
209 previousButton->show();
210 nextButton->flash(1500);
211 previousButton->flash(1500);
214 void MainWindow::showBig()
216 Trace t("MainWindow::showBig");
218 QList<QWidget *> otherChildren;
219 otherChildren << progress << nextButton << previousButton;
220 QRect screen = QApplication::desktop()->screenGeometry();
221 progress->setGeometry(0, 0, screen.width(), PROGRESS_HEIGHT);
222 nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
223 TranslucentButton::pixels, TranslucentButton::pixels);
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 QAction *MainWindow::addToolBarAction(const QObject *receiver,
247 addAction(QIcon(ICON_PREFIX + name + ".png"), "", receiver, member);
250 void MainWindow::showLibrary()
252 (new LibraryDialog(this))->show();
255 void MainWindow::showSettings()
257 (new SettingsWindow(this))->show();
260 void MainWindow::showInfo()
262 if (mCurrent.isValid()) {
263 (new InfoDialog(Library::instance()->book(mCurrent), this, false))->exec();
267 void MainWindow::showDevTools()
269 (new DevTools())->exec();
272 void MainWindow::showBookmarks()
274 Book *book = Library::instance()->book(mCurrent);
276 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
277 bookmarks->setWindowModality(Qt::WindowModal);
278 connect(bookmarks, SIGNAL(addBookmark()), this, SLOT(onAddBookmark()));
279 connect(bookmarks, SIGNAL(goToBookmark(int)),
280 this, SLOT(onGoToBookmark(int)));
285 void MainWindow::closeEvent(QCloseEvent *event)
287 Trace t("MainWindow::closeEvent");
288 view->setLastBookmark();
292 void MainWindow::onSettingsChanged(const QString &key)
295 if (key == "orientation") {
296 QString value = Settings::instance()->value(key).toString();
297 qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
298 if (value == "portrait") {
299 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
300 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
303 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
304 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
306 } else if (key == "lightson") {
307 bool enable = Settings::instance()->value(key, false).toBool();
308 qDebug() << "MainWindow::onSettingsChanged: lightson:" << enable;
309 killTimer(preventBlankingTimer);
311 preventBlankingTimer = startTimer(29 * 1000);
313 } else if (key == "usevolumekeys") {
314 bool value = Settings::instance()->value(key).toBool();
315 qDebug() << "MainWindow::onSettingsChanged: usevolumekeys" << value;
317 fullScreenWindow->grabZoomKeys(value);
321 #endif // Q_WS_MAEMO_5
324 void MainWindow::onPartLoadStart()
326 Trace t("MainWindow::onPartLoadStart");
328 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
332 void MainWindow::onPartLoadEnd(int index)
334 Trace t("MainWindow::onPartLoadEnd");
335 bool enablePrevious = false;
336 bool enableNext = false;
337 Book *book = Library::instance()->book(mCurrent);
340 enablePrevious = true;
342 if (index < (book->parts.size() - 1)) {
347 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
348 #endif // Q_WS_MAEMO_5
351 void MainWindow::onAddBookmark()
353 Trace t("MainWindow:onAddBookmark");
357 void MainWindow::onGoToBookmark(int index)
359 Trace t("MainWindow::onGoToBookmark");
360 Book *book = Library::instance()->book(mCurrent);
361 view->goToBookmark(book->bookmarks()[index]);
364 void MainWindow::showChapters()
366 Book *book = Library::instance()->book(mCurrent);
368 ChaptersDialog *chapters = new ChaptersDialog(book, this);
369 chapters->setWindowModality(Qt::WindowModal);
370 connect(chapters, SIGNAL(goToChapter(int)),
371 this, SLOT(onGoToChapter(int)));
376 void MainWindow::onGoToChapter(int index)
378 Trace t("MainWindow::onGoToChapter");
380 Book *book = Library::instance()->book(mCurrent);
382 int partIndex = book->partFromChapter(index);
383 if (partIndex != -1) {
384 view->goToBookmark(Book::Bookmark(partIndex, 0));
389 void MainWindow::timerEvent(QTimerEvent *event)
391 if (event->timerId() == preventBlankingTimer) {
393 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
394 MCE_REQUEST_IF, QDBusConnection::systemBus());
395 mce.call(MCE_PREVENT_BLANK_REQ);
396 #endif // Q_WS_MAEMO_5
397 qDebug() << "MainWindow::timerEvent: Prevent display blanking";
401 void MainWindow::resizeEvent(QResizeEvent *e)
403 Trace t("MainWindow::resizeEvent");
404 progress->setGeometry(QRect(0, 0, e->size().width(), PROGRESS_HEIGHT));
405 qDebug() << "Toolbar height" << toolBar->height();
407 previousButton->setGeometry(0,
408 e->size().height() - toolBar->height() - TranslucentButton::pixels,
409 TranslucentButton::pixels, TranslucentButton::pixels);
410 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
411 TranslucentButton::pixels, TranslucentButton::pixels);
413 previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
414 TranslucentButton::pixels, TranslucentButton::pixels);
415 nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
416 toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
417 #endif // Q_WS_MAEMO_5
418 qDebug() << "previousButton geometry" << previousButton->geometry();
419 previousButton->flash(1500);
420 nextButton->flash(1500);
421 QMainWindow::resizeEvent(e);
424 void MainWindow::about()
426 Dyalog *aboutDialog = new Dyalog(this);
427 aboutDialog->setWindowTitle(tr("About Dorian"));
428 QLabel *label = new QLabel(aboutDialog);
429 label->setTextFormat(Qt::RichText);
430 label->setOpenExternalLinks(true);
431 label->setText(tr("<b>Dorian %1</b><br><br>Copyright © 2010 by "
432 "Akos Polster <akos@pipacs.com><br>"
433 "Licensed under GNU General Public License, Version 3<br>"
434 "Source code: <a href='https://garage.maemo.org/projects/dorian/'>"
435 "garage.maemo.org/projects/dorian</a>").arg(DORIAN_VERSION));
436 aboutDialog->addWidget(label);
441 void MainWindow::goToNextPage()
443 nextButton->flash(1500);
444 previousButton->flash(1500);
448 void MainWindow::goToPreviousPage()
450 nextButton->flash(1500);
451 previousButton->flash(1500);
452 view->goPreviousPage();