From 9753052af50b2b0a805943212f45276d9ec5f809 Mon Sep 17 00:00:00 2001 From: ouv Date: Fri, 5 Jul 2013 15:10:10 +0000 Subject: [PATCH] Interaction flags. --- src/GraphicsView/GraphicsView_ViewPort.cxx | 53 +++++++++++++--------- src/GraphicsView/GraphicsView_ViewPort.h | 33 ++++++++------ src/GraphicsView/GraphicsView_Viewer.cxx | 20 ++------ src/GraphicsView/GraphicsView_Viewer.h | 4 -- 4 files changed, 55 insertions(+), 55 deletions(-) diff --git a/src/GraphicsView/GraphicsView_ViewPort.cxx b/src/GraphicsView/GraphicsView_ViewPort.cxx index 962756819..6eab70e08 100644 --- a/src/GraphicsView/GraphicsView_ViewPort.cxx +++ b/src/GraphicsView/GraphicsView_ViewPort.cxx @@ -120,6 +120,7 @@ void GraphicsView_ViewPort::destroyCursors() //======================================================================= GraphicsView_ViewPort::GraphicsView_ViewPort( QWidget* theParent ) : QGraphicsView( theParent ), + myInteractionFlags( 0 ), myNameLabel( 0 ), myNamePosition( NP_None ), myNameLayout( 0 ), @@ -145,10 +146,12 @@ GraphicsView_ViewPort::GraphicsView_ViewPort( QWidget* theParent ) mySceneGap = 20; myFitAllGap = 40; - myIsTraceBoundingRectEnabled = false; // testing ImageViewer // interaction flags - myInteractionFlags = AllFlags; + setInteractionFlags( EditFlags ); + //setInteractionFlag( TraceBoundingRect ); + //setInteractionFlag( DraggingByMiddleButton ); + //setInteractionFlag( ImmediateSelection ); // background setBackgroundBrush( QBrush( Qt::white ) ); @@ -443,39 +446,43 @@ void GraphicsView_ViewPort::setFitAllGap( double theFitAllGap ) } //================================================================ -// Function : setTraceBoundingRectEnabled +// Function : interactionFlags // Purpose : //================================================================ -void GraphicsView_ViewPort::setTraceBoundingRectEnabled( bool theState ) +int GraphicsView_ViewPort::interactionFlags() const { - myIsTraceBoundingRectEnabled = theState; + return myInteractionFlags; } //================================================================ -// Function : setInteractionFlags +// Function : hasInteractionFlag // Purpose : //================================================================ -void GraphicsView_ViewPort::setInteractionFlags( const int theFlags ) +bool GraphicsView_ViewPort::hasInteractionFlag( InteractionFlag theFlag ) { - myInteractionFlags |= theFlags; + return ( interactionFlags() & theFlag ) == theFlag; } //================================================================ -// Function : clearInteractionFlags +// Function : setInteractionFlag // Purpose : //================================================================ -void GraphicsView_ViewPort::clearInteractionFlags( const int theFlags ) +void GraphicsView_ViewPort::setInteractionFlag( InteractionFlag theFlag, + bool theIsEnabled ) { - myInteractionFlags &= ~theFlags; + if( theIsEnabled ) + setInteractionFlags( myInteractionFlags | theFlag ); + else + setInteractionFlags( myInteractionFlags & ~theFlag ); } //================================================================ -// Function : testInteractionFlags +// Function : setInteractionFlags // Purpose : //================================================================ -bool GraphicsView_ViewPort::testInteractionFlags( const int theFlags ) const +void GraphicsView_ViewPort::setInteractionFlags( InteractionFlags theFlags ) { - return ( myInteractionFlags & theFlags ) == theFlags; + myInteractionFlags = theFlags; } //================================================================ @@ -1321,7 +1328,8 @@ void GraphicsView_ViewPort::dragObjects( QGraphicsSceneMouseEvent* e ) else anObjectsToMove.append( anObject ); } - else if( nbSelected() && ( e->buttons() & Qt::MidButton ) ) + else if( hasInteractionFlag( DraggingByMiddleButton ) && + nbSelected() && ( e->buttons() & Qt::MidButton ) ) { for( initSelected(); moreSelected(); nextSelected() ) if( GraphicsView_Object* aMovingObject = selectedObject() ) @@ -1470,7 +1478,7 @@ bool GraphicsView_ViewPort::cancelCurrentOperation() //================================================================ void GraphicsView_ViewPort::onBoundingRectChanged() { - if( myIsTraceBoundingRectEnabled ) + if( hasInteractionFlag( TraceBoundingRect ) ) { QRectF aRect = objectsBoundingRect(); myScene->setSceneRect( aRect.adjusted( -mySceneGap, -mySceneGap, mySceneGap, mySceneGap ) ); @@ -1499,18 +1507,19 @@ void GraphicsView_ViewPort::onMouseEvent( QGraphicsSceneMouseEvent* e ) { case QEvent::GraphicsSceneMousePress: { - if( testInteractionFlags( EditFlags ) && nbSelected() ) + if( hasInteractionFlag( EditFlags ) && nbSelected() ) for( initSelected(); moreSelected() && !anIsHandled; nextSelected() ) if( GraphicsView_Object* anObject = selectedObject() ) anIsHandled = anObject->handleMousePress( e ); - if( !anIsHandled && testInteractionFlags( Dragging ) ) + if( !anIsHandled && hasInteractionFlag( Dragging ) ) { bool anAccel = e->modifiers() & GraphicsView_ViewTransformer::accelKey(); if( ( getHighlightedObject() && getHighlightedObject()->isMovable() && - !( anAccel || e->button() == Qt::RightButton ) ) || - ( nbSelected() && !anAccel && e->button() == Qt::MidButton ) ) + !( anAccel || e->button() != Qt::LeftButton ) ) || + ( hasInteractionFlag( DraggingByMiddleButton ) && + nbSelected() && !anAccel && e->button() == Qt::MidButton ) ) { myIsDragging = true; myStoredCursor = cursor(); @@ -1521,7 +1530,7 @@ void GraphicsView_ViewPort::onMouseEvent( QGraphicsSceneMouseEvent* e ) } case QEvent::GraphicsSceneMouseMove: { - if( testInteractionFlags( EditFlags ) && nbSelected() ) + if( hasInteractionFlag( EditFlags ) && nbSelected() ) for( initSelected(); moreSelected() && !anIsHandled; nextSelected() ) if( GraphicsView_Object* anObject = selectedObject() ) anIsHandled = anObject->handleMousePress( e ); @@ -1532,7 +1541,7 @@ void GraphicsView_ViewPort::onMouseEvent( QGraphicsSceneMouseEvent* e ) } case QEvent::GraphicsSceneMouseRelease: { - if( testInteractionFlags( EditFlags ) && nbSelected() ) + if( hasInteractionFlag( EditFlags ) && nbSelected() ) for( initSelected(); moreSelected() && !anIsHandled; nextSelected() ) if( GraphicsView_Object* anObject = selectedObject() ) anIsHandled = anObject->handleMousePress( e ); diff --git a/src/GraphicsView/GraphicsView_ViewPort.h b/src/GraphicsView/GraphicsView_ViewPort.h index 75d420e03..7261e4b86 100644 --- a/src/GraphicsView/GraphicsView_ViewPort.h +++ b/src/GraphicsView/GraphicsView_ViewPort.h @@ -46,16 +46,19 @@ class GRAPHICSVIEW_API GraphicsView_ViewPort : public QGraphicsView public: class NameLabel; - enum InteractionFlags + enum InteractionFlag { - Highlighting = 0x0001, // not implemented yet - Selecting = 0x0002, // not implemented yet - Dragging = 0x0004, - Pulling = 0x0008, - WheelScaling = 0x0010, - EditFlags = Dragging | Pulling | WheelScaling, - AllFlags = Highlighting | Selecting | Dragging | Pulling | WheelScaling + // main flags + Dragging = 0x0001, + Pulling = 0x0002, + WheelScaling = 0x0004, + EditFlags = Dragging | Pulling | WheelScaling, + // advanced flags + TraceBoundingRect = 0x0008, + DraggingByMiddleButton = 0x0010, + ImmediateSelection = 0x0020 }; + Q_DECLARE_FLAGS( InteractionFlags, InteractionFlag ) enum BlockStatus { @@ -94,12 +97,13 @@ public: // scene void setSceneGap( double theSceneGap ); void setFitAllGap( double theFitAllGap ); - void setTraceBoundingRectEnabled( bool theState ); // interaction flags - void setInteractionFlags( const int ); - void clearInteractionFlags( const int ); - bool testInteractionFlags( const int ) const; + int interactionFlags() const; + bool hasInteractionFlag( InteractionFlag theFlag ); + void setInteractionFlag( InteractionFlag theFlag, + bool theIsEnabled = true ); + void setInteractionFlags( InteractionFlags theFlags ); // view name void setViewNamePosition( NamePosition thePosition, @@ -241,11 +245,10 @@ private: GraphicsView_Scene* myScene; double mySceneGap; double myFitAllGap; - bool myIsTraceBoundingRectEnabled; GraphicsView_ObjectList myObjects; // interaction flags - int myInteractionFlags; + InteractionFlags myInteractionFlags; // view name NameLabel* myNameLabel; @@ -301,4 +304,6 @@ private: QCursor myStoredCursor; }; +Q_DECLARE_OPERATORS_FOR_FLAGS( GraphicsView_ViewPort::InteractionFlags ) + #endif diff --git a/src/GraphicsView/GraphicsView_Viewer.cxx b/src/GraphicsView/GraphicsView_Viewer.cxx index ddc24dabe..4321fc9bc 100644 --- a/src/GraphicsView/GraphicsView_Viewer.cxx +++ b/src/GraphicsView/GraphicsView_Viewer.cxx @@ -53,8 +53,7 @@ GraphicsView_Viewer::GraphicsView_Viewer( const QString& title ) : SUIT_ViewModel(), mySelector( 0 ), myTransformer( 0 ), - myIsInitialized( false ), - myIsImmediateSelectionEnabled( false ) + myIsInitialized( false ) { } @@ -224,15 +223,6 @@ void GraphicsView_Viewer::setIsInitialized( bool theFlag ) myIsInitialized = theFlag; } -//================================================================ -// Function : setIsImmediateSelectionEnabled -// Purpose : -//================================================================ -void GraphicsView_Viewer::setIsImmediateSelectionEnabled( bool theFlag ) -{ - myIsImmediateSelectionEnabled = theFlag; -} - //================================================================ // Function : createTransformer // Purpose : @@ -385,7 +375,7 @@ void GraphicsView_Viewer::handleMousePress( QGraphicsSceneMouseEvent* e ) if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() ) { if( e->button() == Qt::RightButton && - isImmediateSelectionEnabled() && + aViewPort->hasInteractionFlag( GraphicsView_ViewPort::ImmediateSelection ) && aViewPort->nbSelected() < 1 ) { // If the 'immediate selection' mode is enabled, @@ -394,7 +384,7 @@ void GraphicsView_Viewer::handleMousePress( QGraphicsSceneMouseEvent* e ) getSelector()->select( QRectF(), append ); } else if( e->button() == Qt::LeftButton && - aViewPort->testInteractionFlags( GraphicsView_ViewPort::Pulling ) && + aViewPort->hasInteractionFlag( GraphicsView_ViewPort::Pulling ) && !aViewPort->isSelectByRect() && !aViewPort->isDragging() && aViewPort->startPulling( e->scenePos() ) ) @@ -436,7 +426,7 @@ void GraphicsView_Viewer::handleMouseMove( QGraphicsSceneMouseEvent* e ) aViewPort->drawPulling( e->scenePos() ); } else if( e->button() == Qt::LeftButton && - aViewPort->testInteractionFlags( GraphicsView_ViewPort::Pulling ) && + aViewPort->hasInteractionFlag( GraphicsView_ViewPort::Pulling ) && !aViewPort->isSelectByRect() && !aViewPort->isDragging() && aViewPort->startPulling( e->scenePos() ) ) @@ -500,7 +490,7 @@ void GraphicsView_Viewer::handleWheel( QGraphicsSceneWheelEvent* e ) { if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() ) { - if( aViewPort->testInteractionFlags( GraphicsView_ViewPort::WheelScaling ) ) + if( aViewPort->hasInteractionFlag( GraphicsView_ViewPort::WheelScaling ) ) { bool anIsScaleUp = e->delta() > 0; bool anIsCtrl = e->modifiers() & Qt::ControlModifier; diff --git a/src/GraphicsView/GraphicsView_Viewer.h b/src/GraphicsView/GraphicsView_Viewer.h index 06ef7526a..b5c20d4d0 100644 --- a/src/GraphicsView/GraphicsView_Viewer.h +++ b/src/GraphicsView/GraphicsView_Viewer.h @@ -76,9 +76,6 @@ public: bool isInitialized() const { return myIsInitialized; } void setIsInitialized( bool ); - bool isImmediateSelectionEnabled() const { return myIsImmediateSelectionEnabled; } - void setIsImmediateSelectionEnabled( bool ); - signals: void selectionChanged( GV_SelectionChangeStatus ); void wheelScaleChanged(); @@ -126,7 +123,6 @@ protected: GraphicsView_ViewTransformer* myTransformer; bool myIsInitialized; - bool myIsImmediateSelectionEnabled; }; #endif -- 2.39.2