Increase default trace level. Set level from Developer dialog box.
[dorian] / mainwindow.cpp
1 #include <QtGui>
2 #include <QtDebug>
3 #include <QDir>
4 #include <QApplication>
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_Maemo5LandscapeOrientation, false);
234             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
235         }
236         else {
237             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
238             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
239         }
240
241         // FIXME: Orientation change should re-activate the window but it doesn't.
242         // And I have no idea how to force it
243
244         // view->restoreLastBookmark();
245         // view->setFocus();
246         // raise();
247
248         // QApplication::setActiveWindow(this);
249         // activateWindow();
250         // QEvent *enter = new QEvent(QEvent::Enter);
251         // QApplication::postEvent(view, enter);
252
253         // view->grabKeyboard();
254         // showNormal();
255
256         // QTestEventList events;
257         // events.addMouseClick(Qt::LeftButton);
258         // events.simulate(view);
259     }
260 #else
261     Q_UNUSED(key);
262 #endif // Q_WS_MAEMO_5
263 }
264
265 void MainWindow::onChapterLoadStart()
266 {
267     Trace t("MainWindow::onChapterLoadStart");
268 #ifdef Q_WS_MAEMO_5
269     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
270 #endif
271 }
272
273 void MainWindow::onChapterLoadEnd(int index)
274 {
275     Trace t("MainWindow::onChapterLoadEnd");
276     bool enablePrevious = false;
277     bool enableNext = false;
278     Book *book = Library::instance()->book(mCurrent);
279     if (book) {
280         if (index > 0) {
281             enablePrevious = true;
282         }
283         if (index < (book->toc.size() - 1)) {
284             enableNext = true;
285         }
286     }
287 #ifdef Q_WS_MAEMO_5
288     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
289     previousAction->setIcon(QIcon(enablePrevious?
290         ":/icons/previous.png" : ":/icons/previous-disabled.png"));
291     nextAction->setIcon(QIcon(enableNext?
292         ":/icons/next.png": ":/icons/next-disabled.png"));
293 #endif // Q_WS_MAEMO_5
294     previousAction->setEnabled(enablePrevious);
295     nextAction->setEnabled(enableNext);
296 }
297
298 void MainWindow::onAddBookmark()
299 {
300     Trace t("MainWindow:onAddBookmark");
301     view->addBookmark();
302 }
303
304 void MainWindow::onGoToBookmark(int index)
305 {
306     Trace t("MainWindow::onGoToBookmark");
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 }