From 4916d16c66554ddc1af1274ec76e03bc0a6584a6 Mon Sep 17 00:00:00 2001 From: CHEMIN Sebastien Date: Wed, 7 Feb 2024 10:08:19 +0100 Subject: [PATCH] Working version --- src/GraphicsView/GraphicsView_Object.cxx | 39 +++++++++++++---- src/GraphicsView/GraphicsView_Object.h | 28 ++++++++----- src/GraphicsView/GraphicsView_Scene.cxx | 46 ++++++++++++++++++--- src/GraphicsView/GraphicsView_ViewFrame.cxx | 2 + src/GraphicsView/GraphicsView_ViewPort.cxx | 28 +++++++++++-- src/GraphicsView/GraphicsView_ViewPort.h | 2 + src/GraphicsView/GraphicsView_Viewer.cxx | 6 ++- 7 files changed, 124 insertions(+), 27 deletions(-) diff --git a/src/GraphicsView/GraphicsView_Object.cxx b/src/GraphicsView/GraphicsView_Object.cxx index a13a7241a..5b5753efc 100644 --- a/src/GraphicsView/GraphicsView_Object.cxx +++ b/src/GraphicsView/GraphicsView_Object.cxx @@ -29,11 +29,11 @@ GraphicsView_Object::GraphicsView_Object( QGraphicsItem* theParent ) : QGraphicsItem( theParent ), myPriority( 0 ), - myIsOnTop( false ), - myIsHighlighted( false ), - myIsSelected( false ), - myIsMoving( false ), - myIsMovable( true ) + myIsOnTop( false ) +// myIsHighlighted( false ), + //myIsSelected( false ), +// myIsMoving( false ), +// myIsMovable( true ) { } @@ -91,7 +91,8 @@ void GraphicsView_Object::setName( const QString& theName ) //================================================================ QRectF GraphicsView_Object::getRect() const { - return sceneBoundingRect(); +// return sceneBoundingRect(); + return boundingRect(); } //================================================================ @@ -109,10 +110,13 @@ bool GraphicsView_Object::checkHighlight( double theX, double theY, QCursor& /*t //================================================================ bool GraphicsView_Object::highlight( double theX, double theY ) { +/* QCursor aCursor; if( (myIsHighlighted = isVisible()) ) myIsHighlighted = checkHighlight( theX, theY, aCursor ); return myIsHighlighted; +*/ + return false; } //================================================================ @@ -121,7 +125,7 @@ bool GraphicsView_Object::highlight( double theX, double theY ) //================================================================ void GraphicsView_Object::unhighlight() { - myIsHighlighted = false; +// myIsHighlighted = false; } //================================================================ @@ -130,6 +134,7 @@ void GraphicsView_Object::unhighlight() //================================================================ bool GraphicsView_Object::select( double theX, double theY, const QRectF& theRect ) { +/* QCursor aCursor; if( (myIsSelected = isVisible()) ) { @@ -139,6 +144,8 @@ bool GraphicsView_Object::select( double theX, double theY, const QRectF& theRec myIsSelected = checkHighlight( theX, theY, aCursor ); } return myIsSelected; +*/ + return false; } //================================================================ @@ -147,7 +154,7 @@ bool GraphicsView_Object::select( double theX, double theY, const QRectF& theRec //================================================================ void GraphicsView_Object::unselect() { - myIsSelected = false; +// myIsSelected = false; } //================================================================ @@ -156,6 +163,7 @@ void GraphicsView_Object::unselect() //================================================================ void GraphicsView_Object::move( double theDX, double theDY, bool theIsAtOnce ) { +/* if( !myIsMovable ) return; @@ -167,6 +175,7 @@ void GraphicsView_Object::move( double theDX, double theDY, bool theIsAtOnce ) myIsMoving = true; moveBy( theDX, theDY ); +*/ } //================================================================ @@ -175,11 +184,14 @@ void GraphicsView_Object::move( double theDX, double theDY, bool theIsAtOnce ) //================================================================ bool GraphicsView_Object::finishMove( bool theStatus ) { +/* myIsMoving = false; if( theStatus ) if( GraphicsView_Scene* aScene = dynamic_cast( scene() ) ) aScene->processRectChanged(); return true; +*/ + return false; } //================================================================ @@ -190,3 +202,14 @@ void GraphicsView_Object::setViewTransform( const QTransform& theTransform ) { myViewTransform = theTransform; } + + +bool GraphicsView_Object::isMovable() const +{ + return (flags() & QGraphicsItem::ItemIsMovable); +} + +void GraphicsView_Object::setMovable(bool theMovable) +{ + setFlag(QGraphicsItem::ItemIsMovable, theMovable); +} diff --git a/src/GraphicsView/GraphicsView_Object.h b/src/GraphicsView/GraphicsView_Object.h index 17d5daef5..913678267 100644 --- a/src/GraphicsView/GraphicsView_Object.h +++ b/src/GraphicsView/GraphicsView_Object.h @@ -55,8 +55,11 @@ public: virtual bool isSelectable() const { return true; } - virtual bool isMovable() const { return myIsMovable; } - virtual void setMovable( bool theMovable ) { myIsMovable = theMovable; } +// virtual bool isMovable() const { return myIsMovable; } +// virtual void setMovable( bool theMovable ) { myIsMovable = theMovable; } + + virtual bool isMovable() const; + virtual void setMovable( bool theMovable ); virtual QRectF getRect() const; @@ -64,16 +67,21 @@ public: virtual bool highlight( double theX, double theY ); virtual void unhighlight(); - virtual bool isHighlighted() const { return myIsHighlighted; } +// virtual bool isHighlighted() const { return myIsHighlighted; } virtual bool select( double theX, double theY, const QRectF& theRect ); virtual void unselect(); - virtual bool isSelected() const { return myIsSelected; } - virtual void setSelected( bool theState ) { myIsSelected = theState; } + +// virtual bool isSelected() const { return myIsSelected; } +// virtual void setSelected( bool theState ) { myIsSelected = theState; } + + virtual void setSelected( bool theState ) { } virtual void move( double theDX, double theDY, bool theIsAtOnce = false ); virtual bool finishMove( bool theStatus ); - virtual bool isMoving() const { return myIsMoving; } + +// virtual bool isMoving() const { return myIsMoving; } + virtual bool isMoving() const { return false; } virtual bool isMovingByXAllowed( double /*theDX*/ ) { return true; } virtual bool isMovingByYAllowed( double /*theDY*/ ) { return true; } @@ -101,11 +109,11 @@ protected: int myPriority; bool myIsOnTop; - bool myIsHighlighted; - bool myIsSelected; +// bool myIsHighlighted; +// bool myIsSelected; - bool myIsMoving; - bool myIsMovable; +// bool myIsMoving; +// bool myIsMovable; QTransform myViewTransform; }; diff --git a/src/GraphicsView/GraphicsView_Scene.cxx b/src/GraphicsView/GraphicsView_Scene.cxx index 8c9727c2d..d0dc1a9cf 100644 --- a/src/GraphicsView/GraphicsView_Scene.cxx +++ b/src/GraphicsView/GraphicsView_Scene.cxx @@ -22,7 +22,8 @@ #include #include #include - +#include +#include "GraphicsView_Object.h" //#define VIEWER_DEBUG //======================================================================= @@ -108,7 +109,33 @@ void GraphicsView_Scene::keyReleaseEvent( QKeyEvent* e ) //================================================================ void GraphicsView_Scene::mousePressEvent( QGraphicsSceneMouseEvent* e ) { - emit gsMouseEvent( 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; + } +*/ + //QGraphicsItem* gitem = mouseGrabberItem(); + GraphicsView_Object* gitem = (GraphicsView_Object*)mouseGrabberItem(); + std::cout << "Grabber : " << mouseGrabberItem() << std::endl; + if (gitem) + std::cout << "Grabber item : " << gitem->getName().toStdString() << std::endl; QGraphicsScene::mousePressEvent( e ); } @@ -128,7 +155,16 @@ void GraphicsView_Scene::mouseMoveEvent( QGraphicsSceneMouseEvent* e ) //================================================================ void GraphicsView_Scene::mouseReleaseEvent( QGraphicsSceneMouseEvent* e ) { - emit gsMouseEvent( e ); +// emit gsMouseEvent( 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; + } + QGraphicsScene::mouseReleaseEvent( e ); } @@ -138,7 +174,7 @@ void GraphicsView_Scene::mouseReleaseEvent( QGraphicsSceneMouseEvent* e ) //================================================================ void GraphicsView_Scene::mouseDoubleClickEvent( QGraphicsSceneMouseEvent* e ) { - emit gsMouseEvent( e ); + // emit gsMouseEvent( e ); QGraphicsScene::mouseDoubleClickEvent( e ); } @@ -163,7 +199,7 @@ void GraphicsView_Scene::wheelEvent( QGraphicsSceneWheelEvent* e ) //================================================================ void GraphicsView_Scene::contextMenuEvent( QGraphicsSceneContextMenuEvent* e ) { - emit gsContextMenuEvent( e ); +// emit gsContextMenuEvent( e ); QGraphicsScene::contextMenuEvent( e ); } diff --git a/src/GraphicsView/GraphicsView_ViewFrame.cxx b/src/GraphicsView/GraphicsView_ViewFrame.cxx index a762bb7f2..292cc1ec1 100644 --- a/src/GraphicsView/GraphicsView_ViewFrame.cxx +++ b/src/GraphicsView/GraphicsView_ViewFrame.cxx @@ -42,6 +42,7 @@ #include #include #include +#include //======================================================================= // Name : GraphicsView_ViewFrame @@ -385,6 +386,7 @@ void GraphicsView_ViewFrame::mouseEvent( QGraphicsSceneMouseEvent* e ) switch ( e->type() ) { case QEvent::GraphicsSceneMousePress: + std::cout << "GraphicsView_ViewFrame::mousePressEvent" << std::endl; emit mousePressed( e ); break; case QEvent::GraphicsSceneMouseMove: diff --git a/src/GraphicsView/GraphicsView_ViewPort.cxx b/src/GraphicsView/GraphicsView_ViewPort.cxx index 2d0cbc40c..aa6396174 100644 --- a/src/GraphicsView/GraphicsView_ViewPort.cxx +++ b/src/GraphicsView/GraphicsView_ViewPort.cxx @@ -38,7 +38,7 @@ #include #include - +#include #define FOREGROUND_Z_VALUE -2 #define GRID_Z_VALUE -1 #define SKETCH_Z_VALUE 3000 @@ -201,8 +201,8 @@ GraphicsView_ViewPort::GraphicsView_ViewPort( QWidget* theParent ) connect( myScene, SIGNAL( gsKeyEvent( QKeyEvent* ) ), this, SLOT( onKeyEvent( QKeyEvent* ) ) ); - connect( myScene, SIGNAL( gsMouseEvent( QGraphicsSceneMouseEvent* ) ), - this, SLOT( onMouseEvent( QGraphicsSceneMouseEvent* ) ) ); +// connect( myScene, SIGNAL( gsMouseEvent( QGraphicsSceneMouseEvent* ) ), +// this, SLOT( onMouseEvent( QGraphicsSceneMouseEvent* ) ) ); connect( myScene, SIGNAL( gsWheelEvent( QGraphicsSceneWheelEvent* ) ), this, SLOT( onWheelEvent( QGraphicsSceneWheelEvent* ) ) ); connect( myScene, SIGNAL( gsContextMenuEvent( QGraphicsSceneContextMenuEvent* ) ), @@ -1289,6 +1289,8 @@ int GraphicsView_ViewPort::select( const QRectF& theRect, bool theIsAppend ) } } return aStatus; + +// return false; } //================================================================ @@ -1789,6 +1791,7 @@ void GraphicsView_ViewPort::onMouseEvent( QGraphicsSceneMouseEvent* e ) { case QEvent::GraphicsSceneMousePress: { + std::cout << "GraphicsView_ViewPort::onMousePressEvent" << std::endl; if( hasInteractionFlag( EditFlags ) && nbSelected() ) for( initSelected(); moreSelected() && !anIsHandled; nextSelected() ) if( GraphicsView_Object* anObject = selectedObject() ) @@ -1895,3 +1898,22 @@ void GraphicsView_ViewPort::scrollContentsBy( int theDX, int theDY ) if( myViewLabel ) myViewLabel->setAcceptMoveEvents( true ); } + +void GraphicsView_ViewPort::mousePressEvent(QMouseEvent *event) +{ +// std::cout << "QGraphicsView::mousePressEvent" << std::endl; + QGraphicsView::mousePressEvent(event); +} + +void GraphicsView_ViewPort::mouseReleaseEvent(QMouseEvent *event) +{ + std::cout << "QGraphicsView::mouseReleaseEvent --> nb selected : " << scene()->selectedItems().count() << std::endl; + + if (!scene()->selectedItems().isEmpty()) + { + GraphicsView_Object* obj = (GraphicsView_Object*) scene()->selectedItems().front(); + std::cout << " Item : " << obj->getName().toStdString() << std::endl; + } + QGraphicsView::mouseReleaseEvent(event); +} + diff --git a/src/GraphicsView/GraphicsView_ViewPort.h b/src/GraphicsView/GraphicsView_ViewPort.h index f6f0b1339..1fdd117bc 100644 --- a/src/GraphicsView/GraphicsView_ViewPort.h +++ b/src/GraphicsView/GraphicsView_ViewPort.h @@ -244,6 +244,8 @@ protected slots: protected: virtual void scrollContentsBy( int theDX, int theDY ); + virtual void mousePressEvent(QMouseEvent *event); + virtual void mouseReleaseEvent(QMouseEvent *event); signals: void vpKeyEvent( QKeyEvent* ); diff --git a/src/GraphicsView/GraphicsView_Viewer.cxx b/src/GraphicsView/GraphicsView_Viewer.cxx index 6ac886e59..5f337ca75 100644 --- a/src/GraphicsView/GraphicsView_Viewer.cxx +++ b/src/GraphicsView/GraphicsView_Viewer.cxx @@ -38,7 +38,7 @@ #include #include - +#include // testing ImageViewer /* #include "GraphicsView_PrsImage.h" @@ -301,9 +301,11 @@ void GraphicsView_Viewer::onKeyEvent( QKeyEvent* e ) //================================================================ void GraphicsView_Viewer::onMouseEvent( QGraphicsSceneMouseEvent* e ) { + switch( e->type() ) { case QEvent::GraphicsSceneMousePress: + std::cout << "GraphicsView_Viewer::onMousePressEvent" << std::endl; handleMousePress( e ); break; case QEvent::GraphicsSceneMouseMove: @@ -374,6 +376,7 @@ void GraphicsView_Viewer::handleKeyRelease( QKeyEvent* /*e*/ ) //!< TODO: unused //================================================================ void GraphicsView_Viewer::handleMousePress( QGraphicsSceneMouseEvent* e ) { + std::cout << "GraphicsView_Viewer::handleMousePress" << std::endl; // test accel for transforms if ( e->modifiers() & GraphicsView_ViewTransformer::accelKey() ) { @@ -483,6 +486,7 @@ void GraphicsView_Viewer::handleMouseMove( QGraphicsSceneMouseEvent* e ) //================================================================ void GraphicsView_Viewer::handleMouseRelease( QGraphicsSceneMouseEvent* e ) { + std::cout << "GraphicsView_Viewer::handleMouseRelease" << std::endl; // selection if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() ) { -- 2.39.2