Quilting-lw/sharedviews/view/mygraphicsview.cpp
2026-01-23 16:37:18 +08:00

396 lines
10 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "mygraphicsview.h"
MyGraphicsView::MyGraphicsView(QWidget *parent, double gridUnit) : QGraphicsView(parent)
{
m_scene = new MyGraphicsScene();
m_scene->setSceneRect(0,0,SCENESIZE,SCENESIZE);//1016像素1像素代表1毫米
this->setScene(m_scene);
setMouseTracking(true); // 跟踪鼠标位置
m_scene->addGrid(gridUnit,m_scene->sceneRect());
connect(m_scene, SIGNAL(siCurrentIndex(int)), this, SIGNAL(siCurrentIndex(int)));
}
MyGraphicsView::~MyGraphicsView()
{
if(m_scene != NULL)
{
delete m_scene;
m_scene = NULL;
}
}
void MyGraphicsView::restoreView()
{
//还原缩放,恢复默认大小
this->resetTransform();
// 计算场景的中心点
// 获取场景中的矩形范围
QRectF sceneRect = m_scene->sceneRect();
// 计算场景的中心点
QPointF centerPoint = sceneRect.center();
// 获取当前视图的大小
int viewWidth = this->width();
int viewHeight = this->height();
// 计算滚动条应该移动的距离
// 目标是让场景的中心点与视图的中心点对齐
int hValue = centerPoint.x() - viewWidth / 2;
int vValue = centerPoint.y() - viewHeight / 2;
// 设置滚动条的位置
this->horizontalScrollBar()->setValue(hValue);
this->verticalScrollBar()->setValue(vValue);
m_scene->cleanScene();
}
void MyGraphicsView::resizeEvent(QResizeEvent *event)
{
if(event == NULL){}//为了去掉警告
}
void MyGraphicsView::mouseDoubleClickEvent(QMouseEvent *event)
{
qreal scaleFactor = this->matrix().m11();
if(scaleFactor >= 200){return;}
if(event->buttons() & Qt::LeftButton &&
event->type() == QEvent::MouseButtonDblClick)
{
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
scale(2.0, 2.0);
}
}
void MyGraphicsView::mousePressEvent(QMouseEvent *event)
{
if(event->buttons() & Qt::LeftButton)
{
m_leftPressPoint = event->pos();
}
QGraphicsView::mousePressEvent(event);
}
//恢复原状
void MyGraphicsView::mouseReleaseEvent(QMouseEvent *event)
{
QGraphicsView::mouseReleaseEvent(event);
}
void MyGraphicsView::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() & Qt::LeftButton)
{
bool bl = m_scene->getCurSelectState();
//当scene选中某点时不拖拽scene位置
if(bl == true)
{
return QGraphicsView::mouseMoveEvent(event);
}
m_leftReleasePoint = event->pos();
// 计算鼠标移动的距离
int deltaX = m_leftReleasePoint.x() - m_leftPressPoint.x();
int deltaY = m_leftReleasePoint.y() - m_leftPressPoint.y();
// 更新滚动条的位置
horizontalScrollBar()->setValue(horizontalScrollBar()->value() - deltaX);
verticalScrollBar()->setValue(verticalScrollBar()->value() - deltaY);
// 更新起始点,以便下次移动时使用
m_leftPressPoint = m_leftReleasePoint;
}
return QGraphicsView::mouseMoveEvent(event);
}
void MyGraphicsView::wheelEvent(QWheelEvent *event)
{
// 获取当前鼠标相对于view的位置;
QPointF cursorPoint = event->pos();
// 获取当前鼠标相对于scene的位置;
QPointF scenePos = this->mapToScene(QPoint(cursorPoint.x(), cursorPoint.y()));
// 获取view的宽高;
qreal viewWidth = this->viewport()->width();
qreal viewHeight = this->viewport()->height();
// 获取当前鼠标位置相当于view大小的横纵比例;
qreal hScale = cursorPoint.x() / viewWidth;
qreal vScale = cursorPoint.y() / viewHeight;
int wheelDeltaValue = event->delta();
// 向上滚动,放大;
if (wheelDeltaValue > 0)
{
this->scale(1.2, 1.2);
}
// 向下滚动,缩小;
else
{
this->scale(1.0 / 1.2, 1.0 / 1.2);
}
// 将scene坐标转换为放大缩小后的坐标;
QPointF viewPoint = this->matrix().map(scenePos);
// 通过滚动条控制view放大缩小后的展示scene的位置;
horizontalScrollBar()->setValue(int(viewPoint.x() - viewWidth * hScale));
verticalScrollBar()->setValue(int(viewPoint.y() - viewHeight * vScale));
}
void MyGraphicsView::showEvent(QShowEvent *event)
{
horizontalScrollBar()->setValue(0);
verticalScrollBar()->setValue(0);
QGraphicsView::showEvent(event);
}
void MyGraphicsView::creatViewByData(QByteArray ary)
{
//还原缩放,恢复默认大小
this->resetTransform();
// 计算场景的中心点
// 获取场景中的矩形范围
QRectF sceneRect = m_scene->sceneRect();
// 计算场景的中心点
QPointF centerPoint = sceneRect.center();
// 获取当前视图的大小
int viewWidth = this->width();
int viewHeight = this->height();
// 计算滚动条应该移动的距离
// 目标是让场景的中心点与视图的中心点对齐
int hValue = centerPoint.x() - viewWidth / 2;
int vValue = centerPoint.y() - viewHeight / 2;
// 设置滚动条的位置
this->horizontalScrollBar()->setValue(hValue);
this->verticalScrollBar()->setValue(vValue);
m_scene->cleanScene();
m_scene->addItemToScene(ary);
}
void MyGraphicsView::creatViewByList(QList<drawItem> list)
{
restoreView();
m_scene->addItemListToScene(list);
}
void MyGraphicsView::enlargeView()
{
if(m_scene == NULL)
{
return;
}
QPointF cursorPoint;
QPointF scenePos;
bool bl = m_scene->getCurSelectState();
if(bl == false)
{
// 缩放视图
scale(1.2, 1.2);
return;
}
else
{
cursorPoint = m_leftPressPoint;
scenePos = this->mapToScene(QPoint(cursorPoint.x(), cursorPoint.y()));
// 获取view的宽高;
qreal viewWidth = this->viewport()->width();
qreal viewHeight = this->viewport()->height();
// 获取当前鼠标位置相当于view大小的横纵比例;
qreal hScale = cursorPoint.x() / viewWidth;
qreal vScale = cursorPoint.y() / viewHeight;
this->scale(1.2, 1.2);
// 将scene坐标转换为放大缩小后的坐标;
QPointF viewPoint = this->matrix().map(scenePos);
// 通过滚动条控制view放大缩小后的展示scene的位置;
horizontalScrollBar()->setValue(int(viewPoint.x() - viewWidth * hScale));
verticalScrollBar()->setValue(int(viewPoint.y() - viewHeight * vScale));
}
}
void MyGraphicsView::narrowView()
{
if(m_scene == NULL)
{
return;
}
QPointF cursorPoint;
QPointF scenePos;
bool bl = m_scene->getCurSelectState();
if(bl == false)
{
// 缩放视图
this->scale(1.0 / 1.2, 1.0 / 1.2);
return;
}
else
{
cursorPoint = m_leftPressPoint;
scenePos = this->mapToScene(QPoint(cursorPoint.x(), cursorPoint.y()));
// 获取view的宽高;
qreal viewWidth = this->viewport()->width();
qreal viewHeight = this->viewport()->height();
// 获取当前鼠标位置相当于view大小的横纵比例;
qreal hScale = cursorPoint.x() / viewWidth;
qreal vScale = cursorPoint.y() / viewHeight;
this->scale(1.0 / 1.2, 1.0 / 1.2);
// 将scene坐标转换为放大缩小后的坐标;
QPointF viewPoint = this->matrix().map(scenePos);
// 通过滚动条控制view放大缩小后的展示scene的位置;
horizontalScrollBar()->setValue(int(viewPoint.x() - viewWidth * hScale));
verticalScrollBar()->setValue(int(viewPoint.y() - viewHeight * vScale));
}
}
void MyGraphicsView::forwardAndBackward(int idx)
{
if(m_scene == NULL)
{
return;
}
bool bl = m_scene->initAPoint();
if(bl == false)
{
return;
}
m_scene->forwardAndBackward(idx);
}
int MyGraphicsView::getItemNums()
{
return m_scene->items().size();
}
void MyGraphicsView::moveScene(int x,int y)
{
// 更新滚动条的位置
horizontalScrollBar()->setValue(horizontalScrollBar()->value() - x);
verticalScrollBar()->setValue(verticalScrollBar()->value() - y);
}
void MyGraphicsView::autoView()
{
//还原缩放,恢复默认大小
this->resetTransform();
// 计算场景的中心点
// 获取场景中的矩形范围
QRectF sceneRect = m_scene->sceneRect();
// 计算场景的中心点
QPointF centerPoint = sceneRect.center();
// 获取当前视图的大小
int viewWidth = this->width();
int viewHeight = this->height();
// 计算滚动条应该移动的距离
// 目标是让场景的中心点与视图的中心点对齐
int hValue = centerPoint.x() - viewWidth / 2;
int vValue = centerPoint.y() - viewHeight / 2;
// 设置滚动条的位置
this->horizontalScrollBar()->setValue(hValue);
this->verticalScrollBar()->setValue(vValue);
}
void MyGraphicsView::insertPoint(int dir)
{
if(m_scene == NULL)
{
return;
}
bool bl = m_scene->getCurSelectState();
if(bl == false)
{
return;
}
m_scene->insertPoint(dir);
}
void MyGraphicsView::removeSelectPoint()
{
if(m_scene == NULL)
{
return;
}
bool bl = m_scene->getCurSelectState();
if(bl == false)
{
return;
}
else
{
m_scene->removeSelectPoint();
}
}
void MyGraphicsView::setShapeType(int type)
{
m_scene->setShapeType(type);
}
QList<QGraphicsEllipseItem *> MyGraphicsView::getPointList()
{
return m_scene->getPointList();
}
void MyGraphicsView::cleanView()
{
m_scene->cleanScene();
}
void MyGraphicsView::setAdsorption(bool adsorption)
{
m_scene->setAdsorption(adsorption);
}
void MyGraphicsView::setMaxSegmentLength(int value)
{
m_scene->setMaxSegmentLength(value);
}
int MyGraphicsView::getMaxSegmentLength()
{
return m_scene->getMaxSegmentLength();
}
void MyGraphicsView::setXYMove(double valX,double valY)
{
m_scene->setXYMove(valX,valY);
}
//撤销最后一个图元
void MyGraphicsView::revokeLastItem()
{
m_scene->revokeLastItem();
}
QList<GLineItem> MyGraphicsView::getLineItemList()
{
return m_scene->getLineItemList();
}
QList<GPathItem> MyGraphicsView::getPathItemList()
{
return m_scene->getPathItemList();
}