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