]> SALOME platform Git repositories - modules/gui.git/commitdiff
Salome HOME
Adding fitArea and zoom (by wheel and button)
authorCHEMIN Sebastien <sc236498@is245491.intra.cea.fr>
Thu, 7 Mar 2024 11:26:39 +0000 (12:26 +0100)
committerCHEMIN Sebastien <sc236498@is245491.intra.cea.fr>
Thu, 7 Mar 2024 11:26:39 +0000 (12:26 +0100)
src/GraphicsView/GraphicsView_Scene.cxx
src/GraphicsView/GraphicsView_ViewFrame.cxx
src/GraphicsView/GraphicsView_ViewPort.cxx
src/GraphicsView/GraphicsView_ViewPort.h
src/GraphicsView/GraphicsView_Viewer.cxx
src/GraphicsView/GraphicsView_Viewer.h

index d521f16e14b64ae5353f7325edda06c6293f08c1..267f32b746380a8c39026ddc6ae984b91da12d01 100644 (file)
 
 #include "GraphicsView_Scene.h"
 
-#include <QGraphicsLineItem>
-#include <QGraphicsRectItem>
-#include <QGraphicsSceneWheelEvent>
-#include <iostream>
-#include "GraphicsView_Object.h"
 //#define VIEWER_DEBUG
 
 //=======================================================================
index 2570c048b1a1d469abb34738ce17ed251d9951eb..974a8a6bcc33cede3f521e0915766583c20fb240 100644 (file)
@@ -42,7 +42,6 @@
 #include <QMouseEvent>
 #include <QString>
 #include <QWheelEvent>
-#include <iostream>
 
 //=======================================================================
 // Name    : GraphicsView_ViewFrame
@@ -318,6 +317,7 @@ void GraphicsView_ViewFrame::onViewPan()
 //================================================================
 void GraphicsView_ViewFrame::onViewZoom()
 {
+  myViewer->zoom();
 //  myViewer->activateTransform( GraphicsView_Viewer::Zoom );
 }
 
index d127eda6e9303c97cc23a85f71073bce3de88ccf..326cd2d8aa4e2e9f79b66b2c9b0273a91fbe5060 100644 (file)
 #include <QGraphicsItem>
 #include <QRubberBand>
 #include <QMouseEvent>
-#include <iostream>
+#include <QCursor>
+
+#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<iostream>
+
 //=======================================================================
 // 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<QGraphicsItem *> selectedItems = scene()->items(selectionRect, Qt::IntersectsItemShape);
-  for (QGraphicsItem *item : selectedItems) 
-    item->setSelected(true);
-   }
-
-  fittingArea = false;
+      QList<QGraphicsItem *> 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);
+}
index 5b5fe29442a8596e20ab2b4caf9c505f5c8c1137..f5bf5a233f1090ea6aad021adf15593849cf0522 100644 (file)
@@ -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
index eaf21cebaec5cf6bd4d699a3ff95435910f1be28..bfb50f147a0690e36360c8743e5312e22802f815 100644 (file)
 #include <QMenu>
 
 #include <math.h>
-#include <iostream>
-// testing ImageViewer
-/*
-#include "GraphicsView_PrsImage.h"
-#include "GraphicsView_PrsPropDlg.h"
-#include <QFileDialog>
-*/
+
 
 //=======================================================================
 // 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();
 }
index 6058af49bd12bd9fd7602e1ccf57bec6c1cc37fa..b5b53c29cdd4f5aa7217b6930b6445efa6ebc172 100644 (file)
@@ -76,6 +76,7 @@ public:
   void fitAll();
   void fitSelect();
   void fitArea();
+  void zoom();
 
   bool                          isInitialized() const { return myIsInitialized; }
   void                          setIsInitialized( bool );