Salome HOME
refs #636: flags for geometry 2d/3d change
[modules/hydro.git] / src / HYDROData / HYDROData_Object.cxx
index 1be3985a854702b26b2b9339a9e2dead81fe6972..fff074adebd0d85ed9f4dbb78b5ae42b49b1de85 100644 (file)
+// Copyright (C) 2014-2015  EDF-R&D
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
 #include "HYDROData_Object.h"
 
+#include "HYDROData_DummyObject3D.h"
+#include "HYDROData_ShapesGroup.h"
+#include "HYDROData_Tool.h"
 #include "HYDROData_Iterator.h"
+#include "HYDROData_IAltitudeObject.h"
 
-#include <TDataStd_Name.hxx>
-#include <TDataStd_ByteArray.hxx>
-#include <TDataStd_UAttribute.hxx>
-#include <TDataStd_IntegerArray.hxx>
-#include <TDataStd_BooleanArray.hxx>
-#include <TDataStd_RealArray.hxx>
-#include <TDataStd_ReferenceList.hxx>
+#include <TNaming_Builder.hxx>
+#include <TNaming_NamedShape.hxx>
 
-#include <TDF_CopyLabel.hxx>
-#include <TDF_ListIteratorOfLabelList.hxx>
+#include <TopoDS_Shape.hxx>
+#include <TDataStd_Integer.hxx>
 
-#include <QString>
-#include <QStringList>
-#include <QVariant>
+#include <QColor>
 
-IMPLEMENT_STANDARD_HANDLE(HYDROData_Object,MMgt_TShared)
-IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Object,MMgt_TShared)
+IMPLEMENT_STANDARD_HANDLE(HYDROData_Object,HYDROData_Entity)
+IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Object,HYDROData_Entity)
 
-// is equal function for unique object mapping
-bool IsEqual(const Handle_HYDROData_Object& theObj1, const Handle_HYDROData_Object& theObj2)
+HYDROData_Object::HYDROData_Object( Geometry theGeometry )
+  : HYDROData_Entity( theGeometry )
 {
-  return (theObj1->ID() == theObj2->ID());
 }
 
-QString HYDROData_Object::GetName() const
+HYDROData_Object::~HYDROData_Object()
 {
-  Handle(TDataStd_Name) aName;
-  if (myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
-    TCollection_AsciiString aStr(aName->Get());
-    return QString(aStr.ToCString());
-  }
-  return QString();
 }
 
-void HYDROData_Object::SetName(const QString& theName)
+void HYDROData_Object::SetName( const QString& theName )
 {
-  TDataStd_Name::Set(myLab, TCollection_ExtendedString(theName.toLatin1().constData()));
+  QString anOldObjName = GetName();
+  if ( anOldObjName != theName )
+  {
+    HYDROData_SequenceOfObjects aGroups = GetGroups();
+    HYDROData_SequenceOfObjects::Iterator anIter( aGroups );
+    for ( ; anIter.More(); anIter.Next() )
+    {
+      Handle(HYDROData_ShapesGroup) aGroup =
+        Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
+      if ( aGroup.IsNull() )
+        continue;
+
+      HYDROData_Tool::UpdateChildObjectName( anOldObjName, theName, aGroup );
+    }
+
+    Handle(HYDROData_DummyObject3D) anObject3D = GetObject3D();
+    if ( !anObject3D.IsNull() )
+      HYDROData_Tool::UpdateChildObjectName( anOldObjName, theName, anObject3D );
+
+    Handle(HYDROData_IAltitudeObject) anAltitudeObj = getChildAltitudeObject();
+    if ( !anAltitudeObj.IsNull() )
+      HYDROData_Tool::UpdateChildObjectName( anOldObjName, theName, anAltitudeObj );
+  }
+
+  HYDROData_Entity::SetName( theName );
 }
 
-QStringList HYDROData_Object::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
+void HYDROData_Object::Update()
 {
-  QStringList anEmptyList;
-  return anEmptyList;
+  if( IsMustBeUpdated2d() )
+  {
+    removeTopShape();
+    removeGroupObjects();
+  }
+  if( IsMustBeUpdated3d() )
+    removeShape3D();
+
+  checkAndSetAltitudeObject();
+  HYDROData_Entity::Update();
 }
 
