Salome HOME
5883262f41a3640b332d8c19eb2ab8912f6abcfc
[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
979     if ( aDraggedPoints.size() > 0 ) {
980       if ( myCurve->canPointsBeSorted() ) {
981         // Apply points sorting
982         CurveCreator_ICurve::SectionToPointList aPoints;
983         startCurveModification( aPoints, false );
984
985         myCurve->setSkipSorting( false );
986
987         CurveCreator_ICurve::SectionToPointCoordsList aCoordList;
988         CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aDraggedPoints.begin(),
989                                                                 aLast = aDraggedPoints.end();
990         for ( ; anIt != aLast; anIt++ ) {
991           int aSectionId = anIt->first;
992           int aPointId = anIt->second;
993           std::deque<float> aPos = myCurve->getPoint( aSectionId, aPointId );
994     
995           aCoordList.push_back(
996             std::make_pair( std::make_pair( aSectionId, aPointId ), aPos ) );
997         }
998
999         myCurve->setSeveralPoints( aCoordList );
1000     
1001         finishCurveModification( aDraggedPoints );
1002       } else {
1003         // if the drag of some points has happened, restore the drag selection
1004         if ( aDraggedPoints.size() > 0 ) {
1005           START_MEASURE_TIME;
1006           setSelectedPoints( aDraggedPoints );
1007           END_MEASURE_TIME( "drop" );
1008         }
1009       }
1010     }
1011   }
1012   else // check whether the segment is clicked an a new point should be added to the segment
1013     insertPointToSelectedSegment( theEvent->pos().x(), theEvent->pos().y() );
1014
1015   // updates the input panel table to show the selected point coordinates
1016   updateLocalPointView();
1017 }
1018
1019 /**
1020  * Manage mouse move events in Modification mode
1021  * \param theWindow an owner of the signal
1022  * \param theEvent a mouse event
1023  */
1024 void CurveCreator_Widget::onMouseMove( SUIT_ViewWindow*, QMouseEvent* theEvent )
1025 {
1026   if ( getActionMode() != ModificationMode || !myDragStarted )
1027     return;
1028
1029   QPoint aPos = theEvent->pos();
1030   if ( (aPos - myDragStartPosition).manhattanLength() < QApplication::startDragDistance() )
1031     return;
1032
1033   START_MEASURE_TIME;
1034
1035   moveSelectedPoints( aPos.x(), aPos.y() );
1036   myDragStartPosition = aPos;
1037
1038   END_MEASURE_TIME( "drag" );
1039 }
1040
1041 /**
1042  * Set zero viewer by the last view closed in
1043  * \param theManager a viewer manager
1044  */
1045 void CurveCreator_Widget::onLastViewClosed( SUIT_ViewManager* theManager )
1046 {
1047   myOCCViewer = 0;
1048 }
1049
1050 void CurveCreator_Widget::onMousePress( QMouseEvent* theEvent )
1051 {
1052   onMousePress( 0, theEvent );
1053 }
1054
1055 void CurveCreator_Widget::onMouseRelease( QMouseEvent* theEvent )
1056 {
1057   onMouseRelease( 0, theEvent );
1058 }
1059
1060 void CurveCreator_Widget::onMouseMove( QMouseEvent* theEvent )
1061 {
1062   onMouseMove( 0, theEvent );
1063 }
1064
1065 void CurveCreator_Widget::onCellChanged( int theRow, int theColumn )
1066 {
1067   int aCurrSect = myLocalPointView->getSectionId( theRow );
1068   int aPntIndex = myLocalPointView->getPointId( theRow );
1069
1070   if ( aPntIndex < 0 )
1071     return;
1072
1073   CurveCreator_ICurve::SectionToPointList aSelPoints;
1074   startCurveModification( aSelPoints );
1075
1076   double aX  = myLocalPointView->item( theRow, 2 )->data( Qt::UserRole ).toDouble();
1077   double anY = myLocalPointView->item( theRow, 3 )->data( Qt::UserRole ).toDouble();
1078   std::deque<float> aChangedPos;
1079   aChangedPos.push_back( aX );
1080   aChangedPos.push_back( anY );
1081   myCurve->setPoint( aCurrSect, aPntIndex, aChangedPos );
1082
1083   finishCurveModification( aSelPoints );
1084 }
1085
1086 /**
1087  * Removes a selected section from the curve. Updates undo/redo status
1088  */
1089 void CurveCreator_Widget::removeSection()
1090 {
1091   stopActionMode();
1092
1093   QList< QPair<int,int> > aSelPoints = mySectionView->getSelectedPoints();
1094   int aCurrSect=-1;
1095   int aRemoveCnt = 0;
1096   for( int i = 0 ; i < aSelPoints.size() ; i++ ){
1097     if( aCurrSect != aSelPoints[i].first ){
1098       aRemoveCnt = 0;
1099       aCurrSect = aSelPoints[i].first;
1100     }
1101     int aPntIndx = aSelPoints[i].second - aRemoveCnt;
1102     myCurve->removePoint( aCurrSect, aPntIndx );
1103     mySectionView->pointsRemoved( aCurrSect, aPntIndx );
1104     aRemoveCnt++;
1105   }
1106   QList<int> aSections = mySectionView->getSelectedSections();
1107   for( int i = 0 ; i < aSections.size() ; i++ ){
1108     int aSectNum = aSections[i] - (i);
1109     myCurve->removeSection( aSectNum );
1110     mySectionView->sectionsRemoved( aSectNum );
1111   }
1112   mySectionView->clearSelection();
1113   updateUndoRedo();
1114 }
1115
1116 /**
1117  * Removes a selected points from the curve. Updates undo/redo status
1118  */
1119 void CurveCreator_Widget::removePoint()
1120 {
1121   CurveCreator_ICurve::SectionToPointList aPoints;
1122   getSelectedPoints( aPoints );
1123   if ( aPoints.size() == 0 )
1124     return;
1125
1126   CurveCreator_ICurve::SectionToPointList aSelPoints;
1127   startCurveModification( aSelPoints, false );
1128
1129   myCurve->removeSeveralPoints( aPoints );
1130   finishCurveModification( CurveCreator_ICurve::SectionToPointList() );
1131 }
1132
1133 void CurveCreator_Widget::addNewPoint(const CurveCreator::Coordinates& theCoords)
1134 {
1135   if( !myCurve )
1136     return;
1137   QList<int> aSections = mySectionView->getSelectedSections();
1138   if( aSections.size() == 0 ){
1139     return;
1140   }
1141   int aSection = aSections[0];
1142   myCurve->addPoints(theCoords, aSection); // add to the end of section
1143   mySectionView->pointsAdded( aSection, myCurve->getNbPoints( aSection ) );
1144   updateActionsStates();
1145   updateUndoRedo();
1146 }
1147
1148 void CurveCreator_Widget::insertPointToSelectedSegment( const int theX,
1149                                                         const int theY )
1150 {
1151   Handle(AIS_InteractiveContext) aContext = getAISContext();
1152
1153   OCCViewer_ViewPort3d* aViewPort = getViewPort();
1154   Handle(V3d_View) aView;
1155   if ( aViewPort )
1156     aView = aViewPort->getView();
1157
1158   if ( aContext.IsNull() || aView.IsNull() )
1159     return;
1160   gp_Pnt aPoint;
1161   gp_Pnt aPoint1, aPoint2;
1162   Handle(AIS_InteractiveObject) anAISObject = myCurve->getAISObject();
1163   bool isFoundPoint = CurveCreator_Utils::pointOnObject( aView, anAISObject, theX, theY,
1164                                                          aPoint, aPoint1, aPoint2 );
1165   if ( !isFoundPoint )
1166     return;
1167
1168   // insert the point to the model curve
1169   CurveCreator_ICurve::SectionToPointList aSelPoints;
1170   startCurveModification( aSelPoints );
1171
1172   CurveCreator::Coordinates aCoords;
1173   aCoords.push_back( aPoint.X() );
1174   aCoords.push_back( aPoint.Y() );
1175
1176   CurveCreator_ICurve::SectionToPointList aPoints1, aPoints2;
1177   findSectionsToPoints( aPoint1.X(), aPoint1.Y(), aPoints1 );
1178   findSectionsToPoints( aPoint2.X(), aPoint2.Y(), aPoints2 );
1179   CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aPoints1.begin(),
1180                                                           aLast = aPoints1.end();
1181   int aSectionId = -1;
1182   // there can be a case when a new point is added into two sections
1183   int aPoint1Id = -1, aPoint2Id = -1;
1184   for ( ; anIt != aLast && aSectionId < 0; anIt++ ) {
1185     int aSectionCur = anIt->first;
1186     CurveCreator_ICurve::SectionToPointList::const_iterator anIt2 = aPoints2.begin(),
1187                                                             aLast2 = aPoints2.end();
1188     for ( ; anIt2 != aLast2 && aSectionId < 0; anIt2++ ) {
1189       if ( anIt2->first == aSectionCur ) {
1190         aSectionId = aSectionCur;
1191         aPoint1Id = anIt->second;
1192         aPoint2Id = anIt2->second;
1193       }
1194     }
1195   }
1196
1197   int anInsertPos = -1;
1198   int aLastPoint = myCurve->getNbPoints( aSectionId )-1; 
1199   if ( ( aPoint1Id == aLastPoint && aPoint2Id == 0 ) ||
1200        ( aPoint2Id == aLastPoint && aPoint1Id == 0 ) )
1201     anInsertPos = -1; // if the section happens between first and last points
1202   else
1203     anInsertPos = aPoint1Id < aPoint2Id ? aPoint1Id + 1 : aPoint2Id + 1;
1204
1205   myCurve->addPoints( aCoords, aSectionId, anInsertPos );
1206   mySectionView->pointsAdded( aSectionId, myCurve->getNbPoints( aSectionId ) );
1207
1208   finishCurveModification( aSelPoints );
1209
1210   setSelectedPoints();
1211 }
1212
1213 void CurveCreator_Widget::moveSelectedPoints( const int theXPosition,
1214                                               const int theYPosition )
1215 {
1216   OCCViewer_ViewPort3d* aViewPort = getViewPort();
1217   if ( !aViewPort )
1218     return;
1219
1220   CurveCreator_ICurve::SectionToPointList aPoints;
1221   startCurveModification( aPoints, false );
1222
1223   gp_Pnt aStartPnt = CurveCreator_Utils::ConvertClickToPoint( myDragStartPosition.x(),
1224                                                               myDragStartPosition.y(),
1225                                                               aViewPort->getView() );
1226   gp_Pnt anEndPnt = CurveCreator_Utils::ConvertClickToPoint( theXPosition, theYPosition,
1227                                                              aViewPort->getView() );
1228   double aXDelta = aStartPnt.X() - anEndPnt.X();
1229   double anYDelta = aStartPnt.Y() - anEndPnt.Y();
1230
1231   CurveCreator_ICurve::SectionToPointCoordsList aCoordList;
1232   std::deque<float> aChangedPos;
1233   CurveCreator_ICurve::SectionToPointList::const_iterator anIt = myDragPoints.begin(),
1234                                                           aLast = myDragPoints.end();
1235   for ( ; anIt != aLast; anIt++ ) {
1236     int aSectionId = anIt->first;
1237     int aPointId = anIt->second;
1238     aChangedPos = myCurve->getPoint( aSectionId, aPointId );
1239     if ( aChangedPos.size() < 2 )
1240       continue;
1241     aChangedPos[0] = aChangedPos[0] - aXDelta;
1242     aChangedPos[1] = aChangedPos[1] - anYDelta;
1243     
1244     aCoordList.push_back(
1245       std::make_pair(std::make_pair( aSectionId, aPointId ), 
1246                      aChangedPos ));
1247   }
1248   myCurve->setSeveralPoints( aCoordList );
1249
1250   myDragged = true;
1251   finishCurveModification( myDragPoints );
1252 }
1253
1254 void CurveCreator_Widget::updateLocalPointView()
1255 {
1256   if ( myDragStarted )
1257     return;
1258   Handle(AIS_InteractiveContext) aContext = getAISContext();
1259   if ( aContext.IsNull() )
1260     return;
1261
1262   CurveCreator_Utils::getSelectedPoints( aContext, myCurve, myLocalPoints );
1263   int aNbPoints = myLocalPoints.size();
1264
1265   bool isRowLimit = aNbPoints > myLocalPointRowLimit;
1266   myLocalPointView->setVisible( getActionMode() == ModificationMode && !isRowLimit );
1267
1268   if ( !isRowLimit ) {
1269     bool isBlocked = myLocalPointView->blockSignals(true);
1270     myLocalPointView->setRowCount( 0 );
1271     CurveCreator_ICurve::SectionToPointList::const_iterator anIt = myLocalPoints.begin(),
1272                                                             aLast = myLocalPoints.end();
1273     for ( ; anIt != aLast; anIt++ )
1274       myLocalPointView->addLocalPointToTable( *anIt );
1275
1276     myLocalPointView->blockSignals( isBlocked );
1277   }
1278 }
1279
1280 /**
1281  * 
1282  */
1283 void CurveCreator_Widget::setLocalPointContext( const bool theOpen, const bool isUpdateTable )
1284 {
1285   CurveCreator_Utils::setLocalPointContext( myCurve, getAISContext(), theOpen );
1286   if ( !theOpen && isUpdateTable )
1287     updateLocalPointView();
1288 }
1289
1290 /**
1291  * Set drag operation started. Save the position and a list of dragged points
1292  * \param theState the drag operation state: started/finished
1293  * \param thePoint the start drag position
1294  */
1295 void CurveCreator_Widget::setDragStarted( const bool theState, const QPoint& thePoint )
1296 {
1297   if ( theState ) {
1298     getSelectedPoints( myDragPoints );
1299
1300     myDragStarted = myDragPoints.size();
1301     myDragStartPosition = thePoint;
1302     if ( myDragStarted ) {
1303       // change a viewer interaction style in order to avoid a select rectangle build
1304       myDragInteractionStyle = changeInteractionStyle( SUIT_ViewModel::KEY_FREE );
1305       myCurve->setSkipSorting( true );
1306     }
1307   }
1308   else {
1309     if ( myDragStarted )
1310       changeInteractionStyle( myDragInteractionStyle );
1311     myDragStarted = false;
1312     myDragPoints.clear();
1313   }
1314   myDragged = false;
1315 }
1316
1317 void CurveCreator_Widget::getSelectedPoints( CurveCreator_ICurve::SectionToPointList& thePoints )
1318 {
1319   thePoints.clear();
1320   thePoints = myLocalPoints;
1321 }
1322
1323 void CurveCreator_Widget::setSelectedPoints( const CurveCreator_ICurve::SectionToPointList& thePoints )
1324 {
1325   if ( myDragStarted )
1326     return;
1327   Handle(AIS_InteractiveContext) aContext = getAISContext();
1328   if ( aContext.IsNull() || !aContext->HasOpenedContext() )
1329     return;
1330
1331   CurveCreator_Utils::setSelectedPoints( aContext, myCurve, thePoints );
1332
1333   updateLocalPointView();
1334 }
1335
1336 void CurveCreator_Widget::stopActionMode()
1337 {
1338   setActionMode( NoneMode );
1339 }
1340
1341 /**
1342  * Get viewer information before perform the curve modification.
1343  * Take a list of selected cuve points an close local context.
1344  * The context should be closed because the curve presentation is
1345  * redisplayed and if it is not closed, when we close the local context
1346  * later, the presentation shown in the local context is disappeared.
1347  * \param thePoints an output list of curve selected points
1348  * \param theFillPoints a flag whether the selection list should be filled
1349  */
1350 void CurveCreator_Widget::startCurveModification(
1351                            CurveCreator_ICurve::SectionToPointList& thePoints,
1352                            const bool theFillPoints )
1353 {
1354   if ( theFillPoints ) {
1355     thePoints.clear();
1356     getSelectedPoints( thePoints );
1357   }
1358   setLocalPointContext( false );
1359 }
1360
1361 /**
1362  * Restore the viewer state after the curve modification is done.
1363  * Open local context and select given points inside it.
1364  * \param thePoints a list of curve selected points
1365  */
1366 void CurveCreator_Widget::finishCurveModification(
1367                            const CurveCreator_ICurve::SectionToPointList& thePoints )
1368 {
1369   if ( getActionMode() == ModificationMode )
1370     setLocalPointContext( true );
1371   setSelectedPoints( thePoints );
1372   updateUndoRedo();
1373 }
1374
1375 /**
1376  * Returns a point index in the model curve by the point coordinates in the viewer
1377  * \param theX the X coordinate of the point
1378  * \param theY the Y coordinate of the point
1379  */
1380 int CurveCreator_Widget::findLocalPointIndex( int theSectionId, float theX, float theY )
1381 {
1382   return CurveCreator_UtilsICurve::findLocalPointIndex( myCurve, theSectionId, theX, theY );
1383 }
1384
1385 void CurveCreator_Widget::findSectionsToPoints( const double theX, const double theY,
1386                                  CurveCreator_ICurve::SectionToPointList& thePoints )
1387 {
1388   return CurveCreator_UtilsICurve::findSectionsToPoints( myCurve, theX, theY, thePoints );
1389 }
1390
1391 void CurveCreator_Widget::convert( const CurveCreator_ICurve::SectionToPointList& thePoints,
1392                                    QMap<int, QList<int> >& theConvPoints )
1393 {
1394   return CurveCreator_UtilsICurve::convert( thePoints, theConvPoints );
1395 }
1396
1397 /**
1398  * Returns whethe the container has the value
1399  * \param theList a container of values
1400  * \param theValue a value
1401  */
1402 bool CurveCreator_Widget::contains( const CurveCreator_ICurve::SectionToPointList& theList,
1403                                     const CurveCreator_ICurve::SectionToPoint& theValue ) const
1404 {
1405   return CurveCreator_UtilsICurve::contains( theList, theValue );
1406 }