Changes to project file.
[mebook:mebook.git] / ui / mainwindow.cpp
1 #include "mainwindow.h"
2 #include "selectordialog.h"
3 #include <QAction>
4
5 #include <QtGui/QX11Info>
6 #include <X11/Xlib.h>
7 #include <X11/Xatom.h>
8
9 MainWindow::MainWindow(const QString &filename, QWidget *parent) : QMainWindow(parent)
10 {
11     Book *book = new Book(filename);
12     MainWindow(book, parent);
13
14 }
15
16 MainWindow::MainWindow(Book *book, QWidget *parent) : QMainWindow(parent)
17 {
18     bookView = new BookView(this);
19     setCentralWidget(bookView); 
20
21     currentBook = book;
22
23     bookView->setBook(book);
24
25     bookView->installEventFilter(this);
26
27     
28     connect(this, SIGNAL(volumeUpPressed()), bookView, SLOT(volumeUpPressed()));
29     connect(this, SIGNAL(volumeDownPressed()), bookView, SLOT(volumeDownPressed()));
30
31     toolbar = new QToolBar("toolbar", this);
32     if(book->hasTOC()){
33         QAction *tocAction = toolbar->addAction("TOC");
34         connect(tocAction, SIGNAL(triggered()), this, SLOT(tocNavigation()));
35     }
36
37     setWindowFlags(windowFlags() | Qt::Window);
38     setAttribute(Qt::WA_Maemo5StackedWindow);
39     addToolBar(toolbar);
40
41     grabZoomKeys(true);
42 }
43
44 MainWindow::~MainWindow()
45 {
46     grabZoomKeys(false);
47 }
48
49 void MainWindow::openBook(const QString &filename)
50 {
51     Book *book = new Book(filename);
52     openBook(book);
53 }
54
55 void MainWindow::openBook(Book *book)
56 {
57     bookView->setBook(book);
58     currentBook = book;
59 }
60
61 void MainWindow::setFont(const QString &family, int size)
62 {
63     if(bookView)
64         bookView->setFont(family, size);
65 }
66
67 bool MainWindow::eventFilter(QObject *object, QEvent *event)
68 {
69     if(event->type() == QEvent::MouseButtonDblClick)
70     {
71         if(!isFullScreen()){
72             showFullScreen();
73             toolbar->hide();            
74         }
75         else{
76             showNormal();
77             toolbar->show();
78         }
79     }else {
80         return QObject::eventFilter(object, event);
81     }
82 }
83
84 void MainWindow::keyPressEvent(QKeyEvent *event)
85 {
86     if(event->key() == Qt::Key_F7)
87         emit volumeUpPressed();
88     else if(event->key() == Qt::Key_F8)
89         emit volumeDownPressed();
90     QMainWindow::keyPressEvent(event);
91 }
92
93 void MainWindow::tocNavigation()
94 {
95     QList<QString> toc = currentBook->getTOC();
96     dialog = new SelectorDialog(toc, this);
97     dialog->setAttribute(Qt::WA_Maemo5StackedWindow);
98     dialog->setWindowFlags(dialog->windowFlags() | Qt::Window);
99     connect(dialog, SIGNAL(selectedString(QString)), bookView, SLOT(goToChapter(QString)));
100     dialog->show();
101
102
103 }
104
105
106 void MainWindow::grabZoomKeys(bool grab)
107 {
108     if(!winId()){
109         qDebug("Can't grab keys unless we have a window id");
110         return;
111     }
112
113     unsigned long val = (grab) ? 1 : 0;
114     Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
115     if(!atom){
116         qDebug("Unable to obtain _HILDON_ZOOM_KEY_ATOM.");
117         return;
118     }
119
120     XChangeProperty(QX11Info::display(),
121         winId(),
122         atom,
123         XA_INTEGER,
124         32,
125         PropModeReplace,
126         reinterpret_cast<unsigned char*>(&val),
127         1);
128
129     grabKeyboard();
130 }