From 677e8367763ad6f2e08e9c0d4799b7ad7ef389ac Mon Sep 17 00:00:00 2001 From: asl Date: Thu, 7 Aug 2014 08:04:14 +0000 Subject: [PATCH] #415: tree synchronization --- src/HYDROGUI/CMakeLists.txt | 2 + src/HYDROGUI/HYDROGUI_DataModel.cxx | 54 +++++++------- src/HYDROGUI/HYDROGUI_DataModelSync.cxx | 96 +++++++++++++++++++++++++ src/HYDROGUI/HYDROGUI_DataModelSync.h | 30 ++++++++ src/HYDROGUI/HYDROGUI_DataObject.cxx | 33 +++++++++ src/HYDROGUI/HYDROGUI_DataObject.h | 4 ++ 6 files changed, 191 insertions(+), 28 deletions(-) create mode 100644 src/HYDROGUI/HYDROGUI_DataModelSync.cxx create mode 100644 src/HYDROGUI/HYDROGUI_DataModelSync.h diff --git a/src/HYDROGUI/CMakeLists.txt b/src/HYDROGUI/CMakeLists.txt index 79549b29..212a5153 100644 --- a/src/HYDROGUI/CMakeLists.txt +++ b/src/HYDROGUI/CMakeLists.txt @@ -14,6 +14,7 @@ set(PROJECT_HEADERS HYDROGUI_CopyPastePositionOp.h HYDROGUI_DataBrowser.h HYDROGUI_DataModel.h + HYDROGUI_DataModelSync.h HYDROGUI_DataObject.h HYDROGUI_DeleteDlg.h HYDROGUI_DeleteOp.h @@ -108,6 +109,7 @@ set(PROJECT_SOURCES HYDROGUI_CopyPastePositionOp.cxx HYDROGUI_DataBrowser.cxx HYDROGUI_DataModel.cxx + HYDROGUI_DataModelSync.cxx HYDROGUI_DataObject.cxx HYDROGUI_DeleteDlg.cxx HYDROGUI_DeleteOp.cxx diff --git a/src/HYDROGUI/HYDROGUI_DataModel.cxx b/src/HYDROGUI/HYDROGUI_DataModel.cxx index 654451d8..cb445127 100644 --- a/src/HYDROGUI/HYDROGUI_DataModel.cxx +++ b/src/HYDROGUI/HYDROGUI_DataModel.cxx @@ -30,6 +30,7 @@ #include #include +#include #include #include #include @@ -61,6 +62,7 @@ #include #include #include +#include #include @@ -219,19 +221,8 @@ void HYDROGUI_DataModel::update( const int theStudyId ) if( !aStudyRoot ) return; - // create root object if not exist - CAM_DataObject* aRootObj = root(); - if( !aRootObj ) - aRootObj = createRootModuleObject( aStudyRoot ); - - if( !aRootObj ) - return; - - DataObjectList aList; - aRootObj->children( aList ); - QListIterator anIter( aList ); - while( anIter.hasNext() ) - removeChild( aRootObj, anIter.next() ); + // create a new root object + CAM_DataObject* aNewRootObj = new CAM_DataObject(); Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theStudyId ); if( aDocument.IsNull() ) @@ -240,34 +231,34 @@ void HYDROGUI_DataModel::update( const int theStudyId ) // Create root objects: // IMAGES - LightApp_DataObject* anImageRootObj = createObject( aRootObj, tr( partitionName( KIND_IMAGE ).toAscii() ) ); + LightApp_DataObject* anImageRootObj = createObject( aNewRootObj, tr( partitionName( KIND_IMAGE ).toAscii() ) ); // BATHYMETRY - LightApp_DataObject* aBathymetryRootObj = createObject( aRootObj, tr( partitionName( KIND_BATHYMETRY ).toAscii() ) ); + LightApp_DataObject* aBathymetryRootObj = createObject( aNewRootObj, tr( partitionName( KIND_BATHYMETRY ).toAscii() ) ); // ARTIFICIAL OBJECTS - LightApp_DataObject* anArtificialObjectsRootObj = createObject( aRootObj, tr( partitionName( KIND_ARTIFICIAL_OBJECT ).toAscii() ) ); + LightApp_DataObject* anArtificialObjectsRootObj = createObject( aNewRootObj, tr( partitionName( KIND_ARTIFICIAL_OBJECT ).toAscii() ) ); // NATURAL OBJECTS - LightApp_DataObject* aNaturalObjectsRootObj = createObject( aRootObj, tr( partitionName( KIND_NATURAL_OBJECT ).toAscii() ) ); + LightApp_DataObject* aNaturalObjectsRootObj = createObject( aNewRootObj, tr( partitionName( KIND_NATURAL_OBJECT ).toAscii() ) ); // OBSTACLES - LightApp_DataObject* anObstaclesRootObj = createObject( aRootObj, tr( partitionName( KIND_OBSTACLE ).toAscii() ) ); + LightApp_DataObject* anObstaclesRootObj = createObject( aNewRootObj, tr( partitionName( KIND_OBSTACLE ).toAscii() ) ); // CALCULATION CASES - LightApp_DataObject* aCalculRootObj = createObject( aRootObj, tr( partitionName( KIND_CALCULATION ).toAscii() ) ); + LightApp_DataObject* aCalculRootObj = createObject( aNewRootObj, tr( partitionName( KIND_CALCULATION ).toAscii() ) ); // POLYLINES - LightApp_DataObject* aPolylineRootObj = createObject( aRootObj, tr( partitionName( KIND_POLYLINEXY ).toAscii() ) ); + LightApp_DataObject* aPolylineRootObj = createObject( aNewRootObj, tr( partitionName( KIND_POLYLINEXY ).toAscii() ) ); // POLYLINES - LightApp_DataObject* aPolyline3DRootObj = createObject( aRootObj, tr( partitionName( KIND_POLYLINE ).toAscii() ) ); + LightApp_DataObject* aPolyline3DRootObj = createObject( aNewRootObj, tr( partitionName( KIND_POLYLINE ).toAscii() ) ); // PROFILES - LightApp_DataObject* aProfileRootObj = createObject( aRootObj, tr( partitionName( KIND_PROFILE ).toAscii() ) ); + LightApp_DataObject* aProfileRootObj = createObject( aNewRootObj, tr( partitionName( KIND_PROFILE ).toAscii() ) ); // VISUAL STATES - LightApp_DataObject* aVisualStateRootObj = createObject( aRootObj, tr( partitionName( KIND_VISUAL_STATE ).toAscii() ) ); + LightApp_DataObject* aVisualStateRootObj = createObject( aNewRootObj, tr( partitionName( KIND_VISUAL_STATE ).toAscii() ) ); HYDROData_Iterator anIterator( aDocument, KIND_UNKNOWN ); for( ; anIterator.More(); anIterator.Next() ) { @@ -410,11 +401,18 @@ void HYDROGUI_DataModel::update( const int theStudyId ) } } - if( SUIT_DataBrowser* anObjectBrowser = anApp->objectBrowser() ) - { - anObjectBrowser->setAutoOpenLevel( 3 ); - anObjectBrowser->openLevels(); - } + //if( SUIT_DataBrowser* anObjectBrowser = anApp->objectBrowser() ) + //{ + // anObjectBrowser->setAutoOpenLevel( 3 ); + // anObjectBrowser->openLevels(); + //} + + HYDROGUI_DataModelSync aSync( aNewRootObj ); + SUIT_DataObject* aRoot = root(); + if( !aRoot ) + aRoot = createRootModuleObject( aStudyRoot ); + ::synchronize < suitPtr, suitPtr, HYDROGUI_DataModelSync > + ( aNewRootObj, aRoot, aSync ); } HYDROGUI_DataObject* HYDROGUI_DataModel::getDataObject( const Handle(HYDROData_Entity)& theModelObject ) diff --git a/src/HYDROGUI/HYDROGUI_DataModelSync.cxx b/src/HYDROGUI/HYDROGUI_DataModelSync.cxx new file mode 100644 index 00000000..58808f11 --- /dev/null +++ b/src/HYDROGUI/HYDROGUI_DataModelSync.cxx @@ -0,0 +1,96 @@ + +#include +#include + +HYDROGUI_DataModelSync::HYDROGUI_DataModelSync( SUIT_DataObject* theRoot ) + : myRoot( theRoot ) +{ +} + +HYDROGUI_DataModelSync::~HYDROGUI_DataModelSync() +{ +} + +void HYDROGUI_DataModelSync::deleteItemWithChildren( const suitPtr& theSuitPtr ) const +{ + if( !theSuitPtr ) + return; + + DataObjectList aChildren; + theSuitPtr->children( aChildren ); + foreach( SUIT_DataObject* anObj, aChildren ) + deleteItemWithChildren( anObj ); + + delete theSuitPtr; +} + +suitPtr HYDROGUI_DataModelSync::nullSrc() const +{ + return 0; +} + +suitPtr HYDROGUI_DataModelSync::nullTrg() const +{ + return 0; +} + +QList HYDROGUI_DataModelSync::children( const suitPtr& theSuitPtr ) const +{ + QList aChildren; + if( theSuitPtr ) + { + DataObjectList anObjList; + theSuitPtr->children( anObjList ); + foreach( SUIT_DataObject* anObj, anObjList ) + aChildren.append( anObj ); + } + return aChildren; +} + +suitPtr HYDROGUI_DataModelSync::parent( const suitPtr& theSuitPtr ) const +{ + return theSuitPtr ? theSuitPtr->parent() : 0; +} + +void HYDROGUI_DataModelSync::updateItem( const suitPtr& theSrcPtr, const suitPtr& theTrgPtr ) const +{ + HYDROGUI_DataObject* aDataObj = dynamic_cast( theTrgPtr ); + if( aDataObj ) + aDataObj->updateBy( theSrcPtr ); + + HYDROGUI_NamedObject* aNamedObj = dynamic_cast( theTrgPtr ); + if( aNamedObj ) + aNamedObj->updateBy( theSrcPtr ); +} + +bool HYDROGUI_DataModelSync::isEqual( const suitPtr& theSrcPtr, const suitPtr& theTrgPtr ) const +{ + if( theSrcPtr==myRoot ) + return true; + + if( theSrcPtr==0 ) + return theTrgPtr==0; + + if( theTrgPtr==0 ) + return theSrcPtr==0; + + QString aSrcClass = typeid( *theSrcPtr ).name(); + QString aTrgClass = typeid( *theTrgPtr ).name(); + return aSrcClass==aTrgClass; +} + +suitPtr HYDROGUI_DataModelSync::createItem( const suitPtr& theSrcPtr, + const suitPtr& theParent, + const suitPtr& theAfter ) const +{ + if( theParent ) + { + int aPos = theParent->childPos( theAfter ); + if( aPos>=0 ) + theParent->insertChild( theSrcPtr, aPos+1 ); + else + theParent->appendChild( theSrcPtr ); + } + return theSrcPtr; +} + diff --git a/src/HYDROGUI/HYDROGUI_DataModelSync.h b/src/HYDROGUI/HYDROGUI_DataModelSync.h new file mode 100644 index 00000000..91920682 --- /dev/null +++ b/src/HYDROGUI/HYDROGUI_DataModelSync.h @@ -0,0 +1,30 @@ + +#ifndef HYDROGUI_DATA_MODEL_SYNC_HEADER +#define HYDROGUI_DATA_MODEL_SYNC_HEADER + +#include + +class SUIT_DataObject; + +typedef SUIT_DataObject* suitPtr; + +class HYDROGUI_DataModelSync +{ +public: + HYDROGUI_DataModelSync( SUIT_DataObject* theRoot ); + ~HYDROGUI_DataModelSync(); + + void deleteItemWithChildren( const suitPtr& ) const; + suitPtr nullSrc() const; + suitPtr nullTrg() const; + QList children( const suitPtr& ) const; + suitPtr parent( const suitPtr& ) const; + void updateItem( const suitPtr&, const suitPtr& ) const; + bool isEqual( const suitPtr&, const suitPtr& ) const; + suitPtr createItem( const suitPtr&, const suitPtr&, const suitPtr& ) const; + +private: + SUIT_DataObject* myRoot; +}; + +#endif diff --git a/src/HYDROGUI/HYDROGUI_DataObject.cxx b/src/HYDROGUI/HYDROGUI_DataObject.cxx index a259a866..cde78b0d 100644 --- a/src/HYDROGUI/HYDROGUI_DataObject.cxx +++ b/src/HYDROGUI/HYDROGUI_DataObject.cxx @@ -220,6 +220,22 @@ bool HYDROGUI_DataObject::renameAllowed( const int theColumnId ) const return aRes; } +void HYDROGUI_DataObject::updateBy( SUIT_DataObject* theObj ) +{ + HYDROGUI_DataObject* aDataObj = dynamic_cast( theObj ); + if( !aDataObj ) + return; + + myData = aDataObj->myData; + myParentEntry = aDataObj->myParentEntry; + myIsValid = aDataObj->myIsValid; + myIsInOperation = aDataObj->myIsInOperation; + myIcon = aDataObj->myIcon; +} + + + + HYDROGUI_NamedObject::HYDROGUI_NamedObject( SUIT_DataObject* theParent, @@ -270,6 +286,23 @@ QFont HYDROGUI_NamedObject::font( const int theId ) const return aFont; } +void HYDROGUI_NamedObject::updateBy( SUIT_DataObject* theObj ) +{ + HYDROGUI_NamedObject* aNamedObj = dynamic_cast( theObj ); + if( !aNamedObj ) + return; + + myName = aNamedObj->myName; + myParentEntry = aNamedObj->myParentEntry; + myIcon = aNamedObj->myIcon; + myIsInOperation = aNamedObj->myIsInOperation; +} + + + + + + HYDROGUI_DropTargetObject::HYDROGUI_DropTargetObject( SUIT_DataObject* theParent, const QString& theName, const QString& theParentEntry, diff --git a/src/HYDROGUI/HYDROGUI_DataObject.h b/src/HYDROGUI/HYDROGUI_DataObject.h index 6ffeaa1e..b35d60c0 100644 --- a/src/HYDROGUI/HYDROGUI_DataObject.h +++ b/src/HYDROGUI/HYDROGUI_DataObject.h @@ -141,6 +141,8 @@ public: */ bool isInOperation() const { return myIsInOperation; } + void updateBy( SUIT_DataObject* ); + protected: Handle(HYDROData_Entity) myData; ///< object from data model QString myParentEntry; @@ -199,6 +201,8 @@ public: */ bool isInOperation() const { return myIsInOperation; } + void updateBy( SUIT_DataObject* ); + private: QString myName; ///< name in the OB QString myParentEntry; -- 2.39.2