Salome HOME
OCC functionality moving out from the widget
[modules/hydro.git] / src / HYDROCurveCreator / CurveCreator_Curve.cxx
index 69b0c38d4fc207f525c5bbc4949f1ba4c95cdfd8..25939c148716e0bf549d00a75a4e8cb04a5838e5 100644 (file)
@@ -23,6 +23,7 @@
 #include "CurveCreator_Curve.hxx"
 
 #include "CurveCreator.hxx"
+#include "CurveCreator_PosPoint.hxx"
 #include "CurveCreator_Section.hxx"
 #include "CurveCreator_Displayer.h"
 
@@ -42,6 +43,8 @@
 
 #include <stdio.h>
 
+//#define AIS_CURVE_DISPLAY
+
 //=======================================================================
 // function: Constructor
 // purpose:
@@ -55,6 +58,9 @@ CurveCreator_Curve::CurveCreator_Curve( const CurveCreator::Dimension theDimensi
   myUndoDepth (-1),
   myOpLevel(0)
 {
+#ifdef AIS_CURVE_DISPLAY
+  myAISCurve = new CurveCreator_AISCurve( this );
+#endif
 }
 
 //=======================================================================
@@ -104,6 +110,19 @@ std::string CurveCreator_Curve::getUniqSectionName() const
 void CurveCreator_Curve::setDisplayer( CurveCreator_Displayer* theDisplayer )
 {
   myDisplayer = theDisplayer;
+  
+#ifdef AIS_CURVE_DISPLAY
+  myDisplayer->displayAIS( myAISCurve, false );
+#endif
+}
+
+//=======================================================================
+// function: getDisplayer
+// purpose: get curve changes Displayer
+//=======================================================================
+CurveCreator_Displayer* CurveCreator_Curve::getDisplayer()
+{
+  return myDisplayer;
 }
 
 //=======================================================================
@@ -342,8 +361,73 @@ void CurveCreator_Curve::getCoordinates( int theISection, int theIPoint, double&
 void CurveCreator_Curve::redisplayCurve()
 {
   if( myDisplayer ) {
-    myDisplayer->erase();
-    myDisplayer->display( constructWire() );
+#ifdef AIS_CURVE_DISPLAY
+    myDisplayer->redisplayAIS(myAISCurve, false);
+#else
+    myDisplayer->erase( false );
+    myDisplayer->display( constructWire(), true );
+#endif
+  }
+}
+
+//! For internal use only! Undo/Redo are not used here.
+bool CurveCreator_Curve::moveSectionInternal(const int theISection,
+                                             const int theNewIndex)
+{
+  bool res = false;
+  if (theISection != theNewIndex) {
+    CurveCreator_Section *aSection = mySections.at(theISection);
+
+    // Remove section
+    CurveCreator::Sections::iterator anIter = mySections.begin() + theISection;
+
+    mySections.erase(anIter);
+
+    // Insert section.
+    anIter = mySections.begin() + theNewIndex;
+    mySections.insert(anIter, aSection);
+    res = true;
+  }
+  return res;
+}
+
+//=======================================================================
+// function: moveSection
+// purpose:
+//=======================================================================
+bool CurveCreator_Curve::moveSection(const int theISection,
+                                     const int theNewIndex)
+{
+  bool res = false;
+  // Set the difference.
+  startOperation();
+  if (addEmptyDiff()) {
+    myListDiffs.back().init(this, CurveCreator_Operation::MoveSection,
+                            theISection, theNewIndex);
+  }
+
+  // Update the curve.
+  res = moveSectionInternal(theISection, theNewIndex);
+  finishOperation();
+  return res;
+}
+
+void CurveCreator_Curve::convert( const SectionToPointList& thePoints,
+                                  std::map< int, std::list<int> >& theConvPoints )
+{
+  theConvPoints.clear();
+
+  SectionToPointList::const_iterator anIt = thePoints.begin(), aLast = thePoints.end();
+  std::list<int> aPoints;
+  int aSectionId, aPointId;
+  for ( ; anIt != aLast; anIt++ ) {
+    aSectionId = anIt->first;
+    aPointId = anIt->second;
+    aPoints.clear();
+    if ( theConvPoints.find( aSectionId ) != theConvPoints.end() )
+      aPoints = theConvPoints[aSectionId];
+    aPoints.push_back( aPointId );
+    theConvPoints[aSectionId] = aPoints;
   }
 }
 
@@ -401,7 +485,7 @@ bool CurveCreator_Curve::clearInternal()
 {
   // erase curve from the viewer
   if( myDisplayer )
-    myDisplayer->erase();
+    myDisplayer->erase( true );
   // Delete all allocated data.
   int i = 0;
   const int aNbSections = getNbSections();
@@ -449,12 +533,13 @@ bool CurveCreator_Curve::joinInternal( const int theISectionTo,
   return res;
 }
 
-//! Join range of sections to one section (join all sections if -1 is passed in one of arguments)
+//! Join range of sections to one section (join all sections if -1 is passed in theISectionFrom argument)
 bool CurveCreator_Curve::join( const int theISectionTo, 
                                const int theISectionFrom )
 {
+  //TODO
   bool res = false;
-  if (theISectionTo != theISectionFrom) {
+  if ( theISectionTo != theISectionFrom ) {
     startOperation();
     if (addEmptyDiff())
       myListDiffs.back().init(this, CurveCreator_Operation::Join, theISectionTo, theISectionFrom);
@@ -583,7 +668,8 @@ int CurveCreator_Curve::getNbPoints( const int theISection ) const
       aNbCoords += mySections[i]->myPoints.size();
     }
   } else {
-    aNbCoords = mySections.at(theISection)->myPoints.size();
+    if ( ( theISection >= 0 ) && ( theISection < mySections.size() ) )
+      aNbCoords = mySections.at(theISection)->myPoints.size();
   }
 
   return aNbCoords/myDimension;
@@ -724,35 +810,35 @@ bool CurveCreator_Curve::setSectionType( const int theISection,
 /***********************************************/
 
 //! For internal use only! Undo/Redo are not used here.
-bool CurveCreator_Curve::addPointsInternal( const CurveCreator::Coordinates& theCoords,
-                                            const std::vector<int> &theISections,
-                                            const std::vector<int> &theIPnts  )
+bool CurveCreator_Curve::addPointsInternal( const CurveCreator::SectionsMap &theSectionsMap )
 {
   bool res = false;
-  if( (theCoords.size()/getDimension()) == theISections.size() == theIPnts.size() ) {
-    for( int ind = 0; ind < theISections.size(); ind++ ) {
-      int anISection = theISections.at(ind);
-      CurveCreator_Section *aSection =
-        (anISection == -1 ? mySections.back() : mySections.at(anISection));
-
-      if( aSection ) {
-        int anIPnt = theIPnts.at(ind);
+  CurveCreator::SectionsMap::const_iterator anIt = theSectionsMap.begin();
+  CurveCreator_Section *aSection = 0;
+  for ( ; anIt != theSectionsMap.end(); anIt++ ) {
+    int anISection = anIt->first;
+    aSection = mySections.at(anISection);
+    if( aSection ) {
+      CurveCreator::PosPointsList aSectionPoints = anIt->second;
+      CurveCreator::PosPointsList::const_iterator aPntIt = aSectionPoints.begin();
+      for( ; aPntIt != aSectionPoints.end(); aPntIt++ ){
+        int anIPnt = (*aPntIt)->myID;
+        CurveCreator::Coordinates aCoords = (*aPntIt)->myCoords;
         CurveCreator::Coordinates::iterator anIterPosition;
         if(anIPnt == -1)
           anIterPosition = aSection->myPoints.end();
         else
           anIterPosition = aSection->myPoints.begin() + toICoord(anIPnt);
         CurveCreator::Coordinates::const_iterator aFirstPosition = 
-          theCoords.begin() + toICoord(ind);        
+          aCoords.begin();
         aSection->myPoints.insert(anIterPosition,
-                                  aFirstPosition, 
-                                  aFirstPosition + getDimension());
-        res = true;
+                                  aCoords.begin(), aCoords.end());
       }
+      res = true;
     }
-    if(res)
-      redisplayCurve();
   }
+  if(res)
+    redisplayCurve();
   return res;
 }
 
@@ -769,34 +855,48 @@ bool CurveCreator_Curve::addPoints( const CurveCreator::Coordinates& theCoords,
   // Set the difference.
   startOperation();
   if (addEmptyDiff()) {
+    CurveCreator_ICurve::SectionToPointCoordsList aList;
+    aList.push_back(std::make_pair(std::make_pair(theISection, theIPnt), theCoords));
     myListDiffs.back().init(this, CurveCreator_Operation::InsertPoints,
-                            theCoords, theISection, theIPnt);
+                            aList);
   }
-  std::vector<int> anISections, anIPnts;
-  anISections.push_back( theISection );
-  anIPnts.push_back( theIPnt );
-  res = addPointsInternal( theCoords, anISections, anIPnts );
+  CurveCreator::SectionsMap aSectionsMap;
+  CurveCreator::PosPointsList aPoints;
+  CurveCreator_PosPoint* aPosPoint = new CurveCreator_PosPoint( theIPnt, theCoords );
+  aPoints.push_back( aPosPoint );
+  aSectionsMap[theISection] = aPoints;
+
+  res = addPointsInternal( aSectionsMap );
+
   finishOperation();
   return res;
 }
 
 //! For internal use only! Undo/Redo are not used here.
-bool CurveCreator_Curve::setPointInternal( const int theISection,
-                                           const int theIPnt,
-                                           const CurveCreator::Coordinates& theNewCoords )
+bool CurveCreator_Curve::setPointInternal( const CurveCreator::SectionsMap &theSectionsMap )
 {
   bool res = false;
   // Update the curve.
-  if (theNewCoords.size() == myDimension) {
-    CurveCreator_Section *aSection = mySections.at(theISection);
-    int i;
-    for (i = 0; i < myDimension; i++) {
-      aSection->myPoints.at(toICoord(theIPnt) + i) = theNewCoords[i];
+  CurveCreator::SectionsMap::const_iterator anIt = theSectionsMap.begin();
+  CurveCreator_Section *aSection = 0;
+  for ( ; anIt != theSectionsMap.end(); anIt++ ) {
+    int anISection = anIt->first;
+    aSection = mySections.at(anISection);
+    if( aSection ) { 
+      CurveCreator::PosPointsList aSectionPoints = anIt->second;
+      CurveCreator::PosPointsList::const_iterator aPntIt = aSectionPoints.begin();
+      for( ; aPntIt != aSectionPoints.end(); aPntIt++ ){
+        int anIPnt = (*aPntIt)->myID;
+        CurveCreator::Coordinates aCoords = (*aPntIt)->myCoords;
+        for ( int i = 0; i < myDimension; i++)
+          aSection->myPoints.at(toICoord(anIPnt) + i) = aCoords[i];
+      }
+      res = true;
     }
-    redisplayCurve();
-    
-    res = true;
   }
+  if(res)
+    redisplayCurve();
+  
   return res;
 }
 
@@ -804,43 +904,95 @@ bool CurveCreator_Curve::setPointInternal( const int theISection,
 bool CurveCreator_Curve::setPoint( const int theISection,
                                    const int theIPnt,
                                    const CurveCreator::Coordinates& theNewCoords )
+{
+  bool res = false;
+  // Set the difference.
+  startOperation();
+  if (addEmptyDiff()) {
+    CurveCreator_ICurve::SectionToPointCoordsList aList;
+    aList.push_back(std::make_pair(std::make_pair(theISection, theIPnt), theNewCoords));
+    myListDiffs.back().init(this, CurveCreator_Operation::SetCoordinates,
+                            aList);
+  }
+  CurveCreator::SectionsMap aSectionsMap;
+  CurveCreator::PosPointsList aPoints;
+  CurveCreator_PosPoint* aPosPoint = new CurveCreator_PosPoint( theIPnt, theNewCoords );
+  aPoints.push_back( aPosPoint );
+  aSectionsMap[theISection] = aPoints;
+
+  int aSize1 = getNbPoints( theISection );
+  res = setPointInternal( aSectionsMap );
+  int aSize2 = getNbPoints( theISection );
+
+  finishOperation();
+
+  return res; 
+}
+
+//! Set coordinates of specified points from different sections
+bool CurveCreator_Curve::setSeveralPoints( const SectionToPointCoordsList &theSectionToPntCoords)
 {
   bool res = false;
   // Set the difference.
   startOperation();
   if (addEmptyDiff()) {
     myListDiffs.back().init(this, CurveCreator_Operation::SetCoordinates,
-                            theNewCoords, theISection, theIPnt);
+                            theSectionToPntCoords);
+  }
+  CurveCreator::SectionsMap aSectionsMap;
+  CurveCreator::PosPointsList aPosPoints;
+  CurveCreator_ICurve::SectionToPointCoordsList::const_iterator anIt = 
+    theSectionToPntCoords.begin(), aLast = theSectionToPntCoords.end();
+  int aSectionId, aPointId;
+  for ( ; anIt != aLast; anIt++ ) {
+    aPosPoints.clear();
+    aSectionId = anIt->first.first;
+    aPointId = anIt->first.second;
+    CurveCreator::Coordinates aNewCoords = anIt->second;
+    CurveCreator_PosPoint* aPosPoint = 
+      new CurveCreator_PosPoint( aPointId, aNewCoords );
+    if( aSectionsMap.find(aSectionId) != aSectionsMap.end() )
+      aPosPoints = aSectionsMap[aSectionId];
+    aPosPoints.push_back( aPosPoint );
+    aSectionsMap[aSectionId] = aPosPoints;
+    
   }
-  res = setPointInternal( theISection, theIPnt, theNewCoords );
+  res = setPointInternal( aSectionsMap );
   finishOperation();
 
   return res; 
 }
 
 //! For internal use only! Undo/Redo are not used here.
-bool CurveCreator_Curve::removePointsInternal( const std::vector<int> &theISections, 
-                                               const std::vector<int> &theIPnts )
+bool CurveCreator_Curve::removePointsInternal( const SectionToPointList &thePoints )
 {
   bool res = false;
-  if( theISections.size() == theIPnts.size() ) {
-    for( int ind = 0; ind < theISections.size(); ind++ ) {
-      int anISection = theISections.at(ind);
-      CurveCreator_Section *aSection = mySections.at(anISection);
-      if( aSection ) {
-        int anIPnt = theIPnts.at(ind);
+  std::map<int, std::list<int> > aConvPoints;
+  convert( thePoints, aConvPoints );
+  std::map<int, std::list<int> >::const_iterator anIt = aConvPoints.begin(),
+                                          aLast = aConvPoints.end();
+  CurveCreator_Section *aSection = 0;
+  for ( ; anIt != aLast; anIt++ ) {
+    int aSectionId = anIt->first;
+    aSection = mySections.at(aSectionId);
+    if( aSection ) {
+      std::list<int> aSectionPoints = anIt->second;
+      aSectionPoints.sort();
+      std::list<int>::const_reverse_iterator aPntIt = aSectionPoints.rbegin();
+      for( ; aPntIt != aSectionPoints.rend(); aPntIt++ ){
+        int aPntIndx = *aPntIt;
         CurveCreator::Coordinates::iterator aFirstPosition;
-        if(anIPnt == -1)
-          aFirstPosition = aSection->myPoints.end();
+        if(aPntIndx == -1)
+          aFirstPosition = aSection->myPoints.end() - getDimension();
         else
-          aFirstPosition = aSection->myPoints.begin() + toICoord(anIPnt);
+          aFirstPosition = aSection->myPoints.begin() + toICoord(aPntIndx);
         aSection->myPoints.erase( aFirstPosition, aFirstPosition + getDimension() );
         res = true;
       }
     }
-    if(res)
-      redisplayCurve();
   }
+  if(res)
+    redisplayCurve();
   return res;
 }
 
@@ -854,15 +1006,29 @@ bool CurveCreator_Curve::removePoint( const int theISection, const int theIPnt )
     myListDiffs.back().init(this, CurveCreator_Operation::RemovePoints,
                             theISection, theIPnt);
   }
-  std::vector<int> anISections, anIPnts;
-  anISections.push_back( theISection );
-  anIPnts.push_back( theIPnt );
-  res = removePointsInternal( anISections, anIPnts );
+  SectionToPointList aListOfSectionsToPoints;
+  aListOfSectionsToPoints.push_back(std::make_pair(theISection, theIPnt));
+  res = removePointsInternal( aListOfSectionsToPoints );
   finishOperation();
   return res;
 }
 
-//=======================================================================
+//! Remove several points from different sections with given ids
+bool CurveCreator_Curve::removeSeveralPoints( const SectionToPointList &theSectionToPntIDs)
+{
+  bool res = false;
+  // Set the difference.
+  startOperation();
+  if (addEmptyDiff()) {
+    myListDiffs.back().init(this, CurveCreator_Operation::RemovePoints,
+                            theSectionToPntIDs);
+  }
+  res = removePointsInternal( theSectionToPntIDs );
+  finishOperation();
+  return res;
+}
+
+  //=======================================================================
 // function: getCoordinates
 // purpose:
 //=======================================================================
@@ -883,7 +1049,12 @@ CurveCreator::Coordinates CurveCreator_Curve::getPoint( const int theISection,
 //=======================================================================
 CurveCreator::Coordinates CurveCreator_Curve::getPoints( const int theISection ) const
 {
-  return mySections.at(theISection)->myPoints;
+  CurveCreator::Coordinates aCoords;
+  if ( ( theISection >= 0 ) && ( theISection < mySections.size() ) )
+  {
+    aCoords = mySections.at(theISection)->myPoints;
+  }
+  return aCoords;
 }