Salome HOME
Merge branch 'BR_H2018_3' into BR_2018_V8_5
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_DataModelSync.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include <HYDROGUI_DataModelSync.h>
20 #include <HYDROGUI_DataObject.h>
21 #include <typeinfo>
22
23 HYDROGUI_DataModelSync::HYDROGUI_DataModelSync( SUIT_DataObject* theRoot )
24   : myRoot( theRoot )
25 {
26 }
27
28 HYDROGUI_DataModelSync::~HYDROGUI_DataModelSync()
29 {
30 }
31
32 void HYDROGUI_DataModelSync::deleteItemWithChildren( const suitPtr& theSuitPtr ) const
33 {
34   if( !theSuitPtr )
35     return;
36
37   theSuitPtr->setAutoDeleteChildren( true );
38   theSuitPtr->deleteLater();
39 }
40
41 suitPtr HYDROGUI_DataModelSync::nullSrc() const
42 {
43   return 0;
44 }
45
46 suitPtr HYDROGUI_DataModelSync::nullTrg() const
47 {
48   return 0;
49 }
50
51 QList<suitPtr> HYDROGUI_DataModelSync::children( const suitPtr& theSuitPtr ) const
52 {
53   QList<suitPtr> aChildren;
54   if( theSuitPtr )
55   {
56     DataObjectList anObjList;
57     theSuitPtr->children( anObjList );
58     foreach( SUIT_DataObject* anObj, anObjList )
59       aChildren.append( anObj );
60   }
61   return aChildren;
62 }
63
64 suitPtr HYDROGUI_DataModelSync::parent( const suitPtr& theSuitPtr ) const
65 {
66   return theSuitPtr ? theSuitPtr->parent() : 0;
67 }
68
69 void HYDROGUI_DataModelSync::updateItem( const suitPtr& theSrcPtr, const suitPtr& theTrgPtr ) const
70 {
71   HYDROGUI_DataObject* aDataObj = dynamic_cast<HYDROGUI_DataObject*>( theTrgPtr );
72   if( aDataObj )
73     aDataObj->updateBy( theSrcPtr );
74
75   HYDROGUI_NamedObject* aNamedObj = dynamic_cast<HYDROGUI_NamedObject*>( theTrgPtr );
76   if( aNamedObj )
77     aNamedObj->updateBy( theSrcPtr );
78 }
79
80 bool HYDROGUI_DataModelSync::isEqual( const suitPtr& theSrcPtr, const suitPtr& theTrgPtr ) const
81 {
82   if( theSrcPtr==myRoot )
83     return true;
84
85   if( theSrcPtr==0 )
86     return theTrgPtr==0;
87
88   if( theTrgPtr==0 )
89     return theSrcPtr==0;
90
91   QString aSrcClass = typeid( *theSrcPtr ).name();
92   QString aTrgClass = typeid( *theTrgPtr ).name();
93   return aSrcClass==aTrgClass;
94 }
95
96 suitPtr HYDROGUI_DataModelSync::createItem( const suitPtr& theSrcPtr,
97                                             const suitPtr& theParent,
98                                             const suitPtr& theAfter ) const
99 {
100   if( theParent )
101   {
102     int aPos = theParent->childPos( theAfter );
103     if( aPos>=0 )
104       theParent->insertChild( theSrcPtr, aPos+1 );
105     else
106       theParent->appendChild( theSrcPtr );
107   }
108   return theSrcPtr;
109 }
110