Salome HOME
Merge branch 'BR_H2018_3' into BR_2018_V8_5
[modules/hydro.git] / src / HYDROData / HYDROData_IPolyline.cxx
old mode 100755 (executable)
new mode 100644 (file)
index 00f7a04..ddd6908
@@ -1,20 +1,37 @@
+// 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_IPolyline.h"
 
-#include "HYDROData_Document.h"
+#include <gp_XY.hxx>
 
 #include <TDataStd_BooleanList.hxx>
 #include <TDataStd_ExtStringList.hxx>
-#include <TDataStd_Integer.hxx>
 #include <TDataStd_IntegerList.hxx>
 #include <TDataStd_RealList.hxx>
+#include <TDataStd_IntegerArray.hxx>
+#include <TopoDS_Shape.hxx>
+#include <QColor>
 
-IMPLEMENT_STANDARD_HANDLE(HYDROData_IPolyline, HYDROData_Entity)
 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_IPolyline, HYDROData_Entity)
 
 HYDROData_IPolyline::HYDROData_IPolyline()
-: HYDROData_Entity(),
-  myIsOperation( false )
+: HYDROData_Entity( Geom_2d )
 {
 }
 
@@ -22,169 +39,43 @@ HYDROData_IPolyline::~HYDROData_IPolyline()
 {
 }
 
-int HYDROData_IPolyline::getNbUndo() const
+void HYDROData_IPolyline::SetWireColor( const QColor& theColor )
 {
-  return getNbOperations();
+  //SetColor( theColor, DataTag_WireColor ); //DEPRECATED;
+  //set theColor for all sections
+  int nbSec = NbSections();
+  for (int i = 0; i < nbSec; i++)
+    SetSectionColor(i, theColor);
 }
 
-bool HYDROData_IPolyline::undo()
+QColor HYDROData_IPolyline::GetWireColor() const
 {
-  Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
-  if ( aDocument.IsNull() )
-    return false;
-
-  if ( getNbUndo() < 1 )
-    return false;
-
-  aDocument->Undo();
-
-  int aNbUndoedOperations = getNbUndoedOperations();
-  setNbUndoedOperations( aNbUndoedOperations + 1 ); 
-
-  return true;
-}
-
-int HYDROData_IPolyline::getNbRedo() const
-{
-  return getNbUndoedOperations();
+  return GetColor( DefaultWireColor(), DataTag_WireColor );
 }
 
-bool HYDROData_IPolyline::redo()
+QColor HYDROData_IPolyline::DefaultWireColor()
 {
-  Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
-  if ( aDocument.IsNull() )
-    return false;
-
-  if ( getNbRedo() < 1 )
-    return false;
-
-  aDocument->Redo();
-
-  int aNbUndoedOperations = getNbUndoedOperations();
-  setNbUndoedOperations( aNbUndoedOperations - 1 ); 
-
-  return true;
+  return QColor( Qt::black );
 }
 
-CurveCreator::Dimension HYDROData_IPolyline::getDimension() const
+int HYDROData_IPolyline::NbPoints( const int theSectionIndex ) const
 {
-  return CurveCreator::Dim2d;
+  return GetPoints( theSectionIndex ).Length();
 }
 
-CurveCreator::Coordinates HYDROData_IPolyline::getPoint( const int theISection, 
-                                                         const int theIPnt ) const
+TopoDS_Shape HYDROData_IPolyline::GetShape() const
 {
-  CurveCreator::Coordinates aResPoint;
-  if ( theISection < 0 || theIPnt < 0 )
-    return aResPoint;
-
-  CurveCreator::Coordinates aSectPoints = getPoints( theISection );
-  if ( theIPnt >= aSectPoints.size() )
-    return aResPoint;
-
-  CurveCreator::Coordinates::reverse_iterator anIt = aSectPoints.rbegin();
-
-  aResPoint.push_back( *anIt++ );
-  aResPoint.push_back( *anIt );
-
-  return aResPoint;
+  return HYDROData_Entity::GetShape( DataTag_PolylineShape );
 }
 
-
-int HYDROData_IPolyline::getNbPoints( const int theSectionIndex ) const
+void HYDROData_IPolyline::SetShape( const TopoDS_Shape& theShape )
 {
-  return getPoints( theSectionIndex ).size();
+  HYDROData_Entity::SetShape( DataTag_PolylineShape, theShape );
 }
 
