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 bool isDragged = myDragged;
1060 CurveCreator_Widget::SectionToPointList aDraggedPoints;
1062 aDraggedPoints = myDragPoints;
1064 setDragStarted( false );
1065 // if the drag of some points has happened, restore the drag selection
1066 if ( aDraggedPoints.size() > 0 )
1067 setSelectedPonts( aDraggedPoints );
1069 else // check whether the segment is clicked an a new point should be added to the segment
1070 insertPointToSelectedSegment( theEvent->pos().x(), theEvent->pos().y() );
1072 // updates the input panel table to show the selected point coordinates
1073 updateLocalPointView();
1077 * Manage mouse move events in Modification mode
1078 * \param theWindow an owner of the signal
1079 * \param theEvent a mouse event
1081 void CurveCreator_Widget::onMouseMove( SUIT_ViewWindow*, QMouseEvent* theEvent )
1083 if ( getActionMode() != ModificationMode || !myDragStarted )
1086 QPoint aPos = theEvent->pos();
1087 if ( (aPos - myDragStartPosition).manhattanLength() < QApplication::startDragDistance() )
1090 moveSelectedPoints( aPos.x(), aPos.y() );
1091 myDragStartPosition = aPos;
1095 * Set zero viewer by the last view closed in
1096 * \param theManager a viewer manager
1098 void CurveCreator_Widget::onLastViewClosed( SUIT_ViewManager* theManager )
1103 void CurveCreator_Widget::onMousePress( QMouseEvent* theEvent )
1105 onMousePress( 0, theEvent );
1108 void CurveCreator_Widget::onMouseRelease( QMouseEvent* theEvent )
1110 onMouseRelease( 0, theEvent );
1113 void CurveCreator_Widget::onMouseMove( QMouseEvent* theEvent )
1115 onMouseMove( 0, theEvent );
1118 void CurveCreator_Widget::onCellChanged( int theRow, int theColumn )
1120 int aCurrSect = getSectionId( theRow );
1121 int aPntIndex = getPointId( theRow );
1123 if ( aPntIndex < 0 )
1126 SectionToPointList aSelPoints;
1127 startCurveModification( aSelPoints );
1129 double aX = myLocalPointView->item( theRow, 2 )->data( Qt::UserRole ).toDouble();
1130 double anY = myLocalPointView->item( theRow, 3 )->data( Qt::UserRole ).toDouble();
1131 std::deque<float> aChangedPos;
1132 aChangedPos.push_back( aX );
1133 aChangedPos.push_back( anY );
1134 myCurve->setPoint( aCurrSect, aPntIndex, aChangedPos );
1136 finishCurveModification( aSelPoints );
1140 * Removes a selected section from the curve. Updates undo/redo status
1142 void CurveCreator_Widget::removeSection()
1144 QList< QPair<int,int> > aSelPoints = mySectionView->getSelectedPoints();
1147 // myCurve->startOperation();
1148 for( int i = 0 ; i < aSelPoints.size() ; i++ ){
1149 if( aCurrSect != aSelPoints[i].first ){
1151 aCurrSect = aSelPoints[i].first;
1153 int aPntIndx = aSelPoints[i].second - aRemoveCnt;
1154 myCurve->removePoint( aCurrSect, aPntIndx );
1155 mySectionView->pointsRemoved( aCurrSect, aPntIndx );
1158 QList<int> aSections = mySectionView->getSelectedSections();
1159 for( int i = 0 ; i < aSections.size() ; i++ ){
1160 int aSectNum = aSections[i] - (i);
1161 myCurve->removeSection( aSectNum );
1162 mySectionView->sectionsRemoved( aSectNum );
1164 // myCurve->finishOperation();
1165 mySectionView->clearSelection();
1170 * Removes a selected points from the curve. Updates undo/redo status
1172 void CurveCreator_Widget::removePoint()
1174 SectionToPointList aPoints;
1175 getSelectedPonts( aPoints );
1176 if ( aPoints.size() == 0 )
1179 SectionToPointList aSelPoints;
1180 startCurveModification( aSelPoints, false );
1182 myCurve->removeSeveralPoints( aPoints );
1183 finishCurveModification( SectionToPointList() );
1186 void CurveCreator_Widget::insertPointToSelectedSegment( const int theX,
1189 Handle(AIS_InteractiveContext) aContext = getAISContext();
1193 gp_Pnt aPoint1, aPoint2;
1194 bool isFoundPoint = false;
1196 for ( aContext->InitSelected(); aContext->MoreSelected() && !isFoundPoint;
1197 aContext->NextSelected() ) {
1198 TopoDS_Shape aTShape = aContext->SelectedShape();
1199 if ( !aTShape.IsNull() && aTShape.ShapeType() == TopAbs_VERTEX )
1202 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
1203 if ( anOwner.IsNull() )
1205 const TopLoc_Location& aLocation = anOwner->Location();
1206 Handle(AIS_InteractiveObject) anAIS = Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
1207 isFoundPoint = pointOnObject( anAIS, theX, theY, aPoint, aPoint1, aPoint2 );
1210 if ( !isFoundPoint )
1213 // insert the point to the model curve
1214 SectionToPointList aSelPoints;
1215 startCurveModification( aSelPoints );
1217 CurveCreator::Coordinates aCoords;
1218 aCoords.push_back( aPoint.X() );
1219 aCoords.push_back( aPoint.Y() );
1221 SectionToPointList aPoints1, aPoints2;
1222 findSectionsToPoints( aPoint1.X(), aPoint1.Y(), aPoints1 );
1223 findSectionsToPoints( aPoint2.X(), aPoint2.Y(), aPoints2 );
1224 SectionToPointList::const_iterator anIt = aPoints1.begin(), aLast = aPoints1.end();
1225 int aSectionId = -1;
1226 // there can be a case when a new point is added into two sections
1227 int aPoint1Id = -1, aPoint2Id = -1;
1228 for ( ; anIt != aLast && aSectionId < 0; anIt++ ) {
1229 int aSectionCur = anIt->first;
1230 SectionToPointList::const_iterator anIt2 = aPoints2.begin(), aLast2 = aPoints2.end();
1231 for ( ; anIt2 != aLast2 && aSectionId < 0; anIt2++ ) {
1232 if ( anIt2->first == aSectionCur ) {
1233 aSectionId = aSectionCur;
1234 aPoint1Id = anIt->second;
1235 aPoint2Id = anIt2->second;
1240 int anInsertPos = -1;
1241 int aLastPoint = myCurve->getNbPoints( aSectionId )-1;
1242 if ( ( aPoint1Id == aLastPoint && aPoint2Id == 0 ) ||
1243 ( aPoint2Id == aLastPoint && aPoint1Id == 0 ) )
1244 anInsertPos = -1; // if the section happens between first and last points
1246 anInsertPos = aPoint1Id < aPoint2Id ? aPoint1Id + 1 : aPoint2Id + 1;
1248 myCurve->addPoints( aCoords, aSectionId, anInsertPos );
1249 mySectionView->pointsAdded( aSectionId, myCurve->getNbPoints( aSectionId ) );
1251 finishCurveModification( aSelPoints );
1256 void CurveCreator_Widget::moveSelectedPoints( const int theXPosition,
1257 const int theYPosition )
1259 OCCViewer_ViewPort3d* aViewPort = getViewPort();
1263 SectionToPointList aPoints;
1264 startCurveModification( aPoints, false );
1266 gp_Pnt aStartPnt = CurveCreator_Utils::ConvertClickToPoint( myDragStartPosition.x(),
1267 myDragStartPosition.y(),
1268 aViewPort->getView() );
1269 gp_Pnt anEndPnt = CurveCreator_Utils::ConvertClickToPoint( theXPosition, theYPosition,
1270 aViewPort->getView() );
1271 double aXDelta = aStartPnt.X() - anEndPnt.X();
1272 double anYDelta = aStartPnt.Y() - anEndPnt.Y();
1274 CurveCreator_ICurve::SectionToPointCoordsList aCoordList;
1275 std::deque<float> aChangedPos;
1276 SectionToPointList::const_iterator anIt = myDragPoints.begin(), aLast = myDragPoints.end();
1277 for ( ; anIt != aLast; anIt++ ) {
1278 int aSectionId = anIt->first;
1279 int aPointId = anIt->second;
1280 aChangedPos = myCurve->getPoint( aSectionId, aPointId );
1281 if ( aChangedPos.size() < 2 )
1283 aChangedPos[0] = aChangedPos[0] - aXDelta;
1284 aChangedPos[1] = aChangedPos[1] - anYDelta;
1286 aCoordList.push_back(
1287 std::make_pair(std::make_pair( aSectionId, aPointId ),
1290 myCurve->setSeveralPoints( aCoordList );
1293 finishCurveModification( myDragPoints );
1296 void CurveCreator_Widget::updateLocalPointView()
1298 if ( myDragStarted )
1300 Handle(AIS_InteractiveContext) aContext = getAISContext();
1304 bool isBlocked = myLocalPointView->blockSignals(true);
1306 myLocalPointView->setRowCount( 0 );
1307 for ( aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected() ) {
1308 TopoDS_Vertex aVertex;
1309 TopoDS_Shape aShape = aContext->SelectedShape();
1310 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
1311 aVertex = TopoDS::Vertex( aContext->SelectedShape() );
1313 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
1314 if ( !anOwner.IsNull() ) {
1315 Handle(AIS_InteractiveObject) anAIS = Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
1316 if ( !anAIS.IsNull() ) {
1317 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast( anAIS);
1318 if ( !aPoint.IsNull() )
1319 aVertex = TopoDS::Vertex( aPoint->Vertex() );
1321 if ( aVertex.IsNull() ) {
1322 // the following happens if there are no points in the current curve, there is only a shape
1323 /*Handle(StdSelect_BRepOwner) aBrepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1324 if ( aBrepOwner.IsNull() )
1326 if ( aBrepOwner->HasShape() ) {
1327 const TopoDS_Shape& aShape = aBrepOwner->Shape();
1328 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
1330 aVertex = TopoDS::Vertex( aShape );
1336 if ( aVertex.IsNull() )
1338 aPnt = BRep_Tool::Pnt( aVertex );
1339 addLocalPointToTable( aPnt.X(), aPnt.Y() );
1341 myLocalPointView->blockSignals(isBlocked);
1347 void CurveCreator_Widget::setLocalPointContext( const bool theOpen, const bool isUpdateTable )
1349 Handle(AIS_InteractiveContext) ic = getAISContext();
1354 // Open local context if there is no one
1355 if ( !ic->HasOpenedContext() ) {
1356 ic->ClearCurrents( false );
1357 ic->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
1359 AIS_ListOfInteractive aList;
1360 ic->DisplayedObjects( aList );
1362 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
1365 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
1367 Handle(AIS_InteractiveObject) anAIS = it.Value();
1368 if ( !anAIS.IsNull() )
1370 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
1372 ic->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
1373 ic->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_WIRE ) );
1374 //ic->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX ) );
1376 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
1378 ic->Load( anAIS, -1/*selection mode*/, false/*allow decomposition*/ );
1379 ic->Activate( anAIS, TopAbs_VERTEX );
1386 if ( ic->HasOpenedContext() )
1387 ic->CloseAllContexts();
1388 if ( isUpdateTable )
1389 updateLocalPointView();
1393 void CurveCreator_Widget::addLocalPointToTable( const double theX, const double theY )
1395 SectionToPointList aPoints;
1396 findSectionsToPoints( theX, theY, aPoints );
1398 SectionToPointList aSkipList;
1399 // table could not contain two equal value rows
1400 int aRowId = myLocalPointView->rowCount();
1401 double aCurrentX, aCurrentY;
1402 int aSectionId, aPointId;
1403 SectionToPoint aPoint;
1404 for ( int i = 0; i < aRowId; i++ ) {
1405 aCurrentX = myLocalPointView->item( i, 2 )->data( Qt::UserRole ).toDouble();
1406 aCurrentY = myLocalPointView->item( i, 3 )->data( Qt::UserRole ).toDouble();
1407 if ( fabs( aCurrentX - theX ) < LOCAL_SELECTION_TOLERANCE &&
1408 fabs( aCurrentY - theY ) < LOCAL_SELECTION_TOLERANCE ) {
1409 aPoint = std::make_pair<int, int>( getSectionId( i ), getPointId( i ) );
1410 if ( !contains( aSkipList, aPoint ) )
1411 aSkipList.push_back( aPoint );
1414 if ( aSkipList.size() == aPoints.size() )
1417 QTableWidgetItem* anItem;
1418 SectionToPointList::const_iterator anIt = aPoints.begin(), aLast = aPoints.end();
1419 for ( ; anIt != aLast; anIt++ ) {
1421 if ( contains( aSkipList, aPoint ) )
1424 myLocalPointView->setRowCount( aRowId+1 );
1425 aSectionId = aPoint.first;
1426 aPointId = aPoint.second;
1428 anItem = new QTableWidgetItem( myCurve->getSectionName( aSectionId ).c_str() );
1429 anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
1430 anItem->setData( Qt::UserRole, aSectionId );
1431 myLocalPointView->setItem( aRowId, 0, anItem );
1433 anItem = new QTableWidgetItem( QString::number( aPointId + 1 ) );
1434 anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
1435 anItem->setData( Qt::UserRole, aPointId );
1436 myLocalPointView->setItem( aRowId, 1, anItem );
1438 anItem = new QTableWidgetItem( QString::number( theX ) );
1439 anItem->setData( Qt::UserRole, theX );
1440 myLocalPointView->setItem( aRowId, 2, anItem );
1442 anItem = new QTableWidgetItem( QString::number( theY ) );
1443 anItem->setData( Qt::UserRole, theY );
1444 myLocalPointView->setItem( aRowId, 3, anItem );
1449 * Set drag operation started. Save the position and a list of dragged points
1450 * \param theState the drag operation state: started/finished
1451 * \param thePoint the start drag position
1453 void CurveCreator_Widget::setDragStarted( const bool theState, const QPoint& thePoint )
1456 getSelectedPonts( myDragPoints );
1457 myDragStarted = myDragPoints.size();
1458 myDragStartPosition = thePoint;
1459 if ( myDragStarted ) {
1460 // change a viewer interaction style in order to avoid a select rectangle build
1461 myDragInteractionStyle = changeInteractionStyle( SUIT_ViewModel::KEY_FREE );
1465 if ( myDragStarted )
1466 changeInteractionStyle( myDragInteractionStyle );
1467 myDragStarted = false;
1468 myDragPoints.clear();
1473 void CurveCreator_Widget::getSelectedPonts( CurveCreator_Widget::SectionToPointList& thePoints )
1476 for ( int i = 0, aNb = myLocalPointView->rowCount(); i < aNb; i++ )
1477 thePoints.push_back( std::make_pair( getSectionId( i ), getPointId( i ) ) );
1480 void CurveCreator_Widget::setSelectedPonts( const CurveCreator_Widget::SectionToPointList& thePoints )
1482 if ( myDragStarted )
1484 Handle(AIS_InteractiveContext) ic = getAISContext();
1485 if ( !ic || !ic->HasOpenedContext() )
1488 AIS_ListOfInteractive aListToSelect;
1489 AIS_ListOfInteractive aDisplayedList;
1490 ic->DisplayedObjects( aDisplayedList );
1492 SectionToPointList::const_iterator anIt = thePoints.begin(), aLast = thePoints.end();
1493 SectionToPoint aSToPoint;
1494 for( ; anIt != aLast; anIt++ ) {
1497 for ( AIS_ListIteratorOfListOfInteractive it( aDisplayedList ); it.More(); it.Next() )
1499 Handle(AIS_InteractiveObject) anAIS = it.Value();
1500 if ( anAIS.IsNull() )
1502 Handle(AIS_Point) anAISPoint = Handle(AIS_Point)::DownCast( anAIS );
1503 if ( anAISPoint.IsNull() )
1506 TopoDS_Vertex aVertex = TopoDS::Vertex( anAISPoint->Vertex() );
1508 if ( aVertex.IsNull() )
1511 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
1513 SectionToPointList aPoints;
1514 findSectionsToPoints( aPnt.X(), aPnt.Y(), aPoints );
1516 SectionToPointList::const_iterator anIt = aPoints.begin(), aLast = aPoints.end();
1517 SectionToPoint aPoint;
1518 for ( ; anIt != aLast; anIt++ ) {
1520 if ( aPoint.first == aSToPoint.first && aPoint.second == aSToPoint.second )
1521 aListToSelect.Append( anAIS );
1526 ic->ClearSelected( Standard_False );
1527 setObjectsSelected( aListToSelect );
1528 updateLocalPointView();
1532 * Get viewer information before perform the curve modification.
1533 * Take a list of selected cuve points an close local context.
1534 * The context should be closed because the curve presentation is
1535 * redisplayed and if it is not closed, when we close the local context
1536 * later, the presentation shown in the local context is disappeared.
1537 * \param thePoints an output list of curve selected points
1538 * \param theFillPoints a flag whether the selection list should be filled
1540 void CurveCreator_Widget::startCurveModification(
1541 CurveCreator_Widget::SectionToPointList& thePoints,
1542 const bool theFillPoints )
1544 if ( theFillPoints ) {
1546 getSelectedPonts( thePoints );
1548 setLocalPointContext( false );
1552 * Restore the viewer state after the curve modification is done.
1553 * Open local context and select given points inside it.
1554 * \param thePoints a list of curve selected points
1556 void CurveCreator_Widget::finishCurveModification(
1557 const CurveCreator_Widget::SectionToPointList& thePoints )
1559 if ( getActionMode() == ModificationMode )
1560 setLocalPointContext( true );
1561 setSelectedPonts( thePoints );
1566 * Returns a point index in the model curve by the point coordinates in the viewer
1567 * \param theX the X coordinate of the point
1568 * \param theY the Y coordinate of the point
1570 int CurveCreator_Widget::findLocalPointIndex( int theSectionId, float theX, float theY )
1574 CurveCreator::Coordinates aCoords;
1575 for ( int i = 0, aNb = myCurve->getNbPoints( theSectionId ); i < aNb && aPntIndex < 0; i++ ) {
1576 aCoords = myCurve->getPoint( theSectionId, i );
1577 if ( aCoords.size() < 2 )
1579 if ( fabs( aCoords[0] - theX ) < LOCAL_SELECTION_TOLERANCE &&
1580 fabs( aCoords[1] - theY ) < LOCAL_SELECTION_TOLERANCE )
1587 void CurveCreator_Widget::findSectionsToPoints( const double theX, const double theY,
1588 CurveCreator_Widget::SectionToPointList& thePoints )
1593 for ( int i = 0, aNb = myCurve->getNbSections(); i < aNb; i++ ) {
1594 aPointId = findLocalPointIndex( i, theX, theY );
1597 SectionToPoint aPoint = std::make_pair( i, aPointId );
1598 if ( !contains( thePoints, aPoint ) )
1599 thePoints.push_back( aPoint );
1603 void CurveCreator_Widget::convert( const SectionToPointList& thePoints,
1604 QMap<int, QList<int> >& theConvPoints )
1606 theConvPoints.clear();
1608 SectionToPointList::const_iterator anIt = thePoints.begin(), aLast = thePoints.end();
1610 int aSectionId, aPointId;
1611 for ( ; anIt != aLast; anIt++ ) {
1612 aSectionId = anIt->first;
1613 aPointId = anIt->second;
1615 if ( theConvPoints.contains( aSectionId ) )
1616 aPoints = theConvPoints[aSectionId];
1617 if ( aPoints.contains( aPointId ) )
1619 aPoints.append( aPointId );
1620 theConvPoints[aSectionId] = aPoints;
1625 * Checks whether the point belongs to the OCC object
1626 * \param theObject a line or shape with a bspline inside
1627 * \param theX the X coordinate in the view.
1628 * \param theY the Y coordinate in the view.
1629 * \param thePoint the output point to be append to the model curve
1630 * \param thePoint1 the output point to bound the line where a new point should be inserted
1631 * \param thePoint2 the output point to bound the line where a new point should be inserted
1633 bool CurveCreator_Widget::pointOnObject( Handle(AIS_InteractiveObject) theObject,
1634 const int theX, const int theY,
1636 gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
1638 bool isFound = false;
1640 Handle(AIS_InteractiveContext) aContext = getAISContext();
1641 if ( theObject.IsNull() || !aContext )
1645 Standard_Real aParameter;
1646 gp_Pnt aPnt1, aPnt2;
1647 Handle(AIS_Line) aLine = Handle(AIS_Line)::DownCast( theObject );
1648 if ( !aLine.IsNull() ) {
1649 const Handle(Geom_Line) aGLine = aLine->Line();
1650 isFound = hasProjectPointOnCurve( theX, theY, aGLine, aParameter );
1652 aPoint = aGLine->Value( aParameter );
1654 Handle(Geom_Point) aPStart;
1655 Handle(Geom_Point) aPEnd;
1656 aLine->Points( aPStart, aPEnd );
1657 aPnt1 = aPStart->Pnt();
1658 aPnt2 = aPEnd->Pnt();
1660 // in case of Geom line a projection is performed to the infinite line,
1661 // so it is necessary to bound it by the line size
1663 aLineBox.Set( aPnt1, gp_Vec( aPnt1, aPnt2 ) );
1664 isFound = !aLineBox.IsOut( aPoint );
1668 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
1669 if ( !aShape.IsNull() ) {
1670 const TopoDS_Wire& aWire = TopoDS::Wire( aShape->Shape() );
1671 if ( !aWire.IsNull() ) {
1672 TopExp_Explorer anExp( aWire, TopAbs_EDGE );
1673 for ( ; anExp.More(); anExp.Next())
1675 const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
1676 if ( !anEdge.IsNull() ) {
1677 Standard_Real aFirst, aLast;
1678 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
1680 if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
1681 Handle(Geom_BSplineCurve) aBSplineCurve = Handle(Geom_BSplineCurve)::DownCast( aCurve );
1682 if ( !aBSplineCurve.IsNull() ) {
1683 isFound = hasProjectPointOnCurve( theX, theY, aCurve, aParameter );
1685 aPoint = aBSplineCurve->Value( aParameter );
1686 Standard_Integer anI1, anI2;
1687 aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
1689 aPnt1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
1690 aPnt2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
1708 * Returns whether the clicked point belong to the curve or has a very near projection
1709 * \param theX the X coordinate of a point clicked in the OCC viewer
1710 * \param theY the Y coordinate of a point clicked in the OCC viewer
1711 * \param theCurve a geometry curve
1712 * \param theOutPoint a found projected point on the curve
1714 bool CurveCreator_Widget::hasProjectPointOnCurve( const int theX, const int theY,
1715 const Handle(Geom_Curve)& theCurve,
1716 Standard_Real& theParameter )
1718 bool isFound = false;
1719 OCCViewer_ViewPort3d* aViewPort = getViewPort();
1723 Handle(V3d_View) aView = aViewPort->getView();
1724 gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, aView );
1726 GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
1727 Standard_Integer aNbPoint = aProj.NbPoints();
1729 for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
1730 gp_Pnt aNewPoint = aProj.Point( j );
1731 theParameter = aProj.Parameter( j );
1734 CurveCreator_Utils::ConvertPointToClick( aNewPoint, aView, aX, anY );
1736 int aXDelta = abs( aX - theX );
1737 int anYDelta = abs( anY - theY );
1738 isFound = aXDelta < SCENE_PIXEL_TOLERANCE && anYDelta < SCENE_PIXEL_TOLERANCE;
1745 * Returns a section index from the table
1746 * \param theRowId a table row
1748 int CurveCreator_Widget::getSectionId( const int theRowId ) const
1750 return myLocalPointView->item( theRowId, 0 )->data( Qt::UserRole ).toInt();
1754 * Returns a point index from the table
1755 * \param theRowId a table row
1757 int CurveCreator_Widget::getPointId( const int theRowId ) const
1759 return myLocalPointView->item( theRowId, 1 )->data( Qt::UserRole ).toInt();
1763 * Returns whethe the container has the value
1764 * \param theList a container of values
1765 * \param theValue a value
1767 bool CurveCreator_Widget::contains( const CurveCreator_Widget::SectionToPointList& theList,
1768 const CurveCreator_Widget::SectionToPoint& theValue ) const
1770 bool isFound = false;
1772 SectionToPointList::const_iterator anIt = theList.begin(), aLast = theList.end();
1773 for ( ; anIt != aLast && !isFound; anIt++ )
1774 isFound = anIt->first == theValue.first && anIt->second == theValue.second;