Salome HOME
Double editor in table
[modules/hydro.git] / src / HYDROCurveCreator / CurveCreator_Widget.cxx
index 94f41248773476f7ddaa35ab4782afb0d6bccc93..56ae6c123358bb31b87f131dac788d00fccb8e93 100644 (file)
@@ -25,6 +25,7 @@
 //#include "CurveCreator_NewPointDlg.h"
 #include "CurveCreator_NewSectionDlg.h"
 #include "CurveCreator_Utils.h"
+#include "CurveCreator_TableView.h"
 
 #include <SUIT_Session.h>
 #include <SUIT_Desktop.h>
@@ -96,6 +97,7 @@ CurveCreator_Widget::CurveCreator_Widget(QWidget* parent,
   connect( mySectionView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onContextMenu(QPoint)) );
 
   myLocalPointView = new QTableWidget();
+  myLocalPointView->setItemDelegate( new CurveCreator_TableItemDelegate( myLocalPointView ) );
   myLocalPointView->setVisible( false );
   myLocalPointView->setColumnCount( 4 );
   myLocalPointView->setColumnWidth( 0, SECTION_NAME_COLUMN_WIDTH );
@@ -292,6 +294,8 @@ void CurveCreator_Widget::onSelectionChanged()
   QList<ActionId> anEnabledAct;
   if( myCurve ){
     anEnabledAct << NEW_SECTION_ID << MODIFICATION_MODE_ID;
+    if ( removeEnabled() )
+      anEnabledAct << REMOVE_ID;
     QList<int> aSelSections = mySectionView->getSelectedSections();
     QList< QPair< int, int > > aSelPoints = mySectionView->getSelectedPoints();
     CurveCreator_TreeView::SelectionType aSelType = mySectionView->getSelectionType();
@@ -305,7 +309,6 @@ void CurveCreator_Widget::onSelectionChanged()
       }*/
       if( aSelSections.size() == 1 ){
         anEnabledAct << ADDITION_MODE_ID << DETECTION_MODE_ID;
-        anEnabledAct << REMOVE_ID;
       }
       switch ( getActionMode() ) {
         case AdditionMode: {
@@ -570,9 +573,14 @@ void CurveCreator_Widget::onModifySection()
   bool isClosed = myNewSectionEditor->isClosed();
   CurveCreator::SectionType aSectType = myNewSectionEditor->getSectionType();
 //  myCurve->startOperation();
-  myCurve->setClosed( isClosed, mySection );
-  myCurve->setSectionName( mySection , aName.toStdString() );
-  myCurve->setSectionType( mySection, aSectType );
+  if( myCurve->getSectionName(mySection) != aName.toStdString() )
+    myCurve->setSectionName( mySection , aName.toStdString() );
+
+  if( myCurve->getSectionType(mySection) != aSectType )
+    myCurve->setSectionType( mySection, aSectType );
+
+  if( myCurve->isClosed(mySection) != isClosed )
+    myCurve->setClosed( mySection, isClosed );
 //  myCurve->finishOperation();
   mySectionView->sectionChanged(mySection);
   updateUndoRedo();
@@ -705,7 +713,7 @@ void CurveCreator_Widget::onCloseSections()
 //  myCurve->startOperation();
   QList<int> aSelSections = mySectionView->getSelectedSections();
   for( int i = 0 ; i < aSelSections.size() ; i++ ){
-    myCurve->setClosed(true, aSelSections[i]);
+    myCurve->setClosed(aSelSections[i], true);
     mySectionView->sectionChanged(aSelSections[i]);
   }
 //  myCurve->finishOperation();
@@ -719,7 +727,7 @@ void CurveCreator_Widget::onUncloseSections()
 //  myCurve->startOperation();
   QList<int> aSelSections = mySectionView->getSelectedSections();
   for( int i = 0 ; i < aSelSections.size() ; i++ ){
-    myCurve->setClosed(false, aSelSections[i]);
+    myCurve->setClosed(aSelSections[i], false);
     mySectionView->sectionChanged(aSelSections[i]);
   }
 //  myCurve->finishOperation();
@@ -730,18 +738,23 @@ void CurveCreator_Widget::onUndo()
 {
     if( !myCurve )
       return;
+
+    CurveCreator_Widget::SectionToPointList aPoints;
+    startCurveModification( aPoints, false );
     myCurve->undo();
+    finishCurveModification();
     mySectionView->reset();
-    updateUndoRedo();
 }
 
 void CurveCreator_Widget::onRedo()
 {
     if( !myCurve )
       return;
+    CurveCreator_Widget::SectionToPointList aPoints;
+    startCurveModification( aPoints, false );
     myCurve->redo();
+    finishCurveModification();
     mySectionView->reset();
-    updateUndoRedo();
 }
 
 void CurveCreator_Widget::updateUndoRedo()
@@ -817,6 +830,30 @@ QList< QPair< int, int > > CurveCreator_Widget::getSelectedPoints()
   return mySectionView->getSelectedPoints();
 }
 
+/**
+ * According to the widget state, performs the remove action
+ */
+void CurveCreator_Widget::removeSelected()
+{
+  onRemove();
+}
+
+/**
+ * Checks whether there are some selection to be removed
+ */
+bool CurveCreator_Widget::removeEnabled()
+{
+  bool isEnabled = getActionMode() == ModificationMode;
+  if ( !isEnabled ) {
+    QList<int> aSelSections = mySectionView->getSelectedSections();
+    CurveCreator_TreeView::SelectionType aSelType = mySectionView->getSelectionType();
+    isEnabled = aSelType == CurveCreator_TreeView::ST_SECTIONS &&
+                aSelSections.size() == 1;
+  }
+  return isEnabled;
+}
+
+
 //=================================================================================
 // function : GeometryGUI::onGetCoordsByClick()
 // purpose  : Manage mouse press events in Additon mode
@@ -939,8 +976,8 @@ void CurveCreator_Widget::onCellChanged( int theRow, int theColumn )
   SectionToPointList aSelPoints;
   startCurveModification( aSelPoints );
 
-  double aX  = myLocalPointView->item( theRow, 2 )->text().toDouble();
-  double anY = myLocalPointView->item( theRow, 3 )->text().toDouble();
+  double aX  = myLocalPointView->item( theRow, 2 )->data( Qt::UserRole ).toDouble();
+  double anY = myLocalPointView->item( theRow, 3 )->data( Qt::UserRole ).toDouble();
   std::deque<float> aChangedPos;
   aChangedPos.push_back( aX );
   aChangedPos.push_back( anY );
@@ -992,16 +1029,7 @@ void CurveCreator_Widget::removePoint()
   SectionToPointList aSelPoints;
   startCurveModification( aSelPoints, false );
 
-  // the points should be removed in a decreased order
-  qSort( aPoints );
-  SectionToPointList::const_iterator anIt = aPoints.end(), aFirst = aPoints.begin();
-  anIt--;
-  for ( ; anIt != aFirst; anIt-- ) {
-    int aSectionId = anIt->first;
-    int aPointId = anIt->second;
-    myCurve->removePoint( aSectionId, aPointId );
-    mySectionView->pointsRemoved( aSectionId, aPointId );
-  }
+  myCurve->removeSeveralPoints( aPoints );
   finishCurveModification( SectionToPointList() );
 }
 
