Add new widget ToolButtonBox. Add setting to prevent display blanking.
[dorian] / mainwindow.cpp
1 #include <QtGui>
2 #include <QtDebug>
3 #include <QDir>
4 #include <QApplication>
5 #include <QFileInfo>
6
7 #ifdef Q_WS_MAEMO_5
8 #   include <QtMaemo5/QMaemo5InformationBox>
9 #   include <QtDBus>
10 #   include <mce/mode-names.h>
11 #   include <mce/dbus-names.h>
12 #endif // Q_WS_MAEMO_5
13
14 #include "bookview.h"
15 #include "book.h"
16 #include "library.h"
17 #include "infodialog.h"
18 #include "librarydialog.h"
19 #include "devtools.h"
20 #include "mainwindow.h"
21 #include "settingswindow.h"
22 #include "bookmarksdialog.h"
23 #include "settings.h"
24 #include "chaptersdialog.h"
25 #include "fullscreenwindow.h"
26 #include "trace.h"
27
28 #ifdef DORIAN_TEST_MODEL
29 #include "modeltest.h"
30 #endif
31
32 #ifdef Q_WS_MAC
33 #   define ICON_PREFIX ":/icons/mac/"
34 #else
35 #   define ICON_PREFIX ":/icons/"
36 #endif
37
38 MainWindow::MainWindow(QWidget *parent):
39     QMainWindow(parent), view(0), preventBlankingTimer(-1)
40 {
41 #ifdef Q_WS_MAEMO_5
42     setAttribute(Qt::WA_Maemo5StackedWindow, true);
43 #endif
44     setWindowTitle("Dorian");
45
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);
49     layout->setMargin(0);
50     central->setLayout(layout);
51     setCentralWidget(central);
52
53     // Book view
54     view = new BookView(central);
55     view->show();
56     layout->addWidget(view);
57
58     // Tool bar
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));
67 #endif
68
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");
73
74 #ifdef Q_WS_MAEMO_5
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()));
83 #else
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
91
92     QFrame *frame = new QFrame(toolBar);
93     frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
94     toolBar->addWidget(frame);
95
96     fullScreenAction = addToolBarAction(this, SLOT(showBig()), "view-fullscreen");
97
98     // Handle model changes
99     connect(Library::instance(), SIGNAL(nowReadingChanged()),
100             this, SLOT(onCurrentBookChanged()));
101
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];
106         library->add(path);
107         QModelIndex index = library->find(path);
108         if (index.isValid()) {
109             library->setNowReading(index);
110         }
111     }
112     else {
113         QModelIndex index = library->nowReading();
114         if (index.isValid()) {
115             library->setNowReading(index);
116         }
117         else {
118             if (!library->rowCount()) {
119                 library->add(":/books/2 B R 0 2 B.epub");
120             }
121             library->setNowReading(library->index(0));
122         }
123     }
124
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"));
131
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)));
137
138     // Shadow window for full screen
139     fullScreenWindow = new FullScreenWindow(this);
140     connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
141
142 #ifdef DORIAN_TEST_MODEL
143     (void)new ModelTest(Library::instance(), this);
144 #endif
145 }
146
147 void MainWindow::onCurrentBookChanged()
148 {
149     setCurrentBook(Library::instance()->nowReading());
150 }
151
152 void MainWindow::showRegular()
153 {
154     Trace t("MainWindow::showRegular");
155     fullScreenWindow->hide();
156     fullScreenWindow->leaveChild();
157     view->setParent(centralWidget());
158     centralWidget()->layout()->addWidget(view);
159 }
160
161 void MainWindow::showBig()
162 {
163     Trace t("MainWindow::showBig");
164     centralWidget()->layout()->removeWidget(view);
165     fullScreenWindow->takeChild(view);
166     fullScreenWindow->showFullScreen();
167 }
168
169 void MainWindow::setCurrentBook(const QModelIndex &current)
170 {
171     mCurrent = current;
172     Book *book = Library::instance()->book(current);
173     view->setBook(book);
174     setWindowTitle(book? book->name(): tr("Dorian"));
175 }
176
177 QAction *MainWindow::addToolBarAction(const QObject *receiver,
178                                       const char *member,
179                                       const QString &name)
180 {
181     return toolBar->
182         addAction(QIcon(ICON_PREFIX + name + ".png"), "", receiver, member);
183 }
184
185 void MainWindow::showLibrary()
186 {
187     LibraryDialog *dialog = new LibraryDialog(this);
188     dialog->show();
189 }
190
191 void MainWindow::showSettings()
192 {
193     SettingsWindow *settings = new SettingsWindow(this);
194     settings->show();
195 }
196
197 void MainWindow::showInfo()
198 {
199     if (mCurrent.isValid()) {
200         InfoDialog *info =
201             new InfoDialog(Library::instance()->book(mCurrent), this);
202         info->exec();
203     }
204 }
205
206 void MainWindow::showDevTools()
207 {
208     DevTools *devTools = new DevTools();
209     devTools->exec();
210 }
211
212 void MainWindow::showBookmarks()
213 {
214     Book *book = Library::instance()->book(mCurrent);
215     if (book) {
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)));
221         bookmarks->show();
222     }
223 }
224
225 void MainWindow::closeEvent(QCloseEvent *event)
226 {
227     Trace t("MainWindow::closeEvent");
228     view->setLastBookmark();
229     event->accept();
230 }
231
232 void MainWindow::onSettingsChanged(const QString &key)
233 {
234 #ifdef Q_WS_MAEMO_5
235     if (key == "orientation") {
236         QString value = Settings::instance()->value(key).toString();
237         Trace::trace(QString("MainWindow::onSettingsChanged: orientation %1").
238                      arg(value));
239         if (value == "portrait") {
240             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
241             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
242         }
243         else {
244             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
245             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
246         }
247     } else if (key == "lightson") {
248         bool enable = Settings::instance()->value(key, false).toBool();
249         Trace::trace(QString("MainWindow::onSettingsChanged: lightson: %1").
250                      arg(enable));
251         killTimer(preventBlankingTimer);
252         if (enable) {
253             preventBlankingTimer = startTimer(29 * 1000);
254         }
255     }
256 #else
257     Q_UNUSED(key);
258 #endif // Q_WS_MAEMO_5
259 }
260
261 void MainWindow::onChapterLoadStart()
262 {
263     Trace t("MainWindow::onChapterLoadStart");
264 #ifdef Q_WS_MAEMO_5
265     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
266 #endif
267 }
268
269 void MainWindow::onChapterLoadEnd(int index)
270 {
271     Trace t("MainWindow::onChapterLoadEnd");
272     bool enablePrevious = false;
273     bool enableNext = false;
274     Book *book = Library::instance()->book(mCurrent);
275     if (book) {
276         if (index > 0) {
277             enablePrevious = true;
278         }
279         if (index < (book->toc.size() - 1)) {
280             enableNext = true;
281         }
282     }
283 #ifdef Q_WS_MAEMO_5
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);
292 }
293
294 void MainWindow::onAddBookmark()
295 {
296     Trace t("MainWindow:onAddBookmark");
297     view->addBookmark();
298 }
299
300 void MainWindow::onGoToBookmark(int index)
301 {
302     Trace t("MainWindow::onGoToBookmark");
303     Book *book = Library::instance()->book(mCurrent);
304     view->goToBookmark(book->bookmarks()[index]);
305 }
306
307 void MainWindow::showChapters()
308 {
309     Book *book = Library::instance()->book(mCurrent);
310     if (book) {
311         ChaptersDialog *chapters = new ChaptersDialog(book, this);
312         chapters->setWindowModality(Qt::WindowModal);
313         connect(chapters, SIGNAL(goToChapter(int)),
314                 this, SLOT(onGoToChapter(int)));
315         chapters->show();
316     }
317 }
318
319 void MainWindow::onGoToChapter(int index)
320 {
321     view->goToBookmark(Book::Bookmark(index, 0));
322 }
323
324 void MainWindow::timerEvent(QTimerEvent *event)
325 {
326     if (event->timerId() == preventBlankingTimer) {
327 #ifdef Q_WS_MAEMO_5
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");
333     }
334 }