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 //#define USE_SEVERAL_POINTS
80 CurveCreator_Widget::CurveCreator_Widget(QWidget* parent,
81 CurveCreator_ICurve *theCurve,
83 : QWidget(parent), myNewSectionEditor(NULL), myCurve(theCurve), mySection(0),
84 myDragStarted( false ), myDragInteractionStyle( SUIT_ViewModel::STANDARD ),
87 myNewSectionEditor = new CurveCreator_NewSectionDlg( this );
88 myNewSectionEditor->hide();
89 connect( myNewSectionEditor, SIGNAL(addSection()), this, SLOT(onAddNewSection()) );
90 connect( myNewSectionEditor, SIGNAL(modifySection()), this, SLOT(onModifySection()) );
91 connect( myNewSectionEditor, SIGNAL(cancelSection()), this, SLOT(onCancelSection()) );
93 QGroupBox* aSectionGroup = new QGroupBox(tr("Sections"),this);
95 mySectionView = new CurveCreator_TreeView(myCurve, aSectionGroup);
96 connect( mySectionView, SIGNAL(selectionChanged()), this, SLOT( onSelectionChanged() ) );
97 connect( mySectionView, SIGNAL(sectionEntered(int)), this, SLOT(onEditSection(int)) );
98 connect( mySectionView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onContextMenu(QPoint)) );
100 myLocalPointView = new QTableWidget();
101 myLocalPointView->setVisible( false );
102 myLocalPointView->setColumnCount( 4 );
103 myLocalPointView->setColumnWidth( 0, SECTION_NAME_COLUMN_WIDTH );
104 myLocalPointView->setColumnWidth( 1, POINT_INDEX_COLUMN_WIDTH );
106 //aLabels << tr( "SECTION_LABEL" ) << tr( "IDENTIFIER_LABEL" ) << tr( "X_POSITION_LBL" ) << tr( "Y_POSITION_LBL" );
107 aLabels << tr( "Section" ) << "Index" << tr( "X" ) << tr( "Y" );
108 myLocalPointView->setHorizontalHeaderLabels( aLabels );
109 connect( myLocalPointView, SIGNAL( cellChanged( int, int ) ),
110 this, SLOT( onCellChanged( int, int ) ) );
112 QToolBar* aTB = new QToolBar(tr("TOOL_BAR_TLT"), aSectionGroup);
113 // QToolButton* anUndoBtn = new QToolButton(aTB);
115 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
116 QPixmap anUndoPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_UNDO")));
117 QPixmap aRedoPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_REDO")));
118 QPixmap aNewSectionPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_NEW_SECTION")));
119 QPixmap aNewPointPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_NEW_POINT")));
120 QPixmap anEditPointsPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_EDIT_POINTS")));
121 QPixmap aDetectPointsPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_EDIT_POINTS")));
122 QPixmap aPolylinePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_POLYLINE")));
123 QPixmap aSplinePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_SPLINE")));
124 QPixmap aRemovePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_DELETE")));
125 QPixmap aJoinPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_JOIN")));
126 QPixmap aStepUpPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_ARROW_UP")));
127 QPixmap aStepDownPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_ARROW_DOWN")));
129 QAction* anAct = createAction( UNDO_ID, tr("UNDO"), anUndoPixmap, tr("UNDO_TLT"),
130 QKeySequence(Qt::ControlModifier|Qt::Key_Z) );
131 connect(anAct, SIGNAL(triggered()), this, SLOT(onUndo()) );
132 aTB->addAction(anAct);
134 anAct = createAction( REDO_ID, tr("REDO"), aRedoPixmap, tr("REDO_TLT"),
135 QKeySequence(Qt::ControlModifier|Qt::Key_Y) );
136 connect(anAct, SIGNAL(triggered()), this, SLOT(onRedo()) );
137 aTB->addAction(anAct);
141 anAct = createAction( NEW_SECTION_ID, tr("NEW_SECTION"), aNewSectionPixmap, tr("NEW_SECTION_TLT"),
142 QKeySequence(Qt::ControlModifier|Qt::Key_N) );
143 connect(anAct, SIGNAL(triggered()), this, SLOT(onNewSection()) );
144 aTB->addAction(anAct);
147 anAct = createAction( ADDITION_MODE_ID, tr("ADDITION_MODE"), aNewPointPixmap, tr("ADDITION_MODE_TLT"),
149 anAct->setCheckable(true);
150 connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onAdditionMode(bool)) );
151 connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
152 aTB->addAction(anAct);
154 anAct = createAction( MODIFICATION_MODE_ID, tr("MODIFICATION_MODE"), anEditPointsPixmap, tr("MODIFICATION_MODE_TLT"),
156 anAct->setCheckable(true);
157 connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onModificationMode(bool)) );
158 connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
159 aTB->addAction(anAct);
161 anAct = createAction( DETECTION_MODE_ID, tr("DETECTION_MODE"), aDetectPointsPixmap, tr("DETECTION_MODE_TLT"),
163 anAct->setCheckable(true);
164 connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onDetectionMode(bool)) );
165 connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
166 aTB->addAction(anAct);
168 anAct = createAction( CLOSE_SECTIONS_ID, tr("CLOSE_SECTIONS"), QPixmap(), tr("CLOSE_SECTIONS_TLT"),
169 QKeySequence(Qt::ControlModifier|Qt::Key_W) );
170 connect(anAct, SIGNAL(triggered()), this, SLOT(onCloseSections()) );
172 anAct = createAction( UNCLOSE_SECTIONS_ID, tr("UNCLOSE_SECTIONS"), QPixmap(),
173 tr("UNCLOSE_SECTIONS_TLT"), QKeySequence(Qt::ControlModifier|Qt::Key_S) );
174 connect(anAct, SIGNAL(triggered()), this, SLOT(onUncloseSections()) );
176 anAct = createAction( SET_SECTIONS_POLYLINE_ID, tr("SET_SECTIONS_POLYLINE"),
177 aPolylinePixmap, tr("SET_POLYLINE_TLT"),
178 QKeySequence(Qt::ControlModifier|Qt::Key_E) );
179 connect(anAct, SIGNAL(triggered()), this, SLOT(onSetPolyline()) );
181 anAct = createAction( SET_SECTIONS_SPLINE_ID, tr("SET_SECTIONS_SPLINE"), aSplinePixmap,
182 tr("SET_SPLINE_TLT"), QKeySequence(Qt::ControlModifier|Qt::Key_R) );
183 connect(anAct, SIGNAL(triggered()), this, SLOT(onSetSpline()) );
185 anAct = createAction( REMOVE_ID, tr("REMOVE"), aRemovePixmap, tr("REMOVE_TLT"),
186 QKeySequence(Qt::ControlModifier|Qt::Key_Delete ) );
187 connect(anAct, SIGNAL(triggered()), this, SLOT(onRemove()) );
188 aTB->addAction(anAct);
191 anAct = createAction( JOIN_ID, tr("JOIN"), aJoinPixmap, tr("JOIN_TLT"),
192 QKeySequence(Qt::ControlModifier|Qt::Key_Plus ) );
193 connect( anAct, SIGNAL(triggered()), this, SLOT(onJoin()) );
194 aTB->addAction(anAct);
197 anAct = createAction( CLEAR_ALL_ID, tr("CLEAR_ALL"), QPixmap(), tr("CLEAR_ALL_TLT"),
198 QKeySequence(Qt::ControlModifier | Qt::ShiftModifier | Qt::Key_Delete ) );
199 connect( anAct, SIGNAL(triggered()), this, SLOT( onClearAll()) );
201 anAct = createAction( JOIN_ALL_ID, tr("JOIN_ALL"), QPixmap(), tr("JOIN_ALL_TLT"),
202 QKeySequence(Qt::ControlModifier | Qt::ShiftModifier | Qt::Key_Plus ) );
203 connect( anAct, SIGNAL(triggered()), this, SLOT(onJoinAll()) );
205 QVBoxLayout* aSectLayout = new QVBoxLayout();
206 aSectLayout->setMargin( 5 );
207 aSectLayout->setSpacing( 5 );
208 aSectLayout->addWidget(aTB);
209 aSectLayout->addWidget(mySectionView);
210 aSectLayout->addWidget( myLocalPointView );
211 aSectionGroup->setLayout(aSectLayout);
212 QVBoxLayout* aLay = new QVBoxLayout();
213 aLay->setMargin( 0 );
214 aLay->setSpacing( 5 );
215 // aLay->addLayout(aNameLayout);
216 aLay->addWidget(aSectionGroup);
218 onSelectionChanged();
224 void CurveCreator_Widget::setOCCViewer( OCCViewer_Viewer* theViewer )
226 if ( myOCCViewer == theViewer )
230 OCCViewer_ViewManager* aViewManager = dynamic_cast<OCCViewer_ViewManager*>
231 ( myOCCViewer->getViewManager() );
232 disconnect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
233 this, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ) );
234 disconnect( aViewManager, SIGNAL( mouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ),
235 this, SLOT( onMouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ) );
236 disconnect( aViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
237 this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
238 // all local contexts should be closed if the viewer is not more used
239 setLocalPointContext( false, true );
242 myOCCViewer = theViewer;
244 OCCViewer_ViewManager* aViewManager = dynamic_cast<OCCViewer_ViewManager*>
245 ( myOCCViewer->getViewManager() );
246 connect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
247 this, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ) );
248 connect( aViewManager, SIGNAL( mouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ),
249 this, SLOT( onMouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ) );
250 connect( aViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
251 this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
256 * Returns current OCC viewer
258 OCCViewer_Viewer* CurveCreator_Widget::getOCCViewer()
263 //=======================================================================
264 // function: getUniqSectionName
265 // purpose: return unique section name
266 //=======================================================================
267 std::string CurveCreator_Widget::getUniqSectionName( CurveCreator_ICurve* theCurve ) const
269 for( int i = 0 ; i < 1000000 ; i++ ){
271 sprintf( aBuffer, "Section_%d", i+1 );
272 std::string aName(aBuffer);
274 for( j = 0 ; j < theCurve->getNbSections() ; j++ ){
275 if( theCurve->getSectionName(j) == aName )
278 if( j == theCurve->getNbSections() )
284 void CurveCreator_Widget::setCurve( CurveCreator_ICurve* theCurve )
287 mySectionView->setCurve(myCurve);
288 onSelectionChanged();
292 void CurveCreator_Widget::onSelectionChanged()
294 QList<ActionId> anEnabledAct;
296 anEnabledAct << NEW_SECTION_ID << MODIFICATION_MODE_ID;
297 if ( removeEnabled() )
298 anEnabledAct << REMOVE_ID;
299 QList<int> aSelSections = mySectionView->getSelectedSections();
300 QList< QPair< int, int > > aSelPoints = mySectionView->getSelectedPoints();
301 CurveCreator_TreeView::SelectionType aSelType = mySectionView->getSelectionType();
303 case CurveCreator_TreeView::ST_NOSEL:{
306 case CurveCreator_TreeView::ST_SECTIONS:{
307 /*if( aSelSections[0] > 0 ){
308 anEnabledAct << UP_ID;
310 if( aSelSections.size() == 1 ){
311 anEnabledAct << ADDITION_MODE_ID << DETECTION_MODE_ID;
313 switch ( getActionMode() ) {
317 QList<int> aSelSection = mySectionView->getSelectedSections();
318 if( aSelSection.size() > 0 ){
319 mySection = aSelSection[0];
320 myPointNum = myCurve->getNbPoints(mySection);
324 case ModificationMode: {
325 anEnabledAct << CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID << SET_SECTIONS_POLYLINE_ID << SET_SECTIONS_SPLINE_ID;
326 int aSectCnt = myCurve->getNbSections();
328 anEnabledAct << CLEAR_ALL_ID;
330 anEnabledAct << JOIN_ALL_ID;
331 if( aSelSections.size() > 1 ){
332 anEnabledAct << JOIN_ID;
336 case DetectionMode: {
343 /*if( aSelSections[ aSelSections.size() - 1 ] < ( myCurve->getNbSections() - 1 ) ){
344 anEnabledAct << DOWN_ID;
348 /*case CurveCreator_TreeView::ST_POINTS_ONE_SECTION:{
349 if( aSelPoints[0].second > 0 ){
350 anEnabledAct << UP_ID;
352 int aLastIndex = aSelPoints.size()-1;
353 int aSect = aSelPoints[0].first;
354 if( aSelPoints[aLastIndex].second < (myCurve->getNbPoints(aSect) - 1)){
355 anEnabledAct << DOWN_ID;
357 if( aSelPoints.size() == 1){
358 anEnabledAct << INSERT_POINT_BEFORE_ID << INSERT_POINT_AFTER_ID;
365 /*int aSelObjsCnt = aSelPoints.size() + aSelSections.size();
366 if( aSelObjsCnt > 0 ){
367 anEnabledAct << REMOVE_ID;
369 if( (myCurve->getNbSections() + myCurve->getNbPoints()) > 0 ){
370 anEnabledAct << REMOVE_ALL_ID;
372 if( myCurve->getNbSections() > 1 ){
373 anEnabledAct << JOIN_ALL_ID;
376 QList<ActionId> anIds = myActionMap.keys();
377 for( int i = 0 ; i < anIds.size() ; i++ ){
378 if( myActionMap.contains(anIds[i]) ){
379 if( anEnabledAct.contains(anIds[i]) ){
380 myActionMap[anIds[i]]->setEnabled(true);
383 myActionMap[anIds[i]]->setEnabled(false);
388 emit selectionChanged();
391 void CurveCreator_Widget::onAdditionMode(bool checked)
393 OCCViewer_Viewer* aViewer = getOCCViewer();
394 if( !myCurve || !aViewer )
397 Handle(AIS_InteractiveContext) aContext = aViewer->getAISContext();
398 OCCViewer_ViewManager* aViewManager = dynamic_cast<OCCViewer_ViewManager*>
399 (aViewer->getViewManager());
400 // if ( aViewManager->getType() == OCCViewer_Viewer::Type() ) {
403 /* myGuiState = aViewWindow->saveState();
404 aViewer->enableMultiselection(false);
405 aViewer->enableSelection(false);*/
407 /* aViewer->enableMultiselection(true);
408 aViewer->enableSelection(true);
409 aViewWindow->restoreState( myGuiState );*/
416 QList<int> aSelSection = mySectionView->getSelectedSections();
417 if( aSelSection.size() > 0 ){
418 mySection = aSelSection[0];
421 QList< QPair<int,int> > aSelPoints = mySectionView->getSelectedPoints();
422 if( aSelPoints.size() > 0 ){
423 mySection = aSelPoints[0].first;
424 myPointNum = aSelPoints[0].second + 1;
427 // emit subOperationStarted( myNewPointEditor );
430 void CurveCreator_Widget::onModificationMode(bool checked)
432 myLocalPointView->setVisible( checked );
435 void CurveCreator_Widget::onDetectionMode(bool checked)
439 void CurveCreator_Widget::onModeChanged(bool checked)
441 ActionMode aMode = NoneMode;
443 QAction* anAction = (QAction*)sender();
444 switch(myActionMap.key(anAction)) {
445 case ADDITION_MODE_ID:
446 aMode = AdditionMode;
447 if (myActionMap[MODIFICATION_MODE_ID]->isChecked())
448 myActionMap[MODIFICATION_MODE_ID]->trigger();
449 else if (myActionMap[DETECTION_MODE_ID]->isChecked())
450 myActionMap[DETECTION_MODE_ID]->trigger();
452 case MODIFICATION_MODE_ID:
453 aMode = ModificationMode;
454 if (myActionMap[ADDITION_MODE_ID]->isChecked())
455 myActionMap[ADDITION_MODE_ID]->trigger();
456 else if (myActionMap[DETECTION_MODE_ID]->isChecked())
457 myActionMap[DETECTION_MODE_ID]->trigger();
459 case DETECTION_MODE_ID:
460 aMode = DetectionMode;
461 if (myActionMap[ADDITION_MODE_ID]->isChecked())
462 myActionMap[ADDITION_MODE_ID]->trigger();
463 else if (myActionMap[MODIFICATION_MODE_ID]->isChecked())
464 myActionMap[MODIFICATION_MODE_ID]->trigger();
468 onSelectionChanged();
469 setLocalPointContext( aMode == ModificationMode, true );
472 void CurveCreator_Widget::onAddNewPoint(const CurveCreator::Coordinates& theCoords)
476 //myCurve->addPoints(theCoords, mySection, myPointNum );
477 //mySectionView->pointsAdded( mySection, myPointNum );
479 QList<int> aSections = mySectionView->getSelectedSections();
480 if( aSections.size() == 0 ){
483 int aSection = aSections[0];
484 myCurve->addPoints(theCoords, aSection); // add to the end of section
485 mySectionView->pointsAdded( aSection, myCurve->getNbPoints( aSection ) );
486 onSelectionChanged();
490 void CurveCreator_Widget::onNewSection()
494 myNewSectionEditor->clear();
495 myNewSectionEditor->setEditMode(false);
496 QString aSectName = QString( getUniqSectionName(myCurve).c_str() );
497 myNewSectionEditor->setSectionParameters(aSectName, true, CurveCreator::Polyline );
498 emit subOperationStarted( myNewSectionEditor );
501 void CurveCreator_Widget::onAddNewSection()
505 myCurve->addSection( myNewSectionEditor->getName().toStdString(), myNewSectionEditor->getSectionType(),
506 myNewSectionEditor->isClosed() );
507 mySectionView->sectionAdded( -1 ); // add a new section to the end of list
508 QString aNewName = QString(getUniqSectionName(myCurve).c_str());
509 myNewSectionEditor->setSectionName(aNewName);
510 onSelectionChanged();
515 void CurveCreator_Widget::onCancelSection()
517 emit subOperationFinished( myNewSectionEditor );
520 QAction* CurveCreator_Widget::createAction( ActionId theId, const QString& theName, const QPixmap& theImage,
521 const QString& theToolTip, const QKeySequence& theShortcut )
523 QAction* anAct = new QAction(theName,this);
524 if( !theImage.isNull() ){
525 anAct->setIcon(theImage);
527 anAct->setShortcut(theShortcut);
528 anAct->setToolTip(theToolTip);
529 myActionMap[theId] = anAct;
533 QAction* CurveCreator_Widget::getAction(ActionId theId)
535 if( myActionMap.contains(theId) )
536 return myActionMap[theId];
540 CurveCreator_Widget::ActionMode CurveCreator_Widget::getActionMode() const
542 ActionMode aMode = NoneMode;
544 if ( myActionMap[ADDITION_MODE_ID]->isChecked() )
545 aMode = AdditionMode;
546 else if ( myActionMap[MODIFICATION_MODE_ID]->isChecked() )
547 aMode = ModificationMode;
548 else if ( myActionMap[DETECTION_MODE_ID]->isChecked() )
549 aMode = DetectionMode;
554 void CurveCreator_Widget::onEditSection( int theSection )
558 mySection = theSection;
559 QString aSectName = QString::fromStdString( myCurve->getSectionName(theSection));
560 bool isClosed = myCurve->isClosed(theSection);
561 CurveCreator::SectionType aType = myCurve->getSectionType(theSection);
562 myNewSectionEditor->setEditMode(true);
563 myNewSectionEditor->setSectionParameters( aSectName, isClosed, aType );
565 emit subOperationStarted( myNewSectionEditor );
568 void CurveCreator_Widget::onModifySection()
572 QString aName = myNewSectionEditor->getName();
573 bool isClosed = myNewSectionEditor->isClosed();
574 CurveCreator::SectionType aSectType = myNewSectionEditor->getSectionType();
575 // myCurve->startOperation();
576 myCurve->setClosed( isClosed, mySection );
577 myCurve->setSectionName( mySection , aName.toStdString() );
578 myCurve->setSectionType( mySection, aSectType );
579 // myCurve->finishOperation();
580 mySectionView->sectionChanged(mySection);
585 /*void CurveCreator_Widget::onEditPoint( int theSection, int thePoint )
587 if( !myNewPointEditor || !myEdit )
589 mySection = theSection;
590 myPointNum = thePoint;
591 QString aSectName = QString::fromStdString( myCurve->getSectionName(theSection));
592 myNewPointEditor->setEditMode(true);
593 myNewPointEditor->setSectionName(aSectName);
594 myNewPointEditor->setDimension( myCurve->getDimension() );
595 CurveCreator::Coordinates aCoords = myCurve->getCoordinates(theSection,thePoint);
596 myNewPointEditor->setCoordinates(aCoords);
597 emit subOperationStarted( myNewPointEditor );
600 void CurveCreator_Widget::onModifyPoint()
604 CurveCreator::Coordinates aCoords = myNewPointEditor->getCoordinates();
605 myEdit->setCoordinates( aCoords, mySection, myPointNum );
606 mySectionView->pointDataChanged( mySection, myPointNum );
611 void CurveCreator_Widget::onJoin()
615 QList<int> aSections = mySectionView->getSelectedSections();
616 if( aSections.size() == 0 ){
619 int aMainSect = aSections[0];
620 int aMainSectSize = myCurve->getNbPoints(aMainSect);
621 // myCurve->startOperation();
622 for( int i = 1 ; i < aSections.size() ; i++ ){
623 int aSectNum = aSections[i] - (i-1);
624 myCurve->join( aMainSect, aSectNum );
625 mySectionView->sectionsRemoved( aSectNum );
627 // myCurve->finishOperation();
628 int aNewSectSize = myCurve->getNbPoints(aMainSect);
629 if( aNewSectSize != aMainSectSize )
630 mySectionView->pointsAdded( aMainSect, aMainSectSize, aNewSectSize-aMainSectSize );
634 void CurveCreator_Widget::onRemove()
639 switch( getActionMode() ) {
643 case ModificationMode:
651 void CurveCreator_Widget::onClearAll()
656 mySectionView->reset();
657 onSelectionChanged();
661 void CurveCreator_Widget::onJoinAll()
666 mySectionView->reset();
667 onSelectionChanged();
671 void CurveCreator_Widget::onUndoSettings()
676 void CurveCreator_Widget::onSetSpline()
680 QList<int> aSelSections = mySectionView->getSelectedSections();
681 // myCurve->startOperation();
682 for( int i = 0 ; i < aSelSections.size() ; i++ ){
683 myCurve->setSectionType(aSelSections[i], CurveCreator::Spline );
684 mySectionView->sectionChanged(aSelSections[i]);
686 // myCurve->finishOperation();
690 void CurveCreator_Widget::onSetPolyline()
694 // myCurve->startOperation();
695 QList<int> aSelSections = mySectionView->getSelectedSections();
696 for( int i = 0 ; i < aSelSections.size() ; i++ ){
697 myCurve->setSectionType( aSelSections[i], CurveCreator::Polyline );
698 mySectionView->sectionChanged( aSelSections[i] );
700 // myCurve->finishOperation();
704 void CurveCreator_Widget::onCloseSections()
708 // myCurve->startOperation();
709 QList<int> aSelSections = mySectionView->getSelectedSections();
710 for( int i = 0 ; i < aSelSections.size() ; i++ ){
711 myCurve->setClosed(true, aSelSections[i]);
712 mySectionView->sectionChanged(aSelSections[i]);
714 // myCurve->finishOperation();
718 void CurveCreator_Widget::onUncloseSections()
722 // myCurve->startOperation();
723 QList<int> aSelSections = mySectionView->getSelectedSections();
724 for( int i = 0 ; i < aSelSections.size() ; i++ ){
725 myCurve->setClosed(false, aSelSections[i]);
726 mySectionView->sectionChanged(aSelSections[i]);
728 // myCurve->finishOperation();
732 void CurveCreator_Widget::onUndo()
737 CurveCreator_Widget::SectionToPointList aPoints;
738 startCurveModification( aPoints, false );
740 finishCurveModification();
741 mySectionView->reset();
745 void CurveCreator_Widget::onRedo()
749 CurveCreator_Widget::SectionToPointList aPoints;
750 startCurveModification( aPoints, false );
752 finishCurveModification();
753 mySectionView->reset();
757 void CurveCreator_Widget::updateUndoRedo()
761 QAction* anAct = myActionMap[UNDO_ID];
763 if( myCurve->getNbUndo() != 0 ){
764 anAct->setEnabled(true);
767 anAct->setDisabled(true);
770 anAct = myActionMap[REDO_ID];
772 if( myCurve->getNbRedo() != 0 ){
773 anAct->setEnabled(true);
776 anAct->setDisabled(true);
781 void CurveCreator_Widget::onContextMenu( QPoint thePoint )
783 QList<ActionId> aContextActions;
784 aContextActions << CLEAR_ALL_ID << JOIN_ALL_ID << SEPARATOR_ID <<
785 CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID << SET_SECTIONS_POLYLINE_ID <<
786 SET_SECTIONS_SPLINE_ID;
787 QPoint aGlPoint = mySectionView->mapToGlobal(thePoint);
789 QList<ActionId> aResAct;
790 for( int i = 0 ; i < aContextActions.size() ; i++ ){
791 if( aContextActions[i] != SEPARATOR_ID ){
792 if( myActionMap.contains(aContextActions[i]) ){
793 QAction* anAct = myActionMap[aContextActions[i]];
794 if( anAct->isEnabled() ){
795 aResAct << aContextActions[i];
801 aResAct << SEPARATOR_ID;
807 QMenu* aMenu = new QMenu(this);
808 for( int i = 0 ; i < aResAct.size() ; i++ ){
809 if( aResAct[i] == SEPARATOR_ID ){
810 aMenu->addSeparator();
813 QAction* anAct = myActionMap[aResAct[i]];
814 aMenu->insertAction(NULL, anAct);
817 aMenu->exec(aGlPoint);
820 QList<int> CurveCreator_Widget::getSelectedSections()
822 return mySectionView->getSelectedSections();
825 QList< QPair< int, int > > CurveCreator_Widget::getSelectedPoints()
827 return mySectionView->getSelectedPoints();
831 * According to the widget state, performs the remove action
833 void CurveCreator_Widget::removeSelected()
839 * Checks whether there are some selection to be removed
841 bool CurveCreator_Widget::removeEnabled()
843 bool isEnabled = getActionMode() == ModificationMode;
845 QList<int> aSelSections = mySectionView->getSelectedSections();
846 CurveCreator_TreeView::SelectionType aSelType = mySectionView->getSelectionType();
847 isEnabled = aSelType == CurveCreator_TreeView::ST_SECTIONS &&
848 aSelSections.size() == 1;
854 //=================================================================================
855 // function : GeometryGUI::onGetCoordsByClick()
856 // purpose : Manage mouse press events in Additon mode
857 //=================================================================================
858 void CurveCreator_Widget::onGetCoordsByClick( QMouseEvent* pe )
860 if (pe->button() != Qt::LeftButton)
863 if ( pe->modifiers() != Qt::ControlModifier ) {
864 OCCViewer_Viewer* aViewer = getOCCViewer();
867 Handle(AIS_InteractiveContext) ic = aViewer->getAISContext();
872 if ( pe->modifiers() == Qt::ShiftModifier )
873 ic->ShiftSelect(); // Append selection
875 ic->Select(); // New selection
877 /*TopoDS_Shape aShape;
880 if ( ic->MoreSelected() )
881 aShape = ic->SelectedShape();
883 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
884 aPnt = BRep_Tool::Pnt( TopoDS::Vertex( ic->SelectedShape() ) );
887 OCCViewer_ViewPort3d* vp =
888 ((OCCViewer_ViewWindow*)aViewer->getViewManager()->getActiveView())->getViewPort();
889 aPnt = CurveCreator_Utils::ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
891 // set the coordinates into dialog
892 CurveCreator::Coordinates aCoords;
893 aCoords.push_back( aPnt.X() );
894 aCoords.push_back( aPnt.Y() );
895 if ( myCurve->getDimension() == 3 ) {
896 aCoords.push_back( aPnt.Z() );
898 onAddNewPoint(aCoords);
899 // myNewPointEditor->setCoordinates( aCoords );
903 //=================================================================================
904 // function : CurveCreator_Widget::onMousePress()
905 // purpose : Manage mouse press events in Modification mode
906 //=================================================================================
907 void CurveCreator_Widget::onMousePress( SUIT_ViewWindow*, QMouseEvent* theEvent )
909 if ( theEvent->button() != Qt::LeftButton )
912 switch( getActionMode() ) {
913 case ModificationMode: {
914 //store initial cursor position for Drag&Drop
915 setDragStarted( true, theEvent->pos() );
919 onGetCoordsByClick( theEvent );
927 //=================================================================================
928 // function : HYDROGUI_PolylineOp::onMouseRelease()
929 // purpose : Manage mouse press events in Modification mode
930 //=================================================================================
931 void CurveCreator_Widget::onMouseRelease( SUIT_ViewWindow*, QMouseEvent* theEvent )
933 if ( getActionMode() != ModificationMode )
936 if ( myDragStarted ) {
937 if ( myDragged ) // if the drag of some points has happened, restore the drag selection
938 setSelectedPonts( myDragPoints );
939 setDragStarted( false );
941 else // check whether the segment is clicked an a new point should be added to the segment
942 insertPointToSelectedSegment( theEvent->pos().x(), theEvent->pos().y() );
944 // updates the input panel table to show the selected point coordinates
945 updateLocalPointView();
948 //=================================================================================
949 // function : GeometryGUI::onMouseMove()
950 // purpose : Manage mouse move events in Modification mode
951 //=================================================================================
952 void CurveCreator_Widget::onMouseMove( SUIT_ViewWindow*, QMouseEvent* theEvent )
954 if ( getActionMode() != ModificationMode || !myDragStarted )
957 QPoint aPos = theEvent->pos();
958 if ( (aPos - myDragStartPosition).manhattanLength() < QApplication::startDragDistance() )
961 moveSelectedPoints( aPos.x(), aPos.y() );
962 myDragStartPosition = aPos;
965 void CurveCreator_Widget::onCellChanged( int theRow, int theColumn )
967 int aCurrSect = getSectionId( theRow );
968 int aPntIndex = getPointId( theRow );
973 SectionToPointList aSelPoints;
974 startCurveModification( aSelPoints );
976 double aX = myLocalPointView->item( theRow, 2 )->text().toDouble();
977 double anY = myLocalPointView->item( theRow, 3 )->text().toDouble();
978 std::deque<float> aChangedPos;
979 aChangedPos.push_back( aX );
980 aChangedPos.push_back( anY );
981 myCurve->setPoint( aCurrSect, aPntIndex, aChangedPos );
983 finishCurveModification( aSelPoints );
987 * Removes a selected section from the curve. Updates undo/redo status
989 void CurveCreator_Widget::removeSection()
991 QList< QPair<int,int> > aSelPoints = mySectionView->getSelectedPoints();
994 // myCurve->startOperation();
995 for( int i = 0 ; i < aSelPoints.size() ; i++ ){
996 if( aCurrSect != aSelPoints[i].first ){
998 aCurrSect = aSelPoints[i].first;
1000 int aPntIndx = aSelPoints[i].second - aRemoveCnt;
1001 myCurve->removePoint( aCurrSect, aPntIndx );
1002 mySectionView->pointsRemoved( aCurrSect, aPntIndx );
1005 QList<int> aSections = mySectionView->getSelectedSections();
1006 for( int i = 0 ; i < aSections.size() ; i++ ){
1007 int aSectNum = aSections[i] - (i);
1008 myCurve->removeSection( aSectNum );
1009 mySectionView->sectionsRemoved( aSectNum );
1011 // myCurve->finishOperation();
1012 mySectionView->clearSelection();
1017 * Removes a selected points from the curve. Updates undo/redo status
1019 void CurveCreator_Widget::removePoint()
1021 SectionToPointList aPoints;
1022 getSelectedPonts( aPoints );
1023 if ( aPoints.size() == 0 )
1026 SectionToPointList aSelPoints;
1027 startCurveModification( aSelPoints, false );
1029 #ifdef USE_SEVERAL_POINTS
1030 myCurve->removeSeveralPoints( aPoints );
1032 // the points should be removed in a decreased order
1033 QMap<int, QList<int> > aConvPoints;
1034 convert( aPoints, aConvPoints );
1035 QMap<int, QList<int> >::const_iterator anIt = aConvPoints.begin(),
1036 aLast = aConvPoints.end();
1037 for ( ; anIt != aLast; anIt++ ) {
1038 int aSectionId = anIt.key();
1040 QList<int> aSectionPoints = anIt.value();
1041 qSort( aSectionPoints );
1042 for( int i = aSectionPoints.size()-1; i >= 0; i-- ){
1043 int aPntIndx = aSectionPoints[i];
1044 myCurve->removePoint( aSectionId, aPntIndx );
1045 mySectionView->pointsRemoved( aSectionId, aPntIndx );
1049 finishCurveModification( SectionToPointList() );
1052 void CurveCreator_Widget::insertPointToSelectedSegment( const int theX,
1055 OCCViewer_Viewer* aViewer = getOCCViewer();
1060 gp_Pnt aPoint1, aPoint2;
1061 bool isFoundPoint = false;
1062 Handle(AIS_InteractiveContext) aContext = aViewer->getAISContext();
1063 for ( aContext->InitSelected(); aContext->MoreSelected() && !isFoundPoint;
1064 aContext->NextSelected() ) {
1065 TopoDS_Shape aTShape = aContext->SelectedShape();
1066 if ( !aTShape.IsNull() && aTShape.ShapeType() == TopAbs_VERTEX )
1069 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
1070 if ( anOwner.IsNull() )
1072 const TopLoc_Location& aLocation = anOwner->Location();
1073 Handle(AIS_InteractiveObject) anAIS = Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
1074 isFoundPoint = pointOnObject( anAIS, theX, theY, aPoint, aPoint1, aPoint2 );
1077 if ( !isFoundPoint )
1080 // insert the point to the model curve
1081 SectionToPointList aSelPoints;
1082 startCurveModification( aSelPoints );
1084 CurveCreator::Coordinates aCoords;
1085 aCoords.push_back( aPoint.X() );
1086 aCoords.push_back( aPoint.Y() );
1088 SectionToPointList aPoints1, aPoints2;
1089 findSectionsToPoints( aPoint1.X(), aPoint1.Y(), aPoints1 );
1090 findSectionsToPoints( aPoint2.X(), aPoint2.Y(), aPoints2 );
1091 SectionToPointList::const_iterator anIt = aPoints1.begin(), aLast = aPoints1.end();
1092 int aSectionId = -1;
1093 // there can be a case when a new point is added into two sections
1094 int aPoint1Id = -1, aPoint2Id = -1;
1095 for ( ; anIt != aLast && aSectionId < 0; anIt++ ) {
1096 int aSectionCur = anIt->first;
1097 SectionToPointList::const_iterator anIt2 = aPoints2.begin(), aLast2 = aPoints2.end();
1098 for ( ; anIt2 != aLast2 && aSectionId < 0; anIt2++ ) {
1099 if ( anIt2->first == aSectionCur ) {
1100 aSectionId = aSectionCur;
1101 aPoint1Id = anIt->second;
1102 aPoint2Id = anIt2->second;
1107 int anInsertPos = -1;
1108 int aLastPoint = myCurve->getNbPoints( aSectionId )-1;
1109 if ( ( aPoint1Id == aLastPoint && aPoint2Id == 0 ) ||
1110 ( aPoint2Id == aLastPoint && aPoint1Id == 0 ) )
1111 anInsertPos = -1; // if the section happens between first and last points
1113 anInsertPos = aPoint1Id < aPoint2Id ? aPoint1Id + 1 : aPoint2Id + 1;
1115 myCurve->addPoints( aCoords, aSectionId, anInsertPos );
1116 mySectionView->pointsAdded( aSectionId, myCurve->getNbPoints( aSectionId ) );
1118 finishCurveModification( aSelPoints );
1124 void CurveCreator_Widget::moveSelectedPoints( const int theXPosition,
1125 const int theYPosition )
1127 OCCViewer_Viewer* aViewer = getOCCViewer();
1131 SectionToPointList aPoints;
1132 startCurveModification( aPoints, false );
1134 OCCViewer_ViewWindow* aWindow =
1135 (OCCViewer_ViewWindow*)aViewer->getViewManager()->getActiveView();
1136 gp_Pnt aStartPnt = CurveCreator_Utils::ConvertClickToPoint( myDragStartPosition.x(),
1137 myDragStartPosition.y(),
1138 aWindow->getViewPort()->getView() );
1139 gp_Pnt anEndPnt = CurveCreator_Utils::ConvertClickToPoint( theXPosition, theYPosition,
1140 aWindow->getViewPort()->getView() );
1141 double aXDelta = aStartPnt.X() - anEndPnt.X();
1142 double anYDelta = aStartPnt.Y() - anEndPnt.Y();
1144 CurveCreator_ICurve::SectionToPointCoordsList aCoordList;
1145 std::deque<float> aChangedPos;
1146 SectionToPointList::const_iterator anIt = myDragPoints.begin(), aLast = myDragPoints.end();
1147 for ( ; anIt != aLast; anIt++ ) {
1148 aChangedPos = myCurve->getPoint( anIt->first, anIt->second );
1149 if ( aChangedPos.size() < 2 )
1151 aChangedPos[0] = aChangedPos[0] - aXDelta;
1152 aChangedPos[1] = aChangedPos[1] - anYDelta;
1153 #ifndef USE_SEVERAL_POINTS
1154 myCurve->setPoint( anIt->first, anIt->second, aChangedPos );
1157 #ifdef USE_SEVERAL_POINTS
1158 myCurve->setSeveralPoints( aCoordList );
1162 finishCurveModification( myDragPoints );
1165 void CurveCreator_Widget::updateLocalPointView()
1167 OCCViewer_Viewer* aViewer = getOCCViewer();
1170 Handle(AIS_InteractiveContext) aContext = aViewer->getAISContext();
1172 bool isBlocked = myLocalPointView->blockSignals(true);
1174 myLocalPointView->setRowCount( 0 );
1175 for ( aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected() ) {
1176 TopoDS_Vertex aVertex;
1177 TopoDS_Shape aShape = aContext->SelectedShape();
1178 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
1179 aVertex = TopoDS::Vertex( aContext->SelectedShape() );
1181 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
1182 if ( !anOwner.IsNull() ) {
1183 Handle(AIS_InteractiveObject) anAIS = Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
1184 if ( !anAIS.IsNull() ) {
1185 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast( anAIS);
1186 if ( !aPoint.IsNull() )
1187 aVertex = TopoDS::Vertex( aPoint->Vertex() );
1189 if ( aVertex.IsNull() ) {
1190 // the following happens if there are no points in the current curve, there is only a shape
1191 /*Handle(StdSelect_BRepOwner) aBrepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1192 if ( aBrepOwner.IsNull() )
1194 if ( aBrepOwner->HasShape() ) {
1195 const TopoDS_Shape& aShape = aBrepOwner->Shape();
1196 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
1198 aVertex = TopoDS::Vertex( aShape );
1203 if ( aVertex.IsNull() )
1205 aPnt = BRep_Tool::Pnt( aVertex );
1206 addLocalPointToTable( aPnt.X(), aPnt.Y() );
1209 myLocalPointView->blockSignals(isBlocked);
1215 void CurveCreator_Widget::setLocalPointContext( const bool theOpen, const bool isUpdateTable )
1217 OCCViewer_Viewer* aViewer = getOCCViewer();
1220 Handle(AIS_InteractiveContext) ic = aViewer->getAISContext();
1223 // Open local context if there is no one
1224 if ( !ic->HasOpenedContext() ) {
1225 ic->ClearCurrents( false );
1226 ic->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
1228 AIS_ListOfInteractive aList;
1229 ic->DisplayedObjects( aList );
1231 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
1234 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
1236 Handle(AIS_InteractiveObject) anAIS = it.Value();
1237 if ( !anAIS.IsNull() )
1239 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
1241 ic->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
1242 ic->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_WIRE ) );
1244 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
1246 ic->Load( anAIS, -1/*selection mode*/, false/*allow decomposition*/ );
1247 ic->Activate( anAIS, TopAbs_VERTEX );
1254 if ( ic->HasOpenedContext() )
1255 ic->CloseAllContexts();
1256 if ( isUpdateTable )
1257 updateLocalPointView();
1261 void CurveCreator_Widget::addLocalPointToTable( const double theX, const double theY )
1263 SectionToPointList aPoints;
1264 findSectionsToPoints( theX, theY, aPoints );
1266 SectionToPointList aSkipList;
1267 // table could not contain two equal value rows
1268 int aRowId = myLocalPointView->rowCount();
1269 double aCurrentX, aCurrentY;
1270 int aSectionId, aPointId;
1271 SectionToPoint aPoint;
1272 for ( int i = 0; i < aRowId; i++ ) {
1273 aCurrentX = myLocalPointView->item( i, 2 )->data( Qt::UserRole ).toDouble();
1274 aCurrentY = myLocalPointView->item( i, 3 )->data( Qt::UserRole ).toDouble();
1275 if ( fabs( aCurrentX - theX ) < LOCAL_SELECTION_TOLERANCE &&
1276 fabs( aCurrentY - theY ) < LOCAL_SELECTION_TOLERANCE ) {
1277 aPoint = std::make_pair<int, int>( getSectionId( i ), getPointId( i ) );
1278 if ( !contains( aSkipList, aPoint ) )
1279 aSkipList.push_back( aPoint );
1282 if ( aSkipList.size() == aPoints.size() )
1285 QTableWidgetItem* anItem;
1286 SectionToPointList::const_iterator anIt = aPoints.begin(), aLast = aPoints.end();
1287 for ( ; anIt != aLast; anIt++ ) {
1289 if ( contains( aSkipList, aPoint ) )
1292 myLocalPointView->setRowCount( aRowId+1 );
1293 aSectionId = aPoint.first;
1294 aPointId = aPoint.second;
1296 anItem = new QTableWidgetItem( myCurve->getSectionName( aSectionId ).c_str() );
1297 anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
1298 anItem->setData( Qt::UserRole, aSectionId );
1299 myLocalPointView->setItem( aRowId, 0, anItem );
1301 anItem = new QTableWidgetItem( QString::number( aPointId + 1 ) );
1302 anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
1303 anItem->setData( Qt::UserRole, aPointId );
1304 myLocalPointView->setItem( aRowId, 1, anItem );
1306 anItem = new QTableWidgetItem( QString::number( theX ) );
1307 anItem->setData( Qt::UserRole, theX );
1308 myLocalPointView->setItem( aRowId, 2, anItem );
1310 anItem = new QTableWidgetItem( QString::number( theY ) );
1311 anItem->setData( Qt::UserRole, theY );
1312 myLocalPointView->setItem( aRowId, 3, anItem );
1317 * Set drag operation started. Save the position and a list of dragged points
1318 * \param theState the drag operation state: started/finished
1319 * \param thePoint the start drag position
1321 void CurveCreator_Widget::setDragStarted( const bool theState, const QPoint& thePoint )
1323 OCCViewer_Viewer* aViewer = getOCCViewer();
1325 getSelectedPonts( myDragPoints );
1326 myDragStarted = myDragPoints.size();
1327 myDragStartPosition = thePoint;
1328 if ( aViewer && myDragStarted ) {
1329 // change a viewer interaction style in order to avoid a select rectangle build
1330 myDragInteractionStyle = aViewer->interactionStyle();
1331 aViewer->setInteractionStyle(SUIT_ViewModel::KEY_FREE);
1335 if ( aViewer && myDragStarted )
1336 aViewer->setInteractionStyle( myDragInteractionStyle );
1337 myDragStarted = false;
1338 myDragPoints.clear();
1343 void CurveCreator_Widget::getSelectedPonts( CurveCreator_Widget::SectionToPointList& thePoints )
1346 for ( int i = 0, aNb = myLocalPointView->rowCount(); i < aNb; i++ )
1347 thePoints.push_back( std::make_pair( getSectionId( i ), getPointId( i ) ) );
1350 void CurveCreator_Widget::setSelectedPonts( const CurveCreator_Widget::SectionToPointList& thePoints )
1352 OCCViewer_Viewer* aViewer = getOCCViewer();
1356 AIS_ListOfInteractive aListToSelect;
1358 Handle(AIS_InteractiveContext) ic = aViewer->getAISContext();
1359 if ( !ic->HasOpenedContext() )
1362 AIS_ListOfInteractive aDisplayedList;
1363 ic->DisplayedObjects( aDisplayedList );
1364 for ( AIS_ListIteratorOfListOfInteractive it( aDisplayedList ); it.More(); it.Next() )
1366 Handle(AIS_InteractiveObject) anAIS = it.Value();
1367 if ( anAIS.IsNull() )
1369 Handle(AIS_Point) anAISPoint = Handle(AIS_Point)::DownCast( anAIS );
1370 if ( anAISPoint.IsNull() )
1373 TopoDS_Vertex aVertex = TopoDS::Vertex( anAISPoint->Vertex() );
1375 if ( aVertex.IsNull() )
1378 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
1380 SectionToPointList aPoints;
1381 findSectionsToPoints( aPnt.X(), aPnt.Y(), aPoints );
1383 SectionToPointList::const_iterator anIt = aPoints.begin(), aLast = aPoints.end();
1384 SectionToPoint aPoint;
1385 for ( ; anIt != aLast; anIt++ ) {
1387 if ( contains( thePoints, aPoint ) )
1388 aListToSelect.Append( anAIS );
1392 ic->ClearSelected( Standard_False );
1393 aViewer->setObjectsSelected( aListToSelect );
1394 updateLocalPointView();
1398 * Get viewer information before perform the curve modification.
1399 * Take a list of selected cuve points an close local context.
1400 * The context should be closed because the curve presentation is
1401 * redisplayed and if it is not closed, when we close the local context
1402 * later, the presentation shown in the local context is disappeared.
1403 * \param thePoints an output list of curve selected points
1404 * \param theFillPoints a flag whether the selection list should be filled
1406 void CurveCreator_Widget::startCurveModification(
1407 CurveCreator_Widget::SectionToPointList& thePoints,
1408 const bool theFillPoints )
1410 if ( theFillPoints ) {
1412 getSelectedPonts( thePoints );
1414 setLocalPointContext( false );
1418 * Restore the viewer state after the curve modification is done.
1419 * Open local context and select given points inside it.
1420 * \param thePoints a list of curve selected points
1422 void CurveCreator_Widget::finishCurveModification(
1423 const CurveCreator_Widget::SectionToPointList& thePoints )
1425 if ( getActionMode() == ModificationMode )
1426 setLocalPointContext( true );
1427 setSelectedPonts( thePoints );
1431 * Returns a point index in the model curve by the point coordinates in the viewer
1432 * \param theX the X coordinate of the point
1433 * \param theY the Y coordinate of the point
1435 int CurveCreator_Widget::findLocalPointIndex( int theSectionId, float theX, float theY )
1439 CurveCreator::Coordinates aCoords;
1440 for ( int i = 0, aNb = myCurve->getNbPoints( theSectionId ); i < aNb && aPntIndex < 0; i++ ) {
1441 aCoords = myCurve->getPoint( theSectionId, i );
1442 if ( aCoords.size() < 2 )
1444 if ( fabs( aCoords[0] - theX ) < LOCAL_SELECTION_TOLERANCE &&
1445 fabs( aCoords[1] - theY ) < LOCAL_SELECTION_TOLERANCE )
1452 void CurveCreator_Widget::findSectionsToPoints( const double theX, const double theY,
1453 CurveCreator_Widget::SectionToPointList& thePoints )
1458 for ( int i = 0, aNb = myCurve->getNbSections(); i < aNb; i++ ) {
1459 aPointId = findLocalPointIndex( i, theX, theY );
1462 SectionToPoint aPoint = std::make_pair( i, aPointId );
1463 if ( !contains( thePoints, aPoint ) )
1464 thePoints.push_back( aPoint );
1468 void CurveCreator_Widget::convert( const SectionToPointList& thePoints,
1469 QMap<int, QList<int> >& theConvPoints )
1471 theConvPoints.clear();
1473 SectionToPointList::const_iterator anIt = thePoints.begin(), aLast = thePoints.end();
1475 int aSectionId, aPointId;
1476 for ( ; anIt != aLast; anIt++ ) {
1477 aSectionId = anIt->first;
1478 aPointId = anIt->second;
1480 if ( theConvPoints.contains( aSectionId ) )
1481 aPoints = theConvPoints[aSectionId];
1482 if ( aPoints.contains( aPointId ) )
1484 aPoints.append( aPointId );
1485 theConvPoints[aSectionId] = aPoints;
1490 * Checks whether the point belongs to the OCC object
1491 * \param theObject a line or shape with a bspline inside
1492 * \param theX the X coordinate in the view.
1493 * \param theY the Y coordinate in the view.
1494 * \param thePoint the output point to be append to the model curve
1495 * \param thePoint1 the output point to bound the line where a new point should be inserted
1496 * \param thePoint2 the output point to bound the line where a new point should be inserted
1498 bool CurveCreator_Widget::pointOnObject( Handle(AIS_InteractiveObject) theObject,
1499 const int theX, const int theY,
1501 gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
1503 bool isFound = false;
1505 OCCViewer_Viewer* aViewer = getOCCViewer();
1506 if ( theObject.IsNull() || !aViewer )
1510 Standard_Real aParameter;
1511 gp_Pnt aPnt1, aPnt2;
1512 Handle(AIS_Line) aLine = Handle(AIS_Line)::DownCast( theObject );
1513 if ( !aLine.IsNull() ) {
1514 const Handle(Geom_Line) aGLine = aLine->Line();
1515 isFound = hasProjectPointOnCurve( theX, theY, aGLine, aParameter );
1517 aPoint = aGLine->Value( aParameter );
1519 Handle(Geom_Point) aPStart;
1520 Handle(Geom_Point) aPEnd;
1521 aLine->Points( aPStart, aPEnd );
1522 aPnt1 = aPStart->Pnt();
1523 aPnt2 = aPEnd->Pnt();
1525 // in case of Geom line a projection is performed to the infinite line,
1526 // so it is necessary to bound it by the line size
1528 aLineBox.Set( aPnt1, gp_Vec( aPnt1, aPnt2 ) );
1529 isFound = !aLineBox.IsOut( aPoint );
1533 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
1534 if ( !aShape.IsNull() ) {
1535 const TopoDS_Wire& aWire = TopoDS::Wire( aShape->Shape() );
1536 if ( !aWire.IsNull() ) {
1537 TopExp_Explorer anExp( aWire, TopAbs_EDGE );
1538 for ( ; anExp.More(); anExp.Next())
1540 const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
1541 if ( !anEdge.IsNull() ) {
1542 Standard_Real aFirst, aLast;
1543 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
1545 if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
1546 Handle(Geom_BSplineCurve) aBSplineCurve = Handle(Geom_BSplineCurve)::DownCast( aCurve );
1547 if ( !aBSplineCurve.IsNull() ) {
1548 isFound = hasProjectPointOnCurve( theX, theY, aCurve, aParameter );
1550 aPoint = aBSplineCurve->Value( aParameter );
1551 Standard_Integer anI1, anI2;
1552 aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
1554 aPnt1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
1555 aPnt2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
1573 * Returns whether the clicked point belong to the curve or has a very near projection
1574 * \param theX the X coordinate of a point clicked in the OCC viewer
1575 * \param theY the Y coordinate of a point clicked in the OCC viewer
1576 * \param theCurve a geometry curve
1577 * \param theOutPoint a found projected point on the curve
1579 bool CurveCreator_Widget::hasProjectPointOnCurve( const int theX, const int theY,
1580 const Handle(Geom_Curve)& theCurve,
1581 Standard_Real& theParameter )
1583 bool isFound = false;
1584 OCCViewer_Viewer* aViewer = getOCCViewer();
1588 OCCViewer_ViewWindow* aWindow =
1589 (OCCViewer_ViewWindow*)aViewer->getViewManager()->getActiveView();
1590 Handle(V3d_View) aView = aWindow->getViewPort()->getView();
1591 gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, aView );
1593 GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
1594 Standard_Integer aNbPoint = aProj.NbPoints();
1596 for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
1597 gp_Pnt aNewPoint = aProj.Point( j );
1598 theParameter = aProj.Parameter( j );
1601 CurveCreator_Utils::ConvertPointToClick( aNewPoint, aView, aX, anY );
1603 int aXDelta = abs( aX - theX );
1604 int anYDelta = abs( anY - theY );
1605 isFound = aXDelta < SCENE_PIXEL_TOLERANCE && anYDelta < SCENE_PIXEL_TOLERANCE;
1612 * Returns a section index from the table
1613 * \param theRowId a table row
1615 int CurveCreator_Widget::getSectionId( const int theRowId ) const
1617 return myLocalPointView->item( theRowId, 0 )->data( Qt::UserRole ).toInt();
1621 * Returns a point index from the table
1622 * \param theRowId a table row
1624 int CurveCreator_Widget::getPointId( const int theRowId ) const
1626 return myLocalPointView->item( theRowId, 1 )->data( Qt::UserRole ).toInt();
1630 * Returns whethe the container has the value
1631 * \param theList a container of values
1632 * \param theValue a value
1634 bool CurveCreator_Widget::contains( const CurveCreator_Widget::SectionToPointList& theList,
1635 const CurveCreator_Widget::SectionToPoint& theValue ) const
1637 bool isFound = false;
1639 SectionToPointList::const_iterator anIt = theList.begin(), aLast = theList.end();
1640 for ( ; anIt != aLast && !isFound; anIt++ )
1641 isFound = anIt->first == theValue.first && anIt->second == theValue.second;