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"
35 #ifdef DORIAN_TEST_MODEL
36 #include "modeltest.h"
40 # define ICON_PREFIX ":/icons/mac/"
42 # define ICON_PREFIX ":/icons/"
45 const int PROGRESS_HEIGHT = 17;
46 const char *DORIAN_VERSION =
47 #include "pkg/version.txt"
50 MainWindow::MainWindow(QWidget *parent):
51 BookWindow(parent), view(0), preventBlankingTimer(-1)
53 Trace t("MainWindow::MainWindow");
55 setAttribute(Qt::WA_Maemo5StackedWindow, true);
57 setWindowTitle("Dorian");
59 // Central widget. Must be an intermediate, because the book view widget
60 // can be re-parented later
61 QFrame *central = new QFrame(this);
62 QVBoxLayout *layout = new QVBoxLayout(central);
64 central->setLayout(layout);
65 setCentralWidget(central);
68 view = new BookView(central);
70 layout->addWidget(view);
73 progress = new Progress(central);
76 setUnifiedTitleAndToolBarOnMac(true);
77 settings = new QDialog(this);
78 toolBar = addToolBar("controls");
79 toolBar->setMovable(false);
80 toolBar->setFloatable(false);
81 toolBar->toggleViewAction()->setVisible(false);
82 #if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)
83 toolBar->setIconSize(QSize(42, 42));
86 previousAction = addToolBarAction(view, SLOT(goPrevious()), "previous");
87 nextAction = addToolBarAction(view, SLOT(goNext()), "next");
88 chaptersAction = addToolBarAction(this, SLOT(showChapters()), "chapters");
89 bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()), "bookmarks");
92 infoAction = menuBar()->addAction(tr("Book details"));
93 connect(infoAction, SIGNAL(triggered()), this, SLOT(showInfo()));
94 libraryAction = menuBar()->addAction(tr("Library"));
95 connect(libraryAction, SIGNAL(triggered()), this, SLOT(showLibrary()));
96 settingsAction = menuBar()->addAction(tr("Settings"));
97 connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
98 devToolsAction = menuBar()->addAction(tr("Developer"));
99 connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
100 QAction *aboutAction = menuBar()->addAction(tr("About"));
101 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
103 infoAction = addToolBarAction(this, SLOT(showInfo()), "document-properties");
104 libraryAction = addToolBarAction(this, SLOT(showLibrary()),
105 "system-file-manager");
106 settingsAction = addToolBarAction(this, SLOT(showSettings()),
107 "preferences-system");
108 devToolsAction = addToolBarAction(this, SLOT(showDevTools()), "developer");
109 addToolBarAction(this, SLOT(about()), "about");
110 #endif // Q_WS_MAEMO_5
112 QFrame *frame = new QFrame(toolBar);
113 frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
114 toolBar->addWidget(frame);
116 fullScreenAction = addToolBarAction(this, SLOT(showBig()), "view-fullscreen");
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 #ifdef DORIAN_TEST_MODEL
165 (void)new ModelTest(Library::instance(), this);
169 MainWindow::~MainWindow()
173 void MainWindow::onCurrentBookChanged()
175 setCurrentBook(Library::instance()->nowReading());
178 void MainWindow::showRegular()
180 Trace t("MainWindow::showRegular");
181 fullScreenWindow->hide();
182 fullScreenWindow->leaveChildren();
183 progress->setGeometry(0, 0, geometry().width(), PROGRESS_HEIGHT);
184 QList<QWidget *> otherChildren;
185 otherChildren.append(progress);
186 takeChildren(view, otherChildren);
190 void MainWindow::showBig()
192 Trace t("MainWindow::showBig");
194 QList<QWidget *> otherChildren;
195 otherChildren.append(progress);
196 progress->setGeometry(0, 0, QApplication::desktop()->screenGeometry().width(),
198 fullScreenWindow->takeChildren(view, otherChildren);
199 fullScreenWindow->showFullScreen();
203 void MainWindow::setCurrentBook(const QModelIndex ¤t)
206 Book *book = Library::instance()->book(current);
208 setWindowTitle(book? book->shortName(): tr("Dorian"));
211 QAction *MainWindow::addToolBarAction(const QObject *receiver,
216 addAction(QIcon(ICON_PREFIX + name + ".png"), "", receiver, member);
219 void MainWindow::showLibrary()
221 (new LibraryDialog(this))->show();
224 void MainWindow::showSettings()
226 (new SettingsWindow(this))->show();
229 void MainWindow::showInfo()
231 if (mCurrent.isValid()) {
232 (new InfoDialog(Library::instance()->book(mCurrent), this))->exec();
236 void MainWindow::showDevTools()
238 (new DevTools())->exec();
241 void MainWindow::showBookmarks()
243 Book *book = Library::instance()->book(mCurrent);
245 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
246 bookmarks->setWindowModality(Qt::WindowModal);
247 connect(bookmarks, SIGNAL(addBookmark()), this, SLOT(onAddBookmark()));
248 connect(bookmarks, SIGNAL(goToBookmark(int)),
249 this, SLOT(onGoToBookmark(int)));
254 void MainWindow::closeEvent(QCloseEvent *event)
256 Trace t("MainWindow::closeEvent");
257 view->setLastBookmark();
261 void MainWindow::onSettingsChanged(const QString &key)
264 if (key == "orientation") {
265 QString value = Settings::instance()->value(key).toString();
266 qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
267 if (value == "portrait") {
268 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
269 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
272 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
273 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
275 } else if (key == "lightson") {
276 bool enable = Settings::instance()->value(key, false).toBool();
277 qDebug() << "MainWindow::onSettingsChanged: lightson:" << enable;
278 killTimer(preventBlankingTimer);
280 preventBlankingTimer = startTimer(29 * 1000);
282 } else if (key == "usevolumekeys") {
283 bool value = Settings::instance()->value(key).toBool();
284 qDebug() << "MainWindow::onSettingsChanged: usevolumekeys" << value;
286 fullScreenWindow->grabZoomKeys(value);
290 #endif // Q_WS_MAEMO_5
293 void MainWindow::onPartLoadStart()
295 Trace t("MainWindow::onPartLoadStart");
297 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
301 void MainWindow::onPartLoadEnd(int index)
303 Trace t("MainWindow::onPartLoadEnd");
304 bool enablePrevious = false;
305 bool enableNext = false;
306 Book *book = Library::instance()->book(mCurrent);
309 enablePrevious = true;
311 if (index < (book->parts.size() - 1)) {
316 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
317 previousAction->setIcon(QIcon(enablePrevious?
318 ":/icons/previous.png" : ":/icons/previous-disabled.png"));
319 nextAction->setIcon(QIcon(enableNext?
320 ":/icons/next.png": ":/icons/next-disabled.png"));
321 #endif // Q_WS_MAEMO_5
322 previousAction->setEnabled(enablePrevious);
323 nextAction->setEnabled(enableNext);
326 void MainWindow::onAddBookmark()
328 Trace t("MainWindow:onAddBookmark");
332 void MainWindow::onGoToBookmark(int index)
334 Trace t("MainWindow::onGoToBookmark");
335 Book *book = Library::instance()->book(mCurrent);
336 view->goToBookmark(book->bookmarks()[index]);
339 void MainWindow::showChapters()
341 Book *book = Library::instance()->book(mCurrent);
343 ChaptersDialog *chapters = new ChaptersDialog(book, this);
344 chapters->setWindowModality(Qt::WindowModal);
345 connect(chapters, SIGNAL(goToChapter(int)),
346 this, SLOT(onGoToChapter(int)));
351 void MainWindow::onGoToChapter(int index)
353 Trace t("MainWindow::onGoToChapter");
355 Book *book = Library::instance()->book(mCurrent);
357 int partIndex = book->partFromChapter(index);
358 if (partIndex != -1) {
359 view->goToBookmark(Book::Bookmark(partIndex, 0));
364 void MainWindow::timerEvent(QTimerEvent *event)
366 if (event->timerId() == preventBlankingTimer) {
368 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
369 MCE_REQUEST_IF, QDBusConnection::systemBus());
370 mce.call(MCE_PREVENT_BLANK_REQ);
371 #endif // Q_WS_MAEMO_5
372 qDebug() << "MainWindow::timerEvent: Prevent display blanking";
376 void MainWindow::resizeEvent(QResizeEvent *e)
378 progress->setGeometry(QRect(0, 0, e->size().width(), PROGRESS_HEIGHT));
379 QMainWindow::resizeEvent(e);
382 void MainWindow::about()
384 Dyalog *aboutDialog = new Dyalog(this);
385 aboutDialog->setWindowTitle(tr("About Dorian"));
386 QLabel *label = new QLabel(aboutDialog);
387 label->setTextFormat(Qt::RichText);
388 label->setOpenExternalLinks(true);
389 label->setText(tr("<b>Dorian %1</b><br><br>Copyright © 2010 by "
390 "Akos Polster <akos@pipacs.com><br>"
391 "Licensed under GNU General Public License, Version 3<br>"
392 "Source code: <a href='https://garage.maemo.org/projects/dorian/'>"
393 "garage.maemo.org/projects/dorian</a>").arg(DORIAN_VERSION));
394 aboutDialog->addWidget(label);