4 #include <QApplication>
8 # include <QtMaemo5/QMaemo5InformationBox>
10 # include <mce/mode-names.h>
11 # include <mce/dbus-names.h>
12 #endif // Q_WS_MAEMO_5
17 #include "infodialog.h"
18 #include "librarydialog.h"
20 #include "mainwindow.h"
21 #include "settingswindow.h"
22 #include "bookmarksdialog.h"
24 #include "chaptersdialog.h"
25 #include "fullscreenwindow.h"
28 #ifdef DORIAN_TEST_MODEL
29 #include "modeltest.h"
33 # define ICON_PREFIX ":/icons/mac/"
35 # define ICON_PREFIX ":/icons/"
38 MainWindow::MainWindow(QWidget *parent):
39 QMainWindow(parent), view(0), preventBlankingTimer(-1)
42 setAttribute(Qt::WA_Maemo5StackedWindow, true);
44 setWindowTitle("Dorian");
46 // Central widget. Must be an intermediate because of reparenting the book view
47 QFrame *central = new QFrame(this);
48 QVBoxLayout *layout = new QVBoxLayout(central);
50 central->setLayout(layout);
51 setCentralWidget(central);
54 view = new BookView(central);
56 layout->addWidget(view);
59 setUnifiedTitleAndToolBarOnMac(true);
60 settings = new QDialog(this);
61 toolBar = addToolBar("controls");
62 toolBar->setMovable(false);
63 toolBar->setFloatable(false);
64 toolBar->toggleViewAction()->setVisible(false);
65 #if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)
66 toolBar->setIconSize(QSize(42, 42));
69 previousAction = addToolBarAction(view, SLOT(goPrevious()), "previous");
70 nextAction = addToolBarAction(view, SLOT(goNext()), "next");
71 chaptersAction = addToolBarAction(this, SLOT(showChapters()), "chapters");
72 bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()), "bookmarks");
75 infoAction = menuBar()->addAction(tr("Book details"));
76 connect(infoAction, SIGNAL(triggered()), this, SLOT(showInfo()));
77 libraryAction = menuBar()->addAction(tr("Library"));
78 connect(libraryAction, SIGNAL(triggered()), this, SLOT(showLibrary()));
79 settingsAction = menuBar()->addAction(tr("Settings"));
80 connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
81 devToolsAction = menuBar()->addAction(tr("Developer"));
82 connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
84 infoAction = addToolBarAction(this, SLOT(showInfo()), "document-properties");
85 libraryAction = addToolBarAction(this, SLOT(showLibrary()),
86 "system-file-manager");
87 settingsAction = addToolBarAction(this, SLOT(showSettings()),
88 "preferences-system");
89 devToolsAction = addToolBarAction(this, SLOT(showDevTools()), "developer");
90 #endif // Q_WS_MAEMO_5
92 QFrame *frame = new QFrame(toolBar);
93 frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
94 toolBar->addWidget(frame);
96 fullScreenAction = addToolBarAction(this, SLOT(showBig()), "view-fullscreen");
98 // Handle model changes
99 connect(Library::instance(), SIGNAL(nowReadingChanged()),
100 this, SLOT(onCurrentBookChanged()));
102 // Load book on command line, or load last read book, or load default book
103 Library *library = Library::instance();
104 if (QCoreApplication::arguments().size() == 2) {
105 QString path = QCoreApplication::arguments()[1];
107 QModelIndex index = library->find(path);
108 if (index.isValid()) {
109 library->setNowReading(index);
113 QModelIndex index = library->nowReading();
114 if (index.isValid()) {
115 library->setNowReading(index);
118 if (!library->rowCount()) {
119 library->add(":/books/2 B R 0 2 B.epub");
121 library->setNowReading(library->index(0));
125 // Handle settings changes
126 Settings *settings = Settings::instance();
127 connect(settings, SIGNAL(valueChanged(const QString &)),
128 this, SLOT(onSettingsChanged(const QString &)));
129 settings->setValue("orientation", settings->value("orientation"));
130 settings->setValue("lightson", settings->value("lightson"));
132 // Handle loading chapters
133 connect(view, SIGNAL(chapterLoadStart(int)),
134 this, SLOT(onChapterLoadStart()));
135 connect(view, SIGNAL(chapterLoadEnd(int)),
136 this, SLOT(onChapterLoadEnd(int)));
138 // Shadow window for full screen
139 fullScreenWindow = new FullScreenWindow(this);
140 connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
142 #ifdef DORIAN_TEST_MODEL
143 (void)new ModelTest(Library::instance(), this);
147 void MainWindow::onCurrentBookChanged()
149 setCurrentBook(Library::instance()->nowReading());
152 void MainWindow::showRegular()
154 Trace t("MainWindow::showRegular");
155 fullScreenWindow->hide();
156 fullScreenWindow->leaveChild();
157 view->setParent(centralWidget());
158 centralWidget()->layout()->addWidget(view);
161 void MainWindow::showBig()
163 Trace t("MainWindow::showBig");
164 centralWidget()->layout()->removeWidget(view);
165 fullScreenWindow->takeChild(view);
166 fullScreenWindow->showFullScreen();
169 void MainWindow::setCurrentBook(const QModelIndex ¤t)
172 Book *book = Library::instance()->book(current);
174 setWindowTitle(book? book->name(): tr("Dorian"));
177 QAction *MainWindow::addToolBarAction(const QObject *receiver,
182 addAction(QIcon(ICON_PREFIX + name + ".png"), "", receiver, member);
185 void MainWindow::showLibrary()
187 LibraryDialog *dialog = new LibraryDialog(this);
191 void MainWindow::showSettings()
193 SettingsWindow *settings = new SettingsWindow(this);
197 void MainWindow::showInfo()
199 if (mCurrent.isValid()) {
201 new InfoDialog(Library::instance()->book(mCurrent), this);
206 void MainWindow::showDevTools()
208 DevTools *devTools = new DevTools();
212 void MainWindow::showBookmarks()
214 Book *book = Library::instance()->book(mCurrent);
216 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
217 bookmarks->setWindowModality(Qt::WindowModal);
218 connect(bookmarks, SIGNAL(addBookmark()), this, SLOT(onAddBookmark()));
219 connect(bookmarks, SIGNAL(goToBookmark(int)),
220 this, SLOT(onGoToBookmark(int)));
225 void MainWindow::closeEvent(QCloseEvent *event)
227 Trace t("MainWindow::closeEvent");
228 view->setLastBookmark();
232 void MainWindow::onSettingsChanged(const QString &key)
235 if (key == "orientation") {
236 QString value = Settings::instance()->value(key).toString();
237 Trace::trace(QString("MainWindow::onSettingsChanged: orientation %1").
239 if (value == "portrait") {
240 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
241 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
244 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
245 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
247 } else if (key == "lightson") {
248 bool enable = Settings::instance()->value(key, false).toBool();
249 Trace::trace(QString("MainWindow::onSettingsChanged: lightson: %1").
251 killTimer(preventBlankingTimer);
253 preventBlankingTimer = startTimer(29 * 1000);
258 #endif // Q_WS_MAEMO_5
261 void MainWindow::onChapterLoadStart()
263 Trace t("MainWindow::onChapterLoadStart");
265 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
269 void MainWindow::onChapterLoadEnd(int index)
271 Trace t("MainWindow::onChapterLoadEnd");
272 bool enablePrevious = false;
273 bool enableNext = false;
274 Book *book = Library::instance()->book(mCurrent);
277 enablePrevious = true;
279 if (index < (book->toc.size() - 1)) {
284 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
285 previousAction->setIcon(QIcon(enablePrevious?
286 ":/icons/previous.png" : ":/icons/previous-disabled.png"));
287 nextAction->setIcon(QIcon(enableNext?
288 ":/icons/next.png": ":/icons/next-disabled.png"));
289 #endif // Q_WS_MAEMO_5
290 previousAction->setEnabled(enablePrevious);
291 nextAction->setEnabled(enableNext);
294 void MainWindow::onAddBookmark()
296 Trace t("MainWindow:onAddBookmark");
300 void MainWindow::onGoToBookmark(int index)
302 Trace t("MainWindow::onGoToBookmark");
303 Book *book = Library::instance()->book(mCurrent);
304 view->goToBookmark(book->bookmarks()[index]);
307 void MainWindow::showChapters()
309 Book *book = Library::instance()->book(mCurrent);
311 ChaptersDialog *chapters = new ChaptersDialog(book, this);
312 chapters->setWindowModality(Qt::WindowModal);
313 connect(chapters, SIGNAL(goToChapter(int)),
314 this, SLOT(onGoToChapter(int)));
319 void MainWindow::onGoToChapter(int index)
321 view->goToBookmark(Book::Bookmark(index, 0));
324 void MainWindow::timerEvent(QTimerEvent *event)
326 if (event->timerId() == preventBlankingTimer) {
328 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
329 MCE_REQUEST_IF, QDBusConnection::systemBus());
330 mce.call(MCE_PREVENT_BLANK_REQ);
331 #endif // Q_WS_MAEMO_5
332 Trace::trace("MainWindow::timerEvent: Prevent display blanking");