Salome HOME
Double editor in table
[modules/hydro.git] / src / HYDROCurveCreator / CurveCreator_Curve.cxx
index 69b0c38d4fc207f525c5bbc4949f1ba4c95cdfd8..824338b29055919aa2f048b9efc3a97e2ef0724e 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"
 
@@ -106,6 +107,15 @@ void CurveCreator_Curve::setDisplayer( CurveCreator_Displayer* theDisplayer )
   myDisplayer = theDisplayer;
 }
 
+//=======================================================================
+// function: getDisplayer
+// purpose: get curve changes Displayer
+//=======================================================================
+CurveCreator_Displayer* CurveCreator_Curve::getDisplayer()
+{
+  return myDisplayer;
+}
+
 //=======================================================================
 // function: removeDisplayer
 // purpose: remove the attached Displayer
@@ -347,6 +357,67 @@ void CurveCreator_Curve::redisplayCurve()
   }
 }
 
+//! 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;
+  }
+}
+
 /************   Implementation of INTERFACE methods   ************/
 
 /***********************************************/
@@ -454,12 +525,15 @@ bool CurveCreator_Curve::join( const int theISectionTo,
                                const int theISectionFrom )
 {
   bool res = false;
-  if (theISectionTo != theISectionFrom) {
+  if (mySections.size() >= 2 && 
+    ( theISectionTo != theISectionFrom || (theISectionTo == -1 && theISectionFrom == -1) )) {
     startOperation();
+    int aISectionTo = getNbSections()-1;
+    int aISectionFrom = 0;
     if (addEmptyDiff())
-      myListDiffs.back().init(this, CurveCreator_Operation::Join, theISectionTo, theISectionFrom);
+      myListDiffs.back().init(this, CurveCreator_Operation::Join, aISectionTo, aISectionFrom);
 
-    res = joinInternal( theISectionTo, theISectionFrom );
+    res = joinInternal( aISectionTo, aISectionFrom );
 
     finishOperation();
   }
@@ -583,7 +657,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 +799,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 +844,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;
 }
 
@@ -809,38 +898,90 @@ bool CurveCreator_Curve::setPoint( const int theISection,
   // 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,
-                            theNewCoords, theISection, theIPnt);
+                            aList);
   }
-  res = setPointInternal( theISection, theIPnt, theNewCoords );
+  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,
+                            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( 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 +995,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 +1038,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;
 }