]> SALOME platform Git repositories - modules/gui.git/commitdiff
Salome HOME
Try to implement resizeEvent and override updateSceneRect
authorCHEMIN Sebastien <sc236498@is245491.intra.cea.fr>
Wed, 28 Feb 2024 08:40:50 +0000 (09:40 +0100)
committerCHEMIN Sebastien <sc236498@is245491.intra.cea.fr>
Wed, 28 Feb 2024 08:40:50 +0000 (09:40 +0100)
src/GraphicsView/GraphicsView_ViewPort.cxx
src/GraphicsView/GraphicsView_ViewPort.h

index e19e2a4d6395868a33124e5e23c400290995e347..4f5b662b89be80b2f68be504522eb535a0f59493 100644 (file)
@@ -35,12 +35,13 @@ GraphicsView_ViewPort::GraphicsView_ViewPort( QWidget* theParent )
 
   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
   setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+  setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
 
   myFitAllGap = 40;
 
   myRectBand = nullptr;
 }
-
+#include<iostream>
 //=======================================================================
 // Name    : GraphicsView_ViewPort
 // Purpose : Destructor
@@ -57,9 +58,21 @@ void GraphicsView_ViewPort::addItem( QGraphicsItem* theItem )
 void GraphicsView_ViewPort::fitAll()
 {
   QRectF aRect = scene()->sceneRect();
+/*
+  QGraphicsRectItem *sceneRectItem = new QGraphicsRectItem(aRect);
+  sceneRectItem->setPen(QPen(QColor("blue"),4));
+  addItem(sceneRectItem);
+*/
   fitInView( aRect.adjusted(-myFitAllGap, -myFitAllGap, myFitAllGap, myFitAllGap), Qt::KeepAspectRatio);
 }
 
+void GraphicsView_ViewPort::updateSceneRect(const QRectF &rect)
+{
+ // std::cout << "Update Scene Rect" << std::endl;
+ // scene()->setSceneRect(rect);
+//  QGraphicsView::updateSceneRect(rect);
+}
+
 void GraphicsView_ViewPort::fitSelect() 
 {
   if (scene()->selectedItems().isEmpty())
@@ -67,16 +80,10 @@ void GraphicsView_ViewPort::fitSelect()
 
   QRectF selectionRect;
   foreach (QGraphicsItem *item, scene()->selectedItems()) 
-    {
-      selectionRect = selectionRect.united(item->sceneBoundingRect());
-    }
+    selectionRect = selectionRect.united(item->sceneBoundingRect());
 
-  if( !selectionRect.isNull() )
-    {
-//      double aGap = qMax( selectionRect.width(), selectionRect.height() ) / 5;
-//      selectionRect.adjust( -aGap, -aGap, aGap, aGap );
-      fitInView( selectionRect, Qt::KeepAspectRatio );
-    }
+  if(!selectionRect.isNull())
+    fitInView(selectionRect, Qt::KeepAspectRatio);
 }
 
 void GraphicsView_ViewPort::fitRect(const QRectF& theRect)
@@ -94,23 +101,6 @@ void GraphicsView_ViewPort::startSelectByRect( int x, int y )
       myRectBand->setGeometry(QRect(myRectBandStart, QSize()));
       myRectBand->show();
     }
-/*
-  if( !myAreSelectionPointsInitialized )
-  {
-    myFirstSelectionPoint = QPoint( x, y );
-    myLastSelectionPoint = QPoint( x, y );
-    myAreSelectionPointsInitialized = true;
-  }
-
-  if( !myRectBand )
-  {
-    myRectBand = new QRubberBand( QRubberBand::Rectangle, this );
-    QPalette palette;
-    palette.setColor( myRectBand->foregroundRole(), Qt::white );
-    myRectBand->setPalette( palette );
-  }
-  myRectBand->hide();
-*/
 }
 
 void GraphicsView_ViewPort::drawSelectByRect( int x, int y )
