Salome HOME
Typo-fix by Kunda
[modules/geom.git] / src / CurveCreator / CurveCreator_Widget.cxx
index f63b71f31b70a90e11d91f4ff68707771138bf1b..ae0c67d91ba8641a3eec5353e2e24c029bc19eac 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2013-2015  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2013-2016  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -19,8 +19,6 @@
 
 #include "CurveCreator_Widget.h"
 #include "CurveCreator_TreeView.h"
-#include "CurveCreator_ICurve.hxx"
-#include "CurveCreator.hxx"
 #include "CurveCreator_NewSectionDlg.h"
 #include "CurveCreator_Utils.hxx"
 #include "CurveCreator_UtilsICurve.hxx"
@@ -48,6 +46,9 @@
 #include <QApplication>
 #include <QTableWidget>
 #include <QTime>
+#include <QSplitter>
+
+#include <utilities.h>
 
 //#define MEASURE_TIME
 
@@ -119,6 +120,7 @@ CurveCreator_Widget::CurveCreator_Widget(QWidget* parent,
   QPixmap aSplinePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_SPLINE")));
   QPixmap aRemovePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_DELETE")));
   QPixmap aJoinPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_JOIN")));
+  QPixmap aBringTogetherPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_BRING_TOGETHER")));
   QPixmap aStepUpPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_ARROW_UP")));
   QPixmap aStepDownPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_ARROW_DOWN")));
 
@@ -161,9 +163,9 @@ CurveCreator_Widget::CurveCreator_Widget(QWidget* parent,
   anAct->setCheckable(true);
   connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onDetectionMode(bool)) );
   connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
-  if ( !(theActionFlags & DisableDetectionMode) ) {
-    aTB->addAction(anAct);
-  }
+//  if ( !(theActionFlags & DisableDetectionMode) ) {
+//    aTB->addAction(anAct);
+//  }
   
   anAct = createAction( CLOSE_SECTIONS_ID, tr("CLOSE_SECTIONS"), QPixmap(), tr("CLOSE_SECTIONS_TLT"), 
                         QKeySequence(Qt::ControlModifier|Qt::Key_W) );
@@ -182,18 +184,23 @@ CurveCreator_Widget::CurveCreator_Widget(QWidget* parent,
                         tr("SET_SECTIONS_SPLINE_TLT"), QKeySequence(Qt::ControlModifier|Qt::Key_R) );
   connect(anAct, SIGNAL(triggered()), this, SLOT(onSetSpline()) );
 
+  aTB->addSeparator();
+
   anAct = createAction( REMOVE_ID, tr("REMOVE"), aRemovePixmap, tr("REMOVE_TLT"), 
                         QKeySequence(Qt::ControlModifier|Qt::Key_Delete ) );
   connect(anAct, SIGNAL(triggered()), this, SLOT(onRemove()) );
   aTB->addAction(anAct);
   
-  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( BRING_TOGETHER_ID, tr("BRING_TOGETHER"), aBringTogetherPixmap, tr("BRING_TOGETHER_TLT"),
+                        QKeySequence(Qt::ControlModifier|Qt::Key_Equal ) );
+  connect( anAct, SIGNAL(triggered()), this, SLOT(onBringTogether()) );
+  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()) );
@@ -206,8 +213,14 @@ CurveCreator_Widget::CurveCreator_Widget(QWidget* parent,
   aSectLayout->setMargin( 5 );
   aSectLayout->setSpacing( 5 );
   aSectLayout->addWidget(aTB);
-  aSectLayout->addWidget(mySectionView);
-  aSectLayout->addWidget( myLocalPointView );
+
+  QSplitter* aSplitter = new QSplitter(aSectionGroup);
+  aSplitter->setOrientation(Qt::Vertical);
+
+  aSplitter->addWidget(mySectionView);
+  aSplitter->addWidget( myLocalPointView );
+
+  aSectLayout->addWidget(aSplitter);
   aSectionGroup->setLayout(aSectLayout);
   QVBoxLayout* aLay = new QVBoxLayout();
   aLay->setMargin( 0 );
@@ -318,6 +331,7 @@ int CurveCreator_Widget::changeInteractionStyle( int theStyle )
 //=======================================================================
 void CurveCreator_Widget::reset()
 {
+  stopActionMode();
 }
 
 void CurveCreator_Widget::setCurve( CurveCreator_ICurve* theCurve )
@@ -377,7 +391,7 @@ void CurveCreator_Widget::updateActionsStates()
           if( aSectCnt > 1 )
             anEnabledAct << JOIN_ALL_ID;
           if( aSelSections.size() > 1 ){
-            anEnabledAct << JOIN_ID;
+            anEnabledAct << JOIN_ID << BRING_TOGETHER_ID;
           }
         }
         break;
