Refactoring. Added comments.
[mebook:mebook.git] / ui / mainwindow.cpp
1 #include "mainwindow.h"
2 #include "bookmark.h"
3 #include "addbookmarkdialog.h"
4 #include "addannotationdialog.h"
5 #include <QAction>
6 #include <QMenu>
7 #include "listviewdialog.h"
8
9 #include <QtGui/QX11Info>
10 #include <X11/Xlib.h>
11 #include <X11/Xatom.h>
12
13 MainWindow::MainWindow(osso_context_t *context, QWidget *parent) : QMainWindow(parent)
14 {
15     bookView = new BookView(this);
16     setCentralWidget(bookView); 
17
18     //Install eventFilter to bookView's viewport so we can intercept doubleclick event.
19     bookView->viewport()->installEventFilter(this);
20
21     
22     connect(this, SIGNAL(volumeUpPressed()), bookView, SLOT(volumeUpPressed()));
23     connect(this, SIGNAL(volumeDownPressed()), bookView, SLOT(volumeDownPressed()));
24
25     toolbar = new QToolBar("toolbar", this);
26     toolbar->setContextMenuPolicy(Qt::NoContextMenu);
27
28     QAction *tocAction = toolbar->addAction("TOC");
29     tocAction->setIcon(QIcon::fromTheme("notes_bullets"));
30     connect(tocAction, SIGNAL(triggered()), this, SLOT(tocNavigation()));
31
32     QAction *bookmarkAction = toolbar->addAction("bookmarkAction");
33     bookmarkAction->setIcon(QIcon(":/bm_icon.png"));
34     connect(bookmarkAction, SIGNAL(triggered()), this, SLOT(viewBookmarks()));
35
36     QAction *annotationAction = toolbar->addAction("annotationAction");
37     annotationAction->setIcon(QIcon(":/annotation_icon.png"));
38     connect(annotationAction, SIGNAL(triggered()), this, SLOT(viewAnnotations()));
39
40     setWindowFlags(windowFlags() | Qt::Window);
41     setAttribute(Qt::WA_Maemo5StackedWindow);
42     addToolBar(toolbar);
43     grabZoomKeys(true);
44
45     osso_context = context;
46     brightnessTimer.setInterval(50000);
47     connect(&brightnessTimer, SIGNAL(timeout()), this, SLOT(forceBacklightOn()));
48     brightnessTimer.start();
49     setWindowTitle("MeBook");
50 }
51
52 MainWindow::~MainWindow()
53 {
54     grabZoomKeys(false);
55 }
56
57 void MainWindow::openBook(const QString &filename)
58 {
59     Book *book = new Book(filename);
60     openBook(book);
61 }
62
63 void MainWindow::openBook(Book *book)
64 {
65     if(bookView)
66         bookView->setBook(book);
67     currentBook = book;
68     setWindowTitle(book->getTitle());
69     forceBacklightOn();
70 }
71
72 void MainWindow::setFont(const QString &family, int size)
73 {
74     if(bookView)
75         bookView->setFont(family, size);
76 }
77
78 void MainWindow::setColorMode(int mode)
79 {
80     if(bookView)
81         bookView->setNightMode(mode);
82 }
83
84 void MainWindow::enableBacklightForcing(bool toggle)
85 {
86     if(toggle)
87         brightnessTimer.start();
88     else
89         brightnessTimer.stop();
90 }
91
92 void MainWindow::setViewMode(BookViewMode mode, Qt::WidgetAttribute orientation)
93 {
94     bookView->setViewMode(mode, orientation);
95     if(mode == ScrollingMode){
96         QAction *previousSectionAction = toolbar->addAction("previousSection");
97         previousSectionAction->setIcon(QIcon::fromTheme("general_back"));
98         connect(previousSectionAction, SIGNAL(triggered()), bookView, SLOT(previousSection()));
99
100         QAction *nextSectionAction = toolbar->addAction("nextSection");
101         nextSectionAction->setIcon(QIcon::fromTheme("general_forward"));
102         connect(nextSectionAction, SIGNAL(triggered()), bookView, SLOT(nextSection()));
103     }else{ 
104         QAction *backAction = toolbar->addAction("back");
105         backAction->setIcon(QIcon::fromTheme("browser_history"));
106         connect(backAction, SIGNAL(triggered()), bookView, SLOT(backInHistory()));
107     }
108 }
109
110 bool MainWindow::eventFilter(QObject *object, QEvent *event)
111 {
112     if(event->type() == QEvent::MouseButtonDblClick)
113     {
114         if(isFullScreen()){
115             showNormal();
116             toolbar->show();
117             return true;
118         }
119
120     } else if(event->type() == QEvent::MouseButtonPress){
121         if(!isFullScreen()){
122             showFullScreen();
123             toolbar->hide();
124             return true;
125         }
126     }
127
128     return QObject::eventFilter(object, event);
129 }
130
131 void MainWindow::keyPressEvent(QKeyEvent *event)
132 {
133     if(event->key() == Qt::Key_F7)
134         emit volumeUpPressed();
135     else if(event->key() == Qt::Key_F8)
136         emit volumeDownPressed();
137     QMainWindow::keyPressEvent(event);
138 }
139
140 void MainWindow::tocNavigation()
141 {
142     QList<QString> toc = currentBook->getTOC();
143     if(toc.isEmpty())
144         return;
145     //TODO: Show notofication banner if no TOC
146     QList<ListItem > list;
147     for(QList<QString>::iterator iter = toc.begin(); iter != toc.end(); ++iter){
148         ListItem item;
149         item.text = *iter;
150         list.push_back(item);
151     }
152
153     ListViewDialog *dialog = new ListViewDialog(this);
154     ListModel *model = new ListModel(list);
155     dialog->setModel(model);
156     dialog->setAttribute(Qt::WA_Maemo5StackedWindow);
157     dialog->setWindowFlags(dialog->windowFlags() | Qt::Window);
158     connect(dialog, SIGNAL(selectedIndex(int)), bookView, SLOT(goToTOCIndex(int)));
159     dialog->show();
160
161 }
162
163 void MainWindow::viewBookmarks()
164 {
165     QList<Bookmark> bm = currentBook->getBookmarks();
166     if(bm.isEmpty())
167         return;
168     QList<ListItem > list;
169     for(QList<Bookmark>::iterator iter = bm.begin(); iter != bm.end(); ++iter){
170         Bookmark bookmark = *iter;
171         QPixmap ip(32, 32); 
172         ip.fill(Qt::transparent);
173         QPainter painter(&ip);
174         painter.setRenderHint(QPainter::Antialiasing, true);
175         painter.setPen(Qt::NoPen);
176         painter.setBrush(bookmark.getColor());
177         painter.drawRoundedRect(ip.rect(), 3, 3);
178         ListItem item;
179         item.text = bookmark.getName();
180         QString sectionName = currentBook->getSectionName(bookmark.getSection());
181         if(sectionName.isEmpty())
182             sectionName = tr("Section") + " " + QString::number(bookmark.getSection());
183         item.subText =  sectionName + ", " + bookmark.percentageAsString() + "%";
184         item.icon = ip;
185
186         list.push_back(item);
187     }
188     
189     ListViewDialog *dialog = new ListViewDialog(this);
190     ListModel *model = new ListModel(list);
191     dialog->setModel(model);
192     dialog->setAttribute(Qt::WA_Maemo5StackedWindow);
193     dialog->setWindowFlags(dialog->windowFlags() | Qt::Window);
194     connect(dialog, SIGNAL(selectedIndex(int)), bookView, SLOT(goToBookmark(int)));
195     dialog->show();
196 }
197
198
199 void MainWindow::viewAnnotations()
200 {
201     QList<Annotation> a = currentBook->getAnnotations();
202     if(a.isEmpty())
203         return;
204     QList<ListItem> list;
205     for(QList<Annotation>::iterator iter = a.begin(); iter != a.end(); ++iter){
206         Annotation annotation = *iter;
207         QPixmap ip(32, 32); 
208         ip.fill(Qt::transparent);
209         QPainter painter(&ip);
210         painter.setRenderHint(QPainter::Antialiasing, true);
211         painter.setPen(Qt::NoPen);
212         painter.setBrush(annotation.getColor());
213         painter.drawRoundedRect(ip.rect(), 3, 3);
214
215         ListItem item;
216         item.icon = ip;
217         item.text = annotation.getAnnotation();
218         item.subText = currentBook->getSectionName(annotation.getSection());
219
220         list.push_back(item);
221     }
222     ListViewDialog *dialog = new ListViewDialog(this);
223     ListModel *model = new ListModel(list);
224     dialog->setModel(model);
225     connect(dialog, SIGNAL(selectedIndex(int)), bookView, SLOT(goToAnnotation(int)));
226     dialog->setAttribute(Qt::WA_Maemo5StackedWindow);
227     dialog->setWindowFlags(dialog->windowFlags() | Qt::Window);
228     dialog->show();
229 }
230
231 void MainWindow::grabZoomKeys(bool grab)
232 {
233     if(!winId()){
234         qDebug("Can't grab keys unless we have a window id");
235         return;
236     }
237
238     unsigned long val = (grab) ? 1 : 0;
239     Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
240     if(!atom){
241         qDebug("Unable to obtain _HILDON_ZOOM_KEY_ATOM.");
242         return;
243     }
244
245     XChangeProperty(QX11Info::display(),
246         winId(),
247         atom,
248         XA_INTEGER,
249         32,
250         PropModeReplace,
251         reinterpret_cast<unsigned char*>(&val),
252         1);
253
254     grabKeyboard();
255 }
256
257 void MainWindow::closeEvent(QCloseEvent *event)
258 {
259     bookView->saveProgress(); 
260     emit closing();
261     QMainWindow::closeEvent(event);
262 }
263
264 void MainWindow::forceBacklightOn()
265 {
266     if(osso_context)
267         osso_display_blanking_pause(osso_context);
268 }