Salome HOME
Move color tags to the base geometrical object class.
[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::SetMustBeUpdatedImages(
29   const Handle(HYDROData_Document)& theDoc  )
30 {
31   bool anIsChanged = true;
32
33   // iterate until there is no changes because images on all level of dependency must be updated
34   while ( anIsChanged )
35   {
36     anIsChanged = false;
37
38     HYDROData_Iterator anIter( theDoc, KIND_IMAGE );
39     for ( ; anIter.More(); anIter.Next() )
40     {
41       Handle(HYDROData_Image) anImage = 
42         Handle(HYDROData_Image)::DownCast( anIter.Current() );
43       if ( anImage.IsNull() || anImage->IsMustBeUpdated() )
44         continue;
45
46       Handle(HYDROData_Image) aTrsfRefImage = anImage->GetTrsfReferenceImage();
47       if ( !aTrsfRefImage.IsNull() && aTrsfRefImage->IsMustBeUpdated() )
48       {
49         anImage->SetToUpdate( true );
50         anIsChanged = true;
51         continue;
52       }
53
54       for ( int i = 0, aNBRefs = anImage->NbReferences(); i < aNBRefs; ++i )
55       {
56         Handle(HYDROData_Image) aRefImage =
57           Handle(HYDROData_Image)::DownCast( anImage->Reference( i ) );
58         if ( !aRefImage.IsNull() && aRefImage->IsMustBeUpdated() )
59         {
60            // image references to updated => also must be updated
61            anImage->SetToUpdate( true );
62            anIsChanged = true;
63         }
64       }
65     }
66   }
67 }
68
69 QString HYDROData_Tool::GenerateObjectName( const Handle(HYDROData_Document)& theDoc,
70                                             const QString&                    thePrefix,
71                                             const QStringList&                theUsedNames )
72 {
73   QStringList aNamesList( theUsedNames );
74
75   // Collect all used names in the document
76   HYDROData_Iterator anIter( theDoc );
77   for( ; anIter.More(); anIter.Next() )
78   {
79     Handle(HYDROData_Entity) anObject = anIter.Current();
80     if( anObject.IsNull() )
81       continue;
82
83     QString anObjName = anObject->GetName();
84     if ( anObjName.isEmpty() )
85       continue;
86
87     aNamesList.append( anObjName );
88   }
89
90   QString aName;
91
92   int anId = 1;
93   while( anId < 1000 )
94   {
95     aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
96
97     // check that there are no other objects with the same name in the document
98     if ( !aNamesList.contains( aName ) )
99       break;
100   }
101
102   return aName;
103 }
104
105 Handle(HYDROData_Entity) HYDROData_Tool::FindObjectByName( const Handle(HYDROData_Document)& theDoc,
106                                                            const QString&                    theName,
107                                                            const ObjectKind                  theObjectKind )
108 {
109   Handle(HYDROData_Entity) anObject;
110   if ( theName.isEmpty() || theDoc.IsNull() )
111     return anObject;
112
113   QStringList aNamesList;
114   aNamesList << theName;
115
116   HYDROData_SequenceOfObjects aSeqOfObjs = FindObjectsByNames( theDoc, aNamesList, theObjectKind );
117   if( aSeqOfObjs.IsEmpty() )
118     return anObject;
119   
120   anObject = aSeqOfObjs.First();
121   return anObject;
122 }
123
124 HYDROData_SequenceOfObjects HYDROData_Tool::FindObjectsByNames( const Handle(HYDROData_Document)& theDoc,
125                                                                 const QStringList&                theNames,
126                                                                 const ObjectKind                  theObjectKind )
127 {
128   HYDROData_SequenceOfObjects aResSeq;
129   if( theDoc.IsNull() )
130     return aResSeq;
131
132   QStringList aNamesList = theNames;
133
134   HYDROData_Iterator anIter( theDoc, theObjectKind );
135   for( ; anIter.More(); anIter.Next() )
136   {
137     Handle(HYDROData_Entity) anObject = anIter.Current();
138     if( anObject.IsNull() )
139       continue;
140
141     QString anObjName = anObject->GetName();
142     if ( anObjName.isEmpty() || !aNamesList.contains( anObjName ) )
143       continue;
144
145     aResSeq.Append( anObject );
146
147     aNamesList.removeAll( anObjName );
148     if ( aNamesList.isEmpty() )
149       break;
150   }
151
152   return aResSeq;
153 }
154
155 bool HYDROData_Tool::IsGeometryObject( const Handle(HYDROData_Entity)& theObject )
156 {
157   if ( theObject.IsNull() )
158     return false;
159   
160   return theObject->IsKind( STANDARD_TYPE(HYDROData_ArtificialObject) ) ||
161          theObject->IsKind( STANDARD_TYPE(HYDROData_NaturalObject) );
162 }