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