4 #include <QApplication>
9 # include <QtMaemo5/QMaemo5InformationBox>
11 # include <mce/mode-names.h>
12 # include <mce/dbus-names.h>
13 #endif // Q_WS_MAEMO_5
18 #include "infodialog.h"
19 #include "librarydialog.h"
21 #include "mainwindow.h"
22 #include "settingswindow.h"
23 #include "bookmarksdialog.h"
25 #include "chaptersdialog.h"
26 #include "fullscreenwindow.h"
28 #include "bookfinder.h"
31 #ifdef DORIAN_TEST_MODEL
32 #include "modeltest.h"
36 # define ICON_PREFIX ":/icons/mac/"
38 # define ICON_PREFIX ":/icons/"
41 const int PROGRESS_HEIGHT = 17;
43 MainWindow::MainWindow(QWidget *parent):
44 QMainWindow(parent), view(0), preventBlankingTimer(-1)
46 Trace t("MainWindow::MainWindow");
48 setAttribute(Qt::WA_Maemo5StackedWindow, true);
50 setWindowTitle("Dorian");
52 // Central widget. Must be an intermediate, because the book view widget
53 // can be re-parented later
54 QFrame *central = new QFrame(this);
55 QVBoxLayout *layout = new QVBoxLayout(central);
57 central->setLayout(layout);
58 setCentralWidget(central);
61 view = new BookView(central);
63 layout->addWidget(view);
66 progress = new Progress(central);
69 setUnifiedTitleAndToolBarOnMac(true);
70 settings = new QDialog(this);
71 toolBar = addToolBar("controls");
72 toolBar->setMovable(false);
73 toolBar->setFloatable(false);
74 toolBar->toggleViewAction()->setVisible(false);
75 #if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)
76 toolBar->setIconSize(QSize(42, 42));
79 previousAction = addToolBarAction(view, SLOT(goPrevious()), "previous");
80 nextAction = addToolBarAction(view, SLOT(goNext()), "next");
81 chaptersAction = addToolBarAction(this, SLOT(showChapters()), "chapters");
82 bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()), "bookmarks");
85 infoAction = menuBar()->addAction(tr("Book details"));
86 connect(infoAction, SIGNAL(triggered()), this, SLOT(showInfo()));
87 libraryAction = menuBar()->addAction(tr("Library"));
88 connect(libraryAction, SIGNAL(triggered()), this, SLOT(showLibrary()));
89 settingsAction = menuBar()->addAction(tr("Settings"));
90 connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
91 devToolsAction = menuBar()->addAction(tr("Developer"));
92 connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
94 infoAction = addToolBarAction(this, SLOT(showInfo()), "document-properties");
95 libraryAction = addToolBarAction(this, SLOT(showLibrary()),
96 "system-file-manager");
97 settingsAction = addToolBarAction(this, SLOT(showSettings()),
98 "preferences-system");
99 devToolsAction = addToolBarAction(this, SLOT(showDevTools()), "developer");
100 #endif // Q_WS_MAEMO_5
102 QFrame *frame = new QFrame(toolBar);
103 frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
104 toolBar->addWidget(frame);
106 fullScreenAction = addToolBarAction(this, SLOT(showBig()), "view-fullscreen");
108 // Handle model changes
109 connect(Library::instance(), SIGNAL(nowReadingChanged()),
110 this, SLOT(onCurrentBookChanged()));
112 // Load book on command line, or load last read book, or load default book
113 Library *library = Library::instance();
114 if (QCoreApplication::arguments().size() == 2) {
115 QString path = QCoreApplication::arguments()[1];
117 QModelIndex index = library->find(path);
118 if (index.isValid()) {
119 library->setNowReading(index);
123 QModelIndex index = library->nowReading();
124 if (index.isValid()) {
125 library->setNowReading(index);
128 if (!library->rowCount()) {
129 library->add(":/books/2 B R 0 2 B.epub");
131 library->setNowReading(library->index(0));
135 // Handle settings changes
136 Settings *settings = Settings::instance();
137 connect(settings, SIGNAL(valueChanged(const QString &)),
138 this, SLOT(onSettingsChanged(const QString &)));
139 settings->setValue("orientation", settings->value("orientation"));
140 settings->setValue("lightson", settings->value("lightson"));
142 // Handle loading chapters
143 connect(view, SIGNAL(chapterLoadStart(int)),
144 this, SLOT(onChapterLoadStart()));
145 connect(view, SIGNAL(chapterLoadEnd(int)),
146 this, SLOT(onChapterLoadEnd(int)));
149 connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
151 // Shadow window for full screen
152 fullScreenWindow = new FullScreenWindow(this);
153 connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
155 // Create thread for finding books in directories
156 bookFinder = new BookFinder();
157 connect(bookFinder, SIGNAL(add(const QString &)),
158 library, SLOT(add(const QString &)));
159 connect(bookFinder, SIGNAL(remove(const QString &)),
160 library, SLOT(remove(const QString &)));
161 bookFinder->moveToThread(&bookFinderThread);
162 bookFinderThread.start();
164 #ifdef DORIAN_TEST_MODEL
165 (void)new ModelTest(Library::instance(), this);
169 MainWindow::~MainWindow()
171 bookFinderThread.quit();
172 bookFinderThread.wait();
176 void MainWindow::onCurrentBookChanged()
178 setCurrentBook(Library::instance()->nowReading());
181 void MainWindow::showRegular()
183 Trace t("MainWindow::showRegular");
184 fullScreenWindow->hide();
185 fullScreenWindow->leaveChild();
186 view->setParent(centralWidget());
187 progress->setParent(centralWidget());
188 progress->setGeometry(0, 0, geometry().width(), PROGRESS_HEIGHT);
189 centralWidget()->layout()->addWidget(view);
193 void MainWindow::showBig()
195 Trace t("MainWindow::showBig");
196 centralWidget()->layout()->removeWidget(view);
197 progress->setParent(fullScreenWindow);
198 progress->setGeometry(0, 0, QApplication::desktop()->screenGeometry().width(),
200 fullScreenWindow->takeChild(view);
201 fullScreenWindow->showFullScreen();
205 void MainWindow::setCurrentBook(const QModelIndex ¤t)
208 Book *book = Library::instance()->book(current);
210 setWindowTitle(book? book->shortName(): tr("Dorian"));
213 QAction *MainWindow::addToolBarAction(const QObject *receiver,
218 addAction(QIcon(ICON_PREFIX + name + ".png"), "", receiver, member);
221 void MainWindow::showLibrary()
223 (new LibraryDialog(this))->show();
226 void MainWindow::showSettings()
228 (new SettingsWindow(this))->show();
231 void MainWindow::showInfo()
233 if (mCurrent.isValid()) {
234 (new InfoDialog(Library::instance()->book(mCurrent), this))->exec();
238 void MainWindow::showDevTools()
240 (new DevTools())->exec();
243 void MainWindow::showBookmarks()
245 Book *book = Library::instance()->book(mCurrent);
247 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
248 bookmarks->setWindowModality(Qt::WindowModal);
249 connect(bookmarks, SIGNAL(addBookmark()), this, SLOT(onAddBookmark()));
250 connect(bookmarks, SIGNAL(goToBookmark(int)),
251 this, SLOT(onGoToBookmark(int)));
256 void MainWindow::closeEvent(QCloseEvent *event)
258 Trace t("MainWindow::closeEvent");
259 view->setLastBookmark();
263 void MainWindow::onSettingsChanged(const QString &key)
266 if (key == "orientation") {
267 QString value = Settings::instance()->value(key).toString();
268 Trace::trace(QString("MainWindow::onSettingsChanged: orientation %1").
270 if (value == "portrait") {
271 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
272 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
275 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
276 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
278 } else if (key == "lightson") {
279 bool enable = Settings::instance()->value(key, false).toBool();
280 Trace::trace(QString("MainWindow::onSettingsChanged: lightson: %1").
282 killTimer(preventBlankingTimer);
284 preventBlankingTimer = startTimer(29 * 1000);
289 #endif // Q_WS_MAEMO_5
292 void MainWindow::onPartLoadStart()
294 Trace t("MainWindow::onPartLoadStart");
296 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
300 void MainWindow::onPartLoadEnd(int index)
302 Trace t("MainWindow::onPartLoadEnd");
303 bool enablePrevious = false;
304 bool enableNext = false;
305 Book *book = Library::instance()->book(mCurrent);
308 enablePrevious = true;
310 if (index < (book->parts.size() - 1)) {
315 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
316 previousAction->setIcon(QIcon(enablePrevious?
317 ":/icons/previous.png" : ":/icons/previous-disabled.png"));
318 nextAction->setIcon(QIcon(enableNext?
319 ":/icons/next.png": ":/icons/next-disabled.png"));
320 #endif // Q_WS_MAEMO_5
321 previousAction->setEnabled(enablePrevious);
322 nextAction->setEnabled(enableNext);
325 void MainWindow::onAddBookmark()
327 Trace t("MainWindow:onAddBookmark");
331 void MainWindow::onGoToBookmark(int index)
333 Trace t("MainWindow::onGoToBookmark");
334 Book *book = Library::instance()->book(mCurrent);
335 view->goToBookmark(book->bookmarks()[index]);
338 void MainWindow::showChapters()
340 Book *book = Library::instance()->book(mCurrent);
342 ChaptersDialog *chapters = new ChaptersDialog(book, this);
343 chapters->setWindowModality(Qt::WindowModal);
344 connect(chapters, SIGNAL(goToChapter(int)),
345 this, SLOT(onGoToChapter(int)));
350 void MainWindow::onGoToChapter(int index)
352 Trace t("MainWindow::onGoToChapter");
354 Book *book = Library::instance()->book(mCurrent);
356 int partIndex = book->partFromChapter(index);
357 if (partIndex != -1) {
358 view->goToBookmark(Book::Bookmark(partIndex, 0));
363 void MainWindow::timerEvent(QTimerEvent *event)
365 if (event->timerId() == preventBlankingTimer) {
367 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
368 MCE_REQUEST_IF, QDBusConnection::systemBus());
369 mce.call(MCE_PREVENT_BLANK_REQ);
370 #endif // Q_WS_MAEMO_5
371 Trace::trace("MainWindow::timerEvent: Prevent display blanking");
375 void MainWindow::resizeEvent(QResizeEvent *e)
377 progress->setGeometry(QRect(0, 0, e->size().width(), PROGRESS_HEIGHT));
378 QMainWindow::resizeEvent(e);