@@ -618,8 +632,6 @@ void CurveCreator_Widget::onModifySection()
   mySectionView->sectionChanged(mySection);
   updateUndoRedo();
   onCancelSection();
-
-  emit curveModified();
 }
 
 void CurveCreator_Widget::onJoin()
@@ -653,8 +665,6 @@ void CurveCreator_Widget::onJoin()
   if( aNewSectSize != aMainSectSize )
     mySectionView->pointsAdded( aMainSect, aMainSectSize, aNewSectSize-aMainSectSize );*/
   updateUndoRedo();
-
-  emit curveModified();
 }
 
 void CurveCreator_Widget::onRemove()
@@ -683,8 +693,6 @@ void CurveCreator_Widget::onClearAll()
   mySectionView->reset();
   updateActionsStates();
   updateUndoRedo();
-  
-  emit curveModified();
 }
 
 void CurveCreator_Widget::onJoinAll()
@@ -702,8 +710,6 @@ void CurveCreator_Widget::onJoinAll()
   mySectionView->reset();
   updateActionsStates();
   updateUndoRedo();
-
-  emit curveModified();
 }
 
 void CurveCreator_Widget::onUndoSettings()
@@ -722,8 +728,6 @@ void CurveCreator_Widget::onSetSpline()
     mySectionView->sectionChanged(aSelSections[i]);
   }
   updateUndoRedo();
-
-  emit curveModified();
 }
 
 void CurveCreator_Widget::onSetPolyline()
@@ -737,8 +741,6 @@ void CurveCreator_Widget::onSetPolyline()
     mySectionView->sectionChanged( aSelSections[i] );
   }
   updateUndoRedo();
-
-  emit curveModified();
 }
 
 void CurveCreator_Widget::onCloseSections()
@@ -752,8 +754,6 @@ void CurveCreator_Widget::onCloseSections()
     mySectionView->sectionChanged(aSelSections[i]);
   }
   updateUndoRedo();
-
-  emit curveModified();
 }
 
 void CurveCreator_Widget::onUncloseSections()
@@ -767,8 +767,6 @@ void CurveCreator_Widget::onUncloseSections()
     mySectionView->sectionChanged(aSelSections[i]);
   }
   updateUndoRedo();
-
-  emit curveModified();
 }
 
 void CurveCreator_Widget::onUndo()
@@ -781,8 +779,6 @@ void CurveCreator_Widget::onUndo()
   myCurve->undo();
   finishCurveModification();
   mySectionView->reset();
-
-  emit curveModified();
 }
 
 void CurveCreator_Widget::onRedo()
@@ -794,8 +790,6 @@ void CurveCreator_Widget::onRedo()
   myCurve->redo();
   finishCurveModification();
   mySectionView->reset();
-
-  emit curveModified();
 }
 
 void CurveCreator_Widget::updateUndoRedo()
@@ -960,7 +954,7 @@ void CurveCreator_Widget::SetViewer2DMode(const bool To2D)
 
 //=================================================================================
 // function : GeometryGUI::addCoordsByClick()
