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