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