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