1 // Copyright (C) 2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "CurveCreator_Widget.h"
21 #include "CurveCreator_TreeView.h"
22 #include "CurveCreator_ICurve.hxx"
23 //#include "CurveCreator_CurveEditor.hxx"
24 #include "CurveCreator.hxx"
25 //#include "CurveCreator_NewPointDlg.h"
26 #include "CurveCreator_NewSectionDlg.h"
27 #include "CurveCreator_Utils.h"
28 #include "CurveCreator_UtilsICurve.hxx"
29 #include "CurveCreator_TableView.h"
31 #include <SUIT_Session.h>
32 #include <SUIT_Desktop.h>
33 #include <SUIT_ResourceMgr.h>
34 #include <SUIT_ViewManager.h>
36 #include <OCCViewer_ViewWindow.h>
37 #include <OCCViewer_ViewManager.h>
38 #include <OCCViewer_ViewPort3d.h>
39 #include "OCCViewer_Utilities.h"
41 #include <BRep_Tool.hxx>
43 #include <TopoDS_Vertex.hxx>
44 #include <TopoDS_Wire.hxx>
45 #include <TopoDS_Edge.hxx>
48 #include <TopExp_Explorer.hxx>
50 #include <GeomAPI_ProjectPointOnCurve.hxx>
52 #include <AIS_ListOfInteractive.hxx>
53 #include <AIS_ListIteratorOfListOfInteractive.hxx>
54 #include <AIS_Shape.hxx>
55 #include <AIS_LocalContext.hxx>
56 #include <Geom_Point.hxx>
57 #include <Geom_BSplineCurve.hxx>
58 #include <Geom_Line.hxx>
60 #include <QHBoxLayout>
61 #include <QVBoxLayout>
65 #include <QToolButton>
69 #include <QMouseEvent>
70 #include <QApplication>
71 #include <QTableWidget>
78 #define START_MEASURE_TIME \
82 #define END_MEASURE_TIME( theMsg ) \
83 double aTime = aTimer.elapsed() * 0.001; \
84 FILE* aFile = fopen( "performance", "a" ); \
85 fprintf( aFile, "%s = %.3lf sec\n", theMsg, aTime ); \
90 #define START_MEASURE_TIME
91 #define END_MEASURE_TIME( theMsg )
100 CurveCreator_Widget::CurveCreator_Widget(QWidget* parent,
101 CurveCreator_ICurve *theCurve,
103 int theLocalPointRowLimit )
104 : QWidget(parent), myNewSectionEditor(NULL), myCurve(theCurve), mySection(0),
105 myDragStarted( false ), myDragInteractionStyle( SUIT_ViewModel::STANDARD ),
106 myOCCViewer( 0 ), myLocalPointRowLimit( theLocalPointRowLimit )
108 myNewSectionEditor = new CurveCreator_NewSectionDlg( this );
109 myNewSectionEditor->hide();
110 connect( myNewSectionEditor, SIGNAL(addSection()), this, SLOT(onAddNewSection()) );
111 connect( myNewSectionEditor, SIGNAL(modifySection()), this, SLOT(onModifySection()) );
112 connect( myNewSectionEditor, SIGNAL(cancelSection()), this, SLOT(onCancelSection()) );
114 QGroupBox* aSectionGroup = new QGroupBox(tr("Sections"),this);
116 mySectionView = new CurveCreator_TreeView(myCurve, aSectionGroup);
117 connect( mySectionView, SIGNAL(selectionChanged()), this, SLOT( onSelectionChanged() ) );
118 connect( mySectionView, SIGNAL(sectionEntered(int)), this, SLOT(onEditSection(int)) );
119 connect( mySectionView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onContextMenu(QPoint)) );
121 myLocalPointView = new CurveCreator_TableView( myCurve, this );
122 connect( myLocalPointView, SIGNAL( cellChanged( int, int ) ),
123 this, SLOT( onCellChanged( int, int ) ) );
125 QToolBar* aTB = new QToolBar(tr("TOOL_BAR_TLT"), aSectionGroup);
126 // QToolButton* anUndoBtn = new QToolButton(aTB);
128 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
129 QPixmap anUndoPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_UNDO")));
130 QPixmap aRedoPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_REDO")));
131 QPixmap aNewSectionPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_NEW_SECTION")));
132 QPixmap aNewPointPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_NEW_POINT")));
133 QPixmap anEditPointsPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_EDIT_POINTS")));
134 QPixmap aDetectPointsPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_EDIT_POINTS")));
135 QPixmap aPolylinePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_POLYLINE")));
136 QPixmap aSplinePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_SPLINE")));
137 QPixmap aRemovePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_DELETE")));
138 QPixmap aJoinPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_JOIN")));
139 QPixmap aStepUpPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_ARROW_UP")));
140 QPixmap aStepDownPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_ARROW_DOWN")));
142 QAction* anAct = createAction( UNDO_ID, tr("UNDO"), anUndoPixmap, tr("UNDO_TLT"),
143 QKeySequence(Qt::ControlModifier|Qt::Key_Z) );
144 connect(anAct, SIGNAL(triggered()), this, SLOT(onUndo()) );
145 aTB->addAction(anAct);
147 anAct = createAction( REDO_ID, tr("REDO"), aRedoPixmap, tr("REDO_TLT"),
148 QKeySequence(Qt::ControlModifier|Qt::Key_Y) );
149 connect(anAct, SIGNAL(triggered()), this, SLOT(onRedo()) );
150 aTB->addAction(anAct);
154 anAct = createAction( NEW_SECTION_ID, tr("NEW_SECTION"), aNewSectionPixmap, tr("NEW_SECTION_TLT"),
155 QKeySequence(Qt::ControlModifier|Qt::Key_N) );
156 connect(anAct, SIGNAL(triggered()), this, SLOT(onNewSection()) );
157 aTB->addAction(anAct);
160 anAct = createAction( ADDITION_MODE_ID, tr("ADDITION_MODE"), aNewPointPixmap, tr("ADDITION_MODE_TLT"),
162 anAct->setCheckable(true);
163 connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onAdditionMode(bool)) );
164 connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
165 aTB->addAction(anAct);
167 anAct = createAction( MODIFICATION_MODE_ID, tr("MODIFICATION_MODE"), anEditPointsPixmap, tr("MODIFICATION_MODE_TLT"),
169 anAct->setCheckable(true);
170 connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onModificationMode(bool)) );
171 connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
172 aTB->addAction(anAct);
174 anAct = createAction( DETECTION_MODE_ID, tr("DETECTION_MODE"), aDetectPointsPixmap, tr("DETECTION_MODE_TLT"),
176 anAct->setCheckable(true);
177 connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onDetectionMode(bool)) );
178 connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
179 aTB->addAction(anAct);
181 anAct = createAction( CLOSE_SECTIONS_ID, tr("CLOSE_SECTIONS"), QPixmap(), tr("CLOSE_SECTIONS_TLT"),
182 QKeySequence(Qt::ControlModifier|Qt::Key_W) );
183 connect(anAct, SIGNAL(triggered()), this, SLOT(onCloseSections()) );
185 anAct = createAction( UNCLOSE_SECTIONS_ID, tr("UNCLOSE_SECTIONS"), QPixmap(),
186 tr("UNCLOSE_SECTIONS_TLT"), QKeySequence(Qt::ControlModifier|Qt::Key_S) );
187 connect(anAct, SIGNAL(triggered()), this, SLOT(onUncloseSections()) );
189 anAct = createAction( SET_SECTIONS_POLYLINE_ID, tr("SET_SECTIONS_POLYLINE"),
190 aPolylinePixmap, tr("SET_POLYLINE_TLT"),
191 QKeySequence(Qt::ControlModifier|Qt::Key_E) );
192 connect(anAct, SIGNAL(triggered()), this, SLOT(onSetPolyline()) );
194 anAct = createAction( SET_SECTIONS_SPLINE_ID, tr("SET_SECTIONS_SPLINE"), aSplinePixmap,
195 tr("SET_SPLINE_TLT"), QKeySequence(Qt::ControlModifier|Qt::Key_R) );
196 connect(anAct, SIGNAL(triggered()), this, SLOT(onSetSpline()) );
198 anAct = createAction( REMOVE_ID, tr("REMOVE"), aRemovePixmap, tr("REMOVE_TLT"),
199 QKeySequence(Qt::ControlModifier|Qt::Key_Delete ) );
200 connect(anAct, SIGNAL(triggered()), this, SLOT(onRemove()) );
201 aTB->addAction(anAct);
203 //aTB->addSeparator();
205 //anAct = createAction( JOIN_ID, tr("JOIN"), aJoinPixmap, tr("JOIN_TLT"),
206 // QKeySequence(Qt::ControlModifier|Qt::Key_Plus ) );
207 //connect( anAct, SIGNAL(triggered()), this, SLOT(onJoin()) );
208 //aTB->addAction(anAct);
210 anAct = createAction( CLEAR_ALL_ID, tr("CLEAR_ALL"), QPixmap(), tr("CLEAR_ALL_TLT"),
211 QKeySequence(Qt::ControlModifier | Qt::ShiftModifier | Qt::Key_Delete ) );
212 connect( anAct, SIGNAL(triggered()), this, SLOT( onClearAll()) );
215 //anAct = createAction( JOIN_ALL_ID, tr("JOIN_ALL"), QPixmap(), tr("JOIN_ALL_TLT"),
216 // QKeySequence(Qt::ControlModifier | Qt::ShiftModifier | Qt::Key_Plus ) );
217 //connect( anAct, SIGNAL(triggered()), this, SLOT(onJoinAll()) );
219 QVBoxLayout* aSectLayout = new QVBoxLayout();
220 aSectLayout->setMargin( 5 );
221 aSectLayout->setSpacing( 5 );
222 aSectLayout->addWidget(aTB);
223 aSectLayout->addWidget(mySectionView);
224 aSectLayout->addWidget( myLocalPointView );
225 aSectionGroup->setLayout(aSectLayout);
226 QVBoxLayout* aLay = new QVBoxLayout();
227 aLay->setMargin( 0 );
228 aLay->setSpacing( 5 );
229 // aLay->addLayout(aNameLayout);
230 aLay->addWidget(aSectionGroup);
233 updateActionsStates();
240 void CurveCreator_Widget::setOCCViewer( OCCViewer_Viewer* theViewer )
242 if ( myOCCViewer == theViewer )
246 OCCViewer_ViewManager* aViewManager = dynamic_cast<OCCViewer_ViewManager*>
247 ( myOCCViewer->getViewManager() );
248 disconnect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
249 this, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ) );
250 disconnect( aViewManager, SIGNAL( mouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ),
251 this, SLOT( onMouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ) );
252 disconnect( aViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
253 this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
254 disconnect( aViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
255 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
256 // restore normal mode in the viewer
257 OCCViewer_Utilities::setViewer2DMode( myOCCViewer, OCCViewer_ViewWindow::No2dMode );
258 // all local contexts should be closed if the viewer is not more used
259 setLocalPointContext( false, true );
262 myOCCViewer = theViewer;
264 OCCViewer_ViewManager* aViewManager = dynamic_cast<OCCViewer_ViewManager*>
265 ( myOCCViewer->getViewManager() );
266 connect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
267 this, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ) );
268 connect( aViewManager, SIGNAL( mouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ),
269 this, SLOT( onMouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ) );
270 connect( aViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
271 this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
272 connect( aViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
273 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
274 OCCViewer_Utilities::setViewer2DMode( myOCCViewer, OCCViewer_ViewWindow::XYPlane );
279 * Returns current OCC viewer
281 OCCViewer_Viewer* CurveCreator_Widget::getOCCViewer()
287 * Returns OCC viewer context
289 Handle(AIS_InteractiveContext) CurveCreator_Widget::getAISContext()
291 Handle(AIS_InteractiveContext) aContext;
292 OCCViewer_Viewer* aViewer = getOCCViewer();
294 aContext = aViewer->getAISContext();
300 * Returns OCC viewer view port
302 OCCViewer_ViewPort3d* CurveCreator_Widget::getViewPort()
304 OCCViewer_ViewPort3d* aViewPort = 0;
305 OCCViewer_Viewer* aViewer = getOCCViewer();
307 aViewPort = ((OCCViewer_ViewWindow*)aViewer->getViewManager()->getActiveView())->getViewPort();
313 * Set interaction style in the OCC viewer
314 * \param theStyle a new style
315 * \return the previous style
317 int CurveCreator_Widget::changeInteractionStyle( int theStyle )
319 OCCViewer_Viewer* aViewer = getOCCViewer();
323 int aPrevStyle = aViewer->interactionStyle();
324 aViewer->setInteractionStyle( theStyle );
329 //=======================================================================
331 // purpose: reset the widget viewer, close local context, clear selection
332 //=======================================================================
333 void CurveCreator_Widget::reset()
337 void CurveCreator_Widget::setCurve( CurveCreator_ICurve* theCurve )
340 mySectionView->setCurve( myCurve );
341 myLocalPointView->setCurve( myCurve );
342 updateActionsStates();
346 void CurveCreator_Widget::onSelectionChanged()
348 updateActionsStates();
350 emit selectionChanged();
353 void CurveCreator_Widget::updateActionsStates()
355 QList<ActionId> anEnabledAct;
357 anEnabledAct << NEW_SECTION_ID << MODIFICATION_MODE_ID;
358 if ( removeEnabled() )
359 anEnabledAct << REMOVE_ID;
360 QList<int> aSelSections = mySectionView->getSelectedSections();
361 QList< QPair< int, int > > aSelPoints = mySectionView->getSelectedPoints();
362 CurveCreator_TreeView::SelectionType aSelType = mySectionView->getSelectionType();
364 case CurveCreator_TreeView::ST_NOSEL:{
367 case CurveCreator_TreeView::ST_SECTIONS:{
368 /*if( aSelSections[0] > 0 ){
369 anEnabledAct << UP_ID;
371 if( aSelSections.size() == 1 ){
372 anEnabledAct << ADDITION_MODE_ID << DETECTION_MODE_ID;
374 switch ( getActionMode() ) {
378 QList<int> aSelSection = mySectionView->getSelectedSections();
379 if( aSelSection.size() > 0 ){
380 mySection = aSelSection[0];
381 myPointNum = myCurve->getNbPoints(mySection);
385 case ModificationMode: {
386 anEnabledAct << CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID << SET_SECTIONS_POLYLINE_ID << SET_SECTIONS_SPLINE_ID;
387 int aSectCnt = myCurve->getNbSections();
389 anEnabledAct << CLEAR_ALL_ID;
392 // anEnabledAct << JOIN_ALL_ID;
393 //if( aSelSections.size() > 1 ){
394 // anEnabledAct << JOIN_ID;
398 case DetectionMode: {
405 /*if( aSelSections[ aSelSections.size() - 1 ] < ( myCurve->getNbSections() - 1 ) ){
406 anEnabledAct << DOWN_ID;
410 /*case CurveCreator_TreeView::ST_POINTS_ONE_SECTION:{
411 if( aSelPoints[0].second > 0 ){
412 anEnabledAct << UP_ID;
414 int aLastIndex = aSelPoints.size()-1;
415 int aSect = aSelPoints[0].first;
416 if( aSelPoints[aLastIndex].second < (myCurve->getNbPoints(aSect) - 1)){
417 anEnabledAct << DOWN_ID;
419 if( aSelPoints.size() == 1){
420 anEnabledAct << INSERT_POINT_BEFORE_ID << INSERT_POINT_AFTER_ID;
427 /*int aSelObjsCnt = aSelPoints.size() + aSelSections.size();
428 if( aSelObjsCnt > 0 ){
429 anEnabledAct << REMOVE_ID;
431 if( (myCurve->getNbSections() + myCurve->getNbPoints()) > 0 ){
432 anEnabledAct << REMOVE_ALL_ID;
435 //if( myCurve->getNbSections() > 1 ){
436 // anEnabledAct << JOIN_ALL_ID;
439 QList<ActionId> anIds = myActionMap.keys();
440 for( int i = 0 ; i < anIds.size() ; i++ ){
441 if( myActionMap.contains(anIds[i]) ){
442 if( anEnabledAct.contains(anIds[i]) ){
443 myActionMap[anIds[i]]->setEnabled(true);
446 myActionMap[anIds[i]]->setEnabled(false);
452 void CurveCreator_Widget::onAdditionMode(bool checked)
457 Handle(AIS_InteractiveContext) aContext = getAISContext();
458 if( !myCurve || aContext.IsNull() )
463 QList<int> aSelSection = mySectionView->getSelectedSections();
464 if( aSelSection.size() > 0 ){
465 mySection = aSelSection[0];
468 QList< QPair<int,int> > aSelPoints = mySectionView->getSelectedPoints();
469 if( aSelPoints.size() > 0 ){
470 mySection = aSelPoints[0].first;
471 myPointNum = aSelPoints[0].second + 1;
474 // emit subOperationStarted( myNewPointEditor );
477 void CurveCreator_Widget::onModificationMode(bool checked)
479 myLocalPointView->setVisible( checked );
482 void CurveCreator_Widget::onDetectionMode(bool checked)
486 void CurveCreator_Widget::onModeChanged(bool checked)
488 ActionMode aMode = NoneMode;
490 QAction* anAction = (QAction*)sender();
491 switch(myActionMap.key(anAction)) {
492 case ADDITION_MODE_ID:
493 aMode = AdditionMode;
494 if (myActionMap[MODIFICATION_MODE_ID]->isChecked())
495 myActionMap[MODIFICATION_MODE_ID]->trigger();
496 else if (myActionMap[DETECTION_MODE_ID]->isChecked())
497 myActionMap[DETECTION_MODE_ID]->trigger();
499 case MODIFICATION_MODE_ID:
500 aMode = ModificationMode;
501 if (myActionMap[ADDITION_MODE_ID]->isChecked())
502 myActionMap[ADDITION_MODE_ID]->trigger();
503 else if (myActionMap[DETECTION_MODE_ID]->isChecked())
504 myActionMap[DETECTION_MODE_ID]->trigger();
506 case DETECTION_MODE_ID:
507 aMode = DetectionMode;
508 if (myActionMap[ADDITION_MODE_ID]->isChecked())
509 myActionMap[ADDITION_MODE_ID]->trigger();
510 else if (myActionMap[MODIFICATION_MODE_ID]->isChecked())
511 myActionMap[MODIFICATION_MODE_ID]->trigger();
515 updateActionsStates();
517 setLocalPointContext( aMode == ModificationMode, true );
520 void CurveCreator_Widget::onAddNewPoint(const CurveCreator::Coordinates& theCoords)
524 //myCurve->addPoints(theCoords, mySection, myPointNum );
525 //mySectionView->pointsAdded( mySection, myPointNum );
527 QList<int> aSections = mySectionView->getSelectedSections();
528 if( aSections.size() == 0 ){
531 int aSection = aSections[0];
532 myCurve->addPoints(theCoords, aSection); // add to the end of section
533 mySectionView->pointsAdded( aSection, myCurve->getNbPoints( aSection ) );
534 updateActionsStates();
538 void CurveCreator_Widget::onNewSection()
542 myNewSectionEditor->clear();
543 myNewSectionEditor->setEditMode(false);
544 QString aSectName = QString( CurveCreator_UtilsICurve::getUniqSectionName( myCurve ).c_str() );
545 myNewSectionEditor->setSectionParameters(aSectName, true, CurveCreator::Polyline );
546 emit subOperationStarted( myNewSectionEditor );
549 void CurveCreator_Widget::onAddNewSection()
553 myCurve->addSection( myNewSectionEditor->getName().toStdString(),
554 myNewSectionEditor->getSectionType(),
555 myNewSectionEditor->isClosed() );
556 mySectionView->sectionAdded( -1 ); // add a new section to the end of list
557 QString aNewName = QString( CurveCreator_UtilsICurve::getUniqSectionName( myCurve ).c_str() );
558 myNewSectionEditor->setSectionName(aNewName);
559 updateActionsStates();
564 void CurveCreator_Widget::onCancelSection()
566 emit subOperationFinished( myNewSectionEditor );
569 QAction* CurveCreator_Widget::createAction( ActionId theId, const QString& theName, const QPixmap& theImage,
570 const QString& theToolTip, const QKeySequence& theShortcut )
572 QAction* anAct = new QAction(theName,this);
573 if( !theImage.isNull() ){
574 anAct->setIcon(theImage);
576 anAct->setShortcut(theShortcut);
577 anAct->setToolTip(theToolTip);
578 myActionMap[theId] = anAct;
582 QAction* CurveCreator_Widget::getAction(ActionId theId)
584 if( myActionMap.contains(theId) )
585 return myActionMap[theId];
589 CurveCreator_Widget::ActionMode CurveCreator_Widget::getActionMode() const
591 ActionMode aMode = NoneMode;
593 if ( myActionMap[ADDITION_MODE_ID]->isChecked() )
594 aMode = AdditionMode;
595 else if ( myActionMap[MODIFICATION_MODE_ID]->isChecked() )
596 aMode = ModificationMode;
597 else if ( myActionMap[DETECTION_MODE_ID]->isChecked() )
598 aMode = DetectionMode;
603 void CurveCreator_Widget::onEditSection( int theSection )
607 mySection = theSection;
608 QString aSectName = QString::fromStdString( myCurve->getSectionName(theSection));
609 bool isClosed = myCurve->isClosed(theSection);
610 CurveCreator::SectionType aType = myCurve->getSectionType(theSection);
611 myNewSectionEditor->setEditMode(true);
612 myNewSectionEditor->setSectionParameters( aSectName, isClosed, aType );
614 emit subOperationStarted( myNewSectionEditor );
617 void CurveCreator_Widget::onModifySection()
621 QString aName = myNewSectionEditor->getName();
622 bool isClosed = myNewSectionEditor->isClosed();
623 CurveCreator::SectionType aSectType = myNewSectionEditor->getSectionType();
624 // myCurve->startOperation();
625 if( myCurve->getSectionName(mySection) != aName.toStdString() )
626 myCurve->setSectionName( mySection , aName.toStdString() );
628 if( myCurve->getSectionType(mySection) != aSectType )
629 myCurve->setSectionType( mySection, aSectType );
631 if( myCurve->isClosed(mySection) != isClosed )
632 myCurve->setClosed( mySection, isClosed );
633 // myCurve->finishOperation();
634 mySectionView->sectionChanged(mySection);
639 /*void CurveCreator_Widget::onEditPoint( int theSection, int thePoint )
641 if( !myNewPointEditor || !myEdit )
643 mySection = theSection;
644 myPointNum = thePoint;
645 QString aSectName = QString::fromStdString( myCurve->getSectionName(theSection));
646 myNewPointEditor->setEditMode(true);
647 myNewPointEditor->setSectionName(aSectName);
648 myNewPointEditor->setDimension( myCurve->getDimension() );
649 CurveCreator::Coordinates aCoords = myCurve->getCoordinates(theSection,thePoint);
650 myNewPointEditor->setCoordinates(aCoords);
651 emit subOperationStarted( myNewPointEditor );
654 void CurveCreator_Widget::onModifyPoint()
658 CurveCreator::Coordinates aCoords = myNewPointEditor->getCoordinates();
659 myEdit->setCoordinates( aCoords, mySection, myPointNum );
660 mySectionView->pointDataChanged( mySection, myPointNum );
665 void CurveCreator_Widget::onJoin()
669 QList<int> aSections = mySectionView->getSelectedSections();
670 if( aSections.size() == 0 ){
673 int aMainSect = aSections[0];
674 int aMainSectSize = myCurve->getNbPoints(aMainSect);
675 // myCurve->startOperation();
676 for( int i = 1 ; i < aSections.size() ; i++ ){
677 int aSectNum = aSections[i] - (i-1);
678 myCurve->join( aMainSect, aSectNum );
679 mySectionView->sectionsRemoved( aSectNum );
681 // myCurve->finishOperation();
682 int aNewSectSize = myCurve->getNbPoints(aMainSect);
683 if( aNewSectSize != aMainSectSize )
684 mySectionView->pointsAdded( aMainSect, aMainSectSize, aNewSectSize-aMainSectSize );
688 void CurveCreator_Widget::onRemove()
693 switch( getActionMode() ) {
697 case ModificationMode:
705 void CurveCreator_Widget::onClearAll()
710 mySectionView->reset();
711 updateActionsStates();
715 void CurveCreator_Widget::onJoinAll()
720 mySectionView->reset();
721 updateActionsStates();
725 void CurveCreator_Widget::onUndoSettings()
730 void CurveCreator_Widget::onSetSpline()
734 QList<int> aSelSections = mySectionView->getSelectedSections();
735 // myCurve->startOperation();
736 for( int i = 0 ; i < aSelSections.size() ; i++ ){
737 myCurve->setSectionType(aSelSections[i], CurveCreator::Spline );
738 mySectionView->sectionChanged(aSelSections[i]);
740 // myCurve->finishOperation();
744 void CurveCreator_Widget::onSetPolyline()
748 // myCurve->startOperation();
749 QList<int> aSelSections = mySectionView->getSelectedSections();
750 for( int i = 0 ; i < aSelSections.size() ; i++ ){
751 myCurve->setSectionType( aSelSections[i], CurveCreator::Polyline );
752 mySectionView->sectionChanged( aSelSections[i] );
754 // myCurve->finishOperation();
758 void CurveCreator_Widget::onCloseSections()
762 // myCurve->startOperation();
763 QList<int> aSelSections = mySectionView->getSelectedSections();
764 for( int i = 0 ; i < aSelSections.size() ; i++ ){
765 myCurve->setClosed(aSelSections[i], true);
766 mySectionView->sectionChanged(aSelSections[i]);
768 // myCurve->finishOperation();
772 void CurveCreator_Widget::onUncloseSections()
776 // myCurve->startOperation();
777 QList<int> aSelSections = mySectionView->getSelectedSections();
778 for( int i = 0 ; i < aSelSections.size() ; i++ ){
779 myCurve->setClosed(aSelSections[i], false);
780 mySectionView->sectionChanged(aSelSections[i]);
782 // myCurve->finishOperation();
786 void CurveCreator_Widget::onUndo()
791 CurveCreator_ICurve::SectionToPointList aPoints;
792 startCurveModification( aPoints, false );
794 finishCurveModification();
795 mySectionView->reset();
798 void CurveCreator_Widget::onRedo()
802 CurveCreator_ICurve::SectionToPointList aPoints;
803 startCurveModification( aPoints, false );
805 finishCurveModification();
806 mySectionView->reset();
809 void CurveCreator_Widget::updateUndoRedo()
813 QAction* anAct = myActionMap[UNDO_ID];
815 if( myCurve->getNbUndo() != 0 ){
816 anAct->setEnabled(true);
819 anAct->setDisabled(true);
822 anAct = myActionMap[REDO_ID];
824 if( myCurve->getNbRedo() != 0 ){
825 anAct->setEnabled(true);
828 anAct->setDisabled(true);
833 void CurveCreator_Widget::onContextMenu( QPoint thePoint )
835 QList<ActionId> aContextActions;
836 aContextActions << CLEAR_ALL_ID << JOIN_ALL_ID << SEPARATOR_ID <<
837 CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID << SET_SECTIONS_POLYLINE_ID <<
838 SET_SECTIONS_SPLINE_ID;
839 QPoint aGlPoint = mySectionView->mapToGlobal(thePoint);
841 QList<ActionId> aResAct;
842 for( int i = 0 ; i < aContextActions.size() ; i++ ){
843 if( aContextActions[i] != SEPARATOR_ID ){
844 if( myActionMap.contains(aContextActions[i]) ){
845 QAction* anAct = myActionMap[aContextActions[i]];
846 if( anAct->isEnabled() ){
847 aResAct << aContextActions[i];
853 aResAct << SEPARATOR_ID;
859 QMenu* aMenu = new QMenu(this);
860 for( int i = 0 ; i < aResAct.size() ; i++ ){
861 if( aResAct[i] == SEPARATOR_ID ){
862 aMenu->addSeparator();
865 QAction* anAct = myActionMap[aResAct[i]];
866 aMenu->insertAction(NULL, anAct);
869 aMenu->exec(aGlPoint);
872 QList<int> CurveCreator_Widget::getSelectedSections()
874 return mySectionView->getSelectedSections();
877 QList< QPair< int, int > > CurveCreator_Widget::getSelectedPoints()
879 return mySectionView->getSelectedPoints();
883 * According to the widget state, performs the remove action
885 void CurveCreator_Widget::removeSelected()
891 * Checks whether there are some selection to be removed
893 bool CurveCreator_Widget::removeEnabled()
895 bool isEnabled = getActionMode() == ModificationMode;
897 QList<int> aSelSections = mySectionView->getSelectedSections();
898 CurveCreator_TreeView::SelectionType aSelType = mySectionView->getSelectionType();
899 isEnabled = aSelType == CurveCreator_TreeView::ST_SECTIONS &&
900 aSelSections.size() == 1;
906 //=================================================================================
907 // function : GeometryGUI::onGetCoordsByClick()
908 // purpose : Manage mouse press events in Additon mode
909 //=================================================================================
910 void CurveCreator_Widget::onGetCoordsByClick( QMouseEvent* pe )
912 if (pe->button() != Qt::LeftButton)
915 if ( pe->modifiers() != Qt::ControlModifier ) {
916 Handle(AIS_InteractiveContext) ic = getAISContext();
923 if ( pe->modifiers() == Qt::ShiftModifier )
924 ic->ShiftSelect(); // Append selection
926 ic->Select(); // New selection
928 /*TopoDS_Shape aShape;
931 if ( ic->MoreSelected() )
932 aShape = ic->SelectedShape();
934 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
935 aPnt = BRep_Tool::Pnt( TopoDS::Vertex( ic->SelectedShape() ) );
938 OCCViewer_ViewPort3d* vp = getViewPort();
939 aPnt = CurveCreator_Utils::ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
941 // set the coordinates into dialog
942 CurveCreator::Coordinates aCoords;
943 aCoords.push_back( aPnt.X() );
944 aCoords.push_back( aPnt.Y() );
945 if ( myCurve->getDimension() == 3 ) {
946 aCoords.push_back( aPnt.Z() );
948 onAddNewPoint(aCoords);
949 // myNewPointEditor->setCoordinates( aCoords );
954 * Manage mouse press events
955 * \param theWindow an owner of the signal
956 * \param theEvent a mouse event
958 void CurveCreator_Widget::onMousePress( SUIT_ViewWindow*, QMouseEvent* theEvent )
960 if ( theEvent->button() != Qt::LeftButton )
963 switch( getActionMode() ) {
964 case ModificationMode: {
965 //store initial cursor position for Drag&Drop
966 setDragStarted( true, theEvent->pos() );
970 onGetCoordsByClick( theEvent );
979 * Manage mouse release events in Modification mode
980 * \param theWindow an owner of the signal
981 * \param theEvent a mouse event
983 void CurveCreator_Widget::onMouseRelease( SUIT_ViewWindow*, QMouseEvent* theEvent )
985 if ( getActionMode() != ModificationMode )
988 if ( myDragStarted ) {
989 bool isDragged = myDragged;
990 CurveCreator_ICurve::SectionToPointList aDraggedPoints;
992 aDraggedPoints = myDragPoints;
994 setDragStarted( false );
995 // if the drag of some points has happened, restore the drag selection
996 if ( aDraggedPoints.size() > 0 )
999 setSelectedPoints( aDraggedPoints );
1000 END_MEASURE_TIME( "drop" );
1003 else // check whether the segment is clicked an a new point should be added to the segment
1004 insertPointToSelectedSegment( theEvent->pos().x(), theEvent->pos().y() );
1006 // updates the input panel table to show the selected point coordinates
1007 updateLocalPointView();
1011 * Manage mouse move events in Modification mode
1012 * \param theWindow an owner of the signal
1013 * \param theEvent a mouse event
1015 void CurveCreator_Widget::onMouseMove( SUIT_ViewWindow*, QMouseEvent* theEvent )
1017 if ( getActionMode() != ModificationMode || !myDragStarted )
1020 QPoint aPos = theEvent->pos();
1021 if ( (aPos - myDragStartPosition).manhattanLength() < QApplication::startDragDistance() )
1026 moveSelectedPoints( aPos.x(), aPos.y() );
1027 myDragStartPosition = aPos;
1029 END_MEASURE_TIME( "drag" );
1033 * Set zero viewer by the last view closed in
1034 * \param theManager a viewer manager
1036 void CurveCreator_Widget::onLastViewClosed( SUIT_ViewManager* theManager )
1041 void CurveCreator_Widget::onMousePress( QMouseEvent* theEvent )
1043 onMousePress( 0, theEvent );
1046 void CurveCreator_Widget::onMouseRelease( QMouseEvent* theEvent )
1048 onMouseRelease( 0, theEvent );
1051 void CurveCreator_Widget::onMouseMove( QMouseEvent* theEvent )
1053 onMouseMove( 0, theEvent );
1056 void CurveCreator_Widget::onCellChanged( int theRow, int theColumn )
1058 int aCurrSect = myLocalPointView->getSectionId( theRow );
1059 int aPntIndex = myLocalPointView->getPointId( theRow );
1061 if ( aPntIndex < 0 )
1064 CurveCreator_ICurve::SectionToPointList aSelPoints;
1065 startCurveModification( aSelPoints );
1067 double aX = myLocalPointView->item( theRow, 2 )->data( Qt::UserRole ).toDouble();
1068 double anY = myLocalPointView->item( theRow, 3 )->data( Qt::UserRole ).toDouble();
1069 std::deque<float> aChangedPos;
1070 aChangedPos.push_back( aX );
1071 aChangedPos.push_back( anY );
1072 myCurve->setPoint( aCurrSect, aPntIndex, aChangedPos );
1074 finishCurveModification( aSelPoints );
1078 * Removes a selected section from the curve. Updates undo/redo status
1080 void CurveCreator_Widget::removeSection()
1082 QList< QPair<int,int> > aSelPoints = mySectionView->getSelectedPoints();
1085 // myCurve->startOperation();
1086 for( int i = 0 ; i < aSelPoints.size() ; i++ ){
1087 if( aCurrSect != aSelPoints[i].first ){
1089 aCurrSect = aSelPoints[i].first;
1091 int aPntIndx = aSelPoints[i].second - aRemoveCnt;
1092 myCurve->removePoint( aCurrSect, aPntIndx );
1093 mySectionView->pointsRemoved( aCurrSect, aPntIndx );
1096 QList<int> aSections = mySectionView->getSelectedSections();
1097 for( int i = 0 ; i < aSections.size() ; i++ ){
1098 int aSectNum = aSections[i] - (i);
1099 myCurve->removeSection( aSectNum );
1100 mySectionView->sectionsRemoved( aSectNum );
1102 // myCurve->finishOperation();
1103 mySectionView->clearSelection();
1108 * Removes a selected points from the curve. Updates undo/redo status
1110 void CurveCreator_Widget::removePoint()
1112 CurveCreator_ICurve::SectionToPointList aPoints;
1113 getSelectedPoints( aPoints );
1114 if ( aPoints.size() == 0 )
1117 CurveCreator_ICurve::SectionToPointList aSelPoints;
1118 startCurveModification( aSelPoints, false );
1120 myCurve->removeSeveralPoints( aPoints );
1121 finishCurveModification( CurveCreator_ICurve::SectionToPointList() );
1124 void CurveCreator_Widget::insertPointToSelectedSegment( const int theX,
1127 Handle(AIS_InteractiveContext) aContext = getAISContext();
1129 OCCViewer_ViewPort3d* aViewPort = getViewPort();
1130 Handle(V3d_View) aView;
1132 aView = aViewPort->getView();
1134 if ( aContext.IsNull() || aView.IsNull() )
1137 gp_Pnt aPoint1, aPoint2;
1138 //bool isFoundPoint = CurveCreator_Utils::getNeighbourPoints( aContext, aView, theX, theY,
1139 // aPoint, aPoint1, aPoint2 );
1140 Handle(AIS_InteractiveObject) anAISObject = myCurve->getAISObject();
1141 bool isFoundPoint = CurveCreator_Utils::pointOnObject( aView, anAISObject, theX, theY,
1142 aPoint, aPoint1, aPoint2 );
1143 if ( !isFoundPoint )
1146 // insert the point to the model curve
1147 CurveCreator_ICurve::SectionToPointList aSelPoints;
1148 startCurveModification( aSelPoints );
1150 CurveCreator::Coordinates aCoords;
1151 aCoords.push_back( aPoint.X() );
1152 aCoords.push_back( aPoint.Y() );
1154 CurveCreator_ICurve::SectionToPointList aPoints1, aPoints2;
1155 findSectionsToPoints( aPoint1.X(), aPoint1.Y(), aPoints1 );
1156 findSectionsToPoints( aPoint2.X(), aPoint2.Y(), aPoints2 );
1157 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aPoints1.begin(),
1158 aLast = aPoints1.end();
1159 int aSectionId = -1;
1160 // there can be a case when a new point is added into two sections
1161 int aPoint1Id = -1, aPoint2Id = -1;
1162 for ( ; anIt != aLast && aSectionId < 0; anIt++ ) {
1163 int aSectionCur = anIt->first;
1164 CurveCreator_ICurve::SectionToPointList::const_iterator anIt2 = aPoints2.begin(),
1165 aLast2 = aPoints2.end();
1166 for ( ; anIt2 != aLast2 && aSectionId < 0; anIt2++ ) {
1167 if ( anIt2->first == aSectionCur ) {
1168 aSectionId = aSectionCur;
1169 aPoint1Id = anIt->second;
1170 aPoint2Id = anIt2->second;
1175 int anInsertPos = -1;
1176 int aLastPoint = myCurve->getNbPoints( aSectionId )-1;
1177 if ( ( aPoint1Id == aLastPoint && aPoint2Id == 0 ) ||
1178 ( aPoint2Id == aLastPoint && aPoint1Id == 0 ) )
1179 anInsertPos = -1; // if the section happens between first and last points
1181 anInsertPos = aPoint1Id < aPoint2Id ? aPoint1Id + 1 : aPoint2Id + 1;
1183 myCurve->addPoints( aCoords, aSectionId, anInsertPos );
1184 mySectionView->pointsAdded( aSectionId, myCurve->getNbPoints( aSectionId ) );
1186 finishCurveModification( aSelPoints );
1188 setSelectedPoints();
1191 void CurveCreator_Widget::moveSelectedPoints( const int theXPosition,
1192 const int theYPosition )
1194 OCCViewer_ViewPort3d* aViewPort = getViewPort();
1198 CurveCreator_ICurve::SectionToPointList aPoints;
1199 startCurveModification( aPoints, false );
1201 gp_Pnt aStartPnt = CurveCreator_Utils::ConvertClickToPoint( myDragStartPosition.x(),
1202 myDragStartPosition.y(),
1203 aViewPort->getView() );
1204 gp_Pnt anEndPnt = CurveCreator_Utils::ConvertClickToPoint( theXPosition, theYPosition,
1205 aViewPort->getView() );
1206 double aXDelta = aStartPnt.X() - anEndPnt.X();
1207 double anYDelta = aStartPnt.Y() - anEndPnt.Y();
1209 CurveCreator_ICurve::SectionToPointCoordsList aCoordList;
1210 std::deque<float> aChangedPos;
1211 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = myDragPoints.begin(),
1212 aLast = myDragPoints.end();
1213 for ( ; anIt != aLast; anIt++ ) {
1214 int aSectionId = anIt->first;
1215 int aPointId = anIt->second;
1216 aChangedPos = myCurve->getPoint( aSectionId, aPointId );
1217 if ( aChangedPos.size() < 2 )
1219 aChangedPos[0] = aChangedPos[0] - aXDelta;
1220 aChangedPos[1] = aChangedPos[1] - anYDelta;
1222 aCoordList.push_back(
1223 std::make_pair(std::make_pair( aSectionId, aPointId ),
1226 myCurve->setSeveralPoints( aCoordList );
1229 finishCurveModification( myDragPoints );
1232 void CurveCreator_Widget::updateLocalPointView()
1234 if ( myDragStarted )
1236 Handle(AIS_InteractiveContext) aContext = getAISContext();
1237 if ( aContext.IsNull() )
1240 CurveCreator_Utils::getSelectedPoints( aContext, myCurve, myLocalPoints );
1241 int aNbPoints = myLocalPoints.size();
1242 bool isRowLimit = aNbPoints > myLocalPointRowLimit;
1243 myLocalPointView->setVisible( !isRowLimit );
1245 if ( !isRowLimit ) {
1246 bool isBlocked = myLocalPointView->blockSignals(true);
1247 myLocalPointView->setRowCount( 0 );
1248 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = myLocalPoints.begin(),
1249 aLast = myLocalPoints.end();
1250 for ( ; anIt != aLast; anIt++ )
1251 myLocalPointView->addLocalPointToTable( *anIt );
1253 myLocalPointView->blockSignals( isBlocked );
1260 void CurveCreator_Widget::setLocalPointContext( const bool theOpen, const bool isUpdateTable )
1262 CurveCreator_Utils::setLocalPointContext( getAISContext(), theOpen );
1263 if ( !theOpen && isUpdateTable )
1264 updateLocalPointView();
1268 * Set drag operation started. Save the position and a list of dragged points
1269 * \param theState the drag operation state: started/finished
1270 * \param thePoint the start drag position
1272 void CurveCreator_Widget::setDragStarted( const bool theState, const QPoint& thePoint )
1275 getSelectedPoints( myDragPoints );
1276 myDragStarted = myDragPoints.size();
1277 myDragStartPosition = thePoint;
1278 if ( myDragStarted ) {
1279 // change a viewer interaction style in order to avoid a select rectangle build
1280 myDragInteractionStyle = changeInteractionStyle( SUIT_ViewModel::KEY_FREE );
1284 if ( myDragStarted )
1285 changeInteractionStyle( myDragInteractionStyle );
1286 myDragStarted = false;
1287 myDragPoints.clear();
1292 void CurveCreator_Widget::getSelectedPoints( CurveCreator_ICurve::SectionToPointList& thePoints )
1295 thePoints = myLocalPoints;
1298 void CurveCreator_Widget::setSelectedPoints( const CurveCreator_ICurve::SectionToPointList& thePoints )
1300 if ( myDragStarted )
1302 Handle(AIS_InteractiveContext) aContext = getAISContext();
1303 if ( aContext.IsNull() || !aContext->HasOpenedContext() )
1306 CurveCreator_Utils::setSelectedPoints( aContext, myCurve, thePoints );
1308 updateLocalPointView();
1312 * Get viewer information before perform the curve modification.
1313 * Take a list of selected cuve points an close local context.
1314 * The context should be closed because the curve presentation is
1315 * redisplayed and if it is not closed, when we close the local context
1316 * later, the presentation shown in the local context is disappeared.
1317 * \param thePoints an output list of curve selected points
1318 * \param theFillPoints a flag whether the selection list should be filled
1320 void CurveCreator_Widget::startCurveModification(
1321 CurveCreator_ICurve::SectionToPointList& thePoints,
1322 const bool theFillPoints )
1324 if ( theFillPoints ) {
1326 getSelectedPoints( thePoints );
1328 setLocalPointContext( false );
1332 * Restore the viewer state after the curve modification is done.
1333 * Open local context and select given points inside it.
1334 * \param thePoints a list of curve selected points
1336 void CurveCreator_Widget::finishCurveModification(
1337 const CurveCreator_ICurve::SectionToPointList& thePoints )
1339 if ( getActionMode() == ModificationMode )
1340 setLocalPointContext( true );
1341 setSelectedPoints( thePoints );
1346 * Returns a point index in the model curve by the point coordinates in the viewer
1347 * \param theX the X coordinate of the point
1348 * \param theY the Y coordinate of the point
1350 int CurveCreator_Widget::findLocalPointIndex( int theSectionId, float theX, float theY )
1352 return CurveCreator_UtilsICurve::findLocalPointIndex( myCurve, theSectionId, theX, theY );
1355 void CurveCreator_Widget::findSectionsToPoints( const double theX, const double theY,
1356 CurveCreator_ICurve::SectionToPointList& thePoints )
1358 return CurveCreator_UtilsICurve::findSectionsToPoints( myCurve, theX, theY, thePoints );
1361 void CurveCreator_Widget::convert( const CurveCreator_ICurve::SectionToPointList& thePoints,
1362 QMap<int, QList<int> >& theConvPoints )
1364 return CurveCreator_UtilsICurve::convert( thePoints, theConvPoints );
1368 * Returns whethe the container has the value
1369 * \param theList a container of values
1370 * \param theValue a value
1372 bool CurveCreator_Widget::contains( const CurveCreator_ICurve::SectionToPointList& theList,
1373 const CurveCreator_ICurve::SectionToPoint& theValue ) const
1375 return CurveCreator_UtilsICurve::contains( theList, theValue );