Salome HOME
merge trunk on BR_quadtree 20140820
[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   theSuitPtr->setAutoDeleteChildren( true );
21   theSuitPtr->deleteLater();
22 }
23
24 suitPtr HYDROGUI_DataModelSync::nullSrc() const
25 {
26   return 0;
27 }
28
29 suitPtr HYDROGUI_DataModelSync::nullTrg() const
30 {
31   return 0;
32 }
33
34 QList<suitPtr> HYDROGUI_DataModelSync::children( const suitPtr& theSuitPtr ) const
35 {
36   QList<suitPtr> aChildren;
37   if( theSuitPtr )
38   {
39     DataObjectList anObjList;
40     theSuitPtr->children( anObjList );
41     foreach( SUIT_DataObject* anObj, anObjList )
42       aChildren.append( anObj );
43   }
44   return aChildren;
45 }
46
47 suitPtr HYDROGUI_DataModelSync::parent( const suitPtr& theSuitPtr ) const
48 {
49   return theSuitPtr ? theSuitPtr->parent() : 0;
50 }
51
52 void HYDROGUI_DataModelSync::updateItem( const suitPtr& theSrcPtr, const suitPtr& theTrgPtr ) const
53 {
54   HYDROGUI_DataObject* aDataObj = dynamic_cast<HYDROGUI_DataObject*>( theTrgPtr );
55   if( aDataObj )
56     aDataObj->updateBy( theSrcPtr );
57
58   HYDROGUI_NamedObject* aNamedObj = dynamic_cast<HYDROGUI_NamedObject*>( theTrgPtr );
59   if( aNamedObj )
60     aNamedObj->updateBy( theSrcPtr );
61 }
62
63 bool HYDROGUI_DataModelSync::isEqual( const suitPtr& theSrcPtr, const suitPtr& theTrgPtr ) const
64 {
65   if( theSrcPtr==myRoot )
66     return true;
67
68   if( theSrcPtr==0 )
69     return theTrgPtr==0;
70
71   if( theTrgPtr==0 )
72     return theSrcPtr==0;
73
74   QString aSrcClass = typeid( *theSrcPtr ).name();
75   QString aTrgClass = typeid( *theTrgPtr ).name();
76   return aSrcClass==aTrgClass;
77 }
78
79 suitPtr HYDROGUI_DataModelSync::createItem( const suitPtr& theSrcPtr,
80                                             const suitPtr& theParent,
81                                             const suitPtr& theAfter ) const
82 {
83   if( theParent )
84   {
85     int aPos = theParent->childPos( theAfter );
86     if( aPos>=0 )
87       theParent->insertChild( theSrcPtr, aPos+1 );
88     else
89       theParent->appendChild( theSrcPtr );
90   }
91   return theSrcPtr;
92 }
93