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.hxx"
24 #include "CurveCreator_NewSectionDlg.h"
25 #include "CurveCreator_Utils.h"
26 #include "CurveCreator_UtilsICurve.hxx"
27 #include "CurveCreator_TableView.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>
37 #include "OCCViewer_Utilities.h"
39 #include <QHBoxLayout>
40 #include <QVBoxLayout>
44 #include <QToolButton>
48 #include <QMouseEvent>
49 #include <QApplication>
50 #include <QTableWidget>
53 //#define MEASURE_TIME
57 #define START_MEASURE_TIME \
61 #define END_MEASURE_TIME( theMsg ) \
62 double aTime = aTimer.elapsed() * 0.001; \
63 FILE* aFile = fopen( "performance", "a" ); \
64 fprintf( aFile, "%s = %.3lf sec\n", theMsg, aTime ); \
69 #define START_MEASURE_TIME
70 #define END_MEASURE_TIME( theMsg )
79 CurveCreator_Widget::CurveCreator_Widget(QWidget* parent,
80 CurveCreator_ICurve *theCurve,
81 const int theActionFlags,
83 int theLocalPointRowLimit )
84 : QWidget(parent), myNewSectionEditor(NULL), myCurve(theCurve), mySection(0),
85 myDragStarted( false ), myDragInteractionStyle( SUIT_ViewModel::STANDARD ),
86 myOCCViewer( 0 ), myLocalPointRowLimit( theLocalPointRowLimit )
88 myNewSectionEditor = new CurveCreator_NewSectionDlg( this );
89 myNewSectionEditor->hide();
90 connect( myNewSectionEditor, SIGNAL(addSection()), this, SLOT(onAddNewSection()) );
91 connect( myNewSectionEditor, SIGNAL(modifySection()), this, SLOT(onModifySection()) );
92 connect( myNewSectionEditor, SIGNAL(cancelSection()), this, SLOT(onCancelSection()) );
94 QGroupBox* aSectionGroup = new QGroupBox(tr("Sections"),this);
96 mySectionView = new CurveCreator_TreeView(myCurve, aSectionGroup);
97 connect( mySectionView, SIGNAL(selectionChanged()), this, SLOT( onSelectionChanged() ) );
98 connect( mySectionView, SIGNAL(sectionEntered(int)), this, SLOT(onEditSection(int)) );
99 connect( mySectionView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onContextMenu(QPoint)) );
101 myLocalPointView = new CurveCreator_TableView( myCurve, this );
102 connect( myLocalPointView, SIGNAL( cellChanged( int, int ) ),
103 this, SLOT( onCellChanged( int, int ) ) );
105 QToolBar* aTB = new QToolBar(tr("TOOL_BAR_TLT"), aSectionGroup);
106 // QToolButton* anUndoBtn = new QToolButton(aTB);
108 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
109 QPixmap anUndoPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_UNDO")));
110 QPixmap aRedoPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_REDO")));
111 QPixmap aNewSectionPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_NEW_SECTION")));
112 QPixmap aNewPointPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_NEW_POINT")));
113 QPixmap anEditPointsPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_EDIT_POINTS")));
114 QPixmap aDetectPointsPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_EDIT_POINTS")));
115 QPixmap aPolylinePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_POLYLINE")));
116 QPixmap aSplinePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_SPLINE")));
117 QPixmap aRemovePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_DELETE")));
118 QPixmap aJoinPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_JOIN")));
119 QPixmap aStepUpPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_ARROW_UP")));
120 QPixmap aStepDownPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_ARROW_DOWN")));
122 QAction* anAct = createAction( UNDO_ID, tr("UNDO"), anUndoPixmap, tr("UNDO_TLT"),
123 QKeySequence(Qt::ControlModifier|Qt::Key_Z) );
124 connect(anAct, SIGNAL(triggered()), this, SLOT(onUndo()) );
125 aTB->addAction(anAct);
127 anAct = createAction( REDO_ID, tr("REDO"), aRedoPixmap, tr("REDO_TLT"),
128 QKeySequence(Qt::ControlModifier|Qt::Key_Y) );
129 connect(anAct, SIGNAL(triggered()), this, SLOT(onRedo()) );
130 aTB->addAction(anAct);
134 anAct = createAction( NEW_SECTION_ID, tr("NEW_SECTION"), aNewSectionPixmap, tr("NEW_SECTION_TLT"),
135 QKeySequence(Qt::ControlModifier|Qt::Key_N) );
136 connect(anAct, SIGNAL(triggered()), this, SLOT(onNewSection()) );
137 if ( !(theActionFlags & DisableNewSection) ) {
138 aTB->addAction(anAct);
142 anAct = createAction( ADDITION_MODE_ID, tr("ADDITION_MODE"), aNewPointPixmap, tr("ADDITION_MODE_TLT"),
144 anAct->setCheckable(true);
145 connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onAdditionMode(bool)) );
146 connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
147 aTB->addAction(anAct);
149 anAct = createAction( MODIFICATION_MODE_ID, tr("MODIFICATION_MODE"), anEditPointsPixmap, tr("MODIFICATION_MODE_TLT"),
151 anAct->setCheckable(true);
152 connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onModificationMode(bool)) );
153 connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
154 aTB->addAction(anAct);
156 anAct = createAction( DETECTION_MODE_ID, tr("DETECTION_MODE"), aDetectPointsPixmap, tr("DETECTION_MODE_TLT"),
158 anAct->setCheckable(true);
159 connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onDetectionMode(bool)) );
160 connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
161 if ( !(theActionFlags & DisableDetectionMode) ) {
162 aTB->addAction(anAct);
165 anAct = createAction( CLOSE_SECTIONS_ID, tr("CLOSE_SECTIONS"), QPixmap(), tr("CLOSE_SECTIONS_TLT"),
166 QKeySequence(Qt::ControlModifier|Qt::Key_W) );
167 connect(anAct, SIGNAL(triggered()), this, SLOT(onCloseSections()) );
169 anAct = createAction( UNCLOSE_SECTIONS_ID, tr("UNCLOSE_SECTIONS"), QPixmap(),
170 tr("UNCLOSE_SECTIONS_TLT"), QKeySequence(Qt::ControlModifier|Qt::Key_S) );
171 connect(anAct, SIGNAL(triggered()), this, SLOT(onUncloseSections()) );
173 anAct = createAction( SET_SECTIONS_POLYLINE_ID, tr("SET_SECTIONS_POLYLINE"),
174 aPolylinePixmap, tr("SET_POLYLINE_TLT"),
175 QKeySequence(Qt::ControlModifier|Qt::Key_E) );
176 connect(anAct, SIGNAL(triggered()), this, SLOT(onSetPolyline()) );
178 anAct = createAction( SET_SECTIONS_SPLINE_ID, tr("SET_SECTIONS_SPLINE"), aSplinePixmap,
179 tr("SET_SPLINE_TLT"), QKeySequence(Qt::ControlModifier|Qt::Key_R) );
180 connect(anAct, SIGNAL(triggered()), this, SLOT(onSetSpline()) );
182 anAct = createAction( REMOVE_ID, tr("REMOVE"), aRemovePixmap, tr("REMOVE_TLT"),
183 QKeySequence(Qt::ControlModifier|Qt::Key_Delete ) );
184 connect(anAct, SIGNAL(triggered()), this, SLOT(onRemove()) );
185 aTB->addAction(anAct);
188 //aTB->addSeparator();
190 //anAct = createAction( JOIN_ID, tr("JOIN"), aJoinPixmap, tr("JOIN_TLT"),
191 // QKeySequence(Qt::ControlModifier|Qt::Key_Plus ) );
192 //connect( anAct, SIGNAL(triggered()), this, SLOT(onJoin()) );
193 //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()) );
200 //anAct = createAction( JOIN_ALL_ID, tr("JOIN_ALL"), QPixmap(), tr("JOIN_ALL_TLT"),
201 // QKeySequence(Qt::ControlModifier | Qt::ShiftModifier | Qt::Key_Plus ) );
202 //connect( anAct, SIGNAL(triggered()), this, SLOT(onJoinAll()) );
204 QVBoxLayout* aSectLayout = new QVBoxLayout();
205 aSectLayout->setMargin( 5 );
206 aSectLayout->setSpacing( 5 );
207 aSectLayout->addWidget(aTB);
208 aSectLayout->addWidget(mySectionView);
209 aSectLayout->addWidget( myLocalPointView );
210 aSectionGroup->setLayout(aSectLayout);
211 QVBoxLayout* aLay = new QVBoxLayout();
212 aLay->setMargin( 0 );
213 aLay->setSpacing( 5 );
214 // aLay->addLayout(aNameLayout);
215 aLay->addWidget(aSectionGroup);
218 updateActionsStates();
225 void CurveCreator_Widget::setOCCViewer( OCCViewer_Viewer* theViewer )
227 if ( myOCCViewer == theViewer )
231 OCCViewer_ViewManager* aViewManager = dynamic_cast<OCCViewer_ViewManager*>
232 ( myOCCViewer->getViewManager() );
233 disconnect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
234 this, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ) );
235 disconnect( aViewManager, SIGNAL( mouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ),
236 this, SLOT( onMouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ) );
237 disconnect( aViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
238 this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
239 disconnect( aViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
240 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
241 // restore normal mode in the viewer
242 OCCViewer_Utilities::setViewer2DMode( myOCCViewer, OCCViewer_ViewWindow::No2dMode );
243 // all local contexts should be closed if the viewer is not more used
244 setLocalPointContext( false, true );
247 myOCCViewer = theViewer;
249 OCCViewer_ViewManager* aViewManager = dynamic_cast<OCCViewer_ViewManager*>
250 ( myOCCViewer->getViewManager() );
251 connect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
252 this, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ) );
253 connect( aViewManager, SIGNAL( mouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ),
254 this, SLOT( onMouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ) );
255 connect( aViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
256 this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
257 connect( aViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
258 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
259 OCCViewer_Utilities::setViewer2DMode( myOCCViewer, OCCViewer_ViewWindow::XYPlane );
264 * Returns current OCC viewer
266 OCCViewer_Viewer* CurveCreator_Widget::getOCCViewer()
272 * Returns OCC viewer context
274 Handle(AIS_InteractiveContext) CurveCreator_Widget::getAISContext()
276 Handle(AIS_InteractiveContext) aContext;
277 OCCViewer_Viewer* aViewer = getOCCViewer();
279 aContext = aViewer->getAISContext();
285 * Returns OCC viewer view port
287 OCCViewer_ViewPort3d* CurveCreator_Widget::getViewPort()
289 OCCViewer_ViewPort3d* aViewPort = 0;
290 OCCViewer_Viewer* aViewer = getOCCViewer();
292 aViewPort = ((OCCViewer_ViewWindow*)aViewer->getViewManager()->getActiveView())->getViewPort();
298 * Set interaction style in the OCC viewer
299 * \param theStyle a new style
300 * \return the previous style
302 int CurveCreator_Widget::changeInteractionStyle( int theStyle )
304 OCCViewer_Viewer* aViewer = getOCCViewer();
308 int aPrevStyle = aViewer->interactionStyle();
309 aViewer->setInteractionStyle( theStyle );
314 //=======================================================================
316 // purpose: reset the widget viewer, close local context, clear selection
317 //=======================================================================
318 void CurveCreator_Widget::reset()
322 void CurveCreator_Widget::setCurve( CurveCreator_ICurve* theCurve )
325 mySectionView->setCurve( myCurve );
326 myLocalPointView->setCurve( myCurve );
327 updateActionsStates();
331 void CurveCreator_Widget::onSelectionChanged()
333 updateActionsStates();
335 emit selectionChanged();
338 void CurveCreator_Widget::updateActionsStates()
340 QList<ActionId> anEnabledAct;
342 anEnabledAct << NEW_SECTION_ID << MODIFICATION_MODE_ID;
343 if ( removeEnabled() )
344 anEnabledAct << REMOVE_ID;
345 QList<int> aSelSections = mySectionView->getSelectedSections();
346 QList< QPair< int, int > > aSelPoints = mySectionView->getSelectedPoints();
347 CurveCreator_TreeView::SelectionType aSelType = mySectionView->getSelectionType();
349 case CurveCreator_TreeView::ST_NOSEL:{
352 case CurveCreator_TreeView::ST_SECTIONS:{
353 /*if( aSelSections[0] > 0 ){
354 anEnabledAct << UP_ID;
356 if( aSelSections.size() == 1 ){
357 anEnabledAct << ADDITION_MODE_ID << DETECTION_MODE_ID;
359 switch ( getActionMode() ) {
363 QList<int> aSelSection = mySectionView->getSelectedSections();
364 if( aSelSection.size() > 0 ){
365 mySection = aSelSection[0];
366 myPointNum = myCurve->getNbPoints(mySection);
370 case ModificationMode: {
371 anEnabledAct << CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID << SET_SECTIONS_POLYLINE_ID << SET_SECTIONS_SPLINE_ID;
372 int aSectCnt = myCurve->getNbSections();
374 anEnabledAct << CLEAR_ALL_ID;
377 // anEnabledAct << JOIN_ALL_ID;
378 //if( aSelSections.size() > 1 ){
379 // anEnabledAct << JOIN_ID;
383 case DetectionMode: {
390 /*if( aSelSections[ aSelSections.size() - 1 ] < ( myCurve->getNbSections() - 1 ) ){
391 anEnabledAct << DOWN_ID;
395 /*case CurveCreator_TreeView::ST_POINTS_ONE_SECTION:{
396 if( aSelPoints[0].second > 0 ){
397 anEnabledAct << UP_ID;
399 int aLastIndex = aSelPoints.size()-1;
400 int aSect = aSelPoints[0].first;
401 if( aSelPoints[aLastIndex].second < (myCurve->getNbPoints(aSect) - 1)){
402 anEnabledAct << DOWN_ID;
404 if( aSelPoints.size() == 1){
405 anEnabledAct << INSERT_POINT_BEFORE_ID << INSERT_POINT_AFTER_ID;
412 /*int aSelObjsCnt = aSelPoints.size() + aSelSections.size();
413 if( aSelObjsCnt > 0 ){
414 anEnabledAct << REMOVE_ID;
416 if( (myCurve->getNbSections() + myCurve->getNbPoints()) > 0 ){
417 anEnabledAct << REMOVE_ALL_ID;
420 //if( myCurve->getNbSections() > 1 ){
421 // anEnabledAct << JOIN_ALL_ID;
424 QList<ActionId> anIds = myActionMap.keys();
425 for( int i = 0 ; i < anIds.size() ; i++ ){
426 if( myActionMap.contains(anIds[i]) ){
427 if( anEnabledAct.contains(anIds[i]) ){
428 myActionMap[anIds[i]]->setEnabled(true);
431 myActionMap[anIds[i]]->setEnabled(false);
437 void CurveCreator_Widget::onAdditionMode(bool checked)
442 Handle(AIS_InteractiveContext) aContext = getAISContext();
443 if( !myCurve || aContext.IsNull() )
448 QList<int> aSelSection = mySectionView->getSelectedSections();
449 if( aSelSection.size() > 0 ){
450 mySection = aSelSection[0];
453 QList< QPair<int,int> > aSelPoints = mySectionView->getSelectedPoints();
454 if( aSelPoints.size() > 0 ){
455 mySection = aSelPoints[0].first;
456 myPointNum = aSelPoints[0].second + 1;
459 // emit subOperationStarted( myNewPointEditor );
462 void CurveCreator_Widget::onModificationMode(bool checked)
464 myLocalPointView->setVisible( checked );
467 void CurveCreator_Widget::onDetectionMode(bool checked)
471 void CurveCreator_Widget::onModeChanged(bool checked)
473 ActionMode aMode = NoneMode;
475 QAction* anAction = (QAction*)sender();
476 switch(myActionMap.key(anAction)) {
477 case ADDITION_MODE_ID:
478 aMode = AdditionMode;
479 if (myActionMap[MODIFICATION_MODE_ID]->isChecked())
480 myActionMap[MODIFICATION_MODE_ID]->trigger();
481 else if (myActionMap[DETECTION_MODE_ID]->isChecked())
482 myActionMap[DETECTION_MODE_ID]->trigger();
484 case MODIFICATION_MODE_ID:
485 aMode = ModificationMode;
486 if (myActionMap[ADDITION_MODE_ID]->isChecked())
487 myActionMap[ADDITION_MODE_ID]->trigger();
488 else if (myActionMap[DETECTION_MODE_ID]->isChecked())
489 myActionMap[DETECTION_MODE_ID]->trigger();
491 case DETECTION_MODE_ID:
492 aMode = DetectionMode;
493 if (myActionMap[ADDITION_MODE_ID]->isChecked())
494 myActionMap[ADDITION_MODE_ID]->trigger();
495 else if (myActionMap[MODIFICATION_MODE_ID]->isChecked())
496 myActionMap[MODIFICATION_MODE_ID]->trigger();
500 updateActionsStates();
502 setLocalPointContext( aMode == ModificationMode, true );
505 void CurveCreator_Widget::onNewSection()
511 myNewSectionEditor->clear();
512 myNewSectionEditor->setEditMode(false);
513 QString aSectName = QString( CurveCreator_UtilsICurve::getUniqSectionName( myCurve ).c_str() );
514 myNewSectionEditor->setSectionParameters(aSectName, true, CurveCreator::Polyline );
515 emit subOperationStarted( myNewSectionEditor );
518 void CurveCreator_Widget::onAddNewSection()
522 myCurve->addSection( myNewSectionEditor->getName().toStdString(),
523 myNewSectionEditor->getSectionType(),
524 myNewSectionEditor->isClosed() );
525 mySectionView->sectionAdded( -1 ); // add a new section to the end of list
526 QString aNewName = QString( CurveCreator_UtilsICurve::getUniqSectionName( myCurve ).c_str() );
527 myNewSectionEditor->setSectionName(aNewName);
528 updateActionsStates();
533 void CurveCreator_Widget::onCancelSection()
535 emit subOperationFinished( myNewSectionEditor );
538 QAction* CurveCreator_Widget::createAction( ActionId theId, const QString& theName, const QPixmap& theImage,
539 const QString& theToolTip, const QKeySequence& theShortcut )
541 QAction* anAct = new QAction(theName,this);
542 if( !theImage.isNull() ){
543 anAct->setIcon(theImage);
545 anAct->setShortcut(theShortcut);
546 anAct->setToolTip(theToolTip);
547 myActionMap[theId] = anAct;
551 QAction* CurveCreator_Widget::getAction( ActionId theId )
553 if( myActionMap.contains(theId) )
554 return myActionMap[theId];
558 QAction* CurveCreator_Widget::getAction( ActionMode theMode )
560 ActionId anActionId = NONE_ID;
563 anActionId = ADDITION_MODE_ID;
565 case ModificationMode:
566 anActionId = MODIFICATION_MODE_ID;
569 anActionId = DETECTION_MODE_ID;
574 QAction* anAction = 0;
575 if ( anActionId != NONE_ID && myActionMap.contains( anActionId ) )
576 anAction = myActionMap[anActionId];
580 void CurveCreator_Widget::setActionMode( const ActionMode& theMode )
582 ActionMode aPrevMode = getActionMode();
583 QAction* aPrevAction = getAction( aPrevMode );
587 if ( aPrevAction->isChecked() )
588 aPrevAction->setChecked( false );
590 if ( aPrevMode == ModificationMode )
591 onModificationMode( false );
592 if ( aPrevMode == AdditionMode )
593 onAdditionMode( false );
600 case ModificationMode:
610 CurveCreator_Widget::ActionMode CurveCreator_Widget::getActionMode() const
612 ActionMode aMode = NoneMode;
614 if ( myActionMap[ADDITION_MODE_ID]->isChecked() )
615 aMode = AdditionMode;
616 else if ( myActionMap[MODIFICATION_MODE_ID]->isChecked() )
617 aMode = ModificationMode;
618 else if ( myActionMap[DETECTION_MODE_ID]->isChecked() )
619 aMode = DetectionMode;
624 void CurveCreator_Widget::onEditSection( int theSection )
630 mySection = theSection;
631 QString aSectName = QString::fromStdString( myCurve->getSectionName(theSection));
632 bool isClosed = myCurve->isClosed(theSection);
633 CurveCreator::SectionType aType = myCurve->getSectionType(theSection);
634 myNewSectionEditor->setEditMode(true);
635 myNewSectionEditor->setSectionParameters( aSectName, isClosed, aType );
637 emit subOperationStarted( myNewSectionEditor );
640 void CurveCreator_Widget::onModifySection()
644 QString aName = myNewSectionEditor->getName();
645 bool isClosed = myNewSectionEditor->isClosed();
646 CurveCreator::SectionType aSectType = myNewSectionEditor->getSectionType();
647 if( myCurve->getSectionName(mySection) != aName.toStdString() )
648 myCurve->setSectionName( mySection , aName.toStdString() );
650 if( myCurve->getSectionType(mySection) != aSectType )
651 myCurve->setSectionType( mySection, aSectType );
653 if( myCurve->isClosed(mySection) != isClosed )
654 myCurve->setClosed( mySection, isClosed );
655 mySectionView->sectionChanged(mySection);
660 void CurveCreator_Widget::onJoin()
664 QList<int> aSections = mySectionView->getSelectedSections();
665 if( aSections.size() == 0 ){
670 int aMainSect = aSections[0];
671 int aMainSectSize = myCurve->getNbPoints(aMainSect);
672 for( int i = 1 ; i < aSections.size() ; i++ ){
673 int aSectNum = aSections[i] - (i-1);
674 myCurve->join( aMainSect, aSectNum );
675 mySectionView->sectionsRemoved( aSectNum );
677 int aNewSectSize = myCurve->getNbPoints(aMainSect);
678 if( aNewSectSize != aMainSectSize )
679 mySectionView->pointsAdded( aMainSect, aMainSectSize, aNewSectSize-aMainSectSize );
683 void CurveCreator_Widget::onRemove()
688 switch( getActionMode() ) {
692 case ModificationMode:
700 void CurveCreator_Widget::onClearAll()
706 mySectionView->reset();
707 updateActionsStates();
711 void CurveCreator_Widget::onJoinAll()
717 mySectionView->reset();
718 updateActionsStates();
722 void CurveCreator_Widget::onUndoSettings()
727 void CurveCreator_Widget::onSetSpline()
732 QList<int> aSelSections = mySectionView->getSelectedSections();
733 for( int i = 0 ; i < aSelSections.size() ; i++ ){
734 myCurve->setSectionType(aSelSections[i], CurveCreator::Spline );
735 mySectionView->sectionChanged(aSelSections[i]);
740 void CurveCreator_Widget::onSetPolyline()
745 QList<int> aSelSections = mySectionView->getSelectedSections();
746 for( int i = 0 ; i < aSelSections.size() ; i++ ){
747 myCurve->setSectionType( aSelSections[i], CurveCreator::Polyline );
748 mySectionView->sectionChanged( aSelSections[i] );
753 void CurveCreator_Widget::onCloseSections()
758 QList<int> aSelSections = mySectionView->getSelectedSections();
759 for( int i = 0 ; i < aSelSections.size() ; i++ ){
760 myCurve->setClosed(aSelSections[i], true);
761 mySectionView->sectionChanged(aSelSections[i]);
766 void CurveCreator_Widget::onUncloseSections()
771 QList<int> aSelSections = mySectionView->getSelectedSections();
772 for( int i = 0 ; i < aSelSections.size() ; i++ ){
773 myCurve->setClosed(aSelSections[i], false);
774 mySectionView->sectionChanged(aSelSections[i]);
779 void CurveCreator_Widget::onUndo()
784 CurveCreator_ICurve::SectionToPointList aPoints;
785 startCurveModification( aPoints, false );
787 finishCurveModification();
788 mySectionView->reset();
791 void CurveCreator_Widget::onRedo()
795 CurveCreator_ICurve::SectionToPointList aPoints;
796 startCurveModification( aPoints, false );
798 finishCurveModification();
799 mySectionView->reset();
802 void CurveCreator_Widget::updateUndoRedo()
806 QAction* anAct = myActionMap[UNDO_ID];
808 if( myCurve->getNbUndo() != 0 ){
809 anAct->setEnabled(true);
812 anAct->setDisabled(true);
815 anAct = myActionMap[REDO_ID];
817 if( myCurve->getNbRedo() != 0 ){
818 anAct->setEnabled(true);
821 anAct->setDisabled(true);
826 void CurveCreator_Widget::onContextMenu( QPoint thePoint )
828 QList<ActionId> aContextActions;
829 aContextActions << CLEAR_ALL_ID << JOIN_ALL_ID << SEPARATOR_ID <<
830 CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID << SET_SECTIONS_POLYLINE_ID <<
831 SET_SECTIONS_SPLINE_ID;
832 QPoint aGlPoint = mySectionView->mapToGlobal(thePoint);
834 QList<ActionId> aResAct;
835 for( int i = 0 ; i < aContextActions.size() ; i++ ){
836 if( aContextActions[i] != SEPARATOR_ID ){
837 if( myActionMap.contains(aContextActions[i]) ){
838 QAction* anAct = myActionMap[aContextActions[i]];
839 if( anAct->isEnabled() ){
840 aResAct << aContextActions[i];
846 aResAct << SEPARATOR_ID;
852 QMenu* aMenu = new QMenu(this);
853 for( int i = 0 ; i < aResAct.size() ; i++ ){
854 if( aResAct[i] == SEPARATOR_ID ){
855 aMenu->addSeparator();
858 QAction* anAct = myActionMap[aResAct[i]];
859 aMenu->insertAction(NULL, anAct);
862 aMenu->exec(aGlPoint);
865 QList<int> CurveCreator_Widget::getSelectedSections()
867 return mySectionView->getSelectedSections();
870 QList< QPair< int, int > > CurveCreator_Widget::getSelectedPoints()
872 return mySectionView->getSelectedPoints();
876 * According to the widget state, performs the remove action
878 void CurveCreator_Widget::removeSelected()
884 * Checks whether there are some selection to be removed
886 bool CurveCreator_Widget::removeEnabled()
888 bool isEnabled = getActionMode() == ModificationMode;
890 QList<int> aSelSections = mySectionView->getSelectedSections();
891 CurveCreator_TreeView::SelectionType aSelType = mySectionView->getSelectionType();
892 isEnabled = aSelType == CurveCreator_TreeView::ST_SECTIONS &&
893 aSelSections.size() == 1;
899 //=================================================================================
900 // function : GeometryGUI::addCoordsByClick()
901 // purpose : Manage mouse press events in Additon mode
902 //=================================================================================
903 void CurveCreator_Widget::addCoordsByClick( QMouseEvent* pe )
905 if (pe->button() != Qt::LeftButton)
908 if ( pe->modifiers() != Qt::ControlModifier ) {
909 Handle(AIS_InteractiveContext) ic = getAISContext();
916 if ( pe->modifiers() == Qt::ShiftModifier )
917 ic->ShiftSelect(); // Append selection
919 ic->Select(); // New selection
922 OCCViewer_ViewPort3d* vp = getViewPort();
923 aPnt = CurveCreator_Utils::ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
925 // set the coordinates into dialog
926 CurveCreator::Coordinates aCoords;
927 aCoords.push_back( aPnt.X() );
928 aCoords.push_back( aPnt.Y() );
929 if ( myCurve->getDimension() == 3 ) {
930 aCoords.push_back( aPnt.Z() );
932 addNewPoint(aCoords);
937 * Manage mouse press events
938 * \param theWindow an owner of the signal
939 * \param theEvent a mouse event
941 void CurveCreator_Widget::onMousePress( SUIT_ViewWindow*, QMouseEvent* theEvent )
943 if ( theEvent->button() != Qt::LeftButton )
946 switch( getActionMode() ) {
947 case ModificationMode: {
948 //store initial cursor position for Drag&Drop
949 setDragStarted( true, theEvent->pos() );
953 addCoordsByClick( theEvent );
962 * Manage mouse release events in Modification mode
963 * \param theWindow an owner of the signal
964 * \param theEvent a mouse event
966 void CurveCreator_Widget::onMouseRelease( SUIT_ViewWindow*, QMouseEvent* theEvent )
968 if ( getActionMode() != ModificationMode )
971 if ( myDragStarted ) {
972 bool isDragged = myDragged;
973 CurveCreator_ICurve::SectionToPointList aDraggedPoints;
975 aDraggedPoints = myDragPoints;
977 setDragStarted( false );
979 if ( aDraggedPoints.size() > 0 ) {
980 if ( myCurve->canPointsBeSorted() ) {
981 // Apply points sorting
982 CurveCreator_ICurve::SectionToPointList aPoints;
983 startCurveModification( aPoints, false );
985 myCurve->setSkipSorting( false );
987 CurveCreator_ICurve::SectionToPointCoordsList aCoordList;
988 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aDraggedPoints.begin(),
989 aLast = aDraggedPoints.end();
990 for ( ; anIt != aLast; anIt++ ) {
991 int aSectionId = anIt->first;
992 int aPointId = anIt->second;
993 std::deque<float> aPos = myCurve->getPoint( aSectionId, aPointId );
995 aCoordList.push_back(
996 std::make_pair( std::make_pair( aSectionId, aPointId ), aPos ) );
999 myCurve->setSeveralPoints( aCoordList );
1001 finishCurveModification( aDraggedPoints );
1003 // if the drag of some points has happened, restore the drag selection
1004 if ( aDraggedPoints.size() > 0 ) {
1006 setSelectedPoints( aDraggedPoints );
1007 END_MEASURE_TIME( "drop" );
1012 else // check whether the segment is clicked an a new point should be added to the segment
1013 insertPointToSelectedSegment( theEvent->pos().x(), theEvent->pos().y() );
1015 // updates the input panel table to show the selected point coordinates
1016 updateLocalPointView();
1020 * Manage mouse move events in Modification mode
1021 * \param theWindow an owner of the signal
1022 * \param theEvent a mouse event
1024 void CurveCreator_Widget::onMouseMove( SUIT_ViewWindow*, QMouseEvent* theEvent )
1026 if ( getActionMode() != ModificationMode || !myDragStarted )
1029 QPoint aPos = theEvent->pos();
1030 if ( (aPos - myDragStartPosition).manhattanLength() < QApplication::startDragDistance() )
1035 moveSelectedPoints( aPos.x(), aPos.y() );
1036 myDragStartPosition = aPos;
1038 END_MEASURE_TIME( "drag" );
1042 * Set zero viewer by the last view closed in
1043 * \param theManager a viewer manager
1045 void CurveCreator_Widget::onLastViewClosed( SUIT_ViewManager* theManager )
1050 void CurveCreator_Widget::onMousePress( QMouseEvent* theEvent )
1052 onMousePress( 0, theEvent );
1055 void CurveCreator_Widget::onMouseRelease( QMouseEvent* theEvent )
1057 onMouseRelease( 0, theEvent );
1060 void CurveCreator_Widget::onMouseMove( QMouseEvent* theEvent )
1062 onMouseMove( 0, theEvent );
1065 void CurveCreator_Widget::onCellChanged( int theRow, int theColumn )
1067 int aCurrSect = myLocalPointView->getSectionId( theRow );
1068 int aPntIndex = myLocalPointView->getPointId( theRow );
1070 if ( aPntIndex < 0 )
1073 CurveCreator_ICurve::SectionToPointList aSelPoints;
1074 startCurveModification( aSelPoints );
1076 double aX = myLocalPointView->item( theRow, 2 )->data( Qt::UserRole ).toDouble();
1077 double anY = myLocalPointView->item( theRow, 3 )->data( Qt::UserRole ).toDouble();
1078 std::deque<float> aChangedPos;
1079 aChangedPos.push_back( aX );
1080 aChangedPos.push_back( anY );
1081 myCurve->setPoint( aCurrSect, aPntIndex, aChangedPos );
1083 finishCurveModification( aSelPoints );
1087 * Removes a selected section from the curve. Updates undo/redo status
1089 void CurveCreator_Widget::removeSection()
1093 QList< QPair<int,int> > aSelPoints = mySectionView->getSelectedPoints();
1096 for( int i = 0 ; i < aSelPoints.size() ; i++ ){
1097 if( aCurrSect != aSelPoints[i].first ){
1099 aCurrSect = aSelPoints[i].first;
1101 int aPntIndx = aSelPoints[i].second - aRemoveCnt;
1102 myCurve->removePoint( aCurrSect, aPntIndx );
1103 mySectionView->pointsRemoved( aCurrSect, aPntIndx );
1106 QList<int> aSections = mySectionView->getSelectedSections();
1107 for( int i = 0 ; i < aSections.size() ; i++ ){
1108 int aSectNum = aSections[i] - (i);
1109 myCurve->removeSection( aSectNum );
1110 mySectionView->sectionsRemoved( aSectNum );
1112 mySectionView->clearSelection();
1117 * Removes a selected points from the curve. Updates undo/redo status
1119 void CurveCreator_Widget::removePoint()
1121 CurveCreator_ICurve::SectionToPointList aPoints;
1122 getSelectedPoints( aPoints );
1123 if ( aPoints.size() == 0 )
1126 CurveCreator_ICurve::SectionToPointList aSelPoints;
1127 startCurveModification( aSelPoints, false );
1129 myCurve->removeSeveralPoints( aPoints );
1130 finishCurveModification( CurveCreator_ICurve::SectionToPointList() );
1133 void CurveCreator_Widget::addNewPoint(const CurveCreator::Coordinates& theCoords)
1137 QList<int> aSections = mySectionView->getSelectedSections();
1138 if( aSections.size() == 0 ){
1141 int aSection = aSections[0];
1142 myCurve->addPoints(theCoords, aSection); // add to the end of section
1143 mySectionView->pointsAdded( aSection, myCurve->getNbPoints( aSection ) );
1144 updateActionsStates();
1148 void CurveCreator_Widget::insertPointToSelectedSegment( const int theX,
1151 Handle(AIS_InteractiveContext) aContext = getAISContext();
1153 OCCViewer_ViewPort3d* aViewPort = getViewPort();
1154 Handle(V3d_View) aView;
1156 aView = aViewPort->getView();
1158 if ( aContext.IsNull() || aView.IsNull() )
1161 gp_Pnt aPoint1, aPoint2;
1162 Handle(AIS_InteractiveObject) anAISObject = myCurve->getAISObject();
1163 bool isFoundPoint = CurveCreator_Utils::pointOnObject( aView, anAISObject, theX, theY,
1164 aPoint, aPoint1, aPoint2 );
1165 if ( !isFoundPoint )
1168 // insert the point to the model curve
1169 CurveCreator_ICurve::SectionToPointList aSelPoints;
1170 startCurveModification( aSelPoints );
1172 CurveCreator::Coordinates aCoords;
1173 aCoords.push_back( aPoint.X() );
1174 aCoords.push_back( aPoint.Y() );
1176 CurveCreator_ICurve::SectionToPointList aPoints1, aPoints2;
1177 findSectionsToPoints( aPoint1.X(), aPoint1.Y(), aPoints1 );
1178 findSectionsToPoints( aPoint2.X(), aPoint2.Y(), aPoints2 );
1179 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aPoints1.begin(),
1180 aLast = aPoints1.end();
1181 int aSectionId = -1;
1182 // there can be a case when a new point is added into two sections
1183 int aPoint1Id = -1, aPoint2Id = -1;
1184 for ( ; anIt != aLast && aSectionId < 0; anIt++ ) {
1185 int aSectionCur = anIt->first;
1186 CurveCreator_ICurve::SectionToPointList::const_iterator anIt2 = aPoints2.begin(),
1187 aLast2 = aPoints2.end();
1188 for ( ; anIt2 != aLast2 && aSectionId < 0; anIt2++ ) {
1189 if ( anIt2->first == aSectionCur ) {
1190 aSectionId = aSectionCur;
1191 aPoint1Id = anIt->second;
1192 aPoint2Id = anIt2->second;
1197 int anInsertPos = -1;
1198 int aLastPoint = myCurve->getNbPoints( aSectionId )-1;
1199 if ( ( aPoint1Id == aLastPoint && aPoint2Id == 0 ) ||
1200 ( aPoint2Id == aLastPoint && aPoint1Id == 0 ) )
1201 anInsertPos = -1; // if the section happens between first and last points
1203 anInsertPos = aPoint1Id < aPoint2Id ? aPoint1Id + 1 : aPoint2Id + 1;
1205 myCurve->addPoints( aCoords, aSectionId, anInsertPos );
1206 mySectionView->pointsAdded( aSectionId, myCurve->getNbPoints( aSectionId ) );
1208 finishCurveModification( aSelPoints );
1210 setSelectedPoints();
1213 void CurveCreator_Widget::moveSelectedPoints( const int theXPosition,
1214 const int theYPosition )
1216 OCCViewer_ViewPort3d* aViewPort = getViewPort();
1220 CurveCreator_ICurve::SectionToPointList aPoints;
1221 startCurveModification( aPoints, false );
1223 gp_Pnt aStartPnt = CurveCreator_Utils::ConvertClickToPoint( myDragStartPosition.x(),
1224 myDragStartPosition.y(),
1225 aViewPort->getView() );
1226 gp_Pnt anEndPnt = CurveCreator_Utils::ConvertClickToPoint( theXPosition, theYPosition,
1227 aViewPort->getView() );
1228 double aXDelta = aStartPnt.X() - anEndPnt.X();
1229 double anYDelta = aStartPnt.Y() - anEndPnt.Y();
1231 CurveCreator_ICurve::SectionToPointCoordsList aCoordList;
1232 std::deque<float> aChangedPos;
1233 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = myDragPoints.begin(),
1234 aLast = myDragPoints.end();
1235 for ( ; anIt != aLast; anIt++ ) {
1236 int aSectionId = anIt->first;
1237 int aPointId = anIt->second;
1238 aChangedPos = myCurve->getPoint( aSectionId, aPointId );
1239 if ( aChangedPos.size() < 2 )
1241 aChangedPos[0] = aChangedPos[0] - aXDelta;
1242 aChangedPos[1] = aChangedPos[1] - anYDelta;
1244 aCoordList.push_back(
1245 std::make_pair(std::make_pair( aSectionId, aPointId ),
1248 myCurve->setSeveralPoints( aCoordList );
1251 finishCurveModification( myDragPoints );
1254 void CurveCreator_Widget::updateLocalPointView()
1256 if ( myDragStarted )
1258 Handle(AIS_InteractiveContext) aContext = getAISContext();
1259 if ( aContext.IsNull() )
1262 CurveCreator_Utils::getSelectedPoints( aContext, myCurve, myLocalPoints );
1263 int aNbPoints = myLocalPoints.size();
1265 bool isRowLimit = aNbPoints > myLocalPointRowLimit;
1266 myLocalPointView->setVisible( getActionMode() == ModificationMode && !isRowLimit );
1268 if ( !isRowLimit ) {
1269 bool isBlocked = myLocalPointView->blockSignals(true);
1270 myLocalPointView->setRowCount( 0 );
1271 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = myLocalPoints.begin(),
1272 aLast = myLocalPoints.end();
1273 for ( ; anIt != aLast; anIt++ )
1274 myLocalPointView->addLocalPointToTable( *anIt );
1276 myLocalPointView->blockSignals( isBlocked );
1283 void CurveCreator_Widget::setLocalPointContext( const bool theOpen, const bool isUpdateTable )
1285 CurveCreator_Utils::setLocalPointContext( myCurve, getAISContext(), theOpen );
1286 if ( !theOpen && isUpdateTable )
1287 updateLocalPointView();
1291 * Set drag operation started. Save the position and a list of dragged points
1292 * \param theState the drag operation state: started/finished
1293 * \param thePoint the start drag position
1295 void CurveCreator_Widget::setDragStarted( const bool theState, const QPoint& thePoint )
1298 getSelectedPoints( myDragPoints );
1300 myDragStarted = myDragPoints.size();
1301 myDragStartPosition = thePoint;
1302 if ( myDragStarted ) {
1303 // change a viewer interaction style in order to avoid a select rectangle build
1304 myDragInteractionStyle = changeInteractionStyle( SUIT_ViewModel::KEY_FREE );
1305 myCurve->setSkipSorting( true );
1309 if ( myDragStarted )
1310 changeInteractionStyle( myDragInteractionStyle );
1311 myDragStarted = false;
1312 myDragPoints.clear();
1317 void CurveCreator_Widget::getSelectedPoints( CurveCreator_ICurve::SectionToPointList& thePoints )
1320 thePoints = myLocalPoints;
1323 void CurveCreator_Widget::setSelectedPoints( const CurveCreator_ICurve::SectionToPointList& thePoints )
1325 if ( myDragStarted )
1327 Handle(AIS_InteractiveContext) aContext = getAISContext();
1328 if ( aContext.IsNull() || !aContext->HasOpenedContext() )
1331 CurveCreator_Utils::setSelectedPoints( aContext, myCurve, thePoints );
1333 updateLocalPointView();
1336 void CurveCreator_Widget::stopActionMode()
1338 setActionMode( NoneMode );
1342 * Get viewer information before perform the curve modification.
1343 * Take a list of selected cuve points an close local context.
1344 * The context should be closed because the curve presentation is
1345 * redisplayed and if it is not closed, when we close the local context
1346 * later, the presentation shown in the local context is disappeared.
1347 * \param thePoints an output list of curve selected points
1348 * \param theFillPoints a flag whether the selection list should be filled
1350 void CurveCreator_Widget::startCurveModification(
1351 CurveCreator_ICurve::SectionToPointList& thePoints,
1352 const bool theFillPoints )
1354 if ( theFillPoints ) {
1356 getSelectedPoints( thePoints );
1358 setLocalPointContext( false );
1362 * Restore the viewer state after the curve modification is done.
1363 * Open local context and select given points inside it.
1364 * \param thePoints a list of curve selected points
1366 void CurveCreator_Widget::finishCurveModification(
1367 const CurveCreator_ICurve::SectionToPointList& thePoints )
1369 if ( getActionMode() == ModificationMode )
1370 setLocalPointContext( true );
1371 setSelectedPoints( thePoints );
1376 * Returns a point index in the model curve by the point coordinates in the viewer
1377 * \param theX the X coordinate of the point
1378 * \param theY the Y coordinate of the point
1380 int CurveCreator_Widget::findLocalPointIndex( int theSectionId, float theX, float theY )
1382 return CurveCreator_UtilsICurve::findLocalPointIndex( myCurve, theSectionId, theX, theY );
1385 void CurveCreator_Widget::findSectionsToPoints( const double theX, const double theY,
1386 CurveCreator_ICurve::SectionToPointList& thePoints )
1388 return CurveCreator_UtilsICurve::findSectionsToPoints( myCurve, theX, theY, thePoints );
1391 void CurveCreator_Widget::convert( const CurveCreator_ICurve::SectionToPointList& thePoints,
1392 QMap<int, QList<int> >& theConvPoints )
1394 return CurveCreator_UtilsICurve::convert( thePoints, theConvPoints );
1398 * Returns whethe the container has the value
1399 * \param theList a container of values
1400 * \param theValue a value
1402 bool CurveCreator_Widget::contains( const CurveCreator_ICurve::SectionToPointList& theList,
1403 const CurveCreator_ICurve::SectionToPoint& theValue ) const
1405 return CurveCreator_UtilsICurve::contains( theList, theValue );