From: CHEMIN Sebastien Date: Thu, 7 Mar 2024 11:26:39 +0000 (+0100) Subject: Adding fitArea and zoom (by wheel and button) X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=88447d42f8d6454ba1ca3c10d08d0937338310c9;p=modules%2Fgui.git Adding fitArea and zoom (by wheel and button) --- diff --git a/src/GraphicsView/GraphicsView_Scene.cxx b/src/GraphicsView/GraphicsView_Scene.cxx index d521f16e1..267f32b74 100644 --- a/src/GraphicsView/GraphicsView_Scene.cxx +++ b/src/GraphicsView/GraphicsView_Scene.cxx @@ -19,11 +19,6 @@ #include "GraphicsView_Scene.h" -#include -#include -#include -#include -#include "GraphicsView_Object.h" //#define VIEWER_DEBUG //======================================================================= diff --git a/src/GraphicsView/GraphicsView_ViewFrame.cxx b/src/GraphicsView/GraphicsView_ViewFrame.cxx index 2570c048b..974a8a6bc 100644 --- a/src/GraphicsView/GraphicsView_ViewFrame.cxx +++ b/src/GraphicsView/GraphicsView_ViewFrame.cxx @@ -42,7 +42,6 @@ #include #include #include -#include //======================================================================= // Name : GraphicsView_ViewFrame @@ -318,6 +317,7 @@ void GraphicsView_ViewFrame::onViewPan() //================================================================ void GraphicsView_ViewFrame::onViewZoom() { + myViewer->zoom(); // myViewer->activateTransform( GraphicsView_Viewer::Zoom ); } diff --git a/src/GraphicsView/GraphicsView_ViewPort.cxx b/src/GraphicsView/GraphicsView_ViewPort.cxx index d127eda6e..326cd2d8a 100644 --- a/src/GraphicsView/GraphicsView_ViewPort.cxx +++ b/src/GraphicsView/GraphicsView_ViewPort.cxx @@ -22,7 +22,10 @@ #include #include #include -#include +#include + +#include "SUIT_ResourceMgr.h" +#include "SUIT_Session.h" //======================================================================= // Name : GraphicsView_ViewPort @@ -36,6 +39,9 @@ GraphicsView_ViewPort::GraphicsView_ViewPort( QWidget* theParent ) setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); + + setMouseTracking( true ); + setFocusPolicy( Qt::StrongFocus ); myFitAllGap = 40; @@ -43,14 +49,22 @@ GraphicsView_ViewPort::GraphicsView_ViewPort( QWidget* theParent ) myRectBandStart = QPoint(); myRectBandEnd = QPoint(); fittingArea = false; + + zooming = false; + previousPos = QPoint(); + + SUIT_ResourceMgr* rmgr = SUIT_Session::session()->resourceMgr(); + zoomCursor = new QCursor( rmgr->loadPixmap( "GraphicsView", tr( "ICON_GV_CURSOR_ZOOM" ) ) ); } -#include + //======================================================================= // Name : GraphicsView_ViewPort // Purpose : Destructor //======================================================================= GraphicsView_ViewPort::~GraphicsView_ViewPort() { + delete zoomCursor; + zoomCursor = nullptr; } void GraphicsView_ViewPort::addItem( QGraphicsItem* theItem ) @@ -105,7 +119,7 @@ void GraphicsView_ViewPort::fitRect(const QRectF& theRect) fitInView(theRect, Qt::KeepAspectRatio); } -void GraphicsView_ViewPort::startSelectByRect( int x, int y ) +void GraphicsView_ViewPort::startDrawingRect( int x, int y ) { myRectBandStart = QPoint(x,y); @@ -117,17 +131,13 @@ void GraphicsView_ViewPort::startSelectByRect( int x, int y ) } } -void GraphicsView_ViewPort::drawSelectByRect( int x, int y ) +void GraphicsView_ViewPort::drawingRect( int x, int y ) { myRectBandEnd = QPoint(x,y); - - if (myRectBand) - { - myRectBand->setGeometry(QRect(myRectBandStart, myRectBandEnd).normalized()); - } + myRectBand->setGeometry(QRect(myRectBandStart, myRectBandEnd).normalized()); } -void GraphicsView_ViewPort::finishSelectByRect() +void GraphicsView_ViewPort::finishDrawingRect() { myRectBand->hide(); @@ -137,12 +147,11 @@ void GraphicsView_ViewPort::finishSelectByRect() fitInView(selectionRect, Qt::KeepAspectRatio); else { - QList selectedItems = scene()->items(selectionRect, Qt::IntersectsItemShape); - for (QGraphicsItem *item : selectedItems) - item->setSelected(true); - } - - fittingArea = false; + QList selectedItems = scene()->items(selectionRect, Qt::IntersectsItemShape); + for (QGraphicsItem *item : selectedItems) + item->setSelected(true); + } + myRectBandStart = QPoint(); myRectBandEnd = QPoint(); delete myRectBand; @@ -150,10 +159,10 @@ void GraphicsView_ViewPort::finishSelectByRect() } void GraphicsView_ViewPort::mousePressEvent(QMouseEvent *event) -{ - if (items(event->pos()).count()==0) +{ + if (!zooming && items(event->pos()).count()==0) { - startSelectByRect(event->pos().x(), event->pos().y()); + startDrawingRect(event->pos().x(), event->pos().y()); } QGraphicsView::mousePressEvent(event); @@ -162,8 +171,21 @@ void GraphicsView_ViewPort::mousePressEvent(QMouseEvent *event) void GraphicsView_ViewPort::mouseMoveEvent(QMouseEvent *event) -{ - drawSelectByRect(event->pos().x(), event->pos().y()); +{ + if (zooming && (event->buttons() & Qt::LeftButton)) + { + QPoint currentPos = event->pos(); + if (!previousPos.isNull()) + { + int deltaX = currentPos.x() - previousPos.x(); + double scaleFactor = deltaX>0 ? 1.1 : 0.9; + scale(scaleFactor, scaleFactor); + } + previousPos = currentPos; + } + else if (myRectBand) + drawingRect(event->pos().x(), event->pos().y()); + QGraphicsView::mouseMoveEvent(event); emit vpMouseEvent(event); } @@ -171,8 +193,9 @@ void GraphicsView_ViewPort::mouseMoveEvent(QMouseEvent *event) void GraphicsView_ViewPort::mouseReleaseEvent(QMouseEvent *event) { if (myRectBand) - finishSelectByRect(); + finishDrawingRect(); + clearActions(); QGraphicsView::mouseReleaseEvent(event); emit vpMouseEvent(event); } @@ -181,3 +204,37 @@ void GraphicsView_ViewPort::resizeEvent(QResizeEvent *event) { QGraphicsView::resizeEvent(event); } + +void GraphicsView_ViewPort::wheelEvent(QWheelEvent *event) +{ + int delta = event->angleDelta().y(); + double scaleFactor = delta>0 ? 1.1 : 0.9; + scale(scaleFactor, scaleFactor); +} + +void GraphicsView_ViewPort::keyPressEvent(QKeyEvent *event) +{ + if (event->key() == Qt::Key_Escape) + clearActions(); + + QGraphicsView::keyPressEvent(event); +} + +void GraphicsView_ViewPort::clearActions() +{ + setCursor(Qt::ArrowCursor); + fittingArea = false; + zooming = false; +} + +void GraphicsView_ViewPort::activateZoomAction() +{ + zooming = true; + setCursor(*zoomCursor); +} + +void GraphicsView_ViewPort::activateFitAreaAction() +{ + fittingArea = true; + setCursor(Qt::PointingHandCursor); +} diff --git a/src/GraphicsView/GraphicsView_ViewPort.h b/src/GraphicsView/GraphicsView_ViewPort.h index 5b5fe2944..f5bf5a233 100644 --- a/src/GraphicsView/GraphicsView_ViewPort.h +++ b/src/GraphicsView/GraphicsView_ViewPort.h @@ -49,11 +49,13 @@ public: void fitRect(const QRectF& theRect); // rectangle selection - void startSelectByRect( int x, int y ); - void drawSelectByRect( int x, int y ); - void finishSelectByRect(); + void startDrawingRect( int x, int y ); + void drawingRect( int x, int y ); + void finishDrawingRect(); QRect selectionRect(); - void setFitArea(bool isFitting) { fittingArea = isFitting; } + + void activateZoomAction(); + void activateFitAreaAction(); signals: void vpMouseEvent(QMouseEvent*); @@ -68,6 +70,10 @@ protected: virtual void mouseMoveEvent(QMouseEvent *event); virtual void mouseReleaseEvent(QMouseEvent *event); virtual void resizeEvent(QResizeEvent *event); + virtual void wheelEvent(QWheelEvent *event); + virtual void keyPressEvent(QKeyEvent *event); + + void clearActions(); private: @@ -77,6 +83,9 @@ private: QPoint myRectBandEnd; bool fittingArea; + bool zooming; + QPoint previousPos; + QCursor* zoomCursor; }; #endif diff --git a/src/GraphicsView/GraphicsView_Viewer.cxx b/src/GraphicsView/GraphicsView_Viewer.cxx index eaf21ceba..bfb50f147 100644 --- a/src/GraphicsView/GraphicsView_Viewer.cxx +++ b/src/GraphicsView/GraphicsView_Viewer.cxx @@ -38,13 +38,7 @@ #include #include -#include -// testing ImageViewer -/* -#include "GraphicsView_PrsImage.h" -#include "GraphicsView_PrsPropDlg.h" -#include -*/ + //======================================================================= // Name : GraphicsView_Viewer @@ -377,5 +371,11 @@ void GraphicsView_Viewer::fitSelect() void GraphicsView_Viewer::fitArea() { if (GraphicsView_ViewPort* aViewPort = getActiveViewPort()) - aViewPort->setFitArea(true); + aViewPort->activateFitAreaAction(); +} + +void GraphicsView_Viewer::zoom() +{ + if (GraphicsView_ViewPort* aViewPort = getActiveViewPort()) + aViewPort->activateZoomAction(); } diff --git a/src/GraphicsView/GraphicsView_Viewer.h b/src/GraphicsView/GraphicsView_Viewer.h index 6058af49b..b5b53c29c 100644 --- a/src/GraphicsView/GraphicsView_Viewer.h +++ b/src/GraphicsView/GraphicsView_Viewer.h @@ -76,6 +76,7 @@ public: void fitAll(); void fitSelect(); void fitArea(); + void zoom(); bool isInitialized() const { return myIsInitialized; } void setIsInitialized( bool );