@@ -1074,7 +1102,6 @@ void CurveCreator_Widget::insertPointToSelectedSegment( const int theX,
   finishCurveModification( aSelPoints );
 
   setSelectedPonts();
-  updateUndoRedo();
 }
 
 void CurveCreator_Widget::moveSelectedPoints( const int theXPosition,
@@ -1097,20 +1124,24 @@ void CurveCreator_Widget::moveSelectedPoints( const int theXPosition,
   double aXDelta = aStartPnt.X() - anEndPnt.X();
   double anYDelta = aStartPnt.Y() - anEndPnt.Y();
 
-  int aSectionId;
-  int aPointId;
+  CurveCreator_ICurve::SectionToPointCoordsList aCoordList;
   std::deque<float> aChangedPos;
   SectionToPointList::const_iterator anIt = myDragPoints.begin(), aLast = myDragPoints.end();
   for ( ; anIt != aLast; anIt++ ) {
-    aSectionId = anIt->first;
-    aPointId = anIt->second;
+    int aSectionId = anIt->first;
+    int aPointId = anIt->second;
     aChangedPos = myCurve->getPoint( aSectionId, aPointId );
     if ( aChangedPos.size() < 2 )
       continue;
     aChangedPos[0] = aChangedPos[0] - aXDelta;
     aChangedPos[1] = aChangedPos[1] - anYDelta;
-    myCurve->setPoint( aSectionId, aPointId, aChangedPos );
+    
+    aCoordList.push_back(
+      std::make_pair(std::make_pair( aSectionId, aPointId ), 
+                     aChangedPos ));
   }
+  myCurve->setSeveralPoints( aCoordList );
+
   myDragged = true;
   finishCurveModification( myDragPoints );
 }
@@ -1227,9 +1258,9 @@ void CurveCreator_Widget::addLocalPointToTable( const double theX, const double
     aCurrentY = myLocalPointView->item( i, 3 )->data( Qt::UserRole ).toDouble();
     if ( fabs( aCurrentX - theX ) < LOCAL_SELECTION_TOLERANCE &&
          fabs( aCurrentY - theY ) < LOCAL_SELECTION_TOLERANCE ) {
-      aPoint = qMakePair( getSectionId( i ), getPointId( i ) );
-      if ( !aSkipList.contains( aPoint ) )
-        aSkipList.append( aPoint );
+           aPoint = std::make_pair<int, int>( getSectionId( i ), getPointId( i ) );
+      if ( !contains( aSkipList, aPoint ) )
+        aSkipList.push_back( aPoint );
     }
   }
   if ( aSkipList.size() == aPoints.size() )
@@ -1239,7 +1270,7 @@ void CurveCreator_Widget::addLocalPointToTable( const double theX, const double
   SectionToPointList::const_iterator anIt = aPoints.begin(), aLast = aPoints.end();
   for ( ; anIt != aLast; anIt++ ) {
     aPoint = *anIt;
-    if ( aSkipList.contains( aPoint ) )
+    if ( contains( aSkipList, aPoint ) )
       continue;
 
     myLocalPointView->setRowCount( aRowId+1 );
@@ -1297,7 +1328,7 @@ void CurveCreator_Widget::getSelectedPonts( CurveCreator_Widget::SectionToPointL
 {
   thePoints.clear();
   for ( int i = 0, aNb = myLocalPointView->rowCount(); i < aNb; i++ )
-    thePoints.append( qMakePair( getSectionId( i ), getPointId( i ) ) );
+    thePoints.push_back( std::make_pair( getSectionId( i ), getPointId( i ) ) );
 }
 
 void CurveCreator_Widget::setSelectedPonts( const CurveCreator_Widget::SectionToPointList& thePoints )
@@ -1314,31 +1345,38 @@ void CurveCreator_Widget::setSelectedPonts( const CurveCreator_Widget::SectionTo
 
   AIS_ListOfInteractive aDisplayedList;
   ic->DisplayedObjects( aDisplayedList );
-  for ( AIS_ListIteratorOfListOfInteractive it( aDisplayedList ); it.More(); it.Next() )
-  {
-    Handle(AIS_InteractiveObject) anAIS = it.Value();
-    if ( anAIS.IsNull() )
-      continue;
-    Handle(AIS_Point) anAISPoint = Handle(AIS_Point)::DownCast( anAIS );
-    if ( anAISPoint.IsNull() )
-      continue;
 
-    TopoDS_Vertex aVertex = TopoDS::Vertex( anAISPoint->Vertex() );
+  SectionToPointList::const_iterator anIt = thePoints.begin(), aLast = thePoints.end();
+  SectionToPoint aSToPoint;
+  for( ; anIt != aLast; anIt++ ) {
+    aSToPoint = *anIt;
 
-    if ( aVertex.IsNull() )
-      continue;
+    for ( AIS_ListIteratorOfListOfInteractive it( aDisplayedList ); it.More(); it.Next() )
+    {
+      Handle(AIS_InteractiveObject) anAIS = it.Value();
+      if ( anAIS.IsNull() )
+        continue;
+      Handle(AIS_Point) anAISPoint = Handle(AIS_Point)::DownCast( anAIS );
+      if ( anAISPoint.IsNull() )
+        continue;
+
+      TopoDS_Vertex aVertex = TopoDS::Vertex( anAISPoint->Vertex() );
+
+      if ( aVertex.IsNull() )
+        continue;
 
-    gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
+      gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
 
-    SectionToPointList aPoints;
-    findSectionsToPoints( aPnt.X(), aPnt.Y(), aPoints );
+      SectionToPointList aPoints;
+      findSectionsToPoints( aPnt.X(), aPnt.Y(), aPoints );
 
-    SectionToPointList::const_iterator anIt = aPoints.begin(), aLast = aPoints.end();
-    SectionToPoint aPoint;
-    for ( ; anIt != aLast; anIt++ ) {
-      aPoint = *anIt;
-      if ( thePoints.contains( aPoint ) )
-        aListToSelect.Append( anAIS );
+      SectionToPointList::const_iterator anIt = aPoints.begin(), aLast = aPoints.end();
+      SectionToPoint aPoint;
+      for ( ; anIt != aLast; anIt++ ) {
+        aPoint = *anIt;
+        if ( aPoint.first == aSToPoint.first && aPoint.second == aSToPoint.second )
+          aListToSelect.Append( anAIS );
+      }
     }
   }
 
@@ -1375,9 +1413,10 @@ void CurveCreator_Widget::startCurveModification(
 void CurveCreator_Widget::finishCurveModification(
                            const CurveCreator_Widget::SectionToPointList& thePoints )
 {
-  setLocalPointContext( true );
-  int aSectionId = 0;
+  if ( getActionMode() == ModificationMode )
+    setLocalPointContext( true );
   setSelectedPonts( thePoints );
+  updateUndoRedo();
 }
 
 /**
@@ -1412,9 +1451,30 @@ void CurveCreator_Widget::findSectionsToPoints( const double theX, const double
     aPointId = findLocalPointIndex( i, theX, theY );
     if ( aPointId < 0 )
       continue;
-    SectionToPoint aPoint = qMakePair( i, aPointId );
-    if ( !thePoints.contains( aPoint ) )
-     thePoints.append( aPoint );
+    SectionToPoint aPoint = std::make_pair( i, aPointId );
+    if ( !contains( thePoints, aPoint ) )
+      thePoints.push_back( aPoint );
+  }
+}
+
+void CurveCreator_Widget::convert( const SectionToPointList& thePoints,
+                                   QMap<int, QList<int> >& theConvPoints )
+{
+  theConvPoints.clear();
+
+  SectionToPointList::const_iterator anIt = thePoints.begin(), aLast = thePoints.end();
+  QList<int> aPoints;
+  int aSectionId, aPointId;
+  for ( ; anIt != aLast; anIt++ ) {
+    aSectionId = anIt->first;
+    aPointId = anIt->second;
+    aPoints.clear();
+    if ( theConvPoints.contains( aSectionId ) )
+      aPoints = theConvPoints[aSectionId];
+    if ( aPoints.contains( aPointId ) )
+      continue;
+    aPoints.append( aPointId );
+    theConvPoints[aSectionId] = aPoints;
   }
 }
 
@@ -1495,12 +1555,8 @@ bool CurveCreator_Widget::pointOnObject( Handle(AIS_InteractiveObject) theObject
   }
   if ( isFound ) {
     thePoint = aPoint;
-
     thePoint1 = aPnt1;
     thePoint2 = aPnt2;
-    //thePoint1 = findLocalPointIndex( 0, aPnt1.X(), aPnt1.Y() );
-    //thePoint2 = findLocalPointIndex( 0, aPnt2.X(), aPnt2.Y() );
-    //isFound = thePoint1 >= 0 && thePoint2 >= 0;
   }
   return isFound;
 }
@@ -1561,3 +1617,20 @@ int CurveCreator_Widget::getPointId( const int theRowId ) const
 {
   return myLocalPointView->item( theRowId, 1 )->data( Qt::UserRole ).toInt();
 }
+
+/**
+ * Returns whethe the container has the value
+ * \param theList a container of values
+ * \param theValue a value
+ */
+bool CurveCreator_Widget::contains( const CurveCreator_Widget::SectionToPointList& theList,
+                                    const CurveCreator_Widget::SectionToPoint& theValue ) const
+{
+  bool isFound = false;
+
+  SectionToPointList::const_iterator anIt = theList.begin(), aLast = theList.end();
+  for ( ; anIt != aLast && !isFound; anIt++ )
+    isFound = anIt->first == theValue.first && anIt->second == theValue.second;
+
+  return isFound;
+}