Переглянути джерело

chang adcxodrviewer. add support for touch.

yuchuli 4 роки тому
батько
коміт
884d56bfb8

+ 2 - 0
src/tool/map_lanetoxodr/main.cpp

@@ -17,7 +17,9 @@
 #endif
 
 
+#ifdef XODRViewer
 #include "filedialogextern.h"
+#endif
 #ifdef ANDROID
 #include "simpleCustomEvent.h"
 #endif

+ 65 - 0
src/tool/map_lanetoxodr/myview.cpp

@@ -9,8 +9,15 @@ MyView::MyView(QWidget *parent) :
     beishu(1.00000)
 {
     setDragMode(QGraphicsView::ScrollHandDrag);
+
+//    grabGesture(Qt::PanGesture);
+    grabGesture(Qt::PinchGesture);
+ //   grabGesture(Qt::SwipeGesture);
 }
 
+
+
+
 void MyView::mousePressEvent(QMouseEvent *event)
 {
 //    qDebug("x is %d",event->pos().x());
@@ -19,6 +26,7 @@ void MyView::mousePressEvent(QMouseEvent *event)
 }
 void MyView::mouseMoveEvent(QMouseEvent *event)
 {
+    if(mbInPinch == true)return;
     QGraphicsView::mouseMoveEvent(event);
 
 //    QScrollBar * ps = verticalScrollBar();
@@ -145,3 +153,60 @@ void MyView::mouseDoubleClickEvent(QMouseEvent *event)
     emit dbclickxy(viewx,viewy);
     qDebug("view x is %d view y is %d ",viewx,viewy);
 }
+
+bool MyView::event(QEvent *event)
+{
+    if (event->type() == QEvent::Gesture)
+    {
+ //       qDebug("gestrue event");
+ //       return true;
+        return gestureEvent(static_cast<QGestureEvent*>(event));
+    }
+
+    return QGraphicsView::event(event);
+}
+
+bool MyView::gestureEvent(QGestureEvent *event)
+{
+    if (QGesture *pinch = event->gesture(Qt::PinchGesture))
+        pinchTriggered(static_cast<QPinchGesture *>(pinch));
+    return true;
+}
+
+void MyView::pinchTriggered(QPinchGesture *gesture)
+{
+
+    static double currentStepScaleFactor = 1;
+    static double oldfactor = 1;
+    QPinchGesture::ChangeFlags changeFlags = gesture->changeFlags();
+    if (changeFlags & QPinchGesture::ScaleFactorChanged) {
+        currentStepScaleFactor = gesture->totalScaleFactor();
+        mbInPinch = true;
+    }
+    if (gesture->state() == Qt::GestureFinished) {
+//        scaleFactor *= currentStepScaleFactor;
+//        qDebug("scale is %f ",currentStepScaleFactor);
+
+        currentStepScaleFactor = 1;
+        oldfactor = 1;
+        mbInPinch = false;
+    }
+
+    int width,hgt;
+    width = sceneRect().width();
+    hgt = sceneRect().height();
+    QScrollBar * psV = verticalScrollBar();
+    QScrollBar * psH = horizontalScrollBar();
+
+    int centery = (psV->value() + psV->size().height()/2)/beishu;
+    int centerx = (psH->value() + psH->size().width()/2)/beishu;
+
+    double fscale = currentStepScaleFactor/oldfactor;
+    scale(fscale,fscale);
+    beishu *= fscale;
+    oldfactor = currentStepScaleFactor;
+
+    centerOn(centerx,centery);
+
+}
+

+ 11 - 0
src/tool/map_lanetoxodr/myview.h

@@ -11,6 +11,9 @@
 #include <QGraphicsItem>
 #include <QKeyEvent>
 
+#include <QGestureEvent>
+#include <QPinchGesture>
+
 class MyView : public QGraphicsView
 {
     Q_OBJECT
@@ -26,6 +29,8 @@ protected:
     void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
     void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
     void mouseDoubleClickEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
+
+    bool event(QEvent *event) Q_DECL_OVERRIDE;
 public Q_SLOTS:
     void zoomIn();  // 放大
     void zoomOut();  // 缩小
@@ -36,6 +41,12 @@ signals:
 private:
     bool bottonstatus = false;
     QPoint myview_lastMousePos;
+
+private:
+    bool gestureEvent(QGestureEvent *event);
+    void pinchTriggered(QPinchGesture*);
+
+    bool mbInPinch = false;
 };
 
 #endif // MYVIEW_H