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);
47 setWindowTitle("Dorian");
50 // Central widget. Must be an intermediate, because the book view widget
51 // can be re-parented later
52 QFrame *central = new QFrame(this);
53 QVBoxLayout *layout = new QVBoxLayout(central);
55 central->setLayout(layout);
56 setCentralWidget(central);
59 view = new BookView(this);
64 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
65 "view-fullscreen", tr("Full screen"));
68 chaptersAction = addToolBarAction(this, SLOT(showChapters()),
69 "chapters", tr("Chapters"), true);
70 bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
71 "bookmarks", tr("Bookmarks"), true);
72 infoAction = addToolBarAction(this, SLOT(showInfo()),
73 "info", tr("Book info"), true);
74 libraryAction = addToolBarAction(this, SLOT(showLibrary()),
75 "library", tr("Library"), true);
78 settingsAction = menuBar()->addAction(tr("Settings"));
79 connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
80 devToolsAction = menuBar()->addAction(tr("Developer"));
81 connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
82 QAction *aboutAction = menuBar()->addAction(tr("About"));
83 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
85 settingsAction = addToolBarAction(this, SLOT(showSettings()),
86 "preferences-system", tr("Settings"));
87 devToolsAction = addToolBarAction(this, SLOT(showDevTools()),
88 "developer", tr("Developer"));
89 addToolBarAction(this, SLOT(about()), "about", tr("About"));
90 #endif // Q_WS_MAEMO_5
94 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
95 "view-fullscreen", tr("Full screen"));
97 (void)addToolBarAction(this, SLOT(close()), "", tr("Exit"));
101 prev = new TranslucentButton("back", this);
102 next = new TranslucentButton("forward", this);
103 prog = new Progress(this);
105 // Handle model changes
106 connect(Library::instance(), SIGNAL(nowReadingChanged()),
107 this, SLOT(onCurrentBookChanged()));
109 // Load library, upgrade it if needed
110 libraryProgress = new ProgressDialog(tr("Upgrading library"), this);
111 Library *library = Library::instance();
112 connect(library, SIGNAL(beginUpgrade(int)),
113 this, SLOT(onBeginUpgrade(int)));
114 connect(library, SIGNAL(upgrading(const QString &)),
115 this, SLOT(onUpgrading(const QString &)));
116 connect(library, SIGNAL(endUpgrade()), this, SLOT(onEndUpgrade()));
118 // Handle loading book parts
119 connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
120 connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
123 connect(view, SIGNAL(progress(qreal)), prog, SLOT(setProgress(qreal)));
125 // Shadow window for full screen reading
126 fullScreenWindow = new FullScreenWindow(this);
127 connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
129 // Handle settings changes
130 connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
131 this, SLOT(onSettingsChanged(const QString &)));
133 // Handle book view buttons
134 connect(next, SIGNAL(triggered()), this, SLOT(goToNextPage()));
135 connect(prev, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
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)));
172 // Show in regular (non full-screen) mode
176 void MainWindow::onCurrentBookChanged()
179 setCurrentBook(Library::instance()->nowReading());
182 void MainWindow::showRegular()
186 // Re-parent children
187 fullScreenWindow->leaveBookView();
188 takeBookView(view, prog, prev, next);
190 fullScreenWindow->hide();
193 #if defined(Q_OS_SYMBIAN)
198 void MainWindow::showBig()
202 // Re-parent children
204 fullScreenWindow->takeBookView(view, prog, prev, next);
206 fullScreenWindow->showFullScreen();
210 fullScreenWindow->activateWindow();
214 void MainWindow::setCurrentBook(const QModelIndex ¤t)
217 Book *book = Library::instance()->book(current);
219 setWindowTitle(book? book->shortName(): tr("Dorian"));
222 void MainWindow::showLibrary()
224 (new LibraryDialog(this))->show();
227 void MainWindow::showSettings()
229 (new SettingsWindow(this))->show();
232 void MainWindow::showInfo()
234 if (mCurrent.isValid()) {
235 (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
240 void MainWindow::showDevTools()
242 (new DevTools())->exec();
245 void MainWindow::showBookmarks()
247 Book *book = Library::instance()->book(mCurrent);
249 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
250 connect(bookmarks, SIGNAL(addBookmark(const QString &)),
251 this, SLOT(onAddBookmark(const QString &)));
252 connect(bookmarks, SIGNAL(goToBookmark(int)),
253 this, SLOT(onGoToBookmark(int)));
258 void MainWindow::onSettingsChanged(const QString &key)
260 #if defined(Q_WS_MAEMO_5)
261 if (key == "orientation") {
262 QString value = Settings::instance()->value(key,
263 Platform::instance()->defaultOrientation()).toString();
264 qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
265 if (value == "portrait") {
266 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
267 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
268 fullScreenWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,
270 fullScreenWindow->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
272 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
273 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
274 fullScreenWindow->setAttribute(Qt::WA_Maemo5PortraitOrientation,
276 fullScreenWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,
279 } else if (key == "lightson") {
280 bool enable = Settings::instance()->value(key, false).toBool();
281 qDebug() << "MainWindow::onSettingsChanged: lightson" << enable;
282 killTimer(preventBlankingTimer);
284 preventBlankingTimer = startTimer(29 * 1000);
289 #endif // Q_WS_MAEMO_5
292 void MainWindow::onPartLoadStart()
295 Platform::instance()->showBusy(this, true);
298 void MainWindow::onPartLoadEnd(int index)
301 bool enablePrevious = false;
302 bool enableNext = false;
303 Book *book = Library::instance()->book(mCurrent);
306 enablePrevious = true;
308 if (index < (book->parts.size() - 1)) {
312 Platform::instance()->showBusy(this, false);
315 void MainWindow::onAddBookmark(const QString ¬e)
318 view->addBookmark(note);
319 Platform::instance()->information(tr("Bookmarked current position"), this);
322 void MainWindow::onGoToBookmark(int index)
325 Book *book = Library::instance()->book(mCurrent);
326 view->goToBookmark(book->bookmarks()[index]);
329 void MainWindow::showChapters()
331 Book *book = Library::instance()->book(mCurrent);
333 ChaptersDialog *chapters = new ChaptersDialog(book, this);
334 connect(chapters, SIGNAL(goToChapter(int)),
335 this, SLOT(onGoToChapter(int)));
340 void MainWindow::onGoToChapter(int index)
344 Book *book = Library::instance()->book(mCurrent);
347 int partIndex = book->partFromChapter(index, fragment);
348 if (partIndex != -1) {
349 view->goToPart(partIndex, fragment);
354 void MainWindow::timerEvent(QTimerEvent *event)
356 if (event->timerId() == preventBlankingTimer) {
358 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
359 MCE_REQUEST_IF, QDBusConnection::systemBus());
360 mce.call(MCE_PREVENT_BLANK_REQ);
361 #endif // Q_WS_MAEMO_5
362 qDebug() << "MainWindow::timerEvent: Prevent display blanking";
364 AdopterWindow::timerEvent(event);
367 void MainWindow::about()
369 Dyalog *aboutDialog = new Dyalog(this, false);
370 aboutDialog->setWindowTitle(tr("About Dorian"));
371 QString version = Platform::instance()->version();
372 QLabel *label = new QLabel(aboutDialog);
373 label->setTextFormat(Qt::RichText);
374 label->setOpenExternalLinks(true);
375 label->setWordWrap(true);
376 label->setText(tr("<b>Dorian %1</b><br><br>Copyright © 2010 "
377 "Akos Polster <akos@pipacs.com><br>"
378 "Licensed under GNU General Public License, Version 3<br>"
379 "Source code:<br><a href='http://dorian.garage.maemo.org/'>"
380 "dorian.garage.maemo.org</a>").arg(version));
381 aboutDialog->addWidget(label);
382 aboutDialog->addStretch();
386 void MainWindow::goToNextPage()
393 void MainWindow::goToPreviousPage()
397 view->goPreviousPage();
400 void MainWindow::onBeginUpgrade(int total)
402 libraryProgress->setVisible(total > 0);
403 libraryProgress->setWindowTitle(tr("Upgrading library"));
404 libraryProgress->setMaximum(total);
407 void MainWindow::onUpgrading(const QString &path)
409 libraryProgress->setLabelText(tr("Upgrading %1").
410 arg(QFileInfo(path).fileName()));
411 libraryProgress->setValue(libraryProgress->value() + 1);
414 void MainWindow::onEndUpgrade()
416 libraryProgress->hide();
417 libraryProgress->reset();
420 void MainWindow::onBeginLoad(int total)
422 libraryProgress->setVisible(total > 0);
423 libraryProgress->setWindowTitle(tr("Loading library"));
424 libraryProgress->setMaximum(total);
427 void MainWindow::onLoading(const QString &path)
429 libraryProgress->setLabelText(tr("Loading %1").
430 arg(QFileInfo(path).fileName()));
431 libraryProgress->setValue(libraryProgress->value() + 1);
434 void MainWindow::onEndLoad()
436 libraryProgress->hide();
437 libraryProgress->reset();