summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin T. H. Sandsmark <martin.sandsmark@kde.org>2016-10-15 13:22:35 (GMT)
committerMartin T. H. Sandsmark <martin.sandsmark@kde.org>2016-10-15 13:22:35 (GMT)
commit9d90a08ef5e22d682a5a5004128f72d1a5a84aff (patch)
treed6c464fb48b8054e8917c9a0b48f92b230bb0557
parentafb5e01aed442c862b053fbb8cb9323fb20c2dbe (diff)
Implement zooming with a pinch gesture on a touch screen
Patch by Oliver Sander <oliver.sander@tu-dresden.de> REVIEW: 128821 BUG: 366781
-rw-r--r--ui/pageview.cpp49
-rw-r--r--ui/pageview.h5
2 files changed, 54 insertions, 0 deletions
diff --git a/ui/pageview.cpp b/ui/pageview.cpp
index 5fbabb2..339ba69 100644
--- a/ui/pageview.cpp
+++ b/ui/pageview.cpp
@@ -37,6 +37,7 @@
#include <QDesktopServices>
#include <QMimeDatabase>
#include <QMimeData>
+#include <QGestureEvent>
#include <qaction.h>
#include <kactionmenu.h>
@@ -416,6 +417,9 @@ PageView::PageView( QWidget *parent, Okular::Document *document )
// connect(...);
setAttribute( Qt::WA_InputMethodEnabled, true );
+ // Grab pinch gestures to rotate the view (and do things like zooming eventually)
+ grabGesture(Qt::PinchGesture);
+
d->magnifierView = new MagnifierView(document, this);
d->magnifierView->hide();
d->magnifierView->setGeometry(0, 0, 351, 201); // TODO: more dynamic?
@@ -1481,6 +1485,51 @@ void PageView::setCapability( ViewCapability capability, const QVariant &option
//END View inherited methods
//BEGIN widget events
+bool PageView::event( QEvent * event )
+{
+ if ( event->type() == QEvent::Gesture )
+ {
+ return gestureEvent(static_cast<QGestureEvent*>( event ));
+ }
+
+ // do not stop the event
+ return QAbstractScrollArea::event( event );
+}
+
+
+bool PageView::gestureEvent( QGestureEvent * event )
+{
+ QPinchGesture *pinch = static_cast<QPinchGesture*>(event->gesture(Qt::PinchGesture));
+
+ if (pinch)
+ {
+ // Viewport zoom level at the moment where the pinch gesture starts.
+ // The viewport zoom level _during_ the gesture will be this value
+ // times the relative zoom reported by QGestureEvent.
+ static qreal vanillaZoom = d->zoomFactor;
+
+ if (pinch->state() == Qt::GestureStarted)
+ {
+ vanillaZoom = d->zoomFactor;
+ }
+
+ // Zoom
+ if (pinch->changeFlags() & QPinchGesture::ScaleFactorChanged)
+ {
+ d->zoomFactor = vanillaZoom * pinch->totalScaleFactor();
+
+ d->blockPixmapsRequest = true;
+ updateZoom( ZoomRefreshCurrent );
+ d->blockPixmapsRequest = false;
+ viewport()->repaint();
+ }
+
+ return true;
+ }
+
+ return false;
+}
+
void PageView::paintEvent(QPaintEvent *pe)
{
const QPoint areaPos = contentAreaPosition();
diff --git a/ui/pageview.h b/ui/pageview.h
index 7c43f51..77781c3 100644
--- a/ui/pageview.h
+++ b/ui/pageview.h
@@ -43,6 +43,8 @@ class PageViewPrivate;
class MagnifierView;
+class QGestureEvent;
+
/**
* @short The main view. Handles zoom and continuous mode.. oh, and page
* @short display of course :-)
@@ -126,7 +128,10 @@ Q_OBJECT
void fitWindowToPage( const QSize& pageViewPortSize, const QSize& pageSize );
protected:
+ bool event( QEvent * event ) Q_DECL_OVERRIDE;
+
void resizeEvent( QResizeEvent* ) Q_DECL_OVERRIDE;
+ bool gestureEvent( QGestureEvent * e );
// mouse / keyboard events
void keyPressEvent( QKeyEvent* ) Q_DECL_OVERRIDE;