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