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 #ifdef DORIAN_TEST_MODEL
35 #include "modeltest.h"
39 # define ICON_PREFIX ":/icons/mac/"
41 # define ICON_PREFIX ":/icons/"
44 const int PROGRESS_HEIGHT = 17;
46 MainWindow::MainWindow(QWidget *parent):
47 BookWindow(parent), view(0), preventBlankingTimer(-1)
49 Trace t("MainWindow::MainWindow");
51 setAttribute(Qt::WA_Maemo5StackedWindow, true);
53 setWindowTitle("Dorian");
55 // Central widget. Must be an intermediate, because the book view widget
56 // can be re-parented later
57 QFrame *central = new QFrame(this);
58 QVBoxLayout *layout = new QVBoxLayout(central);
60 central->setLayout(layout);
61 setCentralWidget(central);
64 view = new BookView(central);
66 layout->addWidget(view);
69 progress = new Progress(central);
72 setUnifiedTitleAndToolBarOnMac(true);
73 settings = new QDialog(this);
74 toolBar = addToolBar("controls");
75 toolBar->setMovable(false);
76 toolBar->setFloatable(false);
77 toolBar->toggleViewAction()->setVisible(false);
78 #if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)
79 toolBar->setIconSize(QSize(42, 42));
82 previousAction = addToolBarAction(view, SLOT(goPrevious()), "previous");
83 nextAction = addToolBarAction(view, SLOT(goNext()), "next");
84 chaptersAction = addToolBarAction(this, SLOT(showChapters()), "chapters");
85 bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()), "bookmarks");
88 infoAction = menuBar()->addAction(tr("Book details"));
89 connect(infoAction, SIGNAL(triggered()), this, SLOT(showInfo()));
90 libraryAction = menuBar()->addAction(tr("Library"));
91 connect(libraryAction, SIGNAL(triggered()), this, SLOT(showLibrary()));
92 settingsAction = menuBar()->addAction(tr("Settings"));
93 connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
94 devToolsAction = menuBar()->addAction(tr("Developer"));
95 connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
97 infoAction = addToolBarAction(this, SLOT(showInfo()), "document-properties");
98 libraryAction = addToolBarAction(this, SLOT(showLibrary()),
99 "system-file-manager");
100 settingsAction = addToolBarAction(this, SLOT(showSettings()),
101 "preferences-system");
102 devToolsAction = addToolBarAction(this, SLOT(showDevTools()), "developer");
103 #endif // Q_WS_MAEMO_5
105 QFrame *frame = new QFrame(toolBar);
106 frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
107 toolBar->addWidget(frame);
109 fullScreenAction = addToolBarAction(this, SLOT(showBig()), "view-fullscreen");
111 // Handle model changes
112 connect(Library::instance(), SIGNAL(nowReadingChanged()),
113 this, SLOT(onCurrentBookChanged()));
115 // Load book on command line, or load last read book, or load default book
116 Library *library = Library::instance();
117 if (QCoreApplication::arguments().size() == 2) {
118 QString path = QCoreApplication::arguments()[1];
120 QModelIndex index = library->find(path);
121 if (index.isValid()) {
122 library->setNowReading(index);
126 QModelIndex index = library->nowReading();
127 if (index.isValid()) {
128 library->setNowReading(index);
131 if (!library->rowCount()) {
132 library->add(":/books/2 B R 0 2 B.epub");
134 library->setNowReading(library->index(0));
138 // Handle loading book parts
139 connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
140 connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
143 connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
145 // Shadow window for full screen
146 fullScreenWindow = new FullScreenWindow(this);
147 connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
149 // Create thread for finding books in directories
150 bookFinder = new BookFinder();
151 connect(bookFinder, SIGNAL(add(const QString &)),
152 library, SLOT(add(const QString &)));
153 connect(bookFinder, SIGNAL(remove(const QString &)),
154 library, SLOT(remove(const QString &)));
155 bookFinder->moveToThread(&bookFinderThread);
156 bookFinderThread.start();
158 // Handle settings changes
159 Settings *settings = Settings::instance();
160 connect(settings, SIGNAL(valueChanged(const QString &)),
161 this, SLOT(onSettingsChanged(const QString &)));
162 settings->setValue("orientation", settings->value("orientation"));
163 settings->setValue("lightson", settings->value("lightson"));
164 settings->setValue("usevolumekeys", settings->value("usevolumekeys"));
166 #ifdef DORIAN_TEST_MODEL
167 (void)new ModelTest(Library::instance(), this);
171 MainWindow::~MainWindow()
173 bookFinderThread.quit();
174 bookFinderThread.wait();
178 void MainWindow::onCurrentBookChanged()
180 setCurrentBook(Library::instance()->nowReading());
183 void MainWindow::showRegular()
185 Trace t("MainWindow::showRegular");
186 fullScreenWindow->hide();
187 fullScreenWindow->leaveChildren();
188 progress->setGeometry(0, 0, geometry().width(), PROGRESS_HEIGHT);
189 QList<QWidget *> otherChildren;
190 otherChildren.append(progress);
191 takeChildren(view, otherChildren);
195 void MainWindow::showBig()
197 Trace t("MainWindow::showBig");
199 QList<QWidget *> otherChildren;
200 otherChildren.append(progress);
201 progress->setGeometry(0, 0, QApplication::desktop()->screenGeometry().width(),
203 fullScreenWindow->takeChildren(view, otherChildren);
204 fullScreenWindow->showFullScreen();
208 void MainWindow::setCurrentBook(const QModelIndex ¤t)
211 Book *book = Library::instance()->book(current);
213 setWindowTitle(book? book->shortName(): tr("Dorian"));
216 QAction *MainWindow::addToolBarAction(const QObject *receiver,
221 addAction(QIcon(ICON_PREFIX + name + ".png"), "", receiver, member);
224 void MainWindow::showLibrary()
226 (new LibraryDialog(this))->show();
229 void MainWindow::showSettings()
231 (new SettingsWindow(this))->show();
234 void MainWindow::showInfo()
236 if (mCurrent.isValid()) {
237 (new InfoDialog(Library::instance()->book(mCurrent), this))->exec();
241 void MainWindow::showDevTools()
243 (new DevTools())->exec();
246 void MainWindow::showBookmarks()
248 Book *book = Library::instance()->book(mCurrent);
250 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
251 bookmarks->setWindowModality(Qt::WindowModal);
252 connect(bookmarks, SIGNAL(addBookmark()), this, SLOT(onAddBookmark()));
253 connect(bookmarks, SIGNAL(goToBookmark(int)),
254 this, SLOT(onGoToBookmark(int)));
259 void MainWindow::closeEvent(QCloseEvent *event)
261 Trace t("MainWindow::closeEvent");
262 view->setLastBookmark();
266 void MainWindow::onSettingsChanged(const QString &key)
269 if (key == "orientation") {
270 QString value = Settings::instance()->value(key).toString();
271 Trace::trace(QString("MainWindow::onSettingsChanged: orientation %1").
273 if (value == "portrait") {
274 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
275 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
278 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
279 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
281 } else if (key == "lightson") {
282 bool enable = Settings::instance()->value(key, false).toBool();
283 Trace::trace(QString("MainWindow::onSettingsChanged: lightson: %1").
285 killTimer(preventBlankingTimer);
287 preventBlankingTimer = startTimer(29 * 1000);
289 } else if (key == "usevolumekeys") {
290 bool value = Settings::instance()->value(key).toBool();
291 Trace::trace(QString("MainWindow::onSettingsChanged: usevolumekeys %1").
294 fullScreenWindow->grabZoomKeys(value);
298 #endif // Q_WS_MAEMO_5
301 void MainWindow::onPartLoadStart()
303 Trace t("MainWindow::onPartLoadStart");
305 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
309 void MainWindow::onPartLoadEnd(int index)
311 Trace t("MainWindow::onPartLoadEnd");
312 bool enablePrevious = false;
313 bool enableNext = false;
314 Book *book = Library::instance()->book(mCurrent);
317 enablePrevious = true;
319 if (index < (book->parts.size() - 1)) {
324 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
325 previousAction->setIcon(QIcon(enablePrevious?
326 ":/icons/previous.png" : ":/icons/previous-disabled.png"));
327 nextAction->setIcon(QIcon(enableNext?
328 ":/icons/next.png": ":/icons/next-disabled.png"));
329 #endif // Q_WS_MAEMO_5
330 previousAction->setEnabled(enablePrevious);
331 nextAction->setEnabled(enableNext);
334 void MainWindow::onAddBookmark()
336 Trace t("MainWindow:onAddBookmark");
340 void MainWindow::onGoToBookmark(int index)
342 Trace t("MainWindow::onGoToBookmark");
343 Book *book = Library::instance()->book(mCurrent);
344 view->goToBookmark(book->bookmarks()[index]);
347 void MainWindow::showChapters()
349 Book *book = Library::instance()->book(mCurrent);
351 ChaptersDialog *chapters = new ChaptersDialog(book, this);
352 chapters->setWindowModality(Qt::WindowModal);
353 connect(chapters, SIGNAL(goToChapter(int)),
354 this, SLOT(onGoToChapter(int)));
359 void MainWindow::onGoToChapter(int index)
361 Trace t("MainWindow::onGoToChapter");
363 Book *book = Library::instance()->book(mCurrent);
365 int partIndex = book->partFromChapter(index);
366 if (partIndex != -1) {
367 view->goToBookmark(Book::Bookmark(partIndex, 0));
372 void MainWindow::timerEvent(QTimerEvent *event)
374 if (event->timerId() == preventBlankingTimer) {
376 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
377 MCE_REQUEST_IF, QDBusConnection::systemBus());
378 mce.call(MCE_PREVENT_BLANK_REQ);
379 #endif // Q_WS_MAEMO_5
380 Trace::trace("MainWindow::timerEvent: Prevent display blanking");
384 void MainWindow::resizeEvent(QResizeEvent *e)
386 progress->setGeometry(QRect(0, 0, e->size().width(), PROGRESS_HEIGHT));
387 QMainWindow::resizeEvent(e);