Salome HOME
Feature #102: Display of Lambert coordinates.
[modules/hydro.git] / src / HYDROCurveCreator / CurveCreator_Widget.cxx
index e3bc69f8a29671b7bbd81446ea3d818a4f7cfa39..37a4c78279bf0a7c9d205c4b9f37038c2c1ef90b 100644 (file)
 #include <AIS_Shape.hxx>
 #include <AIS_Point.hxx>
 #include <AIS_Line.hxx>
+#include <AIS_LocalContext.hxx>
 #include <Geom_Point.hxx>
 #include <Geom_BSplineCurve.hxx>
 #include <Geom_Line.hxx>
 #include <StdSelect_BRepOwner.hxx>
+#include <SelectMgr_Selection.hxx>
+#include <Select3D_SensitivePoint.hxx>
 
 #include <QHBoxLayout>
 #include <QVBoxLayout>
 #include <QMouseEvent>
 #include <QApplication>
 #include <QTableWidget>
+#include <QTime>
 
-const double LOCAL_SELECTION_TOLERANCE = 0.0001;
-const int SECTION_NAME_COLUMN_WIDTH = 75;
-const int POINT_INDEX_COLUMN_WIDTH = 40;
+#define MEASURE_TIME
+
+#ifdef MEASURE_TIME
+
+  #define START_MEASURE_TIME \
+    QTime aTimer;            \
+    aTimer.start();          \
+
+  #define END_MEASURE_TIME( theMsg )                      \
+    double aTime = aTimer.elapsed() * 0.001;              \
+    FILE* aFile = fopen( "performance", "a" );            \
+    fprintf( aFile, "%s = %.3lf sec\n", theMsg, aTime );  \
+    fclose( aFile );                                      \
+
+#else
+
+  #define START_MEASURE_TIME
+  #define END_MEASURE_TIME( theMsg )
+
+#endif
 
-const int SCENE_PIXEL_TOLERANCE = 10;
+
+
+
+
+
+const double LOCAL_SELECTION_TOLERANCE = 0.0001;
 
 CurveCreator_Widget::CurveCreator_Widget(QWidget* parent,
                                          CurveCreator_ICurve *theCurve,
-                                         Qt::WindowFlags fl)
+                                         Qt::WindowFlags fl,
+                                         int theLocalPointRowLimit )
 : QWidget(parent), myNewSectionEditor(NULL), myCurve(theCurve), mySection(0),
   myDragStarted( false ), myDragInteractionStyle( SUIT_ViewModel::STANDARD ),