-// purpose  : Manage mouse press events in Additon mode
+// purpose  : Manage mouse press events in Addition mode
 //=================================================================================
 void CurveCreator_Widget::addCoordsByClick( QMouseEvent* pe )
 {
@@ -973,17 +967,10 @@ void CurveCreator_Widget::addCoordsByClick( QMouseEvent* pe )
       return;
 
     gp_Pnt aPnt;    
+    OCCViewer_ViewPort3d* vp = getViewPort();
 
-    ic->InitSelected();
-    if ( pe->modifiers() == Qt::ShiftModifier )
-      ic->ShiftSelect();  // Append selection
-    else
-      ic->Select();       // New selection
+    aPnt = CurveCreator_Utils::ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
 
-    {
-      OCCViewer_ViewPort3d* vp = getViewPort();
-      aPnt = CurveCreator_Utils::ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
-    }
     // set the coordinates into dialog
     CurveCreator::Coordinates aCoords;
     aCoords.push_back( aPnt.X() );
@@ -995,6 +982,68 @@ void CurveCreator_Widget::addCoordsByClick( QMouseEvent* pe )
   }
 }
 
+/**
+ * Set the same coordinates on two section extremities.
+ * Usage: build a closed contour with several sections.
+ * Works with two points selected, at the extremities of different sections.
+ * The second point gets the coordinates of the first.
+ */
+void CurveCreator_Widget::onBringTogether()
+{
+  MESSAGE("onBringTogether");
+  ActionMode aMode = getActionMode();
+  if ( aMode != ModificationMode )
+    return;
+  int nbPoints = myLocalPoints.size();
+  MESSAGE("number of selected points: " << nbPoints);
+  if (nbPoints != 2)
+    {
+      MESSAGE("works only with two points taken at the extremities of different sections, nothing done");
+      return;
+    }
+
+  CurveCreator_ICurve::SectionToPointList::const_iterator anIt = myLocalPoints.begin(),
+                                                          aLast = myLocalPoints.end();
+  std::vector<int> sections(nbPoints);
+  std::vector<int> iPoints(nbPoints);
+  std::vector<int> nbPtsSection(nbPoints);
+  std::vector<double> x(nbPoints);
+  std::vector<double> y(nbPoints);
+  int i = 0;
+  for ( ; anIt != aLast; anIt++, i++ )
+    {
+      CurveCreator_ICurve::SectionToPoint aSPoint = *anIt;
+      sections[i] = aSPoint.first;
+      iPoints[i] = aSPoint.second;
+      nbPtsSection[i] = myCurve->getNbPoints(sections[i]);
+      if ((iPoints[i] != 0) && (iPoints[i] != nbPtsSection[i]-1))
+        {
+          MESSAGE("a point is not on a section extremity, nothing done");
+          return;
+        }
+      gp_Pnt aPoint;
+      CurveCreator_UtilsICurve::getPoint( myCurve, sections[i], iPoints[i], aPoint );
+      x[i] = aPoint.X();
+      y[i] = aPoint.Y();
+      MESSAGE("point: " << sections[i] << " " << iPoints[i] << " " << x[i] << " " << y[i]);
+    }
+  if (sections[1] == sections[0])
+    {
+      MESSAGE("the two points must be at the extremities of different sections, nothing done");
+      return;
+    }
+
+  CurveCreator_ICurve::SectionToPointList aSelPoints;
+  startCurveModification( aSelPoints );
+
+  CurveCreator::Coordinates aChangedPos;
+  aChangedPos.push_back( x[0] ); // apply the first point coordinates to the second point.
+  aChangedPos.push_back( y[0] );
+  myCurve->setPoint( sections[1], iPoints[1], aChangedPos );
+
+  finishCurveModification( aSelPoints );
+}
+
 /**
  * Manage mouse press events
  * \param theWindow an owner of the signal
@@ -1005,8 +1054,9 @@ void CurveCreator_Widget::onMousePress( SUIT_ViewWindow*, QMouseEvent* theEvent
   if ( theEvent->button() != Qt::LeftButton )
     return;
 
-  myPressedX = theEvent->x();
-  myPressedY = theEvent->y();
+  // Initialize the starting point
+  myStartPoint.setX( theEvent->x() );
+  myStartPoint.setY( theEvent->y() );
 
   switch( getActionMode() ) {
     case ModificationMode: {
@@ -1028,15 +1078,71 @@ void CurveCreator_Widget::onMousePress( SUIT_ViewWindow*, QMouseEvent* theEvent
  * \param theWindow an owner of the signal
  * \param theEvent a mouse event
  */
