Salome HOME
Ref #250 - Fatal error after Join all selections operation
[modules/hydro.git] / src / HYDROCurveCreator / CurveCreator_Curve.cxx
index 0c6c45be4b9bb85032d42816ef96cd575d0aabac..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);
 
@@ -374,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();
@@ -382,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();
   }
@@ -540,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
 {
@@ -795,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);
   }
@@ -852,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;