From c8b52db026a108cd170b0891de6edd179490af18 Mon Sep 17 00:00:00 2001 From: Jan Dumon Date: Tue, 2 Mar 2010 23:47:35 +0100 Subject: [PATCH 1/1] Added handlers for nav & zoom keys. --- src/contentwindow.cpp | 74 +++++++++++++++++++++++++++++++++++++++++++++++-- src/contentwindow.h | 2 ++ 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/src/contentwindow.cpp b/src/contentwindow.cpp index a41aa92..8d49b20 100644 --- a/src/contentwindow.cpp +++ b/src/contentwindow.cpp @@ -3,6 +3,11 @@ #include #include #include + +#include +#include +#include + #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(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(m_widget)) { + QGraphicsWebView *wv = (QGraphicsWebView *)m_widget; + wv->setZoomFactor(wv->zoomFactor() * 1.25); + resizeWebViewToFrame(); + } + break; + + case Qt::Key_F8: + if(qgraphicsitem_cast(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(&val), 1); +} + #include "contentwindow.moc" diff --git a/src/contentwindow.h b/src/contentwindow.h index 5b07254..26ad9bb 100644 --- a/src/contentwindow.h +++ b/src/contentwindow.h @@ -25,6 +25,8 @@ class ContentWindow : public QMainWindow { QGraphicsWebView *webview; QAction *starred; QAction *keepUnread; + + void grabZoomKeys(bool grab); }; #endif -- 1.7.9.5