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 QMainWindow(parent), view(0), preventBlankingTimer(-1)
49 Trace t("MainWindow::MainWindow");
51 setAttribute(Qt::WA_Maemo5StackedWindow, true);
54 setWindowTitle("Dorian");
56 // Central widget. Must be an intermediate, because the book view widget
57 // can be re-parented later
58 QFrame *central = new QFrame(this);
59 QVBoxLayout *layout = new QVBoxLayout(central);
61 central->setLayout(layout);
62 setCentralWidget(central);
65 view = new BookView(central);
67 layout->addWidget(view);
70 progress = new Progress(central);
73 setUnifiedTitleAndToolBarOnMac(true);
74 settings = new QDialog(this);
75 toolBar = addToolBar("controls");
76 toolBar->setMovable(false);
77 toolBar->setFloatable(false);
78 toolBar->toggleViewAction()->setVisible(false);
79 #if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)
80 toolBar->setIconSize(QSize(42, 42));
83 previousAction = addToolBarAction(view, SLOT(goPrevious()), "previous");
84 nextAction = addToolBarAction(view, SLOT(goNext()), "next");
85 chaptersAction = addToolBarAction(this, SLOT(showChapters()), "chapters");
86 bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()), "bookmarks");
89 infoAction = menuBar()->addAction(tr("Book details"));
90 connect(infoAction, SIGNAL(triggered()), this, SLOT(showInfo()));
91 libraryAction = menuBar()->addAction(tr("Library"));
92 connect(libraryAction, SIGNAL(triggered()), this, SLOT(showLibrary()));
93 settingsAction = menuBar()->addAction(tr("Settings"));
94 connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
95 devToolsAction = menuBar()->addAction(tr("Developer"));
96 connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
98 infoAction = addToolBarAction(this, SLOT(showInfo()), "document-properties");
99 libraryAction = addToolBarAction(this, SLOT(showLibrary()),
100 "system-file-manager");
101 settingsAction = addToolBarAction(this, SLOT(showSettings()),
102 "preferences-system");
103 devToolsAction = addToolBarAction(this, SLOT(showDevTools()), "developer");
104 #endif // Q_WS_MAEMO_5
106 QFrame *frame = new QFrame(toolBar);
107 frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
108 toolBar->addWidget(frame);
110 fullScreenAction = addToolBarAction(this, SLOT(showBig()), "view-fullscreen");
112 // Handle model changes
113 connect(Library::instance(), SIGNAL(nowReadingChanged()),
114 this, SLOT(onCurrentBookChanged()));
116 // Load book on command line, or load last read book, or load default book
117 Library *library = Library::instance();
118 if (QCoreApplication::arguments().size() == 2) {
119 QString path = QCoreApplication::arguments()[1];
121 QModelIndex index = library->find(path);
122 if (index.isValid()) {
123 library->setNowReading(index);
127 QModelIndex index = library->nowReading();
128 if (index.isValid()) {
129 library->setNowReading(index);
132 if (!library->rowCount()) {
133 library->add(":/books/2 B R 0 2 B.epub");
135 library->setNowReading(library->index(0));
139 // Handle settings changes
140 Settings *settings = Settings::instance();
141 connect(settings, SIGNAL(valueChanged(const QString &)),
142 this, SLOT(onSettingsChanged(const QString &)));
143 settings->setValue("orientation", settings->value("orientation"));
144 settings->setValue("lightson", settings->value("lightson"));
146 // Handle loading chapters
147 connect(view, SIGNAL(chapterLoadStart(int)),
148 this, SLOT(onChapterLoadStart()));
149 connect(view, SIGNAL(chapterLoadEnd(int)),
150 this, SLOT(onChapterLoadEnd(int)));
153 connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
155 // Shadow window for full screen
156 fullScreenWindow = new FullScreenWindow(this);
157 connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
159 // Create thread for finding books in directories
160 bookFinder = new BookFinder();
161 connect(bookFinder, SIGNAL(add(const QString &)),
162 library, SLOT(add(const QString &)));
163 connect(bookFinder, SIGNAL(remove(const QString &)),
164 library, SLOT(remove(const QString &)));
165 bookFinder->moveToThread(&bookFinderThread);
166 bookFinderThread.start();
168 #ifdef DORIAN_TEST_MODEL
169 (void)new ModelTest(Library::instance(), this);
173 MainWindow::~MainWindow()
175 bookFinderThread.quit();
176 bookFinderThread.wait();
180 void MainWindow::onCurrentBookChanged()
182 setCurrentBook(Library::instance()->nowReading());
185 void MainWindow::showRegular()
187 Trace t("MainWindow::showRegular");
188 fullScreenWindow->hide();
189 fullScreenWindow->leaveChild();
190 view->setParent(centralWidget());
191 progress->setParent(centralWidget());
192 progress->setGeometry(0, 0, geometry().width(), PROGRESS_HEIGHT);
193 centralWidget()->layout()->addWidget(view);
197 void MainWindow::showBig()
199 Trace t("MainWindow::showBig");
200 centralWidget()->layout()->removeWidget(view);
201 progress->setParent(fullScreenWindow);
202 progress->setGeometry(0, 0, QApplication::desktop()->screenGeometry().width(),
204 fullScreenWindow->takeChild(view);
205 fullScreenWindow->showFullScreen();
209 void MainWindow::setCurrentBook(const QModelIndex ¤t)
212 Book *book = Library::instance()->book(current);
214 setWindowTitle(book? book->shortName(): tr("Dorian"));
217 QAction *MainWindow::addToolBarAction(const QObject *receiver,
222 addAction(QIcon(ICON_PREFIX + name + ".png"), "", receiver, member);
225 void MainWindow::showLibrary()
227 (new LibraryDialog(this))->show();
230 void MainWindow::showSettings()
232 (new SettingsWindow(this))->show();
235 void MainWindow::showInfo()
237 if (mCurrent.isValid()) {
238 (new InfoDialog(Library::instance()->book(mCurrent), this))->exec();
242 void MainWindow::showDevTools()
244 (new DevTools())->exec();
247 void MainWindow::showBookmarks()
249 Book *book = Library::instance()->book(mCurrent);
251 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
252 bookmarks->setWindowModality(Qt::WindowModal);
253 connect(bookmarks, SIGNAL(addBookmark()), this, SLOT(onAddBookmark()));
254 connect(bookmarks, SIGNAL(goToBookmark(int)),
255 this, SLOT(onGoToBookmark(int)));
260 void MainWindow::closeEvent(QCloseEvent *event)
262 Trace t("MainWindow::closeEvent");
263 view->setLastBookmark();
267 void MainWindow::onSettingsChanged(const QString &key)
270 if (key == "orientation") {
271 QString value = Settings::instance()->value(key).toString();
272 Trace::trace(QString("MainWindow::onSettingsChanged: orientation %1").
274 if (value == "portrait") {
275 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
276 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
279 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
280 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
282 } else if (key == "lightson") {
283 bool enable = Settings::instance()->value(key, false).toBool();
284 Trace::trace(QString("MainWindow::onSettingsChanged: lightson: %1").
286 killTimer(preventBlankingTimer);
288 preventBlankingTimer = startTimer(29 * 1000);
293 #endif // Q_WS_MAEMO_5
296 void MainWindow::onPartLoadStart()
298 Trace t("MainWindow::onPartLoadStart");
300 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
304 void MainWindow::onPartLoadEnd(int index)
306 Trace t("MainWindow::onPartLoadEnd");
307 bool enablePrevious = false;
308 bool enableNext = false;
309 Book *book = Library::instance()->book(mCurrent);
312 enablePrevious = true;
314 if (index < (book->parts.size() - 1)) {
319 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
320 previousAction->setIcon(QIcon(enablePrevious?
321 ":/icons/previous.png" : ":/icons/previous-disabled.png"));
322 nextAction->setIcon(QIcon(enableNext?
323 ":/icons/next.png": ":/icons/next-disabled.png"));
324 #endif // Q_WS_MAEMO_5
325 previousAction->setEnabled(enablePrevious);
326 nextAction->setEnabled(enableNext);
329 void MainWindow::onAddBookmark()
331 Trace t("MainWindow:onAddBookmark");
335 void MainWindow::onGoToBookmark(int index)
337 Trace t("MainWindow::onGoToBookmark");
338 Book *book = Library::instance()->book(mCurrent);
339 view->goToBookmark(book->bookmarks()[index]);
342 void MainWindow::showChapters()
344 Book *book = Library::instance()->book(mCurrent);
346 ChaptersDialog *chapters = new ChaptersDialog(book, this);
347 chapters->setWindowModality(Qt::WindowModal);
348 connect(chapters, SIGNAL(goToChapter(int)),
349 this, SLOT(onGoToChapter(int)));
354 void MainWindow::onGoToChapter(int index)
356 Trace t("MainWindow::onGoToChapter");
358 Book *book = Library::instance()->book(mCurrent);
360 int partIndex = book->partFromChapter(index);
361 if (partIndex != -1) {
362 view->goToBookmark(Book::Bookmark(partIndex, 0));
367 void MainWindow::timerEvent(QTimerEvent *event)
369 if (event->timerId() == preventBlankingTimer) {
371 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
372 MCE_REQUEST_IF, QDBusConnection::systemBus());
373 mce.call(MCE_PREVENT_BLANK_REQ);
374 #endif // Q_WS_MAEMO_5
375 Trace::trace("MainWindow::timerEvent: Prevent display blanking");
379 void MainWindow::resizeEvent(QResizeEvent *e)
381 progress->setGeometry(QRect(0, 0, e->size().width(), PROGRESS_HEIGHT));
382 QMainWindow::resizeEvent(e);
385 void MainWindow::grabZoomKeys()
389 qCritical() << "Can't grab keys unless we have a window id";
392 unsigned long val = 1;
393 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
395 qCritical() << "Unable to obtain _HILDON_ZOOM_KEY_ATOM";
398 XChangeProperty(QX11Info::display(),
404 reinterpret_cast<unsigned char *>(&val),
406 #endif // Q_WS_MAEMO_5