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 ( removeEnabled() )
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;
311 switch ( getActionMode() ) {
315 QList<int> aSelSection = mySectionView->getSelectedSections();
316 if( aSelSection.size() > 0 ){
317 mySection = aSelSection[0];
318 myPointNum = myCurve->getNbPoints(mySection);
322 case ModificationMode: {
323 anEnabledAct << CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID << SET_SECTIONS_POLYLINE_ID << SET_SECTIONS_SPLINE_ID;
324 int aSectCnt = myCurve->getNbSections();
326 anEnabledAct << CLEAR_ALL_ID;
328 anEnabledAct << JOIN_ALL_ID;
329 if( aSelSections.size() > 1 ){
330 anEnabledAct << JOIN_ID;
334 case DetectionMode: {
341 /*if( aSelSections[ aSelSections.size() - 1 ] < ( myCurve->getNbSections() - 1 ) ){
342 anEnabledAct << DOWN_ID;
346 /*case CurveCreator_TreeView::ST_POINTS_ONE_SECTION:{
347 if( aSelPoints[0].second > 0 ){
348 anEnabledAct << UP_ID;
350 int aLastIndex = aSelPoints.size()-1;
351 int aSect = aSelPoints[0].first;
352 if( aSelPoints[aLastIndex].second < (myCurve->getNbPoints(aSect) - 1)){
353 anEnabledAct << DOWN_ID;
355 if( aSelPoints.size() == 1){
356 anEnabledAct << INSERT_POINT_BEFORE_ID << INSERT_POINT_AFTER_ID;
363 /*int aSelObjsCnt = aSelPoints.size() + aSelSections.size();
364 if( aSelObjsCnt > 0 ){
365 anEnabledAct << REMOVE_ID;
367 if( (myCurve->getNbSections() + myCurve->getNbPoints()) > 0 ){
368 anEnabledAct << REMOVE_ALL_ID;
370 if( myCurve->getNbSections() > 1 ){
371 anEnabledAct << JOIN_ALL_ID;
374 QList<ActionId> anIds = myActionMap.keys();
375 for( int i = 0 ; i < anIds.size() ; i++ ){
376 if( myActionMap.contains(anIds[i]) ){
377 if( anEnabledAct.contains(anIds[i]) ){
378 myActionMap[anIds[i]]->setEnabled(true);
381 myActionMap[anIds[i]]->setEnabled(false);
386 emit selectionChanged();
389 void CurveCreator_Widget::onAdditionMode(bool checked)
391 OCCViewer_Viewer* aViewer = getOCCViewer();
392 if( !myCurve || !aViewer )
395 Handle(AIS_InteractiveContext) aContext = aViewer->getAISContext();
396 OCCViewer_ViewManager* aViewManager = dynamic_cast<OCCViewer_ViewManager*>
397 (aViewer->getViewManager());
398 // if ( aViewManager->getType() == OCCViewer_Viewer::Type() ) {
401 /* myGuiState = aViewWindow->saveState();
402 aViewer->enableMultiselection(false);
403 aViewer->enableSelection(false);*/
405 /* aViewer->enableMultiselection(true);
406 aViewer->enableSelection(true);
407 aViewWindow->restoreState( myGuiState );*/
414 QList<int> aSelSection = mySectionView->getSelectedSections();
415 if( aSelSection.size() > 0 ){
416 mySection = aSelSection[0];
419 QList< QPair<int,int> > aSelPoints = mySectionView->getSelectedPoints();
420 if( aSelPoints.size() > 0 ){
421 mySection = aSelPoints[0].first;
422 myPointNum = aSelPoints[0].second + 1;
425 // emit subOperationStarted( myNewPointEditor );
428 void CurveCreator_Widget::onModificationMode(bool checked)
430 myLocalPointView->setVisible( checked );
433 void CurveCreator_Widget::onDetectionMode(bool checked)
437 void CurveCreator_Widget::onModeChanged(bool checked)
439 ActionMode aMode = NoneMode;
441 QAction* anAction = (QAction*)sender();
442 switch(myActionMap.key(anAction)) {
443 case ADDITION_MODE_ID:
444 aMode = AdditionMode;
445 if (myActionMap[MODIFICATION_MODE_ID]->isChecked())
446 myActionMap[MODIFICATION_MODE_ID]->trigger();
447 else if (myActionMap[DETECTION_MODE_ID]->isChecked())
448 myActionMap[DETECTION_MODE_ID]->trigger();
450 case MODIFICATION_MODE_ID:
451 aMode = ModificationMode;
452 if (myActionMap[ADDITION_MODE_ID]->isChecked())
453 myActionMap[ADDITION_MODE_ID]->trigger();
454 else if (myActionMap[DETECTION_MODE_ID]->isChecked())
455 myActionMap[DETECTION_MODE_ID]->trigger();
457 case DETECTION_MODE_ID:
458 aMode = DetectionMode;
459 if (myActionMap[ADDITION_MODE_ID]->isChecked())
460 myActionMap[ADDITION_MODE_ID]->trigger();
461 else if (myActionMap[MODIFICATION_MODE_ID]->isChecked())
462 myActionMap[MODIFICATION_MODE_ID]->trigger();
466 onSelectionChanged();
467 setLocalPointContext( aMode == ModificationMode, true );
470 void CurveCreator_Widget::onAddNewPoint(const CurveCreator::Coordinates& theCoords)
474 //myCurve->addPoints(theCoords, mySection, myPointNum );
475 //mySectionView->pointsAdded( mySection, myPointNum );
477 QList<int> aSections = mySectionView->getSelectedSections();
478 if( aSections.size() == 0 ){
481 int aSection = aSections[0];
482 myCurve->addPoints(theCoords, aSection); // add to the end of section
483 mySectionView->pointsAdded( aSection, myCurve->getNbPoints( aSection ) );
484 onSelectionChanged();
488 void CurveCreator_Widget::onNewSection()
492 myNewSectionEditor->clear();
493 myNewSectionEditor->setEditMode(false);
494 QString aSectName = QString( getUniqSectionName(myCurve).c_str() );
495 myNewSectionEditor->setSectionParameters(aSectName, true, CurveCreator::Polyline );
496 emit subOperationStarted( myNewSectionEditor );
499 void CurveCreator_Widget::onAddNewSection()
503 myCurve->addSection( myNewSectionEditor->getName().toStdString(), myNewSectionEditor->getSectionType(),
504 myNewSectionEditor->isClosed() );
505 mySectionView->sectionAdded( -1 ); // add a new section to the end of list
506 QString aNewName = QString(getUniqSectionName(myCurve).c_str());
507 myNewSectionEditor->setSectionName(aNewName);
508 onSelectionChanged();
513 void CurveCreator_Widget::onCancelSection()
515 emit subOperationFinished( myNewSectionEditor );
518 QAction* CurveCreator_Widget::createAction( ActionId theId, const QString& theName, const QPixmap& theImage,
519 const QString& theToolTip, const QKeySequence& theShortcut )
521 QAction* anAct = new QAction(theName,this);
522 if( !theImage.isNull() ){
523 anAct->setIcon(theImage);
525 anAct->setShortcut(theShortcut);
526 anAct->setToolTip(theToolTip);
527 myActionMap[theId] = anAct;
531 QAction* CurveCreator_Widget::getAction(ActionId theId)
533 if( myActionMap.contains(theId) )
534 return myActionMap[theId];
538 CurveCreator_Widget::ActionMode CurveCreator_Widget::getActionMode() const
540 ActionMode aMode = NoneMode;
542 if ( myActionMap[ADDITION_MODE_ID]->isChecked() )
543 aMode = AdditionMode;
544 else if ( myActionMap[MODIFICATION_MODE_ID]->isChecked() )
545 aMode = ModificationMode;
546 else if ( myActionMap[DETECTION_MODE_ID]->isChecked() )
547 aMode = DetectionMode;
552 void CurveCreator_Widget::onEditSection( int theSection )
556 mySection = theSection;
557 QString aSectName = QString::fromStdString( myCurve->getSectionName(theSection));
558 bool isClosed = myCurve->isClosed(theSection);
559 CurveCreator::SectionType aType = myCurve->getSectionType(theSection);
560 myNewSectionEditor->setEditMode(true);
561 myNewSectionEditor->setSectionParameters( aSectName, isClosed, aType );
563 emit subOperationStarted( myNewSectionEditor );
566 void CurveCreator_Widget::onModifySection()
570 QString aName = myNewSectionEditor->getName();
571 bool isClosed = myNewSectionEditor->isClosed();
572 CurveCreator::SectionType aSectType = myNewSectionEditor->getSectionType();
573 // myCurve->startOperation();
574 if( myCurve->getSectionName(mySection) != aName.toStdString() )
575 myCurve->setSectionName( mySection , aName.toStdString() );
577 if( myCurve->getSectionType(mySection) != aSectType )
578 myCurve->setSectionType( mySection, aSectType );
580 if( myCurve->isClosed(mySection) != isClosed )
581 myCurve->setClosed( mySection, isClosed );
582 // myCurve->finishOperation();
583 mySectionView->sectionChanged(mySection);
588 /*void CurveCreator_Widget::onEditPoint( int theSection, int thePoint )
590 if( !myNewPointEditor || !myEdit )
592 mySection = theSection;
593 myPointNum = thePoint;
594 QString aSectName = QString::fromStdString( myCurve->getSectionName(theSection));
595 myNewPointEditor->setEditMode(true);
596 myNewPointEditor->setSectionName(aSectName);
597 myNewPointEditor->setDimension( myCurve->getDimension() );
598 CurveCreator::Coordinates aCoords = myCurve->getCoordinates(theSection,thePoint);
599 myNewPointEditor->setCoordinates(aCoords);
600 emit subOperationStarted( myNewPointEditor );
603 void CurveCreator_Widget::onModifyPoint()
607 CurveCreator::Coordinates aCoords = myNewPointEditor->getCoordinates();
608 myEdit->setCoordinates( aCoords, mySection, myPointNum );
609 mySectionView->pointDataChanged( mySection, myPointNum );
614 void CurveCreator_Widget::onJoin()
618 QList<int> aSections = mySectionView->getSelectedSections();
619 if( aSections.size() == 0 ){
622 int aMainSect = aSections[0];
623 int aMainSectSize = myCurve->getNbPoints(aMainSect);
624 // myCurve->startOperation();
625 for( int i = 1 ; i < aSections.size() ; i++ ){
626 int aSectNum = aSections[i] - (i-1);
627 myCurve->join( aMainSect, aSectNum );
628 mySectionView->sectionsRemoved( aSectNum );
630 // myCurve->finishOperation();
631 int aNewSectSize = myCurve->getNbPoints(aMainSect);
632 if( aNewSectSize != aMainSectSize )
633 mySectionView->pointsAdded( aMainSect, aMainSectSize, aNewSectSize-aMainSectSize );
637 void CurveCreator_Widget::onRemove()
642 switch( getActionMode() ) {
646 case ModificationMode:
654 void CurveCreator_Widget::onClearAll()
659 mySectionView->reset();
660 onSelectionChanged();
664 void CurveCreator_Widget::onJoinAll()
669 mySectionView->reset();
670 onSelectionChanged();
674 void CurveCreator_Widget::onUndoSettings()
679 void CurveCreator_Widget::onSetSpline()
683 QList<int> aSelSections = mySectionView->getSelectedSections();
684 // myCurve->startOperation();
685 for( int i = 0 ; i < aSelSections.size() ; i++ ){
686 myCurve->setSectionType(aSelSections[i], CurveCreator::Spline );
687 mySectionView->sectionChanged(aSelSections[i]);
689 // myCurve->finishOperation();
693 void CurveCreator_Widget::onSetPolyline()
697 // myCurve->startOperation();
698 QList<int> aSelSections = mySectionView->getSelectedSections();
699 for( int i = 0 ; i < aSelSections.size() ; i++ ){
700 myCurve->setSectionType( aSelSections[i], CurveCreator::Polyline );
701 mySectionView->sectionChanged( aSelSections[i] );
703 // myCurve->finishOperation();
707 void CurveCreator_Widget::onCloseSections()
711 // myCurve->startOperation();
712 QList<int> aSelSections = mySectionView->getSelectedSections();
713 for( int i = 0 ; i < aSelSections.size() ; i++ ){
714 myCurve->setClosed(aSelSections[i], true);
715 mySectionView->sectionChanged(aSelSections[i]);
717 // myCurve->finishOperation();
721 void CurveCreator_Widget::onUncloseSections()
725 // myCurve->startOperation();
726 QList<int> aSelSections = mySectionView->getSelectedSections();
727 for( int i = 0 ; i < aSelSections.size() ; i++ ){
728 myCurve->setClosed(aSelSections[i], false);
729 mySectionView->sectionChanged(aSelSections[i]);
731 // myCurve->finishOperation();
735 void CurveCreator_Widget::onUndo()
740 CurveCreator_Widget::SectionToPointList aPoints;
741 startCurveModification( aPoints, false );
743 finishCurveModification();
744 mySectionView->reset();
747 void CurveCreator_Widget::onRedo()
751 CurveCreator_Widget::SectionToPointList aPoints;
752 startCurveModification( aPoints, false );
754 finishCurveModification();
755 mySectionView->reset();
758 void CurveCreator_Widget::updateUndoRedo()
762 QAction* anAct = myActionMap[UNDO_ID];
764 if( myCurve->getNbUndo() != 0 ){
765 anAct->setEnabled(true);
768 anAct->setDisabled(true);
771 anAct = myActionMap[REDO_ID];
773 if( myCurve->getNbRedo() != 0 ){
774 anAct->setEnabled(true);
777 anAct->setDisabled(true);
782 void CurveCreator_Widget::onContextMenu( QPoint thePoint )
784 QList<ActionId> aContextActions;
785 aContextActions << CLEAR_ALL_ID << JOIN_ALL_ID << SEPARATOR_ID <<
786 CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID << SET_SECTIONS_POLYLINE_ID <<
787 SET_SECTIONS_SPLINE_ID;
788 QPoint aGlPoint = mySectionView->mapToGlobal(thePoint);
790 QList<ActionId> aResAct;
791 for( int i = 0 ; i < aContextActions.size() ; i++ ){
792 if( aContextActions[i] != SEPARATOR_ID ){
793 if( myActionMap.contains(aContextActions[i]) ){
794 QAction* anAct = myActionMap[aContextActions[i]];
795 if( anAct->isEnabled() ){
796 aResAct << aContextActions[i];
802 aResAct << SEPARATOR_ID;
808 QMenu* aMenu = new QMenu(this);
809 for( int i = 0 ; i < aResAct.size() ; i++ ){
810 if( aResAct[i] == SEPARATOR_ID ){
811 aMenu->addSeparator();
814 QAction* anAct = myActionMap[aResAct[i]];
815 aMenu->insertAction(NULL, anAct);
818 aMenu->exec(aGlPoint);
821 QList<int> CurveCreator_Widget::getSelectedSections()
823 return mySectionView->getSelectedSections();
826 QList< QPair< int, int > > CurveCreator_Widget::getSelectedPoints()
828 return mySectionView->getSelectedPoints();
832 * According to the widget state, performs the remove action
834 void CurveCreator_Widget::removeSelected()
840 * Checks whether there are some selection to be removed
842 bool CurveCreator_Widget::removeEnabled()
844 bool isEnabled = getActionMode() == ModificationMode;
846 QList<int> aSelSections = mySectionView->getSelectedSections();
847 CurveCreator_TreeView::SelectionType aSelType = mySectionView->getSelectionType();
848 isEnabled = aSelType == CurveCreator_TreeView::ST_SECTIONS &&
849 aSelSections.size() == 1;
855 //=================================================================================
856 // function : GeometryGUI::onGetCoordsByClick()
857 // purpose : Manage mouse press events in Additon mode
858 //=================================================================================
859 void CurveCreator_Widget::onGetCoordsByClick( QMouseEvent* pe )
861 if (pe->button() != Qt::LeftButton)
864 if ( pe->modifiers() != Qt::ControlModifier ) {
865 OCCViewer_Viewer* aViewer = getOCCViewer();
868 Handle(AIS_InteractiveContext) ic = aViewer->getAISContext();
873 if ( pe->modifiers() == Qt::ShiftModifier )
874 ic->ShiftSelect(); // Append selection
876 ic->Select(); // New selection
878 /*TopoDS_Shape aShape;
881 if ( ic->MoreSelected() )
882 aShape = ic->SelectedShape();
884 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
885 aPnt = BRep_Tool::Pnt( TopoDS::Vertex( ic->SelectedShape() ) );
888 OCCViewer_ViewPort3d* vp =
889 ((OCCViewer_ViewWindow*)aViewer->getViewManager()->getActiveView())->getViewPort();
890 aPnt = CurveCreator_Utils::ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
892 // set the coordinates into dialog
893 CurveCreator::Coordinates aCoords;
894 aCoords.push_back( aPnt.X() );
895 aCoords.push_back( aPnt.Y() );
896 if ( myCurve->getDimension() == 3 ) {
897 aCoords.push_back( aPnt.Z() );
899 onAddNewPoint(aCoords);
900 // myNewPointEditor->setCoordinates( aCoords );
904 //=================================================================================
905 // function : CurveCreator_Widget::onMousePress()
906 // purpose : Manage mouse press events in Modification mode
907 //=================================================================================
908 void CurveCreator_Widget::onMousePress( SUIT_ViewWindow*, QMouseEvent* theEvent )
910 if ( theEvent->button() != Qt::LeftButton )
913 switch( getActionMode() ) {
914 case ModificationMode: {
915 //store initial cursor position for Drag&Drop
916 setDragStarted( true, theEvent->pos() );
920 onGetCoordsByClick( theEvent );
928 //=================================================================================
929 // function : HYDROGUI_PolylineOp::onMouseRelease()
930 // purpose : Manage mouse press events in Modification mode
931 //=================================================================================
932 void CurveCreator_Widget::onMouseRelease( SUIT_ViewWindow*, QMouseEvent* theEvent )
934 if ( getActionMode() != ModificationMode )
937 if ( myDragStarted ) {
938 if ( myDragged ) // if the drag of some points has happened, restore the drag selection
939 setSelectedPonts( myDragPoints );
940 setDragStarted( false );
942 else // check whether the segment is clicked an a new point should be added to the segment
943 insertPointToSelectedSegment( theEvent->pos().x(), theEvent->pos().y() );
945 // updates the input panel table to show the selected point coordinates
946 updateLocalPointView();
949 //=================================================================================
950 // function : GeometryGUI::onMouseMove()
951 // purpose : Manage mouse move events in Modification mode
952 //=================================================================================
953 void CurveCreator_Widget::onMouseMove( SUIT_ViewWindow*, QMouseEvent* theEvent )
955 if ( getActionMode() != ModificationMode || !myDragStarted )
958 QPoint aPos = theEvent->pos();
959 if ( (aPos - myDragStartPosition).manhattanLength() < QApplication::startDragDistance() )
962 moveSelectedPoints( aPos.x(), aPos.y() );
963 myDragStartPosition = aPos;
966 void CurveCreator_Widget::onCellChanged( int theRow, int theColumn )
968 int aCurrSect = getSectionId( theRow );
969 int aPntIndex = getPointId( theRow );
974 SectionToPointList aSelPoints;
975 startCurveModification( aSelPoints );
977 double aX = myLocalPointView->item( theRow, 2 )->text().toDouble();
978 double anY = myLocalPointView->item( theRow, 3 )->text().toDouble();
979 std::deque<float> aChangedPos;
980 aChangedPos.push_back( aX );
981 aChangedPos.push_back( anY );
982 myCurve->setPoint( aCurrSect, aPntIndex, aChangedPos );
984 finishCurveModification( aSelPoints );
988 * Removes a selected section from the curve. Updates undo/redo status
990 void CurveCreator_Widget::removeSection()
992 QList< QPair<int,int> > aSelPoints = mySectionView->getSelectedPoints();
995 // myCurve->startOperation();
996 for( int i = 0 ; i < aSelPoints.size() ; i++ ){
997 if( aCurrSect != aSelPoints[i].first ){
999 aCurrSect = aSelPoints[i].first;
1001 int aPntIndx = aSelPoints[i].second - aRemoveCnt;
1002 myCurve->removePoint( aCurrSect, aPntIndx );
1003 mySectionView->pointsRemoved( aCurrSect, aPntIndx );
1006 QList<int> aSections = mySectionView->getSelectedSections();
1007 for( int i = 0 ; i < aSections.size() ; i++ ){
1008 int aSectNum = aSections[i] - (i);
1009 myCurve->removeSection( aSectNum );
1010 mySectionView->sectionsRemoved( aSectNum );
1012 // myCurve->finishOperation();
1013 mySectionView->clearSelection();
1018 * Removes a selected points from the curve. Updates undo/redo status
1020 void CurveCreator_Widget::removePoint()
1022 SectionToPointList aPoints;
1023 getSelectedPonts( aPoints );
1024 if ( aPoints.size() == 0 )
1027 SectionToPointList aSelPoints;
1028 startCurveModification( aSelPoints, false );
1030 myCurve->removeSeveralPoints( aPoints );
1031 finishCurveModification( SectionToPointList() );
1034 void CurveCreator_Widget::insertPointToSelectedSegment( const int theX,
1037 OCCViewer_Viewer* aViewer = getOCCViewer();
1042 gp_Pnt aPoint1, aPoint2;
1043 bool isFoundPoint = false;
1044 Handle(AIS_InteractiveContext) aContext = aViewer->getAISContext();
1045 for ( aContext->InitSelected(); aContext->MoreSelected() && !isFoundPoint;
1046 aContext->NextSelected() ) {
1047 TopoDS_Shape aTShape = aContext->SelectedShape();
1048 if ( !aTShape.IsNull() && aTShape.ShapeType() == TopAbs_VERTEX )
1051 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
1052 if ( anOwner.IsNull() )
1054 const TopLoc_Location& aLocation = anOwner->Location();
1055 Handle(AIS_InteractiveObject) anAIS = Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
1056 isFoundPoint = pointOnObject( anAIS, theX, theY, aPoint, aPoint1, aPoint2 );
1059 if ( !isFoundPoint )
1062 // insert the point to the model curve
1063 SectionToPointList aSelPoints;
1064 startCurveModification( aSelPoints );
1066 CurveCreator::Coordinates aCoords;
1067 aCoords.push_back( aPoint.X() );
1068 aCoords.push_back( aPoint.Y() );
1070 SectionToPointList aPoints1, aPoints2;
1071 findSectionsToPoints( aPoint1.X(), aPoint1.Y(), aPoints1 );
1072 findSectionsToPoints( aPoint2.X(), aPoint2.Y(), aPoints2 );
1073 SectionToPointList::const_iterator anIt = aPoints1.begin(), aLast = aPoints1.end();
1074 int aSectionId = -1;
1075 // there can be a case when a new point is added into two sections
1076 int aPoint1Id = -1, aPoint2Id = -1;
1077 for ( ; anIt != aLast && aSectionId < 0; anIt++ ) {
1078 int aSectionCur = anIt->first;
1079 SectionToPointList::const_iterator anIt2 = aPoints2.begin(), aLast2 = aPoints2.end();
1080 for ( ; anIt2 != aLast2 && aSectionId < 0; anIt2++ ) {
1081 if ( anIt2->first == aSectionCur ) {
1082 aSectionId = aSectionCur;
1083 aPoint1Id = anIt->second;
1084 aPoint2Id = anIt2->second;
1089 int anInsertPos = -1;
1090 int aLastPoint = myCurve->getNbPoints( aSectionId )-1;
1091 if ( ( aPoint1Id == aLastPoint && aPoint2Id == 0 ) ||
1092 ( aPoint2Id == aLastPoint && aPoint1Id == 0 ) )
1093 anInsertPos = -1; // if the section happens between first and last points
1095 anInsertPos = aPoint1Id < aPoint2Id ? aPoint1Id + 1 : aPoint2Id + 1;
1097 myCurve->addPoints( aCoords, aSectionId, anInsertPos );
1098 mySectionView->pointsAdded( aSectionId, myCurve->getNbPoints( aSectionId ) );
1100 finishCurveModification( aSelPoints );
1105 void CurveCreator_Widget::moveSelectedPoints( const int theXPosition,
1106 const int theYPosition )
1108 OCCViewer_Viewer* aViewer = getOCCViewer();
1112 SectionToPointList aPoints;
1113 startCurveModification( aPoints, false );
1115 OCCViewer_ViewWindow* aWindow =
1116 (OCCViewer_ViewWindow*)aViewer->getViewManager()->getActiveView();
1117 gp_Pnt aStartPnt = CurveCreator_Utils::ConvertClickToPoint( myDragStartPosition.x(),
1118 myDragStartPosition.y(),
1119 aWindow->getViewPort()->getView() );
1120 gp_Pnt anEndPnt = CurveCreator_Utils::ConvertClickToPoint( theXPosition, theYPosition,
1121 aWindow->getViewPort()->getView() );
1122 double aXDelta = aStartPnt.X() - anEndPnt.X();
1123 double anYDelta = aStartPnt.Y() - anEndPnt.Y();
1125 CurveCreator_ICurve::SectionToPointCoordsList aCoordList;
1126 std::deque<float> aChangedPos;
1127 SectionToPointList::const_iterator anIt = myDragPoints.begin(), aLast = myDragPoints.end();
1128 for ( ; anIt != aLast; anIt++ ) {
1129 int aSectionId = anIt->first;
1130 int aPointId = anIt->second;
1131 aChangedPos = myCurve->getPoint( aSectionId, aPointId );
1132 if ( aChangedPos.size() < 2 )
1134 aChangedPos[0] = aChangedPos[0] - aXDelta;
1135 aChangedPos[1] = aChangedPos[1] - anYDelta;
1137 aCoordList.push_back(
1138 std::make_pair(std::make_pair( aSectionId, aPointId ),
1141 myCurve->setSeveralPoints( aCoordList );
1144 finishCurveModification( myDragPoints );
1147 void CurveCreator_Widget::updateLocalPointView()
1149 OCCViewer_Viewer* aViewer = getOCCViewer();
1152 Handle(AIS_InteractiveContext) aContext = aViewer->getAISContext();
1154 bool isBlocked = myLocalPointView->blockSignals(true);
1156 myLocalPointView->setRowCount( 0 );
1157 for ( aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected() ) {
1158 TopoDS_Vertex aVertex;
1159 TopoDS_Shape aShape = aContext->SelectedShape();
1160 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
1161 aVertex = TopoDS::Vertex( aContext->SelectedShape() );
1163 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
1164 if ( !anOwner.IsNull() ) {
1165 Handle(AIS_InteractiveObject) anAIS = Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
1166 if ( !anAIS.IsNull() ) {
1167 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast( anAIS);
1168 if ( !aPoint.IsNull() )
1169 aVertex = TopoDS::Vertex( aPoint->Vertex() );
1171 if ( aVertex.IsNull() ) {
1172 // the following happens if there are no points in the current curve, there is only a shape
1173 /*Handle(StdSelect_BRepOwner) aBrepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1174 if ( aBrepOwner.IsNull() )
1176 if ( aBrepOwner->HasShape() ) {
1177 const TopoDS_Shape& aShape = aBrepOwner->Shape();
1178 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
1180 aVertex = TopoDS::Vertex( aShape );
1185 if ( aVertex.IsNull() )
1187 aPnt = BRep_Tool::Pnt( aVertex );
1188 addLocalPointToTable( aPnt.X(), aPnt.Y() );
1191 myLocalPointView->blockSignals(isBlocked);
1197 void CurveCreator_Widget::setLocalPointContext( const bool theOpen, const bool isUpdateTable )
1199 OCCViewer_Viewer* aViewer = getOCCViewer();
1202 Handle(AIS_InteractiveContext) ic = aViewer->getAISContext();
1205 // Open local context if there is no one
1206 if ( !ic->HasOpenedContext() ) {
1207 ic->ClearCurrents( false );
1208 ic->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
1210 AIS_ListOfInteractive aList;
1211 ic->DisplayedObjects( aList );
1213 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
1216 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
1218 Handle(AIS_InteractiveObject) anAIS = it.Value();
1219 if ( !anAIS.IsNull() )
1221 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
1223 ic->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
1224 ic->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_WIRE ) );
1226 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
1228 ic->Load( anAIS, -1/*selection mode*/, false/*allow decomposition*/ );
1229 ic->Activate( anAIS, TopAbs_VERTEX );
1236 if ( ic->HasOpenedContext() )
1237 ic->CloseAllContexts();
1238 if ( isUpdateTable )
1239 updateLocalPointView();
1243 void CurveCreator_Widget::addLocalPointToTable( const double theX, const double theY )
1245 SectionToPointList aPoints;
1246 findSectionsToPoints( theX, theY, aPoints );
1248 SectionToPointList aSkipList;
1249 // table could not contain two equal value rows
1250 int aRowId = myLocalPointView->rowCount();
1251 double aCurrentX, aCurrentY;
1252 int aSectionId, aPointId;
1253 SectionToPoint aPoint;
1254 for ( int i = 0; i < aRowId; i++ ) {
1255 aCurrentX = myLocalPointView->item( i, 2 )->data( Qt::UserRole ).toDouble();
1256 aCurrentY = myLocalPointView->item( i, 3 )->data( Qt::UserRole ).toDouble();
1257 if ( fabs( aCurrentX - theX ) < LOCAL_SELECTION_TOLERANCE &&
1258 fabs( aCurrentY - theY ) < LOCAL_SELECTION_TOLERANCE ) {
1259 aPoint = std::make_pair<int, int>( getSectionId( i ), getPointId( i ) );
1260 if ( !contains( aSkipList, aPoint ) )
1261 aSkipList.push_back( aPoint );
1264 if ( aSkipList.size() == aPoints.size() )
1267 QTableWidgetItem* anItem;
1268 SectionToPointList::const_iterator anIt = aPoints.begin(), aLast = aPoints.end();
1269 for ( ; anIt != aLast; anIt++ ) {
1271 if ( contains( aSkipList, aPoint ) )
1274 myLocalPointView->setRowCount( aRowId+1 );
1275 aSectionId = aPoint.first;
1276 aPointId = aPoint.second;
1278 anItem = new QTableWidgetItem( myCurve->getSectionName( aSectionId ).c_str() );
1279 anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
1280 anItem->setData( Qt::UserRole, aSectionId );
1281 myLocalPointView->setItem( aRowId, 0, anItem );
1283 anItem = new QTableWidgetItem( QString::number( aPointId + 1 ) );
1284 anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
1285 anItem->setData( Qt::UserRole, aPointId );
1286 myLocalPointView->setItem( aRowId, 1, anItem );
1288 anItem = new QTableWidgetItem( QString::number( theX ) );
1289 anItem->setData( Qt::UserRole, theX );
1290 myLocalPointView->setItem( aRowId, 2, anItem );
1292 anItem = new QTableWidgetItem( QString::number( theY ) );
1293 anItem->setData( Qt::UserRole, theY );
1294 myLocalPointView->setItem( aRowId, 3, anItem );
1299 * Set drag operation started. Save the position and a list of dragged points
1300 * \param theState the drag operation state: started/finished
1301 * \param thePoint the start drag position
1303 void CurveCreator_Widget::setDragStarted( const bool theState, const QPoint& thePoint )
1305 OCCViewer_Viewer* aViewer = getOCCViewer();
1307 getSelectedPonts( myDragPoints );
1308 myDragStarted = myDragPoints.size();
1309 myDragStartPosition = thePoint;
1310 if ( aViewer && myDragStarted ) {
1311 // change a viewer interaction style in order to avoid a select rectangle build
1312 myDragInteractionStyle = aViewer->interactionStyle();
1313 aViewer->setInteractionStyle(SUIT_ViewModel::KEY_FREE);
1317 if ( aViewer && myDragStarted )
1318 aViewer->setInteractionStyle( myDragInteractionStyle );
1319 myDragStarted = false;
1320 myDragPoints.clear();
1325 void CurveCreator_Widget::getSelectedPonts( CurveCreator_Widget::SectionToPointList& thePoints )
1328 for ( int i = 0, aNb = myLocalPointView->rowCount(); i < aNb; i++ )
1329 thePoints.push_back( std::make_pair( getSectionId( i ), getPointId( i ) ) );
1332 void CurveCreator_Widget::setSelectedPonts( const CurveCreator_Widget::SectionToPointList& thePoints )
1334 OCCViewer_Viewer* aViewer = getOCCViewer();
1338 AIS_ListOfInteractive aListToSelect;
1340 Handle(AIS_InteractiveContext) ic = aViewer->getAISContext();
1341 if ( !ic->HasOpenedContext() )
1344 AIS_ListOfInteractive aDisplayedList;
1345 ic->DisplayedObjects( aDisplayedList );
1347 SectionToPointList::const_iterator anIt = thePoints.begin(), aLast = thePoints.end();
1348 SectionToPoint aSToPoint;
1349 for( ; anIt != aLast; anIt++ ) {
1352 for ( AIS_ListIteratorOfListOfInteractive it( aDisplayedList ); it.More(); it.Next() )
1354 Handle(AIS_InteractiveObject) anAIS = it.Value();
1355 if ( anAIS.IsNull() )
1357 Handle(AIS_Point) anAISPoint = Handle(AIS_Point)::DownCast( anAIS );
1358 if ( anAISPoint.IsNull() )
1361 TopoDS_Vertex aVertex = TopoDS::Vertex( anAISPoint->Vertex() );
1363 if ( aVertex.IsNull() )
1366 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
1368 SectionToPointList aPoints;
1369 findSectionsToPoints( aPnt.X(), aPnt.Y(), aPoints );
1371 SectionToPointList::const_iterator anIt = aPoints.begin(), aLast = aPoints.end();
1372 SectionToPoint aPoint;
1373 for ( ; anIt != aLast; anIt++ ) {
1375 if ( aPoint.first == aSToPoint.first && aPoint.second == aSToPoint.second )
1376 aListToSelect.Append( anAIS );
1381 ic->ClearSelected( Standard_False );
1382 aViewer->setObjectsSelected( aListToSelect );
1383 updateLocalPointView();
1387 * Get viewer information before perform the curve modification.
1388 * Take a list of selected cuve points an close local context.
1389 * The context should be closed because the curve presentation is
1390 * redisplayed and if it is not closed, when we close the local context
1391 * later, the presentation shown in the local context is disappeared.
1392 * \param thePoints an output list of curve selected points
1393 * \param theFillPoints a flag whether the selection list should be filled
1395 void CurveCreator_Widget::startCurveModification(
1396 CurveCreator_Widget::SectionToPointList& thePoints,
1397 const bool theFillPoints )
1399 if ( theFillPoints ) {
1401 getSelectedPonts( thePoints );
1403 setLocalPointContext( false );
1407 * Restore the viewer state after the curve modification is done.
1408 * Open local context and select given points inside it.
1409 * \param thePoints a list of curve selected points
1411 void CurveCreator_Widget::finishCurveModification(
1412 const CurveCreator_Widget::SectionToPointList& thePoints )
1414 if ( getActionMode() == ModificationMode )
1415 setLocalPointContext( true );
1416 setSelectedPonts( thePoints );
1421 * Returns a point index in the model curve by the point coordinates in the viewer
1422 * \param theX the X coordinate of the point
1423 * \param theY the Y coordinate of the point
1425 int CurveCreator_Widget::findLocalPointIndex( int theSectionId, float theX, float theY )
1429 CurveCreator::Coordinates aCoords;
1430 for ( int i = 0, aNb = myCurve->getNbPoints( theSectionId ); i < aNb && aPntIndex < 0; i++ ) {
1431 aCoords = myCurve->getPoint( theSectionId, i );
1432 if ( aCoords.size() < 2 )
1434 if ( fabs( aCoords[0] - theX ) < LOCAL_SELECTION_TOLERANCE &&
1435 fabs( aCoords[1] - theY ) < LOCAL_SELECTION_TOLERANCE )
1442 void CurveCreator_Widget::findSectionsToPoints( const double theX, const double theY,
1443 CurveCreator_Widget::SectionToPointList& thePoints )
1448 for ( int i = 0, aNb = myCurve->getNbSections(); i < aNb; i++ ) {
1449 aPointId = findLocalPointIndex( i, theX, theY );
1452 SectionToPoint aPoint = std::make_pair( i, aPointId );
1453 if ( !contains( thePoints, aPoint ) )
1454 thePoints.push_back( aPoint );
1458 void CurveCreator_Widget::convert( const SectionToPointList& thePoints,
1459 QMap<int, QList<int> >& theConvPoints )
1461 theConvPoints.clear();
1463 SectionToPointList::const_iterator anIt = thePoints.begin(), aLast = thePoints.end();
1465 int aSectionId, aPointId;
1466 for ( ; anIt != aLast; anIt++ ) {
1467 aSectionId = anIt->first;
1468 aPointId = anIt->second;
1470 if ( theConvPoints.contains( aSectionId ) )
1471 aPoints = theConvPoints[aSectionId];
1472 if ( aPoints.contains( aPointId ) )
1474 aPoints.append( aPointId );
1475 theConvPoints[aSectionId] = aPoints;
1480 * Checks whether the point belongs to the OCC object
1481 * \param theObject a line or shape with a bspline inside
1482 * \param theX the X coordinate in the view.
1483 * \param theY the Y coordinate in the view.
1484 * \param thePoint the output point to be append to the model curve
1485 * \param thePoint1 the output point to bound the line where a new point should be inserted
1486 * \param thePoint2 the output point to bound the line where a new point should be inserted
1488 bool CurveCreator_Widget::pointOnObject( Handle(AIS_InteractiveObject) theObject,
1489 const int theX, const int theY,
1491 gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
1493 bool isFound = false;
1495 OCCViewer_Viewer* aViewer = getOCCViewer();
1496 if ( theObject.IsNull() || !aViewer )
1500 Standard_Real aParameter;
1501 gp_Pnt aPnt1, aPnt2;
1502 Handle(AIS_Line) aLine = Handle(AIS_Line)::DownCast( theObject );
1503 if ( !aLine.IsNull() ) {
1504 const Handle(Geom_Line) aGLine = aLine->Line();
1505 isFound = hasProjectPointOnCurve( theX, theY, aGLine, aParameter );
1507 aPoint = aGLine->Value( aParameter );
1509 Handle(Geom_Point) aPStart;
1510 Handle(Geom_Point) aPEnd;
1511 aLine->Points( aPStart, aPEnd );
1512 aPnt1 = aPStart->Pnt();
1513 aPnt2 = aPEnd->Pnt();
1515 // in case of Geom line a projection is performed to the infinite line,
1516 // so it is necessary to bound it by the line size
1518 aLineBox.Set( aPnt1, gp_Vec( aPnt1, aPnt2 ) );
1519 isFound = !aLineBox.IsOut( aPoint );
1523 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
1524 if ( !aShape.IsNull() ) {
1525 const TopoDS_Wire& aWire = TopoDS::Wire( aShape->Shape() );
1526 if ( !aWire.IsNull() ) {
1527 TopExp_Explorer anExp( aWire, TopAbs_EDGE );
1528 for ( ; anExp.More(); anExp.Next())
1530 const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
1531 if ( !anEdge.IsNull() ) {
1532 Standard_Real aFirst, aLast;
1533 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
1535 if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
1536 Handle(Geom_BSplineCurve) aBSplineCurve = Handle(Geom_BSplineCurve)::DownCast( aCurve );
1537 if ( !aBSplineCurve.IsNull() ) {
1538 isFound = hasProjectPointOnCurve( theX, theY, aCurve, aParameter );
1540 aPoint = aBSplineCurve->Value( aParameter );
1541 Standard_Integer anI1, anI2;
1542 aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
1544 aPnt1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
1545 aPnt2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
1563 * Returns whether the clicked point belong to the curve or has a very near projection
1564 * \param theX the X coordinate of a point clicked in the OCC viewer
1565 * \param theY the Y coordinate of a point clicked in the OCC viewer
1566 * \param theCurve a geometry curve
1567 * \param theOutPoint a found projected point on the curve
1569 bool CurveCreator_Widget::hasProjectPointOnCurve( const int theX, const int theY,
1570 const Handle(Geom_Curve)& theCurve,
1571 Standard_Real& theParameter )
1573 bool isFound = false;
1574 OCCViewer_Viewer* aViewer = getOCCViewer();
1578 OCCViewer_ViewWindow* aWindow =
1579 (OCCViewer_ViewWindow*)aViewer->getViewManager()->getActiveView();
1580 Handle(V3d_View) aView = aWindow->getViewPort()->getView();
1581 gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, aView );
1583 GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
1584 Standard_Integer aNbPoint = aProj.NbPoints();
1586 for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
1587 gp_Pnt aNewPoint = aProj.Point( j );
1588 theParameter = aProj.Parameter( j );
1591 CurveCreator_Utils::ConvertPointToClick( aNewPoint, aView, aX, anY );
1593 int aXDelta = abs( aX - theX );
1594 int anYDelta = abs( anY - theY );
1595 isFound = aXDelta < SCENE_PIXEL_TOLERANCE && anYDelta < SCENE_PIXEL_TOLERANCE;
1602 * Returns a section index from the table
1603 * \param theRowId a table row
1605 int CurveCreator_Widget::getSectionId( const int theRowId ) const
1607 return myLocalPointView->item( theRowId, 0 )->data( Qt::UserRole ).toInt();
1611 * Returns a point index from the table
1612 * \param theRowId a table row
1614 int CurveCreator_Widget::getPointId( const int theRowId ) const
1616 return myLocalPointView->item( theRowId, 1 )->data( Qt::UserRole ).toInt();
1620 * Returns whethe the container has the value
1621 * \param theList a container of values
1622 * \param theValue a value
1624 bool CurveCreator_Widget::contains( const CurveCreator_Widget::SectionToPointList& theList,
1625 const CurveCreator_Widget::SectionToPoint& theValue ) const
1627 bool isFound = false;
1629 SectionToPointList::const_iterator anIt = theList.begin(), aLast = theList.end();
1630 for ( ; anIt != aLast && !isFound; anIt++ )
1631 isFound = anIt->first == theValue.first && anIt->second == theValue.second;