Salome HOME
Merge branch 'fbt/add_header_for_mpi_compilation'
[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 }
322
323 void CurveCreator_Widget::setCurve( CurveCreator_ICurve* theCurve )
324 {
325   myCurve = theCurve;
326   mySectionView->setCurve( myCurve );
327   myLocalPointView->setCurve( myCurve );
328   updateActionsStates();
329   updateUndoRedo();
330 }
331
332 void CurveCreator_Widget::onSelectionChanged()
333 {
334   updateActionsStates();
335   updateUndoRedo();
336   emit selectionChanged();
337 }
338
339 void CurveCreator_Widget::updateActionsStates()
340 {
341   QList<ActionId> anEnabledAct;
342   if( myCurve ){
343     anEnabledAct << NEW_SECTION_ID << MODIFICATION_MODE_ID;
344     if ( removeEnabled() )
345       anEnabledAct << REMOVE_ID;
346     QList<int> aSelSections = mySectionView->getSelectedSections();
347     CurveCreator_TreeView::SelectionType aSelType = mySectionView->getSelectionType();
348     switch( aSelType ){
349     case CurveCreator_TreeView::ST_NOSEL:{
350       break;
351     }
352     case CurveCreator_TreeView::ST_SECTIONS:{
353       /*if( aSelSections[0] > 0 ){
354         anEnabledAct << UP_ID;
355       }*/
356       if( aSelSections.size() == 1 ){
357         anEnabledAct << ADDITION_MODE_ID << DETECTION_MODE_ID;
358       }
359       switch ( getActionMode() ) {
360         case AdditionMode: {
361           mySection = -1;
362           myPointNum = -1;
363           QList<int> aSelSection = mySectionView->getSelectedSections();
364           if( aSelSection.size() > 0 ){
365             mySection = aSelSection[0];
366             myPointNum = myCurve->getNbPoints(mySection);
367           }
368         }
369         break;
370         case ModificationMode: {
371           if ( myNewSectionEditor->isEnableClosed() )
372             anEnabledAct << CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID;
373           anEnabledAct << SET_SECTIONS_POLYLINE_ID << SET_SECTIONS_SPLINE_ID;
374           int aSectCnt = myCurve->getNbSections();
375           if( aSectCnt > 0 )
376             anEnabledAct << CLEAR_ALL_ID;
377           if( aSectCnt > 1 )
378             anEnabledAct << JOIN_ALL_ID;
379           if( aSelSections.size() > 1 ){
380             anEnabledAct << JOIN_ID;
381           }
382         }
383         break;
384         case DetectionMode: {
385         }
386         break;
387         case NoneMode:
388           {
389             int aSectCnt = myCurve->getNbSections();
390             if( aSectCnt > 1 )
391               anEnabledAct << JOIN_ALL_ID;
392             if( aSelSections.size() > 1 )
393               anEnabledAct << JOIN_ID;
394           }
395           break;
396         default:
397         break;
398       }
399       /*if( aSelSections[ aSelSections.size() - 1 ] < ( myCurve->getNbSections() - 1 ) ){
400         anEnabledAct << DOWN_ID;
401       }*/
402       break;
403     }
404     /*case CurveCreator_TreeView::ST_POINTS_ONE_SECTION:{
405       if( aSelPoints[0].second > 0 ){
406         anEnabledAct << UP_ID;
407       }
408       int aLastIndex = aSelPoints.size()-1;
409       int aSect = aSelPoints[0].first;
410       if( aSelPoints[aLastIndex].second < (myCurve->getNbPoints(aSect) - 1)){
411         anEnabledAct << DOWN_ID;
412       }
413       if( aSelPoints.size() == 1){
414         anEnabledAct << INSERT_POINT_BEFORE_ID << INSERT_POINT_AFTER_ID;
415       }
416       break;
417     }*/
418
419     }
420     
421     /*int aSelObjsCnt = aSelPoints.size() + aSelSections.size();
422     if( aSelObjsCnt > 0 ){
423       anEnabledAct << REMOVE_ID;
424     }
425     if( (myCurve->getNbSections() + myCurve->getNbPoints()) > 0 ){
426       anEnabledAct << REMOVE_ALL_ID;
427     }*/
428     if( myCurve->getNbSections() > 1 ){
429       anEnabledAct << JOIN_ALL_ID;
430     }
431   }
432   QList<ActionId> anIds = myActionMap.keys();
433   for( int i = 0 ; i < anIds.size() ; i++ ){
434     if( myActionMap.contains(anIds[i]) ){
435       if( anEnabledAct.contains(anIds[i]) ){
436         myActionMap[anIds[i]]->setEnabled(true);
437       }
438       else{
439         myActionMap[anIds[i]]->setEnabled(false);
440       }
441     }
442   }
443 }
444
445 void CurveCreator_Widget::onAdditionMode(bool checked)
446 {
447   if (!checked)
448     return;
449
450   Handle(AIS_InteractiveContext) aContext = getAISContext();
451   if( !myCurve || aContext.IsNull() )
452     return;
453
454   mySection= -1;
455   myPointNum = -1;
456   QList<int> aSelSection = mySectionView->getSelectedSections();
457   if( aSelSection.size() > 0 ){
458     mySection = aSelSection[0];
459   }
460 //  emit subOperationStarted( myNewPointEditor );
461 }
462
463 void CurveCreator_Widget::onModificationMode(bool checked)
464 {
465   myLocalPointView->setVisible( checked );
466 }
467
468 void CurveCreator_Widget::onDetectionMode(bool checked)
469 {
470 }
471
472 void CurveCreator_Widget::onModeChanged(bool checked)
473 {
474   ActionMode aMode = NoneMode;
475   if (checked) {
476     QAction* anAction = (QAction*)sender();
477     switch(myActionMap.key(anAction)) {
478       case ADDITION_MODE_ID:
479         aMode = AdditionMode;
480         if (myActionMap[MODIFICATION_MODE_ID]->isChecked())
481           myActionMap[MODIFICATION_MODE_ID]->trigger();
482         else if (myActionMap[DETECTION_MODE_ID]->isChecked())
483           myActionMap[DETECTION_MODE_ID]->trigger();
484         break;
485       case MODIFICATION_MODE_ID:
486         aMode = ModificationMode;
487         if (myActionMap[ADDITION_MODE_ID]->isChecked())
488           myActionMap[ADDITION_MODE_ID]->trigger();
489         else if (myActionMap[DETECTION_MODE_ID]->isChecked())
490           myActionMap[DETECTION_MODE_ID]->trigger();
491         break;
492       case DETECTION_MODE_ID:
493         aMode = DetectionMode;
494         if (myActionMap[ADDITION_MODE_ID]->isChecked())
495           myActionMap[ADDITION_MODE_ID]->trigger();
496         else if (myActionMap[MODIFICATION_MODE_ID]->isChecked())
497           myActionMap[MODIFICATION_MODE_ID]->trigger();
498         break;
499     }
500   }
501   updateActionsStates();
502   updateUndoRedo();
503   setLocalPointContext( aMode == ModificationMode, true );
504 }
505
506 void CurveCreator_Widget::onNewSection()
507 {
508   if( !myCurve )
509     return;
510
511   stopActionMode();
512   myNewSectionEditor->clear();
513   myNewSectionEditor->setEditMode(false);
514   QString aSectName = QString( CurveCreator_UtilsICurve::getUniqSectionName( myCurve ).c_str() );
515   myNewSectionEditor->setSectionParameters(aSectName, true, CurveCreator::Polyline );
516   emit subOperationStarted( myNewSectionEditor, false );
517 }
518
519 void CurveCreator_Widget::onAddNewSection()
520 {
521   if( !myCurve )
522     return;
523   myCurve->addSection( myNewSectionEditor->getName().toStdString(),
524                        myNewSectionEditor->getSectionType(),
525                        myNewSectionEditor->isClosed() );
526   mySectionView->sectionAdded( -1 ); // add a new section to the end of list
527   QString aNewName = QString( CurveCreator_UtilsICurve::getUniqSectionName( myCurve ).c_str() );
528   myNewSectionEditor->setSectionName(aNewName);
529   updateActionsStates();
530   updateUndoRedo();
531   onCancelSection();
532 }
533
534 void CurveCreator_Widget::onCancelSection()
535 {
536   emit subOperationFinished( myNewSectionEditor );
537 }
538
539 QAction* CurveCreator_Widget::createAction( ActionId theId, const QString& theName, const QPixmap& theImage,
540                                             const QString& theToolTip, const QKeySequence& theShortcut )
541 {
542   QAction* anAct = new QAction(theName,this);
543   if( !theImage.isNull() ){
544     anAct->setIcon(theImage);
545   }
546   anAct->setShortcut(theShortcut);
547   anAct->setToolTip(theToolTip);
548   myActionMap[theId] = anAct;
549   return anAct;
550 }
551
552 QAction* CurveCreator_Widget::getAction( ActionId theId )
553 {
554   if( myActionMap.contains(theId) )
555     return myActionMap[theId];
556   return NULL;
557 }
558
559 QAction* CurveCreator_Widget::getAction( ActionMode theMode )
560 {
561   ActionId anActionId = NONE_ID;
562   switch ( theMode ) {
563     case AdditionMode:
564       anActionId = ADDITION_MODE_ID;
565       break;
566     case ModificationMode:
567       anActionId = MODIFICATION_MODE_ID;
568       break;
569     case DetectionMode:
570       anActionId = DETECTION_MODE_ID;
571       break;
572     default:
573       break;
574   }
575   QAction* anAction = 0;
576   if ( anActionId != NONE_ID && myActionMap.contains( anActionId ) )
577     anAction = myActionMap[anActionId];
578   return anAction;
579 }
580
581 void CurveCreator_Widget::onEditSection( int theSection )
582 {
583   if( !myCurve )
584     return;
585   
586   stopActionMode();
587   mySection = theSection;
588   QString aSectName = QString::fromStdString( myCurve->getSectionName(theSection));
589   bool isClosed = myCurve->isClosed(theSection);
590   CurveCreator::SectionType aType = myCurve->getSectionType(theSection);
591   myNewSectionEditor->setEditMode(true);
592   myNewSectionEditor->setSectionParameters( aSectName, isClosed, aType );
593
594   emit subOperationStarted( myNewSectionEditor, true );
595 }
596
597 void CurveCreator_Widget::onModifySection()
598 {
599   if( !myCurve )
600     return;
601   QString aName = myNewSectionEditor->getName();
602   bool isClosed = myNewSectionEditor->isClosed();
603   CurveCreator::SectionType aSectType = myNewSectionEditor->getSectionType();
604   if( myCurve->getSectionName(mySection) != aName.toStdString() )
605     myCurve->setSectionName( mySection , aName.toStdString() );
606
607   bool isGeomModified = false;
608
609   if( myCurve->getSectionType(mySection) != aSectType ) {
610     myCurve->setSectionType( mySection, aSectType );
611     isGeomModified = true;
612   }
613
614   if( myCurve->isClosed(mySection) != isClosed ) {
615     myCurve->setClosed( mySection, isClosed );
616     isGeomModified = true;
617   }
618   mySectionView->sectionChanged(mySection);
619   updateUndoRedo();
620   onCancelSection();
621
622   emit curveModified();
623 }
624
625 void CurveCreator_Widget::onJoin()
626 {
627   if( !myCurve )
628     return;
629   QList<int> aSections = mySectionView->getSelectedSections();
630   if( aSections.size() == 0 ){
631     return;
632   }
633   stopActionMode();
634
635   std::list<int> aSectionsToJoin;
636   for( int i = 0; i < aSections.size() ; i++ ){
637     aSectionsToJoin.push_back( aSections[i] );
638   }
639   //int aMainSect = aSectionsToJoin.front();
640   //int aMainSectSize = myCurve->getNbPoints(aMainSect);
641   if ( myCurve->join( aSectionsToJoin ) )
642   {
643     std::list<int>::const_iterator anIt = aSectionsToJoin.begin(),
644                                    aLast = aSectionsToJoin.end();
645     // the first section should be skipped. It is not removed, but is modified
646     anIt++;
647     for ( ; anIt != aLast; anIt++ )
648       mySectionView->sectionsRemoved( *anIt );
649   }
650
651   /* The update for the points of the main section
652   int aNewSectSize = myCurve->getNbPoints(aMainSect);
653   if( aNewSectSize != aMainSectSize )
654     mySectionView->pointsAdded( aMainSect, aMainSectSize, aNewSectSize-aMainSectSize );*/
655   updateUndoRedo();
656
657   emit curveModified();
658 }
659
660 void CurveCreator_Widget::onRemove()
661 {
662   if( !myCurve )
663     return;
664
665   switch( getActionMode() ) {
666     case NoneMode:
667       removeSection();
668     break;
669     case ModificationMode:
670       removePoint();
671     break;
672     default:
673       break;
674   }
675 }
676
677 void CurveCreator_Widget::onClearAll()
678 {
679   if( !myCurve )
680     return;
681   stopActionMode();
682   myCurve->clear();
683   mySectionView->reset();
684   updateActionsStates();
685   updateUndoRedo();
686   
687   emit curveModified();
688 }
689
690 void CurveCreator_Widget::onJoinAll()
691 {
692   if( !myCurve )
693     return;
694   stopActionMode();
695
696   std::list<int> aSectionsToJoin;
697   for( int i = 0, aNb = myCurve->getNbSections(); i < aNb ; i++ ){
698     aSectionsToJoin.push_back( i );
699   }
700   bool aRes = myCurve->join( aSectionsToJoin );
701
702   mySectionView->reset();
703   updateActionsStates();
704   updateUndoRedo();
705
706   emit curveModified();
707 }
708
709 void CurveCreator_Widget::onUndoSettings()
710 {
711
712 }
713
714 void CurveCreator_Widget::onSetSpline()
715 {
716   if( !myCurve )
717     return;
718   stopActionMode();
719   QList<int> aSelSections = mySectionView->getSelectedSections();
720   for( int i = 0 ; i < aSelSections.size() ; i++ ){
721     myCurve->setSectionType(aSelSections[i], CurveCreator::Spline );
722     mySectionView->sectionChanged(aSelSections[i]);
723   }
724   updateUndoRedo();
725
726   emit curveModified();
727 }
728
729 void CurveCreator_Widget::onSetPolyline()
730 {
731   if( !myCurve )
732     return;
733   stopActionMode();
734   QList<int> aSelSections = mySectionView->getSelectedSections();
735   for( int i = 0 ; i < aSelSections.size() ; i++ ){
736     myCurve->setSectionType( aSelSections[i], CurveCreator::Polyline );
737     mySectionView->sectionChanged( aSelSections[i] );
738   }
739   updateUndoRedo();
740
741   emit curveModified();
742 }
743
744 void CurveCreator_Widget::onCloseSections()
745 {
746   if( !myCurve )
747     return;
748   stopActionMode();
749   QList<int> aSelSections = mySectionView->getSelectedSections();
750   for( int i = 0 ; i < aSelSections.size() ; i++ ){
751     myCurve->setClosed(aSelSections[i], true);
752     mySectionView->sectionChanged(aSelSections[i]);
753   }
754   updateUndoRedo();
755
756   emit curveModified();
757 }
758
759 void CurveCreator_Widget::onUncloseSections()
760 {
761   if( !myCurve )
762     return;
763   stopActionMode();
764   QList<int> aSelSections = mySectionView->getSelectedSections();
765   for( int i = 0 ; i < aSelSections.size() ; i++ ){
766     myCurve->setClosed(aSelSections[i], false);
767     mySectionView->sectionChanged(aSelSections[i]);
768   }
769   updateUndoRedo();
770
771   emit curveModified();
772 }
773
774 void CurveCreator_Widget::onUndo()
775 {
776   if( !myCurve )
777     return;
778
779   CurveCreator_ICurve::SectionToPointList aPoints;
780   startCurveModification( aPoints, false );
781   myCurve->undo();
782   finishCurveModification();
783   mySectionView->reset();
784
785   emit curveModified();
786 }
787
788 void CurveCreator_Widget::onRedo()
789 {
790   if( !myCurve )
791     return;
792   CurveCreator_ICurve::SectionToPointList aPoints;
793   startCurveModification( aPoints, false );
794   myCurve->redo();
795   finishCurveModification();
796   mySectionView->reset();
797
798   emit curveModified();
799 }
800
801 void CurveCreator_Widget::updateUndoRedo()
802 {
803   if( !myCurve )
804     return;
805   QAction* anAct = myActionMap[UNDO_ID];
806   if( anAct != 0 ){
807     if( myCurve->getNbUndo() != 0 ){
808       anAct->setEnabled(true);
809     }
810     else{
811       anAct->setDisabled(true);
812     }
813   }
814   anAct = myActionMap[REDO_ID];
815   if( anAct != 0 ){
816     if( myCurve->getNbRedo() != 0 ){
817       anAct->setEnabled(true);
818     }
819     else{
820       anAct->setDisabled(true);
821     }
822   }
823 }
824
825 void CurveCreator_Widget::onContextMenu( QPoint thePoint )
826 {
827   QList<ActionId> aContextActions;
828   aContextActions << CLEAR_ALL_ID << JOIN_ID << JOIN_ALL_ID << SEPARATOR_ID <<
829                      CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID << SET_SECTIONS_POLYLINE_ID <<
830                      SET_SECTIONS_SPLINE_ID;
831   QPoint aGlPoint = mySectionView->mapToGlobal(thePoint);
832   bool isVis = false;
833   QList<ActionId> aResAct;
834   for( int i = 0 ; i < aContextActions.size() ; i++ ){
835     if( aContextActions[i] != SEPARATOR_ID ){
836       if( myActionMap.contains(aContextActions[i]) ){
837         QAction* anAct = myActionMap[aContextActions[i]];
838         if( anAct->isEnabled() ){
839           aResAct << aContextActions[i];
840           isVis = true;
841         }
842       }
843     }
844     else{
845       aResAct << SEPARATOR_ID;
846     }
847   }
848   if( !isVis )
849     return;
850
851   QMenu* aMenu = new QMenu(this);
852   for( int i = 0 ; i < aResAct.size() ; i++ ){
853     if( aResAct[i] == SEPARATOR_ID ){
854       aMenu->addSeparator();
855     }
856     else{
857       QAction* anAct = myActionMap[aResAct[i]];
858       aMenu->insertAction(NULL, anAct);
859     }
860   }
861   aMenu->exec(aGlPoint);
862 }
863
864 QList<int> CurveCreator_Widget::getSelectedSections()
865 {
866   return mySectionView->getSelectedSections();
867 }
868
869 void CurveCreator_Widget::setSelectedSections( const QList<int>& theSections )
870 {
871   mySectionView->setSelectedSections( theSections );
872   updateActionsStates();
873   updateUndoRedo();
874 }
875
876 /**
877  * According to the widget state, performs the remove action
878  */
879 void CurveCreator_Widget::removeSelected()
880 {
881   onRemove();
882 }
883
884 /**
885  * Checks whether there are some selection to be removed
886  */
887 bool CurveCreator_Widget::removeEnabled()
888 {
889   bool isEnabled = getActionMode() == ModificationMode;
890   if ( !isEnabled ) {
891     QList<int> aSelSections = mySectionView->getSelectedSections();
892     CurveCreator_TreeView::SelectionType aSelType = mySectionView->getSelectionType();
893     isEnabled = aSelType == CurveCreator_TreeView::ST_SECTIONS &&
894                 aSelSections.size() == 1;
895   }
896   return isEnabled;
897 }
898
899 void CurveCreator_Widget::setActionMode( const ActionMode& theMode )
900 {
901   ActionMode aPrevMode = getActionMode();
902   QAction* aPrevAction = getAction( aPrevMode );
903   QAction* anAction = getAction( theMode );
904   switch ( theMode ) {
905     case NoneMode:
906     case AdditionMode: {
907       if ( aPrevAction ) {
908         if ( aPrevAction->isChecked() ) {
909           aPrevAction->setChecked( false );
910         }
911       }
912       if ( aPrevMode == ModificationMode )
913         onModificationMode( false );
914       if ( aPrevMode == AdditionMode )
915         onAdditionMode( false );
916
917       if ( theMode == AdditionMode )
918       {
919         anAction->setChecked( true );
920         onModeChanged( true );
921       }
922     }
923     break;
924     break;
925     case ModificationMode:
926     {
927       //TODO
928     }
929     break;
930     case DetectionMode:
931       break;
932   }
933 }
934
935 CurveCreator_Widget::ActionMode CurveCreator_Widget::getActionMode() const
936 {
937   ActionMode aMode = NoneMode;
938
939   if ( myActionMap[ADDITION_MODE_ID]->isChecked() )
940     aMode = AdditionMode;
941   else if ( myActionMap[MODIFICATION_MODE_ID]->isChecked() )
942     aMode = ModificationMode;
943   else if ( myActionMap[DETECTION_MODE_ID]->isChecked() )
944     aMode = DetectionMode;
945
946   return aMode;
947 }
948
949 void CurveCreator_Widget::SetViewer2DMode(const bool To2D)
950 {
951   if (myOCCViewer) {
952     if (To2D) {
953       myOld2DMode = OCCViewer_Utilities::setViewer2DMode
954                     (myOCCViewer, OCCViewer_ViewWindow::XYPlane);
955     } else {
956       OCCViewer_Utilities::setViewer2DMode(myOCCViewer, myOld2DMode);
957     }
958   }
959 }
960
961 //=================================================================================
962 // function : GeometryGUI::addCoordsByClick()
963 // purpose  : Manage mouse press events in Additon mode
964 //=================================================================================
965 void CurveCreator_Widget::addCoordsByClick( QMouseEvent* pe )
966 {
967   if (pe->button() != Qt::LeftButton)
968     return;
969
970   if ( pe->modifiers() != Qt::ControlModifier ) {
971     Handle(AIS_InteractiveContext) ic = getAISContext();
972     if ( ic.IsNull() )
973       return;
974
975     gp_Pnt aPnt;    
976
977     ic->InitSelected();
978     if ( pe->modifiers() == Qt::ShiftModifier )
979       ic->ShiftSelect();  // Append selection
980     else
981       ic->Select();       // New selection
982
983     {
984       OCCViewer_ViewPort3d* vp = getViewPort();
985       aPnt = CurveCreator_Utils::ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
986     }
987     // set the coordinates into dialog
988     CurveCreator::Coordinates aCoords;
989     aCoords.push_back( aPnt.X() );
990     aCoords.push_back( aPnt.Y() );
991     if ( myCurve->getDimension() == 3 ) {
992       aCoords.push_back( aPnt.Z() );
993     }
994     addNewPoint(aCoords);
995   }
996 }
997
998 /**
999  * Manage mouse press events
1000  * \param theWindow an owner of the signal
1001  * \param theEvent a mouse event
1002  */
1003 void CurveCreator_Widget::onMousePress( SUIT_ViewWindow*, QMouseEvent* theEvent )
1004 {
1005   if ( theEvent->button() != Qt::LeftButton )
1006     return;
1007
1008   myPressedX = theEvent->x();
1009   myPressedY = theEvent->y();
1010
1011   switch( getActionMode() ) {
1012     case ModificationMode: {
1013       //store initial cursor position for Drag&Drop
1014       setDragStarted( true, theEvent->pos() );
1015       break;
1016     }
1017     case AdditionMode: {
1018       addCoordsByClick( theEvent );
1019       break;
1020     }
1021     default:
1022       break;
1023   }
1024 }
1025
1026 /**
1027  * Manage mouse release events in Modification mode
1028  * \param theWindow an owner of the signal
1029  * \param theEvent a mouse event
1030  */
1031 void CurveCreator_Widget::onMouseRelease( SUIT_ViewWindow*, QMouseEvent* theEvent )
1032 {
1033   if ( getActionMode() != ModificationMode )
1034     return;
1035
1036   if ( myDragStarted ) {
1037     bool isDragged = myDragged;
1038     CurveCreator_ICurve::SectionToPointList aDraggedPoints;
1039     QMap<CurveCreator_ICurve::SectionToPoint, std::deque< float > > anInitialDragPointsCoords;
1040     if ( myDragged ) {
1041       aDraggedPoints = myDragPoints;
1042       anInitialDragPointsCoords = myInitialDragPointsCoords;
1043     }
1044
1045     setDragStarted( false );
1046
1047     if ( aDraggedPoints.size() > 0 ) {
1048       // Collect old coordinates of the dragged points
1049       CurveCreator_ICurve::SectionToPointCoordsList anOldPoints;
1050       foreach ( const CurveCreator_ICurve::SectionToPoint aSectionToPoint, anInitialDragPointsCoords.keys() ) {
1051         CurveCreator::Coordinates aCoords = anInitialDragPointsCoords.value( aSectionToPoint );
1052         anOldPoints.push_back( std::make_pair( aSectionToPoint, aCoords ) );
1053       }
1054
1055       if ( myCurve->canPointsBeSorted() ) {
1056         // Add old coordinates of the curve points (except the dragged points) to the list
1057         for( int aSectionId = 0 ; aSectionId < myCurve->getNbSections() ; aSectionId++ ) {
1058           CurveCreator::Coordinates aCoords;
1059           for ( int aPointId = 0, aNb = myCurve->getNbPoints( aSectionId ); aPointId < aNb; aPointId++ ) {
1060             aCoords = myCurve->getPoint( aSectionId, aPointId );
1061             if ( aCoords.size() < 2 ) {
1062               continue;
1063             }
1064             
1065             CurveCreator_ICurve::SectionToPoint aSectionToPoint = std::make_pair( aSectionId, aPointId );
1066
1067             if ( !anInitialDragPointsCoords.contains( aSectionToPoint ) ) {
1068               anOldPoints.push_back( std::make_pair( aSectionToPoint, aCoords ) );
1069             }
1070           }
1071         }
1072         
1073         // Apply points sorting
1074         CurveCreator_ICurve::SectionToPointList aPoints;
1075         startCurveModification( aPoints, false );
1076
1077         myCurve->setSkipSorting( false );
1078
1079         CurveCreator_ICurve::SectionToPointCoordsList aCoordList;
1080         CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aDraggedPoints.begin(),
1081                                                                 aLast = aDraggedPoints.end();
1082         for ( ; anIt != aLast; anIt++ ) {
1083           int aSectionId = anIt->first;
1084           int aPointId = anIt->second;
1085           std::deque<float> aPos = myCurve->getPoint( aSectionId, aPointId );
1086
1087           aCoordList.push_back(
1088             std::make_pair( std::make_pair( aSectionId, aPointId ), aPos ) );
1089         }
1090
1091         myCurve->setSeveralPoints( aCoordList, false );
1092     
1093         finishCurveModification( aDraggedPoints );
1094       } else {
1095         // if the drag of some points has happened, restore the drag selection
1096         START_MEASURE_TIME;
1097         setSelectedPoints( aDraggedPoints );
1098         END_MEASURE_TIME( "drop" );
1099       }
1100
1101       // Save drag difference
1102       myCurve->saveCoordDiff( anOldPoints );
1103     }
1104   }
1105   else // check whether the segment is clicked an a new point should be added to the segment
1106   {
1107     int aReleasedX = theEvent->x();
1108     int aReleasedY = theEvent->y();
1109     if ( myPressedX == aReleasedX && myPressedY == aReleasedY )
1110       insertPointToSelectedSegment( aReleasedX, aReleasedY );
1111   }
1112
1113   // updates the input panel table to show the selected point coordinates
1114   updateLocalPointView();
1115   updateUndoRedo();
1116
1117   emit curveModified();
1118 }
1119
1120 /**
1121  * Manage mouse move events in Modification mode
1122  * \param theWindow an owner of the signal
1123  * \param theEvent a mouse event
1124  */
1125 void CurveCreator_Widget::onMouseMove( SUIT_ViewWindow*, QMouseEvent* theEvent )
1126 {
1127   if ( getActionMode() != ModificationMode || !myDragStarted )
1128     return;
1129
1130   QPoint aPos = theEvent->pos();
1131   if ( (aPos - myDragStartPosition).manhattanLength() < QApplication::startDragDistance() )
1132     return;
1133
1134   START_MEASURE_TIME;
1135
1136   moveSelectedPoints( aPos.x(), aPos.y() );
1137   myDragStartPosition = aPos;
1138
1139   END_MEASURE_TIME( "drag" );
1140 }
1141
1142 /**
1143  * Set zero viewer by the last view closed in
1144  * \param theManager a viewer manager
1145  */
1146 void CurveCreator_Widget::onLastViewClosed( SUIT_ViewManager* theManager )
1147 {
1148   myOCCViewer = 0;
1149 }
1150
1151 void CurveCreator_Widget::onMousePress( QMouseEvent* theEvent )
1152 {
1153   onMousePress( 0, theEvent );
1154 }
1155
1156 void CurveCreator_Widget::onMouseRelease( QMouseEvent* theEvent )
1157 {
1158   onMouseRelease( 0, theEvent );
1159 }
1160
1161 void CurveCreator_Widget::onMouseMove( QMouseEvent* theEvent )
1162 {
1163   onMouseMove( 0, theEvent );
1164 }
1165
1166 void CurveCreator_Widget::onCellChanged( int theRow, int theColumn )
1167 {
1168   int aCurrSect = myLocalPointView->getSectionId( theRow );
1169   int aPntIndex = myLocalPointView->getPointId( theRow );
1170
1171   if ( aPntIndex < 0 )
1172     return;
1173
1174   CurveCreator_ICurve::SectionToPointList aSelPoints;
1175   startCurveModification( aSelPoints );
1176
1177   double aX  = myLocalPointView->item( theRow, 2 )->data( Qt::UserRole ).toDouble();
1178   double anY = myLocalPointView->item( theRow, 3 )->data( Qt::UserRole ).toDouble();
1179   std::deque<float> aChangedPos;
1180   aChangedPos.push_back( aX );
1181   aChangedPos.push_back( anY );
1182   myCurve->setPoint( aCurrSect, aPntIndex, aChangedPos );
1183
1184   finishCurveModification( aSelPoints );
1185
1186   emit curveModified();
1187 }
1188
1189 /**
1190  * Removes a selected section from the curve. Updates undo/redo status
1191  */
1192 void CurveCreator_Widget::removeSection()
1193 {
1194   stopActionMode();
1195
1196   QList<int> aSections = mySectionView->getSelectedSections();
1197   for( int i = 0 ; i < aSections.size() ; i++ ){
1198     int aSectNum = aSections[i] - (i);
1199     myCurve->removeSection( aSectNum );
1200     mySectionView->sectionsRemoved( aSectNum );
1201   }
1202   mySectionView->clearSelection();
1203   updateUndoRedo();
1204
1205   emit curveModified();
1206 }
1207
1208 /**
1209  * Removes a selected points from the curve. Updates undo/redo status
1210  */
1211 void CurveCreator_Widget::removePoint()
1212 {
1213   CurveCreator_ICurve::SectionToPointList aPoints;
1214   getSelectedPoints( aPoints );
1215   if ( aPoints.size() == 0 )
1216     return;
1217
1218   CurveCreator_ICurve::SectionToPointList aSelPoints;
1219   startCurveModification( aSelPoints, false );
1220
1221   myCurve->removeSeveralPoints( aPoints );
1222   finishCurveModification( CurveCreator_ICurve::SectionToPointList() );
1223   mySectionView->reset();
1224
1225   emit curveModified();
1226 }
1227
1228 void CurveCreator_Widget::addNewPoint(const CurveCreator::Coordinates& theCoords)
1229 {
1230   if( !myCurve )
1231     return;
1232   QList<int> aSections = mySectionView->getSelectedSections();
1233   if( aSections.size() == 0 ){
1234     return;
1235   }
1236   int aSection = aSections[0];
1237   myCurve->addPoints(theCoords, aSection); // add to the end of section
1238   mySectionView->pointsAdded( aSection, myCurve->getNbPoints( aSection ) );
1239   updateActionsStates();
1240   updateUndoRedo();
1241
1242   emit curveModified();
1243 }
1244
1245 void CurveCreator_Widget::insertPointToSelectedSegment( const int theX,
1246                                                         const int theY )
1247 {
1248   Handle(AIS_InteractiveContext) aContext = getAISContext();
1249
1250   OCCViewer_ViewPort3d* aViewPort = getViewPort();
1251   Handle(V3d_View) aView;
1252   if ( aViewPort )
1253     aView = aViewPort->getView();
1254
1255   if ( aContext.IsNull() || aView.IsNull() )
1256     return;
1257   gp_Pnt aPoint;
1258   gp_Pnt aPoint1, aPoint2;
1259   Handle(AIS_InteractiveObject) anAISObject = myCurve->getAISObject();
1260   bool isFoundPoint = CurveCreator_Utils::pointOnObject( aView, anAISObject, theX, theY,
1261                                                          aPoint, aPoint1, aPoint2 );
1262   if ( !isFoundPoint )
1263     return;
1264
1265   // insert the point to the model curve
1266   CurveCreator_ICurve::SectionToPointList aSelPoints;
1267   startCurveModification( aSelPoints );
1268
1269   CurveCreator::Coordinates aCoords;
1270   aCoords.push_back( aPoint.X() );
1271   aCoords.push_back( aPoint.Y() );
1272
1273   CurveCreator_ICurve::SectionToPointList aPoints1, aPoints2;
1274   findSectionsToPoints( aPoint1.X(), aPoint1.Y(), aPoints1 );
1275   findSectionsToPoints( aPoint2.X(), aPoint2.Y(), aPoints2 );
1276   CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aPoints1.begin(),
1277                                                           aLast = aPoints1.end();
1278   int aSectionId = -1;
1279   // find the indices of the neighbour point
1280   // there can be a case when a new point is added into two sections
1281   int aPoint1Id = -1, aPoint2Id = -1;
1282   for ( ; anIt != aLast && aSectionId < 0; anIt++ ) {
1283     int aSectionCur = anIt->first;
1284     CurveCreator_ICurve::SectionToPointList::const_iterator anIt2 = aPoints2.begin(),
1285                                                             aLast2 = aPoints2.end();
1286     for ( ; anIt2 != aLast2 && aSectionId < 0; anIt2++ ) {
1287       if ( anIt2->first == aSectionCur ) {
1288         aSectionId = aSectionCur;
1289         aPoint1Id = anIt->second;
1290         aPoint2Id = anIt2->second;
1291       }
1292     }
1293   }
1294
1295   int anInsertPos = -1;
1296   int aLastPoint = myCurve->getNbPoints( aSectionId )-1; 
1297   if ( ( aPoint1Id == aLastPoint && aPoint2Id == 0 ) ||
1298        ( aPoint2Id == aLastPoint && aPoint1Id == 0 ) )
1299     anInsertPos = -1; // if the section happens between first and last points
1300   else
1301     anInsertPos = aPoint1Id < aPoint2Id ? aPoint1Id + 1 : aPoint2Id + 1;
1302
1303   myCurve->addPoints( aCoords, aSectionId, anInsertPos );
1304   mySectionView->pointsAdded( aSectionId, myCurve->getNbPoints( aSectionId ) );
1305
1306   finishCurveModification( aSelPoints );
1307
1308   setSelectedPoints();
1309
1310   emit curveModified();
1311 }
1312
1313 void CurveCreator_Widget::moveSelectedPoints( const int theXPosition,
1314                                               const int theYPosition )
1315 {
1316   OCCViewer_ViewPort3d* aViewPort = getViewPort();
1317   if ( !aViewPort )
1318     return;
1319
1320   CurveCreator_ICurve::SectionToPointList aPoints;
1321   startCurveModification( aPoints, false );
1322
1323   gp_Pnt aStartPnt = CurveCreator_Utils::ConvertClickToPoint( myDragStartPosition.x(),
1324                                                               myDragStartPosition.y(),
1325                                                               aViewPort->getView() );
1326   gp_Pnt anEndPnt = CurveCreator_Utils::ConvertClickToPoint( theXPosition, theYPosition,
1327                                                              aViewPort->getView() );
1328   double aXDelta = aStartPnt.X() - anEndPnt.X();
1329   double anYDelta = aStartPnt.Y() - anEndPnt.Y();
1330
1331   CurveCreator_ICurve::SectionToPointCoordsList aCoordList;
1332   std::deque<float> aChangedPos;
1333   CurveCreator_ICurve::SectionToPointList::const_iterator anIt = myDragPoints.begin(),
1334                                                           aLast = myDragPoints.end();
1335   for ( ; anIt != aLast; anIt++ ) {
1336     int aSectionId = anIt->first;
1337     int aPointId = anIt->second;
1338     aChangedPos = myCurve->getPoint( aSectionId, aPointId );
1339     if ( aChangedPos.size() < 2 )
1340       continue;
1341
1342     // Remember drag points coordinates
1343     if ( !myDragged ) {
1344       myInitialDragPointsCoords.insert( std::make_pair( aSectionId, aPointId ), aChangedPos );
1345     }
1346
1347     aChangedPos[0] = aChangedPos[0] - aXDelta;
1348     aChangedPos[1] = aChangedPos[1] - anYDelta;
1349     
1350     aCoordList.push_back(
1351       std::make_pair(std::make_pair( aSectionId, aPointId ), 
1352                      aChangedPos ));
1353   }
1354   myCurve->setSeveralPoints( aCoordList, false );
1355
1356   myDragged = true;
1357   finishCurveModification( myDragPoints );
1358
1359   emit curveModified();
1360 }
1361
1362 void CurveCreator_Widget::updateLocalPointView()
1363 {
1364   if ( myDragStarted )
1365     return;
1366   Handle(AIS_InteractiveContext) aContext = getAISContext();
1367   if ( aContext.IsNull() )
1368     return;
1369
1370   CurveCreator_Utils::getSelectedPoints( aContext, myCurve, myLocalPoints );
1371   int aNbPoints = myLocalPoints.size();
1372
1373   bool isRowLimit = aNbPoints > myLocalPointRowLimit;
1374   myLocalPointView->setVisible( getActionMode() == ModificationMode && !isRowLimit );
1375
1376   if ( !isRowLimit ) {
1377     bool isBlocked = myLocalPointView->blockSignals(true);
1378
1379     myLocalPointView->setLocalPointsToTable( myLocalPoints );
1380
1381     myLocalPointView->blockSignals( isBlocked );
1382   }
1383 }
1384
1385 /**
1386  * 
1387  */
1388 void CurveCreator_Widget::setLocalPointContext( const bool theOpen, const bool isUpdateTable )
1389 {
1390   CurveCreator_Utils::setLocalPointContext( myCurve, getAISContext(), theOpen );
1391   if ( !theOpen && isUpdateTable )
1392     updateLocalPointView();
1393 }
1394
1395 /**
1396  * Set drag operation started. Save the position and a list of dragged points
1397  * \param theState the drag operation state: started/finished
1398  * \param thePoint the start drag position
1399  */
1400 void CurveCreator_Widget::setDragStarted( const bool theState, const QPoint& thePoint )
1401 {
1402   if ( theState ) {
1403     getSelectedPoints( myDragPoints );
1404
1405     myDragStarted = myDragPoints.size();
1406     myDragStartPosition = thePoint;
1407     if ( myDragStarted ) {
1408       // change a viewer interaction style in order to avoid a select rectangle build
1409       myDragInteractionStyle = changeInteractionStyle( SUIT_ViewModel::KEY_FREE );
1410       myCurve->setSkipSorting( true );
1411     }
1412   }
1413   else {
1414     if ( myDragStarted )
1415       changeInteractionStyle( myDragInteractionStyle );
1416     myDragStarted = false;
1417     myDragPoints.clear();
1418     myInitialDragPointsCoords.clear();
1419   }
1420   myDragged = false;
1421 }
1422
1423 void CurveCreator_Widget::getSelectedPoints( CurveCreator_ICurve::SectionToPointList& thePoints )
1424 {
1425   thePoints.clear();
1426   thePoints = myLocalPoints;
1427 }
1428
1429 void CurveCreator_Widget::setSelectedPoints( const CurveCreator_ICurve::SectionToPointList& thePoints )
1430 {
1431   if ( myDragStarted )
1432     return;
1433   Handle(AIS_InteractiveContext) aContext = getAISContext();
1434   if ( aContext.IsNull() || !aContext->HasOpenedContext() )
1435     return;
1436
1437   CurveCreator_Utils::setSelectedPoints( aContext, myCurve, thePoints );
1438
1439   updateLocalPointView();
1440 }
1441
1442 void CurveCreator_Widget::stopActionMode()
1443 {
1444   setActionMode( NoneMode );
1445 }
1446
1447 /**
1448  * Get viewer information before perform the curve modification.
1449  * Take a list of selected cuve points an close local context.
1450  * The context should be closed because the curve presentation is
1451  * redisplayed and if it is not closed, when we close the local context
1452  * later, the presentation shown in the local context is disappeared.
1453  * \param thePoints an output list of curve selected points
1454  * \param theFillPoints a flag whether the selection list should be filled
1455  */
1456 void CurveCreator_Widget::startCurveModification(
1457                            CurveCreator_ICurve::SectionToPointList& thePoints,
1458                            const bool theFillPoints )
1459 {
1460   if ( theFillPoints ) {
1461     thePoints.clear();
1462     getSelectedPoints( thePoints );
1463   }
1464   setLocalPointContext( false );
1465 }
1466
1467 /**
1468  * Restore the viewer state after the curve modification is done.
1469  * Open local context and select given points inside it.
1470  * \param thePoints a list of curve selected points
1471  */
1472 void CurveCreator_Widget::finishCurveModification(
1473                            const CurveCreator_ICurve::SectionToPointList& thePoints )
1474 {
1475   if ( getActionMode() == ModificationMode )
1476     setLocalPointContext( true );
1477   setSelectedPoints( thePoints );
1478   updateUndoRedo();
1479 }
1480
1481 /**
1482  * Returns a point index in the model curve by the point coordinates in the viewer
1483  * \param theX the X coordinate of the point
1484  * \param theY the Y coordinate of the point
1485  */
1486 int CurveCreator_Widget::findLocalPointIndex( int theSectionId, float theX, float theY )
1487 {
1488   return CurveCreator_UtilsICurve::findLocalPointIndex( myCurve, theSectionId, theX, theY );
1489 }
1490
1491 void CurveCreator_Widget::findSectionsToPoints( const double theX, const double theY,
1492                                  CurveCreator_ICurve::SectionToPointList& thePoints )
1493 {
1494   return CurveCreator_UtilsICurve::findSectionsToPoints( myCurve, theX, theY, thePoints );
1495 }
1496
1497 void CurveCreator_Widget::convert( const CurveCreator_ICurve::SectionToPointList& thePoints,
1498                                    QMap<int, QList<int> >& theConvPoints )
1499 {
1500   return CurveCreator_UtilsICurve::convert( thePoints, theConvPoints );
1501 }
1502
1503 /**
1504  * Returns whethe the container has the value
1505  * \param theList a container of values
1506  * \param theValue a value
1507  */
1508 bool CurveCreator_Widget::contains( const CurveCreator_ICurve::SectionToPointList& theList,
1509                                     const CurveCreator_ICurve::SectionToPoint& theValue ) const
1510 {
1511   return CurveCreator_UtilsICurve::contains( theList, theValue );
1512 }