diff --git a/include/designerView.h b/include/designerView.h index 8dc051b..4f124a0 100644 --- a/include/designerView.h +++ b/include/designerView.h @@ -11,6 +11,11 @@ public: explicit DesignerView(QWidget *parent = 0); virtual ~DesignerView(); + //视图操作-外部调用 + void zoomIn(); + void zoomOut(); + void zoomFit(); + protected: virtual void contextMenuEvent(QContextMenuEvent*) override; virtual void mousePressEvent(QMouseEvent*) override; @@ -20,14 +25,16 @@ protected: private: void initialize(); - //缩放相关 + //视图操作相关 void zoom(const QPointF&, double); bool zoomLimit(double&); double getScaleFactor(); + void translate(const QPointF&); private: - bool m_bMousePress; - double m_dZoomVaule; + bool m_bMousePress; + double m_dScale; + QPointF m_ptLatstMouse_view; //鼠标最后按下在view中的位置 }; #endif diff --git a/include/drawingPanel.h b/include/drawingPanel.h index 6bb309e..fa65e79 100644 --- a/include/drawingPanel.h +++ b/include/drawingPanel.h @@ -22,6 +22,10 @@ public: QGraphicsScene* getQGraphicsScene(); DesignerScene* getDesignerScene(); + void grahpicsViewZoomIn(); + void grahpicsViewZoomOut(); + void grahpicsViewZoomFit(); + public slots: void onSignal_addGraphicsItem(GraphicsItemType&); diff --git a/include/global.h b/include/global.h index 0a792cd..41eee44 100644 --- a/include/global.h +++ b/include/global.h @@ -3,8 +3,8 @@ #include -const double g_dGriaphicsScene_Width = 5000; -const double g_dGriaphicsScene_Height = 5000; +const double g_dGriaphicsScene_Width = 1200; +const double g_dGriaphicsScene_Height = 900; enum GraphicsItemType { diff --git a/include/mainwindow.h b/include/mainwindow.h index 9d83590..48475fc 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -29,12 +29,16 @@ public: protected: virtual void closeEvent(QCloseEvent* event) override; + virtual void changeEvent(QEvent* event) override; private: void initializeDockUi(); void initializeAction(); private slots: + void onAction_zoomIn(); + void onAction_zoomOut(); + void onAction_zoomFit(); void onSignal_addItem(QGraphicsItem*); void onSignal_deleteItem(); diff --git a/source/designerView.cpp b/source/designerView.cpp index 32779b5..3b28a4a 100644 --- a/source/designerView.cpp +++ b/source/designerView.cpp @@ -8,7 +8,7 @@ DesignerView::DesignerView(QWidget *parent) : QGraphicsView(parent) { m_bMousePress = false; - m_dZoomVaule = 1.0; + m_dScale = 1.0; initialize(); } DesignerView::~DesignerView() @@ -19,8 +19,8 @@ DesignerView::~DesignerView() void DesignerView::initialize() { //去掉QGraphicsView自带滚动条 - setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + //setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + //setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); //设置背景 setStyleSheet("background-image: url(:/images/checkerboard.png);\ padding: 0px; \ @@ -33,18 +33,77 @@ void DesignerView::initialize() centerOn(0, 0); } +void DesignerView::contextMenuEvent(QContextMenuEvent* event) +{ + Q_UNUSED(event); + m_bMousePress = false; +} + +void DesignerView::mousePressEvent(QMouseEvent* event) +{ + QGraphicsItem* item = scene()->itemAt(mapToScene(event->pos()), transform()); + if (event->button() == Qt::MiddleButton /*&& !item*/) //在空白处按住中键进行拖动 + { + m_bMousePress = true; + setCursor(Qt::ClosedHandCursor); + m_ptLatstMouse_view = event->pos(); + } + else + QGraphicsView::mousePressEvent(event); +} + +void DesignerView::mouseMoveEvent(QMouseEvent* event) +{ + if(m_bMousePress) + { + QPointF mouseMoveLength = event->pos() - m_ptLatstMouse_view.toPoint(); + translate(mouseMoveLength); + m_ptLatstMouse_view = event->pos(); + } + else + QGraphicsView::mouseMoveEvent(event); +} + +void DesignerView::mouseReleaseEvent(QMouseEvent* event) +{ + if (event->button() == Qt::MiddleButton) //按住中键进行拖动 + { + m_bMousePress = false; + setCursor(Qt::ArrowCursor); + } + 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); +} + +double DesignerView::getScaleFactor() +{ + //QTransform为一个3*3的矩阵,m11和m22元素分别为水平和垂直的缩放值 + return this->transform().m11(); +} + bool DesignerView::zoomLimit(double& value) { - m_dZoomVaule *= value; - if(m_dZoomVaule >= MAX_ZoomValue) + m_dScale *= value; + if(m_dScale >= MAX_ZoomValue) { - m_dZoomVaule = MAX_ZoomValue; - value = m_dZoomVaule / getScaleFactor(); + m_dScale = MAX_ZoomValue; + value = m_dScale / getScaleFactor(); } - else if(m_dZoomVaule <= MIN_ZoomValue) + else if(m_dScale <= MIN_ZoomValue) { - m_dZoomVaule = MIN_ZoomValue; - value = m_dZoomVaule / getScaleFactor(); + m_dScale = MIN_ZoomValue; + value = m_dScale / getScaleFactor(); } if(qFabs(value - 1) < 0.01) //缩放因子近似为1 @@ -73,55 +132,49 @@ void DesignerView::zoom(const QPointF& mousePos, double zoomFactor) setTransformationAnchor(lastAnchor); } -double DesignerView::getScaleFactor() +void DesignerView::zoomIn() { - //QTransform为一个3*3的矩阵,m11和m22元素分别为水平和垂直的缩放值 - return this->transform().m11(); + //以view的中心点作为放大中心 + double dWidth_View = viewport()->width(); + double dHeight_View = viewport()->height(); + QPoint pt(dWidth_View * 0.5, dHeight_View * 0.5); + zoom(pt, 1.1); } -void DesignerView::contextMenuEvent(QContextMenuEvent* event) +void DesignerView::zoomOut() { - Q_UNUSED(event); - m_bMousePress = false; + //以view的中心点作为缩小中心 + double dWidth_View = viewport()->width(); + double dHeight_View = viewport()->height(); + QPoint pt(dWidth_View * 0.5, dHeight_View * 0.5); + zoom(pt, 0.9); } -void DesignerView::mousePressEvent(QMouseEvent* event) +void DesignerView::zoomFit() { - if (event->button() == Qt::MiddleButton) //按住中键进行拖动 - { - m_bMousePress = true; - } - else - QGraphicsView::mousePressEvent(event); + resetTransform(); + + double dWidth_View = viewport()->width(); + double dHeight_View = viewport()->height(); + double dWidth_Scene = scene()->sceneRect().width(); + double dHeight_Scene = scene()->sceneRect().height(); + + double dWidthScale = dWidth_View / dWidth_Scene; + double dHeightScale = dHeight_View / dHeight_Scene; + m_dScale = qMin(dWidthScale, dHeightScale); + + QTransform trans = transform(); + trans.scale(m_dScale, m_dScale); + setTransform(trans); + centerOn(0, 0); } -void DesignerView::mouseMoveEvent(QMouseEvent* event) +void DesignerView::translate(const QPointF& delta) { - 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); + ViewportAnchor lastAnchor = this->transformationAnchor(); + setTransformationAnchor(QGraphicsView::NoAnchor); + QTransform trans = transform(); + trans.translate(delta.x(), delta.y()); + setTransform(trans); + setTransformationAnchor(lastAnchor); } diff --git a/source/drawingPanel.cpp b/source/drawingPanel.cpp index 9a37224..02c20da 100644 --- a/source/drawingPanel.cpp +++ b/source/drawingPanel.cpp @@ -36,6 +36,21 @@ DesignerScene* DrawingPanel::getDesignerScene() return m_pGraphicsScene; } +void DrawingPanel::grahpicsViewZoomIn() +{ + m_pGraphicsView->zoomIn(); +} + +void DrawingPanel::grahpicsViewZoomOut() +{ + m_pGraphicsView->zoomOut(); +} + +void DrawingPanel::grahpicsViewZoomFit() +{ + m_pGraphicsView->zoomFit(); +} + void DrawingPanel::onSignal_addGraphicsItem(GraphicsItemType& itemType) { if(SelectorManager::getInstance()) diff --git a/source/mainwindow.cpp b/source/mainwindow.cpp index 4179d07..1cab47c 100644 --- a/source/mainwindow.cpp +++ b/source/mainwindow.cpp @@ -50,6 +50,12 @@ void CMainWindow::closeEvent(QCloseEvent* event) QMainWindow::closeEvent(event); } +void CMainWindow::changeEvent(QEvent* event) +{ + if (event->type() == QEvent::WindowStateChange) + m_pDrawingPanel->grahpicsViewZoomFit(); +} + void CMainWindow::initializeDockUi() { CDockManager::setConfigFlag(CDockManager::OpaqueSplitterResize, true); @@ -106,6 +112,24 @@ void CMainWindow::initializeAction() ui->actionDelete->setShortcut(QKeySequence::Delete); connect(ui->actionDelete, SIGNAL(triggered()), this, SLOT(onSignal_deleteItem())); + connect(ui->actionZoomIn, SIGNAL(triggered()), this, SLOT(onAction_zoomIn())); + connect(ui->actionZoomOut, SIGNAL(triggered()), this, SLOT(onAction_zoomOut())); + connect(ui->actionZoomFit, SIGNAL(triggered()), this, SLOT(onAction_zoomFit())); +} + +void CMainWindow::onAction_zoomIn() +{ + m_pDrawingPanel->grahpicsViewZoomIn(); +} + +void CMainWindow::onAction_zoomOut() +{ + m_pDrawingPanel->grahpicsViewZoomOut(); +} + +void CMainWindow::onAction_zoomFit() +{ + m_pDrawingPanel->grahpicsViewZoomFit(); } void CMainWindow::onSignal_addItem(QGraphicsItem* item) diff --git a/ui/mainwindow.ui b/ui/mainwindow.ui index e4a0de3..c2f107d 100644 --- a/ui/mainwindow.ui +++ b/ui/mainwindow.ui @@ -6,8 +6,8 @@ 0 0 - 1284 - 756 + 1612 + 1016 @@ -19,7 +19,7 @@ 0 0 - 1284 + 1612 33