Make full screen switching more robust. Add in/out traces.
[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 "settingswindow.h"
18 #include "bookmarksdialog.h"
19 #include "settings.h"
20 #include "chaptersdialog.h"
21 #include "fullscreenwindow.h"
22 #include "trace.h"
23
24 #ifdef DORIAN_TEST_MODEL
25 #include "modeltest.h"
26 #endif
27
28 #ifdef Q_WS_MAC
29 #   define ICON_PREFIX ":/icons/mac/"
30 #else
31 #   define ICON_PREFIX ":/icons/"
32 #endif
33
34 MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), view(0)
35 {
36 #ifdef Q_WS_MAEMO_5
37     setAttribute(Qt::WA_Maemo5StackedWindow, true);
38 #endif
39     setWindowTitle("Dorian");
40
41     // Central widget. Must be an intermediate because of reparenting the book view
42     QFrame *central = new QFrame(this);
43     QVBoxLayout *layout = new QVBoxLayout(central);
44     layout->setMargin(0);
45     central->setLayout(layout);
46     setCentralWidget(central);
47
48     // Book view
49     view = new BookView(central);
50     view->show();
51     layout->addWidget(view);
52
53     // Tool bar
54     setUnifiedTitleAndToolBarOnMac(true);
55     settings = new QDialog(this);
56     toolBar = addToolBar("controls");
57     toolBar->setMovable(false);
58     toolBar->setFloatable(false);
59     toolBar->toggleViewAction()->setVisible(false);
60 #if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)
61     toolBar->setIconSize(QSize(42, 42));
62 #endif
63
64     previousAction = addToolBarAction(view, SLOT(goPrevious()), "previous");
65     nextAction = addToolBarAction(view, SLOT(goNext()), "next");
66     chaptersAction = addToolBarAction(this, SLOT(showChapters()), "chapters");
67     bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()), "bookmarks");
68
69 #ifdef Q_WS_MAEMO_5
70     infoAction = menuBar()->addAction(tr("Book details"));
71     connect(infoAction, SIGNAL(triggered()), this, SLOT(showInfo()));
72     libraryAction = menuBar()->addAction(tr("Library"));
73     connect(libraryAction, SIGNAL(triggered()), this, SLOT(showLibrary()));
74     settingsAction = menuBar()->addAction(tr("Settings"));
75     connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
76     devToolsAction = menuBar()->addAction(tr("Developer"));
77     connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
78 #else
79     infoAction = addToolBarAction(this, SLOT(showInfo()), "document-properties");
80     libraryAction = addToolBarAction(this, SLOT(showLibrary()),
81                                      "system-file-manager");
82     settingsAction = addToolBarAction(this, SLOT(showSettings()),
83                                       "preferences-system");
84     devToolsAction = addToolBarAction(this, SLOT(showDevTools()), "developer");
85 #endif // Q_WS_MAEMO_5
86
87     QFrame *frame = new QFrame(toolBar);
88     frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
89     toolBar->addWidget(frame);
90
91     fullScreenAction = addToolBarAction(this, SLOT(showBig()), "view-fullscreen");
92
93     // Handle model changes
94     connect(Library::instance(), SIGNAL(nowReadingChanged()),
95             this, SLOT(onCurrentBookChanged()));
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     // Shadow window for full screen
133     fullScreenWindow = new FullScreenWindow(this);
134     connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
135
136 #ifdef DORIAN_TEST_MODEL
137     (void)new ModelTest(Library::instance(), this);
138 #endif
139 }
140
141 void MainWindow::onCurrentBookChanged()
142 {
143     setCurrentBook(Library::instance()->nowReading());
144 }
145
146 void MainWindow::showRegular()
147 {
148     Trace t("MainWindow::showRegular");
149     fullScreenWindow->hide();
150     fullScreenWindow->leaveChild();
151     view->setParent(centralWidget());
152     centralWidget()->layout()->addWidget(view);
153 }
154
155 void MainWindow::showBig()
156 {
157     Trace t("MainWindow::showBig");
158     centralWidget()->layout()->removeWidget(view);
159     fullScreenWindow->takeChild(view);
160     fullScreenWindow->showFullScreen();
161 }
162
163 void MainWindow::setCurrentBook(const QModelIndex &current)
164 {
165     mCurrent = current;
166     Book *book = Library::instance()->book(current);
167     view->setBook(book);
168     setWindowTitle(book? book->name(): tr("Dorian"));
169 }
170
171 QAction *MainWindow::addToolBarAction(const QObject *receiver,
172                                       const char *member,
173                                       const QString &name)
174 {
175     return toolBar->
176         addAction(QIcon(ICON_PREFIX + name + ".png"), "", receiver, member);
177 }
178
179 void MainWindow::showLibrary()
180 {
181     LibraryDialog *dialog = new LibraryDialog(this);
182     dialog->show();
183 }
184
185 void MainWindow::showSettings()
186 {
187     SettingsWindow *settings = new SettingsWindow(this);
188     settings->show();
189 }
190
191 void MainWindow::showInfo()
192 {
193     if (mCurrent.isValid()) {
194         InfoDialog *info =
195             new InfoDialog(Library::instance()->book(mCurrent), this);
196         info->exec();
197     }
198 }
199
200 void MainWindow::showDevTools()
201 {
202     DevTools *devTools = new DevTools();
203     devTools->exec();
204 }
205
206 void MainWindow::showBookmarks()
207 {
208     Book *book = Library::instance()->book(mCurrent);
209     if (book) {
210         BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
211         bookmarks->setWindowModality(Qt::WindowModal);
212         connect(bookmarks, SIGNAL(addBookmark()), this, SLOT(onAddBookmark()));
213         connect(bookmarks, SIGNAL(goToBookmark(int)),
214                 this, SLOT(onGoToBookmark(int)));
215         bookmarks->show();
216     }
217 }
218
219 void MainWindow::closeEvent(QCloseEvent *event)
220 {
221     Trace t("MainWindow::closeEvent");
222     view->setLastBookmark();
223     event->accept();
224 }
225
226 void MainWindow::onSettingsChanged(const QString &key)
227 {
228     Trace t("MainWindow::onSettingsChanged");
229 #ifdef Q_WS_MAEMO_5
230     if (key == "orientation") {
231         QString value = Settings::instance()->value(key).toString();
232         if (value == "portrait") {
233             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
234             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
235         }
236         else {
237             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
238             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
239         }
240     }
241 #else
242     Q_UNUSED(key);
243 #endif // Q_WS_MAEMO_5
244 }
245
246 void MainWindow::onChapterLoadStart()
247 {
248     Trace t("MainWindow::onChapterLoadStart");
249 #ifdef Q_WS_MAEMO_5
250     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
251 #endif
252 }
253
254 void MainWindow::onChapterLoadEnd(int index)
255 {
256     Trace t("MainWindow::onChapterLoadEnd");
257     bool enablePrevious = false;
258     bool enableNext = false;
259     Book *book = Library::instance()->book(mCurrent);
260     if (book) {
261         if (index > 0) {
262             enablePrevious = true;
263         }
264         if (index < (book->toc.size() - 1)) {
265             enableNext = true;
266         }
267     }
268 #ifdef Q_WS_MAEMO_5
269     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
270     previousAction->setIcon(QIcon(enablePrevious?
271         ":/icons/previous.png" : ":/icons/previous-disabled.png"));
272     nextAction->setIcon(QIcon(enableNext?
273         ":/icons/next.png": ":/icons/next-disabled.png"));
274 #endif // Q_WS_MAEMO_5
275     previousAction->setEnabled(enablePrevious);
276     nextAction->setEnabled(enableNext);
277 }
278
279 void MainWindow::onAddBookmark()
280 {
281     Trace t("MainWindow:onAddBookmark");
282     view->addBookmark();
283 }
284
285 void MainWindow::onGoToBookmark(int index)
286 {
287     Trace t("MainWindow::onGoToBookmark");
288     Book *book = Library::instance()->book(mCurrent);
289     view->goToBookmark(book->bookmarks()[index]);
290 }
291
292 void MainWindow::showChapters()
293 {
294     Book *book = Library::instance()->book(mCurrent);
295     if (book) {
296         ChaptersDialog *chapters = new ChaptersDialog(book, this);
297         chapters->setWindowModality(Qt::WindowModal);
298         connect(chapters, SIGNAL(goToChapter(int)),
299                 this, SLOT(onGoToChapter(int)));
300         chapters->show();
301     }
302 }
303
304 void MainWindow::onGoToChapter(int index)
305 {
306     view->goToBookmark(Book::Bookmark(index, 0));
307 }