Salome HOME
58808f117e2674f6d9431c5ab31bf9d5bea8c2b7
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_DataModelSync.cxx
1
2 #include <HYDROGUI_DataModelSync.h>
3 #include <HYDROGUI_DataObject.h>
4
5 HYDROGUI_DataModelSync::HYDROGUI_DataModelSync( SUIT_DataObject* theRoot )
6   : myRoot( theRoot )
7 {
8 }
9
10 HYDROGUI_DataModelSync::~HYDROGUI_DataModelSync()
11 {
12 }
13
14 void HYDROGUI_DataModelSync::deleteItemWithChildren( const suitPtr& theSuitPtr ) const
15 {
16   if( !theSuitPtr )
17     return;
18
19   DataObjectList aChildren;
20   theSuitPtr->children( aChildren );
21   foreach( SUIT_DataObject* anObj, aChildren )
22     deleteItemWithChildren( anObj );
23
24   delete theSuitPtr;
25 }
26
27 suitPtr HYDROGUI_DataModelSync::nullSrc() const
28 {
29   return 0;
30 }
31
32 suitPtr HYDROGUI_DataModelSync::nullTrg() const
33 {
34   return 0;
35 }
36
37 QList<suitPtr> HYDROGUI_DataModelSync::children( const suitPtr& theSuitPtr ) const
38 {
39   QList<suitPtr> aChildren;
40   if( theSuitPtr )
41   {
42     DataObjectList anObjList;
43     theSuitPtr->children( anObjList );
44     foreach( SUIT_DataObject* anObj, anObjList )
45       aChildren.append( anObj );
46   }
47   return aChildren;
48 }
49
50 suitPtr HYDROGUI_DataModelSync::parent( const suitPtr& theSuitPtr ) const
51 {
52   return theSuitPtr ? theSuitPtr->parent() : 0;
53 }
54
55 void HYDROGUI_DataModelSync::updateItem( const suitPtr& theSrcPtr, const suitPtr& theTrgPtr ) const
56 {
57   HYDROGUI_DataObject* aDataObj = dynamic_cast<HYDROGUI_DataObject*>( theTrgPtr );
58   if( aDataObj )
59     aDataObj->updateBy( theSrcPtr );
60
61   HYDROGUI_NamedObject* aNamedObj = dynamic_cast<HYDROGUI_NamedObject*>( theTrgPtr );
62   if( aNamedObj )
63     aNamedObj->updateBy( theSrcPtr );
64 }
65
66 bool HYDROGUI_DataModelSync::isEqual( const suitPtr& theSrcPtr, const suitPtr& theTrgPtr ) const
67 {
68   if( theSrcPtr==myRoot )
69     return true;
70
71   if( theSrcPtr==0 )
72     return theTrgPtr==0;
73
74   if( theTrgPtr==0 )
75     return theSrcPtr==0;
76
77   QString aSrcClass = typeid( *theSrcPtr ).name();
78   QString aTrgClass = typeid( *theTrgPtr ).name();
79   return aSrcClass==aTrgClass;
80 }
81
82 suitPtr HYDROGUI_DataModelSync::createItem( const suitPtr& theSrcPtr,
83                                             const suitPtr& theParent,
84                                             const suitPtr& theAfter ) const
85 {
86   if( theParent )
87   {
88     int aPos = theParent->childPos( theAfter );
89     if( aPos>=0 )
90       theParent->insertChild( theSrcPtr, aPos+1 );
91     else
92       theParent->appendChild( theSrcPtr );
93   }
94   return theSrcPtr;
95 }
96