Bug fixes and other improvements.
[mebook:mebook.git] / ui / mainwindow.cpp
1 #include "mainwindow.h"
2 #include "selectordialog.h"
3 #include "bookmark.h"
4 #include "addbookmarkdialog.h"
5 #include <QAction>
6 #include <QMenu>
7
8 #include <QtGui/QX11Info>
9 #include <X11/Xlib.h>
10 #include <X11/Xatom.h>
11
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     setContextMenuPolicy(Qt::CustomContextMenu);
37     connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(openContextMenu(const QPoint&)));
38
39     setWindowFlags(windowFlags() | Qt::Window);
40     setAttribute(Qt::WA_Maemo5StackedWindow);
41     addToolBar(toolbar);
42     grabZoomKeys(true);
43
44     osso_context = context;
45     brightnessTimer.setInterval(60000);
46     connect(&brightnessTimer, SIGNAL(timeout()), this, SLOT(forceBacklightOn()));
47     brightnessTimer.start();
48     setWindowTitle("MeBook");
49 }
50
51 MainWindow::~MainWindow()
52 {
53     grabZoomKeys(false);
54 }
55
56 void MainWindow::openBook(const QString &filename)
57 {
58     Book *book = new Book(filename);
59     openBook(book);
60 }
61
62 void MainWindow::openBook(Book *book)
63 {
64     if(bookView)
65         bookView->setBook(book);
66     currentBook = book;
67     setWindowTitle(book->getTitle());
68     forceBacklightOn();
69 }
70
71 void MainWindow::setFont(const QString &family, int size)
72 {
73     if(bookView)
74         bookView->setFont(family, size);
75 }
76
77 void MainWindow::setColorMode(int mode)
78 {
79     if(bookView)
80         bookView->setNightMode(mode);
81 }
82
83 void MainWindow::enableBacklightForcing(bool toggle)
84 {
85     if(toggle)
86         brightnessTimer.start();
87     else
88         brightnessTimer.stop();
89 }
90
91 void MainWindow::setViewMode(BookViewMode mode)
92 {
93     bookView->setViewMode(mode);    
94 }
95
96 bool MainWindow::eventFilter(QObject *object, QEvent *event)
97 {
98     if(event->type() == QEvent::MouseButtonDblClick)
99     {
100         if(!isFullScreen()){
101             showFullScreen();
102             toolbar->hide();            
103         }
104         else{
105             showNormal();
106             toolbar->show();
107         }
108     }else {
109         return QObject::eventFilter(object, event);
110     }
111 }
112
113 void MainWindow::keyPressEvent(QKeyEvent *event)
114 {
115     if(event->key() == Qt::Key_F7)
116         emit volumeUpPressed();
117     else if(event->key() == Qt::Key_F8)
118         emit volumeDownPressed();
119     QMainWindow::keyPressEvent(event);
120 }
121
122 void MainWindow::tocNavigation()
123 {
124     QList<QString> toc = currentBook->getTOC();
125     //TODO: Show notofication banner if no TOC
126     if(!toc.isEmpty()){
127         SelectorDialog *dialog = new SelectorDialog(toc, this);
128         dialog->setAttribute(Qt::WA_Maemo5StackedWindow);
129         dialog->setWindowFlags(dialog->windowFlags() | Qt::Window);
130         connect(dialog, SIGNAL(selectedString(QString)), bookView, SLOT(goToChapter(QString)));
131         dialog->show();
132     }
133
134
135 }
136
137 void MainWindow::viewBookmarks()
138 {
139     QList<Bookmark> bm = currentBook->getBookmarks();
140     if(bm.isEmpty())
141         return;
142     QList< QPair<QIcon, QString> > list;
143     for(QList<Bookmark>::iterator iter = bm.begin(); iter != bm.end(); ++iter){
144         Bookmark bookmark = *iter;
145         QPixmap ip(32, 32); 
146         QPainter painter(&ip);
147         painter.setPen(Qt::NoPen);
148         painter.setBrush(bookmark.getColor());
149         painter.drawRect(ip.rect());
150         QIcon icon(ip);
151
152         QPair<QIcon, QString> pair;
153         pair.first = icon;
154         pair.second = bookmark.getName();
155
156         list.push_back(pair);
157     }
158     SelectorDialog *dialog = new SelectorDialog(list, this);
159     dialog->setAttribute(Qt::WA_Maemo5StackedWindow);
160     dialog->setWindowFlags(dialog->windowFlags() | Qt::Window);
161     connect(dialog, SIGNAL(selectedIndex(int)), bookView, SLOT(goToBookmark(int)));
162     dialog->show();
163 }
164
165 void MainWindow::openContextMenu(const QPoint &pos)
166 {
167     QMenu *menu = new QMenu();
168     menu->addAction(tr("Add bookmark"), this, SLOT(addBookmark()));
169     menu->exec(mapToGlobal(pos));
170 }
171
172 void MainWindow::addBookmark()
173 {
174     AddBookmarkDialog *dialog = new AddBookmarkDialog(this); 
175     if(dialog->exec() == QDialog::Accepted){
176         bookView->addBookmark(dialog->getName(), dialog->getColor());        
177     }
178 }
179
180
181 void MainWindow::grabZoomKeys(bool grab)
182 {
183     if(!winId()){
184         qDebug("Can't grab keys unless we have a window id");
185         return;
186     }
187
188     unsigned long val = (grab) ? 1 : 0;
189     Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
190     if(!atom){
191         qDebug("Unable to obtain _HILDON_ZOOM_KEY_ATOM.");
192         return;
193     }
194
195     XChangeProperty(QX11Info::display(),
196         winId(),
197         atom,
198         XA_INTEGER,
199         32,
200         PropModeReplace,
201         reinterpret_cast<unsigned char*>(&val),
202         1);
203
204     grabKeyboard();
205 }
206
207 void MainWindow::closeEvent(QCloseEvent *event)
208 {
209     bookView->saveProgress(); 
210     QMainWindow::closeEvent(event);
211 }
212
213 void MainWindow::forceBacklightOn()
214 {
215     qDebug() << "Forcing backlight on!";
216     if(osso_context)
217         osso_display_blanking_pause(osso_context);
218 }