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