-void HYDROData_Object::Update( const bool theIsForce )
+HYDROData_SequenceOfObjects HYDROData_Object::GetAllReferenceObjects() const
 {
+  HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
+
+  Handle(HYDROData_IAltitudeObject) aRefAltitude = GetAltitudeObject();
+  if ( !aRefAltitude.IsNull() )
+    aResSeq.Append( aRefAltitude );
+
+  return aResSeq;
 }
 
-QVariant HYDROData_Object::GetDataVariant()
+void HYDROData_Object::Changed( Geometry theChangedGeometry )
 {
-  return QVariant();
+  HYDROData_Entity::Changed( theChangedGeometry );
+
+  Handle(HYDROData_DummyObject3D) anObject3D = GetObject3D();
+  if ( !anObject3D.IsNull() )
+    anObject3D->Changed( theChangedGeometry );
 }
 
-bool HYDROData_Object::IsRemoved() const
+void HYDROData_Object::SetTopShape( const TopoDS_Shape& theShape )
 {
-  return !myLab.HasAttribute();
+  TNaming_Builder aBuilder( myLab.FindChild( DataTag_TopShape ) );
+  aBuilder.Generated( theShape );
 }
 
-void HYDROData_Object::Remove()
+void HYDROData_Object::SetShape3D( const TopoDS_Shape& theShape )
 {
-  return myLab.ForgetAllAttributes(Standard_True);
+  TNaming_Builder aBuilder( myLab.FindChild( DataTag_Shape3D ) );
+  aBuilder.Generated( theShape );
+  
+  // Check the object 3D existance
+  checkAndSetObject3D();
 }
 
-HYDROData_Object::HYDROData_Object()
+Handle(HYDROData_DummyObject3D) HYDROData_Object::GetObject3D() const
 {
+  Handle(HYDROData_DummyObject3D) anObject;
+  
+  TDF_Label aLabel = myLab.FindChild( DataTag_Object3D, false );
+  if ( !aLabel.IsNull() )
+  {
+    TDF_Label aChildLabel = aLabel.FindChild( 0, false );
+    if ( !aChildLabel.IsNull() )
+    {
+      anObject = Handle(HYDROData_DummyObject3D)::DownCast(
+        HYDROData_Iterator::Object( aChildLabel ) );
+    }
+  }
+
+  return anObject;
 }
 
-HYDROData_Object::~HYDROData_Object()
+void HYDROData_Object::checkAndSetObject3D()
 {
+  TDF_Label aLabel = myLab.FindChild( DataTag_Object3D, false );
+  if ( !aLabel.IsNull() )
+    return;
+
+  TDF_Label aChildLabel = myLab.FindChild( DataTag_Object3D ).FindChild( 0 );
+  HYDROData_Iterator::CreateObject( aChildLabel, KIND_DUMMY_3D );
 }
 
-void HYDROData_Object::CopyTo(Handle_HYDROData_Object theDestination) const
+HYDROData_SequenceOfObjects HYDROData_Object::GetGroups() const
 {
-  TDF_CopyLabel aCopy(myLab, theDestination->Label());
-  aCopy.Perform();
+  return GetReferenceObjects( DataTag_EdgesGroup );
 }
 
-void HYDROData_Object::SetLabel(TDF_Label theLabel)
+Handle(HYDROData_ShapesGroup) HYDROData_Object::GetGroup( const int theGroupId ) const
 {
-  myLab = theLabel;
+  Handle(HYDROData_ShapesGroup) aResGroup;
+
+  HYDROData_SequenceOfObjects aGroups = GetGroups();
+  if ( theGroupId < 0 || theGroupId >= aGroups.Length() )
+    return aResGroup;
+
+  aResGroup = Handle(HYDROData_ShapesGroup)::DownCast( aGroups.Value( theGroupId + 1 ) );
+
+  return aResGroup;
 }
 
