2 #include "HYDROData_Entity.h"
4 #include "HYDROData_Iterator.h"
5 #include "HYDROData_Tool.h"
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>
15 #include <TDF_CopyLabel.hxx>
16 #include <TDF_ListIteratorOfLabelList.hxx>
20 #include <QStringList>
23 static const Standard_GUID GUID_MUST_BE_UPDATED("80f2bb81-3873-4631-8ddd-940d2119f000");
25 IMPLEMENT_STANDARD_HANDLE(HYDROData_Entity,MMgt_TShared)
26 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Entity,MMgt_TShared)
28 // is equal function for unique object mapping
29 bool IsEqual(const Handle_HYDROData_Entity& theObj1, const Handle_HYDROData_Entity& theObj2)
31 if ( !theObj1.IsNull() && !theObj2.IsNull() )
32 return theObj1->Label() == theObj2->Label();
36 QString HYDROData_Entity::GetName() const
38 Handle(TDataStd_Name) aName;
39 if (myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
40 TCollection_AsciiString aStr(aName->Get());
41 return QString(aStr.ToCString());
46 QString HYDROData_Entity::GetObjPyName() const
48 return GetName().replace(" ", "_");
51 void HYDROData_Entity::SetName(const QString& theName)
53 TDataStd_Name::Set(myLab, TCollection_ExtendedString(theName.toLatin1().constData()));
56 QStringList HYDROData_Entity::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
58 QStringList anEmptyList;
62 QStringList HYDROData_Entity::dumpObjectCreation( MapOfTreatedObjects& theTreatedObjects ) const
66 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
67 if ( aDocument.IsNull() )
70 QString aDocName = aDocument->GetDocPyName();
71 QString aName = GetObjPyName();
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( "" );
82 void HYDROData_Entity::Update()
87 void HYDROData_Entity::Show()
89 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
90 if ( aDocument.IsNull() )
93 aDocument->Show( this );
96 QString HYDROData_Entity::getPyTypeID() const
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
129 QVariant HYDROData_Entity::GetDataVariant()
134 void HYDROData_Entity::SetToUpdate( bool theFlag )
136 if ( IsMustBeUpdated() == theFlag )
141 TDataStd_UAttribute::Set( myLab, GUID_MUST_BE_UPDATED );
143 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
144 if ( !aDocument.IsNull() )
146 // Change the states of this and all depended objects
147 HYDROData_Tool::SetMustBeUpdatedObjects( aDocument );
152 myLab.ForgetAttribute( GUID_MUST_BE_UPDATED );
156 bool HYDROData_Entity::IsMustBeUpdated() const
158 return myLab.IsAttribute( GUID_MUST_BE_UPDATED );
161 bool HYDROData_Entity::CanBeUpdated() const
166 bool HYDROData_Entity::IsRemoved() const
168 return !myLab.HasAttribute();
171 void HYDROData_Entity::Remove()
173 return myLab.ForgetAllAttributes( true );
176 bool HYDROData_Entity::CanRemove()
181 HYDROData_Entity::HYDROData_Entity()
185 HYDROData_Entity::~HYDROData_Entity()
189 void HYDROData_Entity::CopyTo( const Handle(HYDROData_Entity)& theDestination ) const
191 TDF_CopyLabel aCopy(myLab, theDestination->Label());
195 Handle(HYDROData_Entity) HYDROData_Entity::GetFatherObject() const
197 Handle(HYDROData_Entity) aFather;
199 if ( !myLab.IsNull() )
201 TDF_Label aFatherLabel = myLab.Father();
203 while ( aFather.IsNull() && !aFatherLabel.IsNull() && !aFatherLabel.IsRoot() )
205 aFather = HYDROData_Iterator::Object( aFatherLabel );
206 aFatherLabel = aFatherLabel.Father();
213 HYDROData_SequenceOfObjects HYDROData_Entity::GetAllReferenceObjects() const
215 return HYDROData_SequenceOfObjects();
218 void HYDROData_Entity::SetLabel(TDF_Label theLabel)
223 void HYDROData_Entity::SaveByteArray(const int theTag,
224 const char* theData, const int theLen)
226 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
227 // array is empty, remove the attribute
229 aLab.ForgetAttribute(TDataStd_ByteArray::GetID());
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);
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);
244 for(int a = 0; a < theLen; a++)
245 aData->SetValue(a + 1, theData[a]);
249 const char* HYDROData_Entity::ByteArray(const int theTag, int& theLen) const
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();
257 return (const char*)(&(aData->InternalArray()->ChangeArray1().ChangeValue(1)));
261 int HYDROData_Entity::NbReferenceObjects( const int theTag ) const
263 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
264 return aRefs.IsNull() ? 0 : aRefs->Extent();
267 bool HYDROData_Entity::HasReference( const Handle_HYDROData_Entity& theObj,
268 const int theTag ) const
270 if ( theObj.IsNull() )
273 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
274 if ( aRefs.IsNull() || aRefs->IsEmpty() )
277 TDF_ListIteratorOfLabelList aListIt( aRefs->List() );
278 for ( ; aListIt.More(); aListIt.Next() )
280 const TDF_Label& aRefLabel = aListIt.Value();
281 if ( theObj->Label() == aRefLabel )
288 void HYDROData_Entity::AddReferenceObject( const Handle_HYDROData_Entity& theObj,
291 if ( theObj.IsNull() )
294 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
295 aRefs->Append( theObj->Label() );
298 void HYDROData_Entity::SetReferenceObject( const Handle_HYDROData_Entity& theObj,
302 if ( theObj.IsNull() )
304 RemoveReferenceObject( theTag, theIndex );
308 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
310 if ( theIndex >= aRefs->Extent() )
312 aRefs->Append( theObj->Label() );
314 else if ( theIndex < 0 )
316 aRefs->Prepend( theObj->Label() );
320 RemoveReferenceObject( theTag, theIndex );
322 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theIndex );
324 aRefs = getReferenceList( theTag, true ); // because reference list can be removed
325 if ( !aBeforeObj.IsNull() )
326 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
328 aRefs->Append( theObj->Label() );
332 void HYDROData_Entity::InsertReferenceObject( const Handle_HYDROData_Entity& theObj,
334 const int theBeforeIndex )
336 if ( theObj.IsNull() )
339 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
341 if ( theBeforeIndex >= aRefs->Extent() )
343 aRefs->Append( theObj->Label() );
345 else if ( theBeforeIndex < 0 )
347 aRefs->Prepend( theObj->Label() );
351 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theBeforeIndex );
352 if ( !aBeforeObj.IsNull() )
353 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
355 aRefs->Append( theObj->Label() );
359 void HYDROData_Entity::SetReferenceObjects( const HYDROData_SequenceOfObjects& theObjects,
362 ClearReferenceObjects( theTag );
363 if ( theObjects.IsEmpty() )
366 HYDROData_SequenceOfObjects::Iterator anIter( theObjects );
367 for ( ; anIter.More(); anIter.Next() )
368 AddReferenceObject( anIter.Value(), theTag );
371 Handle(HYDROData_Entity) HYDROData_Entity::GetReferenceObject( const int theTag,
372 const int theIndex ) const
374 Handle(HYDROData_Entity) aRes;
376 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
377 if ( aRefs.IsNull() || theIndex < 0 || theIndex >= aRefs->Extent() )
380 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
381 for ( int anIndex = 0; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
383 const TDF_Label& aRefLabel = anIter.Value();
384 aRes = HYDROData_Iterator::Object( aRefLabel );
389 HYDROData_SequenceOfObjects HYDROData_Entity::GetReferenceObjects( const int theTag ) const
391 HYDROData_SequenceOfObjects aRes;
393 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
394 if ( aRefs.IsNull() )
397 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
398 for ( ; anIter.More(); anIter.Next() )
400 const TDF_Label& aRefLabel = anIter.Value();
402 Handle(HYDROData_Entity) aRefObject = HYDROData_Iterator::Object( aRefLabel );
403 if ( aRefObject.IsNull() )
406 aRes.Append( aRefObject );
412 void HYDROData_Entity::RemoveReferenceObject( const TDF_Label& theRefLabel,
415 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
416 if ( aRefs.IsNull() )
419 if ( aRefs->Extent() == 1 )
421 // remove all if only one
422 ClearReferenceObjects( theTag );
426 aRefs->Remove( theRefLabel );
429 void HYDROData_Entity::RemoveReferenceObject( const int theTag,
432 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
433 if ( aRefs.IsNull() )
436 if ( aRefs->Extent() == 1 && theIndex == 0 )
438 // remove all if only one
439 ClearReferenceObjects( theTag );
444 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
445 for ( ; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
447 if ( anIndex != theIndex || !anIter.More() )
450 const TDF_Label& aRefLabel = anIter.Value();
451 aRefs->Remove( aRefLabel );
454 void HYDROData_Entity::ClearReferenceObjects( const int theTag )
456 TDF_Label aSetLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
457 aSetLabel.ForgetAttribute( TDataStd_ReferenceList::GetID() );
460 Handle(TDataStd_ReferenceList) HYDROData_Entity::getReferenceList( const int theTag,
461 const bool theIsCreate ) const
463 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
465 Handle(TDataStd_ReferenceList) aRefs;
466 if ( !aLabel.FindAttribute( TDataStd_ReferenceList::GetID(), aRefs ) && theIsCreate )
467 aRefs = TDataStd_ReferenceList::Set( aLabel );
472 void HYDROData_Entity::SetColor( const QColor& theColor,
475 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
477 Handle(TDataStd_IntegerArray) aColorArray;
478 if ( !aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
479 aColorArray = TDataStd_IntegerArray::Set( aLabel, 1, 4 );
481 aColorArray->SetValue( 1, theColor.red() );
482 aColorArray->SetValue( 2, theColor.green() );
483 aColorArray->SetValue( 3, theColor.blue() );
484 aColorArray->SetValue( 4, theColor.alpha() );
487 QColor HYDROData_Entity::GetColor( const QColor& theDefColor,
488 const int theTag ) const
490 QColor aResColor = theDefColor;
492 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
494 Handle(TDataStd_IntegerArray) aColorArray;
495 if ( aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
497 aResColor.setRed( aColorArray->Value( 1 ) );
498 aResColor.setGreen( aColorArray->Value( 2 ) );
499 aResColor.setBlue( aColorArray->Value( 3 ) );
500 aResColor.setAlpha( aColorArray->Value( 4 ) );
506 void HYDROData_Entity::setPythonReferenceObject( MapOfTreatedObjects& theTreatedObjects,
507 QStringList& theScript,
508 const Handle(HYDROData_Entity)& theRefObject,
509 const QString& theMethod ) const
511 if ( !checkObjectPythonDefinition( theTreatedObjects, theScript, theRefObject ) )
514 QString aRefObjName = theRefObject->GetObjPyName();
516 QString anObjName = GetObjPyName();
517 theScript << QString( "%1.%2( %3 );" )
518 .arg( anObjName ).arg( theMethod ).arg( aRefObjName );
521 bool HYDROData_Entity::checkObjectPythonDefinition( MapOfTreatedObjects& theTreatedObjects,
522 QStringList& theScript,
523 const Handle(HYDROData_Entity)& theRefObject ) const
525 if ( theRefObject.IsNull() )
528 QString aRefObjName = theRefObject->GetName();
529 if ( aRefObjName.isEmpty() )
532 if ( theTreatedObjects.contains( aRefObjName ) )
535 // The definition of reference object must be dumped before this
536 QStringList aRefObjDump = theRefObject->DumpToPython( theTreatedObjects );
537 if ( aRefObjDump.isEmpty() )
540 QStringList aTmpList = theScript;
541 theScript = aRefObjDump;
543 theScript << QString( "" );
544 theScript << aTmpList;
546 theTreatedObjects.insert( aRefObjName, theRefObject );
551 void HYDROData_Entity::setPythonObjectColor( QStringList& theScript,
552 const QColor& theColor,
553 const QColor& theDefaultColor,
554 const QString& theMethod ) const
556 if ( theColor == theDefaultColor )
557 return; //Do not set the color for object if it like default
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() );