6 # include <QtGui/QX11Info>
8 # include <X11/Xatom.h>
9 # include <mce/mode-names.h>
10 # include <mce/dbus-names.h>
11 #endif // Q_WS_MAEMO_5
16 #include "infodialog.h"
17 #include "librarydialog.h"
19 #include "mainwindow.h"
20 #include "settingswindow.h"
21 #include "bookmarksdialog.h"
23 #include "chaptersdialog.h"
24 #include "fullscreenwindow.h"
26 #include "bookfinder.h"
29 #include "translucentbutton.h"
31 #include "progressdialog.h"
32 #include "sortedlibrary.h"
34 #ifdef DORIAN_TEST_MODEL
35 # include "modeltest.h"
38 MainWindow::MainWindow(QWidget *parent):
39 AdopterWindow(parent), view(0), preventBlankingTimer(-1)
43 setAttribute(Qt::WA_Maemo5StackedWindow, true);
45 setWindowTitle("Dorian");
47 // Central widget. Must be an intermediate, because the book view widget
48 // can be re-parented later
49 QFrame *central = new QFrame(this);
50 QVBoxLayout *layout = new QVBoxLayout(central);
52 central->setLayout(layout);
53 setCentralWidget(central);
56 view = new BookView(this);
62 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
63 "view-fullscreen", tr("Full screen"));
66 chaptersAction = addToolBarAction(this, SLOT(showChapters()),
67 "chapters", tr("Chapters"), true);
68 bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
69 "bookmarks", tr("Bookmarks"), true);
70 infoAction = addToolBarAction(this, SLOT(showInfo()),
71 "info", tr("Book info"), true);
72 libraryAction = addToolBarAction(this, SLOT(showLibrary()),
73 "library", tr("Library"), true);
76 settingsAction = menuBar()->addAction(tr("Settings"));
77 connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
78 devToolsAction = menuBar()->addAction(tr("Developer"));
79 connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
80 QAction *aboutAction = menuBar()->addAction(tr("About"));
81 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
83 settingsAction = addToolBarAction(this, SLOT(showSettings()),
84 "preferences-system", tr("Settings"));
85 devToolsAction = addToolBarAction(this, SLOT(showDevTools()),
86 "developer", tr("Developer"));
87 addToolBarAction(this, SLOT(about()), "about", tr("About"));
88 #endif // Q_WS_MAEMO_5
92 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
93 "view-fullscreen", tr("Full screen"));
95 (void)addToolBarAction(this, SLOT(close()), "", tr("Exit"));
99 prev = new TranslucentButton("back", this);
100 next = new TranslucentButton("forward", this);
101 prog = new Progress(this);
103 // Handle model changes
104 connect(Library::instance(), SIGNAL(nowReadingChanged()),
105 this, SLOT(onCurrentBookChanged()));
107 // Load library, upgrade it if needed
108 libraryProgress = new ProgressDialog(tr("Upgrading library"), this);
109 Library *library = Library::instance();
110 connect(library, SIGNAL(beginUpgrade(int)), this, SLOT(onBeginUpgrade(int)));
111 connect(library, SIGNAL(upgrading(const QString &)),
112 this, SLOT(onUpgrading(const QString &)));
113 connect(library, SIGNAL(endUpgrade()), this, SLOT(onEndUpgrade()));
115 // Handle loading book parts
116 connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
117 connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
120 connect(view, SIGNAL(progress(qreal)), prog, SLOT(setProgress(qreal)));
122 // Shadow window for full screen reading
123 fullScreenWindow = new FullScreenWindow(this);
124 connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
126 // Handle settings changes
127 connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
128 this, SLOT(onSettingsChanged(const QString &)));
130 // Handle book view buttons
131 connect(next, SIGNAL(triggered()), this, SLOT(goToNextPage()));
132 connect(prev, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
134 // Adopt view, show window
137 #ifdef DORIAN_TEST_MODEL
138 (void)new ModelTest(Library::instance(), this);
142 void MainWindow::initialize()
145 Library *library = Library::instance();
147 // Upgrade library if needed, then load it
151 // Load book on command line, or load last read book, or load default book
152 if (QCoreApplication::arguments().size() == 2) {
153 QString path = QCoreApplication::arguments()[1];
155 QModelIndex index = library->find(path);
156 if (index.isValid()) {
157 library->setNowReading(index);
160 QModelIndex index = library->nowReading();
161 if (index.isValid()) {
162 library->setNowReading(index);
164 if (!library->rowCount()) {
165 library->add(":/books/2BR02B.epub");
167 SortedLibrary sorted;
168 library->setNowReading(sorted.mapToSource(sorted.index(0, 0)));
173 void MainWindow::onCurrentBookChanged()
176 setCurrentBook(Library::instance()->nowReading());
179 void MainWindow::showRegular()
183 // Re-parent children
184 fullScreenWindow->leaveBookView();
185 takeBookView(view, prog, prev, next);
187 fullScreenWindow->hide();
189 #if defined(Q_OS_SYMBIAN)
194 void MainWindow::showBig()
198 // Re-parent children
200 fullScreenWindow->takeBookView(view, prog, prev, next);
202 // #ifdef Q_OS_SYMBIAN
206 fullScreenWindow->showFullScreen();
208 fullScreenWindow->activateWindow();
212 void MainWindow::setCurrentBook(const QModelIndex ¤t)
215 Book *book = Library::instance()->book(current);
217 setWindowTitle(book? book->shortName(): tr("Dorian"));
220 void MainWindow::showLibrary()
222 (new LibraryDialog(this))->show();
225 void MainWindow::showSettings()
227 (new SettingsWindow(this))->show();
230 void MainWindow::showInfo()
232 if (mCurrent.isValid()) {
233 (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
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 connect(bookmarks, SIGNAL(addBookmark(const QString &)),
249 this, SLOT(onAddBookmark(const QString &)));
250 connect(bookmarks, SIGNAL(goToBookmark(int)),
251 this, SLOT(onGoToBookmark(int)));
256 void MainWindow::closeEvent(QCloseEvent *event)
259 view->setLastBookmark();
260 AdopterWindow::closeEvent(event);
263 void MainWindow::onSettingsChanged(const QString &key)
265 #if defined(Q_WS_MAEMO_5)
266 if (key == "orientation") {
267 QString value = Settings::instance()->value(key,
268 Platform::instance()->defaultOrientation()).toString();
269 qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
270 if (value == "portrait") {
271 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
272 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
273 fullScreenWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,
275 fullScreenWindow->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
277 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
278 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
279 fullScreenWindow->setAttribute(Qt::WA_Maemo5PortraitOrientation,
281 fullScreenWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,
284 } else if (key == "lightson") {
285 bool enable = Settings::instance()->value(key, false).toBool();
286 qDebug() << "MainWindow::onSettingsChanged: lightson" << enable;
287 killTimer(preventBlankingTimer);
289 preventBlankingTimer = startTimer(29 * 1000);
294 #endif // Q_WS_MAEMO_5
297 void MainWindow::onPartLoadStart()
300 Platform::instance()->showBusy(this, true);
303 void MainWindow::onPartLoadEnd(int index)
306 bool enablePrevious = false;
307 bool enableNext = false;
308 Book *book = Library::instance()->book(mCurrent);
311 enablePrevious = true;
313 if (index < (book->parts.size() - 1)) {
317 Platform::instance()->showBusy(this, false);
320 void MainWindow::onAddBookmark(const QString ¬e)
323 view->addBookmark(note);
324 Platform::instance()->information(tr("Bookmarked current position"), this);
327 void MainWindow::onGoToBookmark(int index)
330 Book *book = Library::instance()->book(mCurrent);
331 view->goToBookmark(book->bookmarks()[index]);
334 void MainWindow::showChapters()
336 Book *book = Library::instance()->book(mCurrent);
338 ChaptersDialog *chapters = new ChaptersDialog(book, this);
339 connect(chapters, SIGNAL(goToChapter(int)),
340 this, SLOT(onGoToChapter(int)));
345 void MainWindow::onGoToChapter(int index)
349 Book *book = Library::instance()->book(mCurrent);
352 int partIndex = book->partFromChapter(index, fragment);
353 if (partIndex != -1) {
354 view->goToPart(partIndex, fragment);
359 void MainWindow::timerEvent(QTimerEvent *event)
361 if (event->timerId() == preventBlankingTimer) {
363 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
364 MCE_REQUEST_IF, QDBusConnection::systemBus());
365 mce.call(MCE_PREVENT_BLANK_REQ);
366 #endif // Q_WS_MAEMO_5
367 qDebug() << "MainWindow::timerEvent: Prevent display blanking";
369 AdopterWindow::timerEvent(event);
372 void MainWindow::about()
374 Dyalog *aboutDialog = new Dyalog(this, false);
375 aboutDialog->setWindowTitle(tr("About Dorian"));
376 QString version = Platform::instance()->version();
377 QLabel *label = new QLabel(aboutDialog);
378 label->setTextFormat(Qt::RichText);
379 label->setOpenExternalLinks(true);
380 label->setWordWrap(true);
381 label->setText(tr("<b>Dorian %1</b><br><br>Copyright © 2010 "
382 "Akos Polster <akos@pipacs.com><br>"
383 "Licensed under GNU General Public License, Version 3<br>"
384 "Source code:<br><a href='http://dorian.garage.maemo.org/'>"
385 "dorian.garage.maemo.org</a>").arg(version));
386 aboutDialog->addWidget(label);
387 aboutDialog->addStretch();
391 void MainWindow::goToNextPage()
398 void MainWindow::goToPreviousPage()
402 view->goPreviousPage();
405 void MainWindow::onBeginUpgrade(int total)
407 libraryProgress->setVisible(total > 0);
408 libraryProgress->setWindowTitle(tr("Upgrading library"));
409 libraryProgress->setMaximum(total);
412 void MainWindow::onUpgrading(const QString &path)
414 libraryProgress->setLabelText(tr("Upgrading %1").
415 arg(QFileInfo(path).fileName()));
416 libraryProgress->setValue(libraryProgress->value() + 1);
419 void MainWindow::onEndUpgrade()
421 libraryProgress->hide();
422 libraryProgress->reset();
425 void MainWindow::onBeginLoad(int total)
427 libraryProgress->setVisible(total > 0);
428 libraryProgress->setWindowTitle(tr("Loading library"));
429 libraryProgress->setMaximum(total);
432 void MainWindow::onLoading(const QString &path)
434 libraryProgress->setLabelText(tr("Loading %1").
435 arg(QFileInfo(path).fileName()));
436 libraryProgress->setValue(libraryProgress->value() + 1);
439 void MainWindow::onEndLoad()
441 libraryProgress->hide();
442 libraryProgress->reset();