Salome HOME
Ref #250 - Fatal error after Join all selections operation
[modules/hydro.git] / src / HYDROCurveCreator / CurveCreator_Curve.cxx
index 3f88a8b2bddda6622673f0070e50d193fa438794..53576dbc04a494505ea208a9a9f38f3ce500c2b3 100644 (file)
@@ -51,7 +51,8 @@ CurveCreator_Curve::CurveCreator_Curve( const CurveCreator::Dimension theDimensi
   myNbUndos   (0),
   myNbRedos   (0),
   myUndoDepth (-1),
-  myOpLevel(0)
+  myOpLevel(0),
+  mySkipSorting(false)
 {
 }
 
@@ -257,11 +258,13 @@ bool CurveCreator_Curve::moveSectionInternal(const int theISection,
                                              const int theNewIndex)
 {
   bool res = false;
-  if (theISection != theNewIndex) {
-    CurveCreator_Section *aSection = mySections.at(theISection);
+  int aMovedSectionId = theISection >= 0 ? theISection : mySections.size()-1;
+
+  if (aMovedSectionId != theNewIndex) {
+    CurveCreator_Section *aSection = mySections.at(aMovedSectionId);
 
     // Remove section
-    CurveCreator::Sections::iterator anIter = mySections.begin() + theISection;
+    CurveCreator::Sections::iterator anIter = mySections.begin() + aMovedSectionId;
 
     mySections.erase(anIter);
 
@@ -294,25 +297,6 @@ bool CurveCreator_Curve::moveSection(const int theISection,
   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   ************/
 
 /***********************************************/
@@ -393,7 +377,7 @@ bool CurveCreator_Curve::clear()
   startOperation();
   // Set the difference.
   if (addEmptyDiff()) {
-    myListDiffs.back().init(this, CurveCreator_Operation::Clear);
+    myListDiffs.back().init(this);
   }
   res = clearInternal();
   finishOperation();
@@ -401,34 +385,46 @@ bool CurveCreator_Curve::clear()
 }
 
 //! For internal use only! Undo/Redo are not used here.
-bool CurveCreator_Curve::joinInternal( const int theISectionTo, 
-                                       const int theISectionFrom )
+bool CurveCreator_Curve::joinInternal( const std::list<int>& theSections )
 {
   bool res = false;
-  CurveCreator_Section *aSection1 = mySections.at(theISectionTo);
-  CurveCreator_Section *aSection2 = mySections.at(theISectionFrom);
-
-  aSection1->myPoints.insert(aSection1->myPoints.end(),
-                             aSection2->myPoints.begin(), 
-                             aSection2->myPoints.end());
+  if ( theSections.empty() )
+    return res;
+
+  int anISectionMain = theSections.front();
+  CurveCreator_Section* aSectionMain = mySections.at( anISectionMain );
+  std::list <int> aSectionsToJoin = theSections;
+  aSectionsToJoin.erase( aSectionsToJoin.begin() ); // skip the main section
+  // it is important to sort and reverse the section ids in order to correctly remove them
+  aSectionsToJoin.sort();
+  aSectionsToJoin.reverse();
+
+  std::list<int>::const_iterator anIt = aSectionsToJoin.begin(), aLast = aSectionsToJoin.end();
+  CurveCreator_Section* aSection;
+  for (; anIt != aLast; anIt++) {
+    aSection = mySections.at(*anIt);
+    aSectionMain->myPoints.insert(aSectionMain->myPoints.end(), aSection->myPoints.begin(),
+                                  aSection->myPoints.end());
+    res = removeSectionInternal(*anIt);
+    if ( !res )
+      break;
+  }
 
-  res = removeSection(theISectionFrom);
   redisplayCurve();
   return res;
 }
 
-//! 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 )
+bool CurveCreator_Curve::join( const std::list<int>& theSections )
 {
-  //TODO
   bool res = false;
-  if ( theISectionTo != theISectionFrom ) {
+
+  if ( !theSections.empty() )
+  {
     startOperation();
     if (addEmptyDiff())
-      myListDiffs.back().init(this, CurveCreator_Operation::Join, theISectionTo, theISectionFrom);
+      myListDiffs.back().init(this, CurveCreator_Operation::Join, theSections);
 
-    res = joinInternal( theISectionTo, theISectionFrom );
+    res = joinInternal( theSections );
 
     finishOperation();
   }
@@ -559,6 +555,30 @@ int CurveCreator_Curve::getNbPoints( const int theISection ) const
   return aNbCoords/myDimension;
 }
 
+void CurveCreator_Curve::setSkipSorting( const bool theIsToSkip )
+{
+  mySkipSorting = theIsToSkip;
+}
+
+bool CurveCreator_Curve::canPointsBeSorted()
+{
+  return false;
+}
+
+/**
+ * Saves points coordinates difference.
+ * \param theOldCoords the old points coordinates
+ */
+void CurveCreator_Curve::saveCoordDiff( const SectionToPointCoordsList &theOldCoords )
+{
+  // Set the difference.
+  startOperation();
+  if (addEmptyDiff()) {
+    myListDiffs.back().init(this, theOldCoords);
+  }
+  finishOperation();
+}
+
 //! Get "closed" flag of the specified section
 bool CurveCreator_Curve::isClosed( const int theISection ) const
 {
@@ -814,12 +834,13 @@ bool CurveCreator_Curve::setPoint( const int theISection,
 }
 
 //! Set coordinates of specified points from different sections
-bool CurveCreator_Curve::setSeveralPoints( const SectionToPointCoordsList &theSectionToPntCoords)
+bool CurveCreator_Curve::setSeveralPoints( const SectionToPointCoordsList &theSectionToPntCoords,
+                                           const bool theIsToSaveDiff )
 {
   bool res = false;
   // Set the difference.
   startOperation();
-  if (addEmptyDiff()) {
+  if (theIsToSaveDiff && addEmptyDiff()) {
     myListDiffs.back().init(this, CurveCreator_Operation::SetCoordinates,
                             theSectionToPntCoords);
   }
@@ -850,34 +871,19 @@ bool CurveCreator_Curve::setSeveralPoints( const SectionToPointCoordsList &theSe
 //! For internal use only! Undo/Redo are not used here.
 bool CurveCreator_Curve::removePointsInternal( const SectionToPointList &thePoints )
 {
-  bool res = false;
+  bool aRes = false;
   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(aPntIndx == -1)
-          aFirstPosition = aSection->myPoints.end() - getDimension();
-        else
-          aFirstPosition = aSection->myPoints.begin() + toICoord(aPntIndx);
-        aSection->myPoints.erase( aFirstPosition, aFirstPosition + getDimension() );
-        res = true;
-      }
-    }
+    aRes = removeSectionPoints(aSectionId, anIt->second);
   }
-  if(res)
+  if( aRes)
     redisplayCurve();
-  return res;
+
+  return aRes;
 }
 
 //! Remove point with given id
@@ -886,12 +892,12 @@ bool CurveCreator_Curve::removePoint( const int theISection, const int theIPnt )
   bool res = false;
   // Set the difference.
   startOperation();
+  SectionToPointList aListOfSectionsToPoints;
+  aListOfSectionsToPoints.push_back(std::make_pair(theISection, theIPnt));
   if (addEmptyDiff()) {
     myListDiffs.back().init(this, CurveCreator_Operation::RemovePoints,
-                            theISection, theIPnt);
+                            aListOfSectionsToPoints);
   }
-  SectionToPointList aListOfSectionsToPoints;
-  aListOfSectionsToPoints.push_back(std::make_pair(theISection, theIPnt));
   res = removePointsInternal( aListOfSectionsToPoints );
   finishOperation();
   return res;
@@ -956,4 +962,48 @@ Handle(AIS_InteractiveObject) CurveCreator_Curve::getAISObject( const bool theNe
     aCurve->constructAISObject();
   }
   return myAISShape;
-}
\ No newline at end of file
+}
+
+bool CurveCreator_Curve::removeSectionPoints( const int theSectionId,
+                                              const std::list<int>& thePointIds )
+{
+  bool aRes = false;
+
+  CurveCreator_Section *aSection = mySections.at( theSectionId );
+  if ( !aSection )
+    return aRes;
+
+  std::list<int> aSectionPoints = thePointIds;
+  aSectionPoints.sort();
+  std::list<int>::const_reverse_iterator aPntIt = aSectionPoints.rbegin();
+  for ( ; aPntIt != aSectionPoints.rend(); aPntIt++ ) {
+    int aPntIndx = *aPntIt;
+    CurveCreator::Coordinates::iterator aFirstPosition;
+    if ( aPntIndx == -1 )
+      aFirstPosition = aSection->myPoints.end() - getDimension();
+    else
+      aFirstPosition = aSection->myPoints.begin() + toICoord( aPntIndx );
+    aSection->myPoints.erase( aFirstPosition, aFirstPosition + getDimension() );
+    aRes = true;
+  }
+  return aRes;
+}
+
+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;
+  }
+}