1 // Copyright (C) 2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "CurveCreator_Widget.h"
21 #include "CurveCreator_TreeView.h"
22 #include "CurveCreator_ICurve.hxx"
23 //#include "CurveCreator_CurveEditor.hxx"
24 #include "CurveCreator.hxx"
25 //#include "CurveCreator_NewPointDlg.h"
26 #include "CurveCreator_NewSectionDlg.h"
27 #include "CurveCreator_Utils.h"
29 #include <SUIT_Session.h>
30 #include <SUIT_Desktop.h>
31 #include <SUIT_ResourceMgr.h>
32 #include <SUIT_ViewManager.h>
34 #include <OCCViewer_ViewWindow.h>
35 #include <OCCViewer_ViewManager.h>
36 #include <OCCViewer_ViewPort3d.h>
38 #include <BRep_Tool.hxx>
40 #include <TopoDS_Vertex.hxx>
41 #include <TopoDS_Wire.hxx>
42 #include <TopoDS_Edge.hxx>
45 #include <TopExp_Explorer.hxx>
47 #include <GeomAPI_ProjectPointOnCurve.hxx>
49 #include <AIS_ListOfInteractive.hxx>
50 #include <AIS_ListIteratorOfListOfInteractive.hxx>
51 #include <AIS_Shape.hxx>
52 #include <AIS_Point.hxx>
53 #include <AIS_Line.hxx>
54 #include <Geom_Point.hxx>
55 #include <StdSelect_BRepOwner.hxx>
57 #include <QHBoxLayout>
58 #include <QVBoxLayout>
62 #include <QToolButton>
66 #include <QMouseEvent>
67 #include <QApplication>
68 #include <QTableWidget>
70 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
71 const int POINT_INDEX_COLUMN_WIDTH = 50;
73 const int SCENE_PIXEL_TOLERANCE = 4;
75 CurveCreator_Widget::CurveCreator_Widget(QWidget* parent,
76 CurveCreator_ICurve *theCurve,
78 : QWidget(parent), myNewSectionEditor(NULL), myCurve(theCurve), mySection(0), myDragStarted( false ),
81 myNewSectionEditor = new CurveCreator_NewSectionDlg( this );
82 myNewSectionEditor->hide();
83 connect( myNewSectionEditor, SIGNAL(addSection()), this, SLOT(onAddNewSection()) );
84 connect( myNewSectionEditor, SIGNAL(modifySection()), this, SLOT(onModifySection()) );
85 connect( myNewSectionEditor, SIGNAL(cancelSection()), this, SLOT(onCancelSection()) );
87 QGroupBox* aSectionGroup = new QGroupBox(tr("Sections"),this);
89 mySectionView = new CurveCreator_TreeView(myCurve, aSectionGroup);
90 connect( mySectionView, SIGNAL(selectionChanged()), this, SLOT( onSelectionChanged() ) );
91 connect( mySectionView, SIGNAL(sectionEntered(int)), this, SLOT(onEditSection(int)) );
92 connect( mySectionView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onContextMenu(QPoint)) );
94 myLocalPointView = new QTableWidget();
95 myLocalPointView->setVisible( false );
96 myLocalPointView->setColumnCount( 3 );
97 myLocalPointView->setColumnWidth( 0, POINT_INDEX_COLUMN_WIDTH );
99 //aLabels << tr( "IDENTIFIER_LABEL" ) << tr( "X_POSITION_LBL" ) << tr( "Y_POSITION_LBL" );
100 aLabels << tr( "id" ) << tr( "X" ) << tr( "Y" );
101 myLocalPointView->setHorizontalHeaderLabels( aLabels );
102 connect( myLocalPointView, SIGNAL( cellChanged( int, int ) ),
103 this, SLOT( onLocalPointChanged( int, int ) ) );
105 QToolBar* aTB = new QToolBar(tr("TOOL_BAR_TLT"), aSectionGroup);
106 // QToolButton* anUndoBtn = new QToolButton(aTB);
108 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
109 QPixmap anUndoPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_UNDO")));
110 QPixmap aRedoPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_REDO")));
111 QPixmap aNewSectionPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_NEW_SECTION")));
112 QPixmap aNewPointPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_NEW_POINT")));
113 QPixmap anEditPointsPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_EDIT_POINTS")));
114 QPixmap aDetectPointsPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_EDIT_POINTS")));
115 QPixmap aPolylinePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_POLYLINE")));
116 QPixmap aSplinePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_SPLINE")));
117 QPixmap aRemovePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_DELETE")));
118 QPixmap aJoinPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_JOIN")));
119 QPixmap aStepUpPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_ARROW_UP")));
120 QPixmap aStepDownPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_ARROW_DOWN")));
122 QAction* anAct = createAction( UNDO_ID, tr("UNDO"), anUndoPixmap, tr("UNDO_TLT"),
123 QKeySequence(Qt::ControlModifier|Qt::Key_Z) );
124 connect(anAct, SIGNAL(triggered()), this, SLOT(onUndo()) );
125 aTB->addAction(anAct);
127 anAct = createAction( REDO_ID, tr("REDO"), aRedoPixmap, tr("REDO_TLT"),
128 QKeySequence(Qt::ControlModifier|Qt::Key_Y) );
129 connect(anAct, SIGNAL(triggered()), this, SLOT(onRedo()) );
130 aTB->addAction(anAct);
134 anAct = createAction( NEW_SECTION_ID, tr("NEW_SECTION"), aNewSectionPixmap, tr("NEW_SECTION_TLT"),
135 QKeySequence(Qt::ControlModifier|Qt::Key_N) );
136 connect(anAct, SIGNAL(triggered()), this, SLOT(onNewSection()) );
137 aTB->addAction(anAct);
140 anAct = createAction( ADDITION_MODE_ID, tr("ADDITION_MODE"), aNewPointPixmap, tr("ADDITION_MODE_TLT"),
142 anAct->setCheckable(true);
143 connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onAdditionMode(bool)) );
144 connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
145 aTB->addAction(anAct);
147 anAct = createAction( MODIFICATION_MODE_ID, tr("MODIFICATION_MODE"), anEditPointsPixmap, tr("MODIFICATION_MODE_TLT"),
149 anAct->setCheckable(true);
150 connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onModificationMode(bool)) );
151 connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
152 aTB->addAction(anAct);
154 anAct = createAction( DETECTION_MODE_ID, tr("DETECTION_MODE"), aDetectPointsPixmap, tr("DETECTION_MODE_TLT"),
156 anAct->setCheckable(true);
157 connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onDetectionMode(bool)) );
158 connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
159 aTB->addAction(anAct);
161 anAct = createAction( CLOSE_SECTIONS_ID, tr("CLOSE_SECTIONS"), QPixmap(), tr("CLOSE_SECTIONS_TLT"),
162 QKeySequence(Qt::ControlModifier|Qt::Key_W) );
163 connect(anAct, SIGNAL(triggered()), this, SLOT(onCloseSections()) );
165 anAct = createAction( UNCLOSE_SECTIONS_ID, tr("UNCLOSE_SECTIONS"), QPixmap(),
166 tr("UNCLOSE_SECTIONS_TLT"), QKeySequence(Qt::ControlModifier|Qt::Key_S) );
167 connect(anAct, SIGNAL(triggered()), this, SLOT(onUncloseSections()) );
169 anAct = createAction( SET_SECTIONS_POLYLINE_ID, tr("SET_SECTIONS_POLYLINE"),
170 aPolylinePixmap, tr("SET_POLYLINE_TLT"),
171 QKeySequence(Qt::ControlModifier|Qt::Key_E) );
172 connect(anAct, SIGNAL(triggered()), this, SLOT(onSetPolyline()) );
174 anAct = createAction( SET_SECTIONS_SPLINE_ID, tr("SET_SECTIONS_SPLINE"), aSplinePixmap,
175 tr("SET_SPLINE_TLT"), QKeySequence(Qt::ControlModifier|Qt::Key_R) );
176 connect(anAct, SIGNAL(triggered()), this, SLOT(onSetSpline()) );
178 anAct = createAction( REMOVE_ID, tr("REMOVE"), aRemovePixmap, tr("REMOVE_TLT"),
179 QKeySequence(Qt::ControlModifier|Qt::Key_Delete ) );
180 connect(anAct, SIGNAL(triggered()), this, SLOT(onRemove()) );
181 aTB->addAction(anAct);
184 anAct = createAction( JOIN_ID, tr("JOIN"), aJoinPixmap, tr("JOIN_TLT"),
185 QKeySequence(Qt::ControlModifier|Qt::Key_Plus ) );
186 connect( anAct, SIGNAL(triggered()), this, SLOT(onJoin()) );
187 aTB->addAction(anAct);
190 anAct = createAction( CLEAR_ALL_ID, tr("CLEAR_ALL"), QPixmap(), tr("CLEAR_ALL_TLT"),
191 QKeySequence(Qt::ControlModifier | Qt::ShiftModifier | Qt::Key_Delete ) );
192 connect( anAct, SIGNAL(triggered()), this, SLOT( onClearAll()) );
194 anAct = createAction( JOIN_ALL_ID, tr("JOIN_ALL"), QPixmap(), tr("JOIN_ALL_TLT"),
195 QKeySequence(Qt::ControlModifier | Qt::ShiftModifier | Qt::Key_Plus ) );
196 connect( anAct, SIGNAL(triggered()), this, SLOT(onJoinAll()) );
198 QVBoxLayout* aSectLayout = new QVBoxLayout();
199 aSectLayout->setMargin( 5 );
200 aSectLayout->setSpacing( 5 );
201 aSectLayout->addWidget(aTB);
202 aSectLayout->addWidget(mySectionView);
203 aSectLayout->addWidget( myLocalPointView );
204 aSectionGroup->setLayout(aSectLayout);
205 QVBoxLayout* aLay = new QVBoxLayout();
206 aLay->setMargin( 0 );
207 aLay->setSpacing( 5 );
208 // aLay->addLayout(aNameLayout);
209 aLay->addWidget(aSectionGroup);
211 onSelectionChanged();
217 void CurveCreator_Widget::setOCCViewer( OCCViewer_Viewer* theViewer )
220 OCCViewer_ViewManager* aViewManager = dynamic_cast<OCCViewer_ViewManager*>
221 ( myOCCViewer->getViewManager() );
222 disconnect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
223 this, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ) );
224 disconnect( aViewManager, SIGNAL( mouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ),
225 this, SLOT( onMouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ) );
226 disconnect( aViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
227 this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
230 myOCCViewer = theViewer;
232 //OCCViewer_ViewWindow* aWnd = dynamic_cast<OCCViewer_ViewWindow*>(myOCCViewer->getViewManager()->getActiveView());
234 // aWnd->installEventFilter( this );
235 OCCViewer_ViewManager* aViewManager = dynamic_cast<OCCViewer_ViewManager*>
236 ( myOCCViewer->getViewManager() );
237 connect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
238 this, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ) );
239 connect( aViewManager, SIGNAL( mouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ),
240 this, SLOT( onMouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ) );
241 connect( aViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
242 this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
247 * Returns current OCC viewer
249 OCCViewer_Viewer* CurveCreator_Widget::getOCCViewer()
255 \brief Customize event handling
256 \param watched event receiver object
258 \return \c true if the event processing should be stopped
260 /*bool CurveCreator_Widget::eventFilter( QObject* theWatched, QEvent* theEvent )
262 OCCViewer_Viewer* aViewer = getOCCViewer();
265 Handle(AIS_InteractiveContext) aContext = aViewer->getAISContext();
266 bool isLocalContext = aContext->HasOpenedContext();
267 if ( !isLocalContext )
268 return QWidget::eventFilter( theWatched, theEvent );
270 bool isProcessed = true;
275 //=======================================================================
276 // function: getUniqSectionName
277 // purpose: return unique section name
278 //=======================================================================
279 std::string CurveCreator_Widget::getUniqSectionName( CurveCreator_ICurve* theCurve ) const
281 for( int i = 0 ; i < 1000000 ; i++ ){
283 sprintf( aBuffer, "Section_%d", i+1 );
284 std::string aName(aBuffer);
286 for( j = 0 ; j < theCurve->getNbSections() ; j++ ){
287 if( theCurve->getSectionName(j) == aName )
290 if( j == theCurve->getNbSections() )
296 void CurveCreator_Widget::setCurve( CurveCreator_ICurve* theCurve )
299 mySectionView->setCurve(myCurve);
300 onSelectionChanged();
304 void CurveCreator_Widget::onSelectionChanged()
306 QList<ActionId> anEnabledAct;
308 anEnabledAct << NEW_SECTION_ID;
309 QList<int> aSelSections = mySectionView->getSelectedSections();
310 QList< QPair< int, int > > aSelPoints = mySectionView->getSelectedPoints();
311 CurveCreator_TreeView::SelectionType aSelType = mySectionView->getSelectionType();
313 case CurveCreator_TreeView::ST_NOSEL:{
316 case CurveCreator_TreeView::ST_SECTIONS:{
317 /*if( aSelSections[0] > 0 ){
318 anEnabledAct << UP_ID;
320 if( aSelSections.size() == 1 ){
321 anEnabledAct << ADDITION_MODE_ID << MODIFICATION_MODE_ID << DETECTION_MODE_ID;
322 anEnabledAct << REMOVE_ID;
324 switch ( getActionMode() ) {
328 QList<int> aSelSection = mySectionView->getSelectedSections();
329 if( aSelSection.size() > 0 ){
330 mySection = aSelSection[0];
331 myPointNum = myCurve->getNbPoints(mySection);
335 case ModificationMode: {
336 anEnabledAct << CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID << SET_SECTIONS_POLYLINE_ID << SET_SECTIONS_SPLINE_ID;
337 int aSectCnt = myCurve->getNbSections();
339 anEnabledAct << CLEAR_ALL_ID;
341 anEnabledAct << JOIN_ALL_ID;
342 if( aSelSections.size() > 1 ){
343 anEnabledAct << JOIN_ID;
347 case DetectionMode: {
354 /*if( aSelSections[ aSelSections.size() - 1 ] < ( myCurve->getNbSections() - 1 ) ){
355 anEnabledAct << DOWN_ID;
359 /*case CurveCreator_TreeView::ST_POINTS_ONE_SECTION:{
360 if( aSelPoints[0].second > 0 ){
361 anEnabledAct << UP_ID;
363 int aLastIndex = aSelPoints.size()-1;
364 int aSect = aSelPoints[0].first;
365 if( aSelPoints[aLastIndex].second < (myCurve->getNbPoints(aSect) - 1)){
366 anEnabledAct << DOWN_ID;
368 if( aSelPoints.size() == 1){
369 anEnabledAct << INSERT_POINT_BEFORE_ID << INSERT_POINT_AFTER_ID;
376 /*int aSelObjsCnt = aSelPoints.size() + aSelSections.size();
377 if( aSelObjsCnt > 0 ){
378 anEnabledAct << REMOVE_ID;
380 if( (myCurve->getNbSections() + myCurve->getNbPoints()) > 0 ){
381 anEnabledAct << REMOVE_ALL_ID;
383 if( myCurve->getNbSections() > 1 ){
384 anEnabledAct << JOIN_ALL_ID;
387 QList<ActionId> anIds = myActionMap.keys();
388 for( int i = 0 ; i < anIds.size() ; i++ ){
389 if( myActionMap.contains(anIds[i]) ){
390 if( anEnabledAct.contains(anIds[i]) ){
391 myActionMap[anIds[i]]->setEnabled(true);
394 myActionMap[anIds[i]]->setEnabled(false);
398 emit selectionChanged();
401 void CurveCreator_Widget::onAdditionMode(bool checked)
403 OCCViewer_Viewer* aViewer = getOCCViewer();
404 if( !myCurve || !aViewer )
407 Handle(AIS_InteractiveContext) aContext = aViewer->getAISContext();
408 OCCViewer_ViewManager* aViewManager = dynamic_cast<OCCViewer_ViewManager*>
409 (aViewer->getViewManager());
410 // if ( aViewManager->getType() == OCCViewer_Viewer::Type() ) {
413 /* myGuiState = aViewWindow->saveState();
414 aViewer->enableMultiselection(false);
415 aViewer->enableSelection(false);*/
417 /* aViewer->enableMultiselection(true);
418 aViewer->enableSelection(true);
419 aViewWindow->restoreState( myGuiState );*/
426 QList<int> aSelSection = mySectionView->getSelectedSections();
427 if( aSelSection.size() > 0 ){
428 mySection = aSelSection[0];
431 QList< QPair<int,int> > aSelPoints = mySectionView->getSelectedPoints();
432 if( aSelPoints.size() > 0 ){
433 mySection = aSelPoints[0].first;
434 myPointNum = aSelPoints[0].second + 1;
437 // emit subOperationStarted( myNewPointEditor );
440 void CurveCreator_Widget::onModificationMode(bool checked)
442 myLocalPointView->setVisible( checked );
443 setLocalPointContext( true );
446 void CurveCreator_Widget::onDetectionMode(bool checked)
450 void CurveCreator_Widget::onModeChanged(bool checked)
452 setLocalPointContext( false );
454 QAction* anAction = (QAction*)sender();
455 switch(myActionMap.key(anAction)) {
456 case ADDITION_MODE_ID:
457 if (myActionMap[MODIFICATION_MODE_ID]->isChecked())
458 myActionMap[MODIFICATION_MODE_ID]->trigger();
459 else if (myActionMap[DETECTION_MODE_ID]->isChecked())
460 myActionMap[DETECTION_MODE_ID]->trigger();
462 case MODIFICATION_MODE_ID:
463 if (myActionMap[ADDITION_MODE_ID]->isChecked())
464 myActionMap[ADDITION_MODE_ID]->trigger();
465 else if (myActionMap[DETECTION_MODE_ID]->isChecked())
466 myActionMap[DETECTION_MODE_ID]->trigger();
468 case DETECTION_MODE_ID:
469 if (myActionMap[ADDITION_MODE_ID]->isChecked())
470 myActionMap[ADDITION_MODE_ID]->trigger();
471 else if (myActionMap[MODIFICATION_MODE_ID]->isChecked())
472 myActionMap[MODIFICATION_MODE_ID]->trigger();
476 onSelectionChanged();
479 void CurveCreator_Widget::onAddNewPoint(const CurveCreator::Coordinates& theCoords)
483 myCurve->addPoints(theCoords, mySection, myPointNum );
484 mySectionView->pointsAdded( mySection, myPointNum );
486 onSelectionChanged();
490 void CurveCreator_Widget::onNewSection()
494 myNewSectionEditor->clear();
495 myNewSectionEditor->setEditMode(false);
496 QString aSectName = QString( getUniqSectionName(myCurve).c_str() );
497 myNewSectionEditor->setSectionParameters(aSectName, true, CurveCreator::Polyline );
498 emit subOperationStarted( myNewSectionEditor );
501 void CurveCreator_Widget::onAddNewSection()
505 myCurve->addSection( myNewSectionEditor->getName().toStdString(), myNewSectionEditor->getSectionType(),
506 myNewSectionEditor->isClosed() );
507 mySectionView->sectionAdded( -1 ); // add a new section to the end of list
508 QString aNewName = QString(getUniqSectionName(myCurve).c_str());
509 myNewSectionEditor->setSectionName(aNewName);
510 onSelectionChanged();
515 void CurveCreator_Widget::onCancelSection()
517 emit subOperationFinished( myNewSectionEditor );
520 QAction* CurveCreator_Widget::createAction( ActionId theId, const QString& theName, const QPixmap& theImage,
521 const QString& theToolTip, const QKeySequence& theShortcut )
523 QAction* anAct = new QAction(theName,this);
524 if( !theImage.isNull() ){
525 anAct->setIcon(theImage);
527 anAct->setShortcut(theShortcut);
528 anAct->setToolTip(theToolTip);
529 myActionMap[theId] = anAct;
533 QAction* CurveCreator_Widget::getAction(ActionId theId)
535 if( myActionMap.contains(theId) )
536 return myActionMap[theId];
540 CurveCreator_Widget::ActionMode CurveCreator_Widget::getActionMode() const
542 ActionMode aMode = NoneMode;
544 if ( myActionMap[ADDITION_MODE_ID]->isChecked() )
545 aMode = AdditionMode;
546 else if ( myActionMap[MODIFICATION_MODE_ID]->isChecked() )
547 aMode = ModificationMode;
548 else if ( myActionMap[DETECTION_MODE_ID]->isChecked() )
549 aMode = DetectionMode;
554 void CurveCreator_Widget::onEditSection( int theSection )
558 mySection = theSection;
559 QString aSectName = QString::fromStdString( myCurve->getSectionName(theSection));
560 bool isClosed = myCurve->isClosed(theSection);
561 CurveCreator::SectionType aType = myCurve->getSectionType(theSection);
562 myNewSectionEditor->setEditMode(true);
563 myNewSectionEditor->setSectionParameters( aSectName, isClosed, aType );
565 emit subOperationStarted( myNewSectionEditor );
568 void CurveCreator_Widget::onModifySection()
572 QString aName = myNewSectionEditor->getName();
573 bool isClosed = myNewSectionEditor->isClosed();
574 CurveCreator::SectionType aSectType = myNewSectionEditor->getSectionType();
575 // myCurve->startOperation();
576 myCurve->setClosed( isClosed, mySection );
577 myCurve->setSectionName( mySection , aName.toStdString() );
578 myCurve->setSectionType( mySection, aSectType );
579 // myCurve->finishOperation();
580 mySectionView->sectionChanged(mySection);
585 /*void CurveCreator_Widget::onEditPoint( int theSection, int thePoint )
587 if( !myNewPointEditor || !myEdit )
589 mySection = theSection;
590 myPointNum = thePoint;
591 QString aSectName = QString::fromStdString( myCurve->getSectionName(theSection));
592 myNewPointEditor->setEditMode(true);
593 myNewPointEditor->setSectionName(aSectName);
594 myNewPointEditor->setDimension( myCurve->getDimension() );
595 CurveCreator::Coordinates aCoords = myCurve->getCoordinates(theSection,thePoint);
596 myNewPointEditor->setCoordinates(aCoords);
597 emit subOperationStarted( myNewPointEditor );
600 void CurveCreator_Widget::onModifyPoint()
604 CurveCreator::Coordinates aCoords = myNewPointEditor->getCoordinates();
605 myEdit->setCoordinates( aCoords, mySection, myPointNum );
606 mySectionView->pointDataChanged( mySection, myPointNum );
611 void CurveCreator_Widget::onJoin()
615 QList<int> aSections = mySectionView->getSelectedSections();
616 if( aSections.size() == 0 ){
619 int aMainSect = aSections[0];
620 int aMainSectSize = myCurve->getNbPoints(aMainSect);
621 // myCurve->startOperation();
622 for( int i = 1 ; i < aSections.size() ; i++ ){
623 int aSectNum = aSections[i] - (i-1);
624 myCurve->join( aMainSect, aSectNum );
625 mySectionView->sectionsRemoved( aSectNum );
627 // myCurve->finishOperation();
628 int aNewSectSize = myCurve->getNbPoints(aMainSect);
629 if( aNewSectSize != aMainSectSize )
630 mySectionView->pointsAdded( aMainSect, aMainSectSize, aNewSectSize-aMainSectSize );
634 void CurveCreator_Widget::onRemove()
639 switch( getActionMode() ) {
643 case ModificationMode:
651 void CurveCreator_Widget::onClearAll()
656 mySectionView->reset();
657 onSelectionChanged();
661 void CurveCreator_Widget::onJoinAll()
666 mySectionView->reset();
667 onSelectionChanged();
671 void CurveCreator_Widget::onUndoSettings()
676 void CurveCreator_Widget::onSetSpline()
680 QList<int> aSelSections = mySectionView->getSelectedSections();
681 // myCurve->startOperation();
682 for( int i = 0 ; i < aSelSections.size() ; i++ ){
683 myCurve->setSectionType(aSelSections[i], CurveCreator::Spline );
684 mySectionView->sectionChanged(aSelSections[i]);
686 // myCurve->finishOperation();
690 void CurveCreator_Widget::onSetPolyline()
694 // myCurve->startOperation();
695 QList<int> aSelSections = mySectionView->getSelectedSections();
696 for( int i = 0 ; i < aSelSections.size() ; i++ ){
697 myCurve->setSectionType( aSelSections[i], CurveCreator::Polyline );
698 mySectionView->sectionChanged( aSelSections[i] );
700 // myCurve->finishOperation();
704 void CurveCreator_Widget::onCloseSections()
708 // myCurve->startOperation();
709 QList<int> aSelSections = mySectionView->getSelectedSections();
710 for( int i = 0 ; i < aSelSections.size() ; i++ ){
711 myCurve->setClosed(true, aSelSections[i]);
712 mySectionView->sectionChanged(aSelSections[i]);
714 // myCurve->finishOperation();
718 void CurveCreator_Widget::onUncloseSections()
722 // myCurve->startOperation();
723 QList<int> aSelSections = mySectionView->getSelectedSections();
724 for( int i = 0 ; i < aSelSections.size() ; i++ ){
725 myCurve->setClosed(false, aSelSections[i]);
726 mySectionView->sectionChanged(aSelSections[i]);
728 // myCurve->finishOperation();
732 void CurveCreator_Widget::onUndo()
737 mySectionView->reset();
741 void CurveCreator_Widget::onRedo()
746 mySectionView->reset();
750 void CurveCreator_Widget::updateUndoRedo()
752 QAction* anAct = myActionMap[UNDO_ID];
754 if( myCurve->getNbUndo() != 0 ){
755 anAct->setEnabled(true);
758 anAct->setDisabled(true);
761 anAct = myActionMap[REDO_ID];
763 if( myCurve->getNbRedo() != 0 ){
764 anAct->setEnabled(true);
767 anAct->setDisabled(true);
772 void CurveCreator_Widget::onContextMenu( QPoint thePoint )
774 QList<ActionId> aContextActions;
775 aContextActions << CLEAR_ALL_ID << JOIN_ALL_ID << SEPARATOR_ID <<
776 CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID << SET_SECTIONS_POLYLINE_ID <<
777 SET_SECTIONS_SPLINE_ID;
778 QPoint aGlPoint = mySectionView->mapToGlobal(thePoint);
780 QList<ActionId> aResAct;
781 for( int i = 0 ; i < aContextActions.size() ; i++ ){
782 if( aContextActions[i] != SEPARATOR_ID ){
783 if( myActionMap.contains(aContextActions[i]) ){
784 QAction* anAct = myActionMap[aContextActions[i]];
785 if( anAct->isEnabled() ){
786 aResAct << aContextActions[i];
792 aResAct << SEPARATOR_ID;
798 QMenu* aMenu = new QMenu(this);
799 for( int i = 0 ; i < aResAct.size() ; i++ ){
800 if( aResAct[i] == SEPARATOR_ID ){
801 aMenu->addSeparator();
804 QAction* anAct = myActionMap[aResAct[i]];
805 aMenu->insertAction(NULL, anAct);
808 aMenu->exec(aGlPoint);
811 QList<int> CurveCreator_Widget::getSelectedSections()
813 return mySectionView->getSelectedSections();
816 QList< QPair< int, int > > CurveCreator_Widget::getSelectedPoints()
818 return mySectionView->getSelectedPoints();
821 //=================================================================================
822 // function : GeometryGUI::onGetCoordsByClick()
823 // purpose : Manage mouse press events in Additon mode
824 //=================================================================================
825 void CurveCreator_Widget::onGetCoordsByClick( QMouseEvent* pe )
827 if (pe->button() != Qt::LeftButton)
830 if ( pe->modifiers() != Qt::ControlModifier ) {
831 OCCViewer_Viewer* aViewer = getOCCViewer();
834 Handle(AIS_InteractiveContext) ic = aViewer->getAISContext();
839 if ( pe->modifiers() == Qt::ShiftModifier )
840 ic->ShiftSelect(); // Append selection
842 ic->Select(); // New selection
844 /*TopoDS_Shape aShape;
847 if ( ic->MoreSelected() )
848 aShape = ic->SelectedShape();
850 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
851 aPnt = BRep_Tool::Pnt( TopoDS::Vertex( ic->SelectedShape() ) );
854 OCCViewer_ViewPort3d* vp =
855 ((OCCViewer_ViewWindow*)aViewer->getViewManager()->getActiveView())->getViewPort();
856 aPnt = CurveCreator_Utils::ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
858 // set the coordinates into dialog
859 CurveCreator::Coordinates aCoords;
860 aCoords.push_back( aPnt.X() );
861 aCoords.push_back( aPnt.Y() );
862 if ( myCurve->getDimension() == 3 ) {
863 aCoords.push_back( aPnt.Z() );
865 onAddNewPoint(aCoords);
866 // myNewPointEditor->setCoordinates( aCoords );
870 //=================================================================================
871 // function : CurveCreator_Widget::onMousePress()
872 // purpose : Manage mouse press events in Modification mode
873 //=================================================================================
874 void CurveCreator_Widget::onMousePress( SUIT_ViewWindow*, QMouseEvent* theEvent )
876 if ( theEvent->button() != Qt::LeftButton )
879 switch( getActionMode() ) {
880 case ModificationMode: {
881 //store initial cursor position for Drag&Drop
882 setDragStarted( true, theEvent->pos() );
886 onGetCoordsByClick( theEvent );
894 //=================================================================================
895 // function : HYDROGUI_PolylineOp::onMouseRelease()
896 // purpose : Manage mouse press events in Modification mode
897 //=================================================================================
898 void CurveCreator_Widget::onMouseRelease( SUIT_ViewWindow*, QMouseEvent* theEvent )
900 if ( getActionMode() != ModificationMode || !myDragStarted )
903 setDragStarted( false );
904 //store initial cursor position for Drag&Drop
905 //QPoint aPos = theEvent->pos();
906 //if (pe->button() == Qt::LeftButton)
907 // myDragStartPosition = pe->pos();
909 QPoint aPos = theEvent->pos();
910 // check whether the segment is clicked an a new point should be added to the segment
911 if ( insertPointToSelectedSegment( aPos.x(), aPos.y() ) ) {
912 setSelectedPonts( 0 );
916 // updates the input panel table to show the selected point coordinates
917 updateLocalPointView();
920 //=================================================================================
921 // function : GeometryGUI::onMouseMove()
922 // purpose : Manage mouse move events in Modification mode
923 //=================================================================================
924 void CurveCreator_Widget::onMouseMove( SUIT_ViewWindow*, QMouseEvent* theEvent )
926 if ( getActionMode() != ModificationMode || !myDragStarted )
929 OCCViewer_Viewer* aViewer = getOCCViewer();
933 //if ( !(pe->buttons() & Qt::LeftButton) )
935 QPoint aPos = theEvent->pos();
936 if ( (aPos - myDragStartPosition).manhattanLength() < QApplication::startDragDistance() )
939 //setLocalPointContext( false );
940 OCCViewer_ViewWindow* aWindow =
941 (OCCViewer_ViewWindow*)aViewer->getViewManager()->getActiveView();
942 gp_Pnt aStartPnt = CurveCreator_Utils::ConvertClickToPoint( myDragStartPosition.x(), myDragStartPosition.y(),
943 aWindow->getViewPort()->getView() );
944 gp_Pnt anEndPnt = CurveCreator_Utils::ConvertClickToPoint( aPos.x(), aPos.y(),
945 aWindow->getViewPort()->getView() );
946 double aXDelta = aStartPnt.X() - anEndPnt.X();
947 double anYDelta = aStartPnt.Y() - anEndPnt.Y();
951 std::deque<float> aChangedPos;
952 for ( int i = 0, aNb = myDragPoints.size(); i < aNb; i++ ) {
953 aPointId = myDragPoints[i];
954 aChangedPos = myCurve->getPoint( aSectionId, aPointId );
955 if ( aChangedPos.size() < 2 )
957 aChangedPos[0] = aChangedPos[0] - aXDelta;
958 aChangedPos[1] = aChangedPos[1] - anYDelta;
959 myCurve->setPoint( aSectionId, aPointId, aChangedPos );
962 //setLocalPointContext( true );
963 setSelectedPonts( aSectionId, myDragPoints );
965 myDragStartPosition = aPos;
967 QDrag *drag = new QDrag(this);
968 QMimeData *mimeData = new QMimeData;
970 mimeData->setData(mimeType, data);
971 drag->setMimeData(mimeData);
973 Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction);
977 void CurveCreator_Widget::onLocalPointChanged( int theRow, int theColumn )
979 QList<int> aSelSections = mySectionView->getSelectedSections();
980 if ( aSelSections.size() < 0 )
983 int aSection = aSelSections[0];
985 QList<int> aSelPoints;
986 getSelectedPonts( aSection, aSelPoints );
987 setLocalPointContext( false );
991 std::deque<float> aChangedPos;
992 float aPrevX, aPrevY, aX, anY;
993 //for( int i = 0 ; i < aSelSections.size() ; i++ ){
994 aCurrSect = aSection;//aSelSections[i];
996 aPrevX = myLocalPointView->item( theRow, 1 )->data( Qt::UserRole ).toDouble();
997 aPrevY = myLocalPointView->item( theRow, 2 )->data( Qt::UserRole ).toDouble();
999 aPntIndex = findLocalPointIndex( aCurrSect, aPrevX, aPrevY );
1000 if ( aPntIndex >= 0 ) {
1001 aX = myLocalPointView->item( theRow, 1 )->text().toDouble();
1002 anY = myLocalPointView->item( theRow, 2 )->text().toDouble();
1003 aChangedPos.clear();
1004 aChangedPos.push_back( aX );
1005 aChangedPos.push_back( anY );
1006 myCurve->setPoint( aCurrSect, aPntIndex, aChangedPos );
1009 setLocalPointContext( true );
1010 setSelectedPonts( aSection, aSelPoints );
1014 * Removes a selected section from the curve. Updates undo/redo status
1016 void CurveCreator_Widget::removeSection()
1018 QList< QPair<int,int> > aSelPoints = mySectionView->getSelectedPoints();
1021 // myCurve->startOperation();
1022 for( int i = 0 ; i < aSelPoints.size() ; i++ ){
1023 if( aCurrSect != aSelPoints[i].first ){
1025 aCurrSect = aSelPoints[i].first;
1027 int aPntIndx = aSelPoints[i].second - aRemoveCnt;
1028 myCurve->removePoint( aCurrSect, aPntIndx );
1029 mySectionView->pointsRemoved( aCurrSect, aPntIndx );
1032 QList<int> aSections = mySectionView->getSelectedSections();
1033 for( int i = 0 ; i < aSections.size() ; i++ ){
1034 int aSectNum = aSections[i] - (i);
1035 myCurve->removeSection( aSectNum );
1036 mySectionView->sectionsRemoved( aSectNum );
1038 // myCurve->finishOperation();
1039 mySectionView->clearSelection();
1044 * Removes a selected points from the curve. Updates undo/redo status
1046 void CurveCreator_Widget::removePoint()
1051 getSelectedPonts( aSectionId, aPoints );
1056 for( int i = aPoints.size()-1; i >= 0; i-- ){
1057 int aPntIndx = aPoints[i];
1058 myCurve->removePoint( aSectionId, aPntIndx );
1059 mySectionView->pointsRemoved( aSectionId, aPntIndx );
1063 bool CurveCreator_Widget::insertPointToSelectedSegment( const int theX,
1066 OCCViewer_Viewer* aViewer = getOCCViewer();
1070 int aPoint1 = -1, aPoint2 = -1;
1073 bool isFoundPoint = false;
1074 Handle(AIS_InteractiveContext) aContext = aViewer->getAISContext();
1075 for ( aContext->InitSelected(); aContext->MoreSelected() && !isFoundPoint;
1076 aContext->NextSelected() ) {
1077 TopoDS_Shape aTShape = aContext->SelectedShape();
1078 if ( !aTShape.IsNull() && aTShape.ShapeType() == TopAbs_VERTEX )
1081 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
1082 if ( anOwner.IsNull() )
1084 const TopLoc_Location& aLocation = anOwner->Location();
1085 Handle(AIS_InteractiveObject) anAIS = Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
1086 isFoundPoint = pointOnObject( anAIS, theX, theY, aPoint, aPoint1, aPoint2 );
1089 // insert the point to the model curve
1090 bool isDone = false;
1091 if ( isFoundPoint && aPoint1 >= 0 && aPoint2 >= 0 ) {
1093 CurveCreator::Coordinates aCoords;
1094 aCoords.push_back( aPoint.X() );
1095 aCoords.push_back( aPoint.Y() );
1097 int anInsertPos = aPoint1 < aPoint2 ? aPoint1 + 1 : aPoint2 + 1;
1098 myCurve->addPoints( aCoords, aSection, anInsertPos );
1099 mySectionView->pointsAdded( aSection, myCurve->getNbPoints( aSection ) );
1106 void CurveCreator_Widget::updateLocalPointView()
1108 OCCViewer_Viewer* aViewer = getOCCViewer();
1111 Handle(AIS_InteractiveContext) aContext = aViewer->getAISContext();
1113 bool isBlocked = myLocalPointView->blockSignals(true);
1115 myLocalPointView->setRowCount( 0 );
1116 for ( aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected() ) {
1117 TopoDS_Vertex aVertex;
1118 TopoDS_Shape aShape = aContext->SelectedShape();
1119 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
1120 aVertex = TopoDS::Vertex( aContext->SelectedShape() );
1122 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
1123 if ( !anOwner.IsNull() ) {
1124 Handle(AIS_InteractiveObject) anAIS = Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
1125 if ( !anAIS.IsNull() ) {
1126 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast( anAIS);
1127 if ( !aPoint.IsNull() )
1128 aVertex = TopoDS::Vertex( aPoint->Vertex() );
1130 if ( aVertex.IsNull() ) {
1131 // the following happens if there are no points in the current curve, there is only a shape
1132 /*Handle(StdSelect_BRepOwner) aBrepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1133 if ( aBrepOwner.IsNull() )
1135 if ( aBrepOwner->HasShape() ) {
1136 const TopoDS_Shape& aShape = aBrepOwner->Shape();
1137 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
1139 aVertex = TopoDS::Vertex( aShape );
1144 if ( aVertex.IsNull() )
1146 aPnt = BRep_Tool::Pnt( aVertex );
1147 addLocalPointToTable( aPnt.X(), aPnt.Y() );
1150 myLocalPointView->blockSignals(isBlocked);
1153 void CurveCreator_Widget::setLocalPointContext( const bool theOpen )
1155 OCCViewer_Viewer* aViewer = getOCCViewer();
1158 Handle(AIS_InteractiveContext) ic = aViewer->getAISContext();
1161 // Open local context if there is no one
1162 if ( !ic->HasOpenedContext() ) {
1163 ic->ClearCurrents( false );
1164 ic->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
1166 AIS_ListOfInteractive aList;
1167 ic->DisplayedObjects( aList );
1169 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
1172 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
1174 Handle(AIS_InteractiveObject) anAIS = it.Value();
1175 if ( !anAIS.IsNull() )
1177 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
1179 ic->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
1180 ic->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_WIRE ) );
1182 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
1184 ic->Load( anAIS, -1/*selection mode*/, false/*allow decomposition*/ );
1185 ic->Activate( anAIS, TopAbs_VERTEX );
1192 ic->CloseAllContexts();
1196 void CurveCreator_Widget::addLocalPointToTable( const double theX, const double theY )
1198 int aRowId = myLocalPointView->rowCount();
1199 double aCurrentX, aCurrentY;
1200 for ( int i = 0; i < aRowId; i++ ) {
1201 aCurrentX = myLocalPointView->item( i, 1 )->data( Qt::UserRole ).toDouble();
1202 aCurrentY = myLocalPointView->item( i, 2 )->data( Qt::UserRole ).toDouble();
1203 if ( fabs( aCurrentX - theX ) < LOCAL_SELECTION_TOLERANCE &&
1204 fabs( aCurrentY - theY ) < LOCAL_SELECTION_TOLERANCE )
1207 QTableWidgetItem* anItem;
1209 myLocalPointView->setRowCount( aRowId+1 );
1211 int aPointSection = 0;
1212 int aPointIndex = findLocalPointIndex( aPointSection, theX, theY );
1214 anItem = new QTableWidgetItem( QString::number( aPointIndex + 1 ) );
1215 anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
1216 myLocalPointView->setItem( aRowId, 0, anItem );
1218 anItem = new QTableWidgetItem( QString::number( theX ) );
1219 anItem->setData( Qt::UserRole, theX );
1220 myLocalPointView->setItem( aRowId, 1, anItem );
1222 anItem = new QTableWidgetItem( QString::number( theY ) );
1223 anItem->setData( Qt::UserRole, theY );
1224 myLocalPointView->setItem( aRowId, 2, anItem );
1228 * Set drag operation started. Save the position and a list of dragged points
1229 * \param theState the drag operation state: started/finished
1230 * \param thePoint the start drag position
1232 void CurveCreator_Widget::setDragStarted( const bool theState, const QPoint& thePoint )
1234 myDragStarted = theState;
1235 if ( myDragStarted ) {
1236 myDragStartPosition = thePoint;
1237 getSelectedPonts( 0, myDragPoints );
1241 void CurveCreator_Widget::getSelectedPonts( int theSectionId, QList<int>& thePoints )
1245 float aPrevX, aPrevY;
1247 for ( int i = 0, aNb = myLocalPointView->rowCount(); i < aNb; i++ ) {
1248 aPrevX = myLocalPointView->item( i, 1 )->data( Qt::UserRole ).toDouble();
1249 aPrevY = myLocalPointView->item( i, 2 )->data( Qt::UserRole ).toDouble();
1251 aPntIndex = findLocalPointIndex( theSectionId, aPrevX, aPrevY );
1252 if ( aPntIndex >= 0 )
1253 thePoints.append( aPntIndex );
1257 void CurveCreator_Widget::setSelectedPonts( const int theSectionId, const QList<int>& thePoints )
1259 OCCViewer_Viewer* aViewer = getOCCViewer();
1263 AIS_ListOfInteractive aListToSelect;
1265 Handle(AIS_InteractiveContext) ic = aViewer->getAISContext();
1266 if ( !ic->HasOpenedContext() )
1269 AIS_ListOfInteractive aDisplayedList;
1270 ic->DisplayedObjects( aDisplayedList );
1271 for ( AIS_ListIteratorOfListOfInteractive it( aDisplayedList ); it.More(); it.Next() )
1273 Handle(AIS_InteractiveObject) anAIS = it.Value();
1274 if ( anAIS.IsNull() )
1276 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast( anAIS );
1277 if ( aPoint.IsNull() )
1280 TopoDS_Vertex aVertex = TopoDS::Vertex( aPoint->Vertex() );
1282 if ( aVertex.IsNull() )
1285 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
1286 int aPointIndex = findLocalPointIndex( theSectionId, aPnt.X(), aPnt.Y() );
1287 if ( thePoints.contains( aPointIndex ) )
1288 aListToSelect.Append( anAIS );
1291 aViewer->setObjectsSelected( aListToSelect );
1292 updateLocalPointView();
1296 * Returns a point index in the model curve by the point coordinates in the viewer
1297 * \param theX the X coordinate of the point
1298 * \param theY the Y coordinate of the point
1300 int CurveCreator_Widget::findLocalPointIndex( int theSectionId, float theX, float theY )
1304 CurveCreator::Coordinates aCoords;
1305 for ( int i = 0, aNb = myCurve->getNbPoints( theSectionId ); i < aNb && aPntIndex < 0; i++ ) {
1306 aCoords = myCurve->getPoint( theSectionId, i );
1307 if ( aCoords.size() < 2 )
1309 if ( fabs( aCoords[0] - theX ) < LOCAL_SELECTION_TOLERANCE &&
1310 fabs( aCoords[1] - theY ) < LOCAL_SELECTION_TOLERANCE )
1318 * Checks whether the point belongs to the OCC object
1319 * \param theObject a line or shape with a bspline inside
1320 * \param theX the X coordinate in the view.
1321 * \param theY the Y coordinate in the view.
1322 * \param thePoint the output point to be append to the model curve
1323 * \param thePoint1 the output point to bound the line where a new point should be inserted
1324 * \param thePoint2 the output point to bound the line where a new point should be inserted
1326 bool CurveCreator_Widget::pointOnObject( Handle(AIS_InteractiveObject) theObject,
1327 const int theX, const int theY,
1329 int& thePoint1, int& thePoint2 )
1331 bool isFound = false;
1333 OCCViewer_Viewer* aViewer = getOCCViewer();
1334 if ( theObject.IsNull() || !aViewer )
1338 gp_Pnt aPnt1, aPnt2;
1339 Handle(AIS_Line) aLine = Handle(AIS_Line)::DownCast( theObject );
1340 if ( !aLine.IsNull() ) {
1341 const Handle(Geom_Line) aGLine = aLine->Line();
1342 Handle(Geom_Point) aPStart;
1343 Handle(Geom_Point) aPEnd;
1344 aLine->Points( aPStart, aPEnd );
1345 aPnt1 = aPStart->Pnt();
1346 aPnt2 = aPEnd->Pnt();
1347 isFound = hasProjectPointOnCurve( theX, theY, aGLine, aPoint );
1350 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
1351 if ( !aShape.IsNull() ) {
1352 const TopoDS_Wire& aWire = TopoDS::Wire( aShape->Shape() );
1353 if ( !aWire.IsNull() ) {
1354 TopExp_Explorer anExp( aWire, TopAbs_EDGE );
1355 for ( ; anExp.More(); anExp.Next())
1357 const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
1358 if ( !anEdge.IsNull() ) {
1359 Standard_Real aFirst, aLast;
1360 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
1361 aCurve->D0(aFirst,aPnt1);
1362 aCurve->D0(aLast,aPnt2);
1363 isFound = hasProjectPointOnCurve( theX, theY, aCurve, aPoint );
1371 thePoint1 = findLocalPointIndex( 0, aPnt1.X(), aPnt1.Y() );
1372 thePoint2 = findLocalPointIndex( 0, aPnt2.X(), aPnt2.Y() );
1373 isFound = thePoint1 >= 0 && thePoint2 >= 0;
1379 * Returns whether the clicked point belong to the curve or has a very near projection
1380 * \param theX the X coordinate of a point clicked in the OCC viewer
1381 * \param theY the Y coordinate of a point clicked in the OCC viewer
1382 * \param theCurve a geometry curve
1383 * \param theOutPoint a found projected point on the curve
1385 bool CurveCreator_Widget::hasProjectPointOnCurve( const int theX, const int theY,
1386 const Handle(Geom_Curve)& theCurve,
1387 gp_Pnt& theOutPoint )
1389 bool isFound = false;
1390 OCCViewer_Viewer* aViewer = getOCCViewer();
1394 OCCViewer_ViewWindow* aWindow =
1395 (OCCViewer_ViewWindow*)aViewer->getViewManager()->getActiveView();
1396 Handle(V3d_View) aView = aWindow->getViewPort()->getView();
1397 gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, aView );
1399 GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
1400 Standard_Integer aNbPoint = aProj.NbPoints();
1402 for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
1403 gp_Pnt aNewPoint = aProj.Point(j);
1406 CurveCreator_Utils::ConvertPointToClick( aNewPoint, aView, aX, anY );
1408 int aXDelta = abs( aX - theX );
1409 int anYDelta = abs( anY - theY );
1410 isFound = aXDelta < SCENE_PIXEL_TOLERANCE && anYDelta < SCENE_PIXEL_TOLERANCE;
1412 theOutPoint = aNewPoint;