Salome HOME
A fix for a refs #266 - Point insertion in a polyline with type spline
[modules/hydro.git] / src / HYDROCurveCreator / CurveCreator_Widget.cxx
index 6961f01b44933e1707915be691da17cc9dcdd09f..dc2aff372fe74bc9bd5d2cb5350693d558321b81 100644 (file)
@@ -343,7 +343,6 @@ void CurveCreator_Widget::updateActionsStates()
     if ( removeEnabled() )
       anEnabledAct << REMOVE_ID;
     QList<int> aSelSections = mySectionView->getSelectedSections();
-    QList< QPair< int, int > > aSelPoints = mySectionView->getSelectedPoints();
     CurveCreator_TreeView::SelectionType aSelType = mySectionView->getSelectionType();
     switch( aSelType ){
     case CurveCreator_TreeView::ST_NOSEL:{
@@ -455,13 +454,6 @@ void CurveCreator_Widget::onAdditionMode(bool checked)
   if( aSelSection.size() > 0 ){
     mySection = aSelSection[0];
   }
-  else{
-    QList< QPair<int,int> > aSelPoints = mySectionView->getSelectedPoints();
-    if( aSelPoints.size() > 0 ){
-      mySection = aSelPoints[0].first;
-      myPointNum = aSelPoints[0].second + 1;
-    }
-  }
 //  emit subOperationStarted( myNewPointEditor );
 }
 
@@ -673,16 +665,26 @@ void CurveCreator_Widget::onJoin()
   }
   stopActionMode();
 
-  int aMainSect = aSections[0];
-  int aMainSectSize = myCurve->getNbPoints(aMainSect);
-  for( int i = 1 ; i < aSections.size() ; i++ ){
-    int aSectNum = aSections[i] - (i-1);
-    myCurve->join( aMainSect, aSectNum );
-    mySectionView->sectionsRemoved( aSectNum );
+  std::list<int> aSectionsToJoin;
+  for( int i = 0; i < aSections.size() ; i++ ){
+    aSectionsToJoin.push_back( aSections[i] );
   }
+  //int aMainSect = aSectionsToJoin.front();
+  //int aMainSectSize = myCurve->getNbPoints(aMainSect);
+  if ( myCurve->join( aSectionsToJoin ) )
+  {
+    std::list<int>::const_iterator anIt = aSectionsToJoin.begin(),
+                                   aLast = aSectionsToJoin.end();
+    // the first section should be skipped. It is not removed, but is modified
+    anIt++;
+    for ( ; anIt != aLast; anIt++ )
+      mySectionView->sectionsRemoved( *anIt );
+  }
+
+  /* The update for the points of the main section
   int aNewSectSize = myCurve->getNbPoints(aMainSect);
   if( aNewSectSize != aMainSectSize )
-    mySectionView->pointsAdded( aMainSect, aMainSectSize, aNewSectSize-aMainSectSize );
+    mySectionView->pointsAdded( aMainSect, aMainSectSize, aNewSectSize-aMainSectSize );*/
   updateUndoRedo();
 }
 
@@ -719,7 +721,13 @@ void CurveCreator_Widget::onJoinAll()
   if( !myCurve )
     return;
   stopActionMode();
-  myCurve->join( 0, myCurve->getNbSections() );
+
+  std::list<int> aSectionsToJoin;
+  for( int i = 0, aNb = myCurve->getNbSections(); i < aNb ; i++ ){
+    aSectionsToJoin.push_back( i );
+  }
+  bool aRes = myCurve->join( aSectionsToJoin );
+
   mySectionView->reset();
   updateActionsStates();
   updateUndoRedo();
@@ -873,11 +881,6 @@ QList<int> CurveCreator_Widget::getSelectedSections()
   return mySectionView->getSelectedSections();
 }
 
-QList< QPair< int, int > > CurveCreator_Widget::getSelectedPoints()
-{
-  return mySectionView->getSelectedPoints();
-}
-
 /**
  * According to the widget state, performs the remove action
  */
