Implement zooming with a pinch gesture on a touch screen

Patch by Oliver Sander <oliver.sander@tu-dresden.de>

REVIEW: 128821
BUG: 366781
frameworks
Martin T. H. Sandsmark 10 years ago
parent afb5e01aed
commit 9d90a08ef5
  1. 49
      ui/pageview.cpp
  2. 5
      ui/pageview.h

@ -37,6 +37,7 @@
#include <QDesktopServices> #include <QDesktopServices>
#include <QMimeDatabase> #include <QMimeDatabase>
#include <QMimeData> #include <QMimeData>
#include <QGestureEvent>
#include <qaction.h> #include <qaction.h>
#include <kactionmenu.h> #include <kactionmenu.h>
@ -416,6 +417,9 @@ PageView::PageView( QWidget *parent, Okular::Document *document )
// connect(...); // connect(...);
setAttribute( Qt::WA_InputMethodEnabled, true ); 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 = new MagnifierView(document, this);
d->magnifierView->hide(); d->magnifierView->hide();
d->magnifierView->setGeometry(0, 0, 351, 201); // TODO: more dynamic? 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 //END View inherited methods
//BEGIN widget events //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) void PageView::paintEvent(QPaintEvent *pe)
{ {
const QPoint areaPos = contentAreaPosition(); const QPoint areaPos = contentAreaPosition();

@ -43,6 +43,8 @@ class PageViewPrivate;
class MagnifierView; class MagnifierView;
class QGestureEvent;
/** /**
* @short The main view. Handles zoom and continuous mode.. oh, and page * @short The main view. Handles zoom and continuous mode.. oh, and page
* @short display of course :-) * @short display of course :-)
@ -126,7 +128,10 @@ Q_OBJECT
void fitWindowToPage( const QSize& pageViewPortSize, const QSize& pageSize ); void fitWindowToPage( const QSize& pageViewPortSize, const QSize& pageSize );
protected: protected:
bool event( QEvent * event ) Q_DECL_OVERRIDE;
void resizeEvent( QResizeEvent* ) Q_DECL_OVERRIDE; void resizeEvent( QResizeEvent* ) Q_DECL_OVERRIDE;
bool gestureEvent( QGestureEvent * e );
// mouse / keyboard events // mouse / keyboard events
void keyPressEvent( QKeyEvent* ) Q_DECL_OVERRIDE; void keyPressEvent( QKeyEvent* ) Q_DECL_OVERRIDE;

Loading…
Cancel
Save