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