From: CHEMIN Sebastien Date: Wed, 28 Feb 2024 08:40:50 +0000 (+0100) Subject: Try to implement resizeEvent and override updateSceneRect X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=720f8262b102cbab9e7f513a016967dfe3962d8a;p=modules%2Fgui.git Try to implement resizeEvent and override updateSceneRect --- diff --git a/src/GraphicsView/GraphicsView_ViewPort.cxx b/src/GraphicsView/GraphicsView_ViewPort.cxx index e19e2a4d6..4f5b662b8 100644 --- a/src/GraphicsView/GraphicsView_ViewPort.cxx +++ b/src/GraphicsView/GraphicsView_ViewPort.cxx @@ -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 //======================================================================= // 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 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); + +} diff --git a/src/GraphicsView/GraphicsView_ViewPort.h b/src/GraphicsView/GraphicsView_ViewPort.h index 8e2866d28..4929f4fad 100644 --- a/src/GraphicsView/GraphicsView_ViewPort.h +++ b/src/GraphicsView/GraphicsView_ViewPort.h @@ -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: