From 7238b9d273ba451652c0812efb71e9363e9d8ecb Mon Sep 17 00:00:00 2001 From: ouv Date: Mon, 11 Jul 2011 07:51:38 +0000 Subject: [PATCH] Improvement of the highlighting mechanism: list of objects to highlight is sorted by their priority, then by their selection status (selected objects are highlighted first). --- src/GraphicsView/GraphicsView_Object.cxx | 1 + src/GraphicsView/GraphicsView_Object.h | 4 ++ src/GraphicsView/GraphicsView_ViewPort.cxx | 57 +++++++++++++++++++--- src/GraphicsView/GraphicsView_ViewPort.h | 1 + 4 files changed, 57 insertions(+), 6 deletions(-) diff --git a/src/GraphicsView/GraphicsView_Object.cxx b/src/GraphicsView/GraphicsView_Object.cxx index 75523c448..a884e1bbf 100644 --- a/src/GraphicsView/GraphicsView_Object.cxx +++ b/src/GraphicsView/GraphicsView_Object.cxx @@ -30,6 +30,7 @@ //======================================================================= GraphicsView_Object::GraphicsView_Object( QGraphicsItem* theParent ) : QGraphicsItemGroup( theParent ), + myPriority( 0 ), myIsHighlighted( false ), myIsSelected( false ) { diff --git a/src/GraphicsView/GraphicsView_Object.h b/src/GraphicsView/GraphicsView_Object.h index 7cb2a18c1..c1e06cdfb 100644 --- a/src/GraphicsView/GraphicsView_Object.h +++ b/src/GraphicsView/GraphicsView_Object.h @@ -42,6 +42,8 @@ public: const QString& getName() const { return myName; } virtual void setName( const QString& theName ); + virtual int getPriority() const { return myPriority; } + virtual bool isSelectable() const { return true; } virtual bool isMovable() const { return true; } @@ -76,6 +78,8 @@ protected: protected: QString myName; + int myPriority; + bool myIsHighlighted; bool myIsSelected; }; diff --git a/src/GraphicsView/GraphicsView_ViewPort.cxx b/src/GraphicsView/GraphicsView_ViewPort.cxx index f7d995c38..089b3442d 100644 --- a/src/GraphicsView/GraphicsView_ViewPort.cxx +++ b/src/GraphicsView/GraphicsView_ViewPort.cxx @@ -231,6 +231,20 @@ void GraphicsView_ViewPort::cleanup() //================================================================ void GraphicsView_ViewPort::addItem( QGraphicsItem* theItem ) { + if( GraphicsView_Object* anObject = dynamic_cast( theItem ) ) + { + int aPriority = anObject->getPriority(); + GraphicsView_ObjectList::iterator anIter, anIterEnd = myObjects.end(); + for( anIter = myObjects.begin(); anIter != anIterEnd; anIter++ ) + { + if( GraphicsView_Object* anObjectRef = *anIter ) + { + if( anObjectRef->getPriority() > aPriority ) + break; + } + } + myObjects.insert( anIter, anObject ); + } myScene->addItem( theItem ); onBoundingRectChanged(); } @@ -246,6 +260,7 @@ void GraphicsView_ViewPort::removeItem( QGraphicsItem* theItem ) if( myHighlightedObject == anObject ) myHighlightedObject = 0; mySelectedObjects.removeAll( anObject ); + myObjects.removeAll( anObject ); } myScene->removeItem( theItem ); onBoundingRectChanged(); @@ -257,16 +272,45 @@ void GraphicsView_ViewPort::removeItem( QGraphicsItem* theItem ) //================================================================ GraphicsView_ObjectList GraphicsView_ViewPort::getObjects( bool theIsSortSelected ) const { + if( !theIsSortSelected ) + return myObjects; + + // to append selected objects after their non-selected siblings with similar priority + int aCurrentPriority = -1; + GraphicsView_ObjectList aSelectedObjects; + GraphicsView_ObjectList aList; - QListIterator anIter( items() ); + GraphicsView_ObjectListIterator anIter( myObjects ); while( anIter.hasNext() ) - if( GraphicsView_Object* anObject = dynamic_cast( anIter.next() ) ) + { + if( GraphicsView_Object* anObject = anIter.next() ) { - if( theIsSortSelected && anObject->isSelected() ) - aList.prepend( anObject ); // put the selected objects to a head of the list + int aPriority = anObject->getPriority(); + if( aPriority > aCurrentPriority ) + { + if( !aSelectedObjects.isEmpty() ) + { + aList.append( aSelectedObjects ); + aSelectedObjects.clear(); + } + aCurrentPriority = aPriority; + } + + if( anObject->isSelected() ) + aSelectedObjects.append( anObject ); else aList.append( anObject ); } + } + + // for selected objects with highest priority, + // which were not pushed to the result list yet + if( !aSelectedObjects.isEmpty() ) + { + aList.append( aSelectedObjects ); + aSelectedObjects.clear(); + } + return aList; } @@ -781,9 +825,10 @@ void GraphicsView_ViewPort::highlight( double theX, double theY ) GraphicsView_ObjectList aList = getObjects( true ); GraphicsView_ObjectListIterator anIter( aList ); - while( anIter.hasNext() ) + anIter.toBack(); // objects with higher priority have to be checked earlier + while( anIter.hasPrevious() ) { - if( GraphicsView_Object* anObject = anIter.next() ) + if( GraphicsView_Object* anObject = anIter.previous() ) { if( anObject->isVisible() && anObject->isSelectable() ) { diff --git a/src/GraphicsView/GraphicsView_ViewPort.h b/src/GraphicsView/GraphicsView_ViewPort.h index 0cb8921b4..227040e30 100644 --- a/src/GraphicsView/GraphicsView_ViewPort.h +++ b/src/GraphicsView/GraphicsView_ViewPort.h @@ -220,6 +220,7 @@ private: double mySceneGap; double myFitAllGap; bool myIsTraceBoundingRectEnabled; + GraphicsView_ObjectList myObjects; // view name NameLabel* myNameLabel; -- 2.39.2