5c5aeaf8c40dd84c3b0ff80650aceb03b726be3e
[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
22 #ifdef DORIAN_TEST_MODEL
23 #include "modeltest.h"
24 #endif
25
26 #ifdef Q_WS_MAC
27 #   define ICON_PREFIX ":/icons/mac/"
28 #else
29 #   define ICON_PREFIX ":/icons/"
30 #endif
31
32 const Qt::WindowFlags WIN_BIG_FLAGS =
33         Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint;
34 const int WIN_BIG_TIMER = 3000;
35
36 MainWindow::MainWindow(QWidget *parent):
37         QMainWindow(parent), view(0), book(0), isFullscreen(false)
38 {
39 #ifdef Q_WS_MAEMO_5
40     setAttribute(Qt::WA_Maemo5StackedWindow, true);
41 #endif
42     setWindowTitle("Dorian");
43
44     // Book view
45     view = new BookView(this);
46     setCentralWidget(view);
47
48     // Tool bar
49     setUnifiedTitleAndToolBarOnMac(true);
50     settings = new QDialog(this);
51     toolBar = addToolBar("controls");
52     toolBar->setMovable(false);
53     toolBar->setFloatable(false);
54     toolBar->toggleViewAction()->setVisible(false);
55 #if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)
56     toolBar->setIconSize(QSize(42, 42));
57 #endif
58     previousAction = addToolBarAction(view, SLOT(goPrevious()), "previous");
59     nextAction = addToolBarAction(view, SLOT(goNext()), "next");
60     bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
61                                        "bookmarks");
62 #ifdef Q_WS_MAEMO_5
63     infoAction = new QAction(this);
64 #else
65     infoAction = addToolBarAction(this, SLOT(showInfo()), "document-properties");
66 #endif
67     libraryAction = addToolBarAction(this, SLOT(showLibrary()),
68                                      "system-file-manager");
69     settingsAction = addToolBarAction(this, SLOT(showSettings()),
70                                       "preferences-system");
71 #ifdef Q_WS_MAEMO_5
72     devToolsAction = new QAction(this);
73 #else
74     devToolsAction = addToolBarAction(this, SLOT(showDevTools()), "developer");
75     QFrame *frame = new QFrame(toolBar);
76     frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
77     toolBar->addWidget(frame);
78 #endif
79     fullScreenAction = addToolBarAction(this, SLOT(showFullScreen()),
80                                         "view-fullscreen");
81
82     // Handle model changes
83     connect(Library::instance(), SIGNAL(currentBookChanged()),
84             this, SLOT(onCurrentBookChanged()));
85
86     normalFlags = windowFlags();
87     restoreButton = new TranslucentButton("view-fullscreen", this);
88
89     // Load book on command line, or load last read book, or load default book
90     Library *library = Library::instance();
91     if (QCoreApplication::arguments().size() == 2) {
92         QString path = QCoreApplication::arguments()[1];
93         library->add(path);
94         QModelIndex index = library->find(path);
95         if (index.isValid()) {
96             library->setCurrent(index);
97         }
98     }
99     else {
100         Book *current = library->current();
101         if (current) {
102             setCurrentBook(current);
103         }
104         else {
105             if (!library->rowCount()) {
106                 library->add(":/books/2 B R 0 2 B.epub");
107             }
108             library->setCurrent(library->index(0));
109         }
110     }
111
112     // Handle settings changes
113     connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
114             this, SLOT(onSettingsChanged(const QString &)));
115     Settings::instance()->setValue("orientation",
116                                    Settings::instance()->value("orientation"));
117
118     // Handle loading chapters
119     connect(view, SIGNAL(chapterLoaded(int)), this, SLOT(onChapterLoaded(int)));
120
121 #ifdef DORIAN_TEST_MODEL
122     (void)new ModelTest(Library::instance(), this);
123 #endif
124 }
125
126 void MainWindow::onCurrentBookChanged()
127 {
128     setCurrentBook(Library::instance()->current());
129 }
130
131 void MainWindow::showNormal()
132 {
133     qDebug() << "MainWindow::showNormal";
134     isFullscreen = false;
135     setWindowFlags(normalFlags);
136     hide();
137     setGeometry(normalGeometry);
138     toolBar->show();
139     restoreButton->hide();
140     show();
141 }
142
143 void MainWindow::showFullScreen()
144 {
145     qDebug() << "MainWindow::showFullscreen";
146     normalGeometry = geometry();
147     isFullscreen = true;
148     toolBar->hide();
149     setWindowFlags(normalFlags | WIN_BIG_FLAGS);
150     showMaximized();
151     restoreButton->flash();
152 }
153
154 void MainWindow::setCurrentBook(Book *current)
155 {
156     book = current;
157     view->setBook(current);
158     setWindowTitle(current? current->title: "Dorian");
159 }
160
161 QAction *MainWindow::addToolBarAction(const QObject *receiver, const char *member,
162                                       const QString &name)
163 {
164     return toolBar->
165         addAction(QIcon(ICON_PREFIX + name + ".png"), "", receiver, member);
166 }
167
168 void MainWindow::showLibrary()
169 {
170     LibraryDialog *dialog = new LibraryDialog();
171     dialog->exec();
172 }
173
174 void MainWindow::showSettings()
175 {
176     SettingsWindow *settings = new SettingsWindow(this);
177     settings->show();
178 }
179
180 void MainWindow::showInfo()
181 {
182     if (book) {
183         InfoDialog *info = new InfoDialog(book, this);
184         info->exec();
185     }
186 }
187
188 void MainWindow::showDevTools()
189 {
190     DevTools *devTools = new DevTools();
191     devTools->exec();
192 }
193
194 void MainWindow::showBookmarks()
195 {
196     if (book) {
197         BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
198         int ret = bookmarks->exec();
199         if (ret > 0) {
200             int index = ret - 1;
201             view->goToBookmark(book->bookmarks()[index]);
202         }
203         else if (ret < 0) {
204             view->addBookmark();
205         }
206     }
207 }
208
209 void MainWindow::MOUSE_ACTIVATE_EVENT(QMouseEvent *event)
210 {
211     qDebug() << "MainWindow::mousePress/ReleaseEvent at" << event->pos()
212             << "against" << fullScreenZone();
213     if (isFullscreen && fullScreenZone().contains(event->x(), event->y())) {
214         qDebug() << " In fullScreenZone";
215         showNormal();
216     }
217     QMainWindow::MOUSE_ACTIVATE_EVENT(event);
218 }
219
220 QRect MainWindow::fullScreenZone() const
221 {
222     return QRect(width() / 2 - 45, height() - 104, 95, 95);
223 }
224
225 void MainWindow::resizeEvent(QResizeEvent *event)
226 {
227     (void)event;
228     restoreButton->setGeometry(fullScreenZone());
229 }
230
231 void MainWindow::closeEvent(QCloseEvent *event)
232 {
233     qDebug() << "MainWindow::closeEvent";
234     view->setLastBookmark();
235     event->accept();
236 }
237
238 void MainWindow::onSettingsChanged(const QString &key)
239 {
240 #ifdef Q_WS_MAEMO_5
241     if (key == "orientation") {
242         QString value = Settings::instance()->value(key).toString();
243         if (value == "portrait") {
244             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
245             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
246         }
247         else {
248             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
249             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
250         }
251     }
252 #else
253     Q_UNUSED(key);
254 #endif // Q_WS_MAEMO_5
255 }
256
257 void MainWindow::onChapterLoaded(int index)
258 {
259     bool enablePrevious = false;
260     bool enableNext = false;
261     if (book) {
262         if (index > 0) {
263             enablePrevious = true;
264         }
265         if (index < (book->toc.size() - 1)) {
266             enableNext = true;
267         }
268     }
269 #ifdef Q_WS_MAEMO_5
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 }