Add some documentation. Fix popup button locations on Maemo. Ensure popup buttons...
[dorian] / mainwindow.cpp
1 #include <QtGui>
2 #include <QtDebug>
3 #include <QDir>
4 #include <QApplication>
5 #include <QFileInfo>
6 #include <QStringList>
7
8 #ifdef Q_WS_MAEMO_5
9 #   include <QtMaemo5/QMaemo5InformationBox>
10 #   include <QtDBus>
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
17
18 #include "bookview.h"
19 #include "book.h"
20 #include "library.h"
21 #include "infodialog.h"
22 #include "librarydialog.h"
23 #include "devtools.h"
24 #include "mainwindow.h"
25 #include "settingswindow.h"
26 #include "bookmarksdialog.h"
27 #include "settings.h"
28 #include "chaptersdialog.h"
29 #include "fullscreenwindow.h"
30 #include "trace.h"
31 #include "bookfinder.h"
32 #include "progress.h"
33 #include "dyalog.h"
34 #include "translucentbutton.h"
35
36 #ifdef DORIAN_TEST_MODEL
37 #include "modeltest.h"
38 #endif
39
40 #ifdef Q_WS_MAC
41 #   define ICON_PREFIX ":/icons/mac/"
42 #else
43 #   define ICON_PREFIX ":/icons/"
44 #endif
45
46 const int PROGRESS_HEIGHT = 17;
47 const char *DORIAN_VERSION =
48 #include "pkg/version.txt"
49 ;
50
51 MainWindow::MainWindow(QWidget *parent):
52     AdopterWindow(parent), view(0), preventBlankingTimer(-1)
53 {
54     Trace t("MainWindow::MainWindow");
55 #ifdef Q_WS_MAEMO_5
56     setAttribute(Qt::WA_Maemo5StackedWindow, true);
57 #endif
58     setWindowTitle("Dorian");
59
60     // Central widget. Must be an intermediate, because the book view widget
61     // can be re-parented later
62     QFrame *central = new QFrame(this);
63     QVBoxLayout *layout = new QVBoxLayout(central);
64     layout->setMargin(0);
65     central->setLayout(layout);
66     setCentralWidget(central);
67
68     // Book view
69     view = new BookView(central);
70     view->show();
71     layout->addWidget(view);
72
73     // Progress
74     progress = new Progress(central);
75
76     // Tool bar
77
78     setUnifiedTitleAndToolBarOnMac(true);
79     settings = new QDialog(this);
80     toolBar = addToolBar("controls");
81     toolBar->setMovable(false);
82     toolBar->setFloatable(false);
83     toolBar->toggleViewAction()->setVisible(false);
84 #if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)
85     toolBar->setIconSize(QSize(42, 42));
86 #endif
87
88     previousAction = addToolBarAction(view, SLOT(goPrevious()), "previous");
89     nextAction = addToolBarAction(view, SLOT(goNext()), "next");
90     chaptersAction = addToolBarAction(this, SLOT(showChapters()), "chapters");
91     bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()), "bookmarks");
92
93 #ifdef Q_WS_MAEMO_5
94     infoAction = menuBar()->addAction(tr("Book details"));
95     connect(infoAction, SIGNAL(triggered()), this, SLOT(showInfo()));
96     libraryAction = menuBar()->addAction(tr("Library"));
97     connect(libraryAction, SIGNAL(triggered()), this, SLOT(showLibrary()));
98     settingsAction = menuBar()->addAction(tr("Settings"));
99     connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
100     devToolsAction = menuBar()->addAction(tr("Developer"));
101     connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
102     QAction *aboutAction = menuBar()->addAction(tr("About"));
103     connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
104 #else
105     infoAction = addToolBarAction(this, SLOT(showInfo()), "document-properties");
106     libraryAction = addToolBarAction(this, SLOT(showLibrary()),
107                                      "system-file-manager");
108     settingsAction = addToolBarAction(this, SLOT(showSettings()),
109                                       "preferences-system");
110     devToolsAction = addToolBarAction(this, SLOT(showDevTools()), "developer");
111     addToolBarAction(this, SLOT(about()), "about");
112 #endif // Q_WS_MAEMO_5
113
114     QFrame *frame = new QFrame(toolBar);
115     frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
116     toolBar->addWidget(frame);
117
118     fullScreenAction = addToolBarAction(this, SLOT(showBig()),
119                                         "view-fullscreen");
120
121     // Buttons on top of the book view
122     previousButton = new TranslucentButton("back", this);
123     nextButton = new TranslucentButton("forward", this);
124
125     // Handle model changes
126     connect(Library::instance(), SIGNAL(nowReadingChanged()),
127             this, SLOT(onCurrentBookChanged()));
128
129     // Load book on command line, or load last read book, or load default book
130     Library *library = Library::instance();
131     if (QCoreApplication::arguments().size() == 2) {
132         QString path = QCoreApplication::arguments()[1];
133         library->add(path);
134         QModelIndex index = library->find(path);
135         if (index.isValid()) {
136             library->setNowReading(index);
137         }
138     }
139     else {
140         QModelIndex index = library->nowReading();
141         if (index.isValid()) {
142             library->setNowReading(index);
143         }
144         else {
145             if (!library->rowCount()) {
146                 library->add(":/books/2 B R 0 2 B.epub");
147             }
148             library->setNowReading(library->index(0));
149         }
150     }
151
152     // Handle loading book parts
153     connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
154     connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
155
156     // Handle progress
157     connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
158
159     // Shadow window for full screen reading
160     fullScreenWindow = new FullScreenWindow(this);
161     connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
162
163     // Handle settings changes
164     Settings *settings = Settings::instance();
165     connect(settings, SIGNAL(valueChanged(const QString &)),
166             this, SLOT(onSettingsChanged(const QString &)));
167     settings->setValue("orientation", settings->value("orientation"));
168     settings->setValue("lightson", settings->value("lightson"));
169     settings->setValue("usevolumekeys", settings->value("usevolumekeys"));
170
171     // Handle book view buttons
172     connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
173     connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
174
175 #ifdef DORIAN_TEST_MODEL
176     (void)new ModelTest(Library::instance(), this);
177 #endif
178 }
179
180 MainWindow::~MainWindow()
181 {
182 }
183
184 void MainWindow::onCurrentBookChanged()
185 {
186     setCurrentBook(Library::instance()->nowReading());
187 }
188
189 void MainWindow::showRegular()
190 {
191     Trace t("MainWindow::showRegular");
192     fullScreenWindow->hide();
193     fullScreenWindow->leaveChildren();
194
195     QList<QWidget *> otherChildren;
196     otherChildren << progress << previousButton << nextButton;
197     takeChildren(view, otherChildren);
198     QRect geo = geometry();
199     qDebug() << "Geometry" << geo << "toolbar" << toolBar->height();
200     progress->setGeometry(0, 0, geo.width(), PROGRESS_HEIGHT);
201 #ifdef Q_WS_MAEMO_5
202     previousButton->setGeometry(0,
203         geo.height() - toolBar->height() - TranslucentButton::pixels,
204         TranslucentButton::pixels, TranslucentButton::pixels);
205     nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
206         TranslucentButton::pixels, TranslucentButton::pixels);
207 #else
208     previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
209         TranslucentButton::pixels, TranslucentButton::pixels);
210     nextButton->setGeometry(geo.width() - TranslucentButton::pixels,
211         toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
212 #endif // Q_WS_MAEMO_5
213     qDebug() << "previousButton geometry" << previousButton->geometry();
214     progress->flash();
215     nextButton->show();
216     previousButton->show();
217     nextButton->flash(1500);
218     previousButton->flash(1500);
219 }
220
221 void MainWindow::showBig()
222 {
223     Trace t("MainWindow::showBig");
224     leaveChildren();
225     QList<QWidget *> otherChildren;
226     otherChildren << progress << nextButton << previousButton;
227     QRect screen = QApplication::desktop()->screenGeometry();
228     progress->setGeometry(0, 0, screen.width(), PROGRESS_HEIGHT);
229     nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
230         TranslucentButton::pixels, TranslucentButton::pixels);
231     previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
232         TranslucentButton::pixels, TranslucentButton::pixels);
233
234     fullScreenWindow->takeChildren(view, otherChildren);
235     fullScreenWindow->showFullScreen();
236     progress->flash();
237     nextButton->flash(1500);
238     previousButton->flash(1500);
239 }
240
241 void MainWindow::setCurrentBook(const QModelIndex &current)
242 {
243     mCurrent = current;
244     Book *book = Library::instance()->book(current);
245     view->setBook(book);
246     setWindowTitle(book? book->shortName(): tr("Dorian"));
247 }
248
249 QAction *MainWindow::addToolBarAction(const QObject *receiver,
250                                       const char *member,
251                                       const QString &name)
252 {
253     return toolBar->
254         addAction(QIcon(ICON_PREFIX + name + ".png"), "", receiver, member);
255 }
256
257 void MainWindow::showLibrary()
258 {
259     (new LibraryDialog(this))->show();
260 }
261
262 void MainWindow::showSettings()
263 {
264     (new SettingsWindow(this))->show();
265 }
266
267 void MainWindow::showInfo()
268 {
269     if (mCurrent.isValid()) {
270         (new InfoDialog(Library::instance()->book(mCurrent), this))->exec();
271     }
272 }
273
274 void MainWindow::showDevTools()
275 {
276     (new DevTools())->exec();
277 }
278
279 void MainWindow::showBookmarks()
280 {
281     Book *book = Library::instance()->book(mCurrent);
282     if (book) {
283         BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
284         bookmarks->setWindowModality(Qt::WindowModal);
285         connect(bookmarks, SIGNAL(addBookmark()), this, SLOT(onAddBookmark()));
286         connect(bookmarks, SIGNAL(goToBookmark(int)),
287                 this, SLOT(onGoToBookmark(int)));
288         bookmarks->show();
289     }
290 }
291
292 void MainWindow::closeEvent(QCloseEvent *event)
293 {
294     Trace t("MainWindow::closeEvent");
295     view->setLastBookmark();
296     event->accept();
297 }
298
299 void MainWindow::onSettingsChanged(const QString &key)
300 {
301 #ifdef Q_WS_MAEMO_5
302     if (key == "orientation") {
303         QString value = Settings::instance()->value(key).toString();
304         qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
305         if (value == "portrait") {
306             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
307             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
308         }
309         else {
310             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
311             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
312         }
313     } else if (key == "lightson") {
314         bool enable = Settings::instance()->value(key, false).toBool();
315         qDebug() << "MainWindow::onSettingsChanged: lightson:" << enable;
316         killTimer(preventBlankingTimer);
317         if (enable) {
318             preventBlankingTimer = startTimer(29 * 1000);
319         }
320     } else if (key == "usevolumekeys") {
321         bool value = Settings::instance()->value(key).toBool();
322         qDebug() << "MainWindow::onSettingsChanged: usevolumekeys" << value;
323         grabZoomKeys(value);
324         fullScreenWindow->grabZoomKeys(value);
325     }
326 #else
327     Q_UNUSED(key);
328 #endif // Q_WS_MAEMO_5
329 }
330
331 void MainWindow::onPartLoadStart()
332 {
333     Trace t("MainWindow::onPartLoadStart");
334 #ifdef Q_WS_MAEMO_5
335     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
336 #endif
337 }
338
339 void MainWindow::onPartLoadEnd(int index)
340 {
341     Trace t("MainWindow::onPartLoadEnd");
342     bool enablePrevious = false;
343     bool enableNext = false;
344     Book *book = Library::instance()->book(mCurrent);
345     if (book) {
346         if (index > 0) {
347             enablePrevious = true;
348         }
349         if (index < (book->parts.size() - 1)) {
350             enableNext = true;
351         }
352     }
353 #ifdef Q_WS_MAEMO_5
354     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
355     previousAction->setIcon(QIcon(enablePrevious?
356         ":/icons/previous.png" : ":/icons/previous-disabled.png"));
357     nextAction->setIcon(QIcon(enableNext?
358         ":/icons/next.png": ":/icons/next-disabled.png"));
359 #endif // Q_WS_MAEMO_5
360     previousAction->setEnabled(enablePrevious);
361     nextAction->setEnabled(enableNext);
362 }
363
364 void MainWindow::onAddBookmark()
365 {
366     Trace t("MainWindow:onAddBookmark");
367     view->addBookmark();
368 }
369
370 void MainWindow::onGoToBookmark(int index)
371 {
372     Trace t("MainWindow::onGoToBookmark");
373     Book *book = Library::instance()->book(mCurrent);
374     view->goToBookmark(book->bookmarks()[index]);
375 }
376
377 void MainWindow::showChapters()
378 {
379     Book *book = Library::instance()->book(mCurrent);
380     if (book) {
381         ChaptersDialog *chapters = new ChaptersDialog(book, this);
382         chapters->setWindowModality(Qt::WindowModal);
383         connect(chapters, SIGNAL(goToChapter(int)),
384                 this, SLOT(onGoToChapter(int)));
385         chapters->show();
386     }
387 }
388
389 void MainWindow::onGoToChapter(int index)
390 {
391     Trace t("MainWindow::onGoToChapter");
392
393     Book *book = Library::instance()->book(mCurrent);
394     if (book) {
395         int partIndex = book->partFromChapter(index);
396         if (partIndex != -1) {
397             view->goToBookmark(Book::Bookmark(partIndex, 0));
398         }
399     }
400 }
401
402 void MainWindow::timerEvent(QTimerEvent *event)
403 {
404     if (event->timerId() == preventBlankingTimer) {
405 #ifdef Q_WS_MAEMO_5
406         QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
407                            MCE_REQUEST_IF, QDBusConnection::systemBus());
408         mce.call(MCE_PREVENT_BLANK_REQ);
409 #endif // Q_WS_MAEMO_5
410         qDebug() << "MainWindow::timerEvent: Prevent display blanking";
411     }
412 }
413
414 void MainWindow::resizeEvent(QResizeEvent *e)
415 {
416     Trace t("MainWindow::resizeEvent");
417     progress->setGeometry(QRect(0, 0, e->size().width(), PROGRESS_HEIGHT));
418     qDebug() << "Toolbar height" << toolBar->height();
419 #ifdef Q_WS_MAEMO_5
420     previousButton->setGeometry(0,
421         e->size().height() - toolBar->height() - TranslucentButton::pixels,
422         TranslucentButton::pixels, TranslucentButton::pixels);
423     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
424         TranslucentButton::pixels, TranslucentButton::pixels);
425 #else
426     previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
427         TranslucentButton::pixels, TranslucentButton::pixels);
428     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
429         toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
430 #endif // Q_WS_MAEMO_5
431     qDebug() << "previousButton geometry" << previousButton->geometry();
432     previousButton->flash(1500);
433     nextButton->flash(1500);
434     QMainWindow::resizeEvent(e);
435 }
436
437 void MainWindow::about()
438 {
439     Dyalog *aboutDialog = new Dyalog(this);
440     aboutDialog->setWindowTitle(tr("About Dorian"));
441     QLabel *label = new QLabel(aboutDialog);
442     label->setTextFormat(Qt::RichText);
443     label->setOpenExternalLinks(true);
444     label->setText(tr("<b>Dorian %1</b><br><br>Copyright &copy; 2010 by "
445         "Akos Polster &lt;akos@pipacs.com&gt;<br>"
446         "Licensed under GNU General Public License, Version 3<br>"
447         "Source code: <a href='https://garage.maemo.org/projects/dorian/'>"
448         "garage.maemo.org/projects/dorian</a>").arg(DORIAN_VERSION));
449     aboutDialog->addWidget(label);
450     aboutDialog->show();
451 }
452
453
454 void MainWindow::goToNextPage()
455 {
456     nextButton->flash(1500);
457     previousButton->flash(1500);
458     view->goNextPage();
459 }
460
461 void MainWindow::goToPreviousPage()
462 {
463     nextButton->flash(1500);
464     previousButton->flash(1500);
465     view->goPreviousPage();
466 }