#include "mygraphicsview.h" MyGraphicsView::MyGraphicsView() { m_viewSize = this->size(); m_scene = new MyGraphicsScene(); this->setScene(m_scene); setMouseTracking(true); // 跟踪鼠标位置 // this->setRenderHint(QPainter::Antialiasing, true); // this->setRenderHint(QPainter::SmoothPixmapTransform, true);//防锯齿 } MyGraphicsView::~MyGraphicsView() { if(m_scene != NULL) { delete m_scene; m_scene = NULL; } } void MyGraphicsView::resizeEvent(QResizeEvent *event) { if(event == NULL){}//为了去掉警告 if(m_scene != NULL && m_viewSize != this->size()) { QRectF rectItem = m_scene->itemsBoundingRect(); this->setScene(m_scene); this->fitInView(rectItem, Qt::KeepAspectRatio); } m_viewSize = this->size(); } void MyGraphicsView::mouseDoubleClickEvent(QMouseEvent *event) { qreal scaleFactor = this->matrix().m11(); if(scaleFactor >= 20){return;} if(event->button() == Qt::LeftButton && event->type() == QEvent::MouseButtonDblClick) { setTransformationAnchor(QGraphicsView::AnchorUnderMouse); scale(2.0, 2.0); } if(event->button() == Qt::RightButton && event->type() == QEvent::MouseButtonDblClick) { QRectF rectItem = m_scene->itemsBoundingRect(); this->fitInView(rectItem, Qt::KeepAspectRatio); } } void MyGraphicsView::mousePressEvent(QMouseEvent *event) { if(event->button() == Qt::LeftButton) { setDragMode(QGraphicsView::RubberBandDrag); m_leftBtnPressed = true; m_startPoint = m_endPoint = event->pos(); } else { setDragMode(QGraphicsView::NoDrag); } if(event->button() == Qt::RightButton) { m_startPoint = event->pos(); m_rightBtnPressed = true; } QGraphicsView::mousePressEvent(event); } //恢复原状 void MyGraphicsView::mouseReleaseEvent(QMouseEvent *event) { if(event->button() == Qt::LeftButton) { m_leftBtnPressed = false; if(m_startPoint.x() == m_endPoint.x() || m_startPoint.y() == m_endPoint.y()) { return; } double cx = (m_endPoint.x() + m_startPoint.x()) / 2.0; double cy = (m_endPoint.y() + m_startPoint.y()) / 2.0; // 获取当前鼠标相对于scene的位置; QPointF scenePos = this->mapToScene(QPoint(cx,cy)); // 获取view的宽高; qreal viewWidth = this->viewport()->width(); qreal viewHeight = this->viewport()->height(); // 获取当前鼠标位置相当于view大小的横纵比例; qreal hScale = cx / viewWidth; qreal vScale = cy / viewHeight; this->scale(2.0, 2.0); // 将scene坐标转换为放大缩小后的坐标; QPointF viewPoint = this->matrix().map(scenePos); // 通过滚动条控制view放大缩小后的展示scene的位置; horizontalScrollBar()->setValue(int(viewPoint.x() - viewWidth * hScale)); verticalScrollBar()->setValue(int(viewPoint.y() - viewHeight * vScale)); } if(event->button() == Qt::RightButton) { m_rightBtnPressed = false; } } void MyGraphicsView::mouseMoveEvent(QMouseEvent *event) { //QGraphicsView坐标 QPoint viewPoint = event->pos(); //QGraphicsScene坐标 QPointF scenePoint = mapToScene(viewPoint); // double x = scenePoint.x() / MMPIXELY; // double y = abs(abs(scenePoint.y()) - m_scene->itemsBoundingRect().height()) / MMPIXELY; double x = (scenePoint.x() - DRAWMARGINS/2.0) / MMPIXELY; double y = (m_scene->itemsBoundingRect().height() - scenePoint.y() + DRAWMARGINS/2.0) / MMPIXELY; scenePoint.setX(x); scenePoint.setY(y); setMouseTracking(true); siMouseMove(scenePoint); if(m_leftBtnPressed) { m_endPoint = event->pos(); } if(m_rightBtnPressed) { QPoint delta = m_startPoint - event->pos(); int hValue = horizontalScrollBar()->value(); int vValue = verticalScrollBar()->value(); horizontalScrollBar()->setValue(delta.x() + hValue); verticalScrollBar()->setValue(delta.y() + vValue); m_startPoint = event->pos(); } 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)); } CreatPictureStr MyGraphicsView::getPictureByDat(Marker marker,int paperWidth,int butSpace,int rightSpace,int penWidth) { CreatPictureStr picStr; if(m_scene != NULL) { picStr = m_scene->getPictureByDat(marker,paperWidth,butSpace,rightSpace,penWidth); m_scene->addItemToScene(); QRectF rectItem = m_scene->itemsBoundingRect(); this->fitInView(rectItem, Qt::KeepAspectRatio); } return picStr; } CreatPictureStr MyGraphicsView::getPictureByBmp(QPixmap pixmap,int paperWidth,int butSpace,int rightSpace,int penWidth) { CreatPictureStr picStr; if(m_scene != NULL) { picStr = m_scene->getPictureByBmp(pixmap,paperWidth,butSpace,rightSpace,penWidth); m_scene->addItemToScene(); QRectF rectItem = m_scene->itemsBoundingRect(); this->fitInView(rectItem, Qt::KeepAspectRatio); } return picStr; } void MyGraphicsView::cleanView() { if(m_scene != NULL) { //这里delete掉再重新new是因为view显示完大图再显示小图,图形会不居中,未找到其他解决方法 delete m_scene; m_scene = NULL; m_scene = new MyGraphicsScene(); this->setScene(m_scene); } } void MyGraphicsView::swithViewByPic(QPicture pic) { if(m_scene != NULL) { m_scene->swithSceneByPic(pic); QRectF rectItem = m_scene->itemsBoundingRect(); this->fitInView(rectItem, Qt::KeepAspectRatio); } } void MyGraphicsView::reflushBlockView(QPoint p) { if(m_scene != NULL) { m_scene->reflushBlockScene(p); } }