Add chapter navigation. Move some toolbar items to the menu on Maemo.
[dorian] / mainwindow.cpp
1 #include <QtGui>
2 #include <QtDebug>
3 #include <QDir>
4 #include <QCoreApplication>
5 #include <QFileInfo>
6 #ifdef Q_WS_MAEMO_5
7 #   include <QtMaemo5/QMaemo5InformationBox>
8 #endif
9
10 #include "bookview.h"
11 #include "book.h"
12 #include "library.h"
13 #include "infodialog.h"
14 #include "librarydialog.h"
15 #include "devtools.h"
16 #include "mainwindow.h"
17 #include "translucentbutton.h"
18 #include "settingswindow.h"
19 #include "bookmarksdialog.h"
20 #include "settings.h"
21 #include "chaptersdialog.h"
22
23 #ifdef DORIAN_TEST_MODEL
24 #include "modeltest.h"
25 #endif
26
27 #ifdef Q_WS_MAC
28 #   define ICON_PREFIX ":/icons/mac/"
29 #else
30 #   define ICON_PREFIX ":/icons/"
31 #endif
32
33 const Qt::WindowFlags WIN_BIG_FLAGS =
34         Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint;
35 const int WIN_BIG_TIMER = 3000;
36
37 MainWindow::MainWindow(QWidget *parent):
38         QMainWindow(parent), view(0), isFullscreen(false)
39 {
40 #ifdef Q_WS_MAEMO_5
41     setAttribute(Qt::WA_Maemo5StackedWindow, true);
42 #endif
43     setWindowTitle("Dorian");
44
45     // Book view
46     view = new BookView(this);
47     setCentralWidget(view);
48
49     // Tool bar
50     setUnifiedTitleAndToolBarOnMac(true);
51     settings = new QDialog(this);
52     toolBar = addToolBar("controls");
53     toolBar->setMovable(false);
54     toolBar->setFloatable(false);
55     toolBar->toggleViewAction()->setVisible(false);
56 #if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)
57     toolBar->setIconSize(QSize(42, 42));
58 #endif
59
60     previousAction = addToolBarAction(view, SLOT(goPrevious()), "previous");
61     nextAction = addToolBarAction(view, SLOT(goNext()), "next");
62     chaptersAction = addToolBarAction(this, SLOT(showChapters()), "chapters");
63     bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()), "bookmarks");
64
65 #ifdef Q_WS_MAEMO_5
66     infoAction = menuBar()->addAction(tr("Book details"));
67     connect(infoAction, SIGNAL(triggered()), this, SLOT(showInfo()));
68     libraryAction = menuBar()->addAction(tr("Library"));
69     connect(libraryAction, SIGNAL(triggered()), this, SLOT(showLibrary()));
70     settingsAction = menuBar()->addAction(tr("Settings"));
71     connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
72     devToolsAction = menuBar()->addAction(tr("Developer"));
73     connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
74 #else
75     infoAction = addToolBarAction(this, SLOT(showInfo()), "document-properties");
76     libraryAction = addToolBarAction(this, SLOT(showLibrary()),
77                                      "system-file-manager");
78     settingsAction = addToolBarAction(this, SLOT(showSettings()),
79                                       "preferences-system");
80     devToolsAction = addToolBarAction(this, SLOT(showDevTools()), "developer");
81 #endif // Q_WS_MAEMO_5
82
83     QFrame *frame = new QFrame(toolBar);
84     frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
85     toolBar->addWidget(frame);
86
87     fullScreenAction = addToolBarAction(this, SLOT(showFullScreen()),
88                                         "view-fullscreen");
89
90     // Handle model changes
91     connect(Library::instance(), SIGNAL(nowReadingChanged()),
92             this, SLOT(onCurrentBookChanged()));
93
94     normalFlags = windowFlags();
95     restoreButton = new TranslucentButton("view-fullscreen", this);
96
97     // Load book on command line, or load last read book, or load default book
98     Library *library = Library::instance();
99     if (QCoreApplication::arguments().size() == 2) {
100         QString path = QCoreApplication::arguments()[1];
101         library->add(path);
102         QModelIndex index = library->find(path);
103         if (index.isValid()) {
104             library->setNowReading(index);
105         }
106     }
107     else {
108         QModelIndex index = library->nowReading();
109         if (index.isValid()) {
110             library->setNowReading(index);
111         }
112         else {
113             if (!library->rowCount()) {
114                 library->add(":/books/2 B R 0 2 B.epub");
115             }
116             library->setNowReading(library->index(0));
117         }
118     }
119
120     // Handle settings changes
121     connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
122             this, SLOT(onSettingsChanged(const QString &)));
123     Settings::instance()->setValue("orientation",
124                                    Settings::instance()->value("orientation"));
125
126     // Handle loading chapters
127     connect(view, SIGNAL(chapterLoadStart(int)),
128             this, SLOT(onChapterLoadStart()));
129     connect(view, SIGNAL(chapterLoadEnd(int)),
130             this, SLOT(onChapterLoadEnd(int)));
131
132 #ifdef DORIAN_TEST_MODEL
133     (void)new ModelTest(Library::instance(), this);
134 #endif
135 }
136
137 void MainWindow::onCurrentBookChanged()
138 {
139     setCurrentBook(Library::instance()->nowReading());
140 }
141
142 void MainWindow::showNormal()
143 {
144     qDebug() << "MainWindow::showNormal";
145     isFullscreen = false;
146     setWindowFlags(normalFlags);
147     hide();
148     setGeometry(normalGeometry);
149     toolBar->show();
150     restoreButton->hide();
151     show();
152 }
153
154 void MainWindow::showFullScreen()
155 {
156     qDebug() << "MainWindow::showFullscreen";
157     normalGeometry = geometry();
158     isFullscreen = true;
159     toolBar->hide();
160     setWindowFlags(normalFlags | WIN_BIG_FLAGS);
161     showMaximized();
162     restoreButton->flash();
163 }
164
165 void MainWindow::setCurrentBook(const QModelIndex &current)
166 {
167     mCurrent = current;
168     Book *book = Library::instance()->book(current);
169     view->setBook(book);
170     setWindowTitle(book? book->name(): tr("Dorian"));
171 }
172
173 QAction *MainWindow::addToolBarAction(const QObject *receiver,
174                                       const char *member,
175                                       const QString &name)
176 {
177     return toolBar->
178         addAction(QIcon(ICON_PREFIX + name + ".png"), "", receiver, member);
179 }
180
181 void MainWindow::showLibrary()
182 {
183     LibraryDialog *dialog = new LibraryDialog(this);
184     dialog->show();
185 }
186
187 void MainWindow::showSettings()
188 {
189     SettingsWindow *settings = new SettingsWindow(this);
190     settings->show();
191 }
192
193 void MainWindow::showInfo()
194 {
195     if (mCurrent.isValid()) {
196         InfoDialog *info =
197             new InfoDialog(Library::instance()->book(mCurrent), this);
198         info->exec();
199     }
200 }
201
202 void MainWindow::showDevTools()
203 {
204     DevTools *devTools = new DevTools();
205     devTools->exec();
206 }
207
208 void MainWindow::showBookmarks()
209 {
210     Book *book = Library::instance()->book(mCurrent);
211     if (book) {
212         BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
213         bookmarks->setWindowModality(Qt::WindowModal);
214         connect(bookmarks, SIGNAL(addBookmark()), this, SLOT(onAddBookmark()));
215         connect(bookmarks, SIGNAL(goToBookmark(int)),
216                 this, SLOT(onGoToBookmark(int)));
217         bookmarks->show();
218     }
219 }
220
221 void MainWindow::MOUSE_ACTIVATE_EVENT(QMouseEvent *event)
222 {
223     qDebug() << "MainWindow::mousePress/ReleaseEvent at" << event->pos()
224             << "against" << fullScreenZone();
225     if (isFullscreen && fullScreenZone().contains(event->x(), event->y())) {
226         qDebug() << " In fullScreenZone";
227         showNormal();
228     }
229     QMainWindow::MOUSE_ACTIVATE_EVENT(event);
230 }
231
232 QRect MainWindow::fullScreenZone() const
233 {
234     return QRect(width() / 2 - 45, height() - 104, 95, 95);
235 }
236
237 void MainWindow::resizeEvent(QResizeEvent *event)
238 {
239     (void)event;
240     restoreButton->setGeometry(fullScreenZone());
241 }
242
243 void MainWindow::closeEvent(QCloseEvent *event)
244 {
245     qDebug() << "MainWindow::closeEvent";
246     view->setLastBookmark();
247     event->accept();
248 }
249
250 void MainWindow::onSettingsChanged(const QString &key)
251 {
252 #ifdef Q_WS_MAEMO_5
253     if (key == "orientation") {
254         QString value = Settings::instance()->value(key).toString();
255         if (value == "portrait") {
256             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
257             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
258         }
259         else {
260             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
261             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
262         }
263     }
264 #else
265     Q_UNUSED(key);
266 #endif // Q_WS_MAEMO_5
267 }
268
269 void MainWindow::onChapterLoadStart()
270 {
271 #ifdef Q_WS_MAEMO_5
272     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
273 #endif
274 }
275
276 void MainWindow::onChapterLoadEnd(int index)
277 {
278     bool enablePrevious = false;
279     bool enableNext = false;
280     Book *book = Library::instance()->book(mCurrent);
281     if (book) {
282         if (index > 0) {
283             enablePrevious = true;
284         }
285         if (index < (book->toc.size() - 1)) {
286             enableNext = true;
287         }
288     }
289 #ifdef Q_WS_MAEMO_5
290     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
291     previousAction->setIcon(QIcon(enablePrevious?
292         ":/icons/previous.png" : ":/icons/previous-disabled.png"));
293     nextAction->setIcon(QIcon(enableNext?
294         ":/icons/next.png": ":/icons/next-disabled.png"));
295 #endif // Q_WS_MAEMO_5
296     previousAction->setEnabled(enablePrevious);
297     nextAction->setEnabled(enableNext);
298 }
299
300 void MainWindow::onAddBookmark()
301 {
302     view->addBookmark();
303 }
304
305 void MainWindow::onGoToBookmark(int index)
306 {
307     Book *book = Library::instance()->book(mCurrent);
308     view->goToBookmark(book->bookmarks()[index]);
309 }
310
311 void MainWindow::showChapters()
312 {
313     Book *book = Library::instance()->book(mCurrent);
314     if (book) {
315         ChaptersDialog *chapters = new ChaptersDialog(book, this);
316         chapters->setWindowModality(Qt::WindowModal);
317         connect(chapters, SIGNAL(goToChapter(int)),
318                 this, SLOT(onGoToChapter(int)));
319         chapters->show();
320     }
321 }
322
323 void MainWindow::onGoToChapter(int index)
324 {
325     view->goToBookmark(Book::Bookmark(index, 0));
326 }
327