-  myOCCViewer( 0 )
+  myOCCViewer( 0 ), myLocalPointRowLimit( theLocalPointRowLimit )
 {
   myNewSectionEditor = new CurveCreator_NewSectionDlg( this );
   myNewSectionEditor->hide();
@@ -98,16 +125,7 @@ CurveCreator_Widget::CurveCreator_Widget(QWidget* parent,
   connect( mySectionView, SIGNAL(sectionEntered(int)), this, SLOT(onEditSection(int)) );
   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 );
-  myLocalPointView->setColumnWidth( 1, POINT_INDEX_COLUMN_WIDTH );
-  QStringList aLabels;
-  //aLabels << tr( "SECTION_LABEL" ) << tr( "IDENTIFIER_LABEL" ) << tr( "X_POSITION_LBL" ) << tr( "Y_POSITION_LBL" );
-  aLabels << tr( "Section" ) << "Index" << tr( "X" ) << tr( "Y" );
-  myLocalPointView->setHorizontalHeaderLabels( aLabels );
+  myLocalPointView = new CurveCreator_TableView( myCurve, this );
   connect( myLocalPointView, SIGNAL( cellChanged( int, int ) ),
            this, SLOT( onCellChanged( int, int ) ) );
 
@@ -218,7 +236,9 @@ CurveCreator_Widget::CurveCreator_Widget(QWidget* parent,
 //    aLay->addLayout(aNameLayout);
   aLay->addWidget(aSectionGroup);
   setLayout(aLay);
-  onSelectionChanged();
+
+  updateActionsStates();
+  updateUndoRedo();
 }
 
 /**
@@ -333,36 +353,23 @@ void CurveCreator_Widget::reset()
 {
 }
 
-//=======================================================================
-// function: getUniqSectionName
-// purpose: return unique section name
-//=======================================================================
-std::string CurveCreator_Widget::getUniqSectionName( CurveCreator_ICurve* theCurve ) const
-{
-  for( int i = 0 ; i < 1000000 ; i++ ){
-      char aBuffer[255];
-      sprintf( aBuffer, "Section_%d", i+1 );
-      std::string aName(aBuffer);
-      int j;
-      for( j = 0 ; j < theCurve->getNbSections() ; j++ ){
-        if( theCurve->getSectionName(j) == aName )
-            break;
-      }
-      if( j == theCurve->getNbSections() )
-          return aName;
-  }
-  return "";
-}
-
 void CurveCreator_Widget::setCurve( CurveCreator_ICurve* theCurve )
 {
   myCurve = theCurve;
-  mySectionView->setCurve(myCurve);
-  onSelectionChanged();
+  mySectionView->setCurve( myCurve );
+  myLocalPointView->setCurve( myCurve );
+  updateActionsStates();
   updateUndoRedo();
 }
 
 void CurveCreator_Widget::onSelectionChanged()
+{
+  updateActionsStates();
+  updateUndoRedo();
+  emit selectionChanged();
+}
+
+void CurveCreator_Widget::updateActionsStates()
 {
   QList<ActionId> anEnabledAct;
   if( myCurve ){
@@ -459,8 +466,6 @@ void CurveCreator_Widget::onSelectionChanged()
       }
     }
   }
-  updateUndoRedo();
-  emit selectionChanged();
 }
 
 void CurveCreator_Widget::onAdditionMode(bool checked)
@@ -526,7 +531,8 @@ void CurveCreator_Widget::onModeChanged(bool checked)
         break;
     }
   }
-  onSelectionChanged();
+  updateActionsStates();
+  updateUndoRedo();
   setLocalPointContext( aMode == ModificationMode, true );
 }
 
@@ -544,7 +550,7 @@ void CurveCreator_Widget::onAddNewPoint(const CurveCreator::Coordinates& theCoor
   int aSection = aSections[0];
   myCurve->addPoints(theCoords, aSection); // add to the end of section
   mySectionView->pointsAdded( aSection, myCurve->getNbPoints( aSection ) );
-  onSelectionChanged();
+  updateActionsStates();
   updateUndoRedo();
 }
 
@@ -554,7 +560,7 @@ void CurveCreator_Widget::onNewSection()
     return;
   myNewSectionEditor->clear();
   myNewSectionEditor->setEditMode(false);
-  QString aSectName = QString( getUniqSectionName(myCurve).c_str() );
+  QString aSectName = QString( CurveCreator_UtilsICurve::getUniqSectionName( myCurve ).c_str() );
   myNewSectionEditor->setSectionParameters(aSectName, true, CurveCreator::Polyline );
   emit subOperationStarted( myNewSectionEditor );
 }
@@ -563,12 +569,13 @@ void CurveCreator_Widget::onAddNewSection()
 {
   if( !myCurve )
     return;
-  myCurve->addSection( myNewSectionEditor->getName().toStdString(), myNewSectionEditor->getSectionType(),
-                      myNewSectionEditor->isClosed() );
+  myCurve->addSection( myNewSectionEditor->getName().toStdString(),
+                       myNewSectionEditor->getSectionType(),
+                       myNewSectionEditor->isClosed() );
   mySectionView->sectionAdded( -1 ); // add a new section to the end of list
-  QString aNewName = QString(getUniqSectionName(myCurve).c_str());
+  QString aNewName = QString( CurveCreator_UtilsICurve::getUniqSectionName( myCurve ).c_str() );
   myNewSectionEditor->setSectionName(aNewName);
-  onSelectionChanged();
+  updateActionsStates();
   updateUndoRedo();
   onCancelSection();
 }
@@ -720,7 +727,7 @@ void CurveCreator_Widget::onClearAll()
     return;
   myCurve->clear();
   mySectionView->reset();
-  onSelectionChanged();
+  updateActionsStates();
   updateUndoRedo();
 }
 
@@ -730,7 +737,7 @@ void CurveCreator_Widget::onJoinAll()
     return;
   myCurve->join();
   mySectionView->reset();
-  onSelectionChanged();
+  updateActionsStates();
   updateUndoRedo();
 }
 
@@ -1006,7 +1013,11 @@ void CurveCreator_Widget::onMouseRelease( SUIT_ViewWindow*, QMouseEvent* theEven
     setDragStarted( false );
     // if the drag of some points has happened, restore the drag selection
     if ( aDraggedPoints.size() > 0 )
+    {
+      START_MEASURE_TIME;
       setSelectedPonts( aDraggedPoints );
+      END_MEASURE_TIME( "drop" );
+    }
   }
   else // check whether the segment is clicked an a new point should be added to the segment
     insertPointToSelectedSegment( theEvent->pos().x(), theEvent->pos().y() );
@@ -1029,8 +1040,12 @@ void CurveCreator_Widget::onMouseMove( SUIT_ViewWindow*, QMouseEvent* theEvent )
   if ( (aPos - myDragStartPosition).manhattanLength() < QApplication::startDragDistance() )
     return;
 
+  START_MEASURE_TIME;
+
   moveSelectedPoints( aPos.x(), aPos.y() );
   myDragStartPosition = aPos;
+
+  END_MEASURE_TIME( "drag" );
 }
 
 /**
@@ -1059,8 +1074,8 @@ void CurveCreator_Widget::onMouseMove( QMouseEvent* theEvent )
 
 void CurveCreator_Widget::onCellChanged( int theRow, int theColumn )
 {
-  int aCurrSect = getSectionId( theRow );
-  int aPntIndex = getPointId( theRow );
+  int aCurrSect = myLocalPointView->getSectionId( theRow );
+  int aPntIndex = myLocalPointView->getPointId( theRow );
 
   if ( aPntIndex < 0 )
     return;
@@ -1239,6 +1254,11 @@ void CurveCreator_Widget::updateLocalPointView()
     return;
 
   std::list<float> aSelectedList = CurveCreator_Utils::getSelectedPoints( aContext );
+  int aNbPoints = aSelectedList.size()/3;
+  bool isRowLimit = aNbPoints > myLocalPointRowLimit;
+  myLocalPointView->setVisible( !isRowLimit );
+  if ( isRowLimit )
+    return;
 
   std::list<float>::const_iterator anIt = aSelectedList.begin(), aLast = aSelectedList.end();
 
@@ -1251,7 +1271,7 @@ void CurveCreator_Widget::updateLocalPointView()
     float anY = *anIt;
     anIt++;
     float aZ = *anIt;
-    addLocalPointToTable( aX, anY );
+    myLocalPointView->addLocalPointToTable( aX, anY );
   }
   myLocalPointView->blockSignals(isBlocked);
 }
@@ -1266,62 +1286,6 @@ void CurveCreator_Widget::setLocalPointContext( const bool theOpen, const bool i
     updateLocalPointView();
 }
 
-void CurveCreator_Widget::addLocalPointToTable( const double theX, const double theY )
-{
-  CurveCreator_ICurve::SectionToPointList aPoints;
-  findSectionsToPoints( theX, theY, aPoints );
-
-  CurveCreator_ICurve::SectionToPointList aSkipList;
-  // table could not contain two equal value rows
-  int aRowId = myLocalPointView->rowCount();
-  double aCurrentX, aCurrentY;
-  int aSectionId, aPointId;
-  CurveCreator_ICurve::SectionToPoint aPoint;
-  for ( int i = 0; i < aRowId; i++ ) {
-    aCurrentX = myLocalPointView->item( i, 2 )->data( Qt::UserRole ).toDouble();
-    aCurrentY = myLocalPointView->item( i, 3 )->data( Qt::UserRole ).toDouble();
-    if ( fabs( aCurrentX - theX ) < LOCAL_SELECTION_TOLERANCE &&
-         fabs( aCurrentY - theY ) < LOCAL_SELECTION_TOLERANCE ) {
-           aPoint = std::make_pair<int, int>( getSectionId( i ), getPointId( i ) );
-      if ( !contains( aSkipList, aPoint ) )
-        aSkipList.push_back( aPoint );
-    }
-  }
-  if ( aSkipList.size() == aPoints.size() )
-    return;
-
-  QTableWidgetItem* anItem;
-  CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aPoints.begin(),
-                                                          aLast = aPoints.end();
-  for ( ; anIt != aLast; anIt++ ) {
-    aPoint = *anIt;
-    if ( contains( aSkipList, aPoint ) )
-      continue;
-
-    myLocalPointView->setRowCount( aRowId+1 );
-    aSectionId = aPoint.first;
-    aPointId = aPoint.second;
-
-    anItem = new QTableWidgetItem( myCurve->getSectionName( aSectionId ).c_str() );
-    anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
-    anItem->setData( Qt::UserRole, aSectionId );
-    myLocalPointView->setItem( aRowId, 0, anItem );
-
-    anItem = new QTableWidgetItem( QString::number( aPointId + 1 ) );
-    anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
-    anItem->setData( Qt::UserRole, aPointId );
-    myLocalPointView->setItem( aRowId, 1, anItem );
-
-    anItem = new QTableWidgetItem( QString::number( theX ) );
-    anItem->setData( Qt::UserRole, theX );
-    myLocalPointView->setItem( aRowId, 2, anItem );
-
-    anItem = new QTableWidgetItem( QString::number( theY ) );
-    anItem->setData( Qt::UserRole, theY );
-    myLocalPointView->setItem( aRowId, 3, anItem );
-  }
-}
-
 /**
  * Set drag operation started. Save the position and a list of dragged points
  * \param theState the drag operation state: started/finished
@@ -1351,68 +1315,137 @@ void CurveCreator_Widget::getSelectedPonts( CurveCreator_ICurve::SectionToPointL
 {
   thePoints.clear();
   for ( int i = 0, aNb = myLocalPointView->rowCount(); i < aNb; i++ )
-    thePoints.push_back( std::make_pair( getSectionId( i ), getPointId( i ) ) );
+    thePoints.push_back( std::make_pair( myLocalPointView->getSectionId( i ),
+                                         myLocalPointView->getPointId( i ) ) );
+}
+
+
+bool CurveCreator_Widget::isIntersectVertexToPoint( const TopoDS_Vertex& theVertex,
+                               const CurveCreator_ICurve::SectionToPoint& theSToPoint )
+{
+  bool isIntersect = false;
+  if ( theVertex.IsNull() )
+    return isIntersect;
+
+  gp_Pnt aSPoint;
+  CurveCreator_UtilsICurve::getPoint( myCurve, theSToPoint.first, theSToPoint.second,
+                                      aSPoint );
+  gp_Pnt aVertexPoint = BRep_Tool::Pnt( theVertex );
+  isIntersect = fabs( aVertexPoint.X() - aSPoint.X() ) < LOCAL_SELECTION_TOLERANCE &&
+                fabs( aVertexPoint.Y() - aSPoint.Y() ) < LOCAL_SELECTION_TOLERANCE;
+
+  return isIntersect;
 }
 
+
 void CurveCreator_Widget::setSelectedPonts( const CurveCreator_ICurve::SectionToPointList& thePoints )
 {
   if ( myDragStarted )
     return;
+
   Handle(AIS_InteractiveContext) ic = getAISContext();
   if ( ic.IsNull() || !ic->HasOpenedContext() )
     return;
 
   AIS_ListOfInteractive aListToSelect;
-
   AIS_ListOfInteractive aDisplayedList;
   ic->DisplayedObjects( aDisplayedList );
+  ic->ClearSelected( Standard_False );
+
+  bool isSelectedVertex = false;
+
+  //ASL: std::vector<TopoDS_Vertex> aVetexVec;
+  for ( AIS_ListIteratorOfListOfInteractive it( aDisplayedList ); it.More(); it.Next() )
+  {
+    Handle(AIS_InteractiveObject) anAIS = it.Value();
+    if ( anAIS.IsNull() )
+      continue;
+    Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast( anAIS );
+    if ( anAISShape.IsNull() )
+      continue;
+
+
+    /*ASL: const TopoDS_Shape& aShape = anAISShape->Shape();
+    TopExp_Explorer aExpV( aShape, TopAbs_VERTEX);
+    for ( ; aExpV.More(); aExpV.Next() )
+    {
+      const TopoDS_Vertex& aVertex = TopoDS::Vertex( aExpV.Current() );
+      aVetexVec.push_back( aVertex );
+    }*/
+
+
+    //ASL: we convert list of point indices to list of points coordinates
+    int aSize = thePoints.size();
+    std::vector<gp_Pnt> aPntsToSelect( aSize );
+
+    CurveCreator_ICurve::SectionToPointList::const_iterator
+      aPIt = thePoints.begin(), aPLast = thePoints.end();
+    CurveCreator_ICurve::SectionToPoint aSToPoint;
+    for( int i=0; aPIt != aPLast; aPIt++, i++ )
+    {
+      gp_Pnt aPntToSelect;
+      CurveCreator_UtilsICurve::getPoint( myCurve, aPIt->first, aPIt->second, aPntToSelect );
+      aPntsToSelect[i] = aPntToSelect;
+    }
+
+
+    //ASL: we switch off automatic highlight to improve performance of selection
+    ic->SetAutomaticHilight( Standard_False );
+
+    Handle_SelectMgr_Selection aSelection = anAISShape->Selection( AIS_Shape::SelectionMode( TopAbs_VERTEX ) );
+    for( aSelection->Init(); aSelection->More(); aSelection->Next() )
+    {
+      Handle_SelectBasics_SensitiveEntity aSenEntity = aSelection->Sensitive();
+      Handle_Select3D_SensitivePoint aSenPnt = Handle_Select3D_SensitivePoint::DownCast( aSenEntity );
+
+      gp_Pnt anOwnerPnt = aSenPnt->Point();
+      Handle_SelectMgr_EntityOwner anOwner = Handle_SelectMgr_EntityOwner::DownCast( aSenPnt->OwnerId() );
+
+
+      CurveCreator_ICurve::SectionToPointList::const_iterator anIt = thePoints.begin(),
+                                                                     aLast = thePoints.end();
+      bool isFound = false;
+      for( int i=0; i<aSize; i++ )
+      {
+        bool isIntersect = fabs( aPntsToSelect[i].X() - anOwnerPnt.X() ) < LOCAL_SELECTION_TOLERANCE &&
+                           fabs( aPntsToSelect[i].Y() - anOwnerPnt.Y() ) < LOCAL_SELECTION_TOLERANCE;
+        if( isIntersect )
+        {
+          ic->AddOrRemoveSelected( anOwner, Standard_False );
+          break;
+        }
+      }
+    }
+  }
 
