Salome HOME
8118eca90a7488c2565b58aaa7e6c3d92452f034
[modules/hydro.git] / src / HYDROCurveCreator / CurveCreator_Widget.cxx
1 // Copyright (C) 2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "CurveCreator_Widget.h"
21 #include "CurveCreator_TreeView.h"
22 #include "CurveCreator_ICurve.hxx"
23 //#include "CurveCreator_CurveEditor.hxx"
24 #include "CurveCreator.hxx"
25 //#include "CurveCreator_NewPointDlg.h"
26 #include "CurveCreator_NewSectionDlg.h"
27 #include "CurveCreator_Utils.h"
28 #include "CurveCreator_UtilsICurve.hxx"
29 #include "CurveCreator_TableView.h"
30
31 #include <SUIT_Session.h>
32 #include <SUIT_Desktop.h>
33 #include <SUIT_ResourceMgr.h>
34 #include <SUIT_ViewManager.h>
35
36 #include <OCCViewer_ViewWindow.h>
37 #include <OCCViewer_ViewManager.h>
38 #include <OCCViewer_ViewPort3d.h>
39 #include "OCCViewer_Utilities.h"
40
41 #include <BRep_Tool.hxx>
42 #include <TopoDS.hxx>
43 #include <TopoDS_Vertex.hxx>
44 #include <TopoDS_Wire.hxx>
45 #include <TopoDS_Edge.hxx>
46 #include <gp_Lin.hxx>
47 #include <gp_Dir.hxx>
48 #include <TopExp_Explorer.hxx>
49
50 #include <GeomAPI_ProjectPointOnCurve.hxx>
51
52 #include <AIS_ListOfInteractive.hxx>
53 #include <AIS_ListIteratorOfListOfInteractive.hxx>
54 #include <AIS_Shape.hxx>
55 #include <AIS_LocalContext.hxx>
56 #include <Geom_Point.hxx>
57 #include <Geom_BSplineCurve.hxx>
58 #include <Geom_Line.hxx>
59
60 #include <QHBoxLayout>
61 #include <QVBoxLayout>
62 #include <QLabel>
63 #include <QLineEdit>
64 #include <QGroupBox>
65 #include <QToolButton>
66 #include <QToolBar>
67 #include <QAction>
68 #include <QMenu>
69 #include <QMouseEvent>
70 #include <QApplication>
71 #include <QTableWidget>
72 #include <QTime>
73
74 //#define MEASURE_TIME
75
76 #ifdef MEASURE_TIME
77
78   #define START_MEASURE_TIME \
79     QTime aTimer;            \
80     aTimer.start();          \
81
82   #define END_MEASURE_TIME( theMsg )                      \
83     double aTime = aTimer.elapsed() * 0.001;              \
84     FILE* aFile = fopen( "performance", "a" );            \
85     fprintf( aFile, "%s = %.3lf sec\n", theMsg, aTime );  \
86     fclose( aFile );                                      \
87
88 #else
89
90   #define START_MEASURE_TIME
91   #define END_MEASURE_TIME( theMsg )
92
93 #endif
94
95
96
97
98
99
100 CurveCreator_Widget::CurveCreator_Widget(QWidget* parent,
101                                          CurveCreator_ICurve *theCurve,
102                                          Qt::WindowFlags fl,
103                                          int theLocalPointRowLimit )
104 : QWidget(parent), myNewSectionEditor(NULL), myCurve(theCurve), mySection(0),
105   myDragStarted( false ), myDragInteractionStyle( SUIT_ViewModel::STANDARD ),
106   myOCCViewer( 0 ), myLocalPointRowLimit( theLocalPointRowLimit )
107 {
108   myNewSectionEditor = new CurveCreator_NewSectionDlg( this );
109   myNewSectionEditor->hide();
110   connect( myNewSectionEditor, SIGNAL(addSection()), this, SLOT(onAddNewSection()) );
111   connect( myNewSectionEditor, SIGNAL(modifySection()), this, SLOT(onModifySection()) );
112   connect( myNewSectionEditor, SIGNAL(cancelSection()), this, SLOT(onCancelSection()) );
113
114   QGroupBox* aSectionGroup = new QGroupBox(tr("Sections"),this);
115
116   mySectionView = new CurveCreator_TreeView(myCurve, aSectionGroup);
117   connect( mySectionView, SIGNAL(selectionChanged()), this, SLOT( onSelectionChanged() ) );
118   connect( mySectionView, SIGNAL(sectionEntered(int)), this, SLOT(onEditSection(int)) );
119   connect( mySectionView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onContextMenu(QPoint)) );
120
121   myLocalPointView = new CurveCreator_TableView( myCurve, this );
122   connect( myLocalPointView, SIGNAL( cellChanged( int, int ) ),
123            this, SLOT( onCellChanged( int, int ) ) );
124
125   QToolBar* aTB = new QToolBar(tr("TOOL_BAR_TLT"), aSectionGroup);
126 //    QToolButton* anUndoBtn = new QToolButton(aTB);
127
128   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
129   QPixmap anUndoPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_UNDO")));
130   QPixmap aRedoPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_REDO")));
131   QPixmap aNewSectionPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_NEW_SECTION")));
132   QPixmap aNewPointPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_NEW_POINT")));
133   QPixmap anEditPointsPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_EDIT_POINTS")));
134   QPixmap aDetectPointsPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_EDIT_POINTS")));
135   QPixmap aPolylinePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_POLYLINE")));
136   QPixmap aSplinePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_SPLINE")));
137   QPixmap aRemovePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_DELETE")));
138   QPixmap aJoinPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_JOIN")));
139   QPixmap aStepUpPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_ARROW_UP")));
140   QPixmap aStepDownPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_ARROW_DOWN")));
141
142   QAction* anAct = createAction( UNDO_ID, tr("UNDO"), anUndoPixmap, tr("UNDO_TLT"), 
143                                  QKeySequence(Qt::ControlModifier|Qt::Key_Z) );
144   connect(anAct, SIGNAL(triggered()), this, SLOT(onUndo()) );
145   aTB->addAction(anAct);
146
147   anAct = createAction( REDO_ID, tr("REDO"), aRedoPixmap, tr("REDO_TLT"), 
148                         QKeySequence(Qt::ControlModifier|Qt::Key_Y) );
149   connect(anAct, SIGNAL(triggered()), this, SLOT(onRedo()) );
150   aTB->addAction(anAct);
151
152   aTB->addSeparator();
153
154   anAct = createAction( NEW_SECTION_ID, tr("NEW_SECTION"), aNewSectionPixmap, tr("NEW_SECTION_TLT"), 
155                         QKeySequence(Qt::ControlModifier|Qt::Key_N) );
156   connect(anAct, SIGNAL(triggered()), this, SLOT(onNewSection()) );
157   aTB->addAction(anAct);
158   aTB->addSeparator();
159
160   anAct = createAction( ADDITION_MODE_ID, tr("ADDITION_MODE"), aNewPointPixmap, tr("ADDITION_MODE_TLT"), 
161                         QKeySequence() );
162   anAct->setCheckable(true);
163   connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onAdditionMode(bool)) );
164   connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
165   aTB->addAction(anAct);
166   
167   anAct = createAction( MODIFICATION_MODE_ID, tr("MODIFICATION_MODE"), anEditPointsPixmap, tr("MODIFICATION_MODE_TLT"), 
168                         QKeySequence() );
169   anAct->setCheckable(true);
170   connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onModificationMode(bool)) );
171   connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
172   aTB->addAction(anAct);
173
174   anAct = createAction( DETECTION_MODE_ID, tr("DETECTION_MODE"), aDetectPointsPixmap, tr("DETECTION_MODE_TLT"), 
175                         QKeySequence() );
176   anAct->setCheckable(true);
177   connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onDetectionMode(bool)) );
178   connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
179   aTB->addAction(anAct);
180
181   anAct = createAction( CLOSE_SECTIONS_ID, tr("CLOSE_SECTIONS"), QPixmap(), tr("CLOSE_SECTIONS_TLT"), 
182                         QKeySequence(Qt::ControlModifier|Qt::Key_W) );
183   connect(anAct, SIGNAL(triggered()), this, SLOT(onCloseSections()) );
184
185   anAct = createAction( UNCLOSE_SECTIONS_ID, tr("UNCLOSE_SECTIONS"), QPixmap(), 
186                         tr("UNCLOSE_SECTIONS_TLT"), QKeySequence(Qt::ControlModifier|Qt::Key_S) );
187   connect(anAct, SIGNAL(triggered()), this, SLOT(onUncloseSections()) );
188
189   anAct = createAction( SET_SECTIONS_POLYLINE_ID, tr("SET_SECTIONS_POLYLINE"), 
190                         aPolylinePixmap, tr("SET_POLYLINE_TLT"), 
191                         QKeySequence(Qt::ControlModifier|Qt::Key_E) );
192   connect(anAct, SIGNAL(triggered()), this, SLOT(onSetPolyline()) );
193
194   anAct = createAction( SET_SECTIONS_SPLINE_ID, tr("SET_SECTIONS_SPLINE"), aSplinePixmap, 
195                         tr("SET_SPLINE_TLT"), QKeySequence(Qt::ControlModifier|Qt::Key_R) );
196   connect(anAct, SIGNAL(triggered()), this, SLOT(onSetSpline()) );
197
198   anAct = createAction( REMOVE_ID, tr("REMOVE"), aRemovePixmap, tr("REMOVE_TLT"), 
199                         QKeySequence(Qt::ControlModifier|Qt::Key_Delete ) );
200   connect(anAct, SIGNAL(triggered()), this, SLOT(onRemove()) );
201   aTB->addAction(anAct);
202   // TODO join
203   //aTB->addSeparator();
204
205   //anAct = createAction( JOIN_ID, tr("JOIN"), aJoinPixmap, tr("JOIN_TLT"), 
206   //                      QKeySequence(Qt::ControlModifier|Qt::Key_Plus ) );
207   //connect( anAct, SIGNAL(triggered()), this, SLOT(onJoin()) );
208   //aTB->addAction(anAct);
209
210   anAct = createAction( CLEAR_ALL_ID, tr("CLEAR_ALL"), QPixmap(), tr("CLEAR_ALL_TLT"), 
211                         QKeySequence(Qt::ControlModifier | Qt::ShiftModifier | Qt::Key_Delete ) );
212   connect( anAct, SIGNAL(triggered()), this, SLOT( onClearAll()) );
213
214   // TODO join
215   //anAct = createAction( JOIN_ALL_ID, tr("JOIN_ALL"), QPixmap(), tr("JOIN_ALL_TLT"), 
216   //                      QKeySequence(Qt::ControlModifier | Qt::ShiftModifier | Qt::Key_Plus ) );
217   //connect( anAct, SIGNAL(triggered()), this, SLOT(onJoinAll()) );
218
219   QVBoxLayout* aSectLayout = new QVBoxLayout();
220   aSectLayout->setMargin( 5 );
221   aSectLayout->setSpacing( 5 );
222   aSectLayout->addWidget(aTB);
223   aSectLayout->addWidget(mySectionView);
224   aSectLayout->addWidget( myLocalPointView );
225   aSectionGroup->setLayout(aSectLayout);
226   QVBoxLayout* aLay = new QVBoxLayout();
227   aLay->setMargin( 0 );
228   aLay->setSpacing( 5 );
229 //    aLay->addLayout(aNameLayout);
230   aLay->addWidget(aSectionGroup);
231   setLayout(aLay);
232
233   updateActionsStates();
234   updateUndoRedo();
235 }
236
237 /**
238  * Set an OCC viewer
239  */
240 void CurveCreator_Widget::setOCCViewer( OCCViewer_Viewer* theViewer )
241 {
242   if ( myOCCViewer == theViewer )
243     return;
244
245   if ( myOCCViewer ) {
246     OCCViewer_ViewManager* aViewManager = dynamic_cast<OCCViewer_ViewManager*>
247                                                     ( myOCCViewer->getViewManager() );
248     disconnect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
249            this, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ) );
250     disconnect( aViewManager, SIGNAL( mouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ),
251            this, SLOT( onMouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ) );
252     disconnect( aViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
253            this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
254     disconnect( aViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
255            this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
256     // restore normal mode in the viewer
257     OCCViewer_Utilities::setViewer2DMode( myOCCViewer, OCCViewer_ViewWindow::No2dMode );
258     // all local contexts should be closed if the viewer is not more used
259     setLocalPointContext( false, true );
260   }
261
262   myOCCViewer = theViewer;
263   if ( myOCCViewer ) {
264     OCCViewer_ViewManager* aViewManager = dynamic_cast<OCCViewer_ViewManager*>
265                                                     ( myOCCViewer->getViewManager() );
266     connect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
267            this, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ) );
268     connect( aViewManager, SIGNAL( mouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ),
269            this, SLOT( onMouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ) );
270     connect( aViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
271            this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
272     connect( aViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
273            this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
274     OCCViewer_Utilities::setViewer2DMode( myOCCViewer, OCCViewer_ViewWindow::XYPlane );
275   }
276 }
277
278 /**
279  * Returns current OCC viewer
280  */
281 OCCViewer_Viewer* CurveCreator_Widget::getOCCViewer()
282 {
283   return myOCCViewer;
284 }
285
286 /**
287  * Returns OCC viewer context
288  */
289 Handle(AIS_InteractiveContext) CurveCreator_Widget::getAISContext()
290 {
291   Handle(AIS_InteractiveContext) aContext;
292   OCCViewer_Viewer* aViewer = getOCCViewer();
293   if ( aViewer )
294     aContext = aViewer->getAISContext();
295
296   return aContext;
297 }
298
299 /**
300  * Returns OCC viewer view port
301  */
302 OCCViewer_ViewPort3d* CurveCreator_Widget::getViewPort()
303 {
304   OCCViewer_ViewPort3d* aViewPort = 0;
305   OCCViewer_Viewer* aViewer = getOCCViewer();
306   if ( aViewer )
307     aViewPort = ((OCCViewer_ViewWindow*)aViewer->getViewManager()->getActiveView())->getViewPort();
308     
309   return aViewPort;
310 }
311
312 /**
313  * Set interaction style in the OCC viewer
314  * \param theStyle a new style
315  * \return the previous style
316  */
317 int CurveCreator_Widget::changeInteractionStyle( int theStyle )
318 {
319   OCCViewer_Viewer* aViewer = getOCCViewer();
320   if ( !aViewer )
321     return -1;
322
323   int aPrevStyle = aViewer->interactionStyle();
324   aViewer->setInteractionStyle( theStyle );
325
326   return aPrevStyle;
327 }
328
329 //=======================================================================
330 // function: reset
331 // purpose: reset the widget viewer, close local context, clear selection
332 //=======================================================================
333 void CurveCreator_Widget::reset()
334 {
335 }
336
337 void CurveCreator_Widget::setCurve( CurveCreator_ICurve* theCurve )
338 {
339   myCurve = theCurve;
340   mySectionView->setCurve( myCurve );
341   myLocalPointView->setCurve( myCurve );
342   updateActionsStates();
343   updateUndoRedo();
344 }
345
346 void CurveCreator_Widget::onSelectionChanged()
347 {
348   updateActionsStates();
349   updateUndoRedo();
350   emit selectionChanged();
351 }
352
353 void CurveCreator_Widget::updateActionsStates()
354 {
355   QList<ActionId> anEnabledAct;
356   if( myCurve ){
357     anEnabledAct << NEW_SECTION_ID << MODIFICATION_MODE_ID;
358     if ( removeEnabled() )
359       anEnabledAct << REMOVE_ID;
360     QList<int> aSelSections = mySectionView->getSelectedSections();
361     QList< QPair< int, int > > aSelPoints = mySectionView->getSelectedPoints();
362     CurveCreator_TreeView::SelectionType aSelType = mySectionView->getSelectionType();
363     switch( aSelType ){
364     case CurveCreator_TreeView::ST_NOSEL:{
365       break;
366     }
367     case CurveCreator_TreeView::ST_SECTIONS:{
368       /*if( aSelSections[0] > 0 ){
369         anEnabledAct << UP_ID;
370       }*/
371       if( aSelSections.size() == 1 ){
372         anEnabledAct << ADDITION_MODE_ID << DETECTION_MODE_ID;
373       }
374       switch ( getActionMode() ) {
375         case AdditionMode: {
376           mySection = -1;
377           myPointNum = -1;
378           QList<int> aSelSection = mySectionView->getSelectedSections();
379           if( aSelSection.size() > 0 ){
380             mySection = aSelSection[0];
381             myPointNum = myCurve->getNbPoints(mySection);
382           }
383         }
384         break;
385         case ModificationMode: {
386          anEnabledAct << CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID << SET_SECTIONS_POLYLINE_ID << SET_SECTIONS_SPLINE_ID;
387           int aSectCnt = myCurve->getNbSections();
388           if( aSectCnt > 0 )
389             anEnabledAct << CLEAR_ALL_ID;
390           // TODO
391           //if( aSectCnt > 1 )
392           //  anEnabledAct << JOIN_ALL_ID;
393           //if( aSelSections.size() > 1 ){
394           //  anEnabledAct << JOIN_ID;
395           //}
396         }
397         break;
398         case DetectionMode: {
399         }
400         break;
401         case NoneMode:
402         default:
403         break;
404       }
405       /*if( aSelSections[ aSelSections.size() - 1 ] < ( myCurve->getNbSections() - 1 ) ){
406         anEnabledAct << DOWN_ID;
407       }*/
408       break;
409     }
410     /*case CurveCreator_TreeView::ST_POINTS_ONE_SECTION:{
411       if( aSelPoints[0].second > 0 ){
412         anEnabledAct << UP_ID;
413       }
414       int aLastIndex = aSelPoints.size()-1;
415       int aSect = aSelPoints[0].first;
416       if( aSelPoints[aLastIndex].second < (myCurve->getNbPoints(aSect) - 1)){
417         anEnabledAct << DOWN_ID;
418       }
419       if( aSelPoints.size() == 1){
420         anEnabledAct << INSERT_POINT_BEFORE_ID << INSERT_POINT_AFTER_ID;
421       }
422       break;
423     }*/
424
425     }
426     
427     /*int aSelObjsCnt = aSelPoints.size() + aSelSections.size();
428     if( aSelObjsCnt > 0 ){
429       anEnabledAct << REMOVE_ID;
430     }
431     if( (myCurve->getNbSections() + myCurve->getNbPoints()) > 0 ){
432       anEnabledAct << REMOVE_ALL_ID;
433     }*/
434     // TODO
435     //if( myCurve->getNbSections() > 1 ){
436     //  anEnabledAct << JOIN_ALL_ID;
437     //}
438   }
439   QList<ActionId> anIds = myActionMap.keys();
440   for( int i = 0 ; i < anIds.size() ; i++ ){
441     if( myActionMap.contains(anIds[i]) ){
442       if( anEnabledAct.contains(anIds[i]) ){
443         myActionMap[anIds[i]]->setEnabled(true);
444       }
445       else{
446         myActionMap[anIds[i]]->setEnabled(false);
447       }
448     }
449   }
450 }
451
452 void CurveCreator_Widget::onAdditionMode(bool checked)
453 {
454   if (!checked)
455     return;
456
457   Handle(AIS_InteractiveContext) aContext = getAISContext();
458   if( !myCurve || aContext.IsNull() )
459     return;
460
461   mySection= -1;
462   myPointNum = -1;
463   QList<int> aSelSection = mySectionView->getSelectedSections();
464   if( aSelSection.size() > 0 ){
465     mySection = aSelSection[0];
466   }
467   else{
468     QList< QPair<int,int> > aSelPoints = mySectionView->getSelectedPoints();
469     if( aSelPoints.size() > 0 ){
470       mySection = aSelPoints[0].first;
471       myPointNum = aSelPoints[0].second + 1;
472     }
473   }
474 //  emit subOperationStarted( myNewPointEditor );
475 }
476
477 void CurveCreator_Widget::onModificationMode(bool checked)
478 {
479   myLocalPointView->setVisible( checked );
480 }
481
482 void CurveCreator_Widget::onDetectionMode(bool checked)
483 {
484 }
485
486 void CurveCreator_Widget::onModeChanged(bool checked)
487 {
488   ActionMode aMode = NoneMode;
489   if (checked) {
490     QAction* anAction = (QAction*)sender();
491     switch(myActionMap.key(anAction)) {
492       case ADDITION_MODE_ID:
493         aMode = AdditionMode;
494         if (myActionMap[MODIFICATION_MODE_ID]->isChecked())
495           myActionMap[MODIFICATION_MODE_ID]->trigger();
496         else if (myActionMap[DETECTION_MODE_ID]->isChecked())
497           myActionMap[DETECTION_MODE_ID]->trigger();
498         break;
499       case MODIFICATION_MODE_ID:
500         aMode = ModificationMode;
501         if (myActionMap[ADDITION_MODE_ID]->isChecked())
502           myActionMap[ADDITION_MODE_ID]->trigger();
503         else if (myActionMap[DETECTION_MODE_ID]->isChecked())
504           myActionMap[DETECTION_MODE_ID]->trigger();
505         break;
506       case DETECTION_MODE_ID:
507         aMode = DetectionMode;
508         if (myActionMap[ADDITION_MODE_ID]->isChecked())
509           myActionMap[ADDITION_MODE_ID]->trigger();
510         else if (myActionMap[MODIFICATION_MODE_ID]->isChecked())
511           myActionMap[MODIFICATION_MODE_ID]->trigger();
512         break;
513     }
514   }
515   updateActionsStates();
516   updateUndoRedo();
517   setLocalPointContext( aMode == ModificationMode, true );
518 }
519
520 void CurveCreator_Widget::onAddNewPoint(const CurveCreator::Coordinates& theCoords)
521 {
522   if( !myCurve )
523     return;
524   //myCurve->addPoints(theCoords, mySection, myPointNum );
525   //mySectionView->pointsAdded( mySection, myPointNum );
526   //myPointNum++;
527   QList<int> aSections = mySectionView->getSelectedSections();
528   if( aSections.size() == 0 ){
529     return;
530   }
531   int aSection = aSections[0];
532   myCurve->addPoints(theCoords, aSection); // add to the end of section
533   mySectionView->pointsAdded( aSection, myCurve->getNbPoints( aSection ) );
534   updateActionsStates();
535   updateUndoRedo();
536 }
537
538 void CurveCreator_Widget::onNewSection()
539 {
540   if( !myCurve )
541     return;
542   myNewSectionEditor->clear();
543   myNewSectionEditor->setEditMode(false);
544   QString aSectName = QString( CurveCreator_UtilsICurve::getUniqSectionName( myCurve ).c_str() );
545   myNewSectionEditor->setSectionParameters(aSectName, true, CurveCreator::Polyline );
546   emit subOperationStarted( myNewSectionEditor );
547 }
548
549 void CurveCreator_Widget::onAddNewSection()
550 {
551   if( !myCurve )
552     return;
553   myCurve->addSection( myNewSectionEditor->getName().toStdString(),
554                        myNewSectionEditor->getSectionType(),
555                        myNewSectionEditor->isClosed() );
556   mySectionView->sectionAdded( -1 ); // add a new section to the end of list
557   QString aNewName = QString( CurveCreator_UtilsICurve::getUniqSectionName( myCurve ).c_str() );
558   myNewSectionEditor->setSectionName(aNewName);
559   updateActionsStates();
560   updateUndoRedo();
561   onCancelSection();
562 }
563
564 void CurveCreator_Widget::onCancelSection()
565 {
566   emit subOperationFinished( myNewSectionEditor );
567 }
568
569 QAction* CurveCreator_Widget::createAction( ActionId theId, const QString& theName, const QPixmap& theImage,
570                                             const QString& theToolTip, const QKeySequence& theShortcut )
571 {
572   QAction* anAct = new QAction(theName,this);
573   if( !theImage.isNull() ){
574     anAct->setIcon(theImage);
575   }
576   anAct->setShortcut(theShortcut);
577   anAct->setToolTip(theToolTip);
578   myActionMap[theId] = anAct;
579   return anAct;
580 }
581
582 QAction* CurveCreator_Widget::getAction(ActionId theId)
583 {
584   if( myActionMap.contains(theId) )
585     return myActionMap[theId];
586   return NULL;
587 }
588
589 CurveCreator_Widget::ActionMode CurveCreator_Widget::getActionMode() const
590 {
591   ActionMode aMode = NoneMode;
592
593   if ( myActionMap[ADDITION_MODE_ID]->isChecked() )
594     aMode = AdditionMode;
595   else if ( myActionMap[MODIFICATION_MODE_ID]->isChecked() )
596     aMode = ModificationMode;
597   else if ( myActionMap[DETECTION_MODE_ID]->isChecked() )
598     aMode = DetectionMode;
599
600   return aMode;
601 }
602
603 void CurveCreator_Widget::onEditSection( int theSection )
604 {
605   if( !myCurve )
606     return;
607   mySection = theSection;
608   QString aSectName = QString::fromStdString( myCurve->getSectionName(theSection));
609   bool isClosed = myCurve->isClosed(theSection);
610   CurveCreator::SectionType aType = myCurve->getSectionType(theSection);
611   myNewSectionEditor->setEditMode(true);
612   myNewSectionEditor->setSectionParameters( aSectName, isClosed, aType );
613
614   emit subOperationStarted( myNewSectionEditor );
615 }
616
617 void CurveCreator_Widget::onModifySection()
618 {
619   if( !myCurve )
620     return;
621   QString aName = myNewSectionEditor->getName();
622   bool isClosed = myNewSectionEditor->isClosed();
623   CurveCreator::SectionType aSectType = myNewSectionEditor->getSectionType();
624 //  myCurve->startOperation();
625   if( myCurve->getSectionName(mySection) != aName.toStdString() )
626     myCurve->setSectionName( mySection , aName.toStdString() );
627
628   if( myCurve->getSectionType(mySection) != aSectType )
629     myCurve->setSectionType( mySection, aSectType );
630
631   if( myCurve->isClosed(mySection) != isClosed )
632     myCurve->setClosed( mySection, isClosed );
633 //  myCurve->finishOperation();
634   mySectionView->sectionChanged(mySection);
635   updateUndoRedo();
636   onCancelSection();
637 }
638
639 /*void CurveCreator_Widget::onEditPoint( int theSection, int thePoint )
640 {
641   if( !myNewPointEditor || !myEdit )
642     return;
643   mySection = theSection;
644   myPointNum = thePoint;
645   QString aSectName = QString::fromStdString( myCurve->getSectionName(theSection));
646   myNewPointEditor->setEditMode(true);
647   myNewPointEditor->setSectionName(aSectName);
648   myNewPointEditor->setDimension( myCurve->getDimension() );
649   CurveCreator::Coordinates aCoords = myCurve->getCoordinates(theSection,thePoint);
650   myNewPointEditor->setCoordinates(aCoords);
651   emit subOperationStarted( myNewPointEditor );
652 }
653
654 void CurveCreator_Widget::onModifyPoint()
655 {
656   if( !myEdit )
657     return;
658   CurveCreator::Coordinates aCoords = myNewPointEditor->getCoordinates();
659   myEdit->setCoordinates( aCoords, mySection, myPointNum );
660   mySectionView->pointDataChanged( mySection, myPointNum );
661   updateUndoRedo();
662   onCancelPoint();
663 }*/
664
665 void CurveCreator_Widget::onJoin()
666 {
667   if( !myCurve )
668     return;
669   QList<int> aSections = mySectionView->getSelectedSections();
670   if( aSections.size() == 0 ){
671     return;
672   }
673   int aMainSect = aSections[0];
674   int aMainSectSize = myCurve->getNbPoints(aMainSect);
675 //  myCurve->startOperation();
676   for( int i = 1 ; i < aSections.size() ; i++ ){
677     int aSectNum = aSections[i] - (i-1);
678     myCurve->join( aMainSect, aSectNum );
679     mySectionView->sectionsRemoved( aSectNum );
680   }
681 //  myCurve->finishOperation();
682   int aNewSectSize = myCurve->getNbPoints(aMainSect);
683   if( aNewSectSize != aMainSectSize )
684     mySectionView->pointsAdded( aMainSect, aMainSectSize, aNewSectSize-aMainSectSize );
685   updateUndoRedo();
686 }
687
688 void CurveCreator_Widget::onRemove()
689 {
690   if( !myCurve )
691     return;
692
693   switch( getActionMode() ) {
694     case NoneMode:
695       removeSection();
696     break;
697     case ModificationMode:
698       removePoint();
699     break;
700     default:
701       break;
702   }
703 }
704
705 void CurveCreator_Widget::onClearAll()
706 {
707   if( !myCurve )
708     return;
709   myCurve->clear();
710   mySectionView->reset();
711   updateActionsStates();
712   updateUndoRedo();
713 }
714
715 void CurveCreator_Widget::onJoinAll()
716 {
717   if( !myCurve )
718     return;
719   myCurve->join();
720   mySectionView->reset();
721   updateActionsStates();
722   updateUndoRedo();
723 }
724
725 void CurveCreator_Widget::onUndoSettings()
726 {
727
728 }
729
730 void CurveCreator_Widget::onSetSpline()
731 {
732   if( !myCurve )
733     return;
734   QList<int> aSelSections = mySectionView->getSelectedSections();
735 //  myCurve->startOperation();
736   for( int i = 0 ; i < aSelSections.size() ; i++ ){
737     myCurve->setSectionType(aSelSections[i], CurveCreator::Spline );
738     mySectionView->sectionChanged(aSelSections[i]);
739   }
740 //  myCurve->finishOperation();
741   updateUndoRedo();
742 }
743
744 void CurveCreator_Widget::onSetPolyline()
745 {
746   if( !myCurve )
747     return;
748 //  myCurve->startOperation();
749   QList<int> aSelSections = mySectionView->getSelectedSections();
750   for( int i = 0 ; i < aSelSections.size() ; i++ ){
751     myCurve->setSectionType( aSelSections[i], CurveCreator::Polyline );
752     mySectionView->sectionChanged( aSelSections[i] );
753   }
754 //  myCurve->finishOperation();
755   updateUndoRedo();
756 }
757
758 void CurveCreator_Widget::onCloseSections()
759 {
760   if( !myCurve )
761     return;
762 //  myCurve->startOperation();
763   QList<int> aSelSections = mySectionView->getSelectedSections();
764   for( int i = 0 ; i < aSelSections.size() ; i++ ){
765     myCurve->setClosed(aSelSections[i], true);
766     mySectionView->sectionChanged(aSelSections[i]);
767   }
768 //  myCurve->finishOperation();
769   updateUndoRedo();
770 }
771
772 void CurveCreator_Widget::onUncloseSections()
773 {
774   if( !myCurve )
775     return;
776 //  myCurve->startOperation();
777   QList<int> aSelSections = mySectionView->getSelectedSections();
778   for( int i = 0 ; i < aSelSections.size() ; i++ ){
779     myCurve->setClosed(aSelSections[i], false);
780     mySectionView->sectionChanged(aSelSections[i]);
781   }
782 //  myCurve->finishOperation();
783   updateUndoRedo();
784 }
785
786 void CurveCreator_Widget::onUndo()
787 {
788     if( !myCurve )
789       return;
790
791     CurveCreator_ICurve::SectionToPointList aPoints;
792     startCurveModification( aPoints, false );
793     myCurve->undo();
794     finishCurveModification();
795     mySectionView->reset();
796 }
797
798 void CurveCreator_Widget::onRedo()
799 {
800     if( !myCurve )
801       return;
802     CurveCreator_ICurve::SectionToPointList aPoints;
803     startCurveModification( aPoints, false );
804     myCurve->redo();
805     finishCurveModification();
806     mySectionView->reset();
807 }
808
809 void CurveCreator_Widget::updateUndoRedo()
810 {
811   if( !myCurve )
812     return;
813   QAction* anAct = myActionMap[UNDO_ID];
814   if( anAct != 0 ){
815     if( myCurve->getNbUndo() != 0 ){
816       anAct->setEnabled(true);
817     }
818     else{
819       anAct->setDisabled(true);
820     }
821   }
822   anAct = myActionMap[REDO_ID];
823   if( anAct != 0 ){
824     if( myCurve->getNbRedo() != 0 ){
825       anAct->setEnabled(true);
826     }
827     else{
828       anAct->setDisabled(true);
829     }
830   }
831 }
832
833 void CurveCreator_Widget::onContextMenu( QPoint thePoint )
834 {
835   QList<ActionId> aContextActions;
836   aContextActions << CLEAR_ALL_ID << JOIN_ALL_ID << SEPARATOR_ID <<
837                      CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID << SET_SECTIONS_POLYLINE_ID <<
838                      SET_SECTIONS_SPLINE_ID;
839   QPoint aGlPoint = mySectionView->mapToGlobal(thePoint);
840   bool isVis = false;
841   QList<ActionId> aResAct;
842   for( int i = 0 ; i < aContextActions.size() ; i++ ){
843     if( aContextActions[i] != SEPARATOR_ID ){
844       if( myActionMap.contains(aContextActions[i]) ){
845         QAction* anAct = myActionMap[aContextActions[i]];
846         if( anAct->isEnabled() ){
847           aResAct << aContextActions[i];
848           isVis = true;
849         }
850       }
851     }
852     else{
853       aResAct << SEPARATOR_ID;
854     }
855   }
856   if( !isVis )
857     return;
858
859   QMenu* aMenu = new QMenu(this);
860   for( int i = 0 ; i < aResAct.size() ; i++ ){
861     if( aResAct[i] == SEPARATOR_ID ){
862       aMenu->addSeparator();
863     }
864     else{
865       QAction* anAct = myActionMap[aResAct[i]];
866       aMenu->insertAction(NULL, anAct);
867     }
868   }
869   aMenu->exec(aGlPoint);
870 }
871
872 QList<int> CurveCreator_Widget::getSelectedSections()
873 {
874   return mySectionView->getSelectedSections();
875 }
876
877 QList< QPair< int, int > > CurveCreator_Widget::getSelectedPoints()
878 {
879   return mySectionView->getSelectedPoints();
880 }
881
882 /**
883  * According to the widget state, performs the remove action
884  */
885 void CurveCreator_Widget::removeSelected()
886 {
887   onRemove();
888 }
889
890 /**
891  * Checks whether there are some selection to be removed
892  */
893 bool CurveCreator_Widget::removeEnabled()
894 {
895   bool isEnabled = getActionMode() == ModificationMode;
896   if ( !isEnabled ) {
897     QList<int> aSelSections = mySectionView->getSelectedSections();
898     CurveCreator_TreeView::SelectionType aSelType = mySectionView->getSelectionType();
899     isEnabled = aSelType == CurveCreator_TreeView::ST_SECTIONS &&
900                 aSelSections.size() == 1;
901   }
902   return isEnabled;
903 }
904
905
906 //=================================================================================
907 // function : GeometryGUI::onGetCoordsByClick()
908 // purpose  : Manage mouse press events in Additon mode
909 //=================================================================================
910 void CurveCreator_Widget::onGetCoordsByClick( QMouseEvent* pe )
911 {
912   if (pe->button() != Qt::LeftButton)
913     return;
914
915   if ( pe->modifiers() != Qt::ControlModifier ) {
916     Handle(AIS_InteractiveContext) ic = getAISContext();
917     if ( ic.IsNull() )
918       return;
919
920     gp_Pnt aPnt;    
921
922     ic->InitSelected();
923     if ( pe->modifiers() == Qt::ShiftModifier )
924       ic->ShiftSelect();  // Append selection
925     else
926       ic->Select();       // New selection
927
928     /*TopoDS_Shape aShape;
929
930     ic->InitSelected();
931     if ( ic->MoreSelected() )
932       aShape = ic->SelectedShape();
933
934     if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
935       aPnt = BRep_Tool::Pnt( TopoDS::Vertex( ic->SelectedShape() ) );
936     else*/
937     {
938       OCCViewer_ViewPort3d* vp = getViewPort();
939       aPnt = CurveCreator_Utils::ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
940     }
941     // set the coordinates into dialog
942     CurveCreator::Coordinates aCoords;
943     aCoords.push_back( aPnt.X() );
944     aCoords.push_back( aPnt.Y() );
945     if ( myCurve->getDimension() == 3 ) {
946       aCoords.push_back( aPnt.Z() );
947     }
948     onAddNewPoint(aCoords);
949 //    myNewPointEditor->setCoordinates( aCoords );
950   }
951 }
952
953 /**
954  * Manage mouse press events
955  * \param theWindow an owner of the signal
956  * \param theEvent a mouse event
957  */
958 void CurveCreator_Widget::onMousePress( SUIT_ViewWindow*, QMouseEvent* theEvent )
959 {
960   if ( theEvent->button() != Qt::LeftButton )
961     return;
962
963   switch( getActionMode() ) {
964     case ModificationMode: {
965       //store initial cursor position for Drag&Drop
966       setDragStarted( true, theEvent->pos() );
967       break;
968     }
969     case AdditionMode: {
970       onGetCoordsByClick( theEvent );
971       break;
972     }
973     default:
974       break;
975   }
976 }
977
978 /**
979  * Manage mouse release events in Modification mode
980  * \param theWindow an owner of the signal
981  * \param theEvent a mouse event
982  */
983 void CurveCreator_Widget::onMouseRelease( SUIT_ViewWindow*, QMouseEvent* theEvent )
984 {
985   if ( getActionMode() != ModificationMode )
986     return;
987
988   if ( myDragStarted ) {
989     bool isDragged = myDragged;
990     CurveCreator_ICurve::SectionToPointList aDraggedPoints;
991     if ( myDragged )
992       aDraggedPoints = myDragPoints;
993
994     setDragStarted( false );
995     // if the drag of some points has happened, restore the drag selection
996     if ( aDraggedPoints.size() > 0 )
997     {
998       START_MEASURE_TIME;
999       setSelectedPoints( aDraggedPoints );
1000       END_MEASURE_TIME( "drop" );
1001     }
1002   }
1003   else // check whether the segment is clicked an a new point should be added to the segment
1004     insertPointToSelectedSegment( theEvent->pos().x(), theEvent->pos().y() );
1005
1006   // updates the input panel table to show the selected point coordinates
1007   updateLocalPointView();
1008 }
1009
1010 /**
1011  * Manage mouse move events in Modification mode
1012  * \param theWindow an owner of the signal
1013  * \param theEvent a mouse event
1014  */
1015 void CurveCreator_Widget::onMouseMove( SUIT_ViewWindow*, QMouseEvent* theEvent )
1016 {
1017   if ( getActionMode() != ModificationMode || !myDragStarted )
1018     return;
1019
1020   QPoint aPos = theEvent->pos();
1021   if ( (aPos - myDragStartPosition).manhattanLength() < QApplication::startDragDistance() )
1022     return;
1023
1024   START_MEASURE_TIME;
1025
1026   moveSelectedPoints( aPos.x(), aPos.y() );
1027   myDragStartPosition = aPos;
1028
1029   END_MEASURE_TIME( "drag" );
1030 }
1031
1032 /**
1033  * Set zero viewer by the last view closed in
1034  * \param theManager a viewer manager
1035  */
1036 void CurveCreator_Widget::onLastViewClosed( SUIT_ViewManager* theManager )
1037 {
1038   myOCCViewer = 0;
1039 }
1040
1041 void CurveCreator_Widget::onMousePress( QMouseEvent* theEvent )
1042 {
1043   onMousePress( 0, theEvent );
1044 }
1045
1046 void CurveCreator_Widget::onMouseRelease( QMouseEvent* theEvent )
1047 {
1048   onMouseRelease( 0, theEvent );
1049 }
1050
1051 void CurveCreator_Widget::onMouseMove( QMouseEvent* theEvent )
1052 {
1053   onMouseMove( 0, theEvent );
1054 }
1055
1056 void CurveCreator_Widget::onCellChanged( int theRow, int theColumn )
1057 {
1058   int aCurrSect = myLocalPointView->getSectionId( theRow );
1059   int aPntIndex = myLocalPointView->getPointId( theRow );
1060
1061   if ( aPntIndex < 0 )
1062     return;
1063
1064   CurveCreator_ICurve::SectionToPointList aSelPoints;
1065   startCurveModification( aSelPoints );
1066
1067   double aX  = myLocalPointView->item( theRow, 2 )->data( Qt::UserRole ).toDouble();
1068   double anY = myLocalPointView->item( theRow, 3 )->data( Qt::UserRole ).toDouble();
1069   std::deque<float> aChangedPos;
1070   aChangedPos.push_back( aX );
1071   aChangedPos.push_back( anY );
1072   myCurve->setPoint( aCurrSect, aPntIndex, aChangedPos );
1073
1074   finishCurveModification( aSelPoints );
1075 }
1076
1077 /**
1078  * Removes a selected section from the curve. Updates undo/redo status
1079  */
1080 void CurveCreator_Widget::removeSection()
1081 {
1082   QList< QPair<int,int> > aSelPoints = mySectionView->getSelectedPoints();
1083   int aCurrSect=-1;
1084   int aRemoveCnt = 0;
1085 //  myCurve->startOperation();
1086   for( int i = 0 ; i < aSelPoints.size() ; i++ ){
1087     if( aCurrSect != aSelPoints[i].first ){
1088       aRemoveCnt = 0;
1089       aCurrSect = aSelPoints[i].first;
1090     }
1091     int aPntIndx = aSelPoints[i].second - aRemoveCnt;
1092     myCurve->removePoint( aCurrSect, aPntIndx );
1093     mySectionView->pointsRemoved( aCurrSect, aPntIndx );
1094     aRemoveCnt++;
1095   }
1096   QList<int> aSections = mySectionView->getSelectedSections();
1097   for( int i = 0 ; i < aSections.size() ; i++ ){
1098     int aSectNum = aSections[i] - (i);
1099     myCurve->removeSection( aSectNum );
1100     mySectionView->sectionsRemoved( aSectNum );
1101   }
1102 //  myCurve->finishOperation();
1103   mySectionView->clearSelection();
1104   updateUndoRedo();
1105 }
1106
1107 /**
1108  * Removes a selected points from the curve. Updates undo/redo status
1109  */
1110 void CurveCreator_Widget::removePoint()
1111 {
1112   CurveCreator_ICurve::SectionToPointList aPoints;
1113   getSelectedPoints( aPoints );
1114   if ( aPoints.size() == 0 )
1115     return;
1116
1117   CurveCreator_ICurve::SectionToPointList aSelPoints;
1118   startCurveModification( aSelPoints, false );
1119
1120   myCurve->removeSeveralPoints( aPoints );
1121   finishCurveModification( CurveCreator_ICurve::SectionToPointList() );
1122 }
1123
1124 void CurveCreator_Widget::insertPointToSelectedSegment( const int theX,
1125                                                         const int theY )
1126 {
1127   Handle(AIS_InteractiveContext) aContext = getAISContext();
1128
1129   OCCViewer_ViewPort3d* aViewPort = getViewPort();
1130   Handle(V3d_View) aView;
1131   if ( aViewPort )
1132     aView = aViewPort->getView();
1133
1134   if ( aContext.IsNull() || aView.IsNull() )
1135     return;
1136   gp_Pnt aPoint;
1137   gp_Pnt aPoint1, aPoint2;
1138   //bool isFoundPoint = CurveCreator_Utils::getNeighbourPoints( aContext, aView, theX, theY,
1139   //                                                            aPoint, aPoint1, aPoint2 );
1140   Handle(AIS_InteractiveObject) anAISObject = myCurve->getAISObject();
1141   bool isFoundPoint = CurveCreator_Utils::pointOnObject( aView, anAISObject, theX, theY,
1142                                                          aPoint, aPoint1, aPoint2 );
1143   if ( !isFoundPoint )
1144     return;
1145
1146   // insert the point to the model curve
1147   CurveCreator_ICurve::SectionToPointList aSelPoints;
1148   startCurveModification( aSelPoints );
1149
1150   CurveCreator::Coordinates aCoords;
1151   aCoords.push_back( aPoint.X() );
1152   aCoords.push_back( aPoint.Y() );
1153
1154   CurveCreator_ICurve::SectionToPointList aPoints1, aPoints2;
1155   findSectionsToPoints( aPoint1.X(), aPoint1.Y(), aPoints1 );
1156   findSectionsToPoints( aPoint2.X(), aPoint2.Y(), aPoints2 );
1157   CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aPoints1.begin(),
1158                                                           aLast = aPoints1.end();
1159   int aSectionId = -1;
1160   // there can be a case when a new point is added into two sections
1161   int aPoint1Id = -1, aPoint2Id = -1;
1162   for ( ; anIt != aLast && aSectionId < 0; anIt++ ) {
1163     int aSectionCur = anIt->first;
1164     CurveCreator_ICurve::SectionToPointList::const_iterator anIt2 = aPoints2.begin(),
1165                                                             aLast2 = aPoints2.end();
1166     for ( ; anIt2 != aLast2 && aSectionId < 0; anIt2++ ) {
1167       if ( anIt2->first == aSectionCur ) {
1168         aSectionId = aSectionCur;
1169         aPoint1Id = anIt->second;
1170         aPoint2Id = anIt2->second;
1171       }
1172     }
1173   }
1174
1175   int anInsertPos = -1;
1176   int aLastPoint = myCurve->getNbPoints( aSectionId )-1; 
1177   if ( ( aPoint1Id == aLastPoint && aPoint2Id == 0 ) ||
1178        ( aPoint2Id == aLastPoint && aPoint1Id == 0 ) )
1179     anInsertPos = -1; // if the section happens between first and last points
1180   else
1181     anInsertPos = aPoint1Id < aPoint2Id ? aPoint1Id + 1 : aPoint2Id + 1;
1182
1183   myCurve->addPoints( aCoords, aSectionId, anInsertPos );
1184   mySectionView->pointsAdded( aSectionId, myCurve->getNbPoints( aSectionId ) );
1185
1186   finishCurveModification( aSelPoints );
1187
1188   setSelectedPoints();
1189 }
1190
1191 void CurveCreator_Widget::moveSelectedPoints( const int theXPosition,
1192                                               const int theYPosition )
1193 {
1194   OCCViewer_ViewPort3d* aViewPort = getViewPort();
1195   if ( !aViewPort )
1196     return;
1197
1198   CurveCreator_ICurve::SectionToPointList aPoints;
1199   startCurveModification( aPoints, false );
1200
1201   gp_Pnt aStartPnt = CurveCreator_Utils::ConvertClickToPoint( myDragStartPosition.x(),
1202                                                               myDragStartPosition.y(),
1203                                                               aViewPort->getView() );
1204   gp_Pnt anEndPnt = CurveCreator_Utils::ConvertClickToPoint( theXPosition, theYPosition,
1205                                                              aViewPort->getView() );
1206   double aXDelta = aStartPnt.X() - anEndPnt.X();
1207   double anYDelta = aStartPnt.Y() - anEndPnt.Y();
1208
1209   CurveCreator_ICurve::SectionToPointCoordsList aCoordList;
1210   std::deque<float> aChangedPos;
1211   CurveCreator_ICurve::SectionToPointList::const_iterator anIt = myDragPoints.begin(),
1212                                                           aLast = myDragPoints.end();
1213   for ( ; anIt != aLast; anIt++ ) {
1214     int aSectionId = anIt->first;
1215     int aPointId = anIt->second;
1216     aChangedPos = myCurve->getPoint( aSectionId, aPointId );
1217     if ( aChangedPos.size() < 2 )
1218       continue;
1219     aChangedPos[0] = aChangedPos[0] - aXDelta;
1220     aChangedPos[1] = aChangedPos[1] - anYDelta;
1221     
1222     aCoordList.push_back(
1223       std::make_pair(std::make_pair( aSectionId, aPointId ), 
1224                      aChangedPos ));
1225   }
1226   myCurve->setSeveralPoints( aCoordList );
1227
1228   myDragged = true;
1229   finishCurveModification( myDragPoints );
1230 }
1231
1232 void CurveCreator_Widget::updateLocalPointView()
1233 {
1234   if ( myDragStarted )
1235     return;
1236   Handle(AIS_InteractiveContext) aContext = getAISContext();
1237   if ( aContext.IsNull() )
1238     return;
1239
1240   CurveCreator_Utils::getSelectedPoints( aContext, myCurve, myLocalPoints );
1241   int aNbPoints = myLocalPoints.size();
1242   bool isRowLimit = aNbPoints > myLocalPointRowLimit;
1243   myLocalPointView->setVisible( !isRowLimit );
1244
1245   if ( !isRowLimit ) {
1246     bool isBlocked = myLocalPointView->blockSignals(true);
1247     myLocalPointView->setRowCount( 0 );
1248     CurveCreator_ICurve::SectionToPointList::const_iterator anIt = myLocalPoints.begin(),
1249                                                             aLast = myLocalPoints.end();
1250     for ( ; anIt != aLast; anIt++ )
1251       myLocalPointView->addLocalPointToTable( *anIt );
1252
1253     myLocalPointView->blockSignals( isBlocked );
1254   }
1255 }
1256
1257 /**
1258  * 
1259  */
1260 void CurveCreator_Widget::setLocalPointContext( const bool theOpen, const bool isUpdateTable )
1261 {
1262   CurveCreator_Utils::setLocalPointContext( getAISContext(), theOpen );
1263   if ( !theOpen && isUpdateTable )
1264     updateLocalPointView();
1265 }
1266
1267 /**
1268  * Set drag operation started. Save the position and a list of dragged points
1269  * \param theState the drag operation state: started/finished
1270  * \param thePoint the start drag position
1271  */
1272 void CurveCreator_Widget::setDragStarted( const bool theState, const QPoint& thePoint )
1273 {
1274   if ( theState ) {
1275     getSelectedPoints( myDragPoints );
1276     myDragStarted = myDragPoints.size();
1277     myDragStartPosition = thePoint;
1278     if ( myDragStarted ) {
1279       // change a viewer interaction style in order to avoid a select rectangle build
1280       myDragInteractionStyle = changeInteractionStyle( SUIT_ViewModel::KEY_FREE );
1281     }
1282   }
1283   else {
1284     if ( myDragStarted )
1285       changeInteractionStyle( myDragInteractionStyle );
1286     myDragStarted = false;
1287     myDragPoints.clear();
1288   }
1289   myDragged = false;
1290 }
1291
1292 void CurveCreator_Widget::getSelectedPoints( CurveCreator_ICurve::SectionToPointList& thePoints )
1293 {
1294   thePoints.clear();
1295   thePoints = myLocalPoints;
1296 }
1297
1298 void CurveCreator_Widget::setSelectedPoints( const CurveCreator_ICurve::SectionToPointList& thePoints )
1299 {
1300   if ( myDragStarted )
1301     return;
1302   Handle(AIS_InteractiveContext) aContext = getAISContext();
1303   if ( aContext.IsNull() || !aContext->HasOpenedContext() )
1304     return;
1305
1306   CurveCreator_Utils::setSelectedPoints( aContext, myCurve, thePoints );
1307
1308   updateLocalPointView();
1309 }
1310
1311 /**
1312  * Get viewer information before perform the curve modification.
1313  * Take a list of selected cuve points an close local context.
1314  * The context should be closed because the curve presentation is
1315  * redisplayed and if it is not closed, when we close the local context
1316  * later, the presentation shown in the local context is disappeared.
1317  * \param thePoints an output list of curve selected points
1318  * \param theFillPoints a flag whether the selection list should be filled
1319  */
1320 void CurveCreator_Widget::startCurveModification(
1321                            CurveCreator_ICurve::SectionToPointList& thePoints,
1322                            const bool theFillPoints )
1323 {
1324   if ( theFillPoints ) {
1325     thePoints.clear();
1326     getSelectedPoints( thePoints );
1327   }
1328   setLocalPointContext( false );
1329 }
1330
1331 /**
1332  * Restore the viewer state after the curve modification is done.
1333  * Open local context and select given points inside it.
1334  * \param thePoints a list of curve selected points
1335  */
1336 void CurveCreator_Widget::finishCurveModification(
1337                            const CurveCreator_ICurve::SectionToPointList& thePoints )
1338 {
1339   if ( getActionMode() == ModificationMode )
1340     setLocalPointContext( true );
1341   setSelectedPoints( thePoints );
1342   updateUndoRedo();
1343 }
1344
1345 /**
1346  * Returns a point index in the model curve by the point coordinates in the viewer
1347  * \param theX the X coordinate of the point
1348  * \param theY the Y coordinate of the point
1349  */
1350 int CurveCreator_Widget::findLocalPointIndex( int theSectionId, float theX, float theY )
1351 {
1352   return CurveCreator_UtilsICurve::findLocalPointIndex( myCurve, theSectionId, theX, theY );
1353 }
1354
1355 void CurveCreator_Widget::findSectionsToPoints( const double theX, const double theY,
1356                                  CurveCreator_ICurve::SectionToPointList& thePoints )
1357 {
1358   return CurveCreator_UtilsICurve::findSectionsToPoints( myCurve, theX, theY, thePoints );
1359 }
1360
1361 void CurveCreator_Widget::convert( const CurveCreator_ICurve::SectionToPointList& thePoints,
1362                                    QMap<int, QList<int> >& theConvPoints )
1363 {
1364   return CurveCreator_UtilsICurve::convert( thePoints, theConvPoints );
1365 }
1366
1367 /**
1368  * Returns whethe the container has the value
1369  * \param theList a container of values
1370  * \param theValue a value
1371  */
1372 bool CurveCreator_Widget::contains( const CurveCreator_ICurve::SectionToPointList& theList,
1373                                     const CurveCreator_ICurve::SectionToPoint& theValue ) const
1374 {
1375   return CurveCreator_UtilsICurve::contains( theList, theValue );
1376 }