From 5babe9ebe23d003c1b7c7351363b8035077f95c5 Mon Sep 17 00:00:00 2001 From: ouv Date: Fri, 5 Jul 2013 10:46:57 +0000 Subject: [PATCH] Escape key cancels the current operation (or clears the current selection). --- src/GraphicsView/GraphicsView_Object.cxx | 9 ++-- src/GraphicsView/GraphicsView_Object.h | 4 +- src/GraphicsView/GraphicsView_PrsImage.cxx | 16 +++--- src/GraphicsView/GraphicsView_PrsImage.h | 2 +- .../GraphicsView_PrsImageFrame.cxx | 9 +++- src/GraphicsView/GraphicsView_PrsImageFrame.h | 2 +- src/GraphicsView/GraphicsView_ViewPort.cxx | 49 +++++++++++++++++-- src/GraphicsView/GraphicsView_ViewPort.h | 6 ++- src/GraphicsView/GraphicsView_Viewer.cxx | 35 ++++++++++--- 9 files changed, 102 insertions(+), 30 deletions(-) diff --git a/src/GraphicsView/GraphicsView_Object.cxx b/src/GraphicsView/GraphicsView_Object.cxx index 648f9d294..a290044d3 100644 --- a/src/GraphicsView/GraphicsView_Object.cxx +++ b/src/GraphicsView/GraphicsView_Object.cxx @@ -168,7 +168,7 @@ void GraphicsView_Object::move( double theDX, double theDY, bool theIsAtOnce ) { if( theIsAtOnce ) { - finishMove(); + finishMove( true ); return; } @@ -180,11 +180,12 @@ void GraphicsView_Object::move( double theDX, double theDY, bool theIsAtOnce ) // Function : finishMove // Purpose : //================================================================ -bool GraphicsView_Object::finishMove() +bool GraphicsView_Object::finishMove( bool theStatus ) { myIsMoving = false; - if( GraphicsView_Scene* aScene = dynamic_cast( scene() ) ) - aScene->processRectChanged(); + if( theStatus ) + if( GraphicsView_Scene* aScene = dynamic_cast( scene() ) ) + aScene->processRectChanged(); return true; } diff --git a/src/GraphicsView/GraphicsView_Object.h b/src/GraphicsView/GraphicsView_Object.h index b55386be6..14810d1e0 100644 --- a/src/GraphicsView/GraphicsView_Object.h +++ b/src/GraphicsView/GraphicsView_Object.h @@ -73,7 +73,7 @@ public: virtual void setSelected( bool theState ) { myIsSelected = theState; } virtual void move( double theDX, double theDY, bool theIsAtOnce = false ); - virtual bool finishMove(); + virtual bool finishMove( bool theStatus ); virtual bool isMoving() const { return myIsMoving; } virtual bool isMovingByXAllowed( double theDX ) { return true; } virtual bool isMovingByYAllowed( double theDY ) { return true; } @@ -86,7 +86,7 @@ public: virtual void pull( const QPointF&, GraphicsView_Object*, const GraphicsView_ObjectList& ) {} - virtual void finishPulling( const GraphicsView_ObjectList& ) {} + virtual void finishPulling( bool, const GraphicsView_ObjectList& ) {} virtual bool isPulling() { return false; } virtual bool handleMousePress( QGraphicsSceneMouseEvent* ) { return false; } diff --git a/src/GraphicsView/GraphicsView_PrsImage.cxx b/src/GraphicsView/GraphicsView_PrsImage.cxx index 45cefcd63..6be39005d 100644 --- a/src/GraphicsView/GraphicsView_PrsImage.cxx +++ b/src/GraphicsView/GraphicsView_PrsImage.cxx @@ -355,7 +355,7 @@ void GraphicsView_PrsImage::move( double theDX, double theDY, bool theIsAtOnce ) { if( theIsAtOnce ) { - finishMove(); + finishMove( true ); return; } @@ -373,17 +373,19 @@ void GraphicsView_PrsImage::move( double theDX, double theDY, bool theIsAtOnce ) // Function : finishMove // Purpose : //================================================================ -bool GraphicsView_PrsImage::finishMove() +bool GraphicsView_PrsImage::finishMove( bool theStatus ) { if( myIsMoving ) { - myPosX = myPreviewPosX; - myPosY = myPreviewPosY; - updateTransform(); - + if( theStatus ) + { + myPosX = myPreviewPosX; + myPosY = myPreviewPosY; + updateTransform(); + } enablePreview( false ); } - return GraphicsView_Object::finishMove(); + return GraphicsView_Object::finishMove( theStatus ); } //================================================================ diff --git a/src/GraphicsView/GraphicsView_PrsImage.h b/src/GraphicsView/GraphicsView_PrsImage.h index b30999ee7..e9ae9887d 100644 --- a/src/GraphicsView/GraphicsView_PrsImage.h +++ b/src/GraphicsView/GraphicsView_PrsImage.h @@ -79,7 +79,7 @@ public: virtual void setSelected( bool theState ); virtual void move( double theDX, double theDY, bool theIsAtOnce = false ); - virtual bool finishMove(); + virtual bool finishMove( bool theStatus ); protected: void processResize( const int theAnchor, diff --git a/src/GraphicsView/GraphicsView_PrsImageFrame.cxx b/src/GraphicsView/GraphicsView_PrsImageFrame.cxx index 14951a3b9..2ffb80911 100644 --- a/src/GraphicsView/GraphicsView_PrsImageFrame.cxx +++ b/src/GraphicsView/GraphicsView_PrsImageFrame.cxx @@ -269,11 +269,18 @@ void GraphicsView_PrsImageFrame::pull( const QPointF& thePoint, // Function : finishPulling // Purpose : //================================================================ -void GraphicsView_PrsImageFrame::finishPulling( const GraphicsView_ObjectList& theSyncObjects ) +void GraphicsView_PrsImageFrame::finishPulling( bool theStatus, + const GraphicsView_ObjectList& theSyncObjects ) { if( !myPrsImage ) return; + if( !theStatus ) + { + myPrsImage->enablePreview( false ); + return; + } + if( myPullingAnchor >= Top && myPullingAnchor <= BottomRight ) myPrsImage->finishResize(); else if( myPullingAnchor == TopMost ) diff --git a/src/GraphicsView/GraphicsView_PrsImageFrame.h b/src/GraphicsView/GraphicsView_PrsImageFrame.h index 350a73a95..e5d42ec72 100644 --- a/src/GraphicsView/GraphicsView_PrsImageFrame.h +++ b/src/GraphicsView/GraphicsView_PrsImageFrame.h @@ -69,7 +69,7 @@ public: virtual void pull( const QPointF&, GraphicsView_Object*, const GraphicsView_ObjectList& ); - virtual void finishPulling( const GraphicsView_ObjectList& ); + virtual void finishPulling( bool, const GraphicsView_ObjectList& ); virtual bool isPulling() { return myIsPulling; } public: diff --git a/src/GraphicsView/GraphicsView_ViewPort.cxx b/src/GraphicsView/GraphicsView_ViewPort.cxx index 5384abef6..962756819 100644 --- a/src/GraphicsView/GraphicsView_ViewPort.cxx +++ b/src/GraphicsView/GraphicsView_ViewPort.cxx @@ -129,6 +129,7 @@ GraphicsView_ViewPort::GraphicsView_ViewPort( QWidget* theParent ) myHighlightedObject( 0 ), myHighlightX( 0 ), myHighlightY( 0 ), + myIsHighlighting( false ), mySelectionIterator( 0 ), myRectBand( 0 ), myAreSelectionPointsInitialized( false ), @@ -932,6 +933,7 @@ GraphicsView_ViewPort::BlockStatus GraphicsView_ViewPort::currentBlock() //================================================================ void GraphicsView_ViewPort::highlight( double theX, double theY ) { + myIsHighlighting = true; myHighlightX = theX; myHighlightY = theY; @@ -1018,6 +1020,9 @@ void GraphicsView_ViewPort::clearHighlighted() //================================================================ int GraphicsView_ViewPort::select( const QRectF& theRect, bool theIsAppend ) { + if( !myIsHighlighting ) + return GVSS_NoChanged; + GV_SelectionStatus aStatus = GVSS_Invalid; if( theRect.isNull() ) // point selection { @@ -1109,7 +1114,7 @@ int GraphicsView_ViewPort::select( const QRectF& theRect, bool theIsAppend ) { bool anIsSelected = false; QRectF aRect = anObject->getRect(); - if( theRect.contains( aRect ) ) + if( theRect.contains( aRect ) && myIsHighlighting ) anIsSelected = anObject->select( myHighlightX, myHighlightY, theRect ); if( anIsSelected && mySelectedObjects.indexOf( anObject ) == -1 ) @@ -1418,13 +1423,47 @@ void GraphicsView_ViewPort::drawPulling( const QPointF& thePoint ) // Function : finishPulling // Purpose : //================================================================ -void GraphicsView_ViewPort::finishPulling() +void GraphicsView_ViewPort::finishPulling( bool theStatus ) { myIsPulling = false; - myPullingObject->finishPulling( getSelectedObjects() ); + myPullingObject->finishPulling( theStatus, getSelectedObjects() ); setCursor( *getDefaultCursor() ); } +//================================================================ +// Function : cancelCurrentOperation +// Purpose : +//================================================================ +bool GraphicsView_ViewPort::cancelCurrentOperation() +{ + myIsHighlighting = false; + + if( isDragging() ) + { + for( initSelected(); moreSelected(); nextSelected() ) + if( GraphicsView_Object* aMovingObject = selectedObject() ) + aMovingObject->finishMove( false ); + + if( GraphicsView_Object* aMovingObject = getHighlightedObject() ) + aMovingObject->finishMove( false ); + + myIsDragging = false; + myDragPosition = QPointF(); + //setCursor( myStoredCursor ); + setCursor( *getDefaultCursor() ); + + return true; + } + + if( isPulling() ) + { + finishPulling( false ); + return true; + } + + return false; +} + //================================================================ // Function : onBoundingRectChanged // Purpose : @@ -1505,10 +1544,10 @@ void GraphicsView_ViewPort::onMouseEvent( QGraphicsSceneMouseEvent* e ) bool anIsMoved = false; for( initSelected(); moreSelected(); nextSelected() ) if( GraphicsView_Object* aMovingObject = selectedObject() ) - anIsMoved = aMovingObject->finishMove() || anIsMoved; + anIsMoved = aMovingObject->finishMove( true ) || anIsMoved; if( GraphicsView_Object* aMovingObject = getHighlightedObject() ) - anIsMoved = aMovingObject->finishMove() || anIsMoved; + anIsMoved = aMovingObject->finishMove( true ) || anIsMoved; myIsDragging = false; myDragPosition = QPointF(); diff --git a/src/GraphicsView/GraphicsView_ViewPort.h b/src/GraphicsView/GraphicsView_ViewPort.h index 6943c8d9c..75d420e03 100644 --- a/src/GraphicsView/GraphicsView_ViewPort.h +++ b/src/GraphicsView/GraphicsView_ViewPort.h @@ -186,9 +186,12 @@ public: // pulling bool startPulling( const QPointF& ); void drawPulling( const QPointF& ); - void finishPulling(); + void finishPulling( bool theStatus ); bool isPulling() const { return myIsPulling; } + // other + bool cancelCurrentOperation(); + public: static void createCursors(); static void destroyCursors(); @@ -273,6 +276,7 @@ private: GraphicsView_Object* myHighlightedObject; double myHighlightX; double myHighlightY; + bool myIsHighlighting; // selection GraphicsView_ObjectList mySelectedObjects; diff --git a/src/GraphicsView/GraphicsView_Viewer.cxx b/src/GraphicsView/GraphicsView_Viewer.cxx index cace48376..ddc24dabe 100644 --- a/src/GraphicsView/GraphicsView_Viewer.cxx +++ b/src/GraphicsView/GraphicsView_Viewer.cxx @@ -334,6 +334,27 @@ void GraphicsView_Viewer::onWheelEvent( QGraphicsSceneWheelEvent* e ) //================================================================ void GraphicsView_Viewer::handleKeyPress( QKeyEvent* e ) { + if( e->key() == Qt::Key_Escape ) + { + // Cancel current operation + bool anIsCancelled = false; + if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() ) + { + anIsCancelled = aViewPort->cancelCurrentOperation(); + + // Unselect all objects (if there is no operation to cancel) + if( !anIsCancelled ) + { + aViewPort->finishSelectByRect(); + aViewPort->clearSelected(); + } + } + + // Emit unselection signal + if( !anIsCancelled ) + if( GraphicsView_Selector* aSelector = getSelector() ) + aSelector->unselectAll(); + } } //================================================================ @@ -455,7 +476,7 @@ void GraphicsView_Viewer::handleMouseRelease( QGraphicsSceneMouseEvent* e ) { if( aViewPort->isPulling() ) { - aViewPort->finishPulling(); + aViewPort->finishPulling( true ); } else if( !aViewPort->getHighlightedObject() ) { @@ -572,15 +593,13 @@ void GraphicsView_Viewer::onRemoveImages() { if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() ) { - for( aViewPort->initSelected(); aViewPort->moreSelected(); aViewPort->nextSelected() ) + GraphicsView_ObjectListIterator anIter( aViewPort->getSelectedObjects() ); + while( anIter.hasNext() ) { - if( GraphicsView_Object* anObject = aViewPort->selectedObject() ) + if( GraphicsView_PrsImage* aPrs = dynamic_cast( anIter.next() ) ) { - if( GraphicsView_PrsImage* aPrs = dynamic_cast( anObject ) ) - { - aViewPort->removeItem( aPrs ); - delete aPrs; - } + aViewPort->removeItem( aPrs ); + delete aPrs; } } } -- 2.39.2