@@ -124,10 +114,12 @@ void GraphicsView_ViewPort::finishSelectByRect()
 {
   myRectBand->hide();
   QRectF selectionRect = mapToScene(myRectBand->geometry()).boundingRect();
+  fitInView(selectionRect, Qt::KeepAspectRatio);
+/*
   QList<QGraphicsItem *> selectedItems = scene()->items(selectionRect, Qt::IntersectsItemShape);
   for (QGraphicsItem *item : selectedItems) 
     item->setSelected(true);
-
+*/
   delete myRectBand;
   myRectBand = nullptr;
 
@@ -157,4 +149,65 @@ void GraphicsView_ViewPort::mouseReleaseEvent(QMouseEvent *event)
   QGraphicsView::mouseReleaseEvent(event);
 }
 
+void GraphicsView_ViewPort::resizeEvent(QResizeEvent *event)
+{
+   QGraphicsView::resizeEvent(event);
+
+/*
+    // Get the current visible scene rectangle
+    QRectF visibleSceneRect = mapToScene(viewport()->rect()).boundingRect();
+
+    // Calculate the scaling factors for both width and height
+    qreal widthScaleFactor = event->size().width() / event->oldSize().width();
+    qreal heightScaleFactor = event->size().height() / event->oldSize().height();
+
+    // Apply the scaling factors to the visible scene rectangle
+    visibleSceneRect.setWidth(visibleSceneRect.width() * widthScaleFactor);
+    visibleSceneRect.setHeight(visibleSceneRect.height() * heightScaleFactor);
+
+    // Map the adjusted visible scene rectangle back to viewport coordinates
+    QRectF viewportRect = mapFromScene(visibleSceneRect).boundingRect();
+
+    // Ensure that the viewport rectangle stays within the scene boundaries
+
+    if (viewportRect.width() > sceneRect().width()) {
+        viewportRect.setWidth(sceneRect().width());
+    }
+    if (viewportRect.height() > sceneRect().height()) {
+        viewportRect.setHeight(sceneRect().height());
+    }
+
+    // Update the viewport
+    fitInView(, Qt::KeepAspectRatio);
+*/
+}
+void GraphicsView_ViewPort::fitWidth()
+{
+  double aGap = myFitAllGap;
+
+  QRectF aRect;
+  foreach (QGraphicsItem *item, scene()->selectedItems()) 
+    aRect = aRect.united(item->sceneBoundingRect());
+
+  QRectF aRect1 = aRect;
+
+  double aTop = aRect1.top();
+  double aLeft = aRect1.left();
+  double w = aRect1.width();
+  double h = aRect1.height();
+
+  double aMargin = 10;
 
+  aRect.setY( aRect.center().y() );
+  aRect.setX( aRect.center().x() );
+  aRect.setHeight( aMargin );
+  aRect.setWidth( aMargin );
+
+
+  addItem(new QGraphicsRectItem(aRect));
+
+  fitInView( aRect.adjusted( -aGap, -aGap, aGap, aGap ), Qt::KeepAspectRatio );
+  ensureVisible( aLeft, aTop, aMargin, aMargin, 0, aGap );
+//  ensureVisible(aRect1);
+
+}
index 8e2866d28554472a385346c188ff2588c0a4ede0..4929f4fada77c61d5043f7a78b07742950679aeb 100644 (file)
@@ -47,17 +47,22 @@ public:
   void fitAll();
   void fitSelect();
   void fitRect(const QRectF& theRect);
+  void fitWidth();
 
   // rectangle selection
   void startSelectByRect( int x, int y );
   void drawSelectByRect( int x, int y );
   void finishSelectByRect();
 
+public slots:
+   virtual void updateSceneRect(const QRectF &rect);
+
 protected:
 
   virtual void mousePressEvent(QMouseEvent *event);
   virtual void mouseMoveEvent(QMouseEvent *event);
   virtual void mouseReleaseEvent(QMouseEvent *event);
+  virtual void resizeEvent(QResizeEvent *event);
  
 private: