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