-void HYDROData_Object::SaveByteArray(const int theTag, 
-  const char* theData, const int theLen)
+int HYDROData_Object::GetGroupId( const Handle(HYDROData_ShapesGroup)& theGroup ) const
 {
-  TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
-  // array is empty, remove the attribute
-  if (theLen <= 0) {
-    aLab.ForgetAttribute(TDataStd_ByteArray::GetID());
-    return;
-  }
-  // store data of image in byte array
-  Handle(TDataStd_ByteArray) aData;
-  if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData)) {
-    aData = TDataStd_ByteArray::Set(aLab, 1, theLen);
-  }
-  // copy bytes one by one
-  if (aData->Length() != theLen) {
-    Handle(TColStd_HArray1OfByte) aNewData = new TColStd_HArray1OfByte(1, theLen);
-    for(int a = 0; a < theLen; a++)
-      aNewData->SetValue(a + 1, theData[a]);
-    aData->ChangeArray(aNewData);
-  } else {
-    for(int a = 0; a < theLen; a++)
-      aData->SetValue(a + 1, theData[a]);
+  int aRes = -1;
+
+  HYDROData_SequenceOfObjects aGroups = GetGroups();
+  for ( int i = 1, n = aGroups.Length(); i <= n; ++i )
+  {
+    Handle(HYDROData_ShapesGroup) aGroup =
+      Handle(HYDROData_ShapesGroup)::DownCast( aGroups.Value( i ) );
+    if ( IsEqual( theGroup, aGroup ) )
+    {
+      aRes = i - 1;
+      break;
+    }
   }
+
+  return aRes;
 }
 
-const char* HYDROData_Object::ByteArray(const int theTag, int& theLen) const
+bool HYDROData_Object::SetAltitudeObject( 
+  const Handle(HYDROData_IAltitudeObject)& theAltitude )
 {
-  TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
-  Handle(TDataStd_ByteArray) aData;
-  if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData))
-    return NULL; // return empty image if there is no array
-  theLen = aData->Length();
-  if (theLen)
-    return (const char*)(&(aData->InternalArray()->ChangeArray1().ChangeValue(1)));
-  return NULL;
+  if ( theAltitude.IsNull() )
+    return false;
+  
+  Handle(HYDROData_IAltitudeObject) aPrevAltitude = GetAltitudeObject();
+  if ( IsEqual( aPrevAltitude, theAltitude ) )
+    return true;
+
+  SetReferenceObject( theAltitude, DataTag_AltitudeObject );
+
+  // #636: In the case of the altitude object change the geometry of the main object is not changed,
+  // to the object should not be marked as updated
+  //SetToUpdate( true );
+
+  return true;
 }
 
-int HYDROData_Object::NbReferenceObjects( const int theTag ) const
+Handle(HYDROData_IAltitudeObject) HYDROData_Object::GetAltitudeObject() const
 {
-  Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
-  return aRefs.IsNull() ? 0 : aRefs->Extent();
+  return Handle(HYDROData_IAltitudeObject)::DownCast( 
+           GetReferenceObject( DataTag_AltitudeObject ) );
 }
 
-void HYDROData_Object::AddReferenceObject( const Handle_HYDROData_Object& theObj,
-                                           const int                      theTag )
+void HYDROData_Object::RemoveAltitudeObject()
 {
-  if ( theObj.IsNull() )
+  Handle(HYDROData_IAltitudeObject) aPrevAltitude = GetAltitudeObject();
+  if ( aPrevAltitude.IsNull() )
     return;
 
-  Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
-  aRefs->Append( theObj->Label() );
+  ClearReferenceObjects( DataTag_AltitudeObject );
+
+  // #636: In the case of the altitude object change the geometry of the main object is not changed,
+  // to the object should not be marked as updated
+  //SetToUpdate( true );
 }
 
-void HYDROData_Object::SetReferenceObject( const Handle_HYDROData_Object& theObj,
-                                           const int                      theTag,
-                                           const int                      theIndex )
+void HYDROData_Object::SetFillingColor( const QColor& theColor )
 {
-  if ( theObj.IsNull() )
-    return;
+  SetColor( theColor, DataTag_FillingColor );
+}
 
-  Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
+QColor HYDROData_Object::GetFillingColor() const
+{
+  return GetColor( getDefaultFillingColor(), DataTag_FillingColor );
+}
 
