|
@@ -0,0 +1,218 @@
|
|
|
+#include "myview.h"
|
|
|
+#include <QScrollBar>
|
|
|
+#include <iostream>
|
|
|
+#define VIEW_CENTER viewport()->rect().center()
|
|
|
+const double PI = 3.1415926535898;
|
|
|
+
|
|
|
+MyView::MyView(QWidget *parent) :
|
|
|
+ QGraphicsView(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());
|
|
|
+ bottonstatus = true;
|
|
|
+ QGraphicsView::mousePressEvent(event);
|
|
|
+}
|
|
|
+void MyView::mouseMoveEvent(QMouseEvent *event)
|
|
|
+{
|
|
|
+ if(mbInPinch == true)return;
|
|
|
+ QGraphicsView::mouseMoveEvent(event);
|
|
|
+
|
|
|
+// QScrollBar * ps = verticalScrollBar();
|
|
|
+// std::cout<<" size is "<<ps->size().height()<<" v = "<<ps->value()<<std::endl;
|
|
|
+// QScrollBar * ps2= horizontalScrollBar();
|
|
|
+// std::cout<<" size is "<<ps2->size().width()<<" h = "<<ps2->value()<<std::endl;
|
|
|
+}
|
|
|
+void MyView::mouseReleaseEvent(QMouseEvent *event)
|
|
|
+{
|
|
|
+ bottonstatus = false;
|
|
|
+ QGraphicsView::mouseReleaseEvent(event);
|
|
|
+}
|
|
|
+
|
|
|
+// 放大/缩小
|
|
|
+void MyView::wheelEvent(QWheelEvent *event)
|
|
|
+{
|
|
|
+ // 滚轮的滚动量
|
|
|
+ QPoint scrollAmount = event->angleDelta();
|
|
|
+ // 正值表示滚轮远离使用者(放大),负值表示朝向使用者(缩小)
|
|
|
+ scrollAmount.y() > 0 ? zoomIn() : zoomOut();
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+// 放大
|
|
|
+void MyView::zoomIn()
|
|
|
+{
|
|
|
+
|
|
|
+ int width,hgt;
|
|
|
+ width = sceneRect().width();
|
|
|
+ hgt = sceneRect().height();
|
|
|
+ QScrollBar * psV = verticalScrollBar();
|
|
|
+ QScrollBar * psH = horizontalScrollBar();
|
|
|
+
|
|
|
+
|
|
|
+// qDebug("%d %d ",width,hgt);
|
|
|
+
|
|
|
+
|
|
|
+ int centery = (psV->value() + psV->size().height()/2)/beishu;
|
|
|
+ int centerx = (psH->value() + psH->size().width()/2)/beishu;
|
|
|
+
|
|
|
+#ifndef ANDROID
|
|
|
+ scale(1.1, 1.1);
|
|
|
+ beishu *= 1.1;
|
|
|
+#else
|
|
|
+ scale(1.6, 1.6);
|
|
|
+ beishu *= 1.6;
|
|
|
+#endif
|
|
|
+ // centerOn(450, 700 - (200 / beishu));
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// qDebug("beishu is %f",beishu);
|
|
|
+
|
|
|
+ centerOn(centerx,centery);
|
|
|
+
|
|
|
+ emit beishuchange(beishu);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// QPoint x = viewport()->rect().center();
|
|
|
+
|
|
|
+
|
|
|
+// std::cout<<" x is"<<sceneRect().bottom()<<" y is "<<sceneRect().y()<<std::endl;
|
|
|
+// QScrollBar * ps = verticalScrollBar();
|
|
|
+// std::cout<<" size is "<<ps->size().height()<<" v = "<<ps->value()<<std::endl;
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+// 缩小
|
|
|
+void MyView::zoomOut()
|
|
|
+{
|
|
|
+
|
|
|
+ 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;
|
|
|
+
|
|
|
+#ifndef ANDROID
|
|
|
+ scale(1 / 1.1, 1 / 1.1);
|
|
|
+ beishu /= 1.1;
|
|
|
+#else
|
|
|
+ scale(1 / 1.6, 1 / 1.6);
|
|
|
+ beishu /= 1.6;
|
|
|
+#endif
|
|
|
+// centerOn(450, 700 - (200 / beishu));
|
|
|
+
|
|
|
+
|
|
|
+ centerOn(centerx,centery);
|
|
|
+
|
|
|
+ emit beishuchange(beishu);
|
|
|
+}
|
|
|
+
|
|
|
+void MyView::zoomone()
|
|
|
+{
|
|
|
+
|
|
|
+ scale(1 /beishu, 1 / beishu);
|
|
|
+ beishu = 1.0;
|
|
|
+
|
|
|
+ emit beishuchange(beishu);
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+void MyView::mouseDoubleClickEvent(QMouseEvent *event)
|
|
|
+{
|
|
|
+
|
|
|
+ QScrollBar * psV = verticalScrollBar();
|
|
|
+ QScrollBar * psH = horizontalScrollBar();
|
|
|
+
|
|
|
+ int centery = (psV->value() + psV->size().height()/2)/beishu;
|
|
|
+ int centerx = (psH->value() + psH->size().width()/2)/beishu;
|
|
|
+
|
|
|
+
|
|
|
+// qDebug("x is %d y is %d view center x is %d centerx is %d",event->pos().x(),
|
|
|
+// event->pos().y(),
|
|
|
+// viewport()->rect().center().x(),centerx);
|
|
|
+
|
|
|
+ int viewx,viewy;
|
|
|
+ if(beishu == 0)return;
|
|
|
+ viewx = centerx +(event->pos().x() - viewport()->rect().center().x())/beishu;
|
|
|
+ viewy = centery +(event->pos().y() - viewport()->rect().center().y())/beishu;
|
|
|
+
|
|
|
+ QPoint viewpoint;
|
|
|
+ viewpoint.setX(viewx);
|
|
|
+ viewpoint.setY(viewy);
|
|
|
+
|
|
|
+ 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);
|
|
|
+
|
|
|
+}
|
|
|
+
|