Improve icons. Move some menu items to the toolbar. Don't show buttons in current...
[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     chaptersAction = addToolBarAction(this, SLOT(showChapters()), "chapters");
89     bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()), "bookmarks");
90     infoAction = addToolBarAction(this, SLOT(showInfo()), "info");
91     libraryAction = addToolBarAction(this, SLOT(showLibrary()), "library");
92
93 #ifdef Q_WS_MAEMO_5
94     settingsAction = menuBar()->addAction(tr("Settings"));
95     connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
96     devToolsAction = menuBar()->addAction(tr("Developer"));
97     connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
98     QAction *aboutAction = menuBar()->addAction(tr("About"));
99     connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
100 #else
101     settingsAction = addToolBarAction(this, SLOT(showSettings()),
102                                       "preferences-system");
103     devToolsAction = addToolBarAction(this, SLOT(showDevTools()), "developer");
104     addToolBarAction(this, SLOT(about()), "about");
105 #endif // Q_WS_MAEMO_5
106
107     QFrame *frame = new QFrame(toolBar);
108     frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
109     toolBar->addWidget(frame);
110
111     fullScreenAction = addToolBarAction(this, SLOT(showBig()),
112                                         "view-fullscreen");
113
114     // Buttons on top of the book view
115     previousButton = new TranslucentButton("back", this);
116     nextButton = new TranslucentButton("forward", this);
117
118     // Handle model changes
119     connect(Library::instance(), SIGNAL(nowReadingChanged()),
120             this, SLOT(onCurrentBookChanged()));
121
122     // Load book on command line, or load last read book, or load default book
123     Library *library = Library::instance();
124     if (QCoreApplication::arguments().size() == 2) {
125         QString path = QCoreApplication::arguments()[1];
126         library->add(path);
127         QModelIndex index = library->find(path);
128         if (index.isValid()) {
129             library->setNowReading(index);
130         }
131     }
132     else {
133         QModelIndex index = library->nowReading();
134         if (index.isValid()) {
135             library->setNowReading(index);
136         }
137         else {
138             if (!library->rowCount()) {
139                 library->add(":/books/2 B R 0 2 B.epub");
140             }
141             library->setNowReading(library->index(0));
142         }
143     }
144
145     // Handle loading book parts
146     connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
147     connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
148
149     // Handle progress
150     connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
151
152     // Shadow window for full screen reading
153     fullScreenWindow = new FullScreenWindow(this);
154     connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
155
156     // Handle settings changes
157     Settings *settings = Settings::instance();
158     connect(settings, SIGNAL(valueChanged(const QString &)),
159             this, SLOT(onSettingsChanged(const QString &)));
160     settings->setValue("orientation", settings->value("orientation"));
161     settings->setValue("lightson", settings->value("lightson"));
162     settings->setValue("usevolumekeys", settings->value("usevolumekeys"));
163
164     // Handle book view buttons
165     connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
166     connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
167
168 #ifdef DORIAN_TEST_MODEL
169     (void)new ModelTest(Library::instance(), this);
170 #endif
171 }
172
173 MainWindow::~MainWindow()
174 {
175 }
176
177 void MainWindow::onCurrentBookChanged()
178 {
179     setCurrentBook(Library::instance()->nowReading());
180 }
181
182 void MainWindow::showRegular()
183 {
184     Trace t("MainWindow::showRegular");
185     fullScreenWindow->hide();
186     fullScreenWindow->leaveChildren();
187
188     QList<QWidget *> otherChildren;
189     otherChildren << progress << previousButton << nextButton;
190     takeChildren(view, otherChildren);
191     QRect geo = geometry();
192     qDebug() << "Geometry" << geo << "toolbar" << toolBar->height();
193     progress->setGeometry(0, 0, geo.width(), PROGRESS_HEIGHT);
194 #ifdef Q_WS_MAEMO_5
195     previousButton->setGeometry(0,
196         geo.height() - toolBar->height() - TranslucentButton::pixels,
197         TranslucentButton::pixels, TranslucentButton::pixels);
198     nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
199         TranslucentButton::pixels, TranslucentButton::pixels);
200 #else
201     previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
202         TranslucentButton::pixels, TranslucentButton::pixels);
203     nextButton->setGeometry(geo.width() - TranslucentButton::pixels,
204         toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
205 #endif // Q_WS_MAEMO_5
206     qDebug() << "previousButton geometry" << previousButton->geometry();
207     progress->flash();
208     nextButton->show();
209     previousButton->show();
210     nextButton->flash(1500);
211     previousButton->flash(1500);
212 }
213
214 void MainWindow::showBig()
215 {
216     Trace t("MainWindow::showBig");
217     leaveChildren();
218     QList<QWidget *> otherChildren;
219     otherChildren << progress << nextButton << previousButton;
220     QRect screen = QApplication::desktop()->screenGeometry();
221     progress->setGeometry(0, 0, screen.width(), PROGRESS_HEIGHT);
222     nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
223         TranslucentButton::pixels, TranslucentButton::pixels);
224     previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
225         TranslucentButton::pixels, TranslucentButton::pixels);
226
227     fullScreenWindow->takeChildren(view, otherChildren);
228     fullScreenWindow->showFullScreen();
229     progress->flash();
230     nextButton->flash(1500);
231     previousButton->flash(1500);
232 }
233
234 void MainWindow::setCurrentBook(const QModelIndex &current)
235 {
236     mCurrent = current;
237     Book *book = Library::instance()->book(current);
238     view->setBook(book);
239     setWindowTitle(book? book->shortName(): tr("Dorian"));
240 }
241
242 QAction *MainWindow::addToolBarAction(const QObject *receiver,
243                                       const char *member,
244                                       const QString &name)
245 {
246     return toolBar->
247         addAction(QIcon(ICON_PREFIX + name + ".png"), "", receiver, member);
248 }
249
250 void MainWindow::showLibrary()
251 {
252     (new LibraryDialog(this))->show();
253 }
254
255 void MainWindow::showSettings()
256 {
257     (new SettingsWindow(this))->show();
258 }
259
260 void MainWindow::showInfo()
261 {
262     if (mCurrent.isValid()) {
263         (new InfoDialog(Library::instance()->book(mCurrent), this, false))->exec();
264     }
265 }
266
267 void MainWindow::showDevTools()
268 {
269     (new DevTools())->exec();
270 }
271
272 void MainWindow::showBookmarks()
273 {
274     Book *book = Library::instance()->book(mCurrent);
275     if (book) {
276         BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
277         bookmarks->setWindowModality(Qt::WindowModal);
278         connect(bookmarks, SIGNAL(addBookmark()), this, SLOT(onAddBookmark()));
279         connect(bookmarks, SIGNAL(goToBookmark(int)),
280                 this, SLOT(onGoToBookmark(int)));
281         bookmarks->show();
282     }
283 }
284
285 void MainWindow::closeEvent(QCloseEvent *event)
286 {
287     Trace t("MainWindow::closeEvent");
288     view->setLastBookmark();
289     event->accept();
290 }
291
292 void MainWindow::onSettingsChanged(const QString &key)
293 {
294 #ifdef Q_WS_MAEMO_5
295     if (key == "orientation") {
296         QString value = Settings::instance()->value(key).toString();
297         qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
298         if (value == "portrait") {
299             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
300             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
301         }
302         else {
303             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
304             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
305         }
306     } else if (key == "lightson") {
307         bool enable = Settings::instance()->value(key, false).toBool();
308         qDebug() << "MainWindow::onSettingsChanged: lightson:" << enable;
309         killTimer(preventBlankingTimer);
310         if (enable) {
311             preventBlankingTimer = startTimer(29 * 1000);
312         }
313     } else if (key == "usevolumekeys") {
314         bool value = Settings::instance()->value(key).toBool();
315         qDebug() << "MainWindow::onSettingsChanged: usevolumekeys" << value;
316         grabZoomKeys(value);
317         fullScreenWindow->grabZoomKeys(value);
318     }
319 #else
320     Q_UNUSED(key);
321 #endif // Q_WS_MAEMO_5
322 }
323
324 void MainWindow::onPartLoadStart()
325 {
326     Trace t("MainWindow::onPartLoadStart");
327 #ifdef Q_WS_MAEMO_5
328     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
329 #endif
330 }
331
332 void MainWindow::onPartLoadEnd(int index)
333 {
334     Trace t("MainWindow::onPartLoadEnd");
335     bool enablePrevious = false;
336     bool enableNext = false;
337     Book *book = Library::instance()->book(mCurrent);
338     if (book) {
339         if (index > 0) {
340             enablePrevious = true;
341         }
342         if (index < (book->parts.size() - 1)) {
343             enableNext = true;
344         }
345     }
346 #ifdef Q_WS_MAEMO_5
347     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
348 #endif // Q_WS_MAEMO_5
349 }
350
351 void MainWindow::onAddBookmark()
352 {
353     Trace t("MainWindow:onAddBookmark");
354     view->addBookmark();
355 }
356
357 void MainWindow::onGoToBookmark(int index)
358 {
359     Trace t("MainWindow::onGoToBookmark");
360     Book *book = Library::instance()->book(mCurrent);
361     view->goToBookmark(book->bookmarks()[index]);
362 }
363
364 void MainWindow::showChapters()
365 {
366     Book *book = Library::instance()->book(mCurrent);
367     if (book) {
368         ChaptersDialog *chapters = new ChaptersDialog(book, this);
369         chapters->setWindowModality(Qt::WindowModal);
370         connect(chapters, SIGNAL(goToChapter(int)),
371                 this, SLOT(onGoToChapter(int)));
372         chapters->show();
373     }
374 }
375
376 void MainWindow::onGoToChapter(int index)
377 {
378     Trace t("MainWindow::onGoToChapter");
379
380     Book *book = Library::instance()->book(mCurrent);
381     if (book) {
382         int partIndex = book->partFromChapter(index);
383         if (partIndex != -1) {
384             view->goToBookmark(Book::Bookmark(partIndex, 0));
385         }
386     }
387 }
388
389 void MainWindow::timerEvent(QTimerEvent *event)
390 {
391     if (event->timerId() == preventBlankingTimer) {
392 #ifdef Q_WS_MAEMO_5
393         QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
394                            MCE_REQUEST_IF, QDBusConnection::systemBus());
395         mce.call(MCE_PREVENT_BLANK_REQ);
396 #endif // Q_WS_MAEMO_5
397         qDebug() << "MainWindow::timerEvent: Prevent display blanking";
398     }
399 }
400
401 void MainWindow::resizeEvent(QResizeEvent *e)
402 {
403     Trace t("MainWindow::resizeEvent");
404     progress->setGeometry(QRect(0, 0, e->size().width(), PROGRESS_HEIGHT));
405     qDebug() << "Toolbar height" << toolBar->height();
406 #ifdef Q_WS_MAEMO_5
407     previousButton->setGeometry(0,
408         e->size().height() - toolBar->height() - TranslucentButton::pixels,
409         TranslucentButton::pixels, TranslucentButton::pixels);
410     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
411         TranslucentButton::pixels, TranslucentButton::pixels);
412 #else
413     previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
414         TranslucentButton::pixels, TranslucentButton::pixels);
415     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
416         toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
417 #endif // Q_WS_MAEMO_5
418     qDebug() << "previousButton geometry" << previousButton->geometry();
419     previousButton->flash(1500);
420     nextButton->flash(1500);
421     QMainWindow::resizeEvent(e);
422 }
423
424 void MainWindow::about()
425 {
426     Dyalog *aboutDialog = new Dyalog(this);
427     aboutDialog->setWindowTitle(tr("About Dorian"));
428     QLabel *label = new QLabel(aboutDialog);
429     label->setTextFormat(Qt::RichText);
430     label->setOpenExternalLinks(true);
431     label->setText(tr("<b>Dorian %1</b><br><br>Copyright &copy; 2010 by "
432         "Akos Polster &lt;akos@pipacs.com&gt;<br>"
433         "Licensed under GNU General Public License, Version 3<br>"
434         "Source code: <a href='https://garage.maemo.org/projects/dorian/'>"
435         "garage.maemo.org/projects/dorian</a>").arg(DORIAN_VERSION));
436     aboutDialog->addWidget(label);
437     aboutDialog->show();
438 }
439
440
441 void MainWindow::goToNextPage()
442 {
443     nextButton->flash(1500);
444     previousButton->flash(1500);
445     view->goNextPage();
446 }
447
448 void MainWindow::goToPreviousPage()
449 {
450     nextButton->flash(1500);
451     previousButton->flash(1500);
452     view->goPreviousPage();
453 }