1 // Copyright (C) 2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "CurveCreator_Widget.h"
21 #include "CurveCreator_TreeView.h"
22 #include "CurveCreator_ICurve.hxx"
23 //#include "CurveCreator_CurveEditor.hxx"
24 #include "CurveCreator.hxx"
25 //#include "CurveCreator_NewPointDlg.h"
26 #include "CurveCreator_NewSectionDlg.h"
27 #include "CurveCreator_Utils.h"
29 #include <SUIT_Session.h>
30 #include <SUIT_Desktop.h>
31 #include <SUIT_ResourceMgr.h>
32 #include <SUIT_ViewManager.h>
34 #include <OCCViewer_ViewWindow.h>
35 #include <OCCViewer_ViewManager.h>
36 #include <OCCViewer_ViewPort3d.h>
38 #include <BRep_Tool.hxx>
40 #include <TopoDS_Vertex.hxx>
41 #include <TopoDS_Wire.hxx>
42 #include <TopoDS_Edge.hxx>
45 #include <TopExp_Explorer.hxx>
47 #include <GeomAPI_ProjectPointOnCurve.hxx>
49 #include <AIS_ListOfInteractive.hxx>
50 #include <AIS_ListIteratorOfListOfInteractive.hxx>
51 #include <AIS_Shape.hxx>
52 #include <AIS_Point.hxx>
53 #include <AIS_Line.hxx>
54 #include <Geom_Point.hxx>
55 #include <Geom_BSplineCurve.hxx>
56 #include <Geom_Line.hxx>
57 #include <StdSelect_BRepOwner.hxx>
59 #include <QHBoxLayout>
60 #include <QVBoxLayout>
64 #include <QToolButton>
68 #include <QMouseEvent>
69 #include <QApplication>
70 #include <QTableWidget>
72 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
73 const int SECTION_NAME_COLUMN_WIDTH = 75;
74 const int POINT_INDEX_COLUMN_WIDTH = 40;
76 const int SCENE_PIXEL_TOLERANCE = 10;
78 CurveCreator_Widget::CurveCreator_Widget(QWidget* parent,
79 CurveCreator_ICurve *theCurve,
81 : QWidget(parent), myNewSectionEditor(NULL), myCurve(theCurve), mySection(0),
82 myDragStarted( false ), myDragInteractionStyle( SUIT_ViewModel::STANDARD ),
85 myNewSectionEditor = new CurveCreator_NewSectionDlg( this );
86 myNewSectionEditor->hide();
87 connect( myNewSectionEditor, SIGNAL(addSection()), this, SLOT(onAddNewSection()) );
88 connect( myNewSectionEditor, SIGNAL(modifySection()), this, SLOT(onModifySection()) );
89 connect( myNewSectionEditor, SIGNAL(cancelSection()), this, SLOT(onCancelSection()) );
91 QGroupBox* aSectionGroup = new QGroupBox(tr("Sections"),this);
93 mySectionView = new CurveCreator_TreeView(myCurve, aSectionGroup);
94 connect( mySectionView, SIGNAL(selectionChanged()), this, SLOT( onSelectionChanged() ) );
95 connect( mySectionView, SIGNAL(sectionEntered(int)), this, SLOT(onEditSection(int)) );
96 connect( mySectionView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onContextMenu(QPoint)) );
98 myLocalPointView = new QTableWidget();
99 myLocalPointView->setVisible( false );
100 myLocalPointView->setColumnCount( 4 );
101 myLocalPointView->setColumnWidth( 0, SECTION_NAME_COLUMN_WIDTH );
102 myLocalPointView->setColumnWidth( 1, POINT_INDEX_COLUMN_WIDTH );
104 //aLabels << tr( "SECTION_LABEL" ) << tr( "IDENTIFIER_LABEL" ) << tr( "X_POSITION_LBL" ) << tr( "Y_POSITION_LBL" );
105 aLabels << tr( "Section" ) << "Index" << tr( "X" ) << tr( "Y" );
106 myLocalPointView->setHorizontalHeaderLabels( aLabels );
107 connect( myLocalPointView, SIGNAL( cellChanged( int, int ) ),
108 this, SLOT( onCellChanged( int, int ) ) );
110 QToolBar* aTB = new QToolBar(tr("TOOL_BAR_TLT"), aSectionGroup);
111 // QToolButton* anUndoBtn = new QToolButton(aTB);
113 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
114 QPixmap anUndoPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_UNDO")));
115 QPixmap aRedoPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_REDO")));
116 QPixmap aNewSectionPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_NEW_SECTION")));
117 QPixmap aNewPointPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_NEW_POINT")));
118 QPixmap anEditPointsPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_EDIT_POINTS")));
119 QPixmap aDetectPointsPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_EDIT_POINTS")));
120 QPixmap aPolylinePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_POLYLINE")));
121 QPixmap aSplinePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_SPLINE")));
122 QPixmap aRemovePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_DELETE")));
123 QPixmap aJoinPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_JOIN")));
124 QPixmap aStepUpPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_ARROW_UP")));
125 QPixmap aStepDownPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_ARROW_DOWN")));
127 QAction* anAct = createAction( UNDO_ID, tr("UNDO"), anUndoPixmap, tr("UNDO_TLT"),
128 QKeySequence(Qt::ControlModifier|Qt::Key_Z) );
129 connect(anAct, SIGNAL(triggered()), this, SLOT(onUndo()) );
130 aTB->addAction(anAct);
132 anAct = createAction( REDO_ID, tr("REDO"), aRedoPixmap, tr("REDO_TLT"),
133 QKeySequence(Qt::ControlModifier|Qt::Key_Y) );
134 connect(anAct, SIGNAL(triggered()), this, SLOT(onRedo()) );
135 aTB->addAction(anAct);
139 anAct = createAction( NEW_SECTION_ID, tr("NEW_SECTION"), aNewSectionPixmap, tr("NEW_SECTION_TLT"),
140 QKeySequence(Qt::ControlModifier|Qt::Key_N) );
141 connect(anAct, SIGNAL(triggered()), this, SLOT(onNewSection()) );
142 aTB->addAction(anAct);
145 anAct = createAction( ADDITION_MODE_ID, tr("ADDITION_MODE"), aNewPointPixmap, tr("ADDITION_MODE_TLT"),
147 anAct->setCheckable(true);
148 connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onAdditionMode(bool)) );
149 connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
150 aTB->addAction(anAct);
152 anAct = createAction( MODIFICATION_MODE_ID, tr("MODIFICATION_MODE"), anEditPointsPixmap, tr("MODIFICATION_MODE_TLT"),
154 anAct->setCheckable(true);
155 connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onModificationMode(bool)) );
156 connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
157 aTB->addAction(anAct);
159 anAct = createAction( DETECTION_MODE_ID, tr("DETECTION_MODE"), aDetectPointsPixmap, tr("DETECTION_MODE_TLT"),
161 anAct->setCheckable(true);
162 connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onDetectionMode(bool)) );
163 connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
164 aTB->addAction(anAct);
166 anAct = createAction( CLOSE_SECTIONS_ID, tr("CLOSE_SECTIONS"), QPixmap(), tr("CLOSE_SECTIONS_TLT"),
167 QKeySequence(Qt::ControlModifier|Qt::Key_W) );
168 connect(anAct, SIGNAL(triggered()), this, SLOT(onCloseSections()) );
170 anAct = createAction( UNCLOSE_SECTIONS_ID, tr("UNCLOSE_SECTIONS"), QPixmap(),
171 tr("UNCLOSE_SECTIONS_TLT"), QKeySequence(Qt::ControlModifier|Qt::Key_S) );
172 connect(anAct, SIGNAL(triggered()), this, SLOT(onUncloseSections()) );
174 anAct = createAction( SET_SECTIONS_POLYLINE_ID, tr("SET_SECTIONS_POLYLINE"),
175 aPolylinePixmap, tr("SET_POLYLINE_TLT"),
176 QKeySequence(Qt::ControlModifier|Qt::Key_E) );
177 connect(anAct, SIGNAL(triggered()), this, SLOT(onSetPolyline()) );
179 anAct = createAction( SET_SECTIONS_SPLINE_ID, tr("SET_SECTIONS_SPLINE"), aSplinePixmap,
180 tr("SET_SPLINE_TLT"), QKeySequence(Qt::ControlModifier|Qt::Key_R) );
181 connect(anAct, SIGNAL(triggered()), this, SLOT(onSetSpline()) );
183 anAct = createAction( REMOVE_ID, tr("REMOVE"), aRemovePixmap, tr("REMOVE_TLT"),
184 QKeySequence(Qt::ControlModifier|Qt::Key_Delete ) );
185 connect(anAct, SIGNAL(triggered()), this, SLOT(onRemove()) );
186 aTB->addAction(anAct);
189 anAct = createAction( JOIN_ID, tr("JOIN"), aJoinPixmap, tr("JOIN_TLT"),
190 QKeySequence(Qt::ControlModifier|Qt::Key_Plus ) );
191 connect( anAct, SIGNAL(triggered()), this, SLOT(onJoin()) );
192 aTB->addAction(anAct);
195 anAct = createAction( CLEAR_ALL_ID, tr("CLEAR_ALL"), QPixmap(), tr("CLEAR_ALL_TLT"),
196 QKeySequence(Qt::ControlModifier | Qt::ShiftModifier | Qt::Key_Delete ) );
197 connect( anAct, SIGNAL(triggered()), this, SLOT( onClearAll()) );
199 anAct = createAction( JOIN_ALL_ID, tr("JOIN_ALL"), QPixmap(), tr("JOIN_ALL_TLT"),
200 QKeySequence(Qt::ControlModifier | Qt::ShiftModifier | Qt::Key_Plus ) );
201 connect( anAct, SIGNAL(triggered()), this, SLOT(onJoinAll()) );
203 QVBoxLayout* aSectLayout = new QVBoxLayout();
204 aSectLayout->setMargin( 5 );
205 aSectLayout->setSpacing( 5 );
206 aSectLayout->addWidget(aTB);
207 aSectLayout->addWidget(mySectionView);
208 aSectLayout->addWidget( myLocalPointView );
209 aSectionGroup->setLayout(aSectLayout);
210 QVBoxLayout* aLay = new QVBoxLayout();
211 aLay->setMargin( 0 );
212 aLay->setSpacing( 5 );
213 // aLay->addLayout(aNameLayout);
214 aLay->addWidget(aSectionGroup);
216 onSelectionChanged();
222 void CurveCreator_Widget::setOCCViewer( OCCViewer_Viewer* theViewer )
224 if ( myOCCViewer == theViewer )
228 OCCViewer_ViewManager* aViewManager = dynamic_cast<OCCViewer_ViewManager*>
229 ( myOCCViewer->getViewManager() );
230 disconnect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
231 this, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ) );
232 disconnect( aViewManager, SIGNAL( mouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ),
233 this, SLOT( onMouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ) );
234 disconnect( aViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
235 this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
236 // all local contexts should be closed if the viewer is not more used
237 setLocalPointContext( false, true );
240 myOCCViewer = theViewer;
242 OCCViewer_ViewManager* aViewManager = dynamic_cast<OCCViewer_ViewManager*>
243 ( myOCCViewer->getViewManager() );
244 connect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
245 this, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ) );
246 connect( aViewManager, SIGNAL( mouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ),
247 this, SLOT( onMouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ) );
248 connect( aViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
249 this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
254 * Returns current OCC viewer
256 OCCViewer_Viewer* CurveCreator_Widget::getOCCViewer()
261 //=======================================================================
262 // function: getUniqSectionName
263 // purpose: return unique section name
264 //=======================================================================
265 std::string CurveCreator_Widget::getUniqSectionName( CurveCreator_ICurve* theCurve ) const
267 for( int i = 0 ; i < 1000000 ; i++ ){
269 sprintf( aBuffer, "Section_%d", i+1 );
270 std::string aName(aBuffer);
272 for( j = 0 ; j < theCurve->getNbSections() ; j++ ){
273 if( theCurve->getSectionName(j) == aName )
276 if( j == theCurve->getNbSections() )
282 void CurveCreator_Widget::setCurve( CurveCreator_ICurve* theCurve )
285 mySectionView->setCurve(myCurve);
286 onSelectionChanged();
290 void CurveCreator_Widget::onSelectionChanged()
292 QList<ActionId> anEnabledAct;
294 anEnabledAct << NEW_SECTION_ID << MODIFICATION_MODE_ID;
295 if ( getActionMode() == ModificationMode )
296 anEnabledAct << REMOVE_ID;
297 QList<int> aSelSections = mySectionView->getSelectedSections();
298 QList< QPair< int, int > > aSelPoints = mySectionView->getSelectedPoints();
299 CurveCreator_TreeView::SelectionType aSelType = mySectionView->getSelectionType();
301 case CurveCreator_TreeView::ST_NOSEL:{
304 case CurveCreator_TreeView::ST_SECTIONS:{
305 /*if( aSelSections[0] > 0 ){
306 anEnabledAct << UP_ID;
308 if( aSelSections.size() == 1 ){
309 anEnabledAct << ADDITION_MODE_ID << DETECTION_MODE_ID;
310 anEnabledAct << REMOVE_ID;
312 switch ( getActionMode() ) {
316 QList<int> aSelSection = mySectionView->getSelectedSections();
317 if( aSelSection.size() > 0 ){
318 mySection = aSelSection[0];
319 myPointNum = myCurve->getNbPoints(mySection);
323 case ModificationMode: {
324 anEnabledAct << CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID << SET_SECTIONS_POLYLINE_ID << SET_SECTIONS_SPLINE_ID;
325 int aSectCnt = myCurve->getNbSections();
327 anEnabledAct << CLEAR_ALL_ID;
329 anEnabledAct << JOIN_ALL_ID;
330 if( aSelSections.size() > 1 ){
331 anEnabledAct << JOIN_ID;
335 case DetectionMode: {
342 /*if( aSelSections[ aSelSections.size() - 1 ] < ( myCurve->getNbSections() - 1 ) ){
343 anEnabledAct << DOWN_ID;
347 /*case CurveCreator_TreeView::ST_POINTS_ONE_SECTION:{
348 if( aSelPoints[0].second > 0 ){
349 anEnabledAct << UP_ID;
351 int aLastIndex = aSelPoints.size()-1;
352 int aSect = aSelPoints[0].first;
353 if( aSelPoints[aLastIndex].second < (myCurve->getNbPoints(aSect) - 1)){
354 anEnabledAct << DOWN_ID;
356 if( aSelPoints.size() == 1){
357 anEnabledAct << INSERT_POINT_BEFORE_ID << INSERT_POINT_AFTER_ID;
364 /*int aSelObjsCnt = aSelPoints.size() + aSelSections.size();
365 if( aSelObjsCnt > 0 ){
366 anEnabledAct << REMOVE_ID;
368 if( (myCurve->getNbSections() + myCurve->getNbPoints()) > 0 ){
369 anEnabledAct << REMOVE_ALL_ID;
371 if( myCurve->getNbSections() > 1 ){
372 anEnabledAct << JOIN_ALL_ID;
375 QList<ActionId> anIds = myActionMap.keys();
376 for( int i = 0 ; i < anIds.size() ; i++ ){
377 if( myActionMap.contains(anIds[i]) ){
378 if( anEnabledAct.contains(anIds[i]) ){
379 myActionMap[anIds[i]]->setEnabled(true);
382 myActionMap[anIds[i]]->setEnabled(false);
387 emit selectionChanged();
390 void CurveCreator_Widget::onAdditionMode(bool checked)
392 OCCViewer_Viewer* aViewer = getOCCViewer();
393 if( !myCurve || !aViewer )
396 Handle(AIS_InteractiveContext) aContext = aViewer->getAISContext();
397 OCCViewer_ViewManager* aViewManager = dynamic_cast<OCCViewer_ViewManager*>
398 (aViewer->getViewManager());
399 // if ( aViewManager->getType() == OCCViewer_Viewer::Type() ) {
402 /* myGuiState = aViewWindow->saveState();
403 aViewer->enableMultiselection(false);
404 aViewer->enableSelection(false);*/
406 /* aViewer->enableMultiselection(true);
407 aViewer->enableSelection(true);
408 aViewWindow->restoreState( myGuiState );*/
415 QList<int> aSelSection = mySectionView->getSelectedSections();
416 if( aSelSection.size() > 0 ){
417 mySection = aSelSection[0];
420 QList< QPair<int,int> > aSelPoints = mySectionView->getSelectedPoints();
421 if( aSelPoints.size() > 0 ){
422 mySection = aSelPoints[0].first;
423 myPointNum = aSelPoints[0].second + 1;
426 // emit subOperationStarted( myNewPointEditor );
429 void CurveCreator_Widget::onModificationMode(bool checked)
431 myLocalPointView->setVisible( checked );
434 void CurveCreator_Widget::onDetectionMode(bool checked)
438 void CurveCreator_Widget::onModeChanged(bool checked)
440 ActionMode aMode = NoneMode;
442 QAction* anAction = (QAction*)sender();
443 switch(myActionMap.key(anAction)) {
444 case ADDITION_MODE_ID:
445 aMode = AdditionMode;
446 if (myActionMap[MODIFICATION_MODE_ID]->isChecked())
447 myActionMap[MODIFICATION_MODE_ID]->trigger();
448 else if (myActionMap[DETECTION_MODE_ID]->isChecked())
449 myActionMap[DETECTION_MODE_ID]->trigger();
451 case MODIFICATION_MODE_ID:
452 aMode = ModificationMode;
453 if (myActionMap[ADDITION_MODE_ID]->isChecked())
454 myActionMap[ADDITION_MODE_ID]->trigger();
455 else if (myActionMap[DETECTION_MODE_ID]->isChecked())
456 myActionMap[DETECTION_MODE_ID]->trigger();
458 case DETECTION_MODE_ID:
459 aMode = DetectionMode;
460 if (myActionMap[ADDITION_MODE_ID]->isChecked())
461 myActionMap[ADDITION_MODE_ID]->trigger();
462 else if (myActionMap[MODIFICATION_MODE_ID]->isChecked())
463 myActionMap[MODIFICATION_MODE_ID]->trigger();
467 onSelectionChanged();
468 setLocalPointContext( aMode == ModificationMode, true );
471 void CurveCreator_Widget::onAddNewPoint(const CurveCreator::Coordinates& theCoords)
475 //myCurve->addPoints(theCoords, mySection, myPointNum );
476 //mySectionView->pointsAdded( mySection, myPointNum );
478 QList<int> aSections = mySectionView->getSelectedSections();
479 if( aSections.size() == 0 ){
482 int aSection = aSections[0];
483 myCurve->addPoints(theCoords, aSection); // add to the end of section
484 mySectionView->pointsAdded( aSection, myCurve->getNbPoints( aSection ) );
485 onSelectionChanged();
489 void CurveCreator_Widget::onNewSection()
493 myNewSectionEditor->clear();
494 myNewSectionEditor->setEditMode(false);
495 QString aSectName = QString( getUniqSectionName(myCurve).c_str() );
496 myNewSectionEditor->setSectionParameters(aSectName, true, CurveCreator::Polyline );
497 emit subOperationStarted( myNewSectionEditor );
500 void CurveCreator_Widget::onAddNewSection()
504 myCurve->addSection( myNewSectionEditor->getName().toStdString(), myNewSectionEditor->getSectionType(),
505 myNewSectionEditor->isClosed() );
506 mySectionView->sectionAdded( -1 ); // add a new section to the end of list
507 QString aNewName = QString(getUniqSectionName(myCurve).c_str());
508 myNewSectionEditor->setSectionName(aNewName);
509 onSelectionChanged();
514 void CurveCreator_Widget::onCancelSection()
516 emit subOperationFinished( myNewSectionEditor );
519 QAction* CurveCreator_Widget::createAction( ActionId theId, const QString& theName, const QPixmap& theImage,
520 const QString& theToolTip, const QKeySequence& theShortcut )
522 QAction* anAct = new QAction(theName,this);
523 if( !theImage.isNull() ){
524 anAct->setIcon(theImage);
526 anAct->setShortcut(theShortcut);
527 anAct->setToolTip(theToolTip);
528 myActionMap[theId] = anAct;
532 QAction* CurveCreator_Widget::getAction(ActionId theId)
534 if( myActionMap.contains(theId) )
535 return myActionMap[theId];
539 CurveCreator_Widget::ActionMode CurveCreator_Widget::getActionMode() const
541 ActionMode aMode = NoneMode;
543 if ( myActionMap[ADDITION_MODE_ID]->isChecked() )
544 aMode = AdditionMode;
545 else if ( myActionMap[MODIFICATION_MODE_ID]->isChecked() )
546 aMode = ModificationMode;
547 else if ( myActionMap[DETECTION_MODE_ID]->isChecked() )
548 aMode = DetectionMode;
553 void CurveCreator_Widget::onEditSection( int theSection )
557 mySection = theSection;
558 QString aSectName = QString::fromStdString( myCurve->getSectionName(theSection));
559 bool isClosed = myCurve->isClosed(theSection);
560 CurveCreator::SectionType aType = myCurve->getSectionType(theSection);
561 myNewSectionEditor->setEditMode(true);
562 myNewSectionEditor->setSectionParameters( aSectName, isClosed, aType );
564 emit subOperationStarted( myNewSectionEditor );
567 void CurveCreator_Widget::onModifySection()
571 QString aName = myNewSectionEditor->getName();
572 bool isClosed = myNewSectionEditor->isClosed();
573 CurveCreator::SectionType aSectType = myNewSectionEditor->getSectionType();
574 // myCurve->startOperation();
575 myCurve->setClosed( isClosed, mySection );
576 myCurve->setSectionName( mySection , aName.toStdString() );
577 myCurve->setSectionType( mySection, aSectType );
578 // myCurve->finishOperation();
579 mySectionView->sectionChanged(mySection);
584 /*void CurveCreator_Widget::onEditPoint( int theSection, int thePoint )
586 if( !myNewPointEditor || !myEdit )
588 mySection = theSection;
589 myPointNum = thePoint;
590 QString aSectName = QString::fromStdString( myCurve->getSectionName(theSection));
591 myNewPointEditor->setEditMode(true);
592 myNewPointEditor->setSectionName(aSectName);
593 myNewPointEditor->setDimension( myCurve->getDimension() );
594 CurveCreator::Coordinates aCoords = myCurve->getCoordinates(theSection,thePoint);
595 myNewPointEditor->setCoordinates(aCoords);
596 emit subOperationStarted( myNewPointEditor );
599 void CurveCreator_Widget::onModifyPoint()
603 CurveCreator::Coordinates aCoords = myNewPointEditor->getCoordinates();
604 myEdit->setCoordinates( aCoords, mySection, myPointNum );
605 mySectionView->pointDataChanged( mySection, myPointNum );
610 void CurveCreator_Widget::onJoin()
614 QList<int> aSections = mySectionView->getSelectedSections();
615 if( aSections.size() == 0 ){
618 int aMainSect = aSections[0];
619 int aMainSectSize = myCurve->getNbPoints(aMainSect);
620 // myCurve->startOperation();
621 for( int i = 1 ; i < aSections.size() ; i++ ){
622 int aSectNum = aSections[i] - (i-1);
623 myCurve->join( aMainSect, aSectNum );
624 mySectionView->sectionsRemoved( aSectNum );
626 // myCurve->finishOperation();
627 int aNewSectSize = myCurve->getNbPoints(aMainSect);
628 if( aNewSectSize != aMainSectSize )
629 mySectionView->pointsAdded( aMainSect, aMainSectSize, aNewSectSize-aMainSectSize );
633 void CurveCreator_Widget::onRemove()
638 switch( getActionMode() ) {
642 case ModificationMode:
650 void CurveCreator_Widget::onClearAll()
655 mySectionView->reset();
656 onSelectionChanged();
660 void CurveCreator_Widget::onJoinAll()
665 mySectionView->reset();
666 onSelectionChanged();
670 void CurveCreator_Widget::onUndoSettings()
675 void CurveCreator_Widget::onSetSpline()
679 QList<int> aSelSections = mySectionView->getSelectedSections();
680 // myCurve->startOperation();
681 for( int i = 0 ; i < aSelSections.size() ; i++ ){
682 myCurve->setSectionType(aSelSections[i], CurveCreator::Spline );
683 mySectionView->sectionChanged(aSelSections[i]);
685 // myCurve->finishOperation();
689 void CurveCreator_Widget::onSetPolyline()
693 // myCurve->startOperation();
694 QList<int> aSelSections = mySectionView->getSelectedSections();
695 for( int i = 0 ; i < aSelSections.size() ; i++ ){
696 myCurve->setSectionType( aSelSections[i], CurveCreator::Polyline );
697 mySectionView->sectionChanged( aSelSections[i] );
699 // myCurve->finishOperation();
703 void CurveCreator_Widget::onCloseSections()
707 // myCurve->startOperation();
708 QList<int> aSelSections = mySectionView->getSelectedSections();
709 for( int i = 0 ; i < aSelSections.size() ; i++ ){
710 myCurve->setClosed(true, aSelSections[i]);
711 mySectionView->sectionChanged(aSelSections[i]);
713 // myCurve->finishOperation();
717 void CurveCreator_Widget::onUncloseSections()
721 // myCurve->startOperation();
722 QList<int> aSelSections = mySectionView->getSelectedSections();
723 for( int i = 0 ; i < aSelSections.size() ; i++ ){
724 myCurve->setClosed(false, aSelSections[i]);
725 mySectionView->sectionChanged(aSelSections[i]);
727 // myCurve->finishOperation();
731 void CurveCreator_Widget::onUndo()
736 mySectionView->reset();
740 void CurveCreator_Widget::onRedo()
745 mySectionView->reset();
749 void CurveCreator_Widget::updateUndoRedo()
753 QAction* anAct = myActionMap[UNDO_ID];
755 if( myCurve->getNbUndo() != 0 ){
756 anAct->setEnabled(true);
759 anAct->setDisabled(true);
762 anAct = myActionMap[REDO_ID];
764 if( myCurve->getNbRedo() != 0 ){
765 anAct->setEnabled(true);
768 anAct->setDisabled(true);
773 void CurveCreator_Widget::onContextMenu( QPoint thePoint )
775 QList<ActionId> aContextActions;
776 aContextActions << CLEAR_ALL_ID << JOIN_ALL_ID << SEPARATOR_ID <<
777 CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID << SET_SECTIONS_POLYLINE_ID <<
778 SET_SECTIONS_SPLINE_ID;
779 QPoint aGlPoint = mySectionView->mapToGlobal(thePoint);
781 QList<ActionId> aResAct;
782 for( int i = 0 ; i < aContextActions.size() ; i++ ){
783 if( aContextActions[i] != SEPARATOR_ID ){
784 if( myActionMap.contains(aContextActions[i]) ){
785 QAction* anAct = myActionMap[aContextActions[i]];
786 if( anAct->isEnabled() ){
787 aResAct << aContextActions[i];
793 aResAct << SEPARATOR_ID;
799 QMenu* aMenu = new QMenu(this);
800 for( int i = 0 ; i < aResAct.size() ; i++ ){
801 if( aResAct[i] == SEPARATOR_ID ){
802 aMenu->addSeparator();
805 QAction* anAct = myActionMap[aResAct[i]];
806 aMenu->insertAction(NULL, anAct);
809 aMenu->exec(aGlPoint);
812 QList<int> CurveCreator_Widget::getSelectedSections()
814 return mySectionView->getSelectedSections();
817 QList< QPair< int, int > > CurveCreator_Widget::getSelectedPoints()
819 return mySectionView->getSelectedPoints();
822 //=================================================================================
823 // function : GeometryGUI::onGetCoordsByClick()
824 // purpose : Manage mouse press events in Additon mode
825 //=================================================================================
826 void CurveCreator_Widget::onGetCoordsByClick( QMouseEvent* pe )
828 if (pe->button() != Qt::LeftButton)
831 if ( pe->modifiers() != Qt::ControlModifier ) {
832 OCCViewer_Viewer* aViewer = getOCCViewer();
835 Handle(AIS_InteractiveContext) ic = aViewer->getAISContext();
840 if ( pe->modifiers() == Qt::ShiftModifier )
841 ic->ShiftSelect(); // Append selection
843 ic->Select(); // New selection
845 /*TopoDS_Shape aShape;
848 if ( ic->MoreSelected() )
849 aShape = ic->SelectedShape();
851 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
852 aPnt = BRep_Tool::Pnt( TopoDS::Vertex( ic->SelectedShape() ) );
855 OCCViewer_ViewPort3d* vp =
856 ((OCCViewer_ViewWindow*)aViewer->getViewManager()->getActiveView())->getViewPort();
857 aPnt = CurveCreator_Utils::ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
859 // set the coordinates into dialog
860 CurveCreator::Coordinates aCoords;
861 aCoords.push_back( aPnt.X() );
862 aCoords.push_back( aPnt.Y() );
863 if ( myCurve->getDimension() == 3 ) {
864 aCoords.push_back( aPnt.Z() );
866 onAddNewPoint(aCoords);
867 // myNewPointEditor->setCoordinates( aCoords );
871 //=================================================================================
872 // function : CurveCreator_Widget::onMousePress()
873 // purpose : Manage mouse press events in Modification mode
874 //=================================================================================
875 void CurveCreator_Widget::onMousePress( SUIT_ViewWindow*, QMouseEvent* theEvent )
877 if ( theEvent->button() != Qt::LeftButton )
880 switch( getActionMode() ) {
881 case ModificationMode: {
882 //store initial cursor position for Drag&Drop
883 setDragStarted( true, theEvent->pos() );
887 onGetCoordsByClick( theEvent );
895 //=================================================================================
896 // function : HYDROGUI_PolylineOp::onMouseRelease()
897 // purpose : Manage mouse press events in Modification mode
898 //=================================================================================
899 void CurveCreator_Widget::onMouseRelease( SUIT_ViewWindow*, QMouseEvent* theEvent )
901 if ( getActionMode() != ModificationMode )
904 if ( myDragStarted ) {
905 if ( myDragged ) // if the drag of some points has happened, restore the drag selection
906 setSelectedPonts( myDragPoints );
907 setDragStarted( false );
909 else // check whether the segment is clicked an a new point should be added to the segment
910 insertPointToSelectedSegment( theEvent->pos().x(), theEvent->pos().y() );
912 // updates the input panel table to show the selected point coordinates
913 updateLocalPointView();
916 //=================================================================================
917 // function : GeometryGUI::onMouseMove()
918 // purpose : Manage mouse move events in Modification mode
919 //=================================================================================
920 void CurveCreator_Widget::onMouseMove( SUIT_ViewWindow*, QMouseEvent* theEvent )
922 if ( getActionMode() != ModificationMode || !myDragStarted )
925 QPoint aPos = theEvent->pos();
926 if ( (aPos - myDragStartPosition).manhattanLength() < QApplication::startDragDistance() )
929 moveSelectedPoints( aPos.x(), aPos.y() );
930 myDragStartPosition = aPos;
933 void CurveCreator_Widget::onCellChanged( int theRow, int theColumn )
935 int aCurrSect = getSectionId( theRow );
936 int aPntIndex = getPointId( theRow );
941 SectionToPointList aSelPoints;
942 startCurveModification( aSelPoints );
944 double aX = myLocalPointView->item( theRow, 2 )->text().toDouble();
945 double anY = myLocalPointView->item( theRow, 3 )->text().toDouble();
946 std::deque<float> aChangedPos;
947 aChangedPos.push_back( aX );
948 aChangedPos.push_back( anY );
949 myCurve->setPoint( aCurrSect, aPntIndex, aChangedPos );
951 finishCurveModification( aSelPoints );
955 * Removes a selected section from the curve. Updates undo/redo status
957 void CurveCreator_Widget::removeSection()
959 QList< QPair<int,int> > aSelPoints = mySectionView->getSelectedPoints();
962 // myCurve->startOperation();
963 for( int i = 0 ; i < aSelPoints.size() ; i++ ){
964 if( aCurrSect != aSelPoints[i].first ){
966 aCurrSect = aSelPoints[i].first;
968 int aPntIndx = aSelPoints[i].second - aRemoveCnt;
969 myCurve->removePoint( aCurrSect, aPntIndx );
970 mySectionView->pointsRemoved( aCurrSect, aPntIndx );
973 QList<int> aSections = mySectionView->getSelectedSections();
974 for( int i = 0 ; i < aSections.size() ; i++ ){
975 int aSectNum = aSections[i] - (i);
976 myCurve->removeSection( aSectNum );
977 mySectionView->sectionsRemoved( aSectNum );
979 // myCurve->finishOperation();
980 mySectionView->clearSelection();
985 * Removes a selected points from the curve. Updates undo/redo status
987 void CurveCreator_Widget::removePoint()
989 SectionToPointList aPoints;
990 getSelectedPonts( aPoints );
991 if ( aPoints.size() == 0 )
994 SectionToPointList aSelPoints;
995 startCurveModification( aSelPoints, false );
997 // the points should be removed in a decreased order
998 QMap<int, QList<int> > aConvPoints;
999 convert( aPoints, aConvPoints );
1000 QMap<int, QList<int> >::const_iterator anIt = aConvPoints.begin(),
1001 aLast = aConvPoints.end();
1002 for ( ; anIt != aLast; anIt++ ) {
1003 int aSectionId = anIt.key();
1005 QList<int> aSectionPoints = anIt.value();
1007 for( int i = aSectionPoints.size()-1; i >= 0; i-- ){
1008 int aPntIndx = aSectionPoints[i];
1009 myCurve->removePoint( aSectionId, aPntIndx );
1010 mySectionView->pointsRemoved( aSectionId, aPntIndx );
1014 finishCurveModification( SectionToPointList() );
1017 void CurveCreator_Widget::insertPointToSelectedSegment( const int theX,
1020 OCCViewer_Viewer* aViewer = getOCCViewer();
1025 gp_Pnt aPoint1, aPoint2;
1026 bool isFoundPoint = false;
1027 Handle(AIS_InteractiveContext) aContext = aViewer->getAISContext();
1028 for ( aContext->InitSelected(); aContext->MoreSelected() && !isFoundPoint;
1029 aContext->NextSelected() ) {
1030 TopoDS_Shape aTShape = aContext->SelectedShape();
1031 if ( !aTShape.IsNull() && aTShape.ShapeType() == TopAbs_VERTEX )
1034 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
1035 if ( anOwner.IsNull() )
1037 const TopLoc_Location& aLocation = anOwner->Location();
1038 Handle(AIS_InteractiveObject) anAIS = Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
1039 isFoundPoint = pointOnObject( anAIS, theX, theY, aPoint, aPoint1, aPoint2 );
1042 if ( !isFoundPoint )
1045 // insert the point to the model curve
1046 SectionToPointList aSelPoints;
1047 startCurveModification( aSelPoints );
1049 CurveCreator::Coordinates aCoords;
1050 aCoords.push_back( aPoint.X() );
1051 aCoords.push_back( aPoint.Y() );
1053 SectionToPointList aPoints1, aPoints2;
1054 findSectionsToPoints( aPoint1.X(), aPoint1.Y(), aPoints1 );
1055 findSectionsToPoints( aPoint2.X(), aPoint2.Y(), aPoints2 );
1056 SectionToPointList::const_iterator anIt = aPoints1.begin(), aLast = aPoints1.end();
1057 int aSectionId = -1;
1058 // there can be a case when a new point is added into two sections
1059 int aPoint1Id = -1, aPoint2Id = -1;
1060 for ( ; anIt != aLast && aSectionId < 0; anIt++ ) {
1061 int aSectionCur = anIt->first;
1062 SectionToPointList::const_iterator anIt2 = aPoints2.begin(), aLast2 = aPoints2.end();
1063 for ( ; anIt2 != aLast2 && aSectionId < 0; anIt2++ ) {
1064 if ( anIt2->first == aSectionCur ) {
1065 aSectionId = aSectionCur;
1066 aPoint1Id = anIt->second;
1067 aPoint2Id = anIt2->second;
1072 int anInsertPos = -1;
1073 int aLastPoint = myCurve->getNbPoints( aSectionId )-1;
1074 if ( ( aPoint1Id == aLastPoint && aPoint2Id == 0 ) ||
1075 ( aPoint2Id == aLastPoint && aPoint1Id == 0 ) )
1076 anInsertPos = -1; // if the section happens between first and last points
1078 anInsertPos = aPoint1Id < aPoint2Id ? aPoint1Id + 1 : aPoint2Id + 1;
1080 myCurve->addPoints( aCoords, aSectionId, anInsertPos );
1081 mySectionView->pointsAdded( aSectionId, myCurve->getNbPoints( aSectionId ) );
1083 finishCurveModification( aSelPoints );
1089 void CurveCreator_Widget::moveSelectedPoints( const int theXPosition,
1090 const int theYPosition )
1092 OCCViewer_Viewer* aViewer = getOCCViewer();
1096 SectionToPointList aPoints;
1097 startCurveModification( aPoints, false );
1099 OCCViewer_ViewWindow* aWindow =
1100 (OCCViewer_ViewWindow*)aViewer->getViewManager()->getActiveView();
1101 gp_Pnt aStartPnt = CurveCreator_Utils::ConvertClickToPoint( myDragStartPosition.x(),
1102 myDragStartPosition.y(),
1103 aWindow->getViewPort()->getView() );
1104 gp_Pnt anEndPnt = CurveCreator_Utils::ConvertClickToPoint( theXPosition, theYPosition,
1105 aWindow->getViewPort()->getView() );
1106 double aXDelta = aStartPnt.X() - anEndPnt.X();
1107 double anYDelta = aStartPnt.Y() - anEndPnt.Y();
1111 std::deque<float> aChangedPos;
1112 SectionToPointList::const_iterator anIt = myDragPoints.begin(), aLast = myDragPoints.end();
1113 for ( ; anIt != aLast; anIt++ ) {
1114 aSectionId = anIt->first;
1115 aPointId = anIt->second;
1116 aChangedPos = myCurve->getPoint( aSectionId, aPointId );
1117 if ( aChangedPos.size() < 2 )
1119 aChangedPos[0] = aChangedPos[0] - aXDelta;
1120 aChangedPos[1] = aChangedPos[1] - anYDelta;
1121 myCurve->setPoint( aSectionId, aPointId, aChangedPos );
1124 finishCurveModification( myDragPoints );
1127 void CurveCreator_Widget::updateLocalPointView()
1129 OCCViewer_Viewer* aViewer = getOCCViewer();
1132 Handle(AIS_InteractiveContext) aContext = aViewer->getAISContext();
1134 bool isBlocked = myLocalPointView->blockSignals(true);
1136 myLocalPointView->setRowCount( 0 );
1137 for ( aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected() ) {
1138 TopoDS_Vertex aVertex;
1139 TopoDS_Shape aShape = aContext->SelectedShape();
1140 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
1141 aVertex = TopoDS::Vertex( aContext->SelectedShape() );
1143 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
1144 if ( !anOwner.IsNull() ) {
1145 Handle(AIS_InteractiveObject) anAIS = Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
1146 if ( !anAIS.IsNull() ) {
1147 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast( anAIS);
1148 if ( !aPoint.IsNull() )
1149 aVertex = TopoDS::Vertex( aPoint->Vertex() );
1151 if ( aVertex.IsNull() ) {
1152 // the following happens if there are no points in the current curve, there is only a shape
1153 /*Handle(StdSelect_BRepOwner) aBrepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1154 if ( aBrepOwner.IsNull() )
1156 if ( aBrepOwner->HasShape() ) {
1157 const TopoDS_Shape& aShape = aBrepOwner->Shape();
1158 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
1160 aVertex = TopoDS::Vertex( aShape );
1165 if ( aVertex.IsNull() )
1167 aPnt = BRep_Tool::Pnt( aVertex );
1168 addLocalPointToTable( aPnt.X(), aPnt.Y() );
1171 myLocalPointView->blockSignals(isBlocked);
1177 void CurveCreator_Widget::setLocalPointContext( const bool theOpen, const bool isUpdateTable )
1179 OCCViewer_Viewer* aViewer = getOCCViewer();
1182 Handle(AIS_InteractiveContext) ic = aViewer->getAISContext();
1185 // Open local context if there is no one
1186 if ( !ic->HasOpenedContext() ) {
1187 ic->ClearCurrents( false );
1188 ic->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
1190 AIS_ListOfInteractive aList;
1191 ic->DisplayedObjects( aList );
1193 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
1196 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
1198 Handle(AIS_InteractiveObject) anAIS = it.Value();
1199 if ( !anAIS.IsNull() )
1201 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
1203 ic->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
1204 ic->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_WIRE ) );
1206 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
1208 ic->Load( anAIS, -1/*selection mode*/, false/*allow decomposition*/ );
1209 ic->Activate( anAIS, TopAbs_VERTEX );
1216 if ( ic->HasOpenedContext() )
1217 ic->CloseAllContexts();
1218 if ( isUpdateTable )
1219 updateLocalPointView();
1223 void CurveCreator_Widget::addLocalPointToTable( const double theX, const double theY )
1225 SectionToPointList aPoints;
1226 findSectionsToPoints( theX, theY, aPoints );
1228 SectionToPointList aSkipList;
1229 // table could not contain two equal value rows
1230 int aRowId = myLocalPointView->rowCount();
1231 double aCurrentX, aCurrentY;
1232 int aSectionId, aPointId;
1233 SectionToPoint aPoint;
1234 for ( int i = 0; i < aRowId; i++ ) {
1235 aCurrentX = myLocalPointView->item( i, 2 )->data( Qt::UserRole ).toDouble();
1236 aCurrentY = myLocalPointView->item( i, 3 )->data( Qt::UserRole ).toDouble();
1237 if ( fabs( aCurrentX - theX ) < LOCAL_SELECTION_TOLERANCE &&
1238 fabs( aCurrentY - theY ) < LOCAL_SELECTION_TOLERANCE ) {
1239 aPoint = qMakePair( getSectionId( i ), getPointId( i ) );
1240 if ( !aSkipList.contains( aPoint ) )
1241 aSkipList.append( aPoint );
1244 if ( aSkipList.size() == aPoints.size() )
1247 QTableWidgetItem* anItem;
1248 SectionToPointList::const_iterator anIt = aPoints.begin(), aLast = aPoints.end();
1249 for ( ; anIt != aLast; anIt++ ) {
1251 if ( aSkipList.contains( aPoint ) )
1254 myLocalPointView->setRowCount( aRowId+1 );
1255 aSectionId = aPoint.first;
1256 aPointId = aPoint.second;
1258 anItem = new QTableWidgetItem( myCurve->getSectionName( aSectionId ).c_str() );
1259 anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
1260 anItem->setData( Qt::UserRole, aSectionId );
1261 myLocalPointView->setItem( aRowId, 0, anItem );
1263 anItem = new QTableWidgetItem( QString::number( aPointId + 1 ) );
1264 anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
1265 anItem->setData( Qt::UserRole, aPointId );
1266 myLocalPointView->setItem( aRowId, 1, anItem );
1268 anItem = new QTableWidgetItem( QString::number( theX ) );
1269 anItem->setData( Qt::UserRole, theX );
1270 myLocalPointView->setItem( aRowId, 2, anItem );
1272 anItem = new QTableWidgetItem( QString::number( theY ) );
1273 anItem->setData( Qt::UserRole, theY );
1274 myLocalPointView->setItem( aRowId, 3, anItem );
1279 * Set drag operation started. Save the position and a list of dragged points
1280 * \param theState the drag operation state: started/finished
1281 * \param thePoint the start drag position
1283 void CurveCreator_Widget::setDragStarted( const bool theState, const QPoint& thePoint )
1285 OCCViewer_Viewer* aViewer = getOCCViewer();
1287 getSelectedPonts( myDragPoints );
1288 myDragStarted = myDragPoints.size();
1289 myDragStartPosition = thePoint;
1290 if ( aViewer && myDragStarted ) {
1291 // change a viewer interaction style in order to avoid a select rectangle build
1292 myDragInteractionStyle = aViewer->interactionStyle();
1293 aViewer->setInteractionStyle(SUIT_ViewModel::KEY_FREE);
1297 if ( aViewer && myDragStarted )
1298 aViewer->setInteractionStyle( myDragInteractionStyle );
1299 myDragStarted = false;
1300 myDragPoints.clear();
1305 void CurveCreator_Widget::getSelectedPonts( CurveCreator_Widget::SectionToPointList& thePoints )
1308 for ( int i = 0, aNb = myLocalPointView->rowCount(); i < aNb; i++ )
1309 thePoints.append( qMakePair( getSectionId( i ), getPointId( i ) ) );
1312 void CurveCreator_Widget::setSelectedPonts( const CurveCreator_Widget::SectionToPointList& thePoints )
1314 OCCViewer_Viewer* aViewer = getOCCViewer();
1318 AIS_ListOfInteractive aListToSelect;
1320 Handle(AIS_InteractiveContext) ic = aViewer->getAISContext();
1321 if ( !ic->HasOpenedContext() )
1324 AIS_ListOfInteractive aDisplayedList;
1325 ic->DisplayedObjects( aDisplayedList );
1326 for ( AIS_ListIteratorOfListOfInteractive it( aDisplayedList ); it.More(); it.Next() )
1328 Handle(AIS_InteractiveObject) anAIS = it.Value();
1329 if ( anAIS.IsNull() )
1331 Handle(AIS_Point) anAISPoint = Handle(AIS_Point)::DownCast( anAIS );
1332 if ( anAISPoint.IsNull() )
1335 TopoDS_Vertex aVertex = TopoDS::Vertex( anAISPoint->Vertex() );
1337 if ( aVertex.IsNull() )
1340 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
1342 SectionToPointList aPoints;
1343 findSectionsToPoints( aPnt.X(), aPnt.Y(), aPoints );
1345 SectionToPointList::const_iterator anIt = aPoints.begin(), aLast = aPoints.end();
1346 SectionToPoint aPoint;
1347 for ( ; anIt != aLast; anIt++ ) {
1349 if ( thePoints.contains( aPoint ) )
1350 aListToSelect.Append( anAIS );
1354 ic->ClearSelected( Standard_False );
1355 aViewer->setObjectsSelected( aListToSelect );
1356 updateLocalPointView();
1360 * Get viewer information before perform the curve modification.
1361 * Take a list of selected cuve points an close local context.
1362 * The context should be closed because the curve presentation is
1363 * redisplayed and if it is not closed, when we close the local context
1364 * later, the presentation shown in the local context is disappeared.
1365 * \param thePoints an output list of curve selected points
1366 * \param theFillPoints a flag whether the selection list should be filled
1368 void CurveCreator_Widget::startCurveModification(
1369 CurveCreator_Widget::SectionToPointList& thePoints,
1370 const bool theFillPoints )
1372 if ( theFillPoints ) {
1374 getSelectedPonts( thePoints );
1376 setLocalPointContext( false );
1380 * Restore the viewer state after the curve modification is done.
1381 * Open local context and select given points inside it.
1382 * \param thePoints a list of curve selected points
1384 void CurveCreator_Widget::finishCurveModification(
1385 const CurveCreator_Widget::SectionToPointList& thePoints )
1387 setLocalPointContext( true );
1389 setSelectedPonts( thePoints );
1393 * Returns a point index in the model curve by the point coordinates in the viewer
1394 * \param theX the X coordinate of the point
1395 * \param theY the Y coordinate of the point
1397 int CurveCreator_Widget::findLocalPointIndex( int theSectionId, float theX, float theY )
1401 CurveCreator::Coordinates aCoords;
1402 for ( int i = 0, aNb = myCurve->getNbPoints( theSectionId ); i < aNb && aPntIndex < 0; i++ ) {
1403 aCoords = myCurve->getPoint( theSectionId, i );
1404 if ( aCoords.size() < 2 )
1406 if ( fabs( aCoords[0] - theX ) < LOCAL_SELECTION_TOLERANCE &&
1407 fabs( aCoords[1] - theY ) < LOCAL_SELECTION_TOLERANCE )
1414 void CurveCreator_Widget::findSectionsToPoints( const double theX, const double theY,
1415 CurveCreator_Widget::SectionToPointList& thePoints )
1420 for ( int i = 0, aNb = myCurve->getNbSections(); i < aNb; i++ ) {
1421 aPointId = findLocalPointIndex( i, theX, theY );
1424 SectionToPoint aPoint = qMakePair( i, aPointId );
1425 if ( !thePoints.contains( aPoint ) )
1426 thePoints.append( aPoint );
1430 void CurveCreator_Widget::convert( const SectionToPointList& thePoints,
1431 QMap<int, QList<int> >& theConvPoints )
1433 theConvPoints.clear();
1435 SectionToPointList::const_iterator anIt = thePoints.begin(), aLast = thePoints.end();
1437 int aSectionId, aPointId;
1438 for ( ; anIt != aLast; anIt++ ) {
1439 aSectionId = anIt->first;
1440 aPointId = anIt->second;
1442 if ( theConvPoints.contains( aSectionId ) )
1443 aPoints = theConvPoints[aSectionId];
1444 if ( aPoints.contains( aPointId ) )
1446 aPoints.append( aPointId );
1447 theConvPoints[aSectionId] = aPoints;
1452 * Checks whether the point belongs to the OCC object
1453 * \param theObject a line or shape with a bspline inside
1454 * \param theX the X coordinate in the view.
1455 * \param theY the Y coordinate in the view.
1456 * \param thePoint the output point to be append to the model curve
1457 * \param thePoint1 the output point to bound the line where a new point should be inserted
1458 * \param thePoint2 the output point to bound the line where a new point should be inserted
1460 bool CurveCreator_Widget::pointOnObject( Handle(AIS_InteractiveObject) theObject,
1461 const int theX, const int theY,
1463 gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
1465 bool isFound = false;
1467 OCCViewer_Viewer* aViewer = getOCCViewer();
1468 if ( theObject.IsNull() || !aViewer )
1472 Standard_Real aParameter;
1473 gp_Pnt aPnt1, aPnt2;
1474 Handle(AIS_Line) aLine = Handle(AIS_Line)::DownCast( theObject );
1475 if ( !aLine.IsNull() ) {
1476 const Handle(Geom_Line) aGLine = aLine->Line();
1477 isFound = hasProjectPointOnCurve( theX, theY, aGLine, aParameter );
1479 aPoint = aGLine->Value( aParameter );
1481 Handle(Geom_Point) aPStart;
1482 Handle(Geom_Point) aPEnd;
1483 aLine->Points( aPStart, aPEnd );
1484 aPnt1 = aPStart->Pnt();
1485 aPnt2 = aPEnd->Pnt();
1487 // in case of Geom line a projection is performed to the infinite line,
1488 // so it is necessary to bound it by the line size
1490 aLineBox.Set( aPnt1, gp_Vec( aPnt1, aPnt2 ) );
1491 isFound = !aLineBox.IsOut( aPoint );
1495 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
1496 if ( !aShape.IsNull() ) {
1497 const TopoDS_Wire& aWire = TopoDS::Wire( aShape->Shape() );
1498 if ( !aWire.IsNull() ) {
1499 TopExp_Explorer anExp( aWire, TopAbs_EDGE );
1500 for ( ; anExp.More(); anExp.Next())
1502 const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
1503 if ( !anEdge.IsNull() ) {
1504 Standard_Real aFirst, aLast;
1505 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
1507 if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
1508 Handle(Geom_BSplineCurve) aBSplineCurve = Handle(Geom_BSplineCurve)::DownCast( aCurve );
1509 if ( !aBSplineCurve.IsNull() ) {
1510 isFound = hasProjectPointOnCurve( theX, theY, aCurve, aParameter );
1512 aPoint = aBSplineCurve->Value( aParameter );
1513 Standard_Integer anI1, anI2;
1514 aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
1516 aPnt1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
1517 aPnt2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
1535 * Returns whether the clicked point belong to the curve or has a very near projection
1536 * \param theX the X coordinate of a point clicked in the OCC viewer
1537 * \param theY the Y coordinate of a point clicked in the OCC viewer
1538 * \param theCurve a geometry curve
1539 * \param theOutPoint a found projected point on the curve
1541 bool CurveCreator_Widget::hasProjectPointOnCurve( const int theX, const int theY,
1542 const Handle(Geom_Curve)& theCurve,
1543 Standard_Real& theParameter )
1545 bool isFound = false;
1546 OCCViewer_Viewer* aViewer = getOCCViewer();
1550 OCCViewer_ViewWindow* aWindow =
1551 (OCCViewer_ViewWindow*)aViewer->getViewManager()->getActiveView();
1552 Handle(V3d_View) aView = aWindow->getViewPort()->getView();
1553 gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, aView );
1555 GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
1556 Standard_Integer aNbPoint = aProj.NbPoints();
1558 for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
1559 gp_Pnt aNewPoint = aProj.Point( j );
1560 theParameter = aProj.Parameter( j );
1563 CurveCreator_Utils::ConvertPointToClick( aNewPoint, aView, aX, anY );
1565 int aXDelta = abs( aX - theX );
1566 int anYDelta = abs( anY - theY );
1567 isFound = aXDelta < SCENE_PIXEL_TOLERANCE && anYDelta < SCENE_PIXEL_TOLERANCE;
1574 * Returns a section index from the table
1575 * \param theRowId a table row
1577 int CurveCreator_Widget::getSectionId( const int theRowId ) const
1579 return myLocalPointView->item( theRowId, 0 )->data( Qt::UserRole ).toInt();
1583 * Returns a point index from the table
1584 * \param theRowId a table row
1586 int CurveCreator_Widget::getPointId( const int theRowId ) const
1588 return myLocalPointView->item( theRowId, 1 )->data( Qt::UserRole ).toInt();