[qet] [3533] Element editor add touchpad gestures, thank Yoann

[ Thread Index | Date Index | More lists.tuxfamily.org/qet Archives ]


Revision: 3533
Author:   scorpio810
Date:     2014-12-06 18:56:07 +0100 (Sat, 06 Dec 2014)
Log Message:
-----------
Element editor add touchpad gestures, thank Yoann

Modified Paths:
--------------
    trunk/sources/editor/elementview.cpp
    trunk/sources/editor/elementview.h

Modified: trunk/sources/editor/elementview.cpp
===================================================================
--- trunk/sources/editor/elementview.cpp	2014-12-04 23:25:49 UTC (rev 3532)
+++ trunk/sources/editor/elementview.cpp	2014-12-06 17:56:07 UTC (rev 3533)
@@ -18,6 +18,7 @@
 #include "elementview.h"
 #include "qetelementeditor.h"
 #include "editorcommands.h"
+#include "qetapp.h"
 /**
 	Constructeur
 	@param scene ElementScene visualisee par cette ElementView
@@ -28,6 +29,7 @@
 	scene_(scene),
 	offset_paste_count_(0)
 {
+	grabGesture(Qt::PinchGesture);
 	setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
 	setInteractive(true);
 	setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
@@ -113,6 +115,22 @@
 }
 
 /**
+	Agrandit le schema avec le trackpad
+*/
+void ElementView::zoomInSlowly() {
+    scale(1.02, 1.02);
+	adjustSceneRect();
+}
+
+/**
+	Retrecit le schema avec le trackpad
+*/
+void ElementView::zoomOutSlowly() {
+    scale(0.98, 0.98);
+	adjustSceneRect();
+}
+
+/**
 	Agrandit ou rectrecit le schema de facon a ce que tous les elements du
 	schema soient visibles a l'ecran. S'il n'y a aucun element sur le schema,
 	le zoom est reinitialise
@@ -126,7 +144,7 @@
 	Reinitialise le zoom
 */
 void ElementView::zoomReset() {
-	resetSceneRect();
+    resetSceneRect();
 	resetMatrix();
 	scale(4.0, 4.0);
 }
@@ -382,20 +400,74 @@
 }
 
 /**
+ * @brief ElementView::gestures
+ * @return
+ */
+bool ElementView::gestures() const {
+	return(QETApp::settings().value("diagramview/gestures", false).toBool());
+}
+
+
+/**
  * @brief ElementView::wheelEvent
  * @param e
  */
 void ElementView::wheelEvent(QWheelEvent *e) {
 	//Zoom and scrolling
-	if (e -> buttons() != Qt::MidButton) {
-		if (!(e -> modifiers() & Qt::ControlModifier))
-			e -> delta() > 0 ? zoomIn() : zoomOut();
+//	if (e -> buttons() != Qt::MidButton) {
+//		if (!(e -> modifiers() & Qt::ControlModifier))
+//			e -> delta() > 0 ? zoomIn() : zoomOut();
+//		else
+//			QAbstractScrollArea::wheelEvent(e);
+//	}
+	if ( gestures() ) {
+		if (e -> modifiers() & Qt::ControlModifier)
+			e -> delta() > 0 ? zoomInSlowly() : zoomOutSlowly();
 		else
-			QAbstractScrollArea::wheelEvent(e);
+            QGraphicsView::wheelEvent(e);
+	} else {
+		e -> delta() > 0 ? zoomIn(): zoomOut();
 	}
 }
 
 /**
+	Gere les evenements de la ElementView
+	@param e Evenement
+*/
+bool ElementView::event(QEvent *e) {
+	// By default touch events are converted to mouse events. So
+	// after this event we will get a mouse event also but we want
+	// to handle touch events as gestures only. So we need this safeguard
+	// to block mouse events that are actually generated from touch.
+	if (e->type() == QEvent::Gesture)
+		return gestureEvent(static_cast<QGestureEvent *>(e));
+
+	return(QGraphicsView::event(e));
+}
+
+/**
+ * Utilise le pincement du trackpad pour zoomer
+ * @brief ElementView::gestureEvent
+ * @param event
+ * @return
+ */
+bool ElementView::gestureEvent(QGestureEvent *event){
+	if (QGesture *gesture = event->gesture(Qt::PinchGesture)) {
+		QPinchGesture *pinch = static_cast<QPinchGesture *>(gesture);
+		if (pinch->changeFlags() & QPinchGesture::ScaleFactorChanged){
+			qreal value = gesture->property("scaleFactor").toReal();
+			if (value > 1){
+				zoomInSlowly();
+			}else{
+				zoomOutSlowly();
+			}
+		}
+	}
+	return true;
+}
+
+
+/**
 	Dessine l'arriere-plan de l'editeur, cad la grille.
 	@param p Le QPainter a utiliser pour dessiner
 	@param r Le rectangle de la zone a dessiner

Modified: trunk/sources/editor/elementview.h
===================================================================
--- trunk/sources/editor/elementview.h	2014-12-04 23:25:49 UTC (rev 3532)
+++ trunk/sources/editor/elementview.h	2014-12-06 17:56:07 UTC (rev 3533)
@@ -47,6 +47,8 @@
 	void mousePressEvent(QMouseEvent *);
 	void mouseMoveEvent(QMouseEvent *);
 	void mouseReleaseEvent(QMouseEvent *);
+	bool gestureEvent(QGestureEvent *event);
+	bool event(QEvent *event);
 	void wheelEvent(QWheelEvent *);
 	virtual void drawBackground(QPainter *, const QRectF &);
 	
@@ -58,6 +60,8 @@
 	void setSelectionMode();
 	void zoomIn();
 	void zoomOut();
+	void zoomInSlowly();
+	void zoomOutSlowly();
 	void zoomFit();
 	void zoomReset();
 	void adjustSceneRect();
@@ -86,6 +90,7 @@
 	QPointF start_top_left_corner_;
 	QPointF reference_view_;
 	QPointF center_view_;
+	bool gestures() const;
 	bool is_moving_view_;               ///< Indicate whether the visualisation mode has been enabled due to mouse/keyboard interactions
 };
 #endif


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/