-void CurveCreator_Widget::onMouseRelease( SUIT_ViewWindow*, QMouseEvent* theEvent )
+void CurveCreator_Widget::onMouseRelease( SUIT_ViewWindow* theWindow, QMouseEvent* theEvent )
 {
-  if ( getActionMode() != ModificationMode )
+  ActionMode aMode = getActionMode();
+  if ( aMode != ModificationMode )
+  {
+    // Emit selectionChanged() signal
+    getOCCViewer()->performSelectionChanged();
+
+    if ( aMode == AdditionMode )
+    {
+      Handle(AIS_InteractiveContext) aCtx = getAISContext();
+      if ( !aCtx.IsNull() )
+        aCtx->ClearSelected( Standard_True );
+    }
     return;
+  } 
+  if (theEvent->button() != Qt::LeftButton) return;
+  if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
+
+  // Initialize the ending point
+  myEndPoint.setX( theEvent->x() );
+  myEndPoint.setY( theEvent->y() );
+
+  bool aHasShift = ( theEvent->modifiers() & Qt::ShiftModifier );
+
+  // Highlight detected objects
+  Handle(AIS_InteractiveContext) aCtx = getAISContext();
+  if ( !aCtx.IsNull() )
+  {
+    OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
+    if (!aView)
+      return;
+
+    if (!aHasShift)
+      aCtx->ClearCurrents( false );
+
+    Handle(V3d_View) aView3d = aView->getViewPort()->getView();
+    if ( !aView3d.IsNull() )
+    {
+      // Initialize the single selection if start and end points are equal,
+      // otherwise a rectangular selection.
+      if ( myStartPoint == myEndPoint )
+      {
+        aCtx->MoveTo( myEndPoint.x(), myEndPoint.y(), aView3d, Standard_True );
+        if ( aHasShift )
+          aCtx->ShiftSelect( Standard_True );
+        else
+          aCtx->Select( Standard_True );
+      }
+      else
+      {
+        if ( aHasShift )
+          aCtx->ShiftSelect( myStartPoint.x(), myStartPoint.y(), myEndPoint.x(), myEndPoint.y(),
+                             aView3d, Standard_False );
+        else
+          aCtx->Select( myStartPoint.x(), myStartPoint.y(), myEndPoint.x(), myEndPoint.y(),
+                        aView3d, Standard_False );
+      }
+    }
+  }
 
   if ( myDragStarted ) {
     bool isDragged = myDragged;
     CurveCreator_ICurve::SectionToPointList aDraggedPoints;
-    QMap<CurveCreator_ICurve::SectionToPoint, std::deque< float > > anInitialDragPointsCoords;
+    QMap<CurveCreator_ICurve::SectionToPoint, CurveCreator::Coordinates > anInitialDragPointsCoords;
     if ( myDragged ) {
       aDraggedPoints = myDragPoints;
       anInitialDragPointsCoords = myInitialDragPointsCoords;
@@ -1082,7 +1188,7 @@ void CurveCreator_Widget::onMouseRelease( SUIT_ViewWindow*, QMouseEvent* theEven
         for ( ; anIt != aLast; anIt++ ) {
           int aSectionId = anIt->first;
           int aPointId = anIt->second;
-          std::deque<float> aPos = myCurve->getPoint( aSectionId, aPointId );
+          CurveCreator::Coordinates aPos = myCurve->getPoint( aSectionId, aPointId );
 
           aCoordList.push_back(
             std::make_pair( std::make_pair( aSectionId, aPointId ), aPos ) );
@@ -1104,17 +1210,13 @@ void CurveCreator_Widget::onMouseRelease( SUIT_ViewWindow*, QMouseEvent* theEven
   }
   else // check whether the segment is clicked an a new point should be added to the segment
   {
-    int aReleasedX = theEvent->x();
-    int aReleasedY = theEvent->y();
-    if ( myPressedX == aReleasedX && myPressedY == aReleasedY )
-      insertPointToSelectedSegment( aReleasedX, aReleasedY );
+    if ( myStartPoint.x() == myEndPoint.x() && myStartPoint.y() == myEndPoint.y() )
+      insertPointToSelectedSegment( myEndPoint.x(), myStartPoint.y() );
   }
 
   // updates the input panel table to show the selected point coordinates
   updateLocalPointView();
   updateUndoRedo();
-
-  emit curveModified();
 }
 
 /**
@@ -1176,14 +1278,12 @@ void CurveCreator_Widget::onCellChanged( int theRow, int theColumn )
 
   double aX  = myLocalPointView->item( theRow, 2 )->data( Qt::UserRole ).toDouble();
   double anY = myLocalPointView->item( theRow, 3 )->data( Qt::UserRole ).toDouble();
-  std::deque<float> aChangedPos;
+  CurveCreator::Coordinates aChangedPos;
   aChangedPos.push_back( aX );
   aChangedPos.push_back( anY );
   myCurve->setPoint( aCurrSect, aPntIndex, aChangedPos );
 
   finishCurveModification( aSelPoints );
-
-  emit curveModified();
 }
 
 /**
@@ -1201,8 +1301,6 @@ void CurveCreator_Widget::removeSection()
   }
   mySectionView->clearSelection();
   updateUndoRedo();
-
-  emit curveModified();
 }
 
 /**
@@ -1221,8 +1319,6 @@ void CurveCreator_Widget::removePoint()
   myCurve->removeSeveralPoints( aPoints );
   finishCurveModification( CurveCreator_ICurve::SectionToPointList() );
   mySectionView->reset();
-
-  emit curveModified();
 }
 
 void CurveCreator_Widget::addNewPoint(const CurveCreator::Coordinates& theCoords)
@@ -1238,8 +1334,6 @@ void CurveCreator_Widget::addNewPoint(const CurveCreator::Coordinates& theCoords
   mySectionView->pointsAdded( aSection, myCurve->getNbPoints( aSection ) );
   updateActionsStates();
   updateUndoRedo();
-
-  emit curveModified();
 }
 
 void CurveCreator_Widget::insertPointToSelectedSegment( const int theX,
@@ -1306,8 +1400,6 @@ void CurveCreator_Widget::insertPointToSelectedSegment( const int theX,
   finishCurveModification( aSelPoints );
 
   setSelectedPoints();
-
-  emit curveModified();
 }
 
 void CurveCreator_Widget::moveSelectedPoints( const int theXPosition,
@@ -1329,7 +1421,7 @@ void CurveCreator_Widget::moveSelectedPoints( const int theXPosition,
   double anYDelta = aStartPnt.Y() - anEndPnt.Y();
 
   CurveCreator_ICurve::SectionToPointCoordsList aCoordList;
-  std::deque<float> aChangedPos;
+  CurveCreator::Coordinates aChangedPos;
   CurveCreator_ICurve::SectionToPointList::const_iterator anIt = myDragPoints.begin(),
                                                           aLast = myDragPoints.end();
   for ( ; anIt != aLast; anIt++ ) {
@@ -1355,8 +1447,6 @@ void CurveCreator_Widget::moveSelectedPoints( const int theXPosition,
 
   myDragged = true;
   finishCurveModification( myDragPoints );
-
-  emit curveModified();
 }
 
 void CurveCreator_Widget::updateLocalPointView()
@@ -1483,7 +1573,7 @@ void CurveCreator_Widget::finishCurveModification(
  * \param theX the X coordinate of the point
  * \param theY the Y coordinate of the point
  */
-int CurveCreator_Widget::findLocalPointIndex( int theSectionId, float theX, float theY )
+int CurveCreator_Widget::findLocalPointIndex( int theSectionId, double theX, double theY )
 {
   return CurveCreator_UtilsICurve::findLocalPointIndex( myCurve, theSectionId, theX, theY );
 }
@@ -1501,7 +1591,7 @@ void CurveCreator_Widget::convert( const CurveCreator_ICurve::SectionToPointList
 }
 
 /**
- * Returns whethe the container has the value
+ * Returns whether the container has the value
  * \param theList a container of values
  * \param theValue a value
  */