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,
82 int theLocalPointRowLimit )
83 : QWidget(parent), myNewSectionEditor(NULL), myCurve(theCurve), mySection(0),
84 myDragStarted( false ), myDragInteractionStyle( SUIT_ViewModel::STANDARD ),
85 myOCCViewer( 0 ), myLocalPointRowLimit( theLocalPointRowLimit )
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 CurveCreator_TableView( myCurve, this );
101 connect( myLocalPointView, SIGNAL( cellChanged( int, int ) ),
102 this, SLOT( onCellChanged( int, int ) ) );
104 QToolBar* aTB = new QToolBar(tr("TOOL_BAR_TLT"), aSectionGroup);
105 // QToolButton* anUndoBtn = new QToolButton(aTB);
107 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
108 QPixmap anUndoPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_UNDO")));
109 QPixmap aRedoPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_REDO")));
110 QPixmap aNewSectionPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_NEW_SECTION")));
111 QPixmap aNewPointPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_NEW_POINT")));
112 QPixmap anEditPointsPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_EDIT_POINTS")));
113 QPixmap aDetectPointsPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_EDIT_POINTS")));
114 QPixmap aPolylinePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_POLYLINE")));
115 QPixmap aSplinePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_SPLINE")));
116 QPixmap aRemovePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_DELETE")));
117 QPixmap aJoinPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_JOIN")));
118 QPixmap aStepUpPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_ARROW_UP")));
119 QPixmap aStepDownPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_ARROW_DOWN")));
121 QAction* anAct = createAction( UNDO_ID, tr("UNDO"), anUndoPixmap, tr("UNDO_TLT"),
122 QKeySequence(Qt::ControlModifier|Qt::Key_Z) );
123 connect(anAct, SIGNAL(triggered()), this, SLOT(onUndo()) );
124 aTB->addAction(anAct);
126 anAct = createAction( REDO_ID, tr("REDO"), aRedoPixmap, tr("REDO_TLT"),
127 QKeySequence(Qt::ControlModifier|Qt::Key_Y) );
128 connect(anAct, SIGNAL(triggered()), this, SLOT(onRedo()) );
129 aTB->addAction(anAct);
133 anAct = createAction( NEW_SECTION_ID, tr("NEW_SECTION"), aNewSectionPixmap, tr("NEW_SECTION_TLT"),
134 QKeySequence(Qt::ControlModifier|Qt::Key_N) );
135 connect(anAct, SIGNAL(triggered()), this, SLOT(onNewSection()) );
136 aTB->addAction(anAct);
139 anAct = createAction( ADDITION_MODE_ID, tr("ADDITION_MODE"), aNewPointPixmap, tr("ADDITION_MODE_TLT"),
141 anAct->setCheckable(true);
142 connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onAdditionMode(bool)) );
143 connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
144 aTB->addAction(anAct);
146 anAct = createAction( MODIFICATION_MODE_ID, tr("MODIFICATION_MODE"), anEditPointsPixmap, tr("MODIFICATION_MODE_TLT"),
148 anAct->setCheckable(true);
149 connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onModificationMode(bool)) );
150 connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
151 aTB->addAction(anAct);
153 anAct = createAction( DETECTION_MODE_ID, tr("DETECTION_MODE"), aDetectPointsPixmap, tr("DETECTION_MODE_TLT"),
155 anAct->setCheckable(true);
156 connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onDetectionMode(bool)) );
157 connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
158 aTB->addAction(anAct);
160 anAct = createAction( CLOSE_SECTIONS_ID, tr("CLOSE_SECTIONS"), QPixmap(), tr("CLOSE_SECTIONS_TLT"),
161 QKeySequence(Qt::ControlModifier|Qt::Key_W) );
162 connect(anAct, SIGNAL(triggered()), this, SLOT(onCloseSections()) );
164 anAct = createAction( UNCLOSE_SECTIONS_ID, tr("UNCLOSE_SECTIONS"), QPixmap(),
165 tr("UNCLOSE_SECTIONS_TLT"), QKeySequence(Qt::ControlModifier|Qt::Key_S) );
166 connect(anAct, SIGNAL(triggered()), this, SLOT(onUncloseSections()) );
168 anAct = createAction( SET_SECTIONS_POLYLINE_ID, tr("SET_SECTIONS_POLYLINE"),
169 aPolylinePixmap, tr("SET_POLYLINE_TLT"),
170 QKeySequence(Qt::ControlModifier|Qt::Key_E) );
171 connect(anAct, SIGNAL(triggered()), this, SLOT(onSetPolyline()) );
173 anAct = createAction( SET_SECTIONS_SPLINE_ID, tr("SET_SECTIONS_SPLINE"), aSplinePixmap,
174 tr("SET_SPLINE_TLT"), QKeySequence(Qt::ControlModifier|Qt::Key_R) );
175 connect(anAct, SIGNAL(triggered()), this, SLOT(onSetSpline()) );
177 anAct = createAction( REMOVE_ID, tr("REMOVE"), aRemovePixmap, tr("REMOVE_TLT"),
178 QKeySequence(Qt::ControlModifier|Qt::Key_Delete ) );
179 connect(anAct, SIGNAL(triggered()), this, SLOT(onRemove()) );
180 aTB->addAction(anAct);
182 //aTB->addSeparator();
184 //anAct = createAction( JOIN_ID, tr("JOIN"), aJoinPixmap, tr("JOIN_TLT"),
185 // QKeySequence(Qt::ControlModifier|Qt::Key_Plus ) );
186 //connect( anAct, SIGNAL(triggered()), this, SLOT(onJoin()) );
187 //aTB->addAction(anAct);
189 anAct = createAction( CLEAR_ALL_ID, tr("CLEAR_ALL"), QPixmap(), tr("CLEAR_ALL_TLT"),
190 QKeySequence(Qt::ControlModifier | Qt::ShiftModifier | Qt::Key_Delete ) );
191 connect( anAct, SIGNAL(triggered()), this, SLOT( onClearAll()) );
194 //anAct = createAction( JOIN_ALL_ID, tr("JOIN_ALL"), QPixmap(), tr("JOIN_ALL_TLT"),
195 // QKeySequence(Qt::ControlModifier | Qt::ShiftModifier | Qt::Key_Plus ) );
196 //connect( anAct, SIGNAL(triggered()), this, SLOT(onJoinAll()) );
198 QVBoxLayout* aSectLayout = new QVBoxLayout();
199 aSectLayout->setMargin( 5 );
200 aSectLayout->setSpacing( 5 );
201 aSectLayout->addWidget(aTB);
202 aSectLayout->addWidget(mySectionView);
203 aSectLayout->addWidget( myLocalPointView );
204 aSectionGroup->setLayout(aSectLayout);
205 QVBoxLayout* aLay = new QVBoxLayout();
206 aLay->setMargin( 0 );
207 aLay->setSpacing( 5 );
208 // aLay->addLayout(aNameLayout);
209 aLay->addWidget(aSectionGroup);
212 updateActionsStates();
219 void CurveCreator_Widget::setOCCViewer( OCCViewer_Viewer* theViewer )
221 if ( myOCCViewer == theViewer )
225 OCCViewer_ViewManager* aViewManager = dynamic_cast<OCCViewer_ViewManager*>
226 ( myOCCViewer->getViewManager() );
227 disconnect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
228 this, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ) );
229 disconnect( aViewManager, SIGNAL( mouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ),
230 this, SLOT( onMouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ) );
231 disconnect( aViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
232 this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
233 disconnect( aViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
234 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
235 // restore normal mode in the viewer
236 OCCViewer_Utilities::setViewer2DMode( myOCCViewer, OCCViewer_ViewWindow::No2dMode );
237 // all local contexts should be closed if the viewer is not more used
238 setLocalPointContext( false, true );
241 myOCCViewer = theViewer;
243 OCCViewer_ViewManager* aViewManager = dynamic_cast<OCCViewer_ViewManager*>
244 ( myOCCViewer->getViewManager() );
245 connect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
246 this, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ) );
247 connect( aViewManager, SIGNAL( mouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ),
248 this, SLOT( onMouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ) );
249 connect( aViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
250 this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
251 connect( aViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
252 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
253 OCCViewer_Utilities::setViewer2DMode( myOCCViewer, OCCViewer_ViewWindow::XYPlane );
258 * Returns current OCC viewer
260 OCCViewer_Viewer* CurveCreator_Widget::getOCCViewer()
266 * Returns OCC viewer context
268 Handle(AIS_InteractiveContext) CurveCreator_Widget::getAISContext()
270 Handle(AIS_InteractiveContext) aContext;
271 OCCViewer_Viewer* aViewer = getOCCViewer();
273 aContext = aViewer->getAISContext();
279 * Returns OCC viewer view port
281 OCCViewer_ViewPort3d* CurveCreator_Widget::getViewPort()
283 OCCViewer_ViewPort3d* aViewPort = 0;
284 OCCViewer_Viewer* aViewer = getOCCViewer();
286 aViewPort = ((OCCViewer_ViewWindow*)aViewer->getViewManager()->getActiveView())->getViewPort();
292 * Set interaction style in the OCC viewer
293 * \param theStyle a new style
294 * \return the previous style
296 int CurveCreator_Widget::changeInteractionStyle( int theStyle )
298 OCCViewer_Viewer* aViewer = getOCCViewer();
302 int aPrevStyle = aViewer->interactionStyle();
303 aViewer->setInteractionStyle( theStyle );
308 //=======================================================================
310 // purpose: reset the widget viewer, close local context, clear selection
311 //=======================================================================
312 void CurveCreator_Widget::reset()
316 void CurveCreator_Widget::setCurve( CurveCreator_ICurve* theCurve )
319 mySectionView->setCurve( myCurve );
320 myLocalPointView->setCurve( myCurve );
321 updateActionsStates();
325 void CurveCreator_Widget::onSelectionChanged()
327 updateActionsStates();
329 emit selectionChanged();
332 void CurveCreator_Widget::updateActionsStates()
334 QList<ActionId> anEnabledAct;
336 anEnabledAct << NEW_SECTION_ID << MODIFICATION_MODE_ID;
337 if ( removeEnabled() )
338 anEnabledAct << REMOVE_ID;
339 QList<int> aSelSections = mySectionView->getSelectedSections();
340 QList< QPair< int, int > > aSelPoints = mySectionView->getSelectedPoints();
341 CurveCreator_TreeView::SelectionType aSelType = mySectionView->getSelectionType();
343 case CurveCreator_TreeView::ST_NOSEL:{
346 case CurveCreator_TreeView::ST_SECTIONS:{
347 /*if( aSelSections[0] > 0 ){
348 anEnabledAct << UP_ID;
350 if( aSelSections.size() == 1 ){
351 anEnabledAct << ADDITION_MODE_ID << DETECTION_MODE_ID;
353 switch ( getActionMode() ) {
357 QList<int> aSelSection = mySectionView->getSelectedSections();
358 if( aSelSection.size() > 0 ){
359 mySection = aSelSection[0];
360 myPointNum = myCurve->getNbPoints(mySection);
364 case ModificationMode: {
365 anEnabledAct << CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID << SET_SECTIONS_POLYLINE_ID << SET_SECTIONS_SPLINE_ID;
366 int aSectCnt = myCurve->getNbSections();
368 anEnabledAct << CLEAR_ALL_ID;
371 // anEnabledAct << JOIN_ALL_ID;
372 //if( aSelSections.size() > 1 ){
373 // anEnabledAct << JOIN_ID;
377 case DetectionMode: {
384 /*if( aSelSections[ aSelSections.size() - 1 ] < ( myCurve->getNbSections() - 1 ) ){
385 anEnabledAct << DOWN_ID;
389 /*case CurveCreator_TreeView::ST_POINTS_ONE_SECTION:{
390 if( aSelPoints[0].second > 0 ){
391 anEnabledAct << UP_ID;
393 int aLastIndex = aSelPoints.size()-1;
394 int aSect = aSelPoints[0].first;
395 if( aSelPoints[aLastIndex].second < (myCurve->getNbPoints(aSect) - 1)){
396 anEnabledAct << DOWN_ID;
398 if( aSelPoints.size() == 1){
399 anEnabledAct << INSERT_POINT_BEFORE_ID << INSERT_POINT_AFTER_ID;
406 /*int aSelObjsCnt = aSelPoints.size() + aSelSections.size();
407 if( aSelObjsCnt > 0 ){
408 anEnabledAct << REMOVE_ID;
410 if( (myCurve->getNbSections() + myCurve->getNbPoints()) > 0 ){
411 anEnabledAct << REMOVE_ALL_ID;
414 //if( myCurve->getNbSections() > 1 ){
415 // anEnabledAct << JOIN_ALL_ID;
418 QList<ActionId> anIds = myActionMap.keys();
419 for( int i = 0 ; i < anIds.size() ; i++ ){
420 if( myActionMap.contains(anIds[i]) ){
421 if( anEnabledAct.contains(anIds[i]) ){
422 myActionMap[anIds[i]]->setEnabled(true);
425 myActionMap[anIds[i]]->setEnabled(false);
431 void CurveCreator_Widget::onAdditionMode(bool checked)
436 Handle(AIS_InteractiveContext) aContext = getAISContext();
437 if( !myCurve || aContext.IsNull() )
442 QList<int> aSelSection = mySectionView->getSelectedSections();
443 if( aSelSection.size() > 0 ){
444 mySection = aSelSection[0];
447 QList< QPair<int,int> > aSelPoints = mySectionView->getSelectedPoints();
448 if( aSelPoints.size() > 0 ){
449 mySection = aSelPoints[0].first;
450 myPointNum = aSelPoints[0].second + 1;
453 // emit subOperationStarted( myNewPointEditor );
456 void CurveCreator_Widget::onModificationMode(bool checked)
458 myLocalPointView->setVisible( checked );
461 void CurveCreator_Widget::onDetectionMode(bool checked)
465 void CurveCreator_Widget::onModeChanged(bool checked)
467 ActionMode aMode = NoneMode;
469 QAction* anAction = (QAction*)sender();
470 switch(myActionMap.key(anAction)) {
471 case ADDITION_MODE_ID:
472 aMode = AdditionMode;
473 if (myActionMap[MODIFICATION_MODE_ID]->isChecked())
474 myActionMap[MODIFICATION_MODE_ID]->trigger();
475 else if (myActionMap[DETECTION_MODE_ID]->isChecked())
476 myActionMap[DETECTION_MODE_ID]->trigger();
478 case MODIFICATION_MODE_ID:
479 aMode = ModificationMode;
480 if (myActionMap[ADDITION_MODE_ID]->isChecked())
481 myActionMap[ADDITION_MODE_ID]->trigger();
482 else if (myActionMap[DETECTION_MODE_ID]->isChecked())
483 myActionMap[DETECTION_MODE_ID]->trigger();
485 case DETECTION_MODE_ID:
486 aMode = DetectionMode;
487 if (myActionMap[ADDITION_MODE_ID]->isChecked())
488 myActionMap[ADDITION_MODE_ID]->trigger();
489 else if (myActionMap[MODIFICATION_MODE_ID]->isChecked())
490 myActionMap[MODIFICATION_MODE_ID]->trigger();
494 updateActionsStates();
496 setLocalPointContext( aMode == ModificationMode, true );
499 void CurveCreator_Widget::onNewSection()
503 myNewSectionEditor->clear();
504 myNewSectionEditor->setEditMode(false);
505 QString aSectName = QString( CurveCreator_UtilsICurve::getUniqSectionName( myCurve ).c_str() );
506 myNewSectionEditor->setSectionParameters(aSectName, true, CurveCreator::Polyline );
507 emit subOperationStarted( myNewSectionEditor );
510 void CurveCreator_Widget::onAddNewSection()
514 myCurve->addSection( myNewSectionEditor->getName().toStdString(),
515 myNewSectionEditor->getSectionType(),
516 myNewSectionEditor->isClosed() );
517 mySectionView->sectionAdded( -1 ); // add a new section to the end of list
518 QString aNewName = QString( CurveCreator_UtilsICurve::getUniqSectionName( myCurve ).c_str() );
519 myNewSectionEditor->setSectionName(aNewName);
520 updateActionsStates();
525 void CurveCreator_Widget::onCancelSection()
527 emit subOperationFinished( myNewSectionEditor );
530 QAction* CurveCreator_Widget::createAction( ActionId theId, const QString& theName, const QPixmap& theImage,
531 const QString& theToolTip, const QKeySequence& theShortcut )
533 QAction* anAct = new QAction(theName,this);
534 if( !theImage.isNull() ){
535 anAct->setIcon(theImage);
537 anAct->setShortcut(theShortcut);
538 anAct->setToolTip(theToolTip);
539 myActionMap[theId] = anAct;
543 QAction* CurveCreator_Widget::getAction(ActionId theId)
545 if( myActionMap.contains(theId) )
546 return myActionMap[theId];
550 CurveCreator_Widget::ActionMode CurveCreator_Widget::getActionMode() const
552 ActionMode aMode = NoneMode;
554 if ( myActionMap[ADDITION_MODE_ID]->isChecked() )
555 aMode = AdditionMode;
556 else if ( myActionMap[MODIFICATION_MODE_ID]->isChecked() )
557 aMode = ModificationMode;
558 else if ( myActionMap[DETECTION_MODE_ID]->isChecked() )
559 aMode = DetectionMode;
564 void CurveCreator_Widget::onEditSection( int theSection )
568 mySection = theSection;
569 QString aSectName = QString::fromStdString( myCurve->getSectionName(theSection));
570 bool isClosed = myCurve->isClosed(theSection);
571 CurveCreator::SectionType aType = myCurve->getSectionType(theSection);
572 myNewSectionEditor->setEditMode(true);
573 myNewSectionEditor->setSectionParameters( aSectName, isClosed, aType );
575 emit subOperationStarted( myNewSectionEditor );
578 void CurveCreator_Widget::onModifySection()
582 QString aName = myNewSectionEditor->getName();
583 bool isClosed = myNewSectionEditor->isClosed();
584 CurveCreator::SectionType aSectType = myNewSectionEditor->getSectionType();
585 // myCurve->startOperation();
586 if( myCurve->getSectionName(mySection) != aName.toStdString() )
587 myCurve->setSectionName( mySection , aName.toStdString() );
589 if( myCurve->getSectionType(mySection) != aSectType )
590 myCurve->setSectionType( mySection, aSectType );
592 if( myCurve->isClosed(mySection) != isClosed )
593 myCurve->setClosed( mySection, isClosed );
594 // myCurve->finishOperation();
595 mySectionView->sectionChanged(mySection);
600 void CurveCreator_Widget::onJoin()
604 QList<int> aSections = mySectionView->getSelectedSections();
605 if( aSections.size() == 0 ){
608 int aMainSect = aSections[0];
609 int aMainSectSize = myCurve->getNbPoints(aMainSect);
610 // myCurve->startOperation();
611 for( int i = 1 ; i < aSections.size() ; i++ ){
612 int aSectNum = aSections[i] - (i-1);
613 myCurve->join( aMainSect, aSectNum );
614 mySectionView->sectionsRemoved( aSectNum );
616 // myCurve->finishOperation();
617 int aNewSectSize = myCurve->getNbPoints(aMainSect);
618 if( aNewSectSize != aMainSectSize )
619 mySectionView->pointsAdded( aMainSect, aMainSectSize, aNewSectSize-aMainSectSize );
623 void CurveCreator_Widget::onRemove()
628 switch( getActionMode() ) {
632 case ModificationMode:
640 void CurveCreator_Widget::onClearAll()
645 mySectionView->reset();
646 updateActionsStates();
650 void CurveCreator_Widget::onJoinAll()
655 mySectionView->reset();
656 updateActionsStates();
660 void CurveCreator_Widget::onUndoSettings()
665 void CurveCreator_Widget::onSetSpline()
669 QList<int> aSelSections = mySectionView->getSelectedSections();
670 // myCurve->startOperation();
671 for( int i = 0 ; i < aSelSections.size() ; i++ ){
672 myCurve->setSectionType(aSelSections[i], CurveCreator::Spline );
673 mySectionView->sectionChanged(aSelSections[i]);
675 // myCurve->finishOperation();
679 void CurveCreator_Widget::onSetPolyline()
683 // myCurve->startOperation();
684 QList<int> aSelSections = mySectionView->getSelectedSections();
685 for( int i = 0 ; i < aSelSections.size() ; i++ ){
686 myCurve->setSectionType( aSelSections[i], CurveCreator::Polyline );
687 mySectionView->sectionChanged( aSelSections[i] );
689 // myCurve->finishOperation();
693 void CurveCreator_Widget::onCloseSections()
697 // myCurve->startOperation();
698 QList<int> aSelSections = mySectionView->getSelectedSections();
699 for( int i = 0 ; i < aSelSections.size() ; i++ ){
700 myCurve->setClosed(aSelSections[i], true);
701 mySectionView->sectionChanged(aSelSections[i]);
703 // myCurve->finishOperation();
707 void CurveCreator_Widget::onUncloseSections()
711 // myCurve->startOperation();
712 QList<int> aSelSections = mySectionView->getSelectedSections();
713 for( int i = 0 ; i < aSelSections.size() ; i++ ){
714 myCurve->setClosed(aSelSections[i], false);
715 mySectionView->sectionChanged(aSelSections[i]);
717 // myCurve->finishOperation();
721 void CurveCreator_Widget::onUndo()
726 CurveCreator_ICurve::SectionToPointList aPoints;
727 startCurveModification( aPoints, false );
729 finishCurveModification();
730 mySectionView->reset();
733 void CurveCreator_Widget::onRedo()
737 CurveCreator_ICurve::SectionToPointList aPoints;
738 startCurveModification( aPoints, false );
740 finishCurveModification();
741 mySectionView->reset();
744 void CurveCreator_Widget::updateUndoRedo()
748 QAction* anAct = myActionMap[UNDO_ID];
750 if( myCurve->getNbUndo() != 0 ){
751 anAct->setEnabled(true);
754 anAct->setDisabled(true);
757 anAct = myActionMap[REDO_ID];
759 if( myCurve->getNbRedo() != 0 ){
760 anAct->setEnabled(true);
763 anAct->setDisabled(true);
768 void CurveCreator_Widget::onContextMenu( QPoint thePoint )
770 QList<ActionId> aContextActions;
771 aContextActions << CLEAR_ALL_ID << JOIN_ALL_ID << SEPARATOR_ID <<
772 CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID << SET_SECTIONS_POLYLINE_ID <<
773 SET_SECTIONS_SPLINE_ID;
774 QPoint aGlPoint = mySectionView->mapToGlobal(thePoint);
776 QList<ActionId> aResAct;
777 for( int i = 0 ; i < aContextActions.size() ; i++ ){
778 if( aContextActions[i] != SEPARATOR_ID ){
779 if( myActionMap.contains(aContextActions[i]) ){
780 QAction* anAct = myActionMap[aContextActions[i]];
781 if( anAct->isEnabled() ){
782 aResAct << aContextActions[i];
788 aResAct << SEPARATOR_ID;
794 QMenu* aMenu = new QMenu(this);
795 for( int i = 0 ; i < aResAct.size() ; i++ ){
796 if( aResAct[i] == SEPARATOR_ID ){
797 aMenu->addSeparator();
800 QAction* anAct = myActionMap[aResAct[i]];
801 aMenu->insertAction(NULL, anAct);
804 aMenu->exec(aGlPoint);
807 QList<int> CurveCreator_Widget::getSelectedSections()
809 return mySectionView->getSelectedSections();
812 QList< QPair< int, int > > CurveCreator_Widget::getSelectedPoints()
814 return mySectionView->getSelectedPoints();
818 * According to the widget state, performs the remove action
820 void CurveCreator_Widget::removeSelected()
826 * Checks whether there are some selection to be removed
828 bool CurveCreator_Widget::removeEnabled()
830 bool isEnabled = getActionMode() == ModificationMode;
832 QList<int> aSelSections = mySectionView->getSelectedSections();
833 CurveCreator_TreeView::SelectionType aSelType = mySectionView->getSelectionType();
834 isEnabled = aSelType == CurveCreator_TreeView::ST_SECTIONS &&
835 aSelSections.size() == 1;
841 //=================================================================================
842 // function : GeometryGUI::addCoordsByClick()
843 // purpose : Manage mouse press events in Additon mode
844 //=================================================================================
845 void CurveCreator_Widget::addCoordsByClick( QMouseEvent* pe )
847 if (pe->button() != Qt::LeftButton)
850 if ( pe->modifiers() != Qt::ControlModifier ) {
851 Handle(AIS_InteractiveContext) ic = getAISContext();
858 if ( pe->modifiers() == Qt::ShiftModifier )
859 ic->ShiftSelect(); // Append selection
861 ic->Select(); // New selection
864 OCCViewer_ViewPort3d* vp = getViewPort();
865 aPnt = CurveCreator_Utils::ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
867 // set the coordinates into dialog
868 CurveCreator::Coordinates aCoords;
869 aCoords.push_back( aPnt.X() );
870 aCoords.push_back( aPnt.Y() );
871 if ( myCurve->getDimension() == 3 ) {
872 aCoords.push_back( aPnt.Z() );
874 addNewPoint(aCoords);
879 * Manage mouse press events
880 * \param theWindow an owner of the signal
881 * \param theEvent a mouse event
883 void CurveCreator_Widget::onMousePress( SUIT_ViewWindow*, QMouseEvent* theEvent )
885 if ( theEvent->button() != Qt::LeftButton )
888 switch( getActionMode() ) {
889 case ModificationMode: {
890 //store initial cursor position for Drag&Drop
891 setDragStarted( true, theEvent->pos() );
895 addCoordsByClick( theEvent );
904 * Manage mouse release events in Modification mode
905 * \param theWindow an owner of the signal
906 * \param theEvent a mouse event
908 void CurveCreator_Widget::onMouseRelease( SUIT_ViewWindow*, QMouseEvent* theEvent )
910 if ( getActionMode() != ModificationMode )
913 if ( myDragStarted ) {
914 bool isDragged = myDragged;
915 CurveCreator_ICurve::SectionToPointList aDraggedPoints;
917 aDraggedPoints = myDragPoints;
919 setDragStarted( false );
920 // if the drag of some points has happened, restore the drag selection
921 if ( aDraggedPoints.size() > 0 )
924 setSelectedPoints( aDraggedPoints );
925 END_MEASURE_TIME( "drop" );
928 else // check whether the segment is clicked an a new point should be added to the segment
929 insertPointToSelectedSegment( theEvent->pos().x(), theEvent->pos().y() );
931 // updates the input panel table to show the selected point coordinates
932 updateLocalPointView();
936 * Manage mouse move events in Modification mode
937 * \param theWindow an owner of the signal
938 * \param theEvent a mouse event
940 void CurveCreator_Widget::onMouseMove( SUIT_ViewWindow*, QMouseEvent* theEvent )
942 if ( getActionMode() != ModificationMode || !myDragStarted )
945 QPoint aPos = theEvent->pos();
946 if ( (aPos - myDragStartPosition).manhattanLength() < QApplication::startDragDistance() )
951 moveSelectedPoints( aPos.x(), aPos.y() );
952 myDragStartPosition = aPos;
954 END_MEASURE_TIME( "drag" );
958 * Set zero viewer by the last view closed in
959 * \param theManager a viewer manager
961 void CurveCreator_Widget::onLastViewClosed( SUIT_ViewManager* theManager )
966 void CurveCreator_Widget::onMousePress( QMouseEvent* theEvent )
968 onMousePress( 0, theEvent );
971 void CurveCreator_Widget::onMouseRelease( QMouseEvent* theEvent )
973 onMouseRelease( 0, theEvent );
976 void CurveCreator_Widget::onMouseMove( QMouseEvent* theEvent )
978 onMouseMove( 0, theEvent );
981 void CurveCreator_Widget::onCellChanged( int theRow, int theColumn )
983 int aCurrSect = myLocalPointView->getSectionId( theRow );
984 int aPntIndex = myLocalPointView->getPointId( theRow );
989 CurveCreator_ICurve::SectionToPointList aSelPoints;
990 startCurveModification( aSelPoints );
992 double aX = myLocalPointView->item( theRow, 2 )->data( Qt::UserRole ).toDouble();
993 double anY = myLocalPointView->item( theRow, 3 )->data( Qt::UserRole ).toDouble();
994 std::deque<float> aChangedPos;
995 aChangedPos.push_back( aX );
996 aChangedPos.push_back( anY );
997 myCurve->setPoint( aCurrSect, aPntIndex, aChangedPos );
999 finishCurveModification( aSelPoints );
1003 * Removes a selected section from the curve. Updates undo/redo status
1005 void CurveCreator_Widget::removeSection()
1007 QList< QPair<int,int> > aSelPoints = mySectionView->getSelectedPoints();
1010 // myCurve->startOperation();
1011 for( int i = 0 ; i < aSelPoints.size() ; i++ ){
1012 if( aCurrSect != aSelPoints[i].first ){
1014 aCurrSect = aSelPoints[i].first;
1016 int aPntIndx = aSelPoints[i].second - aRemoveCnt;
1017 myCurve->removePoint( aCurrSect, aPntIndx );
1018 mySectionView->pointsRemoved( aCurrSect, aPntIndx );
1021 QList<int> aSections = mySectionView->getSelectedSections();
1022 for( int i = 0 ; i < aSections.size() ; i++ ){
1023 int aSectNum = aSections[i] - (i);
1024 myCurve->removeSection( aSectNum );
1025 mySectionView->sectionsRemoved( aSectNum );
1027 // myCurve->finishOperation();
1028 mySectionView->clearSelection();
1033 * Removes a selected points from the curve. Updates undo/redo status
1035 void CurveCreator_Widget::removePoint()
1037 CurveCreator_ICurve::SectionToPointList aPoints;
1038 getSelectedPoints( aPoints );
1039 if ( aPoints.size() == 0 )
1042 CurveCreator_ICurve::SectionToPointList aSelPoints;
1043 startCurveModification( aSelPoints, false );
1045 myCurve->removeSeveralPoints( aPoints );
1046 finishCurveModification( CurveCreator_ICurve::SectionToPointList() );
1049 void CurveCreator_Widget::addNewPoint(const CurveCreator::Coordinates& theCoords)
1053 QList<int> aSections = mySectionView->getSelectedSections();
1054 if( aSections.size() == 0 ){
1057 int aSection = aSections[0];
1058 myCurve->addPoints(theCoords, aSection); // add to the end of section
1059 mySectionView->pointsAdded( aSection, myCurve->getNbPoints( aSection ) );
1060 updateActionsStates();
1064 void CurveCreator_Widget::insertPointToSelectedSegment( const int theX,
1067 Handle(AIS_InteractiveContext) aContext = getAISContext();
1069 OCCViewer_ViewPort3d* aViewPort = getViewPort();
1070 Handle(V3d_View) aView;
1072 aView = aViewPort->getView();
1074 if ( aContext.IsNull() || aView.IsNull() )
1077 gp_Pnt aPoint1, aPoint2;
1078 Handle(AIS_InteractiveObject) anAISObject = myCurve->getAISObject();
1079 bool isFoundPoint = CurveCreator_Utils::pointOnObject( aView, anAISObject, theX, theY,
1080 aPoint, aPoint1, aPoint2 );
1081 if ( !isFoundPoint )
1084 // insert the point to the model curve
1085 CurveCreator_ICurve::SectionToPointList aSelPoints;
1086 startCurveModification( aSelPoints );
1088 CurveCreator::Coordinates aCoords;
1089 aCoords.push_back( aPoint.X() );
1090 aCoords.push_back( aPoint.Y() );
1092 CurveCreator_ICurve::SectionToPointList aPoints1, aPoints2;
1093 findSectionsToPoints( aPoint1.X(), aPoint1.Y(), aPoints1 );
1094 findSectionsToPoints( aPoint2.X(), aPoint2.Y(), aPoints2 );
1095 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aPoints1.begin(),
1096 aLast = aPoints1.end();
1097 int aSectionId = -1;
1098 // there can be a case when a new point is added into two sections
1099 int aPoint1Id = -1, aPoint2Id = -1;
1100 for ( ; anIt != aLast && aSectionId < 0; anIt++ ) {
1101 int aSectionCur = anIt->first;
1102 CurveCreator_ICurve::SectionToPointList::const_iterator anIt2 = aPoints2.begin(),
1103 aLast2 = aPoints2.end();
1104 for ( ; anIt2 != aLast2 && aSectionId < 0; anIt2++ ) {
1105 if ( anIt2->first == aSectionCur ) {
1106 aSectionId = aSectionCur;
1107 aPoint1Id = anIt->second;
1108 aPoint2Id = anIt2->second;
1113 int anInsertPos = -1;
1114 int aLastPoint = myCurve->getNbPoints( aSectionId )-1;
1115 if ( ( aPoint1Id == aLastPoint && aPoint2Id == 0 ) ||
1116 ( aPoint2Id == aLastPoint && aPoint1Id == 0 ) )
1117 anInsertPos = -1; // if the section happens between first and last points
1119 anInsertPos = aPoint1Id < aPoint2Id ? aPoint1Id + 1 : aPoint2Id + 1;
1121 myCurve->addPoints( aCoords, aSectionId, anInsertPos );
1122 mySectionView->pointsAdded( aSectionId, myCurve->getNbPoints( aSectionId ) );
1124 finishCurveModification( aSelPoints );
1126 setSelectedPoints();
1129 void CurveCreator_Widget::moveSelectedPoints( const int theXPosition,
1130 const int theYPosition )
1132 OCCViewer_ViewPort3d* aViewPort = getViewPort();
1136 CurveCreator_ICurve::SectionToPointList aPoints;
1137 startCurveModification( aPoints, false );
1139 gp_Pnt aStartPnt = CurveCreator_Utils::ConvertClickToPoint( myDragStartPosition.x(),
1140 myDragStartPosition.y(),
1141 aViewPort->getView() );
1142 gp_Pnt anEndPnt = CurveCreator_Utils::ConvertClickToPoint( theXPosition, theYPosition,
1143 aViewPort->getView() );
1144 double aXDelta = aStartPnt.X() - anEndPnt.X();
1145 double anYDelta = aStartPnt.Y() - anEndPnt.Y();
1147 CurveCreator_ICurve::SectionToPointCoordsList aCoordList;
1148 std::deque<float> aChangedPos;
1149 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = myDragPoints.begin(),
1150 aLast = myDragPoints.end();
1151 for ( ; anIt != aLast; anIt++ ) {
1152 int aSectionId = anIt->first;
1153 int aPointId = anIt->second;
1154 aChangedPos = myCurve->getPoint( aSectionId, aPointId );
1155 if ( aChangedPos.size() < 2 )
1157 aChangedPos[0] = aChangedPos[0] - aXDelta;
1158 aChangedPos[1] = aChangedPos[1] - anYDelta;
1160 aCoordList.push_back(
1161 std::make_pair(std::make_pair( aSectionId, aPointId ),
1164 myCurve->setSeveralPoints( aCoordList );
1167 finishCurveModification( myDragPoints );
1170 void CurveCreator_Widget::updateLocalPointView()
1172 if ( myDragStarted )
1174 Handle(AIS_InteractiveContext) aContext = getAISContext();
1175 if ( aContext.IsNull() )
1178 CurveCreator_Utils::getSelectedPoints( aContext, myCurve, myLocalPoints );
1179 int aNbPoints = myLocalPoints.size();
1180 bool isRowLimit = aNbPoints > myLocalPointRowLimit;
1181 myLocalPointView->setVisible( !isRowLimit );
1183 if ( !isRowLimit ) {
1184 bool isBlocked = myLocalPointView->blockSignals(true);
1185 myLocalPointView->setRowCount( 0 );
1186 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = myLocalPoints.begin(),
1187 aLast = myLocalPoints.end();
1188 for ( ; anIt != aLast; anIt++ )
1189 myLocalPointView->addLocalPointToTable( *anIt );
1191 myLocalPointView->blockSignals( isBlocked );
1198 void CurveCreator_Widget::setLocalPointContext( const bool theOpen, const bool isUpdateTable )
1200 CurveCreator_Utils::setLocalPointContext( myCurve, getAISContext(), theOpen );
1201 if ( !theOpen && isUpdateTable )
1202 updateLocalPointView();
1206 * Set drag operation started. Save the position and a list of dragged points
1207 * \param theState the drag operation state: started/finished
1208 * \param thePoint the start drag position
1210 void CurveCreator_Widget::setDragStarted( const bool theState, const QPoint& thePoint )
1213 getSelectedPoints( myDragPoints );
1214 myDragStarted = myDragPoints.size();
1215 myDragStartPosition = thePoint;
1216 if ( myDragStarted ) {
1217 // change a viewer interaction style in order to avoid a select rectangle build
1218 myDragInteractionStyle = changeInteractionStyle( SUIT_ViewModel::KEY_FREE );
1222 if ( myDragStarted )
1223 changeInteractionStyle( myDragInteractionStyle );
1224 myDragStarted = false;
1225 myDragPoints.clear();
1230 void CurveCreator_Widget::getSelectedPoints( CurveCreator_ICurve::SectionToPointList& thePoints )
1233 thePoints = myLocalPoints;
1236 void CurveCreator_Widget::setSelectedPoints( const CurveCreator_ICurve::SectionToPointList& thePoints )
1238 if ( myDragStarted )
1240 Handle(AIS_InteractiveContext) aContext = getAISContext();
1241 if ( aContext.IsNull() || !aContext->HasOpenedContext() )
1244 CurveCreator_Utils::setSelectedPoints( aContext, myCurve, thePoints );
1246 updateLocalPointView();
1250 * Get viewer information before perform the curve modification.
1251 * Take a list of selected cuve points an close local context.
1252 * The context should be closed because the curve presentation is
1253 * redisplayed and if it is not closed, when we close the local context
1254 * later, the presentation shown in the local context is disappeared.
1255 * \param thePoints an output list of curve selected points
1256 * \param theFillPoints a flag whether the selection list should be filled
1258 void CurveCreator_Widget::startCurveModification(
1259 CurveCreator_ICurve::SectionToPointList& thePoints,
1260 const bool theFillPoints )
1262 if ( theFillPoints ) {
1264 getSelectedPoints( thePoints );
1266 setLocalPointContext( false );
1270 * Restore the viewer state after the curve modification is done.
1271 * Open local context and select given points inside it.
1272 * \param thePoints a list of curve selected points
1274 void CurveCreator_Widget::finishCurveModification(
1275 const CurveCreator_ICurve::SectionToPointList& thePoints )
1277 if ( getActionMode() == ModificationMode )
1278 setLocalPointContext( true );
1279 setSelectedPoints( thePoints );
1284 * Returns a point index in the model curve by the point coordinates in the viewer
1285 * \param theX the X coordinate of the point
1286 * \param theY the Y coordinate of the point
1288 int CurveCreator_Widget::findLocalPointIndex( int theSectionId, float theX, float theY )
1290 return CurveCreator_UtilsICurve::findLocalPointIndex( myCurve, theSectionId, theX, theY );
1293 void CurveCreator_Widget::findSectionsToPoints( const double theX, const double theY,
1294 CurveCreator_ICurve::SectionToPointList& thePoints )
1296 return CurveCreator_UtilsICurve::findSectionsToPoints( myCurve, theX, theY, thePoints );
1299 void CurveCreator_Widget::convert( const CurveCreator_ICurve::SectionToPointList& thePoints,
1300 QMap<int, QList<int> >& theConvPoints )
1302 return CurveCreator_UtilsICurve::convert( thePoints, theConvPoints );
1306 * Returns whethe the container has the value
1307 * \param theList a container of values
1308 * \param theValue a value
1310 bool CurveCreator_Widget::contains( const CurveCreator_ICurve::SectionToPointList& theList,
1311 const CurveCreator_ICurve::SectionToPoint& theValue ) const
1313 return CurveCreator_UtilsICurve::contains( theList, theValue );