-  if ( theIndex >= aRefs->Extent() )
-  {
-    aRefs->Append( theObj->Label() );
-  }
-  else if ( theIndex < 0 )
+void HYDROData_Object::SetBorderColor( const QColor& theColor )
+{
+  SetColor( theColor, DataTag_BorderColor );
+}
+
+QColor HYDROData_Object::GetBorderColor() const
+{
+  return GetColor( getDefaultBorderColor(), DataTag_BorderColor );
+}
+
+QColor HYDROData_Object::getDefaultFillingColor() const
+{
+  return QColor( Qt::yellow );
+}
+
+QColor HYDROData_Object::getDefaultBorderColor() const
+{
+  return QColor( Qt::transparent );
+}
+
+QStringList HYDROData_Object::dumpObjectCreation( MapOfTreatedObjects& theTreatedObjects ) const
+{
+  QStringList aResList = HYDROData_Entity::dumpObjectCreation( theTreatedObjects );
+  if ( aResList.isEmpty() )
+    return aResList; //Object was not created
+
+  QStringList aColorsDef;
+
+  QColor aFillingColor = GetFillingColor();
+  setPythonObjectColor( aColorsDef, aFillingColor, getDefaultFillingColor(), "SetFillingColor" );
+
+  QColor aBorderColor = GetBorderColor();
+  setPythonObjectColor( aColorsDef, aBorderColor, getDefaultBorderColor(), "SetBorderColor" );
+
+  if ( !aColorsDef.isEmpty() )
   {
-    aRefs->Prepend( theObj->Label() );
+    aResList << aColorsDef;
+    aResList << QString( "" );
   }
-  else
-  {
-    RemoveReferenceObject( theTag, theIndex );
 
-    Handle(HYDROData_Object) aBeforeObj = GetReferenceObject( theTag, theIndex );
+  return aResList;
+}
+
+ObjectKind HYDROData_Object::getAltitudeObjectType() const
+{
+  return KIND_UNKNOWN;
+}
 
-    aRefs = getReferenceList( theTag, true ); // because reference list can be removed
-    if ( !aBeforeObj.IsNull() )
-      aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
-    else 
-      aRefs->Append( theObj->Label() );
+Handle(HYDROData_IAltitudeObject) HYDROData_Object::getChildAltitudeObject() const
+{
+  Handle(HYDROData_IAltitudeObject) anObject;
+  
+  TDF_Label aLabel = myLab.FindChild( DataTag_ChildAltitudeObject, false );
+  if ( !aLabel.IsNull() )
+  {
+    TDF_Label aChildLabel = aLabel.FindChild( 0, false );
+    if ( !aChildLabel.IsNull() )
+    {
+      anObject = Handle(HYDROData_IAltitudeObject)::DownCast(
+        HYDROData_Iterator::Object( aChildLabel ) );
+    }
   }
+
+  return anObject;
 }
 
-Handle(HYDROData_Object) HYDROData_Object::GetReferenceObject( const int theTag,
-                                                               const int theIndex ) const
+void HYDROData_Object::checkAndSetAltitudeObject()
 {
-  Handle(HYDROData_Object) aRes;
+  Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
+
+  ObjectKind anAltitudeObjectType = getAltitudeObjectType();
+  if ( anAltitudeObjectType == KIND_UNKNOWN )
+    return; // No need to create altitude object
 
-  Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
-  if ( aRefs.IsNull() || theIndex < 0 || theIndex >= aRefs->Extent() )
-    return aRes;
+  TDF_Label aLabel = myLab.FindChild( DataTag_ChildAltitudeObject, false );
+  if ( !aLabel.IsNull() )
+    return;
 
-  TDF_ListIteratorOfLabelList anIter( aRefs->List() );
-  for ( int anIndex = 0; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
+  TDF_Label aChildLabel = myLab.FindChild( DataTag_ChildAltitudeObject ).FindChild( 0 );
 
-  const TDF_Label& aRefLabel = anIter.Value();
-  aRes = HYDROData_Iterator::Object( aRefLabel );
+  Handle(HYDROData_IAltitudeObject) anAltitudeObject =
+    Handle(HYDROData_IAltitudeObject)::DownCast(
+      HYDROData_Iterator::CreateObject( aChildLabel, anAltitudeObjectType ) );
 
-  return aRes;
+  QString anAltitudePref = GetName() + "_Altitude";
+  QString anAltitudeName = HYDROData_Tool::GenerateObjectName( aDocument, anAltitudePref );
+  anAltitudeObject->SetName( anAltitudeName );
+
+  SetAltitudeObject( anAltitudeObject );
 }
 
-HYDROData_SequenceOfObjects HYDROData_Object::GetReferenceObjects( const int theTag ) const
+Handle(HYDROData_ShapesGroup) HYDROData_Object::createGroupObject()
 {
-  HYDROData_SequenceOfObjects aRes;
+  TDF_Label aNewLab = myLab.FindChild( DataTag_EdgesGroup ).NewChild();
 
-  Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
-  if ( aRefs.IsNull() )
-    return aRes;
+  Handle(HYDROData_ShapesGroup) aNewGroup =
+    Handle(HYDROData_ShapesGroup)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_SHAPES_GROUP ) );
+  AddReferenceObject( aNewGroup, DataTag_EdgesGroup );
 
-  TDF_ListIteratorOfLabelList anIter( aRefs->List() );
-  for ( ; anIter.More(); anIter.Next() )
-  {
-    const TDF_Label& aRefLabel = anIter.Value();
+  return aNewGroup;
+}
 
-    Handle(HYDROData_Object) aRefObject = HYDROData_Iterator::Object( aRefLabel );
-    if ( aRefObject.IsNull() )
-      continue;
+void HYDROData_Object::removeGroupObjects()
+{
+  TDF_Label aLabel = myLab.FindChild( DataTag_EdgesGroup, false );
+  if ( !aLabel.IsNull() )
+    aLabel.ForgetAllAttributes();
+}
 
-    aRes.Append( aRefObject );
+TopoDS_Shape HYDROData_Object::getTopShape() const
+{
+  TDF_Label aLabel = myLab.FindChild( DataTag_TopShape, false );
+  if ( !aLabel.IsNull() )
+  {
+    Handle(TNaming_NamedShape) aNamedShape;
+    if( aLabel.FindAttribute( TNaming_NamedShape::GetID(), aNamedShape ) )
+      return aNamedShape->Get();
   }
 
-  return aRes;
+  return TopoDS_Shape();
 }
 
-void HYDROData_Object::RemoveReferenceObject( const int theTag,
-                                              const int theIndex )
+void HYDROData_Object::removeTopShape()
 {
-  Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
-  if ( aRefs.IsNull() )
-    return;
+  TDF_Label aLabel = myLab.FindChild( DataTag_TopShape, false );
+  if ( !aLabel.IsNull() )
+    aLabel.ForgetAllAttributes();
+}
 
-  if ( aRefs->Extent() == 1 && theIndex == 0 )
-  { 
-    // remove all if only one
-    ClearReferenceObjects( theTag );
-    return;
+TopoDS_Shape HYDROData_Object::getShape3D() const
+{
+  TDF_Label aLabel = myLab.FindChild( DataTag_Shape3D, false );
+  if ( !aLabel.IsNull() )
+  {
+    Handle(TNaming_NamedShape) aNamedShape;
+    if( aLabel.FindAttribute( TNaming_NamedShape::GetID(), aNamedShape ) )
+      return aNamedShape->Get();
   }
 
-  Handle(HYDROData_Object) aRemovedObj = GetReferenceObject( theTag, theIndex );
-  if ( aRemovedObj.IsNull() )
-    return;
-
-  aRefs->Remove( aRemovedObj->Label() );
+  return TopoDS_Shape();
 }
 
-void HYDROData_Object::ClearReferenceObjects( const int theTag )
+void HYDROData_Object::removeShape3D()
 {
-  TDF_Label aSetLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
-  aSetLabel.ForgetAttribute( TDataStd_ReferenceList::GetID() );
+  TDF_Label aLabel = myLab.FindChild( DataTag_Shape3D, false );
+  if ( !aLabel.IsNull() )
+    aLabel.ForgetAllAttributes();
 }
 
-Handle(TDataStd_ReferenceList) HYDROData_Object::getReferenceList( const int theTag,
-                                                                   const bool theIsCreate ) const
+bool HYDROData_Object::IsSubmersible() const
 {
-  TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
+  Handle(TDataStd_Integer) aSubMersibleAttr;
 
-  Handle(TDataStd_ReferenceList) aRefs;
-  if ( !aLabel.FindAttribute( TDataStd_ReferenceList::GetID(), aRefs ) && theIsCreate )
-    aRefs = TDataStd_ReferenceList::Set( aLabel );
+  bool isSubmersible = true; //default
+  if( myLab.FindAttribute(TDataStd_Integer::GetID(), aSubMersibleAttr ) )
+  {
+    int aValue = aSubMersibleAttr->Get();
+    isSubmersible = ( aValue != 0 );
+  }
+  return isSubmersible;
+}
 
-  return aRefs;
+void HYDROData_Object::SetIsSubmersible( bool isSubmersible ) const
+{
+  TDataStd_Integer::Set( myLab, isSubmersible ? 1 : 0 );
 }