Salome HOME
refs #249 - Undo after Join selected sections operation works not properly
[modules/hydro.git] / src / HYDROCurveCreator / CurveCreator_Widget.cxx
index c97f87c4bef9278f73c27021a05155d11ce11909..a808b1406c1442132ca2f63ad7fd01e9a5813761 100644 (file)
@@ -95,6 +95,7 @@ CurveCreator_Widget::CurveCreator_Widget(QWidget* parent,
   QGroupBox* aSectionGroup = new QGroupBox(tr("Sections"),this);
 
   mySectionView = new CurveCreator_TreeView(myCurve, aSectionGroup);
+  mySectionView->setSelectionMode( QTreeView::ExtendedSelection );
   connect( mySectionView, SIGNAL(selectionChanged()), this, SLOT( onSelectionChanged() ) );
   connect( mySectionView, SIGNAL(sectionEntered(int)), this, SLOT(onEditSection(int)) );
   connect( mySectionView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onContextMenu(QPoint)) );
@@ -185,22 +186,20 @@ CurveCreator_Widget::CurveCreator_Widget(QWidget* parent,
   connect(anAct, SIGNAL(triggered()), this, SLOT(onRemove()) );
   aTB->addAction(anAct);
   
-  // TODO join
-  //aTB->addSeparator();
+  aTB->addSeparator();
 
-  //anAct = createAction( JOIN_ID, tr("JOIN"), aJoinPixmap, tr("JOIN_TLT"), 
-  //                      QKeySequence(Qt::ControlModifier|Qt::Key_Plus ) );
-  //connect( anAct, SIGNAL(triggered()), this, SLOT(onJoin()) );
-  //aTB->addAction(anAct);
+  anAct = createAction( JOIN_ID, tr("JOIN"), aJoinPixmap, tr("JOIN_TLT"), 
+                        QKeySequence(Qt::ControlModifier|Qt::Key_Plus ) );
+  connect( anAct, SIGNAL(triggered()), this, SLOT(onJoin()) );
+  aTB->addAction(anAct);
 
   anAct = createAction( CLEAR_ALL_ID, tr("CLEAR_ALL"), QPixmap(), tr("CLEAR_ALL_TLT"), 
                         QKeySequence(Qt::ControlModifier | Qt::ShiftModifier | Qt::Key_Delete ) );
   connect( anAct, SIGNAL(triggered()), this, SLOT( onClearAll()) );
 
-  // TODO join
-  //anAct = createAction( JOIN_ALL_ID, tr("JOIN_ALL"), QPixmap(), tr("JOIN_ALL_TLT"), 
-  //                      QKeySequence(Qt::ControlModifier | Qt::ShiftModifier | Qt::Key_Plus ) );
-  //connect( anAct, SIGNAL(triggered()), this, SLOT(onJoinAll()) );
+  anAct = createAction( JOIN_ALL_ID, tr("JOIN_ALL"), QPixmap(), tr("JOIN_ALL_TLT"), 
+                        QKeySequence(Qt::ControlModifier | Qt::ShiftModifier | Qt::Key_Plus ) );
+  connect( anAct, SIGNAL(triggered()), this, SLOT(onJoinAll()) );
 
   QVBoxLayout* aSectLayout = new QVBoxLayout();
   aSectLayout->setMargin( 5 );
@@ -373,18 +372,25 @@ void CurveCreator_Widget::updateActionsStates()
           int aSectCnt = myCurve->getNbSections();
           if( aSectCnt > 0 )
             anEnabledAct << CLEAR_ALL_ID;
-          // TODO
-          //if( aSectCnt > 1 )
-          //  anEnabledAct << JOIN_ALL_ID;
-          //if( aSelSections.size() > 1 ){
-          //  anEnabledAct << JOIN_ID;
-          //}
+          if( aSectCnt > 1 )
+            anEnabledAct << JOIN_ALL_ID;
+          if( aSelSections.size() > 1 ){
+            anEnabledAct << JOIN_ID;
+          }
         }
         break;
         case DetectionMode: {
         }
         break;
         case NoneMode:
+          {
+            int aSectCnt = myCurve->getNbSections();
+            if( aSectCnt > 1 )
+              anEnabledAct << JOIN_ALL_ID;
+            if( aSelSections.size() > 1 )
+              anEnabledAct << JOIN_ID;
+          }
+          break;
         default:
         break;
       }