+  /*ASL: std::vector<TopoDS_Vertex>::const_iterator aVecIt = aVetexVec.begin(), aVecLast = aVetexVec.end(); 
   CurveCreator_ICurve::SectionToPointList::const_iterator anIt = thePoints.begin(),
                                                           aLast = thePoints.end();
+  int aSize = aVetexVec.size();
   CurveCreator_ICurve::SectionToPoint aSToPoint;
+  int anAddedSize = 0;
   for( ; anIt != aLast; anIt++ ) {
     aSToPoint = *anIt;
 
-    for ( AIS_ListIteratorOfListOfInteractive it( aDisplayedList ); it.More(); it.Next() )
+    for ( aVecIt = aVetexVec.begin(); aVecIt != aVecLast; aVecIt++ )
     {
-      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 );
-
-      CurveCreator_ICurve::SectionToPointList aPoints;
-      findSectionsToPoints( aPnt.X(), aPnt.Y(), aPoints );
-
-      CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aPoints.begin(),
-                                                              aLast = aPoints.end();
-      CurveCreator_ICurve::SectionToPoint aPoint;
-      for ( ; anIt != aLast; anIt++ ) {
-        aPoint = *anIt;
-        if ( aPoint.first == aSToPoint.first && aPoint.second == aSToPoint.second )
-          aListToSelect.Append( anAIS );
+      TopoDS_Vertex aVertex = TopoDS::Vertex( *aVecIt );
+      if ( isIntersectVertexToPoint( aVertex, aSToPoint ) ) {
+        ic->AddOrRemoveSelected( aVertex, Standard_False );
+        isSelectedVertex = true;
+        anAddedSize++;
       }
     }
-  }
-  if ( thePoints.size() > 0 && aListToSelect.First() == aListToSelect.Last() ) {
-    /*TopExp_Explorer aExpF(myShape,TopAbs_FACE);
-    for (; aExpF.More(); aExpF.Next())
-    {
-        const TopoDS_Face& aFace = TopoDS::Face(aExpF.Current());
-    }*/
-  }
+  }*/
 
+  //ASL: we switch on again automatic highlight (otherwise selection will not be shown)
+  //     and call HilightPicked to draw selected owners
+  ic->SetAutomaticHilight( Standard_True );
+  ic->LocalContext()->HilightPicked( Standard_True );
 
-  ic->ClearSelected( Standard_False );
-  setObjectsSelected( aListToSelect );
+  //ic->UpdateCurrentViewer();
+  if ( !isSelectedVertex )
+    setObjectsSelected( aListToSelect );
   updateLocalPointView();
 }
 
@@ -1472,24 +1505,6 @@ void CurveCreator_Widget::convert( const CurveCreator_ICurve::SectionToPointList
   return CurveCreator_UtilsICurve::convert( thePoints, theConvPoints );
 }
 
-/**
- * Returns a section index from the table
- * \param theRowId a table row
- */
-int CurveCreator_Widget::getSectionId( const int theRowId ) const
-{
-  return myLocalPointView->item( theRowId, 0 )->data( Qt::UserRole ).toInt();
-}
-
-/**
- * Returns a point index from the table
- * \param theRowId a table row
- */
-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