#include <AIS_Point.hxx>
#include <AIS_Line.hxx>
#include <Geom_Point.hxx>
+#include <Geom_BSplineCurve.hxx>
+#include <Geom_Line.hxx>
#include <StdSelect_BRepOwner.hxx>
#include <QHBoxLayout>
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,
Qt::WindowFlags fl)
-: QWidget(parent), myNewSectionEditor(NULL), myCurve(theCurve), mySection(0), myDragStarted( false ),
+: QWidget(parent), myNewSectionEditor(NULL), myCurve(theCurve), mySection(0),
+ myDragStarted( false ), myDragInteractionStyle( SUIT_ViewModel::STANDARD ),
myOCCViewer( 0 )
{
myNewSectionEditor = new CurveCreator_NewSectionDlg( this );
*/
void CurveCreator_Widget::setOCCViewer( OCCViewer_Viewer* theViewer )
{
+ if ( myOCCViewer == theViewer )
+ return;
+
if ( myOCCViewer ) {
OCCViewer_ViewManager* aViewManager = dynamic_cast<OCCViewer_ViewManager*>
( myOCCViewer->getViewManager() );
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;
if ( myOCCViewer ) {
- //OCCViewer_ViewWindow* aWnd = dynamic_cast<OCCViewer_ViewWindow*>(myOCCViewer->getViewManager()->getActiveView());
- //if ( aWnd )
- // aWnd->installEventFilter( this );
OCCViewer_ViewManager* aViewManager = dynamic_cast<OCCViewer_ViewManager*>
( myOCCViewer->getViewManager() );
connect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
return myOCCViewer;
}
-/*!
- \brief Customize event handling
- \param watched event receiver object
- \param e event
- \return \c true if the event processing should be stopped
-*/
-/*bool CurveCreator_Widget::eventFilter( QObject* theWatched, QEvent* theEvent )
-{
- OCCViewer_Viewer* aViewer = getOCCViewer();
- if ( !aViewer )
- return;
- Handle(AIS_InteractiveContext) aContext = aViewer->getAISContext();
- bool isLocalContext = aContext->HasOpenedContext();
- if ( !isLocalContext )
- return QWidget::eventFilter( theWatched, theEvent );
-
- bool isProcessed = true;
-
- return isProcessed;
-}*/
-
//=======================================================================
// function: getUniqSectionName
// purpose: return unique section name
}
}
}
+ updateUndoRedo();
emit selectionChanged();
}
void CurveCreator_Widget::onModificationMode(bool checked)
{
myLocalPointView->setVisible( checked );
- setLocalPointContext( true );
}
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())
}
}
onSelectionChanged();
+ setLocalPointContext( aMode == ModificationMode, true );
}
void CurveCreator_Widget::onAddNewPoint(const CurveCreator::Coordinates& theCoords)
{
if( !myCurve )
return;
- myCurve->addPoints(theCoords, mySection, myPointNum );
- mySectionView->pointsAdded( mySection, myPointNum );
- myPointNum++;
+ //myCurve->addPoints(theCoords, mySection, myPointNum );
+ //mySectionView->pointsAdded( mySection, myPointNum );
+ //myPointNum++;
+ QList<int> aSections = mySectionView->getSelectedSections();
+ if( aSections.size() == 0 ){
+ return;
+ }
+ int aSection = aSections[0];
+ myCurve->addPoints(theCoords, aSection); // add to the end of section
+ mySectionView->pointsAdded( aSection, myCurve->getNbPoints( aSection ) );
onSelectionChanged();
updateUndoRedo();
}
void CurveCreator_Widget::updateUndoRedo()
{
- QAction* anAct = myActionMap[UNDO_ID];
- if( anAct != 0 ){
- if( myCurve->getNbUndo() != 0 ){
- anAct->setEnabled(true);
- }
- else{
- anAct->setDisabled(true);
- }
+ if( !myCurve )
+ return;
+ QAction* anAct = myActionMap[UNDO_ID];
+ if( anAct != 0 ){
+ if( myCurve->getNbUndo() != 0 ){
+ anAct->setEnabled(true);
}
- anAct = myActionMap[REDO_ID];
- if( anAct != 0 ){
- if( myCurve->getNbRedo() != 0 ){
- anAct->setEnabled(true);
- }
- else{
- anAct->setDisabled(true);
- }
+ else{
+ anAct->setDisabled(true);
+ }
+ }
+ anAct = myActionMap[REDO_ID];
+ if( anAct != 0 ){
+ if( myCurve->getNbRedo() != 0 ){
+ anAct->setEnabled(true);
+ }
+ else{
+ anAct->setDisabled(true);
}
+ }
}
void CurveCreator_Widget::onContextMenu( QPoint thePoint )
//=================================================================================
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 ) {
+ if ( myDragged ) // if the drag of some points has happened, restore the drag selection
+ setSelectedPonts( 0, myDragPoints );
+ 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();
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<float> 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 )
int aSection = aSelSections[0];
QList<int> aSelPoints;
- getSelectedPonts( aSection, aSelPoints );
- setLocalPointContext( false );
+ startCurveModification( aSelPoints );
int aPntIndex = -1;
int aCurrSect=-1;
myCurve->setPoint( aCurrSect, aPntIndex, aChangedPos );
}
//}
- setLocalPointContext( true );
- setSelectedPonts( aSection, aSelPoints );
+ finishCurveModification( aSelPoints );
}
/**
{
int aSectionId = 0;
QList<int> aPoints;
- qSort( aPoints );
getSelectedPonts( aSectionId, aPoints );
+ if ( aPoints.size() == 0 )
+ return;
- int aCurrSect=-1;
- int aRemoveCnt = 0;
+ QList<int> aSelPoints;
+ startCurveModification( aSelPoints, false );
+ // the points should be removed in a decreased order
+ qSort( aPoints );
for( int i = aPoints.size()-1; i >= 0; i-- ){
int aPntIndx = aPoints[i];
myCurve->removePoint( aSectionId, aPntIndx );
mySectionView->pointsRemoved( aSectionId, aPntIndx );
}
+ finishCurveModification( QList<int>() );
}
-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();
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<int> aSelPoints;
+ startCurveModification( aSelPoints );
+
+ int aSection = 0;
+ CurveCreator::Coordinates aCoords;
+ aCoords.push_back( aPoint.X() );
+ aCoords.push_back( aPoint.Y() );
- int anInsertPos = aPoint1 < aPoint2 ? aPoint1 + 1 : aPoint2 + 1;
- myCurve->addPoints( aCoords, aSection, anInsertPos );
- mySectionView->pointsAdded( aSection, myCurve->getNbPoints( aSection ) );
+ 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;
- isDone = true;
+ 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<int> 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<float> 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;
+ myDragged = true;
+ finishCurveModification( myDragPoints );
}
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 )
}
}
else {
- ic->CloseAllContexts();
+ if ( ic->HasOpenedContext() )
+ ic->CloseAllContexts();
+ if ( isUpdateTable )
+ updateLocalPointView();
}
}
*/
void CurveCreator_Widget::setDragStarted( const bool theState, const QPoint& thePoint )
{
- myDragStarted = theState;
- if ( myDragStarted ) {
- myDragStartPosition = thePoint;
+ OCCViewer_Viewer* aViewer = getOCCViewer();
+ if ( theState ) {
getSelectedPonts( 0, myDragPoints );
+ myDragStarted = myDragPoints.size();
+ myDragStartPosition = thePoint;
+ if ( aViewer && myDragStarted ) {
+ // change a viewer interaction style in order to avoid a select rectangle build
+ myDragInteractionStyle = aViewer->interactionStyle();
+ aViewer->setInteractionStyle(SUIT_ViewModel::KEY_FREE);
+ }
}
+ else {
+ if ( aViewer && myDragStarted )
+ aViewer->setInteractionStyle( myDragInteractionStyle );
+ myDragStarted = false;
+ myDragPoints.clear();
+ }
+ myDragged = false;
}
void CurveCreator_Widget::getSelectedPonts( int theSectionId, QList<int>& thePoints )
aListToSelect.Append( anAIS );
}
+ ic->ClearSelected( Standard_False );
aViewer->setObjectsSelected( aListToSelect );
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<int>& 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<int>& 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
return aPntIndex;
}
+void CurveCreator_Widget::findSections( int thePointId, QList<int>& theSections )
+{
+
+}
+
/**
* Checks whether the point belongs to the OCC object
* \param theObject a line or shape with a bspline inside
return isFound;
gp_Pnt aPoint;
+ Standard_Real aParameter;
gp_Pnt aPnt1, aPnt2;
Handle(AIS_Line) aLine = Handle(AIS_Line)::DownCast( theObject );
if ( !aLine.IsNull() ) {
const Handle(Geom_Line) aGLine = aLine->Line();
- Handle(Geom_Point) aPStart;
- Handle(Geom_Point) aPEnd;
- aLine->Points( aPStart, aPEnd );
- aPnt1 = aPStart->Pnt();
- aPnt2 = aPEnd->Pnt();
- isFound = hasProjectPointOnCurve( theX, theY, aGLine, aPoint );
+ isFound = hasProjectPointOnCurve( theX, theY, aGLine, aParameter );
+ if ( isFound ) {
+ aPoint = aGLine->Value( aParameter );
+
+ Handle(Geom_Point) aPStart;
+ Handle(Geom_Point) aPEnd;
+ aLine->Points( aPStart, aPEnd );
+ aPnt1 = aPStart->Pnt();
+ aPnt2 = aPEnd->Pnt();
+
+ // in case of Geom line a projection is performed to the infinite line,
+ // so it is necessary to bound it by the line size
+ Bnd_Box aLineBox;
+ aLineBox.Set( aPnt1, gp_Vec( aPnt1, aPnt2 ) );
+ isFound = !aLineBox.IsOut( aPoint );
+ }
}
else {
Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
if ( !anEdge.IsNull() ) {
Standard_Real aFirst, aLast;
Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
- aCurve->D0(aFirst,aPnt1);
- aCurve->D0(aLast,aPnt2);
- isFound = hasProjectPointOnCurve( theX, theY, aCurve, aPoint );
+
+ if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
+ Handle(Geom_BSplineCurve) aBSplineCurve = Handle(Geom_BSplineCurve)::DownCast( aCurve );
+ if ( !aBSplineCurve.IsNull() ) {
+ isFound = hasProjectPointOnCurve( theX, theY, aCurve, aParameter );
+ if ( isFound ) {
+ aPoint = aBSplineCurve->Value( aParameter );
+ Standard_Integer anI1, anI2;
+ aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
+
+ aPnt1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
+ aPnt2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
+ }
+ }
+ }
}
}
}
*/
bool CurveCreator_Widget::hasProjectPointOnCurve( const int theX, const int theY,
const Handle(Geom_Curve)& theCurve,
- gp_Pnt& theOutPoint )
+ Standard_Real& theParameter )
{
bool isFound = false;
OCCViewer_Viewer* aViewer = getOCCViewer();
Standard_Integer aNbPoint = aProj.NbPoints();
if (aNbPoint > 0) {
for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
- gp_Pnt aNewPoint = aProj.Point(j);
+ gp_Pnt aNewPoint = aProj.Point( j );
+ theParameter = aProj.Parameter( j );
int aX, anY;
CurveCreator_Utils::ConvertPointToClick( aNewPoint, aView, aX, anY );
int aXDelta = abs( aX - theX );
int anYDelta = abs( anY - theY );
isFound = aXDelta < SCENE_PIXEL_TOLERANCE && anYDelta < SCENE_PIXEL_TOLERANCE;
- if ( isFound ) {
- theOutPoint = aNewPoint;
- }
}
}
return isFound;