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