Salome HOME
Ref #250 - Fatal error after Join all selections operation
[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   bool isToEnableClosed = !( theActionFlags & DisableClosedSection );
89   myNewSectionEditor = new CurveCreator_NewSectionDlg( this, isToEnableClosed );
90   myNewSectionEditor->hide();
91   connect( myNewSectionEditor, SIGNAL(addSection()), this, SLOT(onAddNewSection()) );
92   connect( myNewSectionEditor, SIGNAL(modifySection()), this, SLOT(onModifySection()) );
93   connect( myNewSectionEditor, SIGNAL(cancelSection()), this, SLOT(onCancelSection()) );
94
95   QGroupBox* aSectionGroup = new QGroupBox(tr("Sections"),this);
96
97   mySectionView = new CurveCreator_TreeView(myCurve, aSectionGroup);
98   mySectionView->setSelectionMode( QTreeView::ExtendedSelection );
99   connect( mySectionView, SIGNAL(selectionChanged()), this, SLOT( onSelectionChanged() ) );
100   connect( mySectionView, SIGNAL(sectionEntered(int)), this, SLOT(onEditSection(int)) );
101   connect( mySectionView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onContextMenu(QPoint)) );
102
103   myLocalPointView = new CurveCreator_TableView( myCurve, this );
104   connect( myLocalPointView, SIGNAL( cellChanged( int, int ) ),
105            this, SLOT( onCellChanged( int, int ) ) );
106
107   QToolBar* aTB = new QToolBar(tr("TOOL_BAR_TLT"), aSectionGroup);
108 //    QToolButton* anUndoBtn = new QToolButton(aTB);
109
110   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
111   QPixmap anUndoPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_UNDO")));
112   QPixmap aRedoPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_REDO")));
113   QPixmap aNewSectionPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_NEW_SECTION")));
114   QPixmap aNewPointPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_NEW_POINT")));
115   QPixmap anEditPointsPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_EDIT_POINTS")));
116   QPixmap aDetectPointsPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_EDIT_POINTS")));
117   QPixmap aPolylinePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_POLYLINE")));
118   QPixmap aSplinePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_SPLINE")));
119   QPixmap aRemovePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_DELETE")));
120   QPixmap aJoinPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_JOIN")));
121   QPixmap aStepUpPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_ARROW_UP")));
122   QPixmap aStepDownPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_ARROW_DOWN")));
123
124   QAction* anAct = createAction( UNDO_ID, tr("UNDO"), anUndoPixmap, tr("UNDO_TLT"), 
125                                  QKeySequence(Qt::ControlModifier|Qt::Key_Z) );
126   connect(anAct, SIGNAL(triggered()), this, SLOT(onUndo()) );
127   aTB->addAction(anAct);
128
129   anAct = createAction( REDO_ID, tr("REDO"), aRedoPixmap, tr("REDO_TLT"), 
130                         QKeySequence(Qt::ControlModifier|Qt::Key_Y) );
131   connect(anAct, SIGNAL(triggered()), this, SLOT(onRedo()) );
132   aTB->addAction(anAct);
133
134   aTB->addSeparator();
135   
136   anAct = createAction( NEW_SECTION_ID, tr("NEW_SECTION"), aNewSectionPixmap, tr("NEW_SECTION_TLT"), 
137                         QKeySequence(Qt::ControlModifier|Qt::Key_N) );
138   connect(anAct, SIGNAL(triggered()), this, SLOT(onNewSection()) );
139   if ( !(theActionFlags & DisableNewSection) ) {
140     aTB->addAction(anAct);
141     aTB->addSeparator();
142   }
143
144   anAct = createAction( ADDITION_MODE_ID, tr("ADDITION_MODE"), aNewPointPixmap, tr("ADDITION_MODE_TLT"), 
145                         QKeySequence() );
146   anAct->setCheckable(true);
147   connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onAdditionMode(bool)) );
148   connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
149   aTB->addAction(anAct);
150   
151   anAct = createAction( MODIFICATION_MODE_ID, tr("MODIFICATION_MODE"), anEditPointsPixmap, tr("MODIFICATION_MODE_TLT"), 
152                         QKeySequence() );
153   anAct->setCheckable(true);
154   connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onModificationMode(bool)) );
155   connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
156   aTB->addAction(anAct);
157
158   anAct = createAction( DETECTION_MODE_ID, tr("DETECTION_MODE"), aDetectPointsPixmap, tr("DETECTION_MODE_TLT"), 
159                         QKeySequence() );
160   anAct->setCheckable(true);
161   connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onDetectionMode(bool)) );
162   connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
163   if ( !(theActionFlags & DisableDetectionMode) ) {
164     aTB->addAction(anAct);
165   }
166   
167   anAct = createAction( CLOSE_SECTIONS_ID, tr("CLOSE_SECTIONS"), QPixmap(), tr("CLOSE_SECTIONS_TLT"), 
168                         QKeySequence(Qt::ControlModifier|Qt::Key_W) );
169   connect(anAct, SIGNAL(triggered()), this, SLOT(onCloseSections()) );
170
171   anAct = createAction( UNCLOSE_SECTIONS_ID, tr("UNCLOSE_SECTIONS"), QPixmap(), 
172                         tr("UNCLOSE_SECTIONS_TLT"), QKeySequence(Qt::ControlModifier|Qt::Key_S) );
173   connect(anAct, SIGNAL(triggered()), this, SLOT(onUncloseSections()) );
174
175   anAct = createAction( SET_SECTIONS_POLYLINE_ID, tr("SET_SECTIONS_POLYLINE"), 
176                         aPolylinePixmap, tr("SET_POLYLINE_TLT"), 
177                         QKeySequence(Qt::ControlModifier|Qt::Key_E) );
178   connect(anAct, SIGNAL(triggered()), this, SLOT(onSetPolyline()) );
179
180   anAct = createAction( SET_SECTIONS_SPLINE_ID, tr("SET_SECTIONS_SPLINE"), aSplinePixmap, 
181                         tr("SET_SPLINE_TLT"), QKeySequence(Qt::ControlModifier|Qt::Key_R) );
182   connect(anAct, SIGNAL(triggered()), this, SLOT(onSetSpline()) );
183
184   anAct = createAction( REMOVE_ID, tr("REMOVE"), aRemovePixmap, tr("REMOVE_TLT"), 
185                         QKeySequence(Qt::ControlModifier|Qt::Key_Delete ) );
186   connect(anAct, SIGNAL(triggered()), this, SLOT(onRemove()) );
187   aTB->addAction(anAct);
188   
189   aTB->addSeparator();
190
191   anAct = createAction( JOIN_ID, tr("JOIN"), aJoinPixmap, tr("JOIN_TLT"), 
192                         QKeySequence(Qt::ControlModifier|Qt::Key_Plus ) );
193   connect( anAct, SIGNAL(triggered()), this, SLOT(onJoin()) );
194   aTB->addAction(anAct);
195
196   anAct = createAction( CLEAR_ALL_ID, tr("CLEAR_ALL"), QPixmap(), tr("CLEAR_ALL_TLT"), 
197                         QKeySequence(Qt::ControlModifier | Qt::ShiftModifier | Qt::Key_Delete ) );
198   connect( anAct, SIGNAL(triggered()), this, SLOT( onClearAll()) );
199
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     CurveCreator_TreeView::SelectionType aSelType = mySectionView->getSelectionType();
347     switch( aSelType ){
348     case CurveCreator_TreeView::ST_NOSEL:{
349       break;
350     }
351     case CurveCreator_TreeView::ST_SECTIONS:{
352       /*if( aSelSections[0] > 0 ){
353         anEnabledAct << UP_ID;
354       }*/
355       if( aSelSections.size() == 1 ){
356         anEnabledAct << ADDITION_MODE_ID << DETECTION_MODE_ID;
357       }
358       switch ( getActionMode() ) {
359         case AdditionMode: {
360           mySection = -1;
361           myPointNum = -1;
362           QList<int> aSelSection = mySectionView->getSelectedSections();
363           if( aSelSection.size() > 0 ){
364             mySection = aSelSection[0];
365             myPointNum = myCurve->getNbPoints(mySection);
366           }
367         }
368         break;
369         case ModificationMode: {
370          anEnabledAct << CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID << SET_SECTIONS_POLYLINE_ID << SET_SECTIONS_SPLINE_ID;
371           int aSectCnt = myCurve->getNbSections();
372           if( aSectCnt > 0 )
373             anEnabledAct << CLEAR_ALL_ID;
374           if( aSectCnt > 1 )
375             anEnabledAct << JOIN_ALL_ID;
376           if( aSelSections.size() > 1 ){
377             anEnabledAct << JOIN_ID;
378           }
379         }
380         break;
381         case DetectionMode: {
382         }
383         break;
384         case NoneMode:
385           {
386             int aSectCnt = myCurve->getNbSections();
387             if( aSectCnt > 1 )
388               anEnabledAct << JOIN_ALL_ID;
389             if( aSelSections.size() > 1 )
390               anEnabledAct << JOIN_ID;
391           }
392           break;
393         default:
394         break;
395       }
396       /*if( aSelSections[ aSelSections.size() - 1 ] < ( myCurve->getNbSections() - 1 ) ){
397         anEnabledAct << DOWN_ID;
398       }*/
399       break;
400     }
401     /*case CurveCreator_TreeView::ST_POINTS_ONE_SECTION:{
402       if( aSelPoints[0].second > 0 ){
403         anEnabledAct << UP_ID;
404       }
405       int aLastIndex = aSelPoints.size()-1;
406       int aSect = aSelPoints[0].first;
407       if( aSelPoints[aLastIndex].second < (myCurve->getNbPoints(aSect) - 1)){
408         anEnabledAct << DOWN_ID;
409       }
410       if( aSelPoints.size() == 1){
411         anEnabledAct << INSERT_POINT_BEFORE_ID << INSERT_POINT_AFTER_ID;
412       }
413       break;
414     }*/
415
416     }
417     
418     /*int aSelObjsCnt = aSelPoints.size() + aSelSections.size();
419     if( aSelObjsCnt > 0 ){
420       anEnabledAct << REMOVE_ID;
421     }
422     if( (myCurve->getNbSections() + myCurve->getNbPoints()) > 0 ){
423       anEnabledAct << REMOVE_ALL_ID;
424     }*/
425     if( myCurve->getNbSections() > 1 ){
426       anEnabledAct << JOIN_ALL_ID;
427     }
428   }
429   QList<ActionId> anIds = myActionMap.keys();
430   for( int i = 0 ; i < anIds.size() ; i++ ){
431     if( myActionMap.contains(anIds[i]) ){
432       if( anEnabledAct.contains(anIds[i]) ){
433         myActionMap[anIds[i]]->setEnabled(true);
434       }
435       else{
436         myActionMap[anIds[i]]->setEnabled(false);
437       }
438     }
439   }
440 }
441
442 void CurveCreator_Widget::onAdditionMode(bool checked)
443 {
444   if (!checked)
445     return;
446
447   Handle(AIS_InteractiveContext) aContext = getAISContext();
448   if( !myCurve || aContext.IsNull() )
449     return;
450
451   mySection= -1;
452   myPointNum = -1;
453   QList<int> aSelSection = mySectionView->getSelectedSections();
454   if( aSelSection.size() > 0 ){
455     mySection = aSelSection[0];
456   }
457 //  emit subOperationStarted( myNewPointEditor );
458 }
459
460 void CurveCreator_Widget::onModificationMode(bool checked)
461 {
462   myLocalPointView->setVisible( checked );
463 }
464
465 void CurveCreator_Widget::onDetectionMode(bool checked)
466 {
467 }
468
469 void CurveCreator_Widget::onModeChanged(bool checked)
470 {
471   ActionMode aMode = NoneMode;
472   if (checked) {
473     QAction* anAction = (QAction*)sender();
474     switch(myActionMap.key(anAction)) {
475       case ADDITION_MODE_ID:
476         aMode = AdditionMode;
477         if (myActionMap[MODIFICATION_MODE_ID]->isChecked())
478           myActionMap[MODIFICATION_MODE_ID]->trigger();
479         else if (myActionMap[DETECTION_MODE_ID]->isChecked())
480           myActionMap[DETECTION_MODE_ID]->trigger();
481         break;
482       case MODIFICATION_MODE_ID:
483         aMode = ModificationMode;
484         if (myActionMap[ADDITION_MODE_ID]->isChecked())
485           myActionMap[ADDITION_MODE_ID]->trigger();
486         else if (myActionMap[DETECTION_MODE_ID]->isChecked())
487           myActionMap[DETECTION_MODE_ID]->trigger();
488         break;
489       case DETECTION_MODE_ID:
490         aMode = DetectionMode;
491         if (myActionMap[ADDITION_MODE_ID]->isChecked())
492           myActionMap[ADDITION_MODE_ID]->trigger();
493         else if (myActionMap[MODIFICATION_MODE_ID]->isChecked())
494           myActionMap[MODIFICATION_MODE_ID]->trigger();
495         break;
496     }
497   }
498   updateActionsStates();
499   updateUndoRedo();
500   setLocalPointContext( aMode == ModificationMode, true );
501 }
502
503 void CurveCreator_Widget::onNewSection()
504 {
505   if( !myCurve )
506     return;
507
508   stopActionMode();
509   myNewSectionEditor->clear();
510   myNewSectionEditor->setEditMode(false);
511   QString aSectName = QString( CurveCreator_UtilsICurve::getUniqSectionName( myCurve ).c_str() );
512   myNewSectionEditor->setSectionParameters(aSectName, true, CurveCreator::Polyline );
513   emit subOperationStarted( myNewSectionEditor, false );
514 }
515
516 void CurveCreator_Widget::onAddNewSection()
517 {
518   if( !myCurve )
519     return;
520   myCurve->addSection( myNewSectionEditor->getName().toStdString(),
521                        myNewSectionEditor->getSectionType(),
522                        myNewSectionEditor->isClosed() );
523   mySectionView->sectionAdded( -1 ); // add a new section to the end of list
524   QString aNewName = QString( CurveCreator_UtilsICurve::getUniqSectionName( myCurve ).c_str() );
525   myNewSectionEditor->setSectionName(aNewName);
526   updateActionsStates();
527   updateUndoRedo();
528   onCancelSection();
529 }
530
531 void CurveCreator_Widget::onCancelSection()
532 {
533   emit subOperationFinished( myNewSectionEditor );
534 }
535
536 QAction* CurveCreator_Widget::createAction( ActionId theId, const QString& theName, const QPixmap& theImage,
537                                             const QString& theToolTip, const QKeySequence& theShortcut )
538 {
539   QAction* anAct = new QAction(theName,this);
540   if( !theImage.isNull() ){
541     anAct->setIcon(theImage);
542   }
543   anAct->setShortcut(theShortcut);
544   anAct->setToolTip(theToolTip);
545   myActionMap[theId] = anAct;
546   return anAct;
547 }
548
549 QAction* CurveCreator_Widget::getAction( ActionId theId )
550 {
551   if( myActionMap.contains(theId) )
552     return myActionMap[theId];
553   return NULL;
554 }
555
556 QAction* CurveCreator_Widget::getAction( ActionMode theMode )
557 {
558   ActionId anActionId = NONE_ID;
559   switch ( theMode ) {
560     case AdditionMode:
561       anActionId = ADDITION_MODE_ID;
562       break;
563     case ModificationMode:
564       anActionId = MODIFICATION_MODE_ID;
565       break;
566     case DetectionMode:
567       anActionId = DETECTION_MODE_ID;
568       break;
569     default:
570       break;
571   }
572   QAction* anAction = 0;
573   if ( anActionId != NONE_ID && myActionMap.contains( anActionId ) )
574     anAction = myActionMap[anActionId];
575   return anAction;
576 }
577
578 void CurveCreator_Widget::setActionMode( const ActionMode& theMode )
579 {
580   ActionMode aPrevMode = getActionMode();
581   QAction* aPrevAction = getAction( aPrevMode );
582   switch ( theMode ) {
583     case NoneMode: {
584       if ( aPrevAction ) {
585         if ( aPrevAction->isChecked() )
586           aPrevAction->setChecked( false );
587       }
588       if ( aPrevMode == ModificationMode )
589         onModificationMode( false );
590       if ( aPrevMode == AdditionMode )
591         onAdditionMode( false );
592     }
593     break;
594     case AdditionMode: {
595       // TODO
596     }
597     break;
598     case ModificationMode:
599     {
600       //TODO
601     }
602     break;
603     case DetectionMode:
604       break;
605   }
606 }
607
608 CurveCreator_Widget::ActionMode CurveCreator_Widget::getActionMode() const
609 {
610   ActionMode aMode = NoneMode;
611
612   if ( myActionMap[ADDITION_MODE_ID]->isChecked() )
613     aMode = AdditionMode;
614   else if ( myActionMap[MODIFICATION_MODE_ID]->isChecked() )
615     aMode = ModificationMode;
616   else if ( myActionMap[DETECTION_MODE_ID]->isChecked() )
617     aMode = DetectionMode;
618
619   return aMode;
620 }
621
622 void CurveCreator_Widget::onEditSection( int theSection )
623 {
624   if( !myCurve )
625     return;
626   
627   stopActionMode();
628   mySection = theSection;
629   QString aSectName = QString::fromStdString( myCurve->getSectionName(theSection));
630   bool isClosed = myCurve->isClosed(theSection);
631   CurveCreator::SectionType aType = myCurve->getSectionType(theSection);
632   myNewSectionEditor->setEditMode(true);
633   myNewSectionEditor->setSectionParameters( aSectName, isClosed, aType );
634
635   emit subOperationStarted( myNewSectionEditor, true );
636 }
637
638 void CurveCreator_Widget::onModifySection()
639 {
640   if( !myCurve )
641     return;
642   QString aName = myNewSectionEditor->getName();
643   bool isClosed = myNewSectionEditor->isClosed();
644   CurveCreator::SectionType aSectType = myNewSectionEditor->getSectionType();
645   if( myCurve->getSectionName(mySection) != aName.toStdString() )
646     myCurve->setSectionName( mySection , aName.toStdString() );
647
648   if( myCurve->getSectionType(mySection) != aSectType )
649     myCurve->setSectionType( mySection, aSectType );
650
651   if( myCurve->isClosed(mySection) != isClosed )
652     myCurve->setClosed( mySection, isClosed );
653   mySectionView->sectionChanged(mySection);
654   updateUndoRedo();
655   onCancelSection();
656 }
657
658 void CurveCreator_Widget::onJoin()
659 {
660   if( !myCurve )
661     return;
662   QList<int> aSections = mySectionView->getSelectedSections();
663   if( aSections.size() == 0 ){
664     return;
665   }
666   stopActionMode();
667
668   std::list<int> aSectionsToJoin;
669   for( int i = 0; i < aSections.size() ; i++ ){
670     aSectionsToJoin.push_back( aSections[i] );
671   }
672
673   if ( myCurve->join( aSectionsToJoin ) )
674   {
675     for( int i = 0, aSectionsSize = aSectionsToJoin.size(); i < aSectionsSize; i++ )
676       mySectionView->sectionsRemoved( i );
677   }
678
679   updateUndoRedo();
680 }
681
682 void CurveCreator_Widget::onRemove()
683 {
684   if( !myCurve )
685     return;
686
687   switch( getActionMode() ) {
688     case NoneMode:
689       removeSection();
690     break;
691     case ModificationMode:
692       removePoint();
693     break;
694     default:
695       break;
696   }
697 }
698
699 void CurveCreator_Widget::onClearAll()
700 {
701   if( !myCurve )
702     return;
703   stopActionMode();
704   myCurve->clear();
705   mySectionView->reset();
706   updateActionsStates();
707   updateUndoRedo();
708 }
709
710 void CurveCreator_Widget::onJoinAll()
711 {
712   if( !myCurve )
713     return;
714   stopActionMode();
715
716   std::list<int> aSectionsToJoin;
717   for( int i = 0, aNb = myCurve->getNbSections(); i < aNb ; i++ ){
718     aSectionsToJoin.push_back( i );
719   }
720   bool aRes = myCurve->join( aSectionsToJoin );
721
722   mySectionView->reset();
723   updateActionsStates();
724   updateUndoRedo();
725 }
726
727 void CurveCreator_Widget::onUndoSettings()
728 {
729
730 }
731
732 void CurveCreator_Widget::onSetSpline()
733 {
734   if( !myCurve )
735     return;
736   stopActionMode();
737   QList<int> aSelSections = mySectionView->getSelectedSections();
738   for( int i = 0 ; i < aSelSections.size() ; i++ ){
739     myCurve->setSectionType(aSelSections[i], CurveCreator::Spline );
740     mySectionView->sectionChanged(aSelSections[i]);
741   }
742   updateUndoRedo();
743 }
744
745 void CurveCreator_Widget::onSetPolyline()
746 {
747   if( !myCurve )
748     return;
749   stopActionMode();
750   QList<int> aSelSections = mySectionView->getSelectedSections();
751   for( int i = 0 ; i < aSelSections.size() ; i++ ){
752     myCurve->setSectionType( aSelSections[i], CurveCreator::Polyline );
753     mySectionView->sectionChanged( aSelSections[i] );
754   }
755   updateUndoRedo();
756 }
757
758 void CurveCreator_Widget::onCloseSections()
759 {
760   if( !myCurve )
761     return;
762   stopActionMode();
763   QList<int> aSelSections = mySectionView->getSelectedSections();
764   for( int i = 0 ; i < aSelSections.size() ; i++ ){
765     myCurve->setClosed(aSelSections[i], true);
766     mySectionView->sectionChanged(aSelSections[i]);
767   }
768   updateUndoRedo();
769 }
770
771 void CurveCreator_Widget::onUncloseSections()
772 {
773   if( !myCurve )
774     return;
775   stopActionMode();
776   QList<int> aSelSections = mySectionView->getSelectedSections();
777   for( int i = 0 ; i < aSelSections.size() ; i++ ){
778     myCurve->setClosed(aSelSections[i], false);
779     mySectionView->sectionChanged(aSelSections[i]);
780   }
781   updateUndoRedo();
782 }
783
784 void CurveCreator_Widget::onUndo()
785 {
786     if( !myCurve )
787       return;
788
789     CurveCreator_ICurve::SectionToPointList aPoints;
790     startCurveModification( aPoints, false );
791     myCurve->undo();
792     finishCurveModification();
793     mySectionView->reset();
794 }
795
796 void CurveCreator_Widget::onRedo()
797 {
798     if( !myCurve )
799       return;
800     CurveCreator_ICurve::SectionToPointList aPoints;
801     startCurveModification( aPoints, false );
802     myCurve->redo();
803     finishCurveModification();
804     mySectionView->reset();
805 }
806
807 void CurveCreator_Widget::updateUndoRedo()
808 {
809   if( !myCurve )
810     return;
811   QAction* anAct = myActionMap[UNDO_ID];
812   if( anAct != 0 ){
813     if( myCurve->getNbUndo() != 0 ){
814       anAct->setEnabled(true);
815     }
816     else{
817       anAct->setDisabled(true);
818     }
819   }
820   anAct = myActionMap[REDO_ID];
821   if( anAct != 0 ){
822     if( myCurve->getNbRedo() != 0 ){
823       anAct->setEnabled(true);
824     }
825     else{
826       anAct->setDisabled(true);
827     }
828   }
829 }
830
831 void CurveCreator_Widget::onContextMenu( QPoint thePoint )
832 {
833   QList<ActionId> aContextActions;
834   aContextActions << CLEAR_ALL_ID << JOIN_ID << JOIN_ALL_ID << SEPARATOR_ID <<
835                      CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID << SET_SECTIONS_POLYLINE_ID <<
836                      SET_SECTIONS_SPLINE_ID;
837   QPoint aGlPoint = mySectionView->mapToGlobal(thePoint);
838   bool isVis = false;
839   QList<ActionId> aResAct;
840   for( int i = 0 ; i < aContextActions.size() ; i++ ){
841     if( aContextActions[i] != SEPARATOR_ID ){
842       if( myActionMap.contains(aContextActions[i]) ){
843         QAction* anAct = myActionMap[aContextActions[i]];
844         if( anAct->isEnabled() ){
845           aResAct << aContextActions[i];
846           isVis = true;
847         }
848       }
849     }
850     else{
851       aResAct << SEPARATOR_ID;
852     }
853   }
854   if( !isVis )
855     return;
856
857   QMenu* aMenu = new QMenu(this);
858   for( int i = 0 ; i < aResAct.size() ; i++ ){
859     if( aResAct[i] == SEPARATOR_ID ){
860       aMenu->addSeparator();
861     }
862     else{
863       QAction* anAct = myActionMap[aResAct[i]];
864       aMenu->insertAction(NULL, anAct);
865     }
866   }
867   aMenu->exec(aGlPoint);
868 }
869
870 QList<int> CurveCreator_Widget::getSelectedSections()
871 {
872   return mySectionView->getSelectedSections();
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     QMap<CurveCreator_ICurve::SectionToPoint, std::deque< float > > anInitialDragPointsCoords;
975     if ( myDragged ) {
976       aDraggedPoints = myDragPoints;
977       anInitialDragPointsCoords = myInitialDragPointsCoords;
978     }
979
980     setDragStarted( false );
981
982     if ( aDraggedPoints.size() > 0 ) {
983       // Collect old coordinates of the dragged points
984       CurveCreator_ICurve::SectionToPointCoordsList anOldPoints;
985       foreach ( const CurveCreator_ICurve::SectionToPoint aSectionToPoint, anInitialDragPointsCoords.keys() ) {
986         CurveCreator::Coordinates aCoords = anInitialDragPointsCoords.value( aSectionToPoint );
987         anOldPoints.push_back( std::make_pair( aSectionToPoint, aCoords ) );
988       }
989
990       if ( myCurve->canPointsBeSorted() ) {
991         // Add old coordinates of the curve points (except the dragged points) to the list
992         for( int aSectionId = 0 ; aSectionId < myCurve->getNbSections() ; aSectionId++ ) {
993           CurveCreator::Coordinates aCoords;
994           for ( int aPointId = 0, aNb = myCurve->getNbPoints( aSectionId ); aPointId < aNb; aPointId++ ) {
995             aCoords = myCurve->getPoint( aSectionId, aPointId );
996             if ( aCoords.size() < 2 ) {
997               continue;
998             }
999             
1000             CurveCreator_ICurve::SectionToPoint aSectionToPoint = std::make_pair( aSectionId, aPointId );
1001
1002             if ( !anInitialDragPointsCoords.contains( aSectionToPoint ) ) {
1003               anOldPoints.push_back( std::make_pair( aSectionToPoint, aCoords ) );
1004             }
1005           }
1006         }
1007         
1008         // Apply points sorting
1009         CurveCreator_ICurve::SectionToPointList aPoints;
1010         startCurveModification( aPoints, false );
1011
1012         myCurve->setSkipSorting( false );
1013
1014         CurveCreator_ICurve::SectionToPointCoordsList aCoordList;
1015         CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aDraggedPoints.begin(),
1016                                                                 aLast = aDraggedPoints.end();
1017         for ( ; anIt != aLast; anIt++ ) {
1018           int aSectionId = anIt->first;
1019           int aPointId = anIt->second;
1020           std::deque<float> aPos = myCurve->getPoint( aSectionId, aPointId );
1021
1022           aCoordList.push_back(
1023             std::make_pair( std::make_pair( aSectionId, aPointId ), aPos ) );
1024         }
1025
1026         myCurve->setSeveralPoints( aCoordList, false );
1027     
1028         finishCurveModification( aDraggedPoints );
1029       } else {
1030         // if the drag of some points has happened, restore the drag selection
1031         START_MEASURE_TIME;
1032         setSelectedPoints( aDraggedPoints );
1033         END_MEASURE_TIME( "drop" );
1034       }
1035
1036       // Save drag difference
1037       myCurve->saveCoordDiff( anOldPoints );
1038     }
1039   }
1040   else // check whether the segment is clicked an a new point should be added to the segment
1041     insertPointToSelectedSegment( theEvent->pos().x(), theEvent->pos().y() );
1042
1043   // updates the input panel table to show the selected point coordinates
1044   updateLocalPointView();
1045 }
1046
1047 /**
1048  * Manage mouse move events in Modification mode
1049  * \param theWindow an owner of the signal
1050  * \param theEvent a mouse event
1051  */
1052 void CurveCreator_Widget::onMouseMove( SUIT_ViewWindow*, QMouseEvent* theEvent )
1053 {
1054   if ( getActionMode() != ModificationMode || !myDragStarted )
1055     return;
1056
1057   QPoint aPos = theEvent->pos();
1058   if ( (aPos - myDragStartPosition).manhattanLength() < QApplication::startDragDistance() )
1059     return;
1060
1061   START_MEASURE_TIME;
1062
1063   moveSelectedPoints( aPos.x(), aPos.y() );
1064   myDragStartPosition = aPos;
1065
1066   END_MEASURE_TIME( "drag" );
1067 }
1068
1069 /**
1070  * Set zero viewer by the last view closed in
1071  * \param theManager a viewer manager
1072  */
1073 void CurveCreator_Widget::onLastViewClosed( SUIT_ViewManager* theManager )
1074 {
1075   myOCCViewer = 0;
1076 }
1077
1078 void CurveCreator_Widget::onMousePress( QMouseEvent* theEvent )
1079 {
1080   onMousePress( 0, theEvent );
1081 }
1082
1083 void CurveCreator_Widget::onMouseRelease( QMouseEvent* theEvent )
1084 {
1085   onMouseRelease( 0, theEvent );
1086 }
1087
1088 void CurveCreator_Widget::onMouseMove( QMouseEvent* theEvent )
1089 {
1090   onMouseMove( 0, theEvent );
1091 }
1092
1093 void CurveCreator_Widget::onCellChanged( int theRow, int theColumn )
1094 {
1095   int aCurrSect = myLocalPointView->getSectionId( theRow );
1096   int aPntIndex = myLocalPointView->getPointId( theRow );
1097
1098   if ( aPntIndex < 0 )
1099     return;
1100
1101   CurveCreator_ICurve::SectionToPointList aSelPoints;
1102   startCurveModification( aSelPoints );
1103
1104   double aX  = myLocalPointView->item( theRow, 2 )->data( Qt::UserRole ).toDouble();
1105   double anY = myLocalPointView->item( theRow, 3 )->data( Qt::UserRole ).toDouble();
1106   std::deque<float> aChangedPos;
1107   aChangedPos.push_back( aX );
1108   aChangedPos.push_back( anY );
1109   myCurve->setPoint( aCurrSect, aPntIndex, aChangedPos );
1110
1111   finishCurveModification( aSelPoints );
1112 }
1113
1114 /**
1115  * Removes a selected section from the curve. Updates undo/redo status
1116  */
1117 void CurveCreator_Widget::removeSection()
1118 {
1119   stopActionMode();
1120
1121   QList<int> aSections = mySectionView->getSelectedSections();
1122   for( int i = 0 ; i < aSections.size() ; i++ ){
1123     int aSectNum = aSections[i] - (i);
1124     myCurve->removeSection( aSectNum );
1125     mySectionView->sectionsRemoved( aSectNum );
1126   }
1127   mySectionView->clearSelection();
1128   updateUndoRedo();
1129 }
1130
1131 /**
1132  * Removes a selected points from the curve. Updates undo/redo status
1133  */
1134 void CurveCreator_Widget::removePoint()
1135 {
1136   CurveCreator_ICurve::SectionToPointList aPoints;
1137   getSelectedPoints( aPoints );
1138   if ( aPoints.size() == 0 )
1139     return;
1140
1141   CurveCreator_ICurve::SectionToPointList aSelPoints;
1142   startCurveModification( aSelPoints, false );
1143
1144   myCurve->removeSeveralPoints( aPoints );
1145   finishCurveModification( CurveCreator_ICurve::SectionToPointList() );
1146 }
1147
1148 void CurveCreator_Widget::addNewPoint(const CurveCreator::Coordinates& theCoords)
1149 {
1150   if( !myCurve )
1151     return;
1152   QList<int> aSections = mySectionView->getSelectedSections();
1153   if( aSections.size() == 0 ){
1154     return;
1155   }
1156   int aSection = aSections[0];
1157   myCurve->addPoints(theCoords, aSection); // add to the end of section
1158   updateActionsStates();
1159   updateUndoRedo();
1160 }
1161
1162 void CurveCreator_Widget::insertPointToSelectedSegment( const int theX,
1163                                                         const int theY )
1164 {
1165   Handle(AIS_InteractiveContext) aContext = getAISContext();
1166
1167   OCCViewer_ViewPort3d* aViewPort = getViewPort();
1168   Handle(V3d_View) aView;
1169   if ( aViewPort )
1170     aView = aViewPort->getView();
1171
1172   if ( aContext.IsNull() || aView.IsNull() )
1173     return;
1174   gp_Pnt aPoint;
1175   gp_Pnt aPoint1, aPoint2;
1176   Handle(AIS_InteractiveObject) anAISObject = myCurve->getAISObject();
1177   bool isFoundPoint = CurveCreator_Utils::pointOnObject( aView, anAISObject, theX, theY,
1178                                                          aPoint, aPoint1, aPoint2 );
1179   if ( !isFoundPoint )
1180     return;
1181
1182   // insert the point to the model curve
1183   CurveCreator_ICurve::SectionToPointList aSelPoints;
1184   startCurveModification( aSelPoints );
1185
1186   CurveCreator::Coordinates aCoords;
1187   aCoords.push_back( aPoint.X() );
1188   aCoords.push_back( aPoint.Y() );
1189
1190   CurveCreator_ICurve::SectionToPointList aPoints1, aPoints2;
1191   findSectionsToPoints( aPoint1.X(), aPoint1.Y(), aPoints1 );
1192   findSectionsToPoints( aPoint2.X(), aPoint2.Y(), aPoints2 );
1193   CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aPoints1.begin(),
1194                                                           aLast = aPoints1.end();
1195   int aSectionId = -1;
1196   // there can be a case when a new point is added into two sections
1197   int aPoint1Id = -1, aPoint2Id = -1;
1198   for ( ; anIt != aLast && aSectionId < 0; anIt++ ) {
1199     int aSectionCur = anIt->first;
1200     CurveCreator_ICurve::SectionToPointList::const_iterator anIt2 = aPoints2.begin(),
1201                                                             aLast2 = aPoints2.end();
1202     for ( ; anIt2 != aLast2 && aSectionId < 0; anIt2++ ) {
1203       if ( anIt2->first == aSectionCur ) {
1204         aSectionId = aSectionCur;
1205         aPoint1Id = anIt->second;
1206         aPoint2Id = anIt2->second;
1207       }
1208     }
1209   }
1210
1211   int anInsertPos = -1;
1212   int aLastPoint = myCurve->getNbPoints( aSectionId )-1; 
1213   if ( ( aPoint1Id == aLastPoint && aPoint2Id == 0 ) ||
1214        ( aPoint2Id == aLastPoint && aPoint1Id == 0 ) )
1215     anInsertPos = -1; // if the section happens between first and last points
1216   else
1217     anInsertPos = aPoint1Id < aPoint2Id ? aPoint1Id + 1 : aPoint2Id + 1;
1218
1219   myCurve->addPoints( aCoords, aSectionId, anInsertPos );
1220
1221   finishCurveModification( aSelPoints );
1222
1223   setSelectedPoints();
1224 }
1225
1226 void CurveCreator_Widget::moveSelectedPoints( const int theXPosition,
1227                                               const int theYPosition )
1228 {
1229   OCCViewer_ViewPort3d* aViewPort = getViewPort();
1230   if ( !aViewPort )
1231     return;
1232
1233   CurveCreator_ICurve::SectionToPointList aPoints;
1234   startCurveModification( aPoints, false );
1235
1236   gp_Pnt aStartPnt = CurveCreator_Utils::ConvertClickToPoint( myDragStartPosition.x(),
1237                                                               myDragStartPosition.y(),
1238                                                               aViewPort->getView() );
1239   gp_Pnt anEndPnt = CurveCreator_Utils::ConvertClickToPoint( theXPosition, theYPosition,
1240                                                              aViewPort->getView() );
1241   double aXDelta = aStartPnt.X() - anEndPnt.X();
1242   double anYDelta = aStartPnt.Y() - anEndPnt.Y();
1243
1244   CurveCreator_ICurve::SectionToPointCoordsList aCoordList;
1245   std::deque<float> aChangedPos;
1246   CurveCreator_ICurve::SectionToPointList::const_iterator anIt = myDragPoints.begin(),
1247                                                           aLast = myDragPoints.end();
1248   for ( ; anIt != aLast; anIt++ ) {
1249     int aSectionId = anIt->first;
1250     int aPointId = anIt->second;
1251     aChangedPos = myCurve->getPoint( aSectionId, aPointId );
1252     if ( aChangedPos.size() < 2 )
1253       continue;
1254
1255     // Remember drag points coordinates
1256     if ( !myDragged ) {
1257       myInitialDragPointsCoords.insert( std::make_pair( aSectionId, aPointId ), aChangedPos );
1258     }
1259
1260     aChangedPos[0] = aChangedPos[0] - aXDelta;
1261     aChangedPos[1] = aChangedPos[1] - anYDelta;
1262     
1263     aCoordList.push_back(
1264       std::make_pair(std::make_pair( aSectionId, aPointId ), 
1265                      aChangedPos ));
1266   }
1267   myCurve->setSeveralPoints( aCoordList, false );
1268
1269   myDragged = true;
1270   finishCurveModification( myDragPoints );
1271 }
1272
1273 void CurveCreator_Widget::updateLocalPointView()
1274 {
1275   if ( myDragStarted )
1276     return;
1277   Handle(AIS_InteractiveContext) aContext = getAISContext();
1278   if ( aContext.IsNull() )
1279     return;
1280
1281   CurveCreator_Utils::getSelectedPoints( aContext, myCurve, myLocalPoints );
1282   int aNbPoints = myLocalPoints.size();
1283
1284   bool isRowLimit = aNbPoints > myLocalPointRowLimit;
1285   myLocalPointView->setVisible( getActionMode() == ModificationMode && !isRowLimit );
1286
1287   if ( !isRowLimit ) {
1288     bool isBlocked = myLocalPointView->blockSignals(true);
1289
1290     myLocalPointView->setLocalPointsToTable( myLocalPoints );
1291
1292     myLocalPointView->blockSignals( isBlocked );
1293   }
1294 }
1295
1296 /**
1297  * 
1298  */
1299 void CurveCreator_Widget::setLocalPointContext( const bool theOpen, const bool isUpdateTable )
1300 {
1301   CurveCreator_Utils::setLocalPointContext( myCurve, getAISContext(), theOpen );
1302   if ( !theOpen && isUpdateTable )
1303     updateLocalPointView();
1304 }
1305
1306 /**
1307  * Set drag operation started. Save the position and a list of dragged points
1308  * \param theState the drag operation state: started/finished
1309  * \param thePoint the start drag position
1310  */
1311 void CurveCreator_Widget::setDragStarted( const bool theState, const QPoint& thePoint )
1312 {
1313   if ( theState ) {
1314     getSelectedPoints( myDragPoints );
1315
1316     myDragStarted = myDragPoints.size();
1317     myDragStartPosition = thePoint;
1318     if ( myDragStarted ) {
1319       // change a viewer interaction style in order to avoid a select rectangle build
1320       myDragInteractionStyle = changeInteractionStyle( SUIT_ViewModel::KEY_FREE );
1321       myCurve->setSkipSorting( true );
1322     }
1323   }
1324   else {
1325     if ( myDragStarted )
1326       changeInteractionStyle( myDragInteractionStyle );
1327     myDragStarted = false;
1328     myDragPoints.clear();
1329     myInitialDragPointsCoords.clear();
1330   }
1331   myDragged = false;
1332 }
1333
1334 void CurveCreator_Widget::getSelectedPoints( CurveCreator_ICurve::SectionToPointList& thePoints )
1335 {
1336   thePoints.clear();
1337   thePoints = myLocalPoints;
1338 }
1339
1340 void CurveCreator_Widget::setSelectedPoints( const CurveCreator_ICurve::SectionToPointList& thePoints )
1341 {
1342   if ( myDragStarted )
1343     return;
1344   Handle(AIS_InteractiveContext) aContext = getAISContext();
1345   if ( aContext.IsNull() || !aContext->HasOpenedContext() )
1346     return;
1347
1348   CurveCreator_Utils::setSelectedPoints( aContext, myCurve, thePoints );
1349
1350   updateLocalPointView();
1351 }
1352
1353 void CurveCreator_Widget::stopActionMode()
1354 {
1355   setActionMode( NoneMode );
1356 }
1357
1358 /**
1359  * Get viewer information before perform the curve modification.
1360  * Take a list of selected cuve points an close local context.
1361  * The context should be closed because the curve presentation is
1362  * redisplayed and if it is not closed, when we close the local context
1363  * later, the presentation shown in the local context is disappeared.
1364  * \param thePoints an output list of curve selected points
1365  * \param theFillPoints a flag whether the selection list should be filled
1366  */
1367 void CurveCreator_Widget::startCurveModification(
1368                            CurveCreator_ICurve::SectionToPointList& thePoints,
1369                            const bool theFillPoints )
1370 {
1371   if ( theFillPoints ) {
1372     thePoints.clear();
1373     getSelectedPoints( thePoints );
1374   }
1375   setLocalPointContext( false );
1376 }
1377
1378 /**
1379  * Restore the viewer state after the curve modification is done.
1380  * Open local context and select given points inside it.
1381  * \param thePoints a list of curve selected points
1382  */
1383 void CurveCreator_Widget::finishCurveModification(
1384                            const CurveCreator_ICurve::SectionToPointList& thePoints )
1385 {
1386   if ( getActionMode() == ModificationMode )
1387     setLocalPointContext( true );
1388   setSelectedPoints( thePoints );
1389   updateUndoRedo();
1390 }
1391
1392 /**
1393  * Returns a point index in the model curve by the point coordinates in the viewer
1394  * \param theX the X coordinate of the point
1395  * \param theY the Y coordinate of the point
1396  */
1397 int CurveCreator_Widget::findLocalPointIndex( int theSectionId, float theX, float theY )
1398 {
1399   return CurveCreator_UtilsICurve::findLocalPointIndex( myCurve, theSectionId, theX, theY );
1400 }
1401
1402 void CurveCreator_Widget::findSectionsToPoints( const double theX, const double theY,
1403                                  CurveCreator_ICurve::SectionToPointList& thePoints )
1404 {
1405   return CurveCreator_UtilsICurve::findSectionsToPoints( myCurve, theX, theY, thePoints );
1406 }
1407
1408 void CurveCreator_Widget::convert( const CurveCreator_ICurve::SectionToPointList& thePoints,
1409                                    QMap<int, QList<int> >& theConvPoints )
1410 {
1411   return CurveCreator_UtilsICurve::convert( thePoints, theConvPoints );
1412 }
1413
1414 /**
1415  * Returns whethe the container has the value
1416  * \param theList a container of values
1417  * \param theValue a value
1418  */
1419 bool CurveCreator_Widget::contains( const CurveCreator_ICurve::SectionToPointList& theList,
1420                                     const CurveCreator_ICurve::SectionToPoint& theValue ) const
1421 {
1422   return CurveCreator_UtilsICurve::contains( theList, theValue );
1423 }