Fixes. About dialog. Books properly removed from library when file is deleted.
[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 <QMaemo5InformationBox>
8 #include "listviewdialog.h"
9
10 #include <QtGui/QX11Info>
11 #include <X11/Xlib.h>
12 #include <X11/Xatom.h>
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::fromTheme("notes_bullets"));
31     connect(tocAction, SIGNAL(triggered()), this, SLOT(tocNavigation()));
32
33     QAction *bookmarkAction = toolbar->addAction("bookmarkAction");
34     bookmarkAction->setIcon(QIcon(":/bm_icon.png"));
35     connect(bookmarkAction, SIGNAL(triggered()), this, SLOT(viewBookmarks()));
36
37     QAction *annotationAction = toolbar->addAction("annotationAction");
38     annotationAction->setIcon(QIcon(":/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(50000);
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, Qt::WidgetAttribute orientation)
94 {
95     bookView->setViewMode(mode, orientation);
96     if(mode == ScrollingMode){
97         QAction *previousSectionAction = toolbar->addAction("previousSection");
98         previousSectionAction->setIcon(QIcon::fromTheme("general_back"));
99         connect(previousSectionAction, SIGNAL(triggered()), bookView, SLOT(previousSection()));
100
101         QAction *nextSectionAction = toolbar->addAction("nextSection");
102         nextSectionAction->setIcon(QIcon::fromTheme("general_forward"));
103         connect(nextSectionAction, SIGNAL(triggered()), bookView, SLOT(nextSection()));
104     }else{ 
105         QAction *backAction = toolbar->addAction("back");
106         backAction->setIcon(QIcon::fromTheme("browser_history"));
107         connect(backAction, SIGNAL(triggered()), bookView, SLOT(backInHistory()));
108     }
109 }
110
111 bool MainWindow::eventFilter(QObject *object, QEvent *event)
112 {
113     if(event->type() == QEvent::MouseButtonDblClick)
114     {
115         if(isFullScreen()){
116             showNormal();
117             toolbar->show();
118             return true;
119         }
120
121     } else if(event->type() == QEvent::MouseButtonPress){
122         if(!isFullScreen()){
123             showFullScreen();
124             toolbar->hide();
125             return true;
126         }
127     }
128
129     return QObject::eventFilter(object, event);
130 }
131
132 void MainWindow::keyPressEvent(QKeyEvent *event)
133 {
134     if(event->key() == Qt::Key_F7)
135         emit volumeUpPressed();
136     else if(event->key() == Qt::Key_F8)
137         emit volumeDownPressed();
138     QMainWindow::keyPressEvent(event);
139 }
140
141 void MainWindow::tocNavigation()
142 {
143     QList<QString> toc = currentBook->getTOC();
144     if(toc.isEmpty()){
145         QMaemo5InformationBox::information(this, tr("This book doesn't have table of contents information."), QMaemo5InformationBox::DefaultTimeout); 
146         return;
147     }
148     //TODO: Show notofication banner if no TOC
149     QList<ListItem > list;
150     for(QList<QString>::iterator iter = toc.begin(); iter != toc.end(); ++iter){
151         ListItem item;
152         item.text = *iter;
153         list.push_back(item);
154     }
155
156     ListViewDialog *dialog = new ListViewDialog(this);
157     ListModel *model = new ListModel(list);
158     dialog->setModel(model);
159     dialog->setAttribute(Qt::WA_Maemo5StackedWindow);
160     dialog->setWindowFlags(dialog->windowFlags() | Qt::Window);
161     connect(dialog, SIGNAL(selectedIndex(int)), bookView, SLOT(goToTOCIndex(int)));
162     dialog->show();
163
164 }
165
166 void MainWindow::viewBookmarks()
167 {
168     QList<Bookmark> bm = currentBook->getBookmarks();
169     if(bm.isEmpty()){
170         QMaemo5InformationBox::information(this, tr("No bookmarks found. Press and hold to add a bookmark."), QMaemo5InformationBox::DefaultTimeout); 
171         return;
172     }
173     QList<ListItem > list;
174     for(QList<Bookmark>::iterator iter = bm.begin(); iter != bm.end(); ++iter){
175         Bookmark bookmark = *iter;
176         QPixmap ip(32, 32); 
177         ip.fill(Qt::transparent);
178         QPainter painter(&ip);
179         painter.setRenderHint(QPainter::Antialiasing, true);
180         painter.setPen(Qt::NoPen);
181         painter.setBrush(bookmark.getColor());
182         painter.drawRoundedRect(ip.rect(), 3, 3);
183         ListItem item;
184         item.text = bookmark.getName();
185         QString sectionName = currentBook->getSectionName(bookmark.getSection());
186         if(sectionName.isEmpty())
187             sectionName = tr("Section") + " " + QString::number(bookmark.getSection());
188         item.subText =  sectionName + ", " + bookmark.percentageAsString() + "%";
189         item.icon = ip;
190
191         list.push_back(item);
192     }
193     
194     ListViewDialog *dialog = new ListViewDialog(this);
195     ListModel *model = new ListModel(list);
196     dialog->setModel(model);
197     dialog->setAttribute(Qt::WA_Maemo5StackedWindow);
198     dialog->setWindowFlags(dialog->windowFlags() | Qt::Window);
199     connect(dialog, SIGNAL(selectedIndex(int)), bookView, SLOT(goToBookmark(int)));
200     dialog->show();
201 }
202
203
204 void MainWindow::viewAnnotations()
205 {
206     QList<Annotation> a = currentBook->getAnnotations();
207     if(a.isEmpty()){
208         
209         QMaemo5InformationBox::information(this, tr("No annotations found. Press and hold paragraph to add an annotation."), QMaemo5InformationBox::DefaultTimeout); 
210         return;
211     }
212     QList<ListItem> list;
213     for(QList<Annotation>::iterator iter = a.begin(); iter != a.end(); ++iter){
214         Annotation annotation = *iter;
215         QPixmap ip(32, 32); 
216         ip.fill(Qt::transparent);
217         QPainter painter(&ip);
218         painter.setRenderHint(QPainter::Antialiasing, true);
219         painter.setPen(Qt::NoPen);
220         painter.setBrush(annotation.getColor());
221         painter.drawRoundedRect(ip.rect(), 3, 3);
222
223         ListItem item;
224         item.icon = ip;
225         item.text = annotation.getAnnotation();
226         item.subText = currentBook->getSectionName(annotation.getSection());
227
228         list.push_back(item);
229     }
230     ListViewDialog *dialog = new ListViewDialog(this);
231     ListModel *model = new ListModel(list);
232     dialog->setModel(model);
233     connect(dialog, SIGNAL(selectedIndex(int)), bookView, SLOT(goToAnnotation(int)));
234     dialog->setAttribute(Qt::WA_Maemo5StackedWindow);
235     dialog->setWindowFlags(dialog->windowFlags() | Qt::Window);
236     dialog->show();
237 }
238
239 void MainWindow::grabZoomKeys(bool grab)
240 {
241     if(!winId()){
242         qDebug("Can't grab keys unless we have a window id");
243         return;
244     }
245
246     unsigned long val = (grab) ? 1 : 0;
247     Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
248     if(!atom){
249         qDebug("Unable to obtain _HILDON_ZOOM_KEY_ATOM.");
250         return;
251     }
252
253     XChangeProperty(QX11Info::display(),
254         winId(),
255         atom,
256         XA_INTEGER,
257         32,
258         PropModeReplace,
259         reinterpret_cast<unsigned char*>(&val),
260         1);
261
262     grabKeyboard();
263 }
264
265 void MainWindow::closeEvent(QCloseEvent *event)
266 {
267     bookView->saveProgress(); 
268     hide();
269     emit closing();
270 }
271
272
273 void MainWindow::forceBacklightOn()
274 {
275     //Only force backligh if MainWindow is active.
276     if(osso_context && isActiveWindow())
277         osso_display_blanking_pause(osso_context);
278 }