-void HYDROData_IPolyline::startOperation()
+void HYDROData_IPolyline::RemovePolylineShape()
 {
-  if ( myIsOperation )
-    return;
-
-  Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
-  if ( aDocument.IsNull() )
-    return;
-  
-  aDocument->StartOperation();
-  
-  myIsOperation = true;
-}
-
-void HYDROData_IPolyline::commitOperation()
-{
-  if ( !myIsOperation )
-    return;
-
-  Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
-  if ( aDocument.IsNull() )
-    return;
-
-  int aNbOperations = getNbOperations();
-  setNbOperations( aNbOperations + 1 ); 
-
-  aDocument->CommitOperation();
-
-  myIsOperation = false;
-}
-
-void HYDROData_IPolyline::abortOperation()
-{
-  if ( !myIsOperation )
-    return;
-
-  Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
-  if ( aDocument.IsNull() )
-    return;
-
-  aDocument->AbortOperation();
-
-  myIsOperation = false;
-}
-
-void HYDROData_IPolyline::setNbOperations( const int theNb )
-{
-  TDF_Label anOpLabel = myLab.FindChild( DataTag_Operations ).FindChild( OperationsTag_Commited );
-  TDataStd_Integer::Set( anOpLabel, theNb );
-}
-
-int HYDROData_IPolyline::getNbOperations() const
-{
-  TDF_Label aLabel = myLab.FindChild( DataTag_Operations, false );
-  if ( aLabel.IsNull() )
-    return 0;
-
-  TDF_Label anOpLabel = aLabel.FindChild( OperationsTag_Commited, false );
-  if ( anOpLabel.IsNull() )
-    return 0;
-
-  Handle(TDataStd_Integer) anOpCount;
-  if ( !anOpLabel.FindAttribute( TDataStd_Integer::GetID(), anOpCount ) )
-    return 0;
-
-  return anOpCount->Get();
-}
-
-void HYDROData_IPolyline::setNbUndoedOperations( const int theNb )
-{
-  TDF_Label anUndoedLabel = myLab.FindChild( DataTag_Operations ).FindChild( OperationsTag_Undoed );
-  TDataStd_Integer::Set( anUndoedLabel, theNb );
-}
-
-int HYDROData_IPolyline::getNbUndoedOperations() const
-{
-  TDF_Label aLabel = myLab.FindChild( DataTag_Operations, false );
-  if ( aLabel.IsNull() )
-    return 0;
-
-  TDF_Label anUndoedLabel = aLabel.FindChild( OperationsTag_Undoed, false );
-  if ( anUndoedLabel.IsNull() )
-    return 0;
-
-  Handle(TDataStd_Integer) anUndoedCount;
-  if ( !anUndoedLabel.FindAttribute( TDataStd_Integer::GetID(), anUndoedCount ) )
-    return 0;
-
-  return anUndoedCount->Get();
+  SetShape( TopoDS_Shape() );
 }
 
 void HYDROData_IPolyline::getSectionsLists( Handle(TDataStd_ExtStringList)& theNamesList,
@@ -192,21 +83,31 @@ void HYDROData_IPolyline::getSectionsLists( Handle(TDataStd_ExtStringList)& theN
                                             Handle(TDataStd_BooleanList)&   theClosuresList,
                                             const bool                      theIsCreate ) const
 {
-  TDF_Label aSectsLabel = myLab.FindChild( DataTag_Sections, theIsCreate );
-  if ( aSectsLabel.IsNull() )
+  theNamesList.Nullify();
+  theTypesList.Nullify();
+  theClosuresList.Nullify();
+
+  TDF_Label aSectLabel = myLab.FindChild( DataTag_Sections, theIsCreate );
+  if ( aSectLabel.IsNull() )
     return;
 
-  Handle(TDataStd_ExtStringList) aNamesList;
-  if ( !aSectsLabel.FindAttribute( TDataStd_ExtStringList::GetID(), aNamesList ) && theIsCreate )
-    aNamesList = TDataStd_ExtStringList::Set( aSectsLabel );
+  if ( !aSectLabel.FindAttribute( TDataStd_ExtStringList::GetID(), theNamesList ) && theIsCreate )
+  {
+    theNamesList = TDataStd_ExtStringList::Set( aSectLabel );
+    theNamesList->SetID(TDataStd_ExtStringList::GetID());
+  }
 
-  Handle(TDataStd_IntegerList) aTypesList;
-  if ( !aSectsLabel.FindAttribute( TDataStd_IntegerList::GetID(), aTypesList ) && theIsCreate )
-    aTypesList = TDataStd_IntegerList::Set( aSectsLabel );
+  if ( !aSectLabel.FindAttribute( TDataStd_IntegerList::GetID(), theTypesList ) && theIsCreate )
+  {
+    theTypesList = TDataStd_IntegerList::Set( aSectLabel );
+    theTypesList->SetID(TDataStd_IntegerList::GetID());
+  }
 
-  Handle(TDataStd_BooleanList) aClosuresList;
-  if ( !aSectsLabel.FindAttribute( TDataStd_BooleanList::GetID(), aClosuresList ) && theIsCreate )
-    aClosuresList = TDataStd_BooleanList::Set( aSectsLabel );
+  if ( !aSectLabel.FindAttribute( TDataStd_BooleanList::GetID(), theClosuresList ) && theIsCreate )
+  {
+    theClosuresList = TDataStd_BooleanList::Set( aSectLabel );
+    theClosuresList->SetID(TDataStd_BooleanList::GetID());
+  }
 }
 
 void HYDROData_IPolyline::removeSectionsLists()
@@ -236,24 +137,91 @@ void HYDROData_IPolyline::getPointsLists( const int                  theSectionI
   if ( !aLabelX.IsNull() )
   {
     if ( !aLabelX.FindAttribute( TDataStd_RealList::GetID(), theListX ) && theIsCreate )
+    {
       theListX = TDataStd_RealList::Set( aLabelX );
+      theListX->SetID(TDataStd_RealList::GetID());
+    }
   }
 
   TDF_Label aLabelY = aSectLabel.FindChild( 1, theIsCreate );
   if ( !aLabelY.IsNull() )
   {
     if ( !aLabelY.FindAttribute( TDataStd_RealList::GetID(), theListY ) && theIsCreate )
+    {
       theListY = TDataStd_RealList::Set( aLabelY );
+      theListY->SetID(TDataStd_RealList::GetID());
+    }
   }
 }
 
-void HYDROData_IPolyline::removePointsLists( const int theSectionIndex )
+void HYDROData_IPolyline::removePointsLists( const int theSectionIndex ) const
 {
   TDF_Label aLabel = myLab.FindChild( DataTag_Points, false );
   if ( aLabel.IsNull() )
     return;
 
-  if ( theSectionIndex == -1 )
+  if ( theSectionIndex < 0 )
+  {
+    aLabel.ForgetAllAttributes();
+  }
+  else
+  {
+    TDF_Label aSectLabel = aLabel.FindChild( theSectionIndex, false );
+    if ( !aSectLabel.IsNull() )
+      aSectLabel.ForgetAllAttributes();
+  }
+}
+
+bool HYDROData_IPolyline::GetSectionColor( const int theSectionIndex, QColor &theColor) const
+{
+  TDF_Label aLabel = myLab.FindChild( DataTag_SectionColors, false );
+  if ( aLabel.IsNull() )
+    return false;
+
+  TDF_Label aSectLabel = aLabel.FindChild( theSectionIndex, false );
+  if ( aSectLabel.IsNull() )
+    return false;
+
+  Handle(TDataStd_IntegerArray) aColorArray;
+
+  if ( aSectLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
+  {
+    theColor.setRed(   aColorArray->Value( 1 ) );
+    theColor.setGreen( aColorArray->Value( 2 ) );
+    theColor.setBlue(  aColorArray->Value( 3 ) );
+    theColor.setAlpha( aColorArray->Value( 4 ) );
+  }
+}
+
+void HYDROData_IPolyline::SetSectionColor( const int theSectionIndex,
+                                           const QColor& theColor )
+{
+  TDF_Label aLabel = myLab.FindChild( DataTag_SectionColors, true );
+  if ( aLabel.IsNull() )
+    return;
+
+  TDF_Label aSectLabel = aLabel.FindChild( theSectionIndex, true );
+  if ( aSectLabel.IsNull() )
+    return;
+
+  Handle(TDataStd_IntegerArray) aColorArray;
+
+  if ( !aSectLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
+    aColorArray = TDataStd_IntegerArray::Set( aSectLabel, 1, 4 );
+
+  aColorArray->SetValue( 1, theColor.red()   );
+  aColorArray->SetValue( 2, theColor.green() );
+  aColorArray->SetValue( 3, theColor.blue()  );
+  aColorArray->SetValue( 4, theColor.alpha() );
+}
+
+void HYDROData_IPolyline::removeSectionColor( const int theSectionIndex ) const
+{
+  TDF_Label aLabel = myLab.FindChild( DataTag_SectionColors, false );
+  if ( aLabel.IsNull() )
+    return;
+
+  if ( theSectionIndex < 0 )
   {
     aLabel.ForgetAllAttributes();
   }
@@ -264,3 +232,14 @@ void HYDROData_IPolyline::removePointsLists( const int theSectionIndex )
       aSectLabel.ForgetAllAttributes();
   }
 }
+
+ void HYDROData_IPolyline::setPythonPolylineSectionColor( QStringList&  theScript,
+                                                          const int     theSectIndex,
+                                                          const QColor& theColor ) const
+{
+  QString anObjName = GetObjPyName();
+  theScript << QString( "%1.SetSectionColor( %2, QColor( %3, %4, %5, %6 ) )" )
+              .arg( anObjName ).arg( theSectIndex )
+              .arg( theColor.red()  ).arg( theColor.green() )
+              .arg( theColor.blue() ).arg( theColor.alpha() );
+}