Fixed a bug with orientations change.
[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("data/toc_icon.png"));
30     connect(tocAction, SIGNAL(triggered()), this, SLOT(tocNavigation()));
31
32     QAction *bookmarkAction = toolbar->addAction("bookmarkAction");
33     bookmarkAction->setIcon(QIcon("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 bool MainWindow::eventFilter(QObject *object, QEvent *event)
92 {
93     if(event->type() == QEvent::MouseButtonDblClick)
94     {
95         if(!isFullScreen()){
96             showFullScreen();
97             toolbar->hide();            
98         }
99         else{
100             showNormal();
101             toolbar->show();
102         }
103     }else {
104         return QObject::eventFilter(object, event);
105     }
106 }
107
108 void MainWindow::keyPressEvent(QKeyEvent *event)
109 {
110     if(event->key() == Qt::Key_F7)
111         emit volumeUpPressed();
112     else if(event->key() == Qt::Key_F8)
113         emit volumeDownPressed();
114     QMainWindow::keyPressEvent(event);
115 }
116
117 void MainWindow::tocNavigation()
118 {
119     QList<QString> toc = currentBook->getTOC();
120     SelectorDialog *dialog = new SelectorDialog(toc, this);
121     dialog->setAttribute(Qt::WA_Maemo5StackedWindow);
122     dialog->setWindowFlags(dialog->windowFlags() | Qt::Window);
123     connect(dialog, SIGNAL(selectedString(QString)), bookView, SLOT(goToChapter(QString)));
124     dialog->show();
125
126
127 }
128
129 void MainWindow::viewBookmarks()
130 {
131     QList<Bookmark> bm = currentBook->getBookmarks();
132     if(bm.isEmpty())
133         return;
134     QList< QPair<QIcon, QString> > list;
135     for(QList<Bookmark>::iterator iter = bm.begin(); iter != bm.end(); ++iter){
136         Bookmark bookmark = *iter;
137         QPixmap ip(32, 32); 
138         QPainter painter(&ip);
139         painter.setPen(Qt::NoPen);
140         painter.setBrush(bookmark.getColor());
141         painter.drawRect(ip.rect());
142         QIcon icon(ip);
143
144         QPair<QIcon, QString> pair;
145         pair.first = icon;
146         pair.second = bookmark.getName();
147
148         list.push_back(pair);
149     }
150     SelectorDialog *dialog = new SelectorDialog(list, this);
151     dialog->setAttribute(Qt::WA_Maemo5StackedWindow);
152     dialog->setWindowFlags(dialog->windowFlags() | Qt::Window);
153     connect(dialog, SIGNAL(selectedIndex(int)), bookView, SLOT(goToBookmark(int)));
154     dialog->show();
155 }
156
157 void MainWindow::openContextMenu(const QPoint &pos)
158 {
159     QMenu *menu = new QMenu();
160     menu->addAction(tr("Add bookmark"), this, SLOT(addBookmark()));
161     menu->exec(mapToGlobal(pos));
162 }
163
164 void MainWindow::addBookmark()
165 {
166     AddBookmarkDialog *dialog = new AddBookmarkDialog(this); 
167     if(dialog->exec() == QDialog::Accepted){
168         bookView->addBookmark(dialog->getName(), dialog->getColor());        
169     }
170 }
171
172
173 void MainWindow::grabZoomKeys(bool grab)
174 {
175     if(!winId()){
176         qDebug("Can't grab keys unless we have a window id");
177         return;
178     }
179
180     unsigned long val = (grab) ? 1 : 0;
181     Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
182     if(!atom){
183         qDebug("Unable to obtain _HILDON_ZOOM_KEY_ATOM.");
184         return;
185     }
186
187     XChangeProperty(QX11Info::display(),
188         winId(),
189         atom,
190         XA_INTEGER,
191         32,
192         PropModeReplace,
193         reinterpret_cast<unsigned char*>(&val),
194         1);
195
196     grabKeyboard();
197 }
198
199 void MainWindow::closeEvent(QCloseEvent *event)
200 {
201     bookView->saveProgress(); 
202     QMainWindow::closeEvent(event);
203 }
204
205 void MainWindow::forceBacklightOn()
206 {
207     qDebug() << "Forcing backlight on!";
208     if(osso_context)
209         osso_display_blanking_pause(osso_context);
210 }