@@ -977,7 +980,7 @@ void CurveCreator_Widget::onMouseRelease( SUIT_ViewWindow*, QMouseEvent* theEven
   if ( myDragStarted ) {
     bool isDragged = myDragged;
     CurveCreator_ICurve::SectionToPointList aDraggedPoints;
-    CurveCreator_ICurve::SectionToPointCoordsList anInitialDragPointsCoords;
+    QMap<CurveCreator_ICurve::SectionToPoint, std::deque< float > > anInitialDragPointsCoords;
     if ( myDragged ) {
       aDraggedPoints = myDragPoints;
       anInitialDragPointsCoords = myInitialDragPointsCoords;
@@ -986,7 +989,31 @@ void CurveCreator_Widget::onMouseRelease( SUIT_ViewWindow*, QMouseEvent* theEven
     setDragStarted( false );
 
     if ( aDraggedPoints.size() > 0 ) {
+      // Collect old coordinates of the dragged points
+      CurveCreator_ICurve::SectionToPointCoordsList anOldPoints;
+      foreach ( const CurveCreator_ICurve::SectionToPoint aSectionToPoint, anInitialDragPointsCoords.keys() ) {
+        CurveCreator::Coordinates aCoords = anInitialDragPointsCoords.value( aSectionToPoint );
+        anOldPoints.push_back( std::make_pair( aSectionToPoint, aCoords ) );
+      }
+
       if ( myCurve->canPointsBeSorted() ) {
+        // Add old coordinates of the curve points (except the dragged points) to the list
+        for( int aSectionId = 0 ; aSectionId < myCurve->getNbSections() ; aSectionId++ ) {
+          CurveCreator::Coordinates aCoords;
+          for ( int aPointId = 0, aNb = myCurve->getNbPoints( aSectionId ); aPointId < aNb; aPointId++ ) {
+            aCoords = myCurve->getPoint( aSectionId, aPointId );
+            if ( aCoords.size() < 2 ) {
+              continue;
+            }
+            
+            CurveCreator_ICurve::SectionToPoint aSectionToPoint = std::make_pair( aSectionId, aPointId );
+
+            if ( !anInitialDragPointsCoords.contains( aSectionToPoint ) ) {
+              anOldPoints.push_back( std::make_pair( aSectionToPoint, aCoords ) );
+            }
+          }
+        }
+        
         // Apply points sorting
         CurveCreator_ICurve::SectionToPointList aPoints;
         startCurveModification( aPoints, false );
@@ -1010,15 +1037,13 @@ void CurveCreator_Widget::onMouseRelease( SUIT_ViewWindow*, QMouseEvent* theEven
         finishCurveModification( aDraggedPoints );
       } else {
         // if the drag of some points has happened, restore the drag selection
-        if ( aDraggedPoints.size() > 0 ) {
-          START_MEASURE_TIME;
-          setSelectedPoints( aDraggedPoints );
-          END_MEASURE_TIME( "drop" );
-        }
+        START_MEASURE_TIME;
+        setSelectedPoints( aDraggedPoints );
+        END_MEASURE_TIME( "drop" );
       }
 
       // Save drag difference
-      myCurve->saveCoordDiff( anInitialDragPointsCoords );
+      myCurve->saveCoordDiff( anOldPoints );
     }
   }
   else // check whether the segment is clicked an a new point should be added to the segment
@@ -1026,6 +1051,7 @@ void CurveCreator_Widget::onMouseRelease( SUIT_ViewWindow*, QMouseEvent* theEven
 
   // updates the input panel table to show the selected point coordinates
   updateLocalPointView();
+  updateUndoRedo();
 }
 
 /**
@@ -1102,19 +1128,6 @@ void CurveCreator_Widget::removeSection()
 {
   stopActionMode();
 
-  QList< QPair<int,int> > aSelPoints = mySectionView->getSelectedPoints();
-  int aCurrSect=-1;
-  int aRemoveCnt = 0;
-  for( int i = 0 ; i < aSelPoints.size() ; i++ ){
-    if( aCurrSect != aSelPoints[i].first ){
-      aRemoveCnt = 0;
-      aCurrSect = aSelPoints[i].first;
-    }
-    int aPntIndx = aSelPoints[i].second - aRemoveCnt;
-    myCurve->removePoint( aCurrSect, aPntIndx );
-    mySectionView->pointsRemoved( aCurrSect, aPntIndx );
-    aRemoveCnt++;
-  }
   QList<int> aSections = mySectionView->getSelectedSections();
   for( int i = 0 ; i < aSections.size() ; i++ ){
     int aSectNum = aSections[i] - (i);
@@ -1191,6 +1204,7 @@ void CurveCreator_Widget::insertPointToSelectedSegment( const int theX,
   CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aPoints1.begin(),
                                                           aLast = aPoints1.end();
   int aSectionId = -1;
+  // find the indices of the neighbour point
   // there can be a case when a new point is added into two sections
   int aPoint1Id = -1, aPoint2Id = -1;
   for ( ; anIt != aLast && aSectionId < 0; anIt++ ) {
@@ -1253,9 +1267,7 @@ void CurveCreator_Widget::moveSelectedPoints( const int theXPosition,
 
     // Remember drag points coordinates
     if ( !myDragged ) {
-      myInitialDragPointsCoords.push_back(
-        std::make_pair(std::make_pair( aSectionId, aPointId ), 
-                       aChangedPos ));
+      myInitialDragPointsCoords.insert( std::make_pair( aSectionId, aPointId ), aChangedPos );
     }
 
     aChangedPos[0] = aChangedPos[0] - aXDelta;
@@ -1287,11 +1299,8 @@ void CurveCreator_Widget::updateLocalPointView()
 
   if ( !isRowLimit ) {
     bool isBlocked = myLocalPointView->blockSignals(true);
-    myLocalPointView->setRowCount( 0 );
-    CurveCreator_ICurve::SectionToPointList::const_iterator anIt = myLocalPoints.begin(),
-                                                            aLast = myLocalPoints.end();
-    for ( ; anIt != aLast; anIt++ )
-      myLocalPointView->addLocalPointToTable( *anIt );
+
+    myLocalPointView->setLocalPointsToTable( myLocalPoints );
 
     myLocalPointView->blockSignals( isBlocked );
   }