Facelift bookmarks dialog. Add Maemo-friendly dialog box class.
[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), 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(nowReadingChanged()),
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->setNowReading(index);
97         }
98     }
99     else {
100         QModelIndex index = library->nowReading();
101         if (index.isValid()) {
102             library->setNowReading(index);
103         }
104         else {
105             if (!library->rowCount()) {
106                 library->add(":/books/2 B R 0 2 B.epub");
107             }
108             library->setNowReading(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(chapterLoadStart(int)),
120             this, SLOT(onChapterLoadStart()));
121     connect(view, SIGNAL(chapterLoadEnd(int)),
122             this, SLOT(onChapterLoadEnd(int)));
123
124 #ifdef DORIAN_TEST_MODEL
125     (void)new ModelTest(Library::instance(), this);
126 #endif
127 }
128
129 void MainWindow::onCurrentBookChanged()
130 {
131     setCurrentBook(Library::instance()->nowReading());
132 }
133
134 void MainWindow::showNormal()
135 {
136     qDebug() << "MainWindow::showNormal";
137     isFullscreen = false;
138     setWindowFlags(normalFlags);
139     hide();
140     setGeometry(normalGeometry);
141     toolBar->show();
142     restoreButton->hide();
143     show();
144 }
145
146 void MainWindow::showFullScreen()
147 {
148     qDebug() << "MainWindow::showFullscreen";
149     normalGeometry = geometry();
150     isFullscreen = true;
151     toolBar->hide();
152     setWindowFlags(normalFlags | WIN_BIG_FLAGS);
153     showMaximized();
154     restoreButton->flash();
155 }
156
157 void MainWindow::setCurrentBook(const QModelIndex &current)
158 {
159     mCurrent = current;
160     Book *book = Library::instance()->book(current);
161     view->setBook(book);
162     setWindowTitle(book? book->name(): tr("Dorian"));
163 }
164
165 QAction *MainWindow::addToolBarAction(const QObject *receiver,
166                                       const char *member,
167                                       const QString &name)
168 {
169     return toolBar->
170         addAction(QIcon(ICON_PREFIX + name + ".png"), "", receiver, member);
171 }
172
173 void MainWindow::showLibrary()
174 {
175     LibraryDialog *dialog = new LibraryDialog(this);
176     dialog->show();
177 }
178
179 void MainWindow::showSettings()
180 {
181     SettingsWindow *settings = new SettingsWindow(this);
182     settings->show();
183 }
184
185 void MainWindow::showInfo()
186 {
187     if (mCurrent.isValid()) {
188         InfoDialog *info =
189             new InfoDialog(Library::instance()->book(mCurrent), this);
190         info->exec();
191     }
192 }
193
194 void MainWindow::showDevTools()
195 {
196     DevTools *devTools = new DevTools();
197     devTools->exec();
198 }
199
200 void MainWindow::showBookmarks()
201 {
202     Book *book = Library::instance()->book(mCurrent);
203     if (book) {
204         BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
205         bookmarks->setWindowModality(Qt::WindowModal);
206         connect(bookmarks, SIGNAL(addBookmark()), this, SLOT(onAddBookmark()));
207         connect(bookmarks, SIGNAL(goToBookmark(int)),
208                 this, SLOT(onGoToBookmark(int)));
209         bookmarks->show();
210     }
211 }
212
213 void MainWindow::MOUSE_ACTIVATE_EVENT(QMouseEvent *event)
214 {
215     qDebug() << "MainWindow::mousePress/ReleaseEvent at" << event->pos()
216             << "against" << fullScreenZone();
217     if (isFullscreen && fullScreenZone().contains(event->x(), event->y())) {
218         qDebug() << " In fullScreenZone";
219         showNormal();
220     }
221     QMainWindow::MOUSE_ACTIVATE_EVENT(event);
222 }
223
224 QRect MainWindow::fullScreenZone() const
225 {
226     return QRect(width() / 2 - 45, height() - 104, 95, 95);
227 }
228
229 void MainWindow::resizeEvent(QResizeEvent *event)
230 {
231     (void)event;
232     restoreButton->setGeometry(fullScreenZone());
233 }
234
235 void MainWindow::closeEvent(QCloseEvent *event)
236 {
237     qDebug() << "MainWindow::closeEvent";
238     view->setLastBookmark();
239     event->accept();
240 }
241
242 void MainWindow::onSettingsChanged(const QString &key)
243 {
244 #ifdef Q_WS_MAEMO_5
245     if (key == "orientation") {
246         QString value = Settings::instance()->value(key).toString();
247         if (value == "portrait") {
248             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
249             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
250         }
251         else {
252             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
253             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
254         }
255     }
256 #else
257     Q_UNUSED(key);
258 #endif // Q_WS_MAEMO_5
259 }
260
261 void MainWindow::onChapterLoadStart()
262 {
263 #ifdef Q_WS_MAEMO_5
264     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
265 #endif
266 }
267
268 void MainWindow::onChapterLoadEnd(int index)
269 {
270     bool enablePrevious = false;
271     bool enableNext = false;
272     Book *book = Library::instance()->book(mCurrent);
273     if (book) {
274         if (index > 0) {
275             enablePrevious = true;
276         }
277         if (index < (book->toc.size() - 1)) {
278             enableNext = true;
279         }
280     }
281 #ifdef Q_WS_MAEMO_5
282     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
283     previousAction->setIcon(QIcon(enablePrevious?
284         ":/icons/previous.png" : ":/icons/previous-disabled.png"));
285     nextAction->setIcon(QIcon(enableNext?
286         ":/icons/next.png": ":/icons/next-disabled.png"));
287 #endif // Q_WS_MAEMO_5
288     previousAction->setEnabled(enablePrevious);
289     nextAction->setEnabled(enableNext);
290 }
291
292 void MainWindow::onAddBookmark()
293 {
294     view->addBookmark();
295 }
296
297 void MainWindow::onGoToBookmark(int index)
298 {
299     Book *book = Library::instance()->book(mCurrent);
300     view->goToBookmark(book->bookmarks()[index]);
301 }