Salome HOME
Variables naming.
[modules/hydro.git] / src / HYDROData / HYDROData_Entity.cxx
1
2 #include "HYDROData_Entity.h"
3
4 #include "HYDROData_Iterator.h"
5 #include "HYDROData_Tool.h"
6
7 #include <TDataStd_Name.hxx>
8 #include <TDataStd_ByteArray.hxx>
9 #include <TDataStd_UAttribute.hxx>
10 #include <TDataStd_IntegerArray.hxx>
11 #include <TDataStd_BooleanArray.hxx>
12 #include <TDataStd_RealArray.hxx>
13 #include <TDataStd_ReferenceList.hxx>
14
15 #include <TDF_CopyLabel.hxx>
16 #include <TDF_ListIteratorOfLabelList.hxx>
17
18 #include <QColor>
19 #include <QString>
20 #include <QStringList>
21 #include <QVariant>
22
23 static const Standard_GUID GUID_MUST_BE_UPDATED("80f2bb81-3873-4631-8ddd-940d2119f000");
24
25 IMPLEMENT_STANDARD_HANDLE(HYDROData_Entity,MMgt_TShared)
26 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Entity,MMgt_TShared)
27
28 // is equal function for unique object mapping
29 bool IsEqual(const Handle_HYDROData_Entity& theObj1, const Handle_HYDROData_Entity& theObj2)
30 {
31   if ( !theObj1.IsNull() && !theObj2.IsNull() )
32     return theObj1->Label() == theObj2->Label();
33   return false;
34 }
35
36 QString HYDROData_Entity::GetName() const
37 {
38   Handle(TDataStd_Name) aName;
39   if (myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
40     TCollection_AsciiString aStr(aName->Get());
41     return QString(aStr.ToCString());
42   }
43   return QString();
44 }
45
46 QString HYDROData_Entity::GetObjPyName() const
47 {
48   return GetName().replace(" ", "_");
49 }
50
51 void HYDROData_Entity::SetName(const QString& theName)
52 {
53   TDataStd_Name::Set(myLab, TCollection_ExtendedString(theName.toLatin1().constData()));
54 }
55
56 QStringList HYDROData_Entity::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
57 {
58   QStringList anEmptyList;
59   return anEmptyList;
60 }
61
62 QStringList HYDROData_Entity::dumpObjectCreation( MapOfTreatedObjects& theTreatedObjects ) const
63 {
64   QStringList aResList;
65
66   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
67   if ( aDocument.IsNull() )
68     return aResList;
69
70   QString aDocName = aDocument->GetDocPyName();
71   QString aName = GetObjPyName();
72
73   aResList << QString( "%1 = %2.CreateObject( %3 );" )
74               .arg( aName ).arg( aDocName ).arg( getPyTypeID() );
75   aResList << QString( "%1.SetName( \"%2\" );" )
76               .arg( aName ).arg( GetName() );
77   aResList << QString( "" );
78
79   return aResList;
80 }
81
82 void HYDROData_Entity::Update()
83 {
84   SetToUpdate( false );
85 }
86
87 void HYDROData_Entity::Show()
88 {
89   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
90   if ( aDocument.IsNull() )
91     return;
92
93   aDocument->Show( this );
94 }
95
96 QString HYDROData_Entity::getPyTypeID() const
97 {
98   switch( GetKind() )
99   {
100     case KIND_IMAGE:             return "KIND_IMAGE";
101     case KIND_POLYLINE:          return "KIND_POLYLINE";
102     case KIND_BATHYMETRY:        return "KIND_BATHYMETRY";
103     case KIND_ALTITUDE:          return "KIND_ALTITUDE";
104     case KIND_IMMERSIBLE_ZONE:   return "KIND_IMMERSIBLE_ZONE";
105     case KIND_RIVER:             return "KIND_RIVER";
106     case KIND_STREAM:            return "KIND_STREAM";
107     case KIND_CONFLUENCE:        return "KIND_CONFLUENCE";
108     case KIND_CHANNEL:           return "KIND_CHANNEL";
109     case KIND_OBSTACLE:          return "KIND_OBSTACLE";
110     case KIND_DIGUE:             return "KIND_DIGUE";
111     case KIND_PROFILE:           return "KIND_PROFILE";
112     case KIND_PROFILEUZ:         return "KIND_PROFILEUZ";
113     case KIND_POLYLINEXY:        return "KIND_POLYLINEXY";
114     case KIND_CALCULATION:       return "KIND_CALCULATION";
115     case KIND_ZONE:              return "KIND_ZONE";
116     case KIND_REGION:            return "KIND_REGION";
117     case KIND_VISUAL_STATE:      return "KIND_VISUAL_STATE";
118     case KIND_ARTIFICIAL_OBJECT: return "KIND_ARTIFICIAL_OBJECT";
119     case KIND_NATURAL_OBJECT:    return "KIND_NATURAL_OBJECT";
120     case KIND_DUMMY_3D:          return "KIND_DUMMY_3D";
121     case KIND_SHAPES_GROUP:      return "KIND_SHAPES_GROUP";
122     case KIND_SPLITTED_GROUP:    return "KIND_SPLITTED_GROUP";
123     case KIND_STREAM_ALTITUDE:   return "KIND_STREAM_ALTITUDE";
124     case KIND_OBSTACLE_ALTITUDE: return "KIND_OBSTACLE_ALTITUDE";
125     default:                     return "KIND_UNKNOWN"; ///! Unrecognized object
126   }
127 }
128
129 QVariant HYDROData_Entity::GetDataVariant()
130 {
131   return QVariant();
132 }
133
134 void HYDROData_Entity::SetToUpdate( bool theFlag )
135 {
136   if ( IsMustBeUpdated() == theFlag )
137     return;
138
139   if ( theFlag )
140   {
141     TDataStd_UAttribute::Set( myLab, GUID_MUST_BE_UPDATED );
142
143     Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
144     if ( !aDocument.IsNull() )
145     {
146       // Change the states of this and all depended objects
147       HYDROData_Tool::SetMustBeUpdatedObjects( aDocument );
148     }
149   }
150   else
151   {
152     myLab.ForgetAttribute( GUID_MUST_BE_UPDATED );
153   }
154 }
155
156 bool HYDROData_Entity::IsMustBeUpdated() const
157 {
158   return myLab.IsAttribute( GUID_MUST_BE_UPDATED );
159 }
160
161 bool HYDROData_Entity::CanBeUpdated() const
162 {
163   return true;
164 }
165
166 bool HYDROData_Entity::IsRemoved() const
167 {
168   return !myLab.HasAttribute();
169 }
170
171 void HYDROData_Entity::Remove()
172 {
173   return myLab.ForgetAllAttributes( true );
174 }
175
176 bool HYDROData_Entity::CanRemove()
177 {
178   return true;
179 }
180
181 HYDROData_Entity::HYDROData_Entity()
182 {
183 }
184
185 HYDROData_Entity::~HYDROData_Entity()
186 {
187 }
188
189 void HYDROData_Entity::CopyTo( const Handle(HYDROData_Entity)& theDestination ) const
190 {
191   TDF_CopyLabel aCopy(myLab, theDestination->Label());
192   aCopy.Perform();
193 }
194
195 Handle(HYDROData_Entity) HYDROData_Entity::GetFatherObject() const
196 {
197   Handle(HYDROData_Entity) aFather;
198
199   if ( !myLab.IsNull() )
200   {
201     TDF_Label aFatherLabel = myLab.Father();
202
203     while ( aFather.IsNull() && !aFatherLabel.IsNull() && !aFatherLabel.IsRoot() )
204     {
205       aFather = HYDROData_Iterator::Object( aFatherLabel );
206       aFatherLabel = aFatherLabel.Father();
207     }
208   }
209
210   return aFather;
211 }
212
213 HYDROData_SequenceOfObjects HYDROData_Entity::GetAllReferenceObjects() const
214 {
215   return HYDROData_SequenceOfObjects();
216 }
217
218 void HYDROData_Entity::SetLabel(TDF_Label theLabel)
219 {
220   myLab = theLabel;
221 }
222
223 void HYDROData_Entity::SaveByteArray(const int theTag, 
224   const char* theData, const int theLen)
225 {
226   TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
227   // array is empty, remove the attribute
228   if (theLen <= 0) {
229     aLab.ForgetAttribute(TDataStd_ByteArray::GetID());
230     return;
231   }
232   // store data of image in byte array
233   Handle(TDataStd_ByteArray) aData;
234   if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData)) {
235     aData = TDataStd_ByteArray::Set(aLab, 1, theLen);
236   }
237   // copy bytes one by one
238   if (aData->Length() != theLen) {
239     Handle(TColStd_HArray1OfByte) aNewData = new TColStd_HArray1OfByte(1, theLen);
240     for(int a = 0; a < theLen; a++)
241       aNewData->SetValue(a + 1, theData[a]);
242     aData->ChangeArray(aNewData);
243   } else {
244     for(int a = 0; a < theLen; a++)
245       aData->SetValue(a + 1, theData[a]);
246   }
247 }
248
249 const char* HYDROData_Entity::ByteArray(const int theTag, int& theLen) const
250 {
251   TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
252   Handle(TDataStd_ByteArray) aData;
253   if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData))
254     return NULL; // return empty image if there is no array
255   theLen = aData->Length();
256   if (theLen)
257     return (const char*)(&(aData->InternalArray()->ChangeArray1().ChangeValue(1)));
258   return NULL;
259 }
260
261 int HYDROData_Entity::NbReferenceObjects( const int theTag ) const
262 {
263   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
264   return aRefs.IsNull() ? 0 : aRefs->Extent();
265 }
266
267 bool HYDROData_Entity::HasReference( const Handle_HYDROData_Entity& theObj,
268                                      const int                      theTag ) const
269 {
270   if ( theObj.IsNull() )
271     return false;
272
273   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
274   if ( aRefs.IsNull() || aRefs->IsEmpty() )
275     return false;
276
277   TDF_ListIteratorOfLabelList aListIt( aRefs->List() );
278   for ( ; aListIt.More(); aListIt.Next() )
279   {
280     const TDF_Label& aRefLabel = aListIt.Value();
281     if  ( theObj->Label() == aRefLabel )
282       return true;
283   }
284
285   return false;
286 }
287
288 void HYDROData_Entity::AddReferenceObject( const Handle_HYDROData_Entity& theObj,
289                                            const int                      theTag )
290 {
291   if ( theObj.IsNull() )
292     return;
293
294   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
295   aRefs->Append( theObj->Label() );
296 }
297
298 void HYDROData_Entity::SetReferenceObject( const Handle_HYDROData_Entity& theObj,
299                                            const int                      theTag,
300                                            const int                      theIndex )
301 {
302   if ( theObj.IsNull() )
303   {
304     RemoveReferenceObject( theTag, theIndex );
305     return;
306   }
307
308   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
309
310   if ( theIndex >= aRefs->Extent() )
311   {
312     aRefs->Append( theObj->Label() );
313   }
314   else if ( theIndex < 0 )
315   {
316     aRefs->Prepend( theObj->Label() );
317   }
318   else
319   {
320     RemoveReferenceObject( theTag, theIndex );
321
322     Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theIndex );
323
324     aRefs = getReferenceList( theTag, true ); // because reference list can be removed
325     if ( !aBeforeObj.IsNull() )
326       aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
327     else 
328       aRefs->Append( theObj->Label() );
329   }
330 }
331
332 void HYDROData_Entity::InsertReferenceObject( const Handle_HYDROData_Entity& theObj,
333                                               const int                      theTag,
334                                               const int                      theBeforeIndex )
335 {
336   if ( theObj.IsNull() )
337     return;
338
339   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
340
341   if ( theBeforeIndex >= aRefs->Extent() )
342   {
343     aRefs->Append( theObj->Label() );
344   }
345   else if ( theBeforeIndex < 0 )
346   {
347     aRefs->Prepend( theObj->Label() );
348   }
349   else
350   {
351     Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theBeforeIndex );
352     if ( !aBeforeObj.IsNull() )
353       aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
354     else 
355       aRefs->Append( theObj->Label() );
356   }
357 }
358
359 void HYDROData_Entity::SetReferenceObjects( const HYDROData_SequenceOfObjects& theObjects,
360                                             const int                          theTag )
361 {
362   ClearReferenceObjects( theTag );
363   if ( theObjects.IsEmpty() )
364     return;
365
366   HYDROData_SequenceOfObjects::Iterator anIter( theObjects );
367   for ( ; anIter.More(); anIter.Next() )
368     AddReferenceObject( anIter.Value(), theTag );
369 }
370
371 Handle(HYDROData_Entity) HYDROData_Entity::GetReferenceObject( const int theTag,
372                                                                const int theIndex ) const
373 {
374   Handle(HYDROData_Entity) aRes;
375
376   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
377   if ( aRefs.IsNull() || theIndex < 0 || theIndex >= aRefs->Extent() )
378     return aRes;
379
380   TDF_ListIteratorOfLabelList anIter( aRefs->List() );
381   for ( int anIndex = 0; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
382
383   const TDF_Label& aRefLabel = anIter.Value();
384   aRes = HYDROData_Iterator::Object( aRefLabel );
385
386   return aRes;
387 }
388
389 HYDROData_SequenceOfObjects HYDROData_Entity::GetReferenceObjects( const int theTag ) const
390 {
391   HYDROData_SequenceOfObjects aRes;
392
393   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
394   if ( aRefs.IsNull() )
395     return aRes;
396
397   TDF_ListIteratorOfLabelList anIter( aRefs->List() );
398   for ( ; anIter.More(); anIter.Next() )
399   {
400     const TDF_Label& aRefLabel = anIter.Value();
401
402     Handle(HYDROData_Entity) aRefObject = HYDROData_Iterator::Object( aRefLabel );
403     if ( aRefObject.IsNull() )
404       continue;
405
406     aRes.Append( aRefObject );
407   }
408
409   return aRes;
410 }
411
412 void HYDROData_Entity::RemoveReferenceObject( const TDF_Label& theRefLabel,
413                                               const int        theTag )
414 {
415   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
416   if ( aRefs.IsNull() )
417     return;
418
419   if ( aRefs->Extent() == 1 )
420   { 
421     // remove all if only one
422     ClearReferenceObjects( theTag );
423     return;
424   }
425
426   aRefs->Remove( theRefLabel );
427 }
428
429 void HYDROData_Entity::RemoveReferenceObject( const int theTag,
430                                               const int theIndex )
431 {
432   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
433   if ( aRefs.IsNull() )
434     return;
435
436   if ( aRefs->Extent() == 1 && theIndex == 0 )
437   { 
438     // remove all if only one
439     ClearReferenceObjects( theTag );
440     return;
441   }
442
443   int anIndex = 0;
444   TDF_ListIteratorOfLabelList anIter( aRefs->List() );
445   for ( ; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
446
447   if ( anIndex != theIndex || !anIter.More() )
448     return;
449
450   const TDF_Label& aRefLabel = anIter.Value();
451   aRefs->Remove( aRefLabel );
452 }
453
454 void HYDROData_Entity::ClearReferenceObjects( const int theTag )
455 {
456   TDF_Label aSetLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
457   aSetLabel.ForgetAttribute( TDataStd_ReferenceList::GetID() );
458 }
459
460 Handle(TDataStd_ReferenceList) HYDROData_Entity::getReferenceList( const int theTag,
461                                                                    const bool theIsCreate ) const
462 {
463   TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
464
465   Handle(TDataStd_ReferenceList) aRefs;
466   if ( !aLabel.FindAttribute( TDataStd_ReferenceList::GetID(), aRefs ) && theIsCreate )
467     aRefs = TDataStd_ReferenceList::Set( aLabel );
468
469   return aRefs;
470 }
471
472 void HYDROData_Entity::SetColor( const QColor& theColor,
473                                  const int     theTag )
474 {
475   TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
476
477   Handle(TDataStd_IntegerArray) aColorArray;
478   if ( !aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
479     aColorArray = TDataStd_IntegerArray::Set( aLabel, 1, 4 );
480
481   aColorArray->SetValue( 1, theColor.red()   );
482   aColorArray->SetValue( 2, theColor.green() );
483   aColorArray->SetValue( 3, theColor.blue()  );
484   aColorArray->SetValue( 4, theColor.alpha() );
485 }
486
487 QColor HYDROData_Entity::GetColor( const QColor& theDefColor,
488                                    const int     theTag ) const
489 {
490   QColor aResColor = theDefColor;
491
492   TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
493
494   Handle(TDataStd_IntegerArray) aColorArray;
495   if ( aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
496   {
497     aResColor.setRed(   aColorArray->Value( 1 ) );
498     aResColor.setGreen( aColorArray->Value( 2 ) );
499     aResColor.setBlue(  aColorArray->Value( 3 ) );
500     aResColor.setAlpha( aColorArray->Value( 4 ) );
501   }
502
503   return aResColor;
504 }
505
506 void HYDROData_Entity::setPythonReferenceObject( MapOfTreatedObjects&            theTreatedObjects,
507                                                  QStringList&                    theScript,
508                                                  const Handle(HYDROData_Entity)& theRefObject,
509                                                  const QString&                  theMethod ) const
510 {
511   if ( !checkObjectPythonDefinition( theTreatedObjects, theScript, theRefObject ) )
512     return;
513
514   QString aRefObjName = theRefObject->GetObjPyName();
515
516   QString anObjName = GetObjPyName();
517   theScript << QString( "%1.%2( %3 );" )
518                .arg( anObjName ).arg( theMethod ).arg( aRefObjName );
519 }
520
521 bool HYDROData_Entity::checkObjectPythonDefinition( MapOfTreatedObjects&            theTreatedObjects,
522                                                     QStringList&                    theScript,
523                                                     const Handle(HYDROData_Entity)& theRefObject ) const
524 {
525   if ( theRefObject.IsNull() )
526     return false;
527
528   QString aRefObjName = theRefObject->GetName();
529   if ( aRefObjName.isEmpty() )
530     return false;
531
532   if ( theTreatedObjects.contains( aRefObjName ) )
533     return true;
534
535   // The definition of reference object must be dumped before this
536   QStringList aRefObjDump = theRefObject->DumpToPython( theTreatedObjects );
537   if ( aRefObjDump.isEmpty() )
538     return false;
539
540   QStringList aTmpList = theScript;
541   theScript = aRefObjDump;
542
543   theScript << QString( "" );
544   theScript << aTmpList;
545
546   theTreatedObjects.insert( aRefObjName, theRefObject );
547
548   return true;
549 }
550
551 void HYDROData_Entity::setPythonObjectColor( QStringList&         theScript,
552                                              const QColor&        theColor,
553                                              const QColor&        theDefaultColor,
554                                              const QString&       theMethod ) const
555 {
556   if ( theColor == theDefaultColor )
557     return; //Do not set the color for object if it like default
558
559   QString anObjName = GetObjPyName();
560   theScript << QString( "%1.%2( QColor( %3, %4, %5, %6 ) );" )
561               .arg( anObjName ).arg( theMethod )
562               .arg( theColor.red()  ).arg( theColor.green() )
563               .arg( theColor.blue() ).arg( theColor.alpha() );
564 }
565
566