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