Salome HOME
Update mechanism is corrected (Bug #182).
[modules/hydro.git] / src / HYDROData / HYDROData_Tool.cxx
1
2 #include "HYDROData_Tool.h"
3
4 #include "HYDROData_ArtificialObject.h"
5 #include "HYDROData_Image.h"
6 #include "HYDROData_Iterator.h"
7 #include "HYDROData_NaturalObject.h"
8
9 #include <QFile>
10 #include <QStringList>
11 #include <QTextStream>
12
13 void HYDROData_Tool::WriteStringsToFile( QFile&             theFile,
14                                          const QStringList& theStrings,
15                                          const QString&     theSep )
16 {
17   if ( !theFile.isOpen() || theStrings.isEmpty() )
18     return;
19   
20   QString aWriteStr = theStrings.join( theSep );
21   if ( aWriteStr.isEmpty() )
22     return;
23
24   QTextStream anOutStream( &theFile );
25   anOutStream << aWriteStr << theSep << theSep;
26 }
27
28 void HYDROData_Tool::SetMustBeUpdatedObjects(
29   const Handle(HYDROData_Document)& theDoc  )
30 {
31   bool anIsChanged = true;
32
33   // iterate until there is no changes because objects on all level of dependency must be updated
34   while ( anIsChanged )
35   {
36     anIsChanged = false;
37
38     HYDROData_Iterator anIter( theDoc );
39     for ( ; anIter.More(); anIter.Next() )
40     {
41       Handle(HYDROData_Entity) anObject = anIter.Current();
42       if ( anObject.IsNull() || anObject->IsMustBeUpdated() )
43         continue;
44
45       HYDROData_SequenceOfObjects aRefSeq = anObject->GetAllReferenceObjects();
46       for ( int i = 1, n = aRefSeq.Length(); i <= n; ++i )
47       {
48         Handle(HYDROData_Entity) aRefObject = aRefSeq.Value( i );
49         if ( aRefObject.IsNull() || !aRefObject->IsMustBeUpdated() )
50           continue;
51
52         anObject->SetToUpdate( true );
53         anIsChanged = true;
54         break;
55       }
56     }
57   }
58 }
59
60 QString HYDROData_Tool::GenerateObjectName( const Handle(HYDROData_Document)& theDoc,
61                                             const QString&                    thePrefix,
62                                             const QStringList&                theUsedNames,
63                                             const bool                        theIsTryToUsePurePrefix )
64 {
65   QStringList aNamesList( theUsedNames );
66
67   // Collect all used names in the document
68   HYDROData_Iterator anIter( theDoc );
69   for( ; anIter.More(); anIter.Next() )
70   {
71     Handle(HYDROData_Entity) anObject = anIter.Current();
72     if( anObject.IsNull() )
73       continue;
74
75     QString anObjName = anObject->GetName();
76     if ( anObjName.isEmpty() )
77       continue;
78
79     aNamesList.append( anObjName );
80   }
81
82   QString aName;
83
84   if ( theIsTryToUsePurePrefix && !aNamesList.contains( thePrefix ) ) {
85     aName = thePrefix;
86   } else {
87     int anId = 1;
88     while( anId < 1000 )
89     {
90       aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
91
92       // check that there are no other objects with the same name in the document
93       if ( !aNamesList.contains( aName ) )
94         break;
95     }
96   }
97
98   return aName;
99 }
100
101 Handle(HYDROData_Entity) HYDROData_Tool::FindObjectByName( const Handle(HYDROData_Document)& theDoc,
102                                                            const QString&                    theName,
103                                                            const ObjectKind                  theObjectKind )
104 {
105   Handle(HYDROData_Entity) anObject;
106   if ( theName.isEmpty() || theDoc.IsNull() )
107     return anObject;
108
109   QStringList aNamesList;
110   aNamesList << theName;
111
112   HYDROData_SequenceOfObjects aSeqOfObjs = FindObjectsByNames( theDoc, aNamesList, theObjectKind );
113   if( aSeqOfObjs.IsEmpty() )
114     return anObject;
115   
116   anObject = aSeqOfObjs.First();
117   return anObject;
118 }
119
120 HYDROData_SequenceOfObjects HYDROData_Tool::FindObjectsByNames( const Handle(HYDROData_Document)& theDoc,
121                                                                 const QStringList&                theNames,
122                                                                 const ObjectKind                  theObjectKind )
123 {
124   HYDROData_SequenceOfObjects aResSeq;
125   if( theDoc.IsNull() )
126     return aResSeq;
127
128   QStringList aNamesList = theNames;
129
130   HYDROData_Iterator anIter( theDoc, theObjectKind );
131   for( ; anIter.More(); anIter.Next() )
132   {
133     Handle(HYDROData_Entity) anObject = anIter.Current();
134     if( anObject.IsNull() )
135       continue;
136
137     QString anObjName = anObject->GetName();
138     if ( anObjName.isEmpty() || !aNamesList.contains( anObjName ) )
139       continue;
140
141     aResSeq.Append( anObject );
142
143     aNamesList.removeAll( anObjName );
144     if ( aNamesList.isEmpty() )
145       break;
146   }
147
148   return aResSeq;
149 }
150
151 bool HYDROData_Tool::IsGeometryObject( const Handle(HYDROData_Entity)& theObject )
152 {
153   if ( theObject.IsNull() )
154     return false;
155   
156   return theObject->IsKind( STANDARD_TYPE(HYDROData_ArtificialObject) ) ||
157          theObject->IsKind( STANDARD_TYPE(HYDROData_NaturalObject) );
158 }