@@ -417,10 +423,9 @@ void CurveCreator_Widget::updateActionsStates()
     if( (myCurve->getNbSections() + myCurve->getNbPoints()) > 0 ){
       anEnabledAct << REMOVE_ALL_ID;
     }*/
-    // TODO
-    //if( myCurve->getNbSections() > 1 ){
-    //  anEnabledAct << JOIN_ALL_ID;
-    //}
+    if( myCurve->getNbSections() > 1 ){
+      anEnabledAct << JOIN_ALL_ID;
+    }
   }
   QList<ActionId> anIds = myActionMap.keys();
   for( int i = 0 ; i < anIds.size() ; i++ ){
@@ -714,7 +719,7 @@ void CurveCreator_Widget::onJoinAll()
   if( !myCurve )
     return;
   stopActionMode();
-  myCurve->join();
+  myCurve->join( 0, myCurve->getNbSections()-1 );
   mySectionView->reset();
   updateActionsStates();
   updateUndoRedo();
@@ -827,7 +832,7 @@ void CurveCreator_Widget::updateUndoRedo()
 void CurveCreator_Widget::onContextMenu( QPoint thePoint )
 {
   QList<ActionId> aContextActions;
-  aContextActions << CLEAR_ALL_ID << JOIN_ALL_ID << SEPARATOR_ID <<
+  aContextActions << CLEAR_ALL_ID << JOIN_ID << JOIN_ALL_ID << SEPARATOR_ID <<
                      CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID << SET_SECTIONS_POLYLINE_ID <<
                      SET_SECTIONS_SPLINE_ID;
   QPoint aGlPoint = mySectionView->mapToGlobal(thePoint);
@@ -972,13 +977,40 @@ void CurveCreator_Widget::onMouseRelease( SUIT_ViewWindow*, QMouseEvent* theEven
   if ( myDragStarted ) {
     bool isDragged = myDragged;
     CurveCreator_ICurve::SectionToPointList aDraggedPoints;
-    if ( myDragged )
+    QMap<CurveCreator_ICurve::SectionToPoint, std::deque< float > > anInitialDragPointsCoords;
+    if ( myDragged ) {
       aDraggedPoints = myDragPoints;
+      anInitialDragPointsCoords = myInitialDragPointsCoords;
+    }
 
     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 );
@@ -992,22 +1024,23 @@ void CurveCreator_Widget::onMouseRelease( SUIT_ViewWindow*, QMouseEvent* theEven
           int aSectionId = anIt->first;
           int aPointId = anIt->second;
           std::deque<float> aPos = myCurve->getPoint( aSectionId, aPointId );
-    
+
           aCoordList.push_back(
             std::make_pair( std::make_pair( aSectionId, aPointId ), aPos ) );
         }
 
-        myCurve->setSeveralPoints( aCoordList );
+        myCurve->setSeveralPoints( aCoordList, false );
     
         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( anOldPoints );
     }
   }
   else // check whether the segment is clicked an a new point should be added to the segment
@@ -1239,6 +1272,12 @@ void CurveCreator_Widget::moveSelectedPoints( const int theXPosition,
     aChangedPos = myCurve->getPoint( aSectionId, aPointId );
     if ( aChangedPos.size() < 2 )
       continue;
+
+    // Remember drag points coordinates
+    if ( !myDragged ) {
+      myInitialDragPointsCoords.insert( std::make_pair( aSectionId, aPointId ), aChangedPos );
+    }
+
     aChangedPos[0] = aChangedPos[0] - aXDelta;
     aChangedPos[1] = aChangedPos[1] - anYDelta;
     
@@ -1246,7 +1285,7 @@ void CurveCreator_Widget::moveSelectedPoints( const int theXPosition,
       std::make_pair(std::make_pair( aSectionId, aPointId ), 
                      aChangedPos ));
   }
-  myCurve->setSeveralPoints( aCoordList );
+  myCurve->setSeveralPoints( aCoordList, false );
 
   myDragged = true;
   finishCurveModification( myDragPoints );
@@ -1268,11 +1307,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 );
   }
@@ -1311,6 +1347,7 @@ void CurveCreator_Widget::setDragStarted( const bool theState, const QPoint& the
       changeInteractionStyle( myDragInteractionStyle );
     myDragStarted = false;
     myDragPoints.clear();
+    myInitialDragPointsCoords.clear();
   }
   myDragged = false;
 }