2024-08-16 11:39:30 +08:00
|
|
|
|
#include "designerView.h"
|
2024-08-22 20:21:47 +08:00
|
|
|
|
#include <QMouseEvent>
|
|
|
|
|
|
|
|
|
|
|
|
#define MAX_ZoomValue 50.0
|
|
|
|
|
|
#define MIN_ZoomValue 0.02
|
2024-08-16 11:39:30 +08:00
|
|
|
|
|
|
|
|
|
|
DesignerView::DesignerView(QWidget *parent)
|
|
|
|
|
|
: QGraphicsView(parent)
|
|
|
|
|
|
{
|
2024-08-22 20:21:47 +08:00
|
|
|
|
m_bMousePress = false;
|
|
|
|
|
|
m_dZoomVaule = 1.0;
|
2024-08-16 11:39:30 +08:00
|
|
|
|
initialize();
|
|
|
|
|
|
}
|
|
|
|
|
|
DesignerView::~DesignerView()
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DesignerView::initialize()
|
|
|
|
|
|
{
|
|
|
|
|
|
//去掉QGraphicsView自带滚动条
|
|
|
|
|
|
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
|
|
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
|
|
//设置背景
|
|
|
|
|
|
setStyleSheet("background-image: url(:/images/checkerboard.png);\
|
|
|
|
|
|
padding: 0px; \
|
|
|
|
|
|
border: 0px;");
|
|
|
|
|
|
//打开反锯齿
|
|
|
|
|
|
setRenderHint(QPainter::Antialiasing);
|
|
|
|
|
|
setMouseTracking(true);
|
|
|
|
|
|
//setDragMode(QGraphicsView::RubberBandDrag); //将控制交给selector
|
|
|
|
|
|
|
|
|
|
|
|
centerOn(0, 0);
|
|
|
|
|
|
}
|
2024-08-22 20:21:47 +08:00
|
|
|
|
|
|
|
|
|
|
bool DesignerView::zoomLimit(double& value)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_dZoomVaule *= value;
|
|
|
|
|
|
if(m_dZoomVaule >= MAX_ZoomValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_dZoomVaule = MAX_ZoomValue;
|
|
|
|
|
|
value = m_dZoomVaule / getScaleFactor();
|
|
|
|
|
|
}
|
|
|
|
|
|
else if(m_dZoomVaule <= MIN_ZoomValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_dZoomVaule = MIN_ZoomValue;
|
|
|
|
|
|
value = m_dZoomVaule / getScaleFactor();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(qFabs(value - 1) < 0.01) //缩放因子近似为1
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DesignerView::zoom(const QPointF& mousePos, double zoomFactor)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(zoomLimit(zoomFactor))
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
* QGraphicsView的默认transformationAnchor(变换锚点)是AnchorViewCenter,也就是中心,此处希望以鼠标指向为变换中心,
|
|
|
|
|
|
* AnchorUnderMouse可以实现,但经尝试没有自己进行移动(translate)效果好,所以先设置成NoAnchor,变换后再设置回来
|
|
|
|
|
|
*/
|
|
|
|
|
|
ViewportAnchor lastAnchor = this->transformationAnchor();
|
|
|
|
|
|
setTransformationAnchor(QGraphicsView::NoAnchor);
|
|
|
|
|
|
QPointF targetScenePos = mapToScene(mousePos.toPoint());
|
|
|
|
|
|
QTransform trans = transform();
|
|
|
|
|
|
trans.translate(targetScenePos.x(), targetScenePos.y());
|
|
|
|
|
|
trans.scale(zoomFactor, zoomFactor);
|
|
|
|
|
|
trans.translate(-targetScenePos.x(), -targetScenePos.y());
|
|
|
|
|
|
setTransform(trans);
|
|
|
|
|
|
setTransformationAnchor(lastAnchor);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double DesignerView::getScaleFactor()
|
|
|
|
|
|
{
|
|
|
|
|
|
//QTransform为一个3*3的矩阵,m11和m22元素分别为水平和垂直的缩放值
|
|
|
|
|
|
return this->transform().m11();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DesignerView::contextMenuEvent(QContextMenuEvent* event)
|
|
|
|
|
|
{
|
|
|
|
|
|
Q_UNUSED(event);
|
|
|
|
|
|
m_bMousePress = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DesignerView::mousePressEvent(QMouseEvent* event)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (event->button() == Qt::MiddleButton) //按住中键进行拖动
|
|
|
|
|
|
{
|
|
|
|
|
|
m_bMousePress = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
QGraphicsView::mousePressEvent(event);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DesignerView::mouseMoveEvent(QMouseEvent* event)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(m_bMousePress)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
QGraphicsView::mouseMoveEvent(event);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DesignerView::mouseReleaseEvent(QMouseEvent* event)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (event->button() == Qt::MiddleButton) //按住中键进行拖动
|
|
|
|
|
|
{
|
|
|
|
|
|
m_bMousePress = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
QGraphicsView::mouseReleaseEvent(event);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DesignerView::wheelEvent(QWheelEvent* event) //滚轮进行放大缩小
|
|
|
|
|
|
{
|
|
|
|
|
|
QPointF mousePos = event->position();
|
|
|
|
|
|
double angleDeltaY = event->angleDelta().y();
|
|
|
|
|
|
double zoomFactor = qPow(1.0015, angleDeltaY); //可以实现更平滑的缩放
|
|
|
|
|
|
zoom(mousePos, zoomFactor);
|
|
|
|
|
|
|
|
|
|
|
|
//注意不要调用基类的滚轮函数,否则滚轮事件会映射到滚动条操作
|
|
|
|
|
|
//QGraphicsView::wheelEvent(event);
|
|
|
|
|
|
}
|