Added handlers for nav & zoom keys.
authorJan Dumon <j.dumon@option.com>
Tue, 2 Mar 2010 22:47:35 +0000 (23:47 +0100)
committerJan Dumon <j.dumon@option.com>
Tue, 2 Mar 2010 22:47:35 +0000 (23:47 +0100)
src/contentwindow.cpp
src/contentwindow.h

index a41aa92..8d49b20 100644 (file)
@@ -3,6 +3,11 @@
 #include <QDesktopServices>
 #include <QtGui>
 #include <QtWebKit>
+
+#include <QtGui/QX11Info>
+#include <X11/Xlib.h>
+#include <X11/Xatom.h>
+
 #include "contentwindow.h"
 
 /* Got ViewportItem and GraphicsView from maemobrowser in the qt examples. The
@@ -46,10 +51,8 @@ class ViewportItem : public QGraphicsWidget, public QAbstractKineticScroller {
                                case QEvent::GraphicsSceneMousePress:
                                case QEvent::GraphicsSceneMouseMove:
                                case QEvent::GraphicsSceneMouseRelease:
-                               /*case QEvent::GraphicsSceneMouseDoubleClick:*/ {
                                        res = handleMouseEvent(static_cast<QGraphicsSceneMouseEvent *>(e));
                                        break;
-                               }
                                default:
                                        break;
                                }
@@ -60,6 +63,50 @@ class ViewportItem : public QGraphicsWidget, public QAbstractKineticScroller {
                        return res ? true : QGraphicsWidget::sceneEventFilter(i, e);
                }
 
+               void keyPressEvent(QKeyEvent *e) {
+                       int x, y, w, h;
+
+                       switch(e->key()) {
+                       case Qt::Key_Down:
+                               y = m_widget->y() - 50;
+                               h = (m_widget->size() - size()).height();
+                               m_widget->setY(y < -h ? -h : y);
+                               break;
+
+                       case Qt::Key_Up:
+                               y = m_widget->y() + 50;
+                               m_widget->setY(y > 0 ? 0 : y);
+                               break;
+
+                       case Qt::Key_Right:
+                               x = m_widget->x() - 50;
+                               w = (m_widget->size() - size()).width();
+                               m_widget->setX(x < -w ? -w : x);
+                               break;
+
+                       case Qt::Key_Left:
+                               x = m_widget->x() + 50;
+                               m_widget->setX(x > 0 ? 0 : x);
+                               break;
+
+                       case Qt::Key_F7:
+                               if(qgraphicsitem_cast<QGraphicsWebView *>(m_widget)) {
+                                       QGraphicsWebView *wv = (QGraphicsWebView *)m_widget;
+                                       wv->setZoomFactor(wv->zoomFactor() * 1.25);
+                                       resizeWebViewToFrame();
+                               }
+                               break;
+
+                       case Qt::Key_F8:
+                               if(qgraphicsitem_cast<QGraphicsWebView *>(m_widget)) {
+                                       QGraphicsWebView *wv = (QGraphicsWebView *)m_widget;
+                                       wv->setZoomFactor(wv->zoomFactor() / 1.25);
+                                       resizeWebViewToFrame();
+                               }
+                               break;
+                       }
+               }
+
                QSize viewportSize() const {
                        return size().toSize();
                }
@@ -185,6 +232,10 @@ ContentWindow::ContentWindow(QWidget *parent, Entry *e) : QMainWindow(parent) {
        webview->setHtml(entry->content);
 
        setCentralWidget(gv);
+       gv->viewportItem()->setFocus();
+       gv->viewportItem()->grabKeyboard();
+
+       grabZoomKeys(true);
 }
 
 ContentWindow::~ContentWindow() {
@@ -206,9 +257,28 @@ void ContentWindow::seeOriginal() {
 }
 
 void ContentWindow::closeEvent(QCloseEvent *event) {
+       grabZoomKeys(false);
        entry->markRead(!keepUnread->isChecked());
        entry->markStar(starred->isChecked());
        QMainWindow::closeEvent(event);
 }
 
+void ContentWindow::grabZoomKeys(bool grab) {
+       if (!winId()) {
+               qWarning("Can't grab keys unless we have a window id");
+               return;
+       }
+
+       unsigned long val = (grab) ? 1 : 0;
+       Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
+       if (!atom) {
+               qWarning("Unable to obtain _HILDON_ZOOM_KEY_ATOM. This example will only work "
+                       "on a Maemo 5 device!");
+               return;
+       }
+
+       XChangeProperty (QX11Info::display(), winId(), atom, XA_INTEGER, 32, PropModeReplace,
+               reinterpret_cast<unsigned char *>(&val), 1);
+}
+
 #include "contentwindow.moc"
index 5b07254..26ad9bb 100644 (file)
@@ -25,6 +25,8 @@ class ContentWindow : public QMainWindow {
                QGraphicsWebView *webview;
                QAction *starred;
                QAction *keepUnread;
+
+               void grabZoomKeys(bool grab);
 };
 
 #endif