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 //=======================================================================
387 // function: getUniqSectionName
388 // purpose: return unique section name
389 //=======================================================================
390 std::string CurveCreator_Widget::getUniqSectionName( CurveCreator_ICurve* theCurve ) const
392 for( int i = 0 ; i < 1000000 ; i++ ){
394 sprintf( aBuffer, "Section_%d", i+1 );
395 std::string aName(aBuffer);
397 for( j = 0 ; j < theCurve->getNbSections() ; j++ ){
398 if( theCurve->getSectionName(j) == aName )
401 if( j == theCurve->getNbSections() )
407 void CurveCreator_Widget::setCurve( CurveCreator_ICurve* theCurve )
410 mySectionView->setCurve(myCurve);
411 onSelectionChanged();
415 void CurveCreator_Widget::onSelectionChanged()
417 QList<ActionId> anEnabledAct;
419 anEnabledAct << NEW_SECTION_ID << MODIFICATION_MODE_ID;
420 if ( removeEnabled() )
421 anEnabledAct << REMOVE_ID;
422 QList<int> aSelSections = mySectionView->getSelectedSections();
423 QList< QPair< int, int > > aSelPoints = mySectionView->getSelectedPoints();
424 CurveCreator_TreeView::SelectionType aSelType = mySectionView->getSelectionType();
426 case CurveCreator_TreeView::ST_NOSEL:{
429 case CurveCreator_TreeView::ST_SECTIONS:{
430 /*if( aSelSections[0] > 0 ){
431 anEnabledAct << UP_ID;
433 if( aSelSections.size() == 1 ){
434 anEnabledAct << ADDITION_MODE_ID << DETECTION_MODE_ID;
436 switch ( getActionMode() ) {
440 QList<int> aSelSection = mySectionView->getSelectedSections();
441 if( aSelSection.size() > 0 ){
442 mySection = aSelSection[0];
443 myPointNum = myCurve->getNbPoints(mySection);
447 case ModificationMode: {
448 anEnabledAct << CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID << SET_SECTIONS_POLYLINE_ID << SET_SECTIONS_SPLINE_ID;
449 int aSectCnt = myCurve->getNbSections();
451 anEnabledAct << CLEAR_ALL_ID;
454 // anEnabledAct << JOIN_ALL_ID;
455 //if( aSelSections.size() > 1 ){
456 // anEnabledAct << JOIN_ID;
460 case DetectionMode: {
467 /*if( aSelSections[ aSelSections.size() - 1 ] < ( myCurve->getNbSections() - 1 ) ){
468 anEnabledAct << DOWN_ID;
472 /*case CurveCreator_TreeView::ST_POINTS_ONE_SECTION:{
473 if( aSelPoints[0].second > 0 ){
474 anEnabledAct << UP_ID;
476 int aLastIndex = aSelPoints.size()-1;
477 int aSect = aSelPoints[0].first;
478 if( aSelPoints[aLastIndex].second < (myCurve->getNbPoints(aSect) - 1)){
479 anEnabledAct << DOWN_ID;
481 if( aSelPoints.size() == 1){
482 anEnabledAct << INSERT_POINT_BEFORE_ID << INSERT_POINT_AFTER_ID;
489 /*int aSelObjsCnt = aSelPoints.size() + aSelSections.size();
490 if( aSelObjsCnt > 0 ){
491 anEnabledAct << REMOVE_ID;
493 if( (myCurve->getNbSections() + myCurve->getNbPoints()) > 0 ){
494 anEnabledAct << REMOVE_ALL_ID;
497 //if( myCurve->getNbSections() > 1 ){
498 // anEnabledAct << JOIN_ALL_ID;
501 QList<ActionId> anIds = myActionMap.keys();
502 for( int i = 0 ; i < anIds.size() ; i++ ){
503 if( myActionMap.contains(anIds[i]) ){
504 if( anEnabledAct.contains(anIds[i]) ){
505 myActionMap[anIds[i]]->setEnabled(true);
508 myActionMap[anIds[i]]->setEnabled(false);
513 emit selectionChanged();
516 void CurveCreator_Widget::onAdditionMode(bool checked)
521 Handle(AIS_InteractiveContext) aContext = getAISContext();
522 if( !myCurve || !aContext )
527 QList<int> aSelSection = mySectionView->getSelectedSections();
528 if( aSelSection.size() > 0 ){
529 mySection = aSelSection[0];
532 QList< QPair<int,int> > aSelPoints = mySectionView->getSelectedPoints();
533 if( aSelPoints.size() > 0 ){
534 mySection = aSelPoints[0].first;
535 myPointNum = aSelPoints[0].second + 1;
538 // emit subOperationStarted( myNewPointEditor );
541 void CurveCreator_Widget::onModificationMode(bool checked)
543 myLocalPointView->setVisible( checked );
546 void CurveCreator_Widget::onDetectionMode(bool checked)
550 void CurveCreator_Widget::onModeChanged(bool checked)
552 ActionMode aMode = NoneMode;
554 QAction* anAction = (QAction*)sender();
555 switch(myActionMap.key(anAction)) {
556 case ADDITION_MODE_ID:
557 aMode = AdditionMode;
558 if (myActionMap[MODIFICATION_MODE_ID]->isChecked())
559 myActionMap[MODIFICATION_MODE_ID]->trigger();
560 else if (myActionMap[DETECTION_MODE_ID]->isChecked())
561 myActionMap[DETECTION_MODE_ID]->trigger();
563 case MODIFICATION_MODE_ID:
564 aMode = ModificationMode;
565 if (myActionMap[ADDITION_MODE_ID]->isChecked())
566 myActionMap[ADDITION_MODE_ID]->trigger();
567 else if (myActionMap[DETECTION_MODE_ID]->isChecked())
568 myActionMap[DETECTION_MODE_ID]->trigger();
570 case DETECTION_MODE_ID:
571 aMode = DetectionMode;
572 if (myActionMap[ADDITION_MODE_ID]->isChecked())
573 myActionMap[ADDITION_MODE_ID]->trigger();
574 else if (myActionMap[MODIFICATION_MODE_ID]->isChecked())
575 myActionMap[MODIFICATION_MODE_ID]->trigger();
579 onSelectionChanged();
580 setLocalPointContext( aMode == ModificationMode, true );
583 void CurveCreator_Widget::onAddNewPoint(const CurveCreator::Coordinates& theCoords)
587 //myCurve->addPoints(theCoords, mySection, myPointNum );
588 //mySectionView->pointsAdded( mySection, myPointNum );
590 QList<int> aSections = mySectionView->getSelectedSections();
591 if( aSections.size() == 0 ){
594 int aSection = aSections[0];
595 myCurve->addPoints(theCoords, aSection); // add to the end of section
596 mySectionView->pointsAdded( aSection, myCurve->getNbPoints( aSection ) );
597 onSelectionChanged();
601 void CurveCreator_Widget::onNewSection()
605 myNewSectionEditor->clear();
606 myNewSectionEditor->setEditMode(false);
607 QString aSectName = QString( getUniqSectionName(myCurve).c_str() );
608 myNewSectionEditor->setSectionParameters(aSectName, true, CurveCreator::Polyline );
609 emit subOperationStarted( myNewSectionEditor );
612 void CurveCreator_Widget::onAddNewSection()
616 myCurve->addSection( myNewSectionEditor->getName().toStdString(), myNewSectionEditor->getSectionType(),
617 myNewSectionEditor->isClosed() );
618 mySectionView->sectionAdded( -1 ); // add a new section to the end of list
619 QString aNewName = QString(getUniqSectionName(myCurve).c_str());
620 myNewSectionEditor->setSectionName(aNewName);
621 onSelectionChanged();
626 void CurveCreator_Widget::onCancelSection()
628 emit subOperationFinished( myNewSectionEditor );
631 QAction* CurveCreator_Widget::createAction( ActionId theId, const QString& theName, const QPixmap& theImage,
632 const QString& theToolTip, const QKeySequence& theShortcut )
634 QAction* anAct = new QAction(theName,this);
635 if( !theImage.isNull() ){
636 anAct->setIcon(theImage);
638 anAct->setShortcut(theShortcut);
639 anAct->setToolTip(theToolTip);
640 myActionMap[theId] = anAct;
644 QAction* CurveCreator_Widget::getAction(ActionId theId)
646 if( myActionMap.contains(theId) )
647 return myActionMap[theId];
651 CurveCreator_Widget::ActionMode CurveCreator_Widget::getActionMode() const
653 ActionMode aMode = NoneMode;
655 if ( myActionMap[ADDITION_MODE_ID]->isChecked() )
656 aMode = AdditionMode;
657 else if ( myActionMap[MODIFICATION_MODE_ID]->isChecked() )
658 aMode = ModificationMode;
659 else if ( myActionMap[DETECTION_MODE_ID]->isChecked() )
660 aMode = DetectionMode;
665 void CurveCreator_Widget::onEditSection( int theSection )
669 mySection = theSection;
670 QString aSectName = QString::fromStdString( myCurve->getSectionName(theSection));
671 bool isClosed = myCurve->isClosed(theSection);
672 CurveCreator::SectionType aType = myCurve->getSectionType(theSection);
673 myNewSectionEditor->setEditMode(true);
674 myNewSectionEditor->setSectionParameters( aSectName, isClosed, aType );
676 emit subOperationStarted( myNewSectionEditor );
679 void CurveCreator_Widget::onModifySection()
683 QString aName = myNewSectionEditor->getName();
684 bool isClosed = myNewSectionEditor->isClosed();
685 CurveCreator::SectionType aSectType = myNewSectionEditor->getSectionType();
686 // myCurve->startOperation();
687 if( myCurve->getSectionName(mySection) != aName.toStdString() )
688 myCurve->setSectionName( mySection , aName.toStdString() );
690 if( myCurve->getSectionType(mySection) != aSectType )
691 myCurve->setSectionType( mySection, aSectType );
693 if( myCurve->isClosed(mySection) != isClosed )
694 myCurve->setClosed( mySection, isClosed );
695 // myCurve->finishOperation();
696 mySectionView->sectionChanged(mySection);
701 /*void CurveCreator_Widget::onEditPoint( int theSection, int thePoint )
703 if( !myNewPointEditor || !myEdit )
705 mySection = theSection;
706 myPointNum = thePoint;
707 QString aSectName = QString::fromStdString( myCurve->getSectionName(theSection));
708 myNewPointEditor->setEditMode(true);
709 myNewPointEditor->setSectionName(aSectName);
710 myNewPointEditor->setDimension( myCurve->getDimension() );
711 CurveCreator::Coordinates aCoords = myCurve->getCoordinates(theSection,thePoint);
712 myNewPointEditor->setCoordinates(aCoords);
713 emit subOperationStarted( myNewPointEditor );
716 void CurveCreator_Widget::onModifyPoint()
720 CurveCreator::Coordinates aCoords = myNewPointEditor->getCoordinates();
721 myEdit->setCoordinates( aCoords, mySection, myPointNum );
722 mySectionView->pointDataChanged( mySection, myPointNum );
727 void CurveCreator_Widget::onJoin()
731 QList<int> aSections = mySectionView->getSelectedSections();
732 if( aSections.size() == 0 ){
735 int aMainSect = aSections[0];
736 int aMainSectSize = myCurve->getNbPoints(aMainSect);
737 // myCurve->startOperation();
738 for( int i = 1 ; i < aSections.size() ; i++ ){
739 int aSectNum = aSections[i] - (i-1);
740 myCurve->join( aMainSect, aSectNum );
741 mySectionView->sectionsRemoved( aSectNum );
743 // myCurve->finishOperation();
744 int aNewSectSize = myCurve->getNbPoints(aMainSect);
745 if( aNewSectSize != aMainSectSize )
746 mySectionView->pointsAdded( aMainSect, aMainSectSize, aNewSectSize-aMainSectSize );
750 void CurveCreator_Widget::onRemove()
755 switch( getActionMode() ) {
759 case ModificationMode:
767 void CurveCreator_Widget::onClearAll()
772 mySectionView->reset();
773 onSelectionChanged();
777 void CurveCreator_Widget::onJoinAll()
782 mySectionView->reset();
783 onSelectionChanged();
787 void CurveCreator_Widget::onUndoSettings()
792 void CurveCreator_Widget::onSetSpline()
796 QList<int> aSelSections = mySectionView->getSelectedSections();
797 // myCurve->startOperation();
798 for( int i = 0 ; i < aSelSections.size() ; i++ ){
799 myCurve->setSectionType(aSelSections[i], CurveCreator::Spline );
800 mySectionView->sectionChanged(aSelSections[i]);
802 // myCurve->finishOperation();
806 void CurveCreator_Widget::onSetPolyline()
810 // myCurve->startOperation();
811 QList<int> aSelSections = mySectionView->getSelectedSections();
812 for( int i = 0 ; i < aSelSections.size() ; i++ ){
813 myCurve->setSectionType( aSelSections[i], CurveCreator::Polyline );
814 mySectionView->sectionChanged( aSelSections[i] );
816 // myCurve->finishOperation();
820 void CurveCreator_Widget::onCloseSections()
824 // myCurve->startOperation();
825 QList<int> aSelSections = mySectionView->getSelectedSections();
826 for( int i = 0 ; i < aSelSections.size() ; i++ ){
827 myCurve->setClosed(aSelSections[i], true);
828 mySectionView->sectionChanged(aSelSections[i]);
830 // myCurve->finishOperation();
834 void CurveCreator_Widget::onUncloseSections()
838 // myCurve->startOperation();
839 QList<int> aSelSections = mySectionView->getSelectedSections();
840 for( int i = 0 ; i < aSelSections.size() ; i++ ){
841 myCurve->setClosed(aSelSections[i], false);
842 mySectionView->sectionChanged(aSelSections[i]);
844 // myCurve->finishOperation();
848 void CurveCreator_Widget::onUndo()
853 CurveCreator_Widget::SectionToPointList aPoints;
854 startCurveModification( aPoints, false );
856 finishCurveModification();
857 mySectionView->reset();
860 void CurveCreator_Widget::onRedo()
864 CurveCreator_Widget::SectionToPointList aPoints;
865 startCurveModification( aPoints, false );
867 finishCurveModification();
868 mySectionView->reset();
871 void CurveCreator_Widget::updateUndoRedo()
875 QAction* anAct = myActionMap[UNDO_ID];
877 if( myCurve->getNbUndo() != 0 ){
878 anAct->setEnabled(true);
881 anAct->setDisabled(true);
884 anAct = myActionMap[REDO_ID];
886 if( myCurve->getNbRedo() != 0 ){
887 anAct->setEnabled(true);
890 anAct->setDisabled(true);
895 void CurveCreator_Widget::onContextMenu( QPoint thePoint )
897 QList<ActionId> aContextActions;
898 aContextActions << CLEAR_ALL_ID << JOIN_ALL_ID << SEPARATOR_ID <<
899 CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID << SET_SECTIONS_POLYLINE_ID <<
900 SET_SECTIONS_SPLINE_ID;
901 QPoint aGlPoint = mySectionView->mapToGlobal(thePoint);
903 QList<ActionId> aResAct;
904 for( int i = 0 ; i < aContextActions.size() ; i++ ){
905 if( aContextActions[i] != SEPARATOR_ID ){
906 if( myActionMap.contains(aContextActions[i]) ){
907 QAction* anAct = myActionMap[aContextActions[i]];
908 if( anAct->isEnabled() ){
909 aResAct << aContextActions[i];
915 aResAct << SEPARATOR_ID;
921 QMenu* aMenu = new QMenu(this);
922 for( int i = 0 ; i < aResAct.size() ; i++ ){
923 if( aResAct[i] == SEPARATOR_ID ){
924 aMenu->addSeparator();
927 QAction* anAct = myActionMap[aResAct[i]];
928 aMenu->insertAction(NULL, anAct);
931 aMenu->exec(aGlPoint);
934 QList<int> CurveCreator_Widget::getSelectedSections()
936 return mySectionView->getSelectedSections();
939 QList< QPair< int, int > > CurveCreator_Widget::getSelectedPoints()
941 return mySectionView->getSelectedPoints();
945 * According to the widget state, performs the remove action
947 void CurveCreator_Widget::removeSelected()
953 * Checks whether there are some selection to be removed
955 bool CurveCreator_Widget::removeEnabled()
957 bool isEnabled = getActionMode() == ModificationMode;
959 QList<int> aSelSections = mySectionView->getSelectedSections();
960 CurveCreator_TreeView::SelectionType aSelType = mySectionView->getSelectionType();
961 isEnabled = aSelType == CurveCreator_TreeView::ST_SECTIONS &&
962 aSelSections.size() == 1;
968 //=================================================================================
969 // function : GeometryGUI::onGetCoordsByClick()
970 // purpose : Manage mouse press events in Additon mode
971 //=================================================================================
972 void CurveCreator_Widget::onGetCoordsByClick( QMouseEvent* pe )
974 if (pe->button() != Qt::LeftButton)
977 if ( pe->modifiers() != Qt::ControlModifier ) {
978 Handle(AIS_InteractiveContext) ic = getAISContext();
985 if ( pe->modifiers() == Qt::ShiftModifier )
986 ic->ShiftSelect(); // Append selection
988 ic->Select(); // New selection
990 /*TopoDS_Shape aShape;
993 if ( ic->MoreSelected() )
994 aShape = ic->SelectedShape();
996 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
997 aPnt = BRep_Tool::Pnt( TopoDS::Vertex( ic->SelectedShape() ) );
1000 OCCViewer_ViewPort3d* vp = getViewPort();
1001 aPnt = CurveCreator_Utils::ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
1003 // set the coordinates into dialog
1004 CurveCreator::Coordinates aCoords;
1005 aCoords.push_back( aPnt.X() );
1006 aCoords.push_back( aPnt.Y() );
1007 if ( myCurve->getDimension() == 3 ) {
1008 aCoords.push_back( aPnt.Z() );
1010 onAddNewPoint(aCoords);
1011 // myNewPointEditor->setCoordinates( aCoords );
1016 * Manage mouse press events
1017 * \param theWindow an owner of the signal
1018 * \param theEvent a mouse event
1020 void CurveCreator_Widget::onMousePress( SUIT_ViewWindow*, QMouseEvent* theEvent )
1022 if ( theEvent->button() != Qt::LeftButton )
1025 switch( getActionMode() ) {
1026 case ModificationMode: {
1027 //store initial cursor position for Drag&Drop
1028 setDragStarted( true, theEvent->pos() );
1031 case AdditionMode: {
1032 onGetCoordsByClick( theEvent );
1041 * Manage mouse release events in Modification mode
1042 * \param theWindow an owner of the signal
1043 * \param theEvent a mouse event
1045 void CurveCreator_Widget::onMouseRelease( SUIT_ViewWindow*, QMouseEvent* theEvent )
1047 if ( getActionMode() != ModificationMode )
1050 if ( myDragStarted ) {
1051 if ( myDragged ) // if the drag of some points has happened, restore the drag selection
1052 setSelectedPonts( myDragPoints );
1053 setDragStarted( false );
1055 else // check whether the segment is clicked an a new point should be added to the segment
1056 insertPointToSelectedSegment( theEvent->pos().x(), theEvent->pos().y() );
1058 // updates the input panel table to show the selected point coordinates
1059 updateLocalPointView();
1063 * Manage mouse move events in Modification mode
1064 * \param theWindow an owner of the signal
1065 * \param theEvent a mouse event
1067 void CurveCreator_Widget::onMouseMove( SUIT_ViewWindow*, QMouseEvent* theEvent )
1069 if ( getActionMode() != ModificationMode || !myDragStarted )
1072 QPoint aPos = theEvent->pos();
1073 if ( (aPos - myDragStartPosition).manhattanLength() < QApplication::startDragDistance() )
1076 moveSelectedPoints( aPos.x(), aPos.y() );
1077 myDragStartPosition = aPos;
1081 * Set zero viewer by the last view closed in
1082 * \param theManager a viewer manager
1084 void CurveCreator_Widget::onLastViewClosed( SUIT_ViewManager* theManager )
1089 void CurveCreator_Widget::onMousePress( QMouseEvent* theEvent )
1091 onMousePress( 0, theEvent );
1094 void CurveCreator_Widget::onMouseRelease( QMouseEvent* theEvent )
1096 onMouseRelease( 0, theEvent );
1099 void CurveCreator_Widget::onMouseMove( QMouseEvent* theEvent )
1101 onMouseMove( 0, theEvent );
1104 void CurveCreator_Widget::onCellChanged( int theRow, int theColumn )
1106 int aCurrSect = getSectionId( theRow );
1107 int aPntIndex = getPointId( theRow );
1109 if ( aPntIndex < 0 )
1112 SectionToPointList aSelPoints;
1113 startCurveModification( aSelPoints );
1115 double aX = myLocalPointView->item( theRow, 2 )->data( Qt::UserRole ).toDouble();
1116 double anY = myLocalPointView->item( theRow, 3 )->data( Qt::UserRole ).toDouble();
1117 std::deque<float> aChangedPos;
1118 aChangedPos.push_back( aX );
1119 aChangedPos.push_back( anY );
1120 myCurve->setPoint( aCurrSect, aPntIndex, aChangedPos );
1122 finishCurveModification( aSelPoints );
1126 * Removes a selected section from the curve. Updates undo/redo status
1128 void CurveCreator_Widget::removeSection()
1130 QList< QPair<int,int> > aSelPoints = mySectionView->getSelectedPoints();
1133 // myCurve->startOperation();
1134 for( int i = 0 ; i < aSelPoints.size() ; i++ ){
1135 if( aCurrSect != aSelPoints[i].first ){
1137 aCurrSect = aSelPoints[i].first;
1139 int aPntIndx = aSelPoints[i].second - aRemoveCnt;
1140 myCurve->removePoint( aCurrSect, aPntIndx );
1141 mySectionView->pointsRemoved( aCurrSect, aPntIndx );
1144 QList<int> aSections = mySectionView->getSelectedSections();
1145 for( int i = 0 ; i < aSections.size() ; i++ ){
1146 int aSectNum = aSections[i] - (i);
1147 myCurve->removeSection( aSectNum );
1148 mySectionView->sectionsRemoved( aSectNum );
1150 // myCurve->finishOperation();
1151 mySectionView->clearSelection();
1156 * Removes a selected points from the curve. Updates undo/redo status
1158 void CurveCreator_Widget::removePoint()
1160 SectionToPointList aPoints;
1161 getSelectedPonts( aPoints );
1162 if ( aPoints.size() == 0 )
1165 SectionToPointList aSelPoints;
1166 startCurveModification( aSelPoints, false );
1168 myCurve->removeSeveralPoints( aPoints );
1169 finishCurveModification( SectionToPointList() );
1172 void CurveCreator_Widget::insertPointToSelectedSegment( const int theX,
1175 Handle(AIS_InteractiveContext) aContext = getAISContext();
1179 gp_Pnt aPoint1, aPoint2;
1180 bool isFoundPoint = false;
1182 for ( aContext->InitSelected(); aContext->MoreSelected() && !isFoundPoint;
1183 aContext->NextSelected() ) {
1184 TopoDS_Shape aTShape = aContext->SelectedShape();
1185 if ( !aTShape.IsNull() && aTShape.ShapeType() == TopAbs_VERTEX )
1188 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
1189 if ( anOwner.IsNull() )
1191 const TopLoc_Location& aLocation = anOwner->Location();
1192 Handle(AIS_InteractiveObject) anAIS = Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
1193 isFoundPoint = pointOnObject( anAIS, theX, theY, aPoint, aPoint1, aPoint2 );
1196 if ( !isFoundPoint )
1199 // insert the point to the model curve
1200 SectionToPointList aSelPoints;
1201 startCurveModification( aSelPoints );
1203 CurveCreator::Coordinates aCoords;
1204 aCoords.push_back( aPoint.X() );
1205 aCoords.push_back( aPoint.Y() );
1207 SectionToPointList aPoints1, aPoints2;
1208 findSectionsToPoints( aPoint1.X(), aPoint1.Y(), aPoints1 );
1209 findSectionsToPoints( aPoint2.X(), aPoint2.Y(), aPoints2 );
1210 SectionToPointList::const_iterator anIt = aPoints1.begin(), aLast = aPoints1.end();
1211 int aSectionId = -1;
1212 // there can be a case when a new point is added into two sections
1213 int aPoint1Id = -1, aPoint2Id = -1;
1214 for ( ; anIt != aLast && aSectionId < 0; anIt++ ) {
1215 int aSectionCur = anIt->first;
1216 SectionToPointList::const_iterator anIt2 = aPoints2.begin(), aLast2 = aPoints2.end();
1217 for ( ; anIt2 != aLast2 && aSectionId < 0; anIt2++ ) {
1218 if ( anIt2->first == aSectionCur ) {
1219 aSectionId = aSectionCur;
1220 aPoint1Id = anIt->second;
1221 aPoint2Id = anIt2->second;
1226 int anInsertPos = -1;
1227 int aLastPoint = myCurve->getNbPoints( aSectionId )-1;
1228 if ( ( aPoint1Id == aLastPoint && aPoint2Id == 0 ) ||
1229 ( aPoint2Id == aLastPoint && aPoint1Id == 0 ) )
1230 anInsertPos = -1; // if the section happens between first and last points
1232 anInsertPos = aPoint1Id < aPoint2Id ? aPoint1Id + 1 : aPoint2Id + 1;
1234 myCurve->addPoints( aCoords, aSectionId, anInsertPos );
1235 mySectionView->pointsAdded( aSectionId, myCurve->getNbPoints( aSectionId ) );
1237 finishCurveModification( aSelPoints );
1242 void CurveCreator_Widget::moveSelectedPoints( const int theXPosition,
1243 const int theYPosition )
1245 OCCViewer_ViewPort3d* aViewPort = getViewPort();
1249 SectionToPointList aPoints;
1250 startCurveModification( aPoints, false );
1252 gp_Pnt aStartPnt = CurveCreator_Utils::ConvertClickToPoint( myDragStartPosition.x(),
1253 myDragStartPosition.y(),
1254 aViewPort->getView() );
1255 gp_Pnt anEndPnt = CurveCreator_Utils::ConvertClickToPoint( theXPosition, theYPosition,
1256 aViewPort->getView() );
1257 double aXDelta = aStartPnt.X() - anEndPnt.X();
1258 double anYDelta = aStartPnt.Y() - anEndPnt.Y();
1260 CurveCreator_ICurve::SectionToPointCoordsList aCoordList;
1261 std::deque<float> aChangedPos;
1262 SectionToPointList::const_iterator anIt = myDragPoints.begin(), aLast = myDragPoints.end();
1263 for ( ; anIt != aLast; anIt++ ) {
1264 int aSectionId = anIt->first;
1265 int aPointId = anIt->second;
1266 aChangedPos = myCurve->getPoint( aSectionId, aPointId );
1267 if ( aChangedPos.size() < 2 )
1269 aChangedPos[0] = aChangedPos[0] - aXDelta;
1270 aChangedPos[1] = aChangedPos[1] - anYDelta;
1272 aCoordList.push_back(
1273 std::make_pair(std::make_pair( aSectionId, aPointId ),
1276 myCurve->setSeveralPoints( aCoordList );
1279 finishCurveModification( myDragPoints );
1282 void CurveCreator_Widget::updateLocalPointView()
1284 Handle(AIS_InteractiveContext) aContext = getAISContext();
1288 bool isBlocked = myLocalPointView->blockSignals(true);
1290 myLocalPointView->setRowCount( 0 );
1291 for ( aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected() ) {
1292 TopoDS_Vertex aVertex;
1293 TopoDS_Shape aShape = aContext->SelectedShape();
1294 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
1295 aVertex = TopoDS::Vertex( aContext->SelectedShape() );
1297 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
1298 if ( !anOwner.IsNull() ) {
1299 Handle(AIS_InteractiveObject) anAIS = Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
1300 if ( !anAIS.IsNull() ) {
1301 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast( anAIS);
1302 if ( !aPoint.IsNull() )
1303 aVertex = TopoDS::Vertex( aPoint->Vertex() );
1305 if ( aVertex.IsNull() ) {
1306 // the following happens if there are no points in the current curve, there is only a shape
1307 /*Handle(StdSelect_BRepOwner) aBrepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1308 if ( aBrepOwner.IsNull() )
1310 if ( aBrepOwner->HasShape() ) {
1311 const TopoDS_Shape& aShape = aBrepOwner->Shape();
1312 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
1314 aVertex = TopoDS::Vertex( aShape );
1319 if ( aVertex.IsNull() )
1321 aPnt = BRep_Tool::Pnt( aVertex );
1322 addLocalPointToTable( aPnt.X(), aPnt.Y() );
1325 myLocalPointView->blockSignals(isBlocked);
1331 void CurveCreator_Widget::setLocalPointContext( const bool theOpen, const bool isUpdateTable )
1333 Handle(AIS_InteractiveContext) ic = getAISContext();
1338 // Open local context if there is no one
1339 if ( !ic->HasOpenedContext() ) {
1340 ic->ClearCurrents( false );
1341 ic->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
1343 AIS_ListOfInteractive aList;
1344 ic->DisplayedObjects( aList );
1346 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
1349 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
1351 Handle(AIS_InteractiveObject) anAIS = it.Value();
1352 if ( !anAIS.IsNull() )
1354 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
1356 ic->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
1357 ic->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_WIRE ) );
1359 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
1361 ic->Load( anAIS, -1/*selection mode*/, false/*allow decomposition*/ );
1362 ic->Activate( anAIS, TopAbs_VERTEX );
1369 if ( ic->HasOpenedContext() )
1370 ic->CloseAllContexts();
1371 if ( isUpdateTable )
1372 updateLocalPointView();
1376 void CurveCreator_Widget::addLocalPointToTable( const double theX, const double theY )
1378 SectionToPointList aPoints;
1379 findSectionsToPoints( theX, theY, aPoints );
1381 SectionToPointList aSkipList;
1382 // table could not contain two equal value rows
1383 int aRowId = myLocalPointView->rowCount();
1384 double aCurrentX, aCurrentY;
1385 int aSectionId, aPointId;
1386 SectionToPoint aPoint;
1387 for ( int i = 0; i < aRowId; i++ ) {
1388 aCurrentX = myLocalPointView->item( i, 2 )->data( Qt::UserRole ).toDouble();
1389 aCurrentY = myLocalPointView->item( i, 3 )->data( Qt::UserRole ).toDouble();
1390 if ( fabs( aCurrentX - theX ) < LOCAL_SELECTION_TOLERANCE &&
1391 fabs( aCurrentY - theY ) < LOCAL_SELECTION_TOLERANCE ) {
1392 aPoint = std::make_pair<int, int>( getSectionId( i ), getPointId( i ) );
1393 if ( !contains( aSkipList, aPoint ) )
1394 aSkipList.push_back( aPoint );
1397 if ( aSkipList.size() == aPoints.size() )
1400 QTableWidgetItem* anItem;
1401 SectionToPointList::const_iterator anIt = aPoints.begin(), aLast = aPoints.end();
1402 for ( ; anIt != aLast; anIt++ ) {
1404 if ( contains( aSkipList, aPoint ) )
1407 myLocalPointView->setRowCount( aRowId+1 );
1408 aSectionId = aPoint.first;
1409 aPointId = aPoint.second;
1411 anItem = new QTableWidgetItem( myCurve->getSectionName( aSectionId ).c_str() );
1412 anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
1413 anItem->setData( Qt::UserRole, aSectionId );
1414 myLocalPointView->setItem( aRowId, 0, anItem );
1416 anItem = new QTableWidgetItem( QString::number( aPointId + 1 ) );
1417 anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
1418 anItem->setData( Qt::UserRole, aPointId );
1419 myLocalPointView->setItem( aRowId, 1, anItem );
1421 anItem = new QTableWidgetItem( QString::number( theX ) );
1422 anItem->setData( Qt::UserRole, theX );
1423 myLocalPointView->setItem( aRowId, 2, anItem );
1425 anItem = new QTableWidgetItem( QString::number( theY ) );
1426 anItem->setData( Qt::UserRole, theY );
1427 myLocalPointView->setItem( aRowId, 3, anItem );
1432 * Set drag operation started. Save the position and a list of dragged points
1433 * \param theState the drag operation state: started/finished
1434 * \param thePoint the start drag position
1436 void CurveCreator_Widget::setDragStarted( const bool theState, const QPoint& thePoint )
1439 getSelectedPonts( myDragPoints );
1440 myDragStarted = myDragPoints.size();
1441 myDragStartPosition = thePoint;
1442 if ( myDragStarted ) {
1443 // change a viewer interaction style in order to avoid a select rectangle build
1444 myDragInteractionStyle = changeInteractionStyle( SUIT_ViewModel::KEY_FREE );
1448 if ( myDragStarted )
1449 changeInteractionStyle( myDragInteractionStyle );
1450 myDragStarted = false;
1451 myDragPoints.clear();
1456 void CurveCreator_Widget::getSelectedPonts( CurveCreator_Widget::SectionToPointList& thePoints )
1459 for ( int i = 0, aNb = myLocalPointView->rowCount(); i < aNb; i++ )
1460 thePoints.push_back( std::make_pair( getSectionId( i ), getPointId( i ) ) );
1463 void CurveCreator_Widget::setSelectedPonts( const CurveCreator_Widget::SectionToPointList& thePoints )
1465 Handle(AIS_InteractiveContext) ic = getAISContext();
1466 if ( !ic || !ic->HasOpenedContext() )
1469 AIS_ListOfInteractive aListToSelect;
1470 AIS_ListOfInteractive aDisplayedList;
1471 ic->DisplayedObjects( aDisplayedList );
1473 SectionToPointList::const_iterator anIt = thePoints.begin(), aLast = thePoints.end();
1474 SectionToPoint aSToPoint;
1475 for( ; anIt != aLast; anIt++ ) {
1478 for ( AIS_ListIteratorOfListOfInteractive it( aDisplayedList ); it.More(); it.Next() )
1480 Handle(AIS_InteractiveObject) anAIS = it.Value();
1481 if ( anAIS.IsNull() )
1483 Handle(AIS_Point) anAISPoint = Handle(AIS_Point)::DownCast( anAIS );
1484 if ( anAISPoint.IsNull() )
1487 TopoDS_Vertex aVertex = TopoDS::Vertex( anAISPoint->Vertex() );
1489 if ( aVertex.IsNull() )
1492 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
1494 SectionToPointList aPoints;
1495 findSectionsToPoints( aPnt.X(), aPnt.Y(), aPoints );
1497 SectionToPointList::const_iterator anIt = aPoints.begin(), aLast = aPoints.end();
1498 SectionToPoint aPoint;
1499 for ( ; anIt != aLast; anIt++ ) {
1501 if ( aPoint.first == aSToPoint.first && aPoint.second == aSToPoint.second )
1502 aListToSelect.Append( anAIS );
1507 ic->ClearSelected( Standard_False );
1508 setObjectsSelected( aListToSelect );
1509 updateLocalPointView();
1513 * Get viewer information before perform the curve modification.
1514 * Take a list of selected cuve points an close local context.
1515 * The context should be closed because the curve presentation is
1516 * redisplayed and if it is not closed, when we close the local context
1517 * later, the presentation shown in the local context is disappeared.
1518 * \param thePoints an output list of curve selected points
1519 * \param theFillPoints a flag whether the selection list should be filled
1521 void CurveCreator_Widget::startCurveModification(
1522 CurveCreator_Widget::SectionToPointList& thePoints,
1523 const bool theFillPoints )
1525 if ( theFillPoints ) {
1527 getSelectedPonts( thePoints );
1529 setLocalPointContext( false );
1533 * Restore the viewer state after the curve modification is done.
1534 * Open local context and select given points inside it.
1535 * \param thePoints a list of curve selected points
1537 void CurveCreator_Widget::finishCurveModification(
1538 const CurveCreator_Widget::SectionToPointList& thePoints )
1540 if ( getActionMode() == ModificationMode )
1541 setLocalPointContext( true );
1542 setSelectedPonts( thePoints );
1547 * Returns a point index in the model curve by the point coordinates in the viewer
1548 * \param theX the X coordinate of the point
1549 * \param theY the Y coordinate of the point
1551 int CurveCreator_Widget::findLocalPointIndex( int theSectionId, float theX, float theY )
1555 CurveCreator::Coordinates aCoords;
1556 for ( int i = 0, aNb = myCurve->getNbPoints( theSectionId ); i < aNb && aPntIndex < 0; i++ ) {
1557 aCoords = myCurve->getPoint( theSectionId, i );
1558 if ( aCoords.size() < 2 )
1560 if ( fabs( aCoords[0] - theX ) < LOCAL_SELECTION_TOLERANCE &&
1561 fabs( aCoords[1] - theY ) < LOCAL_SELECTION_TOLERANCE )
1568 void CurveCreator_Widget::findSectionsToPoints( const double theX, const double theY,
1569 CurveCreator_Widget::SectionToPointList& thePoints )
1574 for ( int i = 0, aNb = myCurve->getNbSections(); i < aNb; i++ ) {
1575 aPointId = findLocalPointIndex( i, theX, theY );
1578 SectionToPoint aPoint = std::make_pair( i, aPointId );
1579 if ( !contains( thePoints, aPoint ) )
1580 thePoints.push_back( aPoint );
1584 void CurveCreator_Widget::convert( const SectionToPointList& thePoints,
1585 QMap<int, QList<int> >& theConvPoints )
1587 theConvPoints.clear();
1589 SectionToPointList::const_iterator anIt = thePoints.begin(), aLast = thePoints.end();
1591 int aSectionId, aPointId;
1592 for ( ; anIt != aLast; anIt++ ) {
1593 aSectionId = anIt->first;
1594 aPointId = anIt->second;
1596 if ( theConvPoints.contains( aSectionId ) )
1597 aPoints = theConvPoints[aSectionId];
1598 if ( aPoints.contains( aPointId ) )
1600 aPoints.append( aPointId );
1601 theConvPoints[aSectionId] = aPoints;
1606 * Checks whether the point belongs to the OCC object
1607 * \param theObject a line or shape with a bspline inside
1608 * \param theX the X coordinate in the view.
1609 * \param theY the Y coordinate in the view.
1610 * \param thePoint the output point to be append to the model curve
1611 * \param thePoint1 the output point to bound the line where a new point should be inserted
1612 * \param thePoint2 the output point to bound the line where a new point should be inserted
1614 bool CurveCreator_Widget::pointOnObject( Handle(AIS_InteractiveObject) theObject,
1615 const int theX, const int theY,
1617 gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
1619 bool isFound = false;
1621 Handle(AIS_InteractiveContext) aContext = getAISContext();
1622 if ( theObject.IsNull() || !aContext )
1626 Standard_Real aParameter;
1627 gp_Pnt aPnt1, aPnt2;
1628 Handle(AIS_Line) aLine = Handle(AIS_Line)::DownCast( theObject );
1629 if ( !aLine.IsNull() ) {
1630 const Handle(Geom_Line) aGLine = aLine->Line();
1631 isFound = hasProjectPointOnCurve( theX, theY, aGLine, aParameter );
1633 aPoint = aGLine->Value( aParameter );
1635 Handle(Geom_Point) aPStart;
1636 Handle(Geom_Point) aPEnd;
1637 aLine->Points( aPStart, aPEnd );
1638 aPnt1 = aPStart->Pnt();
1639 aPnt2 = aPEnd->Pnt();
1641 // in case of Geom line a projection is performed to the infinite line,
1642 // so it is necessary to bound it by the line size
1644 aLineBox.Set( aPnt1, gp_Vec( aPnt1, aPnt2 ) );
1645 isFound = !aLineBox.IsOut( aPoint );
1649 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
1650 if ( !aShape.IsNull() ) {
1651 const TopoDS_Wire& aWire = TopoDS::Wire( aShape->Shape() );
1652 if ( !aWire.IsNull() ) {
1653 TopExp_Explorer anExp( aWire, TopAbs_EDGE );
1654 for ( ; anExp.More(); anExp.Next())
1656 const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
1657 if ( !anEdge.IsNull() ) {
1658 Standard_Real aFirst, aLast;
1659 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
1661 if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
1662 Handle(Geom_BSplineCurve) aBSplineCurve = Handle(Geom_BSplineCurve)::DownCast( aCurve );
1663 if ( !aBSplineCurve.IsNull() ) {
1664 isFound = hasProjectPointOnCurve( theX, theY, aCurve, aParameter );
1666 aPoint = aBSplineCurve->Value( aParameter );
1667 Standard_Integer anI1, anI2;
1668 aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
1670 aPnt1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
1671 aPnt2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
1689 * Returns whether the clicked point belong to the curve or has a very near projection
1690 * \param theX the X coordinate of a point clicked in the OCC viewer
1691 * \param theY the Y coordinate of a point clicked in the OCC viewer
1692 * \param theCurve a geometry curve
1693 * \param theOutPoint a found projected point on the curve
1695 bool CurveCreator_Widget::hasProjectPointOnCurve( const int theX, const int theY,
1696 const Handle(Geom_Curve)& theCurve,
1697 Standard_Real& theParameter )
1699 bool isFound = false;
1700 OCCViewer_ViewPort3d* aViewPort = getViewPort();
1704 Handle(V3d_View) aView = aViewPort->getView();
1705 gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, aView );
1707 GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
1708 Standard_Integer aNbPoint = aProj.NbPoints();
1710 for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
1711 gp_Pnt aNewPoint = aProj.Point( j );
1712 theParameter = aProj.Parameter( j );
1715 CurveCreator_Utils::ConvertPointToClick( aNewPoint, aView, aX, anY );
1717 int aXDelta = abs( aX - theX );
1718 int anYDelta = abs( anY - theY );
1719 isFound = aXDelta < SCENE_PIXEL_TOLERANCE && anYDelta < SCENE_PIXEL_TOLERANCE;
1726 * Returns a section index from the table
1727 * \param theRowId a table row
1729 int CurveCreator_Widget::getSectionId( const int theRowId ) const
1731 return myLocalPointView->item( theRowId, 0 )->data( Qt::UserRole ).toInt();
1735 * Returns a point index from the table
1736 * \param theRowId a table row
1738 int CurveCreator_Widget::getPointId( const int theRowId ) const
1740 return myLocalPointView->item( theRowId, 1 )->data( Qt::UserRole ).toInt();
1744 * Returns whethe the container has the value
1745 * \param theList a container of values
1746 * \param theValue a value
1748 bool CurveCreator_Widget::contains( const CurveCreator_Widget::SectionToPointList& theList,
1749 const CurveCreator_Widget::SectionToPoint& theValue ) const
1751 bool isFound = false;
1753 SectionToPointList::const_iterator anIt = theList.begin(), aLast = theList.end();
1754 for ( ; anIt != aLast && !isFound; anIt++ )
1755 isFound = anIt->first == theValue.first && anIt->second == theValue.second;