From: CHEMIN Sebastien Date: Mon, 12 Feb 2024 11:58:43 +0000 (+0100) Subject: fitAll + fitSelected + selectByRect X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=6f349cb9e81e417ea207ecea5328a7e8a9d15e58;p=modules%2Fgui.git fitAll + fitSelected + selectByRect --- diff --git a/src/GraphicsView/GraphicsView_Scene.cxx b/src/GraphicsView/GraphicsView_Scene.cxx index dbf869f76..d521f16e1 100644 --- a/src/GraphicsView/GraphicsView_Scene.cxx +++ b/src/GraphicsView/GraphicsView_Scene.cxx @@ -33,25 +33,6 @@ GraphicsView_Scene::GraphicsView_Scene( QObject* theParent ) : QGraphicsScene( theParent ) { -#ifdef VIEWER_DEBUG - mySceneRectItem = new QGraphicsRectItem(); - mySceneRectItem->setPen( QPen( Qt::red, 0.0 ) ); - - addItem( mySceneRectItem ); - - connect( this, SIGNAL( sceneRectChanged( const QRectF& ) ), - this, SLOT( onSceneRectChanged( const QRectF& ) ) ); - - setSceneRect( -2000, -2000, 4000, 4000 ); - - QGraphicsLineItem* aHorLineItem = new QGraphicsLineItem( -2000, 0, 2000, 0 ); - aHorLineItem->setPen( QPen( Qt::red ) ); - addItem( aHorLineItem ); - - QGraphicsLineItem* aVerLineItem = new QGraphicsLineItem( 0, -2000, 0, 2000 ); - aVerLineItem->setPen( QPen( Qt::red ) ); - addItem( aVerLineItem ); -#endif } //======================================================================= @@ -62,185 +43,3 @@ GraphicsView_Scene::~GraphicsView_Scene() { } -//================================================================ -// Function : processRectChanged -// Purpose : -//================================================================ -void GraphicsView_Scene::processRectChanged() -{ - emit gsBoundingRectChanged(); -} - -//================================================================ -// Function : onSceneRectChanged -// Purpose : -//================================================================ -void GraphicsView_Scene::onSceneRectChanged( const QRectF& theRect ) -{ - (void)theRect; // unused in debug mode -#ifdef VIEWER_DEBUG - mySceneRectItem->setRect( theRect ); -#endif -} - -//================================================================ -// Function : keyPressEvent -// Purpose : -//================================================================ -void GraphicsView_Scene::keyPressEvent( QKeyEvent* e ) -{ - emit gsKeyEvent( e ); - QGraphicsScene::keyPressEvent( e ); -} - -//================================================================ -// Function : keyReleaseEvent -// Purpose : -//================================================================ -void GraphicsView_Scene::keyReleaseEvent( QKeyEvent* e ) -{ - emit gsKeyEvent( e ); - QGraphicsScene::keyReleaseEvent( e ); -} - -//================================================================ -// Function : mousePressEvent -// Purpose : -//================================================================ -void GraphicsView_Scene::mousePressEvent( QGraphicsSceneMouseEvent* e ) -{ -/* - std::cout << "============================================================" << std::endl; - std::cout << "QGraphicsScene::mousePressEvent --> nb selected : " << selectedItems().count() << std::endl; - - if (!selectedItems().isEmpty()) - { - GraphicsView_Object* obj = (GraphicsView_Object*) selectedItems().front(); - std::cout << " Item : " << obj->getName().toStdString() << std::endl; - } - -// emit gsMouseEvent( e ); -*/ -/* - QList items1 = items(e->scenePos()); - QList::const_iterator it; - - for (it = items1.constBegin(); it != items1.constEnd(); ++it) - { - GraphicsView_Object* anItem = (GraphicsView_Object*)(*it); - std::cout << "Item at pos : " << anItem->getName().toStdString() << std::endl; - } -*/ - QGraphicsScene::mousePressEvent( e ); -} - -//================================================================ -// Function : mouseMoveEvent -// Purpose : -//================================================================ -void GraphicsView_Scene::mouseMoveEvent( QGraphicsSceneMouseEvent* e ) -{ - emit gsMouseEvent( e ); - QGraphicsScene::mouseMoveEvent( e ); -} - -//================================================================ -// Function : mouseReleaseEvent -// Purpose : -//================================================================ -void GraphicsView_Scene::mouseReleaseEvent( QGraphicsSceneMouseEvent* e ) -{ -// emit gsMouseEvent( e ); - - QGraphicsScene::mouseReleaseEvent( e ); - std::cout << "QGraphicsScene::mouseReleaseEvent --> nb selected : " << selectedItems().count() << std::endl; -/* - if (!selectedItems().isEmpty()) - { - GraphicsView_Object* obj = (GraphicsView_Object*) selectedItems().front(); - std::cout << " Item : " << obj->getName().toStdString() << std::endl; - } - - - QList items1 = items(e->scenePos()); - QList::const_iterator it; - - for (it = selectedItems().constBegin(); it != selectedItems().constEnd(); ++it) - { - GraphicsView_Object* anItem = (GraphicsView_Object*)(*it); - std::cout << "Item selected : " << anItem->getName().toStdString() << std::endl; - } - -*/ -} - -//================================================================ -// Function : mouseDoubleClickEvent -// Purpose : -//================================================================ -void GraphicsView_Scene::mouseDoubleClickEvent( QGraphicsSceneMouseEvent* e ) -{ - // emit gsMouseEvent( e ); - QGraphicsScene::mouseDoubleClickEvent( e ); -} - -//================================================================ -// Function : wheelEvent -// Purpose : -//================================================================ -void GraphicsView_Scene::wheelEvent( QGraphicsSceneWheelEvent* e ) -{ - emit gsWheelEvent( e ); - - // accept the event to prevent calling QAbstractScrollArea::wheelEvent() - // from QGraphicsView::wheelEvent(), which will change values of scroll-bars - e->accept(); - - //QGraphicsScene::wheelEvent( e ); // don't uncomment -} - -//================================================================ -// Function : contextMenuEvent -// Purpose : -//================================================================ -void GraphicsView_Scene::contextMenuEvent( QGraphicsSceneContextMenuEvent* e ) -{ -// emit gsContextMenuEvent( e ); - QGraphicsScene::contextMenuEvent( e ); -} - -//================================================================ -// Function : dragEnterEvent -// Purpose : -//================================================================ -void GraphicsView_Scene::dragEnterEvent( QGraphicsSceneDragDropEvent* /*e*/ ) //!< TODO: unused variable -{ - //QGraphicsScene::dragEnterEvent( e ); // don't uncomment -} - -//================================================================ -// Function : dragLeaveEvent -// Purpose : -//================================================================ -void GraphicsView_Scene::dragLeaveEvent( QGraphicsSceneDragDropEvent* /*e*/ ) //!< TODO: unused variable -{ - //QGraphicsScene::dragLeaveEvent( e ); // don't uncomment -} - -//================================================================ -// Function : dragMoveEvent -// Purpose : -//================================================================ -void GraphicsView_Scene::dragMoveEvent( QGraphicsSceneDragDropEvent* /*e*/ ) //!< TODO: unused variable -{ - //QGraphicsScene::dragMoveEvent( e ); // don't uncomment -} - -//================================================================ -// Function : dropEvent -// Purpose : -//================================================================ -void GraphicsView_Scene::dropEvent( QGraphicsSceneDragDropEvent* /*e*/ ) //!< TODO: unused variable -{ - //QGraphicsScene::dropEvent( e ); // don't uncomment -} diff --git a/src/GraphicsView/GraphicsView_Scene.h b/src/GraphicsView/GraphicsView_Scene.h index f0c6b7a47..269bce584 100644 --- a/src/GraphicsView/GraphicsView_Scene.h +++ b/src/GraphicsView/GraphicsView_Scene.h @@ -37,7 +37,7 @@ class GRAPHICSVIEW_API GraphicsView_Scene : public QGraphicsScene public: GraphicsView_Scene( QObject* theParent = 0 ); ~GraphicsView_Scene(); - +/* public: void processRectChanged(); @@ -70,6 +70,7 @@ signals: private: QGraphicsRectItem* mySceneRectItem; // for debug +*/ }; #endif diff --git a/src/GraphicsView/GraphicsView_ViewPort.cxx b/src/GraphicsView/GraphicsView_ViewPort.cxx index 74a63f5fa..e19e2a4d6 100644 --- a/src/GraphicsView/GraphicsView_ViewPort.cxx +++ b/src/GraphicsView/GraphicsView_ViewPort.cxx @@ -19,6 +19,9 @@ #include "GraphicsView_ViewPort.h" #include "GraphicsView_Scene.h" +#include +#include +#include #include //======================================================================= @@ -29,6 +32,13 @@ GraphicsView_ViewPort::GraphicsView_ViewPort( QWidget* theParent ) : QGraphicsView( theParent ) { setScene(new GraphicsView_Scene(this)); + + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + + myFitAllGap = 40; + + myRectBand = nullptr; } //======================================================================= @@ -44,3 +54,107 @@ void GraphicsView_ViewPort::addItem( QGraphicsItem* theItem ) scene()->addItem(theItem); } +void GraphicsView_ViewPort::fitAll() +{ + QRectF aRect = scene()->sceneRect(); + fitInView( aRect.adjusted(-myFitAllGap, -myFitAllGap, myFitAllGap, myFitAllGap), Qt::KeepAspectRatio); +} + +void GraphicsView_ViewPort::fitSelect() +{ + if (scene()->selectedItems().isEmpty()) + return; + + QRectF selectionRect; + foreach (QGraphicsItem *item, scene()->selectedItems()) + { + 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 ); + } +} + +void GraphicsView_ViewPort::fitRect(const QRectF& theRect) +{ + fitInView(theRect, Qt::KeepAspectRatio); +} + +void GraphicsView_ViewPort::startSelectByRect( int x, int y ) +{ + myRectBandStart = QPoint(x,y); + + if (!myRectBand) + { + myRectBand = new QRubberBand(QRubberBand::Rectangle, this); + 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 ) +{ + QPoint endPoint = QPoint(x,y); + if (myRectBand) + myRectBand->setGeometry(QRect(myRectBandStart, endPoint).normalized()); +} + +void GraphicsView_ViewPort::finishSelectByRect() +{ + myRectBand->hide(); + QRectF selectionRect = mapToScene(myRectBand->geometry()).boundingRect(); + QList selectedItems = scene()->items(selectionRect, Qt::IntersectsItemShape); + for (QGraphicsItem *item : selectedItems) + item->setSelected(true); + + delete myRectBand; + myRectBand = nullptr; + +} + +void GraphicsView_ViewPort::mousePressEvent(QMouseEvent *event) +{ + if (items(event->pos()).count()==0) + { + startSelectByRect(event->pos().x(), event->pos().y()); + } + + QGraphicsView::mousePressEvent(event); +} + + +void GraphicsView_ViewPort::mouseMoveEvent(QMouseEvent *event) +{ + drawSelectByRect(event->pos().x(), event->pos().y()); + QGraphicsView::mouseMoveEvent(event); +} + +void GraphicsView_ViewPort::mouseReleaseEvent(QMouseEvent *event) +{ + if (myRectBand) + finishSelectByRect(); + QGraphicsView::mouseReleaseEvent(event); +} + + diff --git a/src/GraphicsView/GraphicsView_ViewPort.h b/src/GraphicsView/GraphicsView_ViewPort.h index 80c077158..8e2866d28 100644 --- a/src/GraphicsView/GraphicsView_ViewPort.h +++ b/src/GraphicsView/GraphicsView_ViewPort.h @@ -27,11 +27,8 @@ #include #include -class QGridLayout; -class QRubberBand; -class GraphicsView_Object; -class GraphicsView_Scene; +class QRubberBand; /* Class : GraphicsView_ViewPort @@ -46,9 +43,27 @@ public: ~GraphicsView_ViewPort(); void addItem( QGraphicsItem* theItem ); + + void fitAll(); + void fitSelect(); + void fitRect(const QRectF& theRect); + + // rectangle selection + void startSelectByRect( int x, int y ); + void drawSelectByRect( int x, int y ); + void finishSelectByRect(); + +protected: + + virtual void mousePressEvent(QMouseEvent *event); + virtual void mouseMoveEvent(QMouseEvent *event); + virtual void mouseReleaseEvent(QMouseEvent *event); private: + double myFitAllGap; + QRubberBand* myRectBand; + QPoint myRectBandStart; }; #endif diff --git a/src/GraphicsView/GraphicsView_ViewTransformer.cxx b/src/GraphicsView/GraphicsView_ViewTransformer.cxx index a3f11fc6d..4a4746e23 100644 --- a/src/GraphicsView/GraphicsView_ViewTransformer.cxx +++ b/src/GraphicsView/GraphicsView_ViewTransformer.cxx @@ -101,13 +101,14 @@ void GraphicsView_ViewTransformer::initTransform( bool init ) //================================================================ void GraphicsView_ViewTransformer::exec() { -/* + GraphicsView_ViewPort* aViewPort = myViewer->getActiveViewPort(); if( !aViewPort ) return; switch( myType ) { +/* case GraphicsView_Viewer::Zoom: myMajorBtn = zoomButton(); aViewPort->setCursor( *aViewPort->getZoomCursor() ); @@ -129,21 +130,25 @@ void GraphicsView_ViewTransformer::exec() aViewPort->reset(); onTransform( Finished ); break; +*/ case GraphicsView_Viewer::FitAll: aViewPort->fitAll(); onTransform( Finished ); break; + case GraphicsView_Viewer::FitSelect: aViewPort->fitSelect(); onTransform( Finished ); break; +/* case GraphicsView_Viewer::FitWidth: aViewPort->fitWidth(); onTransform( Finished ); break; +*/ default: break; } -*/ + } //================================================================