Fixes and improvements to slider system.
[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(":/toc_icon.png"));
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(60000);
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)
93 {
94     bookView->setViewMode(mode);    
95 }
96
97 bool MainWindow::eventFilter(QObject *object, QEvent *event)
98 {
99     if(event->type() == QEvent::MouseButtonDblClick)
100     {
101         if(!isFullScreen()){
102             showFullScreen();
103             toolbar->hide();            
104         }
105         else{
106             showNormal();
107             toolbar->show();
108         }
109         return true;
110
111     }
112
113     return QObject::eventFilter(object, event);
114 }
115
116 void MainWindow::keyPressEvent(QKeyEvent *event)
117 {
118     if(event->key() == Qt::Key_F7)
119         emit volumeUpPressed();
120     else if(event->key() == Qt::Key_F8)
121         emit volumeDownPressed();
122     QMainWindow::keyPressEvent(event);
123 }
124
125 void MainWindow::tocNavigation()
126 {
127     QList<QString> toc = currentBook->getTOC();
128     if(toc.isEmpty())
129         return;
130     //TODO: Show notofication banner if no TOC
131     QList<ListItem > list;
132     for(QList<QString>::iterator iter = toc.begin(); iter != toc.end(); ++iter){
133         ListItem item;
134         item.text = *iter;
135         list.push_back(item);
136     }
137
138     ListViewDialog *dialog = new ListViewDialog(this);
139     ListModel *model = new ListModel(list);
140     dialog->setModel(model);
141     dialog->setAttribute(Qt::WA_Maemo5StackedWindow);
142     dialog->setWindowFlags(dialog->windowFlags() | Qt::Window);
143     connect(dialog, SIGNAL(selectedIndex(int)), bookView, SLOT(goToSection(int)));
144     dialog->show();
145
146 }
147
148 void MainWindow::viewBookmarks()
149 {
150     QList<Bookmark> bm = currentBook->getBookmarks();
151     if(bm.isEmpty())
152         return;
153     QList<ListItem > list;
154     for(QList<Bookmark>::iterator iter = bm.begin(); iter != bm.end(); ++iter){
155         Bookmark bookmark = *iter;
156         QPixmap ip(32, 32); 
157         ip.fill(Qt::transparent);
158         QPainter painter(&ip);
159         painter.setRenderHint(QPainter::Antialiasing, true);
160         painter.setPen(Qt::NoPen);
161         painter.setBrush(bookmark.getColor());
162         painter.drawRoundedRect(ip.rect(), 3, 3);
163 //        QIcon icon(ip);
164         ListItem item;
165         item.text = bookmark.getName();
166         item.subText = currentBook->getSectionName(bookmark.getSection()) + ", page " + QString::number(bookmark.getPage() + 1);
167         item.icon = ip;
168
169         list.push_back(item);
170     }
171 //    SelectorDialog *dialog = new SelectorDialog(list, this);
172     
173     ListViewDialog *dialog = new ListViewDialog(this);
174     ListModel *model = new ListModel(list);
175     dialog->setModel(model);
176     dialog->setAttribute(Qt::WA_Maemo5StackedWindow);
177     dialog->setWindowFlags(dialog->windowFlags() | Qt::Window);
178     connect(dialog, SIGNAL(selectedIndex(int)), bookView, SLOT(goToBookmark(int)));
179     dialog->show();
180 }
181
182
183 void MainWindow::viewAnnotations()
184 {
185     QList<Annotation> a = currentBook->getAnnotations();
186     if(a.isEmpty())
187         return;
188     QList<ListItem> list;
189     for(QList<Annotation>::iterator iter = a.begin(); iter != a.end(); ++iter){
190         Annotation annotation = *iter;
191         QPixmap ip(32, 32); 
192         ip.fill(Qt::transparent);
193         QPainter painter(&ip);
194         painter.setRenderHint(QPainter::Antialiasing, true);
195         painter.setPen(Qt::NoPen);
196         painter.setBrush(annotation.getColor());
197         painter.drawRoundedRect(ip.rect(), 3, 3);
198
199         ListItem item;
200         item.icon = ip;
201         item.text = annotation.getAnnotation();
202         item.subText = currentBook->getSectionName(annotation.getSection());
203
204         list.push_back(item);
205     }
206     ListViewDialog *dialog = new ListViewDialog(this);
207     ListModel *model = new ListModel(list);
208     dialog->setModel(model);
209     connect(dialog, SIGNAL(selectedIndex(int)), bookView, SLOT(goToAnnotation(int)));
210     dialog->setAttribute(Qt::WA_Maemo5StackedWindow);
211     dialog->setWindowFlags(dialog->windowFlags() | Qt::Window);
212     dialog->show();
213 }
214
215 void MainWindow::grabZoomKeys(bool grab)
216 {
217     if(!winId()){
218         qDebug("Can't grab keys unless we have a window id");
219         return;
220     }
221
222     unsigned long val = (grab) ? 1 : 0;
223     Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
224     if(!atom){
225         qDebug("Unable to obtain _HILDON_ZOOM_KEY_ATOM.");
226         return;
227     }
228
229     XChangeProperty(QX11Info::display(),
230         winId(),
231         atom,
232         XA_INTEGER,
233         32,
234         PropModeReplace,
235         reinterpret_cast<unsigned char*>(&val),
236         1);
237
238     grabKeyboard();
239 }
240
241 void MainWindow::closeEvent(QCloseEvent *event)
242 {
243     bookView->saveProgress(); 
244     QMainWindow::closeEvent(event);
245     emit closing();
246 }
247
248 void MainWindow::forceBacklightOn()
249 {
250     if(osso_context)
251         osso_display_blanking_pause(osso_context);
252 }