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"
28 #include "CurveCreator_TableView.h"
30 #include <SUIT_Session.h>
31 #include <SUIT_Desktop.h>
32 #include <SUIT_ResourceMgr.h>
33 #include <SUIT_ViewManager.h>
35 #include <OCCViewer_ViewWindow.h>
36 #include <OCCViewer_ViewManager.h>
37 #include <OCCViewer_ViewPort3d.h>
38 #include "OCCViewer_Utilities.h"
39 #include "OCCViewer_ViewWidget.h"
41 #include <BRep_Tool.hxx>
43 #include <TopoDS_Vertex.hxx>
44 #include <TopoDS_Wire.hxx>
45 #include <TopoDS_Edge.hxx>
48 #include <TopExp_Explorer.hxx>
50 #include <GeomAPI_ProjectPointOnCurve.hxx>
52 #include <AIS_ListOfInteractive.hxx>
53 #include <AIS_ListIteratorOfListOfInteractive.hxx>
54 #include <AIS_Shape.hxx>
55 #include <AIS_Point.hxx>
56 #include <AIS_Line.hxx>
57 #include <Geom_Point.hxx>
58 #include <Geom_BSplineCurve.hxx>
59 #include <Geom_Line.hxx>
60 #include <StdSelect_BRepOwner.hxx>
62 #include <QHBoxLayout>
63 #include <QVBoxLayout>
67 #include <QToolButton>
71 #include <QMouseEvent>
72 #include <QApplication>
73 #include <QTableWidget>
75 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
76 const int SECTION_NAME_COLUMN_WIDTH = 75;
77 const int POINT_INDEX_COLUMN_WIDTH = 40;
79 const int SCENE_PIXEL_TOLERANCE = 10;
81 CurveCreator_Widget::CurveCreator_Widget(QWidget* parent,
82 CurveCreator_ICurve *theCurve,
84 : QWidget(parent), myNewSectionEditor(NULL), myCurve(theCurve), mySection(0),
85 myDragStarted( false ), myDragInteractionStyle( SUIT_ViewModel::STANDARD ),
86 myOCCViewer( 0 ), myOCCViewWidget( 0 )
88 myNewSectionEditor = new CurveCreator_NewSectionDlg( this );
89 myNewSectionEditor->hide();
90 connect( myNewSectionEditor, SIGNAL(addSection()), this, SLOT(onAddNewSection()) );
91 connect( myNewSectionEditor, SIGNAL(modifySection()), this, SLOT(onModifySection()) );
92 connect( myNewSectionEditor, SIGNAL(cancelSection()), this, SLOT(onCancelSection()) );
94 QGroupBox* aSectionGroup = new QGroupBox(tr("Sections"),this);
96 mySectionView = new CurveCreator_TreeView(myCurve, aSectionGroup);
97 connect( mySectionView, SIGNAL(selectionChanged()), this, SLOT( onSelectionChanged() ) );
98 connect( mySectionView, SIGNAL(sectionEntered(int)), this, SLOT(onEditSection(int)) );
99 connect( mySectionView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onContextMenu(QPoint)) );
101 myLocalPointView = new QTableWidget();
102 myLocalPointView->setItemDelegate( new CurveCreator_TableItemDelegate( myLocalPointView ) );
103 myLocalPointView->setVisible( false );
104 myLocalPointView->setColumnCount( 4 );
105 myLocalPointView->setColumnWidth( 0, SECTION_NAME_COLUMN_WIDTH );
106 myLocalPointView->setColumnWidth( 1, POINT_INDEX_COLUMN_WIDTH );
108 //aLabels << tr( "SECTION_LABEL" ) << tr( "IDENTIFIER_LABEL" ) << tr( "X_POSITION_LBL" ) << tr( "Y_POSITION_LBL" );
109 aLabels << tr( "Section" ) << "Index" << tr( "X" ) << tr( "Y" );
110 myLocalPointView->setHorizontalHeaderLabels( aLabels );
111 connect( myLocalPointView, SIGNAL( cellChanged( int, int ) ),
112 this, SLOT( onCellChanged( int, int ) ) );
114 QToolBar* aTB = new QToolBar(tr("TOOL_BAR_TLT"), aSectionGroup);
115 // QToolButton* anUndoBtn = new QToolButton(aTB);
117 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
118 QPixmap anUndoPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_UNDO")));
119 QPixmap aRedoPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_REDO")));
120 QPixmap aNewSectionPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_NEW_SECTION")));
121 QPixmap aNewPointPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_NEW_POINT")));
122 QPixmap anEditPointsPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_EDIT_POINTS")));
123 QPixmap aDetectPointsPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_EDIT_POINTS")));
124 QPixmap aPolylinePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_POLYLINE")));
125 QPixmap aSplinePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_SPLINE")));
126 QPixmap aRemovePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_DELETE")));
127 QPixmap aJoinPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_JOIN")));
128 QPixmap aStepUpPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_ARROW_UP")));
129 QPixmap aStepDownPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_ARROW_DOWN")));
131 QAction* anAct = createAction( UNDO_ID, tr("UNDO"), anUndoPixmap, tr("UNDO_TLT"),
132 QKeySequence(Qt::ControlModifier|Qt::Key_Z) );
133 connect(anAct, SIGNAL(triggered()), this, SLOT(onUndo()) );
134 aTB->addAction(anAct);
136 anAct = createAction( REDO_ID, tr("REDO"), aRedoPixmap, tr("REDO_TLT"),
137 QKeySequence(Qt::ControlModifier|Qt::Key_Y) );
138 connect(anAct, SIGNAL(triggered()), this, SLOT(onRedo()) );
139 aTB->addAction(anAct);
143 anAct = createAction( NEW_SECTION_ID, tr("NEW_SECTION"), aNewSectionPixmap, tr("NEW_SECTION_TLT"),
144 QKeySequence(Qt::ControlModifier|Qt::Key_N) );
145 connect(anAct, SIGNAL(triggered()), this, SLOT(onNewSection()) );
146 aTB->addAction(anAct);
149 anAct = createAction( ADDITION_MODE_ID, tr("ADDITION_MODE"), aNewPointPixmap, tr("ADDITION_MODE_TLT"),
151 anAct->setCheckable(true);
152 connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onAdditionMode(bool)) );
153 connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
154 aTB->addAction(anAct);
156 anAct = createAction( MODIFICATION_MODE_ID, tr("MODIFICATION_MODE"), anEditPointsPixmap, tr("MODIFICATION_MODE_TLT"),
158 anAct->setCheckable(true);
159 connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onModificationMode(bool)) );
160 connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
161 aTB->addAction(anAct);
163 anAct = createAction( DETECTION_MODE_ID, tr("DETECTION_MODE"), aDetectPointsPixmap, tr("DETECTION_MODE_TLT"),
165 anAct->setCheckable(true);
166 connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onDetectionMode(bool)) );
167 connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
168 aTB->addAction(anAct);
170 anAct = createAction( CLOSE_SECTIONS_ID, tr("CLOSE_SECTIONS"), QPixmap(), tr("CLOSE_SECTIONS_TLT"),
171 QKeySequence(Qt::ControlModifier|Qt::Key_W) );
172 connect(anAct, SIGNAL(triggered()), this, SLOT(onCloseSections()) );
174 anAct = createAction( UNCLOSE_SECTIONS_ID, tr("UNCLOSE_SECTIONS"), QPixmap(),
175 tr("UNCLOSE_SECTIONS_TLT"), QKeySequence(Qt::ControlModifier|Qt::Key_S) );
176 connect(anAct, SIGNAL(triggered()), this, SLOT(onUncloseSections()) );
178 anAct = createAction( SET_SECTIONS_POLYLINE_ID, tr("SET_SECTIONS_POLYLINE"),
179 aPolylinePixmap, tr("SET_POLYLINE_TLT"),
180 QKeySequence(Qt::ControlModifier|Qt::Key_E) );
181 connect(anAct, SIGNAL(triggered()), this, SLOT(onSetPolyline()) );
183 anAct = createAction( SET_SECTIONS_SPLINE_ID, tr("SET_SECTIONS_SPLINE"), aSplinePixmap,
184 tr("SET_SPLINE_TLT"), QKeySequence(Qt::ControlModifier|Qt::Key_R) );
185 connect(anAct, SIGNAL(triggered()), this, SLOT(onSetSpline()) );
187 anAct = createAction( REMOVE_ID, tr("REMOVE"), aRemovePixmap, tr("REMOVE_TLT"),
188 QKeySequence(Qt::ControlModifier|Qt::Key_Delete ) );
189 connect(anAct, SIGNAL(triggered()), this, SLOT(onRemove()) );
190 aTB->addAction(anAct);
192 //aTB->addSeparator();
194 //anAct = createAction( JOIN_ID, tr("JOIN"), aJoinPixmap, tr("JOIN_TLT"),
195 // QKeySequence(Qt::ControlModifier|Qt::Key_Plus ) );
196 //connect( anAct, SIGNAL(triggered()), this, SLOT(onJoin()) );
197 //aTB->addAction(anAct);
199 anAct = createAction( CLEAR_ALL_ID, tr("CLEAR_ALL"), QPixmap(), tr("CLEAR_ALL_TLT"),
200 QKeySequence(Qt::ControlModifier | Qt::ShiftModifier | Qt::Key_Delete ) );
201 connect( anAct, SIGNAL(triggered()), this, SLOT( onClearAll()) );
204 //anAct = createAction( JOIN_ALL_ID, tr("JOIN_ALL"), QPixmap(), tr("JOIN_ALL_TLT"),
205 // QKeySequence(Qt::ControlModifier | Qt::ShiftModifier | Qt::Key_Plus ) );
206 //connect( anAct, SIGNAL(triggered()), this, SLOT(onJoinAll()) );
208 QVBoxLayout* aSectLayout = new QVBoxLayout();
209 aSectLayout->setMargin( 5 );
210 aSectLayout->setSpacing( 5 );
211 aSectLayout->addWidget(aTB);
212 aSectLayout->addWidget(mySectionView);
213 aSectLayout->addWidget( myLocalPointView );
214 aSectionGroup->setLayout(aSectLayout);
215 QVBoxLayout* aLay = new QVBoxLayout();
216 aLay->setMargin( 0 );
217 aLay->setSpacing( 5 );
218 // aLay->addLayout(aNameLayout);
219 aLay->addWidget(aSectionGroup);
221 onSelectionChanged();
227 void CurveCreator_Widget::setOCCViewer( OCCViewer_Viewer* theViewer )
229 if ( myOCCViewer == theViewer )
233 OCCViewer_ViewManager* aViewManager = dynamic_cast<OCCViewer_ViewManager*>
234 ( myOCCViewer->getViewManager() );
235 disconnect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
236 this, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ) );
237 disconnect( aViewManager, SIGNAL( mouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ),
238 this, SLOT( onMouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ) );
239 disconnect( aViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
240 this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
241 disconnect( aViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
242 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
243 // restore normal mode in the viewer
244 OCCViewer_Utilities::setViewer2DMode( myOCCViewer, OCCViewer_ViewWindow::No2dMode );
245 // all local contexts should be closed if the viewer is not more used
246 setLocalPointContext( false, true );
249 myOCCViewer = theViewer;
251 OCCViewer_ViewManager* aViewManager = dynamic_cast<OCCViewer_ViewManager*>
252 ( myOCCViewer->getViewManager() );
253 connect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
254 this, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ) );
255 connect( aViewManager, SIGNAL( mouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ),
256 this, SLOT( onMouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ) );
257 connect( aViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
258 this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
259 connect( aViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
260 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
261 OCCViewer_Utilities::setViewer2DMode( myOCCViewer, OCCViewer_ViewWindow::XYPlane );
266 * Returns current OCC viewer
268 OCCViewer_Viewer* CurveCreator_Widget::getOCCViewer()
276 void CurveCreator_Widget::setOCCViewWidget( OCCViewer_ViewWidget* theViewWidget )
278 if ( myOCCViewWidget == theViewWidget )
281 if ( myOCCViewWidget ) {
282 disconnect( myOCCViewWidget, SIGNAL( mousePressed( QMouseEvent* ) ),
283 this, SLOT( onMousePress( QMouseEvent* ) ) );
284 disconnect( myOCCViewWidget, SIGNAL( mouseReleased( QMouseEvent* ) ),
285 this, SLOT( onMouseRelease( QMouseEvent* ) ) );
286 disconnect( myOCCViewWidget, SIGNAL( mouseMoving( QMouseEvent* ) ),
287 this, SLOT( onMouseMove( QMouseEvent* ) ) );
288 //disconnect( myOCCViewWidget, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
289 // this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
291 // restore normal mode in the viewer
292 OCCViewer_Utilities::setViewWidget2DMode( myOCCViewWidget, OCCViewer_ViewWidget::No2dMode );
293 // all local contexts should be closed if the viewer is not more used
294 setLocalPointContext( false, true );
297 myOCCViewWidget = theViewWidget;
298 if ( myOCCViewWidget ) {
299 connect( myOCCViewWidget, SIGNAL( mousePressed( QMouseEvent* ) ),
300 this, SLOT( onMousePress( QMouseEvent* ) ) );
301 connect( myOCCViewWidget, SIGNAL( mouseReleased( QMouseEvent* ) ),
302 this, SLOT( onMouseRelease( QMouseEvent* ) ) );
303 connect( myOCCViewWidget, SIGNAL( mouseMoving( QMouseEvent* ) ),
304 this, SLOT( onMouseMove( QMouseEvent* ) ) );
305 //connect( aViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
306 // this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
307 OCCViewer_Utilities::setViewWidget2DMode( myOCCViewWidget, OCCViewer_ViewWidget::XYPlane );
312 * Returns current OCC viewer
314 OCCViewer_ViewWidget* CurveCreator_Widget::getOCCViewWidget()
316 return myOCCViewWidget;
320 * Returns OCC viewer context
322 Handle(AIS_InteractiveContext) CurveCreator_Widget::getAISContext()
324 Handle(AIS_InteractiveContext) aContext;
325 OCCViewer_Viewer* aViewer = getOCCViewer();
327 aContext = aViewer->getAISContext();
328 else if ( myOCCViewWidget )
329 aContext = myOCCViewWidget->getAISContext();
335 * Returns OCC viewer view port
337 OCCViewer_ViewPort3d* CurveCreator_Widget::getViewPort()
339 OCCViewer_ViewPort3d* aViewPort = 0;
340 OCCViewer_Viewer* aViewer = getOCCViewer();
342 aViewPort = ((OCCViewer_ViewWindow*)aViewer->getViewManager()->getActiveView())->getViewPort();
343 else if ( myOCCViewWidget )
344 aViewPort = myOCCViewWidget->getViewPort();
350 * Set interaction style in the OCC viewer
351 * \param theStyle a new style
352 * \return the previous style
354 int CurveCreator_Widget::changeInteractionStyle( int theStyle )
356 OCCViewer_Viewer* aViewer = getOCCViewer();
360 int aPrevStyle = aViewer->interactionStyle();
361 aViewer->setInteractionStyle( theStyle );
367 * Set interaction style in the OCC viewer
368 * \param theStyle a new style
369 * \return the previous style
371 void CurveCreator_Widget::setObjectsSelected(const AIS_ListOfInteractive& theList)
373 OCCViewer_Viewer* aViewer = getOCCViewer();
375 aViewer->setObjectsSelected(theList);
376 else if ( myOCCViewWidget ) {
377 Handle(AIS_InteractiveContext) aContext = myOCCViewWidget->getAISContext();
379 AIS_ListIteratorOfListOfInteractive aIt;
380 for (aIt.Initialize(theList); aIt.More(); aIt.Next())
381 aContext->AddOrRemoveSelected(aIt.Value(), false);
382 aContext->UpdateCurrentViewer();
386 //=======================================================================
388 // purpose: reset the widget viewer, close local context, clear selection
389 //=======================================================================
390 void CurveCreator_Widget::reset()
394 //=======================================================================
395 // function: getUniqSectionName
396 // purpose: return unique section name
397 //=======================================================================
398 std::string CurveCreator_Widget::getUniqSectionName( CurveCreator_ICurve* theCurve ) const
400 for( int i = 0 ; i < 1000000 ; i++ ){
402 sprintf( aBuffer, "Section_%d", i+1 );
403 std::string aName(aBuffer);
405 for( j = 0 ; j < theCurve->getNbSections() ; j++ ){
406 if( theCurve->getSectionName(j) == aName )
409 if( j == theCurve->getNbSections() )
415 void CurveCreator_Widget::setCurve( CurveCreator_ICurve* theCurve )
418 mySectionView->setCurve(myCurve);
419 onSelectionChanged();
423 void CurveCreator_Widget::onSelectionChanged()
425 QList<ActionId> anEnabledAct;
427 anEnabledAct << NEW_SECTION_ID << MODIFICATION_MODE_ID;
428 if ( removeEnabled() )
429 anEnabledAct << REMOVE_ID;
430 QList<int> aSelSections = mySectionView->getSelectedSections();
431 QList< QPair< int, int > > aSelPoints = mySectionView->getSelectedPoints();
432 CurveCreator_TreeView::SelectionType aSelType = mySectionView->getSelectionType();
434 case CurveCreator_TreeView::ST_NOSEL:{
437 case CurveCreator_TreeView::ST_SECTIONS:{
438 /*if( aSelSections[0] > 0 ){
439 anEnabledAct << UP_ID;
441 if( aSelSections.size() == 1 ){
442 anEnabledAct << ADDITION_MODE_ID << DETECTION_MODE_ID;
444 switch ( getActionMode() ) {
448 QList<int> aSelSection = mySectionView->getSelectedSections();
449 if( aSelSection.size() > 0 ){
450 mySection = aSelSection[0];
451 myPointNum = myCurve->getNbPoints(mySection);
455 case ModificationMode: {
456 anEnabledAct << CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID << SET_SECTIONS_POLYLINE_ID << SET_SECTIONS_SPLINE_ID;
457 int aSectCnt = myCurve->getNbSections();
459 anEnabledAct << CLEAR_ALL_ID;
462 // anEnabledAct << JOIN_ALL_ID;
463 //if( aSelSections.size() > 1 ){
464 // anEnabledAct << JOIN_ID;
468 case DetectionMode: {
475 /*if( aSelSections[ aSelSections.size() - 1 ] < ( myCurve->getNbSections() - 1 ) ){
476 anEnabledAct << DOWN_ID;
480 /*case CurveCreator_TreeView::ST_POINTS_ONE_SECTION:{
481 if( aSelPoints[0].second > 0 ){
482 anEnabledAct << UP_ID;
484 int aLastIndex = aSelPoints.size()-1;
485 int aSect = aSelPoints[0].first;
486 if( aSelPoints[aLastIndex].second < (myCurve->getNbPoints(aSect) - 1)){
487 anEnabledAct << DOWN_ID;
489 if( aSelPoints.size() == 1){
490 anEnabledAct << INSERT_POINT_BEFORE_ID << INSERT_POINT_AFTER_ID;
497 /*int aSelObjsCnt = aSelPoints.size() + aSelSections.size();
498 if( aSelObjsCnt > 0 ){
499 anEnabledAct << REMOVE_ID;
501 if( (myCurve->getNbSections() + myCurve->getNbPoints()) > 0 ){
502 anEnabledAct << REMOVE_ALL_ID;
505 //if( myCurve->getNbSections() > 1 ){
506 // anEnabledAct << JOIN_ALL_ID;
509 QList<ActionId> anIds = myActionMap.keys();
510 for( int i = 0 ; i < anIds.size() ; i++ ){
511 if( myActionMap.contains(anIds[i]) ){
512 if( anEnabledAct.contains(anIds[i]) ){
513 myActionMap[anIds[i]]->setEnabled(true);
516 myActionMap[anIds[i]]->setEnabled(false);
521 emit selectionChanged();
524 void CurveCreator_Widget::onAdditionMode(bool checked)
529 Handle(AIS_InteractiveContext) aContext = getAISContext();
530 if( !myCurve || !aContext )
535 QList<int> aSelSection = mySectionView->getSelectedSections();
536 if( aSelSection.size() > 0 ){
537 mySection = aSelSection[0];
540 QList< QPair<int,int> > aSelPoints = mySectionView->getSelectedPoints();
541 if( aSelPoints.size() > 0 ){
542 mySection = aSelPoints[0].first;
543 myPointNum = aSelPoints[0].second + 1;
546 // emit subOperationStarted( myNewPointEditor );
549 void CurveCreator_Widget::onModificationMode(bool checked)
551 myLocalPointView->setVisible( checked );
554 void CurveCreator_Widget::onDetectionMode(bool checked)
558 void CurveCreator_Widget::onModeChanged(bool checked)
560 ActionMode aMode = NoneMode;
562 QAction* anAction = (QAction*)sender();
563 switch(myActionMap.key(anAction)) {
564 case ADDITION_MODE_ID:
565 aMode = AdditionMode;
566 if (myActionMap[MODIFICATION_MODE_ID]->isChecked())
567 myActionMap[MODIFICATION_MODE_ID]->trigger();
568 else if (myActionMap[DETECTION_MODE_ID]->isChecked())
569 myActionMap[DETECTION_MODE_ID]->trigger();
571 case MODIFICATION_MODE_ID:
572 aMode = ModificationMode;
573 if (myActionMap[ADDITION_MODE_ID]->isChecked())
574 myActionMap[ADDITION_MODE_ID]->trigger();
575 else if (myActionMap[DETECTION_MODE_ID]->isChecked())
576 myActionMap[DETECTION_MODE_ID]->trigger();
578 case DETECTION_MODE_ID:
579 aMode = DetectionMode;
580 if (myActionMap[ADDITION_MODE_ID]->isChecked())
581 myActionMap[ADDITION_MODE_ID]->trigger();
582 else if (myActionMap[MODIFICATION_MODE_ID]->isChecked())
583 myActionMap[MODIFICATION_MODE_ID]->trigger();
587 onSelectionChanged();
588 setLocalPointContext( aMode == ModificationMode, true );
591 void CurveCreator_Widget::onAddNewPoint(const CurveCreator::Coordinates& theCoords)
595 //myCurve->addPoints(theCoords, mySection, myPointNum );
596 //mySectionView->pointsAdded( mySection, myPointNum );
598 QList<int> aSections = mySectionView->getSelectedSections();
599 if( aSections.size() == 0 ){
602 int aSection = aSections[0];
603 myCurve->addPoints(theCoords, aSection); // add to the end of section
604 mySectionView->pointsAdded( aSection, myCurve->getNbPoints( aSection ) );
605 onSelectionChanged();
609 void CurveCreator_Widget::onNewSection()
613 myNewSectionEditor->clear();
614 myNewSectionEditor->setEditMode(false);
615 QString aSectName = QString( getUniqSectionName(myCurve).c_str() );
616 myNewSectionEditor->setSectionParameters(aSectName, true, CurveCreator::Polyline );
617 emit subOperationStarted( myNewSectionEditor );
620 void CurveCreator_Widget::onAddNewSection()
624 myCurve->addSection( myNewSectionEditor->getName().toStdString(), myNewSectionEditor->getSectionType(),
625 myNewSectionEditor->isClosed() );
626 mySectionView->sectionAdded( -1 ); // add a new section to the end of list
627 QString aNewName = QString(getUniqSectionName(myCurve).c_str());
628 myNewSectionEditor->setSectionName(aNewName);
629 onSelectionChanged();
634 void CurveCreator_Widget::onCancelSection()
636 emit subOperationFinished( myNewSectionEditor );
639 QAction* CurveCreator_Widget::createAction( ActionId theId, const QString& theName, const QPixmap& theImage,
640 const QString& theToolTip, const QKeySequence& theShortcut )
642 QAction* anAct = new QAction(theName,this);
643 if( !theImage.isNull() ){
644 anAct->setIcon(theImage);
646 anAct->setShortcut(theShortcut);
647 anAct->setToolTip(theToolTip);
648 myActionMap[theId] = anAct;
652 QAction* CurveCreator_Widget::getAction(ActionId theId)
654 if( myActionMap.contains(theId) )
655 return myActionMap[theId];
659 CurveCreator_Widget::ActionMode CurveCreator_Widget::getActionMode() const
661 ActionMode aMode = NoneMode;
663 if ( myActionMap[ADDITION_MODE_ID]->isChecked() )
664 aMode = AdditionMode;
665 else if ( myActionMap[MODIFICATION_MODE_ID]->isChecked() )
666 aMode = ModificationMode;
667 else if ( myActionMap[DETECTION_MODE_ID]->isChecked() )
668 aMode = DetectionMode;
673 void CurveCreator_Widget::onEditSection( int theSection )
677 mySection = theSection;
678 QString aSectName = QString::fromStdString( myCurve->getSectionName(theSection));
679 bool isClosed = myCurve->isClosed(theSection);
680 CurveCreator::SectionType aType = myCurve->getSectionType(theSection);
681 myNewSectionEditor->setEditMode(true);
682 myNewSectionEditor->setSectionParameters( aSectName, isClosed, aType );
684 emit subOperationStarted( myNewSectionEditor );
687 void CurveCreator_Widget::onModifySection()
691 QString aName = myNewSectionEditor->getName();
692 bool isClosed = myNewSectionEditor->isClosed();
693 CurveCreator::SectionType aSectType = myNewSectionEditor->getSectionType();
694 // myCurve->startOperation();
695 if( myCurve->getSectionName(mySection) != aName.toStdString() )
696 myCurve->setSectionName( mySection , aName.toStdString() );
698 if( myCurve->getSectionType(mySection) != aSectType )
699 myCurve->setSectionType( mySection, aSectType );
701 if( myCurve->isClosed(mySection) != isClosed )
702 myCurve->setClosed( mySection, isClosed );
703 // myCurve->finishOperation();
704 mySectionView->sectionChanged(mySection);
709 /*void CurveCreator_Widget::onEditPoint( int theSection, int thePoint )
711 if( !myNewPointEditor || !myEdit )
713 mySection = theSection;
714 myPointNum = thePoint;
715 QString aSectName = QString::fromStdString( myCurve->getSectionName(theSection));
716 myNewPointEditor->setEditMode(true);
717 myNewPointEditor->setSectionName(aSectName);
718 myNewPointEditor->setDimension( myCurve->getDimension() );
719 CurveCreator::Coordinates aCoords = myCurve->getCoordinates(theSection,thePoint);
720 myNewPointEditor->setCoordinates(aCoords);
721 emit subOperationStarted( myNewPointEditor );
724 void CurveCreator_Widget::onModifyPoint()
728 CurveCreator::Coordinates aCoords = myNewPointEditor->getCoordinates();
729 myEdit->setCoordinates( aCoords, mySection, myPointNum );
730 mySectionView->pointDataChanged( mySection, myPointNum );
735 void CurveCreator_Widget::onJoin()
739 QList<int> aSections = mySectionView->getSelectedSections();
740 if( aSections.size() == 0 ){
743 int aMainSect = aSections[0];
744 int aMainSectSize = myCurve->getNbPoints(aMainSect);
745 // myCurve->startOperation();
746 for( int i = 1 ; i < aSections.size() ; i++ ){
747 int aSectNum = aSections[i] - (i-1);
748 myCurve->join( aMainSect, aSectNum );
749 mySectionView->sectionsRemoved( aSectNum );
751 // myCurve->finishOperation();
752 int aNewSectSize = myCurve->getNbPoints(aMainSect);
753 if( aNewSectSize != aMainSectSize )
754 mySectionView->pointsAdded( aMainSect, aMainSectSize, aNewSectSize-aMainSectSize );
758 void CurveCreator_Widget::onRemove()
763 switch( getActionMode() ) {
767 case ModificationMode:
775 void CurveCreator_Widget::onClearAll()
780 mySectionView->reset();
781 onSelectionChanged();
785 void CurveCreator_Widget::onJoinAll()
790 mySectionView->reset();
791 onSelectionChanged();
795 void CurveCreator_Widget::onUndoSettings()
800 void CurveCreator_Widget::onSetSpline()
804 QList<int> aSelSections = mySectionView->getSelectedSections();
805 // myCurve->startOperation();
806 for( int i = 0 ; i < aSelSections.size() ; i++ ){
807 myCurve->setSectionType(aSelSections[i], CurveCreator::Spline );
808 mySectionView->sectionChanged(aSelSections[i]);
810 // myCurve->finishOperation();
814 void CurveCreator_Widget::onSetPolyline()
818 // myCurve->startOperation();
819 QList<int> aSelSections = mySectionView->getSelectedSections();
820 for( int i = 0 ; i < aSelSections.size() ; i++ ){
821 myCurve->setSectionType( aSelSections[i], CurveCreator::Polyline );
822 mySectionView->sectionChanged( aSelSections[i] );
824 // myCurve->finishOperation();
828 void CurveCreator_Widget::onCloseSections()
832 // myCurve->startOperation();
833 QList<int> aSelSections = mySectionView->getSelectedSections();
834 for( int i = 0 ; i < aSelSections.size() ; i++ ){
835 myCurve->setClosed(aSelSections[i], true);
836 mySectionView->sectionChanged(aSelSections[i]);
838 // myCurve->finishOperation();
842 void CurveCreator_Widget::onUncloseSections()
846 // myCurve->startOperation();
847 QList<int> aSelSections = mySectionView->getSelectedSections();
848 for( int i = 0 ; i < aSelSections.size() ; i++ ){
849 myCurve->setClosed(aSelSections[i], false);
850 mySectionView->sectionChanged(aSelSections[i]);
852 // myCurve->finishOperation();
856 void CurveCreator_Widget::onUndo()
861 CurveCreator_Widget::SectionToPointList aPoints;
862 startCurveModification( aPoints, false );
864 finishCurveModification();
865 mySectionView->reset();
868 void CurveCreator_Widget::onRedo()
872 CurveCreator_Widget::SectionToPointList aPoints;
873 startCurveModification( aPoints, false );
875 finishCurveModification();
876 mySectionView->reset();
879 void CurveCreator_Widget::updateUndoRedo()
883 QAction* anAct = myActionMap[UNDO_ID];
885 if( myCurve->getNbUndo() != 0 ){
886 anAct->setEnabled(true);
889 anAct->setDisabled(true);
892 anAct = myActionMap[REDO_ID];
894 if( myCurve->getNbRedo() != 0 ){
895 anAct->setEnabled(true);
898 anAct->setDisabled(true);
903 void CurveCreator_Widget::onContextMenu( QPoint thePoint )
905 QList<ActionId> aContextActions;
906 aContextActions << CLEAR_ALL_ID << JOIN_ALL_ID << SEPARATOR_ID <<
907 CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID << SET_SECTIONS_POLYLINE_ID <<
908 SET_SECTIONS_SPLINE_ID;
909 QPoint aGlPoint = mySectionView->mapToGlobal(thePoint);
911 QList<ActionId> aResAct;
912 for( int i = 0 ; i < aContextActions.size() ; i++ ){
913 if( aContextActions[i] != SEPARATOR_ID ){
914 if( myActionMap.contains(aContextActions[i]) ){
915 QAction* anAct = myActionMap[aContextActions[i]];
916 if( anAct->isEnabled() ){
917 aResAct << aContextActions[i];
923 aResAct << SEPARATOR_ID;
929 QMenu* aMenu = new QMenu(this);
930 for( int i = 0 ; i < aResAct.size() ; i++ ){
931 if( aResAct[i] == SEPARATOR_ID ){
932 aMenu->addSeparator();
935 QAction* anAct = myActionMap[aResAct[i]];
936 aMenu->insertAction(NULL, anAct);
939 aMenu->exec(aGlPoint);
942 QList<int> CurveCreator_Widget::getSelectedSections()
944 return mySectionView->getSelectedSections();
947 QList< QPair< int, int > > CurveCreator_Widget::getSelectedPoints()
949 return mySectionView->getSelectedPoints();
953 * According to the widget state, performs the remove action
955 void CurveCreator_Widget::removeSelected()
961 * Checks whether there are some selection to be removed
963 bool CurveCreator_Widget::removeEnabled()
965 bool isEnabled = getActionMode() == ModificationMode;
967 QList<int> aSelSections = mySectionView->getSelectedSections();
968 CurveCreator_TreeView::SelectionType aSelType = mySectionView->getSelectionType();
969 isEnabled = aSelType == CurveCreator_TreeView::ST_SECTIONS &&
970 aSelSections.size() == 1;
976 //=================================================================================
977 // function : GeometryGUI::onGetCoordsByClick()
978 // purpose : Manage mouse press events in Additon mode
979 //=================================================================================
980 void CurveCreator_Widget::onGetCoordsByClick( QMouseEvent* pe )
982 if (pe->button() != Qt::LeftButton)
985 if ( pe->modifiers() != Qt::ControlModifier ) {
986 Handle(AIS_InteractiveContext) ic = getAISContext();
993 if ( pe->modifiers() == Qt::ShiftModifier )
994 ic->ShiftSelect(); // Append selection
996 ic->Select(); // New selection
998 /*TopoDS_Shape aShape;
1001 if ( ic->MoreSelected() )
1002 aShape = ic->SelectedShape();
1004 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
1005 aPnt = BRep_Tool::Pnt( TopoDS::Vertex( ic->SelectedShape() ) );
1008 OCCViewer_ViewPort3d* vp = getViewPort();
1009 aPnt = CurveCreator_Utils::ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
1011 // set the coordinates into dialog
1012 CurveCreator::Coordinates aCoords;
1013 aCoords.push_back( aPnt.X() );
1014 aCoords.push_back( aPnt.Y() );
1015 if ( myCurve->getDimension() == 3 ) {
1016 aCoords.push_back( aPnt.Z() );
1018 onAddNewPoint(aCoords);
1019 // myNewPointEditor->setCoordinates( aCoords );
1024 * Manage mouse press events
1025 * \param theWindow an owner of the signal
1026 * \param theEvent a mouse event
1028 void CurveCreator_Widget::onMousePress( SUIT_ViewWindow*, QMouseEvent* theEvent )
1030 if ( theEvent->button() != Qt::LeftButton )
1033 switch( getActionMode() ) {
1034 case ModificationMode: {
1035 //store initial cursor position for Drag&Drop
1036 setDragStarted( true, theEvent->pos() );
1039 case AdditionMode: {
1040 onGetCoordsByClick( theEvent );
1049 * Manage mouse release events in Modification mode
1050 * \param theWindow an owner of the signal
1051 * \param theEvent a mouse event
1053 void CurveCreator_Widget::onMouseRelease( SUIT_ViewWindow*, QMouseEvent* theEvent )
1055 if ( getActionMode() != ModificationMode )
1058 if ( myDragStarted ) {
1059 if ( myDragged ) // if the drag of some points has happened, restore the drag selection
1060 setSelectedPonts( myDragPoints );
1061 setDragStarted( false );
1063 else // check whether the segment is clicked an a new point should be added to the segment
1064 insertPointToSelectedSegment( theEvent->pos().x(), theEvent->pos().y() );
1066 // updates the input panel table to show the selected point coordinates
1067 updateLocalPointView();
1071 * Manage mouse move events in Modification mode
1072 * \param theWindow an owner of the signal
1073 * \param theEvent a mouse event
1075 void CurveCreator_Widget::onMouseMove( SUIT_ViewWindow*, QMouseEvent* theEvent )
1077 if ( getActionMode() != ModificationMode || !myDragStarted )
1080 QPoint aPos = theEvent->pos();
1081 if ( (aPos - myDragStartPosition).manhattanLength() < QApplication::startDragDistance() )
1084 moveSelectedPoints( aPos.x(), aPos.y() );
1085 myDragStartPosition = aPos;
1089 * Set zero viewer by the last view closed in
1090 * \param theManager a viewer manager
1092 void CurveCreator_Widget::onLastViewClosed( SUIT_ViewManager* theManager )
1097 void CurveCreator_Widget::onMousePress( QMouseEvent* theEvent )
1099 onMousePress( 0, theEvent );
1102 void CurveCreator_Widget::onMouseRelease( QMouseEvent* theEvent )
1104 onMouseRelease( 0, theEvent );
1107 void CurveCreator_Widget::onMouseMove( QMouseEvent* theEvent )
1109 onMouseMove( 0, theEvent );
1112 void CurveCreator_Widget::onCellChanged( int theRow, int theColumn )
1114 int aCurrSect = getSectionId( theRow );
1115 int aPntIndex = getPointId( theRow );
1117 if ( aPntIndex < 0 )
1120 SectionToPointList aSelPoints;
1121 startCurveModification( aSelPoints );
1123 double aX = myLocalPointView->item( theRow, 2 )->data( Qt::UserRole ).toDouble();
1124 double anY = myLocalPointView->item( theRow, 3 )->data( Qt::UserRole ).toDouble();
1125 std::deque<float> aChangedPos;
1126 aChangedPos.push_back( aX );
1127 aChangedPos.push_back( anY );
1128 myCurve->setPoint( aCurrSect, aPntIndex, aChangedPos );
1130 finishCurveModification( aSelPoints );
1134 * Removes a selected section from the curve. Updates undo/redo status
1136 void CurveCreator_Widget::removeSection()
1138 QList< QPair<int,int> > aSelPoints = mySectionView->getSelectedPoints();
1141 // myCurve->startOperation();
1142 for( int i = 0 ; i < aSelPoints.size() ; i++ ){
1143 if( aCurrSect != aSelPoints[i].first ){
1145 aCurrSect = aSelPoints[i].first;
1147 int aPntIndx = aSelPoints[i].second - aRemoveCnt;
1148 myCurve->removePoint( aCurrSect, aPntIndx );
1149 mySectionView->pointsRemoved( aCurrSect, aPntIndx );
1152 QList<int> aSections = mySectionView->getSelectedSections();
1153 for( int i = 0 ; i < aSections.size() ; i++ ){
1154 int aSectNum = aSections[i] - (i);
1155 myCurve->removeSection( aSectNum );
1156 mySectionView->sectionsRemoved( aSectNum );
1158 // myCurve->finishOperation();
1159 mySectionView->clearSelection();
1164 * Removes a selected points from the curve. Updates undo/redo status
1166 void CurveCreator_Widget::removePoint()
1168 SectionToPointList aPoints;
1169 getSelectedPonts( aPoints );
1170 if ( aPoints.size() == 0 )
1173 SectionToPointList aSelPoints;
1174 startCurveModification( aSelPoints, false );
1176 myCurve->removeSeveralPoints( aPoints );
1177 finishCurveModification( SectionToPointList() );
1180 void CurveCreator_Widget::insertPointToSelectedSegment( const int theX,
1183 Handle(AIS_InteractiveContext) aContext = getAISContext();
1187 gp_Pnt aPoint1, aPoint2;
1188 bool isFoundPoint = false;
1190 for ( aContext->InitSelected(); aContext->MoreSelected() && !isFoundPoint;
1191 aContext->NextSelected() ) {
1192 TopoDS_Shape aTShape = aContext->SelectedShape();
1193 if ( !aTShape.IsNull() && aTShape.ShapeType() == TopAbs_VERTEX )
1196 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
1197 if ( anOwner.IsNull() )
1199 const TopLoc_Location& aLocation = anOwner->Location();
1200 Handle(AIS_InteractiveObject) anAIS = Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
1201 isFoundPoint = pointOnObject( anAIS, theX, theY, aPoint, aPoint1, aPoint2 );
1204 if ( !isFoundPoint )
1207 // insert the point to the model curve
1208 SectionToPointList aSelPoints;
1209 startCurveModification( aSelPoints );
1211 CurveCreator::Coordinates aCoords;
1212 aCoords.push_back( aPoint.X() );
1213 aCoords.push_back( aPoint.Y() );
1215 SectionToPointList aPoints1, aPoints2;
1216 findSectionsToPoints( aPoint1.X(), aPoint1.Y(), aPoints1 );
1217 findSectionsToPoints( aPoint2.X(), aPoint2.Y(), aPoints2 );
1218 SectionToPointList::const_iterator anIt = aPoints1.begin(), aLast = aPoints1.end();
1219 int aSectionId = -1;
1220 // there can be a case when a new point is added into two sections
1221 int aPoint1Id = -1, aPoint2Id = -1;
1222 for ( ; anIt != aLast && aSectionId < 0; anIt++ ) {
1223 int aSectionCur = anIt->first;
1224 SectionToPointList::const_iterator anIt2 = aPoints2.begin(), aLast2 = aPoints2.end();
1225 for ( ; anIt2 != aLast2 && aSectionId < 0; anIt2++ ) {
1226 if ( anIt2->first == aSectionCur ) {
1227 aSectionId = aSectionCur;
1228 aPoint1Id = anIt->second;
1229 aPoint2Id = anIt2->second;
1234 int anInsertPos = -1;
1235 int aLastPoint = myCurve->getNbPoints( aSectionId )-1;
1236 if ( ( aPoint1Id == aLastPoint && aPoint2Id == 0 ) ||
1237 ( aPoint2Id == aLastPoint && aPoint1Id == 0 ) )
1238 anInsertPos = -1; // if the section happens between first and last points
1240 anInsertPos = aPoint1Id < aPoint2Id ? aPoint1Id + 1 : aPoint2Id + 1;
1242 myCurve->addPoints( aCoords, aSectionId, anInsertPos );
1243 mySectionView->pointsAdded( aSectionId, myCurve->getNbPoints( aSectionId ) );
1245 finishCurveModification( aSelPoints );
1250 void CurveCreator_Widget::moveSelectedPoints( const int theXPosition,
1251 const int theYPosition )
1253 OCCViewer_ViewPort3d* aViewPort = getViewPort();
1257 SectionToPointList aPoints;
1258 startCurveModification( aPoints, false );
1260 gp_Pnt aStartPnt = CurveCreator_Utils::ConvertClickToPoint( myDragStartPosition.x(),
1261 myDragStartPosition.y(),
1262 aViewPort->getView() );
1263 gp_Pnt anEndPnt = CurveCreator_Utils::ConvertClickToPoint( theXPosition, theYPosition,
1264 aViewPort->getView() );
1265 double aXDelta = aStartPnt.X() - anEndPnt.X();
1266 double anYDelta = aStartPnt.Y() - anEndPnt.Y();
1268 CurveCreator_ICurve::SectionToPointCoordsList aCoordList;
1269 std::deque<float> aChangedPos;
1270 SectionToPointList::const_iterator anIt = myDragPoints.begin(), aLast = myDragPoints.end();
1271 for ( ; anIt != aLast; anIt++ ) {
1272 int aSectionId = anIt->first;
1273 int aPointId = anIt->second;
1274 aChangedPos = myCurve->getPoint( aSectionId, aPointId );
1275 if ( aChangedPos.size() < 2 )
1277 aChangedPos[0] = aChangedPos[0] - aXDelta;
1278 aChangedPos[1] = aChangedPos[1] - anYDelta;
1280 aCoordList.push_back(
1281 std::make_pair(std::make_pair( aSectionId, aPointId ),
1284 myCurve->setSeveralPoints( aCoordList );
1287 finishCurveModification( myDragPoints );
1290 void CurveCreator_Widget::updateLocalPointView()
1292 Handle(AIS_InteractiveContext) aContext = getAISContext();
1296 bool isBlocked = myLocalPointView->blockSignals(true);
1298 myLocalPointView->setRowCount( 0 );
1299 for ( aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected() ) {
1300 TopoDS_Vertex aVertex;
1301 TopoDS_Shape aShape = aContext->SelectedShape();
1302 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
1303 aVertex = TopoDS::Vertex( aContext->SelectedShape() );
1305 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
1306 if ( !anOwner.IsNull() ) {
1307 Handle(AIS_InteractiveObject) anAIS = Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
1308 if ( !anAIS.IsNull() ) {
1309 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast( anAIS);
1310 if ( !aPoint.IsNull() )
1311 aVertex = TopoDS::Vertex( aPoint->Vertex() );
1313 if ( aVertex.IsNull() ) {
1314 // the following happens if there are no points in the current curve, there is only a shape
1315 /*Handle(StdSelect_BRepOwner) aBrepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1316 if ( aBrepOwner.IsNull() )
1318 if ( aBrepOwner->HasShape() ) {
1319 const TopoDS_Shape& aShape = aBrepOwner->Shape();
1320 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
1322 aVertex = TopoDS::Vertex( aShape );
1327 if ( aVertex.IsNull() )
1329 aPnt = BRep_Tool::Pnt( aVertex );
1330 addLocalPointToTable( aPnt.X(), aPnt.Y() );
1333 myLocalPointView->blockSignals(isBlocked);
1339 void CurveCreator_Widget::setLocalPointContext( const bool theOpen, const bool isUpdateTable )
1341 Handle(AIS_InteractiveContext) ic = getAISContext();
1346 // Open local context if there is no one
1347 if ( !ic->HasOpenedContext() ) {
1348 ic->ClearCurrents( false );
1349 ic->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
1351 AIS_ListOfInteractive aList;
1352 ic->DisplayedObjects( aList );
1354 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
1357 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
1359 Handle(AIS_InteractiveObject) anAIS = it.Value();
1360 if ( !anAIS.IsNull() )
1362 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
1364 ic->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
1365 ic->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_WIRE ) );
1367 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
1369 ic->Load( anAIS, -1/*selection mode*/, false/*allow decomposition*/ );
1370 ic->Activate( anAIS, TopAbs_VERTEX );
1377 if ( ic->HasOpenedContext() )
1378 ic->CloseAllContexts();
1379 if ( isUpdateTable )
1380 updateLocalPointView();
1384 void CurveCreator_Widget::addLocalPointToTable( const double theX, const double theY )
1386 SectionToPointList aPoints;
1387 findSectionsToPoints( theX, theY, aPoints );
1389 SectionToPointList aSkipList;
1390 // table could not contain two equal value rows
1391 int aRowId = myLocalPointView->rowCount();
1392 double aCurrentX, aCurrentY;
1393 int aSectionId, aPointId;
1394 SectionToPoint aPoint;
1395 for ( int i = 0; i < aRowId; i++ ) {
1396 aCurrentX = myLocalPointView->item( i, 2 )->data( Qt::UserRole ).toDouble();
1397 aCurrentY = myLocalPointView->item( i, 3 )->data( Qt::UserRole ).toDouble();
1398 if ( fabs( aCurrentX - theX ) < LOCAL_SELECTION_TOLERANCE &&
1399 fabs( aCurrentY - theY ) < LOCAL_SELECTION_TOLERANCE ) {
1400 aPoint = std::make_pair<int, int>( getSectionId( i ), getPointId( i ) );
1401 if ( !contains( aSkipList, aPoint ) )
1402 aSkipList.push_back( aPoint );
1405 if ( aSkipList.size() == aPoints.size() )
1408 QTableWidgetItem* anItem;
1409 SectionToPointList::const_iterator anIt = aPoints.begin(), aLast = aPoints.end();
1410 for ( ; anIt != aLast; anIt++ ) {
1412 if ( contains( aSkipList, aPoint ) )
1415 myLocalPointView->setRowCount( aRowId+1 );
1416 aSectionId = aPoint.first;
1417 aPointId = aPoint.second;
1419 anItem = new QTableWidgetItem( myCurve->getSectionName( aSectionId ).c_str() );
1420 anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
1421 anItem->setData( Qt::UserRole, aSectionId );
1422 myLocalPointView->setItem( aRowId, 0, anItem );
1424 anItem = new QTableWidgetItem( QString::number( aPointId + 1 ) );
1425 anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
1426 anItem->setData( Qt::UserRole, aPointId );
1427 myLocalPointView->setItem( aRowId, 1, anItem );
1429 anItem = new QTableWidgetItem( QString::number( theX ) );
1430 anItem->setData( Qt::UserRole, theX );
1431 myLocalPointView->setItem( aRowId, 2, anItem );
1433 anItem = new QTableWidgetItem( QString::number( theY ) );
1434 anItem->setData( Qt::UserRole, theY );
1435 myLocalPointView->setItem( aRowId, 3, anItem );
1440 * Set drag operation started. Save the position and a list of dragged points
1441 * \param theState the drag operation state: started/finished
1442 * \param thePoint the start drag position
1444 void CurveCreator_Widget::setDragStarted( const bool theState, const QPoint& thePoint )
1447 getSelectedPonts( myDragPoints );
1448 myDragStarted = myDragPoints.size();
1449 myDragStartPosition = thePoint;
1450 if ( myDragStarted ) {
1451 // change a viewer interaction style in order to avoid a select rectangle build
1452 myDragInteractionStyle = changeInteractionStyle( SUIT_ViewModel::KEY_FREE );
1456 if ( myDragStarted )
1457 changeInteractionStyle( myDragInteractionStyle );
1458 myDragStarted = false;
1459 myDragPoints.clear();
1464 void CurveCreator_Widget::getSelectedPonts( CurveCreator_Widget::SectionToPointList& thePoints )
1467 for ( int i = 0, aNb = myLocalPointView->rowCount(); i < aNb; i++ )
1468 thePoints.push_back( std::make_pair( getSectionId( i ), getPointId( i ) ) );
1471 void CurveCreator_Widget::setSelectedPonts( const CurveCreator_Widget::SectionToPointList& thePoints )
1473 Handle(AIS_InteractiveContext) ic = getAISContext();
1474 if ( !ic || !ic->HasOpenedContext() )
1477 AIS_ListOfInteractive aListToSelect;
1478 AIS_ListOfInteractive aDisplayedList;
1479 ic->DisplayedObjects( aDisplayedList );
1481 SectionToPointList::const_iterator anIt = thePoints.begin(), aLast = thePoints.end();
1482 SectionToPoint aSToPoint;
1483 for( ; anIt != aLast; anIt++ ) {
1486 for ( AIS_ListIteratorOfListOfInteractive it( aDisplayedList ); it.More(); it.Next() )
1488 Handle(AIS_InteractiveObject) anAIS = it.Value();
1489 if ( anAIS.IsNull() )
1491 Handle(AIS_Point) anAISPoint = Handle(AIS_Point)::DownCast( anAIS );
1492 if ( anAISPoint.IsNull() )
1495 TopoDS_Vertex aVertex = TopoDS::Vertex( anAISPoint->Vertex() );
1497 if ( aVertex.IsNull() )
1500 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
1502 SectionToPointList aPoints;
1503 findSectionsToPoints( aPnt.X(), aPnt.Y(), aPoints );
1505 SectionToPointList::const_iterator anIt = aPoints.begin(), aLast = aPoints.end();
1506 SectionToPoint aPoint;
1507 for ( ; anIt != aLast; anIt++ ) {
1509 if ( aPoint.first == aSToPoint.first && aPoint.second == aSToPoint.second )
1510 aListToSelect.Append( anAIS );
1515 ic->ClearSelected( Standard_False );
1516 setObjectsSelected( aListToSelect );
1517 updateLocalPointView();
1521 * Get viewer information before perform the curve modification.
1522 * Take a list of selected cuve points an close local context.
1523 * The context should be closed because the curve presentation is
1524 * redisplayed and if it is not closed, when we close the local context
1525 * later, the presentation shown in the local context is disappeared.
1526 * \param thePoints an output list of curve selected points
1527 * \param theFillPoints a flag whether the selection list should be filled
1529 void CurveCreator_Widget::startCurveModification(
1530 CurveCreator_Widget::SectionToPointList& thePoints,
1531 const bool theFillPoints )
1533 if ( theFillPoints ) {
1535 getSelectedPonts( thePoints );
1537 setLocalPointContext( false );
1541 * Restore the viewer state after the curve modification is done.
1542 * Open local context and select given points inside it.
1543 * \param thePoints a list of curve selected points
1545 void CurveCreator_Widget::finishCurveModification(
1546 const CurveCreator_Widget::SectionToPointList& thePoints )
1548 if ( getActionMode() == ModificationMode )
1549 setLocalPointContext( true );
1550 setSelectedPonts( thePoints );
1555 * Returns a point index in the model curve by the point coordinates in the viewer
1556 * \param theX the X coordinate of the point
1557 * \param theY the Y coordinate of the point
1559 int CurveCreator_Widget::findLocalPointIndex( int theSectionId, float theX, float theY )
1563 CurveCreator::Coordinates aCoords;
1564 for ( int i = 0, aNb = myCurve->getNbPoints( theSectionId ); i < aNb && aPntIndex < 0; i++ ) {
1565 aCoords = myCurve->getPoint( theSectionId, i );
1566 if ( aCoords.size() < 2 )
1568 if ( fabs( aCoords[0] - theX ) < LOCAL_SELECTION_TOLERANCE &&
1569 fabs( aCoords[1] - theY ) < LOCAL_SELECTION_TOLERANCE )
1576 void CurveCreator_Widget::findSectionsToPoints( const double theX, const double theY,
1577 CurveCreator_Widget::SectionToPointList& thePoints )
1582 for ( int i = 0, aNb = myCurve->getNbSections(); i < aNb; i++ ) {
1583 aPointId = findLocalPointIndex( i, theX, theY );
1586 SectionToPoint aPoint = std::make_pair( i, aPointId );
1587 if ( !contains( thePoints, aPoint ) )
1588 thePoints.push_back( aPoint );
1592 void CurveCreator_Widget::convert( const SectionToPointList& thePoints,
1593 QMap<int, QList<int> >& theConvPoints )
1595 theConvPoints.clear();
1597 SectionToPointList::const_iterator anIt = thePoints.begin(), aLast = thePoints.end();
1599 int aSectionId, aPointId;
1600 for ( ; anIt != aLast; anIt++ ) {
1601 aSectionId = anIt->first;
1602 aPointId = anIt->second;
1604 if ( theConvPoints.contains( aSectionId ) )
1605 aPoints = theConvPoints[aSectionId];
1606 if ( aPoints.contains( aPointId ) )
1608 aPoints.append( aPointId );
1609 theConvPoints[aSectionId] = aPoints;
1614 * Checks whether the point belongs to the OCC object
1615 * \param theObject a line or shape with a bspline inside
1616 * \param theX the X coordinate in the view.
1617 * \param theY the Y coordinate in the view.
1618 * \param thePoint the output point to be append to the model curve
1619 * \param thePoint1 the output point to bound the line where a new point should be inserted
1620 * \param thePoint2 the output point to bound the line where a new point should be inserted
1622 bool CurveCreator_Widget::pointOnObject( Handle(AIS_InteractiveObject) theObject,
1623 const int theX, const int theY,
1625 gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
1627 bool isFound = false;
1629 Handle(AIS_InteractiveContext) aContext = getAISContext();
1630 if ( theObject.IsNull() || !aContext )
1634 Standard_Real aParameter;
1635 gp_Pnt aPnt1, aPnt2;
1636 Handle(AIS_Line) aLine = Handle(AIS_Line)::DownCast( theObject );
1637 if ( !aLine.IsNull() ) {
1638 const Handle(Geom_Line) aGLine = aLine->Line();
1639 isFound = hasProjectPointOnCurve( theX, theY, aGLine, aParameter );
1641 aPoint = aGLine->Value( aParameter );
1643 Handle(Geom_Point) aPStart;
1644 Handle(Geom_Point) aPEnd;
1645 aLine->Points( aPStart, aPEnd );
1646 aPnt1 = aPStart->Pnt();
1647 aPnt2 = aPEnd->Pnt();
1649 // in case of Geom line a projection is performed to the infinite line,
1650 // so it is necessary to bound it by the line size
1652 aLineBox.Set( aPnt1, gp_Vec( aPnt1, aPnt2 ) );
1653 isFound = !aLineBox.IsOut( aPoint );
1657 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
1658 if ( !aShape.IsNull() ) {
1659 const TopoDS_Wire& aWire = TopoDS::Wire( aShape->Shape() );
1660 if ( !aWire.IsNull() ) {
1661 TopExp_Explorer anExp( aWire, TopAbs_EDGE );
1662 for ( ; anExp.More(); anExp.Next())
1664 const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
1665 if ( !anEdge.IsNull() ) {
1666 Standard_Real aFirst, aLast;
1667 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
1669 if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
1670 Handle(Geom_BSplineCurve) aBSplineCurve = Handle(Geom_BSplineCurve)::DownCast( aCurve );
1671 if ( !aBSplineCurve.IsNull() ) {
1672 isFound = hasProjectPointOnCurve( theX, theY, aCurve, aParameter );
1674 aPoint = aBSplineCurve->Value( aParameter );
1675 Standard_Integer anI1, anI2;
1676 aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
1678 aPnt1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
1679 aPnt2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
1697 * Returns whether the clicked point belong to the curve or has a very near projection
1698 * \param theX the X coordinate of a point clicked in the OCC viewer
1699 * \param theY the Y coordinate of a point clicked in the OCC viewer
1700 * \param theCurve a geometry curve
1701 * \param theOutPoint a found projected point on the curve
1703 bool CurveCreator_Widget::hasProjectPointOnCurve( const int theX, const int theY,
1704 const Handle(Geom_Curve)& theCurve,
1705 Standard_Real& theParameter )
1707 bool isFound = false;
1708 OCCViewer_ViewPort3d* aViewPort = getViewPort();
1712 Handle(V3d_View) aView = aViewPort->getView();
1713 gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, aView );
1715 GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
1716 Standard_Integer aNbPoint = aProj.NbPoints();
1718 for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
1719 gp_Pnt aNewPoint = aProj.Point( j );
1720 theParameter = aProj.Parameter( j );
1723 CurveCreator_Utils::ConvertPointToClick( aNewPoint, aView, aX, anY );
1725 int aXDelta = abs( aX - theX );
1726 int anYDelta = abs( anY - theY );
1727 isFound = aXDelta < SCENE_PIXEL_TOLERANCE && anYDelta < SCENE_PIXEL_TOLERANCE;
1734 * Returns a section index from the table
1735 * \param theRowId a table row
1737 int CurveCreator_Widget::getSectionId( const int theRowId ) const
1739 return myLocalPointView->item( theRowId, 0 )->data( Qt::UserRole ).toInt();
1743 * Returns a point index from the table
1744 * \param theRowId a table row
1746 int CurveCreator_Widget::getPointId( const int theRowId ) const
1748 return myLocalPointView->item( theRowId, 1 )->data( Qt::UserRole ).toInt();
1752 * Returns whethe the container has the value
1753 * \param theList a container of values
1754 * \param theValue a value
1756 bool CurveCreator_Widget::contains( const CurveCreator_Widget::SectionToPointList& theList,
1757 const CurveCreator_Widget::SectionToPoint& theValue ) const
1759 bool isFound = false;
1761 SectionToPointList::const_iterator anIt = theList.begin(), aLast = theList.end();
1762 for ( ; anIt != aLast && !isFound; anIt++ )
1763 isFound = anIt->first == theValue.first && anIt->second == theValue.second;