From: nds Date: Tue, 19 Nov 2013 10:33:12 +0000 (+0000) Subject: Insert a point on a selected segment - debug X-Git-Tag: BR_hydro_v_0_3_1~65 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=dd5464a8326937e9478012a3ca441461ad50be31;p=modules%2Fhydro.git Insert a point on a selected segment - debug Close local context during modes change, start/stop operation. Drag&drop debug. --- diff --git a/src/HYDROCurveCreator/CurveCreator_Widget.cxx b/src/HYDROCurveCreator/CurveCreator_Widget.cxx index c2f9d69a..90a8ebd9 100644 --- a/src/HYDROCurveCreator/CurveCreator_Widget.cxx +++ b/src/HYDROCurveCreator/CurveCreator_Widget.cxx @@ -70,7 +70,7 @@ const double LOCAL_SELECTION_TOLERANCE = 0.0001; const int POINT_INDEX_COLUMN_WIDTH = 50; -const int SCENE_PIXEL_TOLERANCE = 4; +const int SCENE_PIXEL_TOLERANCE = 10; CurveCreator_Widget::CurveCreator_Widget(QWidget* parent, CurveCreator_ICurve *theCurve, @@ -216,6 +216,9 @@ CurveCreator_Widget::CurveCreator_Widget(QWidget* parent, */ void CurveCreator_Widget::setOCCViewer( OCCViewer_Viewer* theViewer ) { + if ( myOCCViewer == theViewer ) + return; + if ( myOCCViewer ) { OCCViewer_ViewManager* aViewManager = dynamic_cast ( myOCCViewer->getViewManager() ); @@ -225,6 +228,8 @@ void CurveCreator_Widget::setOCCViewer( OCCViewer_Viewer* theViewer ) this, SLOT( onMouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ) ); disconnect( aViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ), this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) ); + // all local contexts should be closed if the viewer is not more used + setLocalPointContext( false, true ); } myOCCViewer = theViewer; @@ -440,7 +445,6 @@ void CurveCreator_Widget::onAdditionMode(bool checked) void CurveCreator_Widget::onModificationMode(bool checked) { myLocalPointView->setVisible( checked ); - setLocalPointContext( true ); } void CurveCreator_Widget::onDetectionMode(bool checked) @@ -449,23 +453,26 @@ void CurveCreator_Widget::onDetectionMode(bool checked) void CurveCreator_Widget::onModeChanged(bool checked) { - setLocalPointContext( false ); + ActionMode aMode = NoneMode; if (checked) { QAction* anAction = (QAction*)sender(); switch(myActionMap.key(anAction)) { case ADDITION_MODE_ID: + aMode = AdditionMode; if (myActionMap[MODIFICATION_MODE_ID]->isChecked()) myActionMap[MODIFICATION_MODE_ID]->trigger(); else if (myActionMap[DETECTION_MODE_ID]->isChecked()) myActionMap[DETECTION_MODE_ID]->trigger(); break; case MODIFICATION_MODE_ID: + aMode = ModificationMode; if (myActionMap[ADDITION_MODE_ID]->isChecked()) myActionMap[ADDITION_MODE_ID]->trigger(); else if (myActionMap[DETECTION_MODE_ID]->isChecked()) myActionMap[DETECTION_MODE_ID]->trigger(); break; case DETECTION_MODE_ID: + aMode = DetectionMode; if (myActionMap[ADDITION_MODE_ID]->isChecked()) myActionMap[ADDITION_MODE_ID]->trigger(); else if (myActionMap[MODIFICATION_MODE_ID]->isChecked()) @@ -474,6 +481,7 @@ void CurveCreator_Widget::onModeChanged(bool checked) } } onSelectionChanged(); + setLocalPointContext( aMode == ModificationMode, true ); } void CurveCreator_Widget::onAddNewPoint(const CurveCreator::Coordinates& theCoords) @@ -897,21 +905,13 @@ void CurveCreator_Widget::onMousePress( SUIT_ViewWindow*, QMouseEvent* theEvent //================================================================================= void CurveCreator_Widget::onMouseRelease( SUIT_ViewWindow*, QMouseEvent* theEvent ) { - if ( getActionMode() != ModificationMode || !myDragStarted ) + if ( getActionMode() != ModificationMode ) return; - setDragStarted( false ); - //store initial cursor position for Drag&Drop - //QPoint aPos = theEvent->pos(); - //if (pe->button() == Qt::LeftButton) - // myDragStartPosition = pe->pos(); - - QPoint aPos = theEvent->pos(); - // check whether the segment is clicked an a new point should be added to the segment - if ( insertPointToSelectedSegment( aPos.x(), aPos.y() ) ) { - setSelectedPonts( 0 ); - updateUndoRedo(); - } + if ( myDragStarted ) + setDragStarted( false ); + else // check whether the segment is clicked an a new point should be added to the segment + insertPointToSelectedSegment( theEvent->pos().x(), theEvent->pos().y() ); // updates the input panel table to show the selected point coordinates updateLocalPointView(); @@ -926,52 +926,12 @@ void CurveCreator_Widget::onMouseMove( SUIT_ViewWindow*, QMouseEvent* theEvent ) if ( getActionMode() != ModificationMode || !myDragStarted ) return; - OCCViewer_Viewer* aViewer = getOCCViewer(); - if ( !aViewer ) - return; - - //if ( !(pe->buttons() & Qt::LeftButton) ) - // return; QPoint aPos = theEvent->pos(); if ( (aPos - myDragStartPosition).manhattanLength() < QApplication::startDragDistance() ) return; - //setLocalPointContext( false ); - OCCViewer_ViewWindow* aWindow = - (OCCViewer_ViewWindow*)aViewer->getViewManager()->getActiveView(); - gp_Pnt aStartPnt = CurveCreator_Utils::ConvertClickToPoint( myDragStartPosition.x(), myDragStartPosition.y(), - aWindow->getViewPort()->getView() ); - gp_Pnt anEndPnt = CurveCreator_Utils::ConvertClickToPoint( aPos.x(), aPos.y(), - aWindow->getViewPort()->getView() ); - double aXDelta = aStartPnt.X() - anEndPnt.X(); - double anYDelta = aStartPnt.Y() - anEndPnt.Y(); - - int aSectionId = 0; - int aPointId; - std::deque aChangedPos; - for ( int i = 0, aNb = myDragPoints.size(); i < aNb; i++ ) { - aPointId = myDragPoints[i]; - 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 ); - } - - //setLocalPointContext( true ); - setSelectedPonts( aSectionId, myDragPoints ); - + moveSelectedPoints( aPos.x(), aPos.y() ); myDragStartPosition = aPos; - /* - QDrag *drag = new QDrag(this); - QMimeData *mimeData = new QMimeData; - - mimeData->setData(mimeType, data); - drag->setMimeData(mimeData); - - Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction); - */ } void CurveCreator_Widget::onLocalPointChanged( int theRow, int theColumn ) @@ -983,8 +943,7 @@ void CurveCreator_Widget::onLocalPointChanged( int theRow, int theColumn ) int aSection = aSelSections[0]; QList aSelPoints; - getSelectedPonts( aSection, aSelPoints ); - setLocalPointContext( false ); + startCurveModification( aSelPoints ); int aPntIndex = -1; int aCurrSect=-1; @@ -1006,8 +965,7 @@ void CurveCreator_Widget::onLocalPointChanged( int theRow, int theColumn ) myCurve->setPoint( aCurrSect, aPntIndex, aChangedPos ); } //} - setLocalPointContext( true ); - setSelectedPonts( aSection, aSelPoints ); + finishCurveModification( aSelPoints ); } /** @@ -1050,25 +1008,28 @@ void CurveCreator_Widget::removePoint() qSort( aPoints ); getSelectedPonts( aSectionId, aPoints ); - int aCurrSect=-1; - int aRemoveCnt = 0; + if ( aPoints.size() == 0 ) + return; + + QList aSelPoints; + startCurveModification( aSelPoints, false ); for( int i = aPoints.size()-1; i >= 0; i-- ){ int aPntIndx = aPoints[i]; myCurve->removePoint( aSectionId, aPntIndx ); mySectionView->pointsRemoved( aSectionId, aPntIndx ); } + finishCurveModification( QList() ); } -bool CurveCreator_Widget::insertPointToSelectedSegment( const int theX, +void CurveCreator_Widget::insertPointToSelectedSegment( const int theX, const int theY ) { OCCViewer_Viewer* aViewer = getOCCViewer(); if ( !aViewer ) - return false; + return; int aPoint1 = -1, aPoint2 = -1; - gp_Pnt aPoint; bool isFoundPoint = false; Handle(AIS_InteractiveContext) aContext = aViewer->getAISContext(); @@ -1086,21 +1047,69 @@ bool CurveCreator_Widget::insertPointToSelectedSegment( const int theX, isFoundPoint = pointOnObject( anAIS, theX, theY, aPoint, aPoint1, aPoint2 ); } } - // insert the point to the model curve bool isDone = false; - if ( isFoundPoint && aPoint1 >= 0 && aPoint2 >= 0 ) { - int aSection = 0; - CurveCreator::Coordinates aCoords; - aCoords.push_back( aPoint.X() ); - aCoords.push_back( aPoint.Y() ); + if ( !isFoundPoint || aPoint1 < 0 || aPoint2 < 0 ) + return; + + // insert the point to the model curve + QList aSelPoints; + startCurveModification( aSelPoints ); - int anInsertPos = aPoint1 < aPoint2 ? aPoint1 + 1 : aPoint2 + 1; - myCurve->addPoints( aCoords, aSection, anInsertPos ); - mySectionView->pointsAdded( aSection, myCurve->getNbPoints( aSection ) ); + int aSection = 0; + CurveCreator::Coordinates aCoords; + aCoords.push_back( aPoint.X() ); + aCoords.push_back( aPoint.Y() ); - isDone = true; + int anInsertPos = -1; + int aLastPoint = myCurve->getNbPoints( 0/*SectionId*/ )-1; + if ( ( aPoint1 == aLastPoint && aPoint2 == 0 ) || + ( aPoint2 == aLastPoint && aPoint1 == 0 ) ) + anInsertPos = -1; // if the section happens between first and last points + else + anInsertPos = aPoint1 < aPoint2 ? aPoint1 + 1 : aPoint2 + 1; + + myCurve->addPoints( aCoords, aSection, anInsertPos ); + mySectionView->pointsAdded( aSection, myCurve->getNbPoints( aSection ) ); + + finishCurveModification( aSelPoints ); + + setSelectedPonts( 0 ); + updateUndoRedo(); +} + +void CurveCreator_Widget::moveSelectedPoints( const int theXPosition, + const int theYPosition ) +{ + OCCViewer_Viewer* aViewer = getOCCViewer(); + if ( !aViewer ) + return; + + QList aPoints; + startCurveModification( aPoints, false ); + + OCCViewer_ViewWindow* aWindow = + (OCCViewer_ViewWindow*)aViewer->getViewManager()->getActiveView(); + gp_Pnt aStartPnt = CurveCreator_Utils::ConvertClickToPoint( myDragStartPosition.x(), + myDragStartPosition.y(), + aWindow->getViewPort()->getView() ); + gp_Pnt anEndPnt = CurveCreator_Utils::ConvertClickToPoint( theXPosition, theYPosition, + aWindow->getViewPort()->getView() ); + double aXDelta = aStartPnt.X() - anEndPnt.X(); + double anYDelta = aStartPnt.Y() - anEndPnt.Y(); + + int aSectionId = 0; + int aPointId; + std::deque aChangedPos; + for ( int i = 0, aNb = myDragPoints.size(); i < aNb; i++ ) { + aPointId = myDragPoints[i]; + 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 ); } - return isDone; + finishCurveModification( myDragPoints ); } void CurveCreator_Widget::updateLocalPointView() @@ -1150,7 +1159,10 @@ void CurveCreator_Widget::updateLocalPointView() myLocalPointView->blockSignals(isBlocked); } -void CurveCreator_Widget::setLocalPointContext( const bool theOpen ) +/** + * + */ +void CurveCreator_Widget::setLocalPointContext( const bool theOpen, const bool isUpdateTable ) { OCCViewer_Viewer* aViewer = getOCCViewer(); if ( !aViewer ) @@ -1189,7 +1201,10 @@ void CurveCreator_Widget::setLocalPointContext( const bool theOpen ) } } else { - ic->CloseAllContexts(); + if ( ic->HasOpenedContext() ) + ic->CloseAllContexts(); + if ( isUpdateTable ) + updateLocalPointView(); } } @@ -1231,10 +1246,14 @@ void CurveCreator_Widget::addLocalPointToTable( const double theX, const double */ void CurveCreator_Widget::setDragStarted( const bool theState, const QPoint& thePoint ) { - myDragStarted = theState; - if ( myDragStarted ) { - myDragStartPosition = thePoint; + if ( !theState ) { + myDragStarted = false; + myDragPoints.clear(); + } + else { getSelectedPonts( 0, myDragPoints ); + myDragStarted = myDragPoints.size(); + myDragStartPosition = thePoint; } } @@ -1292,6 +1311,37 @@ void CurveCreator_Widget::setSelectedPonts( const int theSectionId, const QList< updateLocalPointView(); } +/** + * Get viewer information before perform the curve modification. + * Take a list of selected cuve points an close local context. + * The context should be closed because the curve presentation is + * redisplayed and if it is not closed, when we close the local context + * later, the presentation shown in the local context is disappeared. + * \param thePoints an output list of curve selected points + * \param theFillPoints a flag whether the selection list should be filled + */ +void CurveCreator_Widget::startCurveModification( QList& thePoints, const bool theFillPoints ) +{ + if ( theFillPoints ) { + thePoints.clear(); + int aSectionId = 0; + getSelectedPonts( aSectionId, thePoints ); + } + setLocalPointContext( false ); +} + +/** + * Restore the viewer state after the curve modification is done. + * Open local context and select given points inside it. + * \param thePoints a list of curve selected points + */ +void CurveCreator_Widget::finishCurveModification( const QList& thePoints ) +{ + setLocalPointContext( true ); + int aSectionId = 0; + setSelectedPonts( aSectionId, thePoints ); +} + /** * Returns a point index in the model curve by the point coordinates in the viewer * \param theX the X coordinate of the point diff --git a/src/HYDROCurveCreator/CurveCreator_Widget.h b/src/HYDROCurveCreator/CurveCreator_Widget.h index 0129da99..39e752d8 100644 --- a/src/HYDROCurveCreator/CurveCreator_Widget.h +++ b/src/HYDROCurveCreator/CurveCreator_Widget.h @@ -124,10 +124,10 @@ protected: }; enum ActionMode { + NoneMode, AdditionMode, ModificationMode, - DetectionMode, - NoneMode + DetectionMode }; private: @@ -140,10 +140,11 @@ private: void removeSection(); void removePoint(); - bool insertPointToSelectedSegment( const int theXPosition, + void insertPointToSelectedSegment( const int theXPosition, const int theYPosition ); + void moveSelectedPoints( const int theXPosition, const int theYPosition ); void updateLocalPointView(); - void setLocalPointContext( const bool theOpen ); + void setLocalPointContext( const bool theOpen, const bool isUpdateTable = false ); void addLocalPointToTable( const double theX, const double theY ); void setDragStarted( const bool theState, const QPoint& thePoint = QPoint() ); @@ -151,6 +152,8 @@ private: void getSelectedPonts( const int theSectionId, QList& thePoints ); void setSelectedPonts( const int theSectionId, const QList& thePoints = QList() ); + void startCurveModification( QList& thePoints, const bool theFillPoints = true ); + void finishCurveModification( const QList& thePoints ); // curve algorithm int findLocalPointIndex( int theSectionId, float theX, float theY );