]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_Tool.cxx
Salome HOME
The data model has been redesigned to new format.
[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 aChanged = true;
30
31   // iterate until there is no changes because images on all level of dependency must be updated
32   while ( aChanged )
33   {
34     aChanged = 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->MustBeUpdated() )
42         continue;
43
44       for ( int i = 0, aNBRefs = anImage->NbReferences(); i < aNBRefs; ++i )
45       {
46         Handle(HYDROData_Image) aRefImage =
47           Handle(HYDROData_Image)::DownCast( anImage->Reference( i ) );
48         if ( !aRefImage.IsNull() && aRefImage->MustBeUpdated() )
49         {
50            // image references to updated => also must be updated
51            anImage->MustBeUpdated(true);
52            aChanged = true;
53         }
54       }
55     }
56   }
57 }
58
59 QString HYDROData_Tool::GenerateObjectName( const Handle(HYDROData_Document)& theDoc,
60                                             const QString&                    thePrefix,
61                                             const QStringList&                theUsedNames )
62 {
63   QStringList aNamesList( theUsedNames );
64
65   // Collect all used names in the document
66   HYDROData_Iterator anIter( theDoc );
67   for( ; anIter.More(); anIter.Next() )
68   {
69     Handle(HYDROData_Entity) anObject = anIter.Current();
70     if( anObject.IsNull() )
71       continue;
72
73     QString anObjName = anObject->GetName();
74     if ( anObjName.isEmpty() )
75       continue;
76
77     aNamesList.append( anObjName );
78   }
79
80   QString aName;
81
82   int anId = 1;
83   while( anId < 1000 )
84   {
85     aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
86
87     // check that there are no other objects with the same name in the document
88     if ( !aNamesList.contains( aName ) )
89       break;
90   }
91
92   return aName;
93 }
94
95 Handle(HYDROData_Entity) HYDROData_Tool::FindObjectByName( const Handle(HYDROData_Document)& theDoc,
96                                                            const QString&                    theName,
97                                                            const ObjectKind                  theObjectKind )
98 {
99   Handle(HYDROData_Entity) anObject;
100   if ( theName.isEmpty() || theDoc.IsNull() )
101     return anObject;
102
103   QStringList aNamesList;
104   aNamesList << theName;
105
106   HYDROData_SequenceOfObjects aSeqOfObjs = FindObjectsByNames( theDoc, aNamesList, theObjectKind );
107   if( aSeqOfObjs.IsEmpty() )
108     return anObject;
109   
110   anObject = aSeqOfObjs.First();
111   return anObject;
112 }
113
114 HYDROData_SequenceOfObjects HYDROData_Tool::FindObjectsByNames( const Handle(HYDROData_Document)& theDoc,
115                                                                 const QStringList&                theNames,
116                                                                 const ObjectKind                  theObjectKind )
117 {
118   HYDROData_SequenceOfObjects aResSeq;
119   if( theDoc.IsNull() )
120     return aResSeq;
121
122   QStringList aNamesList = theNames;
123
124   HYDROData_Iterator anIter( theDoc, theObjectKind );
125   for( ; anIter.More(); anIter.Next() )
126   {
127     Handle(HYDROData_Entity) anObject = anIter.Current();
128     if( anObject.IsNull() )
129       continue;
130
131     QString anObjName = anObject->GetName();
132     if ( anObjName.isEmpty() || !aNamesList.contains( anObjName ) )
133       continue;
134
135     aResSeq.Append( anObject );
136
137     aNamesList.removeAll( anObjName );
138     if ( aNamesList.isEmpty() )
139       break;
140   }
141
142   return aResSeq;
143 }
144