]> SALOME platform Git repositories - modules/geom.git/blob - src/CurveCreator/CurveCreator_Widget.cxx
Salome HOME
0023357: EDF 13600 - Problem of partition
[modules/geom.git] / src / CurveCreator / CurveCreator_Widget.cxx
1 // Copyright (C) 2013-2016  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, or (at your option) any later version.
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_NewSectionDlg.h"
23 #include "CurveCreator_Utils.hxx"
24 #include "CurveCreator_UtilsICurve.hxx"
25 #include "CurveCreator_TableView.h"
26
27 #include <SUIT_Session.h>
28 #include <SUIT_Desktop.h>
29 #include <SUIT_ResourceMgr.h>
30 #include <SUIT_ViewManager.h>
31
32 #include <OCCViewer_ViewManager.h>
33 #include <OCCViewer_ViewPort3d.h>
34 #include <OCCViewer_Utilities.h>
35
36 #include <QHBoxLayout>
37 #include <QVBoxLayout>
38 #include <QLabel>
39 #include <QLineEdit>
40 #include <QGroupBox>
41 #include <QToolButton>
42 #include <QToolBar>
43 #include <QAction>
44 #include <QMenu>
45 #include <QMouseEvent>
46 #include <QApplication>
47 #include <QTableWidget>
48 #include <QTime>
49 #include <QSplitter>
50
51 //#define MEASURE_TIME
52
53 #ifdef MEASURE_TIME
54
55   #define START_MEASURE_TIME \
56     QTime aTimer;            \
57     aTimer.start();          \
58
59   #define END_MEASURE_TIME( theMsg )                      \
60     double aTime = aTimer.elapsed() * 0.001;              \
61     FILE* aFile = fopen( "performance", "a" );            \
62     fprintf( aFile, "%s = %.3lf sec\n", theMsg, aTime );  \
63     fclose( aFile );                                      \
64
65 #else
66
67   #define START_MEASURE_TIME
68   #define END_MEASURE_TIME( theMsg )
69
70 #endif
71
72
73
74
75
76
77 CurveCreator_Widget::CurveCreator_Widget(QWidget* parent,
78                                          CurveCreator_ICurve *theCurve,
79                                          const int theActionFlags,
80                                          const QStringList& theCoordTitles,
81                                          Qt::WindowFlags fl,
82                                          int theLocalPointRowLimit )
83 : QWidget(parent), myNewSectionEditor(NULL), myCurve(theCurve), mySection(0),
84   myDragStarted( false ), myDragInteractionStyle( SUIT_ViewModel::STANDARD ),
85   myOCCViewer( 0 ), myLocalPointRowLimit( theLocalPointRowLimit ),
86   myOld2DMode(OCCViewer_ViewWindow::No2dMode)
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("SECTION_GROUP_TITLE"),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, theCoordTitles );
104   connect( myLocalPointView, SIGNAL( cellChanged( int, int ) ),
105            this, SLOT( onCellChanged( int, int ) ) );
106
107   QToolBar* aTB = new QToolBar(tr("SECTION_GROUP_TITLE"), 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_SECTIONS_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_SECTIONS_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
209   QSplitter* aSplitter = new QSplitter(aSectionGroup);
210   aSplitter->setOrientation(Qt::Vertical);
211
212   aSplitter->addWidget(mySectionView);
213   aSplitter->addWidget( myLocalPointView );
214
215   aSectLayout->addWidget(aSplitter);
216   aSectionGroup->setLayout(aSectLayout);
217   QVBoxLayout* aLay = new QVBoxLayout();
218   aLay->setMargin( 0 );
219   aLay->setSpacing( 5 );
220 //    aLay->addLayout(aNameLayout);
221   aLay->addWidget(aSectionGroup);
222   setLayout(aLay);
223
224   updateActionsStates();
225   updateUndoRedo();
226 }
227
228 /**
229  * Set an OCC viewer
230  */
231 void CurveCreator_Widget::setOCCViewer( OCCViewer_Viewer* theViewer )
232 {
233   if ( myOCCViewer == theViewer )
234     return;
235
236   if ( myOCCViewer ) {
237     OCCViewer_ViewManager* aViewManager = dynamic_cast<OCCViewer_ViewManager*>
238                                                     ( myOCCViewer->getViewManager() );
239     disconnect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
240            this, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ) );
241     disconnect( aViewManager, SIGNAL( mouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ),
242            this, SLOT( onMouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ) );
243     disconnect( aViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
244            this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
245     disconnect( aViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
246            this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
247     // restore normal mode in the viewer
248     SetViewer2DMode(false);
249     // all local contexts should be closed if the viewer is not more used
250     setLocalPointContext( false, true );
251   }
252
253   myOCCViewer = theViewer;
254   if ( myOCCViewer ) {
255     OCCViewer_ViewManager* aViewManager = dynamic_cast<OCCViewer_ViewManager*>
256                                                     ( myOCCViewer->getViewManager() );
257     connect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
258            this, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ) );
259     connect( aViewManager, SIGNAL( mouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ),
260            this, SLOT( onMouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ) );
261     connect( aViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
262            this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
263     connect( aViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
264            this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
265     SetViewer2DMode(true);
266   }
267 }
268
269 /**
270  * Returns current OCC viewer
271  */
272 OCCViewer_Viewer* CurveCreator_Widget::getOCCViewer()
273 {
274   return myOCCViewer;
275 }
276
277 /**
278  * Returns OCC viewer context
279  */
280 Handle(AIS_InteractiveContext) CurveCreator_Widget::getAISContext()
281 {
282   Handle(AIS_InteractiveContext) aContext;
283   OCCViewer_Viewer* aViewer = getOCCViewer();
284   if ( aViewer )
285     aContext = aViewer->getAISContext();
286
287   return aContext;
288 }
289
290 /**
291  * Returns OCC viewer view port
292  */
293 OCCViewer_ViewPort3d* CurveCreator_Widget::getViewPort()
294 {
295   OCCViewer_ViewPort3d* aViewPort = 0;
296   OCCViewer_Viewer* aViewer = getOCCViewer();
297   if ( aViewer )
298     aViewPort = ((OCCViewer_ViewWindow*)aViewer->getViewManager()->getActiveView())->getViewPort();
299     
300   return aViewPort;
301 }
302
303 /**
304  * Set interaction style in the OCC viewer
305  * \param theStyle a new style
306  * \return the previous style
307  */
308 int CurveCreator_Widget::changeInteractionStyle( int theStyle )
309 {
310   OCCViewer_Viewer* aViewer = getOCCViewer();
311   if ( !aViewer )
312     return -1;
313
314   int aPrevStyle = aViewer->interactionStyle();
315   aViewer->setInteractionStyle( theStyle );
316
317   return aPrevStyle;
318 }
319
320 //=======================================================================
321 // function: reset
322 // purpose: reset the widget viewer, close local context, clear selection
323 //=======================================================================
324 void CurveCreator_Widget::reset()
325 {
326   stopActionMode();
327 }
328
329 void CurveCreator_Widget::setCurve( CurveCreator_ICurve* theCurve )
330 {
331   myCurve = theCurve;
332   mySectionView->setCurve( myCurve );
333   myLocalPointView->setCurve( myCurve );
334   updateActionsStates();
335   updateUndoRedo();
336 }
337
338 void CurveCreator_Widget::onSelectionChanged()
339 {
340   updateActionsStates();
341   updateUndoRedo();
342   emit selectionChanged();
343 }
344
345 void CurveCreator_Widget::updateActionsStates()
346 {
347   QList<ActionId> anEnabledAct;
348   if( myCurve ){
349     anEnabledAct << NEW_SECTION_ID << MODIFICATION_MODE_ID;
350     if ( removeEnabled() )
351       anEnabledAct << REMOVE_ID;
352     QList<int> aSelSections = mySectionView->getSelectedSections();
353     CurveCreator_TreeView::SelectionType aSelType = mySectionView->getSelectionType();
354     switch( aSelType ){
355     case CurveCreator_TreeView::ST_NOSEL:{
356       break;
357     }
358     case CurveCreator_TreeView::ST_SECTIONS:{
359       /*if( aSelSections[0] > 0 ){
360         anEnabledAct << UP_ID;
361       }*/
362       if( aSelSections.size() == 1 ){
363         anEnabledAct << ADDITION_MODE_ID << DETECTION_MODE_ID;
364       }
365       switch ( getActionMode() ) {
366         case AdditionMode: {
367           mySection = -1;
368           myPointNum = -1;
369           QList<int> aSelSection = mySectionView->getSelectedSections();
370           if( aSelSection.size() > 0 ){
371             mySection = aSelSection[0];
372             myPointNum = myCurve->getNbPoints(mySection);
373           }
374         }
375         break;
376         case ModificationMode: {
377           if ( myNewSectionEditor->isEnableClosed() )
378             anEnabledAct << CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID;
379           anEnabledAct << SET_SECTIONS_POLYLINE_ID << SET_SECTIONS_SPLINE_ID;
380           int aSectCnt = myCurve->getNbSections();
381           if( aSectCnt > 0 )
382             anEnabledAct << CLEAR_ALL_ID;
383           if( aSectCnt > 1 )
384             anEnabledAct << JOIN_ALL_ID;
385           if( aSelSections.size() > 1 ){
386             anEnabledAct << JOIN_ID;
387           }
388         }
389         break;
390         case DetectionMode: {
391         }
392         break;
393         case NoneMode:
394           {
395             int aSectCnt = myCurve->getNbSections();
396             if( aSectCnt > 1 )
397               anEnabledAct << JOIN_ALL_ID;
398             if( aSelSections.size() > 1 )
399               anEnabledAct << JOIN_ID;
400           }
401           break;
402         default:
403         break;
404       }
405       /*if( aSelSections[ aSelSections.size() - 1 ] < ( myCurve->getNbSections() - 1 ) ){
406         anEnabledAct << DOWN_ID;
407       }*/
408       break;
409     }
410     /*case CurveCreator_TreeView::ST_POINTS_ONE_SECTION:{
411       if( aSelPoints[0].second > 0 ){
412         anEnabledAct << UP_ID;
413       }
414       int aLastIndex = aSelPoints.size()-1;
415       int aSect = aSelPoints[0].first;
416       if( aSelPoints[aLastIndex].second < (myCurve->getNbPoints(aSect) - 1)){
417         anEnabledAct << DOWN_ID;
418       }
419       if( aSelPoints.size() == 1){
420         anEnabledAct << INSERT_POINT_BEFORE_ID << INSERT_POINT_AFTER_ID;
421       }
422       break;
423     }*/
424
425     }
426     
427     /*int aSelObjsCnt = aSelPoints.size() + aSelSections.size();
428     if( aSelObjsCnt > 0 ){
429       anEnabledAct << REMOVE_ID;
430     }
431     if( (myCurve->getNbSections() + myCurve->getNbPoints()) > 0 ){
432       anEnabledAct << REMOVE_ALL_ID;
433     }*/
434     if( myCurve->getNbSections() > 1 ){
435       anEnabledAct << JOIN_ALL_ID;
436     }
437   }
438   QList<ActionId> anIds = myActionMap.keys();
439   for( int i = 0 ; i < anIds.size() ; i++ ){
440     if( myActionMap.contains(anIds[i]) ){
441       if( anEnabledAct.contains(anIds[i]) ){
442         myActionMap[anIds[i]]->setEnabled(true);
443       }
444       else{
445         myActionMap[anIds[i]]->setEnabled(false);
446       }
447     }
448   }
449 }
450
451 void CurveCreator_Widget::onAdditionMode(bool checked)
452 {
453   if (!checked)
454     return;
455
456   Handle(AIS_InteractiveContext) aContext = getAISContext();
457   if( !myCurve || aContext.IsNull() )
458     return;
459
460   mySection= -1;
461   myPointNum = -1;
462   QList<int> aSelSection = mySectionView->getSelectedSections();
463   if( aSelSection.size() > 0 ){
464     mySection = aSelSection[0];
465   }
466 //  emit subOperationStarted( myNewPointEditor );
467 }
468
469 void CurveCreator_Widget::onModificationMode(bool checked)
470 {
471   myLocalPointView->setVisible( checked );
472 }
473
474 void CurveCreator_Widget::onDetectionMode(bool checked)
475 {
476 }
477
478 void CurveCreator_Widget::onModeChanged(bool checked)
479 {
480   ActionMode aMode = NoneMode;
481   if (checked) {
482     QAction* anAction = (QAction*)sender();
483     switch(myActionMap.key(anAction)) {
484       case ADDITION_MODE_ID:
485         aMode = AdditionMode;
486         if (myActionMap[MODIFICATION_MODE_ID]->isChecked())
487           myActionMap[MODIFICATION_MODE_ID]->trigger();
488         else if (myActionMap[DETECTION_MODE_ID]->isChecked())
489           myActionMap[DETECTION_MODE_ID]->trigger();
490         break;
491       case MODIFICATION_MODE_ID:
492         aMode = ModificationMode;
493         if (myActionMap[ADDITION_MODE_ID]->isChecked())
494           myActionMap[ADDITION_MODE_ID]->trigger();
495         else if (myActionMap[DETECTION_MODE_ID]->isChecked())
496           myActionMap[DETECTION_MODE_ID]->trigger();
497         break;
498       case DETECTION_MODE_ID:
499         aMode = DetectionMode;
500         if (myActionMap[ADDITION_MODE_ID]->isChecked())
501           myActionMap[ADDITION_MODE_ID]->trigger();
502         else if (myActionMap[MODIFICATION_MODE_ID]->isChecked())
503           myActionMap[MODIFICATION_MODE_ID]->trigger();
504         break;
505     }
506   }
507   updateActionsStates();
508   updateUndoRedo();
509   setLocalPointContext( aMode == ModificationMode, true );
510 }
511
512 void CurveCreator_Widget::onNewSection()
513 {
514   if( !myCurve )
515     return;
516
517   stopActionMode();
518   myNewSectionEditor->clear();
519   myNewSectionEditor->setEditMode(false);
520   QString aSectName = QString( CurveCreator_UtilsICurve::getUniqSectionName( myCurve ).c_str() );
521   myNewSectionEditor->setSectionParameters(aSectName, true, CurveCreator::Polyline );
522   emit subOperationStarted( myNewSectionEditor, false );
523 }
524
525 void CurveCreator_Widget::onAddNewSection()
526 {
527   if( !myCurve )
528     return;
529   myCurve->addSection( myNewSectionEditor->getName().toStdString(),
530                        myNewSectionEditor->getSectionType(),
531                        myNewSectionEditor->isClosed() );
532   mySectionView->sectionAdded( -1 ); // add a new section to the end of list
533   QString aNewName = QString( CurveCreator_UtilsICurve::getUniqSectionName( myCurve ).c_str() );
534   myNewSectionEditor->setSectionName(aNewName);
535   updateActionsStates();
536   updateUndoRedo();
537   onCancelSection();
538 }
539
540 void CurveCreator_Widget::onCancelSection()
541 {
542   emit subOperationFinished( myNewSectionEditor );
543 }
544
545 QAction* CurveCreator_Widget::createAction( ActionId theId, const QString& theName, const QPixmap& theImage,
546                                             const QString& theToolTip, const QKeySequence& theShortcut )
547 {
548   QAction* anAct = new QAction(theName,this);
549   if( !theImage.isNull() ){
550     anAct->setIcon(theImage);
551   }
552   anAct->setShortcut(theShortcut);
553   anAct->setToolTip(theToolTip);
554   myActionMap[theId] = anAct;
555   return anAct;
556 }
557
558 QAction* CurveCreator_Widget::getAction( ActionId theId )
559 {
560   if( myActionMap.contains(theId) )
561     return myActionMap[theId];
562   return NULL;
563 }
564
565 QAction* CurveCreator_Widget::getAction( ActionMode theMode )
566 {
567   ActionId anActionId = NONE_ID;
568   switch ( theMode ) {
569     case AdditionMode:
570       anActionId = ADDITION_MODE_ID;
571       break;
572     case ModificationMode:
573       anActionId = MODIFICATION_MODE_ID;
574       break;
575     case DetectionMode:
576       anActionId = DETECTION_MODE_ID;
577       break;
578     default:
579       break;
580   }
581   QAction* anAction = 0;
582   if ( anActionId != NONE_ID && myActionMap.contains( anActionId ) )
583     anAction = myActionMap[anActionId];
584   return anAction;
585 }
586
587 void CurveCreator_Widget::onEditSection( int theSection )
588 {
589   if( !myCurve )
590     return;
591   
592   stopActionMode();
593   mySection = theSection;
594   QString aSectName = QString::fromStdString( myCurve->getSectionName(theSection));
595   bool isClosed = myCurve->isClosed(theSection);
596   CurveCreator::SectionType aType = myCurve->getSectionType(theSection);
597   myNewSectionEditor->setEditMode(true);
598   myNewSectionEditor->setSectionParameters( aSectName, isClosed, aType );
599
600   emit subOperationStarted( myNewSectionEditor, true );
601 }
602
603 void CurveCreator_Widget::onModifySection()
604 {
605   if( !myCurve )
606     return;
607   QString aName = myNewSectionEditor->getName();
608   bool isClosed = myNewSectionEditor->isClosed();
609   CurveCreator::SectionType aSectType = myNewSectionEditor->getSectionType();
610   if( myCurve->getSectionName(mySection) != aName.toStdString() )
611     myCurve->setSectionName( mySection , aName.toStdString() );
612
613   bool isGeomModified = false;
614
615   if( myCurve->getSectionType(mySection) != aSectType ) {
616     myCurve->setSectionType( mySection, aSectType );
617     isGeomModified = true;
618   }
619
620   if( myCurve->isClosed(mySection) != isClosed ) {
621     myCurve->setClosed( mySection, isClosed );
622     isGeomModified = true;
623   }
624   mySectionView->sectionChanged(mySection);
625   updateUndoRedo();
626   onCancelSection();
627 }
628
629 void CurveCreator_Widget::onJoin()
630 {
631   if( !myCurve )
632     return;
633   QList<int> aSections = mySectionView->getSelectedSections();
634   if( aSections.size() == 0 ){
635     return;
636   }
637   stopActionMode();
638
639   std::list<int> aSectionsToJoin;
640   for( int i = 0; i < aSections.size() ; i++ ){
641     aSectionsToJoin.push_back( aSections[i] );
642   }
643   //int aMainSect = aSectionsToJoin.front();
644   //int aMainSectSize = myCurve->getNbPoints(aMainSect);
645   if ( myCurve->join( aSectionsToJoin ) )
646   {
647     std::list<int>::const_iterator anIt = aSectionsToJoin.begin(),
648                                    aLast = aSectionsToJoin.end();
649     // the first section should be skipped. It is not removed, but is modified
650     anIt++;
651     for ( ; anIt != aLast; anIt++ )
652       mySectionView->sectionsRemoved( *anIt );
653   }
654
655   /* The update for the points of the main section
656   int aNewSectSize = myCurve->getNbPoints(aMainSect);
657   if( aNewSectSize != aMainSectSize )
658     mySectionView->pointsAdded( aMainSect, aMainSectSize, aNewSectSize-aMainSectSize );*/
659   updateUndoRedo();
660 }
661
662 void CurveCreator_Widget::onRemove()
663 {
664   if( !myCurve )
665     return;
666
667   switch( getActionMode() ) {
668     case NoneMode:
669       removeSection();
670     break;
671     case ModificationMode:
672       removePoint();
673     break;
674     default:
675       break;
676   }
677 }
678
679 void CurveCreator_Widget::onClearAll()
680 {
681   if( !myCurve )
682     return;
683   stopActionMode();
684   myCurve->clear();
685   mySectionView->reset();
686   updateActionsStates();
687   updateUndoRedo();
688 }
689
690 void CurveCreator_Widget::onJoinAll()
691 {
692   if( !myCurve )
693     return;
694   stopActionMode();
695
696   std::list<int> aSectionsToJoin;
697   for( int i = 0, aNb = myCurve->getNbSections(); i < aNb ; i++ ){
698     aSectionsToJoin.push_back( i );
699   }
700   bool aRes = myCurve->join( aSectionsToJoin );
701
702   mySectionView->reset();
703   updateActionsStates();
704   updateUndoRedo();
705 }
706
707 void CurveCreator_Widget::onUndoSettings()
708 {
709
710 }
711
712 void CurveCreator_Widget::onSetSpline()
713 {
714   if( !myCurve )
715     return;
716   stopActionMode();
717   QList<int> aSelSections = mySectionView->getSelectedSections();
718   for( int i = 0 ; i < aSelSections.size() ; i++ ){
719     myCurve->setSectionType(aSelSections[i], CurveCreator::Spline );
720     mySectionView->sectionChanged(aSelSections[i]);
721   }
722   updateUndoRedo();
723 }
724
725 void CurveCreator_Widget::onSetPolyline()
726 {
727   if( !myCurve )
728     return;
729   stopActionMode();
730   QList<int> aSelSections = mySectionView->getSelectedSections();
731   for( int i = 0 ; i < aSelSections.size() ; i++ ){
732     myCurve->setSectionType( aSelSections[i], CurveCreator::Polyline );
733     mySectionView->sectionChanged( aSelSections[i] );
734   }
735   updateUndoRedo();
736 }
737
738 void CurveCreator_Widget::onCloseSections()
739 {
740   if( !myCurve )
741     return;
742   stopActionMode();
743   QList<int> aSelSections = mySectionView->getSelectedSections();
744   for( int i = 0 ; i < aSelSections.size() ; i++ ){
745     myCurve->setClosed(aSelSections[i], true);
746     mySectionView->sectionChanged(aSelSections[i]);
747   }
748   updateUndoRedo();
749 }
750
751 void CurveCreator_Widget::onUncloseSections()
752 {
753   if( !myCurve )
754     return;
755   stopActionMode();
756   QList<int> aSelSections = mySectionView->getSelectedSections();
757   for( int i = 0 ; i < aSelSections.size() ; i++ ){
758     myCurve->setClosed(aSelSections[i], false);
759     mySectionView->sectionChanged(aSelSections[i]);
760   }
761   updateUndoRedo();
762 }
763
764 void CurveCreator_Widget::onUndo()
765 {
766   if( !myCurve )
767     return;
768
769   CurveCreator_ICurve::SectionToPointList aPoints;
770   startCurveModification( aPoints, false );
771   myCurve->undo();
772   finishCurveModification();
773   mySectionView->reset();
774 }
775
776 void CurveCreator_Widget::onRedo()
777 {
778   if( !myCurve )
779     return;
780   CurveCreator_ICurve::SectionToPointList aPoints;
781   startCurveModification( aPoints, false );
782   myCurve->redo();
783   finishCurveModification();
784   mySectionView->reset();
785 }
786
787 void CurveCreator_Widget::updateUndoRedo()
788 {
789   if( !myCurve )
790     return;
791   QAction* anAct = myActionMap[UNDO_ID];
792   if( anAct != 0 ){
793     if( myCurve->getNbUndo() != 0 ){
794       anAct->setEnabled(true);
795     }
796     else{
797       anAct->setDisabled(true);
798     }
799   }
800   anAct = myActionMap[REDO_ID];
801   if( anAct != 0 ){
802     if( myCurve->getNbRedo() != 0 ){
803       anAct->setEnabled(true);
804     }
805     else{
806       anAct->setDisabled(true);
807     }
808   }
809 }
810
811 void CurveCreator_Widget::onContextMenu( QPoint thePoint )
812 {
813   QList<ActionId> aContextActions;
814   aContextActions << CLEAR_ALL_ID << JOIN_ID << JOIN_ALL_ID << SEPARATOR_ID <<
815                      CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID << SET_SECTIONS_POLYLINE_ID <<
816                      SET_SECTIONS_SPLINE_ID;
817   QPoint aGlPoint = mySectionView->mapToGlobal(thePoint);
818   bool isVis = false;
819   QList<ActionId> aResAct;
820   for( int i = 0 ; i < aContextActions.size() ; i++ ){
821     if( aContextActions[i] != SEPARATOR_ID ){
822       if( myActionMap.contains(aContextActions[i]) ){
823         QAction* anAct = myActionMap[aContextActions[i]];
824         if( anAct->isEnabled() ){
825           aResAct << aContextActions[i];
826           isVis = true;
827         }
828       }
829     }
830     else{
831       aResAct << SEPARATOR_ID;
832     }
833   }
834   if( !isVis )
835     return;
836
837   QMenu* aMenu = new QMenu(this);
838   for( int i = 0 ; i < aResAct.size() ; i++ ){
839     if( aResAct[i] == SEPARATOR_ID ){
840       aMenu->addSeparator();
841     }
842     else{
843       QAction* anAct = myActionMap[aResAct[i]];
844       aMenu->insertAction(NULL, anAct);
845     }
846   }
847   aMenu->exec(aGlPoint);
848 }
849
850 QList<int> CurveCreator_Widget::getSelectedSections()
851 {
852   return mySectionView->getSelectedSections();
853 }
854
855 void CurveCreator_Widget::setSelectedSections( const QList<int>& theSections )
856 {
857   mySectionView->setSelectedSections( theSections );
858   updateActionsStates();
859   updateUndoRedo();
860 }
861
862 /**
863  * According to the widget state, performs the remove action
864  */
865 void CurveCreator_Widget::removeSelected()
866 {
867   onRemove();
868 }
869
870 /**
871  * Checks whether there are some selection to be removed
872  */
873 bool CurveCreator_Widget::removeEnabled()
874 {
875   bool isEnabled = getActionMode() == ModificationMode;
876   if ( !isEnabled ) {
877     QList<int> aSelSections = mySectionView->getSelectedSections();
878     CurveCreator_TreeView::SelectionType aSelType = mySectionView->getSelectionType();
879     isEnabled = aSelType == CurveCreator_TreeView::ST_SECTIONS &&
880                 aSelSections.size() == 1;
881   }
882   return isEnabled;
883 }
884
885 void CurveCreator_Widget::setActionMode( const ActionMode& theMode )
886 {
887   ActionMode aPrevMode = getActionMode();
888   QAction* aPrevAction = getAction( aPrevMode );
889   QAction* anAction = getAction( theMode );
890   switch ( theMode ) {
891     case NoneMode:
892     case AdditionMode: {
893       if ( aPrevAction ) {
894         if ( aPrevAction->isChecked() ) {
895           aPrevAction->setChecked( false );
896         }
897       }
898       if ( aPrevMode == ModificationMode )
899         onModificationMode( false );
900       if ( aPrevMode == AdditionMode )
901         onAdditionMode( false );
902
903       if ( theMode == AdditionMode )
904       {
905         anAction->setChecked( true );
906         onModeChanged( true );
907       }
908     }
909     break;
910     break;
911     case ModificationMode:
912     {
913       //TODO
914     }
915     break;
916     case DetectionMode:
917       break;
918   }
919 }
920
921 CurveCreator_Widget::ActionMode CurveCreator_Widget::getActionMode() const
922 {
923   ActionMode aMode = NoneMode;
924
925   if ( myActionMap[ADDITION_MODE_ID]->isChecked() )
926     aMode = AdditionMode;
927   else if ( myActionMap[MODIFICATION_MODE_ID]->isChecked() )
928     aMode = ModificationMode;
929   else if ( myActionMap[DETECTION_MODE_ID]->isChecked() )
930     aMode = DetectionMode;
931
932   return aMode;
933 }
934
935 void CurveCreator_Widget::SetViewer2DMode(const bool To2D)
936 {
937   if (myOCCViewer) {
938     if (To2D) {
939       myOld2DMode = OCCViewer_Utilities::setViewer2DMode
940                     (myOCCViewer, OCCViewer_ViewWindow::XYPlane);
941     } else {
942       OCCViewer_Utilities::setViewer2DMode(myOCCViewer, myOld2DMode);
943     }
944   }
945 }
946
947 //=================================================================================
948 // function : GeometryGUI::addCoordsByClick()
949 // purpose  : Manage mouse press events in Additon mode
950 //=================================================================================
951 void CurveCreator_Widget::addCoordsByClick( QMouseEvent* pe )
952 {
953   if (pe->button() != Qt::LeftButton)
954     return;
955
956   if ( pe->modifiers() != Qt::ControlModifier ) {
957     Handle(AIS_InteractiveContext) ic = getAISContext();
958     if ( ic.IsNull() )
959       return;
960
961     gp_Pnt aPnt;    
962     OCCViewer_ViewPort3d* vp = getViewPort();
963
964     aPnt = CurveCreator_Utils::ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
965
966     // set the coordinates into dialog
967     CurveCreator::Coordinates aCoords;
968     aCoords.push_back( aPnt.X() );
969     aCoords.push_back( aPnt.Y() );
970     if ( myCurve->getDimension() == 3 ) {
971       aCoords.push_back( aPnt.Z() );
972     }
973     addNewPoint(aCoords);
974   }
975 }
976
977 /**
978  * Manage mouse press events
979  * \param theWindow an owner of the signal
980  * \param theEvent a mouse event
981  */
982 void CurveCreator_Widget::onMousePress( SUIT_ViewWindow*, QMouseEvent* theEvent )
983 {
984   if ( theEvent->button() != Qt::LeftButton )
985     return;
986
987   // Initialize the starting point
988   myStartPoint.setX( theEvent->x() );
989   myStartPoint.setY( theEvent->y() );
990
991   switch( getActionMode() ) {
992     case ModificationMode: {
993       //store initial cursor position for Drag&Drop
994       setDragStarted( true, theEvent->pos() );
995       break;
996     }
997     case AdditionMode: {
998       addCoordsByClick( theEvent );
999       break;
1000     }
1001     default:
1002       break;
1003   }
1004 }
1005
1006 /**
1007  * Manage mouse release events in Modification mode
1008  * \param theWindow an owner of the signal
1009  * \param theEvent a mouse event
1010  */
1011 void CurveCreator_Widget::onMouseRelease( SUIT_ViewWindow* theWindow, QMouseEvent* theEvent )
1012 {
1013   ActionMode aMode = getActionMode();
1014   if ( aMode != ModificationMode )
1015   {
1016     // Emit selectionChanged() signal
1017     getOCCViewer()->performSelectionChanged();
1018
1019     if ( aMode == AdditionMode )
1020     {
1021       Handle(AIS_InteractiveContext) aCtx = getAISContext();
1022       if ( !aCtx.IsNull() )
1023         aCtx->ClearSelected();
1024     }
1025     return;
1026   } 
1027   if (theEvent->button() != Qt::LeftButton) return;
1028   if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
1029
1030   // Initialize the ending point
1031   myEndPoint.setX( theEvent->x() );
1032   myEndPoint.setY( theEvent->y() );
1033
1034   bool aHasShift = ( theEvent->modifiers() & Qt::ShiftModifier );
1035
1036   // Highlight detected objects
1037   Handle(AIS_InteractiveContext) aCtx = getAISContext();
1038   if ( !aCtx.IsNull() )
1039   {
1040     OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
1041     if (!aView)
1042       return;
1043
1044     if (!aHasShift)
1045       aCtx->ClearCurrents( false );
1046
1047     Handle(V3d_View) aView3d = aView->getViewPort()->getView();
1048     if ( !aView3d.IsNull() )
1049     {
1050       // Initialize the single selection if start and end points are equal,
1051       // otherwise a rectangular selection.
1052       if ( myStartPoint == myEndPoint )
1053       {
1054         aCtx->MoveTo( myEndPoint.x(), myEndPoint.y(), aView3d );
1055         if ( aHasShift )
1056           aCtx->ShiftSelect();
1057         else
1058           aCtx->Select();
1059       }
1060       else
1061       {
1062         if ( aHasShift )
1063           aCtx->ShiftSelect( myStartPoint.x(), myStartPoint.y(), myEndPoint.x(), myEndPoint.y(),
1064                              aView3d, Standard_False );
1065         else
1066           aCtx->Select( myStartPoint.x(), myStartPoint.y(), myEndPoint.x(), myEndPoint.y(),
1067                         aView3d, Standard_False );
1068       }
1069     }
1070   }
1071
1072   if ( myDragStarted ) {
1073     bool isDragged = myDragged;
1074     CurveCreator_ICurve::SectionToPointList aDraggedPoints;
1075     QMap<CurveCreator_ICurve::SectionToPoint, CurveCreator::Coordinates > anInitialDragPointsCoords;
1076     if ( myDragged ) {
1077       aDraggedPoints = myDragPoints;
1078       anInitialDragPointsCoords = myInitialDragPointsCoords;
1079     }
1080
1081     setDragStarted( false );
1082
1083     if ( aDraggedPoints.size() > 0 ) {
1084       // Collect old coordinates of the dragged points
1085       CurveCreator_ICurve::SectionToPointCoordsList anOldPoints;
1086       foreach ( const CurveCreator_ICurve::SectionToPoint aSectionToPoint, anInitialDragPointsCoords.keys() ) {
1087         CurveCreator::Coordinates aCoords = anInitialDragPointsCoords.value( aSectionToPoint );
1088         anOldPoints.push_back( std::make_pair( aSectionToPoint, aCoords ) );
1089       }
1090
1091       if ( myCurve->canPointsBeSorted() ) {
1092         // Add old coordinates of the curve points (except the dragged points) to the list
1093         for( int aSectionId = 0 ; aSectionId < myCurve->getNbSections() ; aSectionId++ ) {
1094           CurveCreator::Coordinates aCoords;
1095           for ( int aPointId = 0, aNb = myCurve->getNbPoints( aSectionId ); aPointId < aNb; aPointId++ ) {
1096             aCoords = myCurve->getPoint( aSectionId, aPointId );
1097             if ( aCoords.size() < 2 ) {
1098               continue;
1099             }
1100             
1101             CurveCreator_ICurve::SectionToPoint aSectionToPoint = std::make_pair( aSectionId, aPointId );
1102
1103             if ( !anInitialDragPointsCoords.contains( aSectionToPoint ) ) {
1104               anOldPoints.push_back( std::make_pair( aSectionToPoint, aCoords ) );
1105             }
1106           }
1107         }
1108         
1109         // Apply points sorting
1110         CurveCreator_ICurve::SectionToPointList aPoints;
1111         startCurveModification( aPoints, false );
1112
1113         myCurve->setSkipSorting( false );
1114
1115         CurveCreator_ICurve::SectionToPointCoordsList aCoordList;
1116         CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aDraggedPoints.begin(),
1117                                                                 aLast = aDraggedPoints.end();
1118         for ( ; anIt != aLast; anIt++ ) {
1119           int aSectionId = anIt->first;
1120           int aPointId = anIt->second;
1121           CurveCreator::Coordinates aPos = myCurve->getPoint( aSectionId, aPointId );
1122
1123           aCoordList.push_back(
1124             std::make_pair( std::make_pair( aSectionId, aPointId ), aPos ) );
1125         }
1126
1127         myCurve->setSeveralPoints( aCoordList, false );
1128     
1129         finishCurveModification( aDraggedPoints );
1130       } else {
1131         // if the drag of some points has happened, restore the drag selection
1132         START_MEASURE_TIME;
1133         setSelectedPoints( aDraggedPoints );
1134         END_MEASURE_TIME( "drop" );
1135       }
1136
1137       // Save drag difference
1138       myCurve->saveCoordDiff( anOldPoints );
1139     }
1140   }
1141   else // check whether the segment is clicked an a new point should be added to the segment
1142   {
1143     if ( myStartPoint.x() == myEndPoint.x() && myStartPoint.y() == myEndPoint.y() )
1144       insertPointToSelectedSegment( myEndPoint.x(), myStartPoint.y() );
1145   }
1146
1147   // updates the input panel table to show the selected point coordinates
1148   updateLocalPointView();
1149   updateUndoRedo();
1150 }
1151
1152 /**
1153  * Manage mouse move events in Modification mode
1154  * \param theWindow an owner of the signal
1155  * \param theEvent a mouse event
1156  */
1157 void CurveCreator_Widget::onMouseMove( SUIT_ViewWindow*, QMouseEvent* theEvent )
1158 {
1159   if ( getActionMode() != ModificationMode || !myDragStarted )
1160     return;
1161
1162   QPoint aPos = theEvent->pos();
1163   if ( (aPos - myDragStartPosition).manhattanLength() < QApplication::startDragDistance() )
1164     return;
1165
1166   START_MEASURE_TIME;
1167
1168   moveSelectedPoints( aPos.x(), aPos.y() );
1169   myDragStartPosition = aPos;
1170
1171   END_MEASURE_TIME( "drag" );
1172 }
1173
1174 /**
1175  * Set zero viewer by the last view closed in
1176  * \param theManager a viewer manager
1177  */
1178 void CurveCreator_Widget::onLastViewClosed( SUIT_ViewManager* theManager )
1179 {
1180   myOCCViewer = 0;
1181 }
1182
1183 void CurveCreator_Widget::onMousePress( QMouseEvent* theEvent )
1184 {
1185   onMousePress( 0, theEvent );
1186 }
1187
1188 void CurveCreator_Widget::onMouseRelease( QMouseEvent* theEvent )
1189 {
1190   onMouseRelease( 0, theEvent );
1191 }
1192
1193 void CurveCreator_Widget::onMouseMove( QMouseEvent* theEvent )
1194 {
1195   onMouseMove( 0, theEvent );
1196 }
1197
1198 void CurveCreator_Widget::onCellChanged( int theRow, int theColumn )
1199 {
1200   int aCurrSect = myLocalPointView->getSectionId( theRow );
1201   int aPntIndex = myLocalPointView->getPointId( theRow );
1202
1203   if ( aPntIndex < 0 )
1204     return;
1205
1206   CurveCreator_ICurve::SectionToPointList aSelPoints;
1207   startCurveModification( aSelPoints );
1208
1209   double aX  = myLocalPointView->item( theRow, 2 )->data( Qt::UserRole ).toDouble();
1210   double anY = myLocalPointView->item( theRow, 3 )->data( Qt::UserRole ).toDouble();
1211   CurveCreator::Coordinates aChangedPos;
1212   aChangedPos.push_back( aX );
1213   aChangedPos.push_back( anY );
1214   myCurve->setPoint( aCurrSect, aPntIndex, aChangedPos );
1215
1216   finishCurveModification( aSelPoints );
1217 }
1218
1219 /**
1220  * Removes a selected section from the curve. Updates undo/redo status
1221  */
1222 void CurveCreator_Widget::removeSection()
1223 {
1224   stopActionMode();
1225
1226   QList<int> aSections = mySectionView->getSelectedSections();
1227   for( int i = 0 ; i < aSections.size() ; i++ ){
1228     int aSectNum = aSections[i] - (i);
1229     myCurve->removeSection( aSectNum );
1230     mySectionView->sectionsRemoved( aSectNum );
1231   }
1232   mySectionView->clearSelection();
1233   updateUndoRedo();
1234 }
1235
1236 /**
1237  * Removes a selected points from the curve. Updates undo/redo status
1238  */
1239 void CurveCreator_Widget::removePoint()
1240 {
1241   CurveCreator_ICurve::SectionToPointList aPoints;
1242   getSelectedPoints( aPoints );
1243   if ( aPoints.size() == 0 )
1244     return;
1245
1246   CurveCreator_ICurve::SectionToPointList aSelPoints;
1247   startCurveModification( aSelPoints, false );
1248
1249   myCurve->removeSeveralPoints( aPoints );
1250   finishCurveModification( CurveCreator_ICurve::SectionToPointList() );
1251   mySectionView->reset();
1252 }
1253
1254 void CurveCreator_Widget::addNewPoint(const CurveCreator::Coordinates& theCoords)
1255 {
1256   if( !myCurve )
1257     return;
1258   QList<int> aSections = mySectionView->getSelectedSections();
1259   if( aSections.size() == 0 ){
1260     return;
1261   }
1262   int aSection = aSections[0];
1263   myCurve->addPoints(theCoords, aSection); // add to the end of section
1264   mySectionView->pointsAdded( aSection, myCurve->getNbPoints( aSection ) );
1265   updateActionsStates();
1266   updateUndoRedo();
1267 }
1268
1269 void CurveCreator_Widget::insertPointToSelectedSegment( const int theX,
1270                                                         const int theY )
1271 {
1272   Handle(AIS_InteractiveContext) aContext = getAISContext();
1273
1274   OCCViewer_ViewPort3d* aViewPort = getViewPort();
1275   Handle(V3d_View) aView;
1276   if ( aViewPort )
1277     aView = aViewPort->getView();
1278
1279   if ( aContext.IsNull() || aView.IsNull() )
1280     return;
1281   gp_Pnt aPoint;
1282   gp_Pnt aPoint1, aPoint2;
1283   Handle(AIS_InteractiveObject) anAISObject = myCurve->getAISObject();
1284   bool isFoundPoint = CurveCreator_Utils::pointOnObject( aView, anAISObject, theX, theY,
1285                                                          aPoint, aPoint1, aPoint2 );
1286   if ( !isFoundPoint )
1287     return;
1288
1289   // insert the point to the model curve
1290   CurveCreator_ICurve::SectionToPointList aSelPoints;
1291   startCurveModification( aSelPoints );
1292
1293   CurveCreator::Coordinates aCoords;
1294   aCoords.push_back( aPoint.X() );
1295   aCoords.push_back( aPoint.Y() );
1296
1297   CurveCreator_ICurve::SectionToPointList aPoints1, aPoints2;
1298   findSectionsToPoints( aPoint1.X(), aPoint1.Y(), aPoints1 );
1299   findSectionsToPoints( aPoint2.X(), aPoint2.Y(), aPoints2 );
1300   CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aPoints1.begin(),
1301                                                           aLast = aPoints1.end();
1302   int aSectionId = -1;
1303   // find the indices of the neighbour point
1304   // there can be a case when a new point is added into two sections
1305   int aPoint1Id = -1, aPoint2Id = -1;
1306   for ( ; anIt != aLast && aSectionId < 0; anIt++ ) {
1307     int aSectionCur = anIt->first;
1308     CurveCreator_ICurve::SectionToPointList::const_iterator anIt2 = aPoints2.begin(),
1309                                                             aLast2 = aPoints2.end();
1310     for ( ; anIt2 != aLast2 && aSectionId < 0; anIt2++ ) {
1311       if ( anIt2->first == aSectionCur ) {
1312         aSectionId = aSectionCur;
1313         aPoint1Id = anIt->second;
1314         aPoint2Id = anIt2->second;
1315       }
1316     }
1317   }
1318
1319   int anInsertPos = -1;
1320   int aLastPoint = myCurve->getNbPoints( aSectionId )-1; 
1321   if ( ( aPoint1Id == aLastPoint && aPoint2Id == 0 ) ||
1322        ( aPoint2Id == aLastPoint && aPoint1Id == 0 ) )
1323     anInsertPos = -1; // if the section happens between first and last points
1324   else
1325     anInsertPos = aPoint1Id < aPoint2Id ? aPoint1Id + 1 : aPoint2Id + 1;
1326
1327   myCurve->addPoints( aCoords, aSectionId, anInsertPos );
1328   mySectionView->pointsAdded( aSectionId, myCurve->getNbPoints( aSectionId ) );
1329
1330   finishCurveModification( aSelPoints );
1331
1332   setSelectedPoints();
1333 }
1334
1335 void CurveCreator_Widget::moveSelectedPoints( const int theXPosition,
1336                                               const int theYPosition )
1337 {
1338   OCCViewer_ViewPort3d* aViewPort = getViewPort();
1339   if ( !aViewPort )
1340     return;
1341
1342   CurveCreator_ICurve::SectionToPointList aPoints;
1343   startCurveModification( aPoints, false );
1344
1345   gp_Pnt aStartPnt = CurveCreator_Utils::ConvertClickToPoint( myDragStartPosition.x(),
1346                                                               myDragStartPosition.y(),
1347                                                               aViewPort->getView() );
1348   gp_Pnt anEndPnt = CurveCreator_Utils::ConvertClickToPoint( theXPosition, theYPosition,
1349                                                              aViewPort->getView() );
1350   double aXDelta = aStartPnt.X() - anEndPnt.X();
1351   double anYDelta = aStartPnt.Y() - anEndPnt.Y();
1352
1353   CurveCreator_ICurve::SectionToPointCoordsList aCoordList;
1354   CurveCreator::Coordinates aChangedPos;
1355   CurveCreator_ICurve::SectionToPointList::const_iterator anIt = myDragPoints.begin(),
1356                                                           aLast = myDragPoints.end();
1357   for ( ; anIt != aLast; anIt++ ) {
1358     int aSectionId = anIt->first;
1359     int aPointId = anIt->second;
1360     aChangedPos = myCurve->getPoint( aSectionId, aPointId );
1361     if ( aChangedPos.size() < 2 )
1362       continue;
1363
1364     // Remember drag points coordinates
1365     if ( !myDragged ) {
1366       myInitialDragPointsCoords.insert( std::make_pair( aSectionId, aPointId ), aChangedPos );
1367     }
1368
1369     aChangedPos[0] = aChangedPos[0] - aXDelta;
1370     aChangedPos[1] = aChangedPos[1] - anYDelta;
1371     
1372     aCoordList.push_back(
1373       std::make_pair(std::make_pair( aSectionId, aPointId ), 
1374                      aChangedPos ));
1375   }
1376   myCurve->setSeveralPoints( aCoordList, false );
1377
1378   myDragged = true;
1379   finishCurveModification( myDragPoints );
1380 }
1381
1382 void CurveCreator_Widget::updateLocalPointView()
1383 {
1384   if ( myDragStarted )
1385     return;
1386   Handle(AIS_InteractiveContext) aContext = getAISContext();
1387   if ( aContext.IsNull() )
1388     return;
1389
1390   CurveCreator_Utils::getSelectedPoints( aContext, myCurve, myLocalPoints );
1391   int aNbPoints = myLocalPoints.size();
1392
1393   bool isRowLimit = aNbPoints > myLocalPointRowLimit;
1394   myLocalPointView->setVisible( getActionMode() == ModificationMode && !isRowLimit );
1395
1396   if ( !isRowLimit ) {
1397     bool isBlocked = myLocalPointView->blockSignals(true);
1398
1399     myLocalPointView->setLocalPointsToTable( myLocalPoints );
1400
1401     myLocalPointView->blockSignals( isBlocked );
1402   }
1403 }
1404
1405 /**
1406  * 
1407  */
1408 void CurveCreator_Widget::setLocalPointContext( const bool theOpen, const bool isUpdateTable )
1409 {
1410   CurveCreator_Utils::setLocalPointContext( myCurve, getAISContext(), theOpen );
1411   if ( !theOpen && isUpdateTable )
1412     updateLocalPointView();
1413 }
1414
1415 /**
1416  * Set drag operation started. Save the position and a list of dragged points
1417  * \param theState the drag operation state: started/finished
1418  * \param thePoint the start drag position
1419  */
1420 void CurveCreator_Widget::setDragStarted( const bool theState, const QPoint& thePoint )
1421 {
1422   if ( theState ) {
1423     getSelectedPoints( myDragPoints );
1424
1425     myDragStarted = myDragPoints.size();
1426     myDragStartPosition = thePoint;
1427     if ( myDragStarted ) {
1428       // change a viewer interaction style in order to avoid a select rectangle build
1429       myDragInteractionStyle = changeInteractionStyle( SUIT_ViewModel::KEY_FREE );
1430       myCurve->setSkipSorting( true );
1431     }
1432   }
1433   else {
1434     if ( myDragStarted )
1435       changeInteractionStyle( myDragInteractionStyle );
1436     myDragStarted = false;
1437     myDragPoints.clear();
1438     myInitialDragPointsCoords.clear();
1439   }
1440   myDragged = false;
1441 }
1442
1443 void CurveCreator_Widget::getSelectedPoints( CurveCreator_ICurve::SectionToPointList& thePoints )
1444 {
1445   thePoints.clear();
1446   thePoints = myLocalPoints;
1447 }
1448
1449 void CurveCreator_Widget::setSelectedPoints( const CurveCreator_ICurve::SectionToPointList& thePoints )
1450 {
1451   if ( myDragStarted )
1452     return;
1453   Handle(AIS_InteractiveContext) aContext = getAISContext();
1454   if ( aContext.IsNull() || !aContext->HasOpenedContext() )
1455     return;
1456
1457   CurveCreator_Utils::setSelectedPoints( aContext, myCurve, thePoints );
1458
1459   updateLocalPointView();
1460 }
1461
1462 void CurveCreator_Widget::stopActionMode()
1463 {
1464   setActionMode( NoneMode );
1465 }
1466
1467 /**
1468  * Get viewer information before perform the curve modification.
1469  * Take a list of selected cuve points an close local context.
1470  * The context should be closed because the curve presentation is
1471  * redisplayed and if it is not closed, when we close the local context
1472  * later, the presentation shown in the local context is disappeared.
1473  * \param thePoints an output list of curve selected points
1474  * \param theFillPoints a flag whether the selection list should be filled
1475  */
1476 void CurveCreator_Widget::startCurveModification(
1477                            CurveCreator_ICurve::SectionToPointList& thePoints,
1478                            const bool theFillPoints )
1479 {
1480   if ( theFillPoints ) {
1481     thePoints.clear();
1482     getSelectedPoints( thePoints );
1483   }
1484   setLocalPointContext( false );
1485 }
1486
1487 /**
1488  * Restore the viewer state after the curve modification is done.
1489  * Open local context and select given points inside it.
1490  * \param thePoints a list of curve selected points
1491  */
1492 void CurveCreator_Widget::finishCurveModification(
1493                            const CurveCreator_ICurve::SectionToPointList& thePoints )
1494 {
1495   if ( getActionMode() == ModificationMode )
1496     setLocalPointContext( true );
1497   setSelectedPoints( thePoints );
1498   updateUndoRedo();
1499 }
1500
1501 /**
1502  * Returns a point index in the model curve by the point coordinates in the viewer
1503  * \param theX the X coordinate of the point
1504  * \param theY the Y coordinate of the point
1505  */
1506 int CurveCreator_Widget::findLocalPointIndex( int theSectionId, double theX, double theY )
1507 {
1508   return CurveCreator_UtilsICurve::findLocalPointIndex( myCurve, theSectionId, theX, theY );
1509 }
1510
1511 void CurveCreator_Widget::findSectionsToPoints( const double theX, const double theY,
1512                                  CurveCreator_ICurve::SectionToPointList& thePoints )
1513 {
1514   return CurveCreator_UtilsICurve::findSectionsToPoints( myCurve, theX, theY, thePoints );
1515 }
1516
1517 void CurveCreator_Widget::convert( const CurveCreator_ICurve::SectionToPointList& thePoints,
1518                                    QMap<int, QList<int> >& theConvPoints )
1519 {
1520   return CurveCreator_UtilsICurve::convert( thePoints, theConvPoints );
1521 }
1522
1523 /**
1524  * Returns whethe the container has the value
1525  * \param theList a container of values
1526  * \param theValue a value
1527  */
1528 bool CurveCreator_Widget::contains( const CurveCreator_ICurve::SectionToPointList& theList,
1529                                     const CurveCreator_ICurve::SectionToPoint& theValue ) const
1530 {
1531   return CurveCreator_UtilsICurve::contains( theList, theValue );
1532 }