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()
509 myNewSectionEditor->clear();
510 myNewSectionEditor->setEditMode(false);
511 QString aSectName = QString( CurveCreator_UtilsICurve::getUniqSectionName( myCurve ).c_str() );
512 myNewSectionEditor->setSectionParameters(aSectName, true, CurveCreator::Polyline );
513 emit subOperationStarted( myNewSectionEditor );
516 void CurveCreator_Widget::onAddNewSection()
520 myCurve->addSection( myNewSectionEditor->getName().toStdString(),
521 myNewSectionEditor->getSectionType(),
522 myNewSectionEditor->isClosed() );
523 mySectionView->sectionAdded( -1 ); // add a new section to the end of list
524 QString aNewName = QString( CurveCreator_UtilsICurve::getUniqSectionName( myCurve ).c_str() );
525 myNewSectionEditor->setSectionName(aNewName);
526 updateActionsStates();
531 void CurveCreator_Widget::onCancelSection()
533 emit subOperationFinished( myNewSectionEditor );
536 QAction* CurveCreator_Widget::createAction( ActionId theId, const QString& theName, const QPixmap& theImage,
537 const QString& theToolTip, const QKeySequence& theShortcut )
539 QAction* anAct = new QAction(theName,this);
540 if( !theImage.isNull() ){
541 anAct->setIcon(theImage);
543 anAct->setShortcut(theShortcut);
544 anAct->setToolTip(theToolTip);
545 myActionMap[theId] = anAct;
549 QAction* CurveCreator_Widget::getAction(ActionId theId)
551 if( myActionMap.contains(theId) )
552 return myActionMap[theId];
556 CurveCreator_Widget::ActionMode CurveCreator_Widget::getActionMode() const
558 ActionMode aMode = NoneMode;
560 if ( myActionMap[ADDITION_MODE_ID]->isChecked() )
561 aMode = AdditionMode;
562 else if ( myActionMap[MODIFICATION_MODE_ID]->isChecked() )
563 aMode = ModificationMode;
564 else if ( myActionMap[DETECTION_MODE_ID]->isChecked() )
565 aMode = DetectionMode;
570 void CurveCreator_Widget::onEditSection( int theSection )
574 mySection = theSection;
575 QString aSectName = QString::fromStdString( myCurve->getSectionName(theSection));
576 bool isClosed = myCurve->isClosed(theSection);
577 CurveCreator::SectionType aType = myCurve->getSectionType(theSection);
578 myNewSectionEditor->setEditMode(true);
579 myNewSectionEditor->setSectionParameters( aSectName, isClosed, aType );
581 emit subOperationStarted( myNewSectionEditor );
584 void CurveCreator_Widget::onModifySection()
588 QString aName = myNewSectionEditor->getName();
589 bool isClosed = myNewSectionEditor->isClosed();
590 CurveCreator::SectionType aSectType = myNewSectionEditor->getSectionType();
591 // myCurve->startOperation();
592 if( myCurve->getSectionName(mySection) != aName.toStdString() )
593 myCurve->setSectionName( mySection , aName.toStdString() );
595 if( myCurve->getSectionType(mySection) != aSectType )
596 myCurve->setSectionType( mySection, aSectType );
598 if( myCurve->isClosed(mySection) != isClosed )
599 myCurve->setClosed( mySection, isClosed );
600 // myCurve->finishOperation();
601 mySectionView->sectionChanged(mySection);
606 void CurveCreator_Widget::onJoin()
610 QList<int> aSections = mySectionView->getSelectedSections();
611 if( aSections.size() == 0 ){
614 int aMainSect = aSections[0];
615 int aMainSectSize = myCurve->getNbPoints(aMainSect);
616 // myCurve->startOperation();
617 for( int i = 1 ; i < aSections.size() ; i++ ){
618 int aSectNum = aSections[i] - (i-1);
619 myCurve->join( aMainSect, aSectNum );
620 mySectionView->sectionsRemoved( aSectNum );
622 // myCurve->finishOperation();
623 int aNewSectSize = myCurve->getNbPoints(aMainSect);
624 if( aNewSectSize != aMainSectSize )
625 mySectionView->pointsAdded( aMainSect, aMainSectSize, aNewSectSize-aMainSectSize );
629 void CurveCreator_Widget::onRemove()
634 switch( getActionMode() ) {
638 case ModificationMode:
646 void CurveCreator_Widget::onClearAll()
651 mySectionView->reset();
652 updateActionsStates();
656 void CurveCreator_Widget::onJoinAll()
661 mySectionView->reset();
662 updateActionsStates();
666 void CurveCreator_Widget::onUndoSettings()
671 void CurveCreator_Widget::onSetSpline()
675 QList<int> aSelSections = mySectionView->getSelectedSections();
676 // myCurve->startOperation();
677 for( int i = 0 ; i < aSelSections.size() ; i++ ){
678 myCurve->setSectionType(aSelSections[i], CurveCreator::Spline );
679 mySectionView->sectionChanged(aSelSections[i]);
681 // myCurve->finishOperation();
685 void CurveCreator_Widget::onSetPolyline()
689 // myCurve->startOperation();
690 QList<int> aSelSections = mySectionView->getSelectedSections();
691 for( int i = 0 ; i < aSelSections.size() ; i++ ){
692 myCurve->setSectionType( aSelSections[i], CurveCreator::Polyline );
693 mySectionView->sectionChanged( aSelSections[i] );
695 // myCurve->finishOperation();
699 void CurveCreator_Widget::onCloseSections()
703 // myCurve->startOperation();
704 QList<int> aSelSections = mySectionView->getSelectedSections();
705 for( int i = 0 ; i < aSelSections.size() ; i++ ){
706 myCurve->setClosed(aSelSections[i], true);
707 mySectionView->sectionChanged(aSelSections[i]);
709 // myCurve->finishOperation();
713 void CurveCreator_Widget::onUncloseSections()
717 // myCurve->startOperation();
718 QList<int> aSelSections = mySectionView->getSelectedSections();
719 for( int i = 0 ; i < aSelSections.size() ; i++ ){
720 myCurve->setClosed(aSelSections[i], false);
721 mySectionView->sectionChanged(aSelSections[i]);
723 // myCurve->finishOperation();
727 void CurveCreator_Widget::onUndo()
732 CurveCreator_ICurve::SectionToPointList aPoints;
733 startCurveModification( aPoints, false );
735 finishCurveModification();
736 mySectionView->reset();
739 void CurveCreator_Widget::onRedo()
743 CurveCreator_ICurve::SectionToPointList aPoints;
744 startCurveModification( aPoints, false );
746 finishCurveModification();
747 mySectionView->reset();
750 void CurveCreator_Widget::updateUndoRedo()
754 QAction* anAct = myActionMap[UNDO_ID];
756 if( myCurve->getNbUndo() != 0 ){
757 anAct->setEnabled(true);
760 anAct->setDisabled(true);
763 anAct = myActionMap[REDO_ID];
765 if( myCurve->getNbRedo() != 0 ){
766 anAct->setEnabled(true);
769 anAct->setDisabled(true);
774 void CurveCreator_Widget::onContextMenu( QPoint thePoint )
776 QList<ActionId> aContextActions;
777 aContextActions << CLEAR_ALL_ID << JOIN_ALL_ID << SEPARATOR_ID <<
778 CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID << SET_SECTIONS_POLYLINE_ID <<
779 SET_SECTIONS_SPLINE_ID;
780 QPoint aGlPoint = mySectionView->mapToGlobal(thePoint);
782 QList<ActionId> aResAct;
783 for( int i = 0 ; i < aContextActions.size() ; i++ ){
784 if( aContextActions[i] != SEPARATOR_ID ){
785 if( myActionMap.contains(aContextActions[i]) ){
786 QAction* anAct = myActionMap[aContextActions[i]];
787 if( anAct->isEnabled() ){
788 aResAct << aContextActions[i];
794 aResAct << SEPARATOR_ID;
800 QMenu* aMenu = new QMenu(this);
801 for( int i = 0 ; i < aResAct.size() ; i++ ){
802 if( aResAct[i] == SEPARATOR_ID ){
803 aMenu->addSeparator();
806 QAction* anAct = myActionMap[aResAct[i]];
807 aMenu->insertAction(NULL, anAct);
810 aMenu->exec(aGlPoint);
813 QList<int> CurveCreator_Widget::getSelectedSections()
815 return mySectionView->getSelectedSections();
818 QList< QPair< int, int > > CurveCreator_Widget::getSelectedPoints()
820 return mySectionView->getSelectedPoints();
824 * According to the widget state, performs the remove action
826 void CurveCreator_Widget::removeSelected()
832 * Checks whether there are some selection to be removed
834 bool CurveCreator_Widget::removeEnabled()
836 bool isEnabled = getActionMode() == ModificationMode;
838 QList<int> aSelSections = mySectionView->getSelectedSections();
839 CurveCreator_TreeView::SelectionType aSelType = mySectionView->getSelectionType();
840 isEnabled = aSelType == CurveCreator_TreeView::ST_SECTIONS &&
841 aSelSections.size() == 1;
847 //=================================================================================
848 // function : GeometryGUI::addCoordsByClick()
849 // purpose : Manage mouse press events in Additon mode
850 //=================================================================================
851 void CurveCreator_Widget::addCoordsByClick( QMouseEvent* pe )
853 if (pe->button() != Qt::LeftButton)
856 if ( pe->modifiers() != Qt::ControlModifier ) {
857 Handle(AIS_InteractiveContext) ic = getAISContext();
864 if ( pe->modifiers() == Qt::ShiftModifier )
865 ic->ShiftSelect(); // Append selection
867 ic->Select(); // New selection
870 OCCViewer_ViewPort3d* vp = getViewPort();
871 aPnt = CurveCreator_Utils::ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
873 // set the coordinates into dialog
874 CurveCreator::Coordinates aCoords;
875 aCoords.push_back( aPnt.X() );
876 aCoords.push_back( aPnt.Y() );
877 if ( myCurve->getDimension() == 3 ) {
878 aCoords.push_back( aPnt.Z() );
880 addNewPoint(aCoords);
885 * Manage mouse press events
886 * \param theWindow an owner of the signal
887 * \param theEvent a mouse event
889 void CurveCreator_Widget::onMousePress( SUIT_ViewWindow*, QMouseEvent* theEvent )
891 if ( theEvent->button() != Qt::LeftButton )
894 switch( getActionMode() ) {
895 case ModificationMode: {
896 //store initial cursor position for Drag&Drop
897 setDragStarted( true, theEvent->pos() );
901 addCoordsByClick( theEvent );
910 * Manage mouse release events in Modification mode
911 * \param theWindow an owner of the signal
912 * \param theEvent a mouse event
914 void CurveCreator_Widget::onMouseRelease( SUIT_ViewWindow*, QMouseEvent* theEvent )
916 if ( getActionMode() != ModificationMode )
919 if ( myDragStarted ) {
920 bool isDragged = myDragged;
921 CurveCreator_ICurve::SectionToPointList aDraggedPoints;
923 aDraggedPoints = myDragPoints;
925 setDragStarted( false );
926 // if the drag of some points has happened, restore the drag selection
927 if ( aDraggedPoints.size() > 0 )
930 setSelectedPoints( aDraggedPoints );
931 END_MEASURE_TIME( "drop" );
934 else // check whether the segment is clicked an a new point should be added to the segment
935 insertPointToSelectedSegment( theEvent->pos().x(), theEvent->pos().y() );
937 // updates the input panel table to show the selected point coordinates
938 updateLocalPointView();
942 * Manage mouse move events in Modification mode
943 * \param theWindow an owner of the signal
944 * \param theEvent a mouse event
946 void CurveCreator_Widget::onMouseMove( SUIT_ViewWindow*, QMouseEvent* theEvent )
948 if ( getActionMode() != ModificationMode || !myDragStarted )
951 QPoint aPos = theEvent->pos();
952 if ( (aPos - myDragStartPosition).manhattanLength() < QApplication::startDragDistance() )
957 moveSelectedPoints( aPos.x(), aPos.y() );
958 myDragStartPosition = aPos;
960 END_MEASURE_TIME( "drag" );
964 * Set zero viewer by the last view closed in
965 * \param theManager a viewer manager
967 void CurveCreator_Widget::onLastViewClosed( SUIT_ViewManager* theManager )
972 void CurveCreator_Widget::onMousePress( QMouseEvent* theEvent )
974 onMousePress( 0, theEvent );
977 void CurveCreator_Widget::onMouseRelease( QMouseEvent* theEvent )
979 onMouseRelease( 0, theEvent );
982 void CurveCreator_Widget::onMouseMove( QMouseEvent* theEvent )
984 onMouseMove( 0, theEvent );
987 void CurveCreator_Widget::onCellChanged( int theRow, int theColumn )
989 int aCurrSect = myLocalPointView->getSectionId( theRow );
990 int aPntIndex = myLocalPointView->getPointId( theRow );
995 CurveCreator_ICurve::SectionToPointList aSelPoints;
996 startCurveModification( aSelPoints );
998 double aX = myLocalPointView->item( theRow, 2 )->data( Qt::UserRole ).toDouble();
999 double anY = myLocalPointView->item( theRow, 3 )->data( Qt::UserRole ).toDouble();
1000 std::deque<float> aChangedPos;
1001 aChangedPos.push_back( aX );
1002 aChangedPos.push_back( anY );
1003 myCurve->setPoint( aCurrSect, aPntIndex, aChangedPos );
1005 finishCurveModification( aSelPoints );
1009 * Removes a selected section from the curve. Updates undo/redo status
1011 void CurveCreator_Widget::removeSection()
1013 QList< QPair<int,int> > aSelPoints = mySectionView->getSelectedPoints();
1016 // myCurve->startOperation();
1017 for( int i = 0 ; i < aSelPoints.size() ; i++ ){
1018 if( aCurrSect != aSelPoints[i].first ){
1020 aCurrSect = aSelPoints[i].first;
1022 int aPntIndx = aSelPoints[i].second - aRemoveCnt;
1023 myCurve->removePoint( aCurrSect, aPntIndx );
1024 mySectionView->pointsRemoved( aCurrSect, aPntIndx );
1027 QList<int> aSections = mySectionView->getSelectedSections();
1028 for( int i = 0 ; i < aSections.size() ; i++ ){
1029 int aSectNum = aSections[i] - (i);
1030 myCurve->removeSection( aSectNum );
1031 mySectionView->sectionsRemoved( aSectNum );
1033 // myCurve->finishOperation();
1034 mySectionView->clearSelection();
1039 * Removes a selected points from the curve. Updates undo/redo status
1041 void CurveCreator_Widget::removePoint()
1043 CurveCreator_ICurve::SectionToPointList aPoints;
1044 getSelectedPoints( aPoints );
1045 if ( aPoints.size() == 0 )
1048 CurveCreator_ICurve::SectionToPointList aSelPoints;
1049 startCurveModification( aSelPoints, false );
1051 myCurve->removeSeveralPoints( aPoints );
1052 finishCurveModification( CurveCreator_ICurve::SectionToPointList() );
1055 void CurveCreator_Widget::addNewPoint(const CurveCreator::Coordinates& theCoords)
1059 QList<int> aSections = mySectionView->getSelectedSections();
1060 if( aSections.size() == 0 ){
1063 int aSection = aSections[0];
1064 myCurve->addPoints(theCoords, aSection); // add to the end of section
1065 mySectionView->pointsAdded( aSection, myCurve->getNbPoints( aSection ) );
1066 updateActionsStates();
1070 void CurveCreator_Widget::insertPointToSelectedSegment( const int theX,
1073 Handle(AIS_InteractiveContext) aContext = getAISContext();
1075 OCCViewer_ViewPort3d* aViewPort = getViewPort();
1076 Handle(V3d_View) aView;
1078 aView = aViewPort->getView();
1080 if ( aContext.IsNull() || aView.IsNull() )
1083 gp_Pnt aPoint1, aPoint2;
1084 Handle(AIS_InteractiveObject) anAISObject = myCurve->getAISObject();
1085 bool isFoundPoint = CurveCreator_Utils::pointOnObject( aView, anAISObject, theX, theY,
1086 aPoint, aPoint1, aPoint2 );
1087 if ( !isFoundPoint )
1090 // insert the point to the model curve
1091 CurveCreator_ICurve::SectionToPointList aSelPoints;
1092 startCurveModification( aSelPoints );
1094 CurveCreator::Coordinates aCoords;
1095 aCoords.push_back( aPoint.X() );
1096 aCoords.push_back( aPoint.Y() );
1098 CurveCreator_ICurve::SectionToPointList aPoints1, aPoints2;
1099 findSectionsToPoints( aPoint1.X(), aPoint1.Y(), aPoints1 );
1100 findSectionsToPoints( aPoint2.X(), aPoint2.Y(), aPoints2 );
1101 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aPoints1.begin(),
1102 aLast = aPoints1.end();
1103 int aSectionId = -1;
1104 // there can be a case when a new point is added into two sections
1105 int aPoint1Id = -1, aPoint2Id = -1;
1106 for ( ; anIt != aLast && aSectionId < 0; anIt++ ) {
1107 int aSectionCur = anIt->first;
1108 CurveCreator_ICurve::SectionToPointList::const_iterator anIt2 = aPoints2.begin(),
1109 aLast2 = aPoints2.end();
1110 for ( ; anIt2 != aLast2 && aSectionId < 0; anIt2++ ) {
1111 if ( anIt2->first == aSectionCur ) {
1112 aSectionId = aSectionCur;
1113 aPoint1Id = anIt->second;
1114 aPoint2Id = anIt2->second;
1119 int anInsertPos = -1;
1120 int aLastPoint = myCurve->getNbPoints( aSectionId )-1;
1121 if ( ( aPoint1Id == aLastPoint && aPoint2Id == 0 ) ||
1122 ( aPoint2Id == aLastPoint && aPoint1Id == 0 ) )
1123 anInsertPos = -1; // if the section happens between first and last points
1125 anInsertPos = aPoint1Id < aPoint2Id ? aPoint1Id + 1 : aPoint2Id + 1;
1127 myCurve->addPoints( aCoords, aSectionId, anInsertPos );
1128 mySectionView->pointsAdded( aSectionId, myCurve->getNbPoints( aSectionId ) );
1130 finishCurveModification( aSelPoints );
1132 setSelectedPoints();
1135 void CurveCreator_Widget::moveSelectedPoints( const int theXPosition,
1136 const int theYPosition )
1138 OCCViewer_ViewPort3d* aViewPort = getViewPort();
1142 CurveCreator_ICurve::SectionToPointList aPoints;
1143 startCurveModification( aPoints, false );
1145 gp_Pnt aStartPnt = CurveCreator_Utils::ConvertClickToPoint( myDragStartPosition.x(),
1146 myDragStartPosition.y(),
1147 aViewPort->getView() );
1148 gp_Pnt anEndPnt = CurveCreator_Utils::ConvertClickToPoint( theXPosition, theYPosition,
1149 aViewPort->getView() );
1150 double aXDelta = aStartPnt.X() - anEndPnt.X();
1151 double anYDelta = aStartPnt.Y() - anEndPnt.Y();
1153 CurveCreator_ICurve::SectionToPointCoordsList aCoordList;
1154 std::deque<float> aChangedPos;
1155 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = myDragPoints.begin(),
1156 aLast = myDragPoints.end();
1157 for ( ; anIt != aLast; anIt++ ) {
1158 int aSectionId = anIt->first;
1159 int aPointId = anIt->second;
1160 aChangedPos = myCurve->getPoint( aSectionId, aPointId );
1161 if ( aChangedPos.size() < 2 )
1163 aChangedPos[0] = aChangedPos[0] - aXDelta;
1164 aChangedPos[1] = aChangedPos[1] - anYDelta;
1166 aCoordList.push_back(
1167 std::make_pair(std::make_pair( aSectionId, aPointId ),
1170 myCurve->setSeveralPoints( aCoordList );
1173 finishCurveModification( myDragPoints );
1176 void CurveCreator_Widget::updateLocalPointView()
1178 if ( myDragStarted )
1180 Handle(AIS_InteractiveContext) aContext = getAISContext();
1181 if ( aContext.IsNull() )
1184 CurveCreator_Utils::getSelectedPoints( aContext, myCurve, myLocalPoints );
1185 int aNbPoints = myLocalPoints.size();
1186 bool isRowLimit = aNbPoints > myLocalPointRowLimit;
1187 myLocalPointView->setVisible( !isRowLimit );
1189 if ( !isRowLimit ) {
1190 bool isBlocked = myLocalPointView->blockSignals(true);
1191 myLocalPointView->setRowCount( 0 );
1192 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = myLocalPoints.begin(),
1193 aLast = myLocalPoints.end();
1194 for ( ; anIt != aLast; anIt++ )
1195 myLocalPointView->addLocalPointToTable( *anIt );
1197 myLocalPointView->blockSignals( isBlocked );
1204 void CurveCreator_Widget::setLocalPointContext( const bool theOpen, const bool isUpdateTable )
1206 CurveCreator_Utils::setLocalPointContext( myCurve, getAISContext(), theOpen );
1207 if ( !theOpen && isUpdateTable )
1208 updateLocalPointView();
1212 * Set drag operation started. Save the position and a list of dragged points
1213 * \param theState the drag operation state: started/finished
1214 * \param thePoint the start drag position
1216 void CurveCreator_Widget::setDragStarted( const bool theState, const QPoint& thePoint )
1219 getSelectedPoints( myDragPoints );
1220 myDragStarted = myDragPoints.size();
1221 myDragStartPosition = thePoint;
1222 if ( myDragStarted ) {
1223 // change a viewer interaction style in order to avoid a select rectangle build
1224 myDragInteractionStyle = changeInteractionStyle( SUIT_ViewModel::KEY_FREE );
1228 if ( myDragStarted )
1229 changeInteractionStyle( myDragInteractionStyle );
1230 myDragStarted = false;
1231 myDragPoints.clear();
1236 void CurveCreator_Widget::getSelectedPoints( CurveCreator_ICurve::SectionToPointList& thePoints )
1239 thePoints = myLocalPoints;
1242 void CurveCreator_Widget::setSelectedPoints( const CurveCreator_ICurve::SectionToPointList& thePoints )
1244 if ( myDragStarted )
1246 Handle(AIS_InteractiveContext) aContext = getAISContext();
1247 if ( aContext.IsNull() || !aContext->HasOpenedContext() )
1250 CurveCreator_Utils::setSelectedPoints( aContext, myCurve, thePoints );
1252 updateLocalPointView();
1256 * Get viewer information before perform the curve modification.
1257 * Take a list of selected cuve points an close local context.
1258 * The context should be closed because the curve presentation is
1259 * redisplayed and if it is not closed, when we close the local context
1260 * later, the presentation shown in the local context is disappeared.
1261 * \param thePoints an output list of curve selected points
1262 * \param theFillPoints a flag whether the selection list should be filled
1264 void CurveCreator_Widget::startCurveModification(
1265 CurveCreator_ICurve::SectionToPointList& thePoints,
1266 const bool theFillPoints )
1268 if ( theFillPoints ) {
1270 getSelectedPoints( thePoints );
1272 setLocalPointContext( false );
1276 * Restore the viewer state after the curve modification is done.
1277 * Open local context and select given points inside it.
1278 * \param thePoints a list of curve selected points
1280 void CurveCreator_Widget::finishCurveModification(
1281 const CurveCreator_ICurve::SectionToPointList& thePoints )
1283 if ( getActionMode() == ModificationMode )
1284 setLocalPointContext( true );
1285 setSelectedPoints( thePoints );
1290 * Returns a point index in the model curve by the point coordinates in the viewer
1291 * \param theX the X coordinate of the point
1292 * \param theY the Y coordinate of the point
1294 int CurveCreator_Widget::findLocalPointIndex( int theSectionId, float theX, float theY )
1296 return CurveCreator_UtilsICurve::findLocalPointIndex( myCurve, theSectionId, theX, theY );
1299 void CurveCreator_Widget::findSectionsToPoints( const double theX, const double theY,
1300 CurveCreator_ICurve::SectionToPointList& thePoints )
1302 return CurveCreator_UtilsICurve::findSectionsToPoints( myCurve, theX, theY, thePoints );
1305 void CurveCreator_Widget::convert( const CurveCreator_ICurve::SectionToPointList& thePoints,
1306 QMap<int, QList<int> >& theConvPoints )
1308 return CurveCreator_UtilsICurve::convert( thePoints, theConvPoints );
1312 * Returns whethe the container has the value
1313 * \param theList a container of values
1314 * \param theValue a value
1316 bool CurveCreator_Widget::contains( const CurveCreator_ICurve::SectionToPointList& theList,
1317 const CurveCreator_ICurve::SectionToPoint& theValue ) const
1319 return CurveCreator_UtilsICurve::contains( theList, theValue );