SelectorDialog replaced with a model/view based system. Improved TextSelectionWidget.
[mebook:mebook.git] / ui / mainwindow.cpp
1 #include "mainwindow.h"
2 #include "selectordialog.h"
3 #include "bookmark.h"
4 #include "addbookmarkdialog.h"
5 #include "addannotationdialog.h"
6 #include <QAction>
7 #include <QMenu>
8
9 #include <QtGui/QX11Info>
10 #include <X11/Xlib.h>
11 #include <X11/Xatom.h>
12
13 #include "listviewdialog.h"
14
15
16 MainWindow::MainWindow(osso_context_t *context, QWidget *parent) : QMainWindow(parent)
17 {
18     bookView = new BookView(this);
19     setCentralWidget(bookView); 
20
21     //Install eventFilter to bookView's viewport so we can intercept doubleclick event.
22     bookView->viewport()->installEventFilter(this);
23
24     
25     connect(this, SIGNAL(volumeUpPressed()), bookView, SLOT(volumeUpPressed()));
26     connect(this, SIGNAL(volumeDownPressed()), bookView, SLOT(volumeDownPressed()));
27
28     toolbar = new QToolBar("toolbar", this);
29     toolbar->setContextMenuPolicy(Qt::NoContextMenu);
30
31     QAction *tocAction = toolbar->addAction("TOC");
32     tocAction->setIcon(QIcon("/opt/mebook/data/toc_icon.png"));
33     connect(tocAction, SIGNAL(triggered()), this, SLOT(tocNavigation()));
34
35     QAction *bookmarkAction = toolbar->addAction("bookmarkAction");
36     bookmarkAction->setIcon(QIcon("/opt/mebook/data/bm_icon.png"));
37     connect(bookmarkAction, SIGNAL(triggered()), this, SLOT(viewBookmarks()));
38
39     QAction *annotationAction = toolbar->addAction("annotationAction");
40     annotationAction->setIcon(QIcon("/opt/mebook/data/annotation_icon.png"));
41     connect(annotationAction, SIGNAL(triggered()), this, SLOT(viewAnnotations()));
42
43     setWindowFlags(windowFlags() | Qt::Window);
44     setAttribute(Qt::WA_Maemo5StackedWindow);
45     addToolBar(toolbar);
46     grabZoomKeys(true);
47
48     osso_context = context;
49     brightnessTimer.setInterval(60000);
50     connect(&brightnessTimer, SIGNAL(timeout()), this, SLOT(forceBacklightOn()));
51     brightnessTimer.start();
52     setWindowTitle("MeBook");
53 }
54
55 MainWindow::~MainWindow()
56 {
57     grabZoomKeys(false);
58 }
59
60 void MainWindow::openBook(const QString &filename)
61 {
62     Book *book = new Book(filename);
63     openBook(book);
64 }
65
66 void MainWindow::openBook(Book *book)
67 {
68     if(bookView)
69         bookView->setBook(book);
70     currentBook = book;
71     setWindowTitle(book->getTitle());
72     forceBacklightOn();
73 }
74
75 void MainWindow::setFont(const QString &family, int size)
76 {
77     if(bookView)
78         bookView->setFont(family, size);
79 }
80
81 void MainWindow::setColorMode(int mode)
82 {
83     if(bookView)
84         bookView->setNightMode(mode);
85 }
86
87 void MainWindow::enableBacklightForcing(bool toggle)
88 {
89     if(toggle)
90         brightnessTimer.start();
91     else
92         brightnessTimer.stop();
93 }
94
95 void MainWindow::setViewMode(BookViewMode mode)
96 {
97     bookView->setViewMode(mode);    
98 }
99
100 bool MainWindow::eventFilter(QObject *object, QEvent *event)
101 {
102     if(event->type() == QEvent::MouseButtonDblClick)
103     {
104         if(!isFullScreen()){
105             showFullScreen();
106             toolbar->hide();            
107         }
108         else{
109             showNormal();
110             toolbar->show();
111         }
112     }else {
113         return QObject::eventFilter(object, event);
114     }
115 }
116
117 void MainWindow::keyPressEvent(QKeyEvent *event)
118 {
119     if(event->key() == Qt::Key_F7)
120         emit volumeUpPressed();
121     else if(event->key() == Qt::Key_F8)
122         emit volumeDownPressed();
123     QMainWindow::keyPressEvent(event);
124 }
125
126 void MainWindow::tocNavigation()
127 {
128     QList<QString> toc = currentBook->getTOC();
129     if(toc.isEmpty())
130         return;
131     //TODO: Show notofication banner if no TOC
132     QList<ListItem > list;
133     for(QList<QString>::iterator iter = toc.begin(); iter != toc.end(); ++iter){
134         ListItem item;
135         item.text = *iter;
136         list.push_back(item);
137     }
138 /*    if(!toc.isEmpty()){
139         SelectorDialog *dialog = new SelectorDialog(toc, this);
140         dialog->setAttribute(Qt::WA_Maemo5StackedWindow);
141         dialog->setWindowFlags(dialog->windowFlags() | Qt::Window);
142         connect(dialog, SIGNAL(selectedString(QString)), bookView, SLOT(goToChapter(QString)));
143         dialog->show();
144     }*/
145
146     ListViewDialog *dialog = new ListViewDialog(this);
147     ListModel *model = new ListModel(list);
148     dialog->setModel(model);
149     dialog->setAttribute(Qt::WA_Maemo5StackedWindow);
150     dialog->setWindowFlags(dialog->windowFlags() | Qt::Window);
151     connect(dialog, SIGNAL(selectedString(QString)), bookView, SLOT(goToChapter(QString)));
152     dialog->show();
153
154 }
155
156 void MainWindow::viewBookmarks()
157 {
158     QList<Bookmark> bm = currentBook->getBookmarks();
159     if(bm.isEmpty())
160         return;
161     QList<ListItem > list;
162     for(QList<Bookmark>::iterator iter = bm.begin(); iter != bm.end(); ++iter){
163         Bookmark bookmark = *iter;
164         QPixmap ip(32, 32); 
165         ip.fill(Qt::transparent);
166         QPainter painter(&ip);
167         painter.setRenderHint(QPainter::Antialiasing, true);
168         painter.setPen(Qt::NoPen);
169         painter.setBrush(bookmark.getColor());
170         painter.drawRoundedRect(ip.rect(), 3, 3);
171 //        QIcon icon(ip);
172         ListItem item;
173         item.text = bookmark.getName();
174         item.subText = currentBook->getSectionName(bookmark.getSection()) + ", page " + QString::number(bookmark.getPage() + 1);
175         item.icon = ip;
176
177         list.push_back(item);
178     }
179 //    SelectorDialog *dialog = new SelectorDialog(list, this);
180     
181     ListViewDialog *dialog = new ListViewDialog(this);
182     ListModel *model = new ListModel(list);
183     dialog->setModel(model);
184     dialog->setAttribute(Qt::WA_Maemo5StackedWindow);
185     dialog->setWindowFlags(dialog->windowFlags() | Qt::Window);
186     connect(dialog, SIGNAL(selectedIndex(int)), bookView, SLOT(goToBookmark(int)));
187     dialog->show();
188 }
189
190
191 void MainWindow::viewAnnotations()
192 {
193     QList<Annotation> a = currentBook->getAnnotations();
194     if(a.isEmpty())
195         return;
196     QList<ListItem> list;
197     for(QList<Annotation>::iterator iter = a.begin(); iter != a.end(); ++iter){
198         Annotation annotation = *iter;
199         QPixmap ip(32, 32); 
200         ip.fill(Qt::transparent);
201         QPainter painter(&ip);
202         painter.setRenderHint(QPainter::Antialiasing, true);
203         painter.setPen(Qt::NoPen);
204         painter.setBrush(annotation.getColor());
205         painter.drawRoundedRect(ip.rect(), 3, 3);
206
207         ListItem item;
208         item.icon = ip;
209         item.text = annotation.getAnnotation();
210         item.subText = currentBook->getSectionName(annotation.getSection());
211
212         list.push_back(item);
213     }
214     ListViewDialog *dialog = new ListViewDialog(this);
215     ListModel *model = new ListModel(list);
216     dialog->setModel(model);
217     connect(dialog, SIGNAL(selectedIndex(int)), bookView, SLOT(goToAnnotation(int)));
218     dialog->setAttribute(Qt::WA_Maemo5StackedWindow);
219     dialog->setWindowFlags(dialog->windowFlags() | Qt::Window);
220     dialog->show();
221 }
222
223 void MainWindow::grabZoomKeys(bool grab)
224 {
225     if(!winId()){
226         qDebug("Can't grab keys unless we have a window id");
227         return;
228     }
229
230     unsigned long val = (grab) ? 1 : 0;
231     Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
232     if(!atom){
233         qDebug("Unable to obtain _HILDON_ZOOM_KEY_ATOM.");
234         return;
235     }
236
237     XChangeProperty(QX11Info::display(),
238         winId(),
239         atom,
240         XA_INTEGER,
241         32,
242         PropModeReplace,
243         reinterpret_cast<unsigned char*>(&val),
244         1);
245
246     grabKeyboard();
247 }
248
249 void MainWindow::closeEvent(QCloseEvent *event)
250 {
251     bookView->saveProgress(); 
252     QMainWindow::closeEvent(event);
253 }
254
255 void MainWindow::forceBacklightOn()
256 {
257     if(osso_context)
258         osso_display_blanking_pause(osso_context);
259 }