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