Salome HOME
Synchronize adm files
[modules/geom.git] / src / CurveCreator / CurveCreator_Widget.cxx
1 // Copyright (C) 2013-2014  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_Curve.hxx"
23 #include "CurveCreator_CurveEditor.hxx"
24 #include "CurveCreator.hxx"
25 #include "CurveCreator_NewPointDlg.h"
26 #include "CurveCreator_NewSectionDlg.h"
27
28 #include <GEOMUtils.hxx>
29
30 #include <SUIT_Session.h>
31 #include <SUIT_Desktop.h>
32 #include <SUIT_ResourceMgr.h>
33 #include <SUIT_ViewManager.h>
34
35 #include <OCCViewer_ViewWindow.h>
36 #include <OCCViewer_ViewManager.h>
37 #include <OCCViewer_ViewPort3d.h>
38
39 #include <BRep_Tool.hxx>
40 #include <TopoDS.hxx>
41
42 #include <QHBoxLayout>
43 #include <QVBoxLayout>
44 #include <QLabel>
45 #include <QLineEdit>
46 #include <QGroupBox>
47 #include <QToolButton>
48 #include <QToolBar>
49 #include <QAction>
50 #include <QMenu>
51 #include <QMouseEvent>
52 #include <QApplication>
53
54 CurveCreator_Widget::CurveCreator_Widget(QWidget* parent,
55                                          CurveCreator_Curve *theCurve,
56                                          Qt::WindowFlags fl) :
57     QWidget(parent), myNewPointEditor(NULL), myNewSectionEditor(NULL), myEdit(NULL), myCurve(theCurve)
58 {
59     if( myCurve )
60         myEdit = new CurveCreator_CurveEditor( myCurve );
61
62     CurveCreator::Dimension aDim = CurveCreator::Dim2d;
63     if( myCurve )
64         aDim = myCurve->getDimension();
65     myNewPointEditor = new CurveCreator_NewPointDlg( aDim, this );
66     myNewPointEditor->hide();
67 //    connect( myNewPointEditor, SIGNAL(addPoint()), this, SLOT(onAddNewPoint()) );
68     connect( myNewPointEditor, SIGNAL(modifyPoint()), this, SLOT(onModifyPoint()) );
69     connect( myNewPointEditor, SIGNAL(cancelPoint()), this, SLOT(onCancelPoint()) );
70
71     myNewSectionEditor = new CurveCreator_NewSectionDlg( this );
72     myNewSectionEditor->hide();
73     connect( myNewSectionEditor, SIGNAL(addSection()), this, SLOT(onAddNewSection()) );
74     connect( myNewSectionEditor, SIGNAL(modifySection()), this, SLOT(onModifySection()) );
75     connect( myNewSectionEditor, SIGNAL(cancelSection()), this, SLOT(onCancelSection()) );
76
77     QGroupBox* aSectionGroup = new QGroupBox(tr("Sections"),this);
78
79     mySectionView = new CurveCreator_TreeView(myCurve, aSectionGroup);
80     connect( mySectionView, SIGNAL(selectionChanged()), this, SLOT( onSelectionChanged() ) );
81     connect( mySectionView, SIGNAL(pointEntered(int,int)), this, SLOT(onEditPoint(int,int)) );
82     connect( mySectionView, SIGNAL(sectionEntered(int)), this, SLOT(onEditSection(int)) );
83     connect( mySectionView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onContextMenu(QPoint)) );
84     QToolBar* aTB = new QToolBar(tr("TOOL_BAR_TLT"), aSectionGroup);
85 //    QToolButton* anUndoBtn = new QToolButton(aTB);
86
87     SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
88     QPixmap anUndoPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_UNDO")));
89     QPixmap aRedoPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_REDO")));
90     QPixmap aNewSectionPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_NEW_SECTION")));
91     QPixmap aNewPointPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_NEW_POINT")));
92     QPixmap anEditPointsPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_EDIT_POINTS")));
93     QPixmap aDetectPointsPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_EDIT_POINTS")));
94     QPixmap aPolylinePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_POLYLINE")));
95     QPixmap aSplinePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_SPLINE")));
96     QPixmap aRemovePixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_DELETE")));
97     QPixmap aJoinPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_JOIN")));
98     QPixmap aStepUpPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_ARROW_UP")));
99     QPixmap aStepDownPixmap(aResMgr->loadPixmap("GEOM", tr("ICON_CC_ARROW_DOWN")));
100
101 /*    QPixmap anUndoPixmap = QPixmap(tr(":images/ICON_UNDO"));
102     QPixmap aRedoPixmap = QPixmap(tr(":images/ICON_REDO"));
103     QPixmap aNewSectionPixmap = QPixmap(tr(":images/ICON_NEW_SECTION"));
104     QPixmap aNewPointPixmap = QPixmap(tr(":images/ICON_NEW_POINT"));
105     QPixmap aPolylinePixmap = QPixmap(tr(":images/ICON_POLYLINE"));
106     QPixmap aSplinePixmap = QPixmap(tr(":images/ICON_SPLINE"));
107     QPixmap aRemovePixmap = QPixmap(tr(":images/ICON_REMOVE"));
108     QPixmap aJoinPixmap = QPixmap(tr(":images/ICON_JOIN"));
109     QPixmap aStepUpPixmap = QPixmap(tr(":images/ICON_STEP_UP"));
110     QPixmap aStepDownPixmap = QPixmap(tr(":images/ICON_STEP_DOWN"));*/
111
112     QAction* anAct = createAction( UNDO_ID, tr("UNDO"), anUndoPixmap, tr("UNDO_TLT"), 
113                                    QKeySequence(Qt::ControlModifier|Qt::Key_Z) );
114     connect(anAct, SIGNAL(triggered()), this, SLOT(onUndo()) );
115     aTB->addAction(anAct);
116
117     anAct = createAction( REDO_ID, tr("REDO"), aRedoPixmap, tr("REDO_TLT"), 
118                           QKeySequence(Qt::ControlModifier|Qt::Key_Y) );
119     connect(anAct, SIGNAL(triggered()), this, SLOT(onRedo()) );
120     aTB->addAction(anAct);
121
122     aTB->addSeparator();
123
124     anAct = createAction( NEW_SECTION_ID, tr("NEW_SECTION"), aNewSectionPixmap, tr("NEW_SECTION_TLT"), 
125                           QKeySequence(Qt::ControlModifier|Qt::Key_N) );
126     connect(anAct, SIGNAL(triggered()), this, SLOT(onNewSection()) );
127     aTB->addAction(anAct);
128     aTB->addSeparator();
129
130     anAct = createAction( INSERT_SECTION_BEFORE_ID, tr("INSERT_SECTION_BEFORE"), QPixmap(), 
131                           tr("INSERT_SECTION_BEFORE_TLT"),
132                           QKeySequence(Qt::ControlModifier | Qt::Key_Insert ) );
133     connect(anAct, SIGNAL(triggered()), this, SLOT(onInsertSectionBefore()) );
134
135     anAct = createAction( INSERT_SECTION_AFTER_ID, tr("INSERT_SECTION_AFTER"), QPixmap(), 
136                           tr("INSERT_SECTION_AFTER_TLT"),
137                           QKeySequence(Qt::ControlModifier | Qt::ShiftModifier | Qt::Key_Insert ) );
138     connect(anAct, SIGNAL(triggered()), this, SLOT(onInsertSectionAfter()) );
139
140     anAct = createAction( ADDITION_MODE_ID, tr("ADDITION_MODE"), aNewPointPixmap, tr("ADDITION_MODE_TLT"), 
141                           QKeySequence() );
142     anAct->setCheckable(true);
143     connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onAdditionMode(bool)) );
144     connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
145     aTB->addAction(anAct);
146     
147     anAct = createAction( MODIFICATION_MODE_ID, tr("MODIFICATION_MODE"), anEditPointsPixmap, tr("MODIFICATION_MODE_TLT"), 
148                           QKeySequence() );
149     anAct->setCheckable(true);
150     connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onModificationMode(bool)) );
151     connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
152     aTB->addAction(anAct);
153
154     anAct = createAction( DETECTION_MODE_ID, tr("DETECTION_MODE"), aDetectPointsPixmap, tr("DETECTION_MODE_TLT"), 
155                           QKeySequence() );
156     anAct->setCheckable(true);
157     connect(anAct, SIGNAL(triggered(bool)), this, SLOT(onDetectPoints(bool)) );
158     connect(anAct, SIGNAL(toggled(bool)), this, SLOT(onModeChanged(bool)) );
159     aTB->addAction(anAct);
160
161     anAct = createAction( INSERT_POINT_BEFORE_ID, tr("INSERT_POINT_BEFORE"), QPixmap(), 
162                           tr("INSERT_POINT_BEFORE_TLT"), QKeySequence(Qt::ControlModifier|Qt::Key_B) );
163     connect(anAct, SIGNAL(triggered()), this, SLOT(onInsertPointBefore()) );
164
165     anAct = createAction( INSERT_POINT_AFTER_ID, tr("INSERT_POINT_AFTER"), QPixmap(), 
166                            tr("INSERT_POINT_AFTER_TLT"), QKeySequence(Qt::ControlModifier|Qt::Key_M) );
167     connect(anAct, SIGNAL(triggered()), this, SLOT(onInsertPointAfter()) );
168                                  
169     anAct = createAction( CLOSE_SECTIONS_ID, tr("CLOSE_SECTIONS"), QPixmap(), tr("CLOSE_SECTIONS_TLT"), 
170                           QKeySequence(Qt::ControlModifier|Qt::Key_W) );
171     connect(anAct, SIGNAL(triggered()), this, SLOT(onCloseSections()) );
172
173     anAct = createAction( UNCLOSE_SECTIONS_ID, tr("UNCLOSE_SECTIONS"), QPixmap(), 
174                           tr("UNCLOSE_SECTIONS_TLT"), QKeySequence(Qt::ControlModifier|Qt::Key_S) );
175     connect(anAct, SIGNAL(triggered()), this, SLOT(onUncloseSections()) );
176
177     anAct = createAction( SET_SECTIONS_POLYLINE_ID, tr("SET_SECTIONS_POLYLINE"), 
178                           aPolylinePixmap, tr("SET_POLYLINE_TLT"), 
179                           QKeySequence(Qt::ControlModifier|Qt::Key_E) );
180     connect(anAct, SIGNAL(triggered()), this, SLOT(onSetPolyline()) );
181
182     anAct = createAction( SET_SECTIONS_SPLINE_ID, tr("SET_SECTIONS_SPLINE"), aSplinePixmap, 
183                           tr("SET_SPLINE_TLT"), QKeySequence(Qt::ControlModifier|Qt::Key_R) );
184     connect(anAct, SIGNAL(triggered()), this, SLOT(onSetSpline()) );
185
186     anAct = createAction( REMOVE_ID, tr("REMOVE"), aRemovePixmap, tr("REMOVE_TLT"), 
187                           QKeySequence(Qt::ControlModifier|Qt::Key_Delete ) );
188     connect(anAct, SIGNAL(triggered()), this, SLOT(onRemove()) );
189     aTB->addAction(anAct);
190     aTB->addSeparator();
191
192     anAct = createAction( JOIN_ID, tr("JOIN"), aJoinPixmap, tr("JOIN_TLT"), 
193                           QKeySequence(Qt::ControlModifier|Qt::Key_Plus ) );
194     connect( anAct, SIGNAL(triggered()), this, SLOT(onJoin()) );
195     aTB->addAction(anAct);
196     aTB->addSeparator();
197
198     anAct = createAction( UP_ID, tr("STEP_UP"), aStepUpPixmap, tr("STEP_UP_TLT"), 
199                           QKeySequence(Qt::ControlModifier|Qt::Key_Up ) );
200     connect( anAct, SIGNAL(triggered()), this, SLOT(onMoveUp()) );
201
202     anAct = createAction( DOWN_ID, tr("STEP_DOWN"), aStepDownPixmap, tr("STEP_DOWN"), 
203                           QKeySequence(Qt::ControlModifier|Qt::Key_Down ) );
204     connect( anAct, SIGNAL(triggered()), this, SLOT(onMoveDown()) );
205
206     anAct = createAction( CLEAR_ALL_ID, tr("CLEAR_ALL"), QPixmap(), tr("CLEAR_ALL_TLT"), 
207                           QKeySequence(Qt::ControlModifier | Qt::ShiftModifier | Qt::Key_Delete ) );
208     connect( anAct, SIGNAL(triggered()), this, SLOT( onClearAll()) );
209
210     anAct = createAction( JOIN_ALL_ID, tr("JOIN_ALL"), QPixmap(), tr("JOIN_ALL_TLT"), 
211                           QKeySequence(Qt::ControlModifier | Qt::ShiftModifier | Qt::Key_Plus ) );
212     connect( anAct, SIGNAL(triggered()), this, SLOT(onJoinAll()) );
213
214     QVBoxLayout* aSectLayout = new QVBoxLayout();
215     aSectLayout->setMargin( 5 );
216     aSectLayout->setSpacing( 5 );
217     aSectLayout->addWidget(aTB);
218     aSectLayout->addWidget(mySectionView);
219     aSectionGroup->setLayout(aSectLayout);
220     QVBoxLayout* aLay = new QVBoxLayout();
221     aLay->setMargin( 0 );
222     aLay->setSpacing( 5 );
223 //    aLay->addLayout(aNameLayout);
224     aLay->addWidget(aSectionGroup);
225     setLayout(aLay);
226     onSelectionChanged();
227 }
228
229 void CurveCreator_Widget::setCurve( CurveCreator_Curve* theCurve )
230 {
231   if( myEdit != NULL ){
232     delete myEdit;
233     myEdit = NULL;
234   }
235   myCurve = theCurve;
236   mySectionView->setCurve(myCurve);
237   if( myCurve != NULL ){
238     myEdit = new CurveCreator_CurveEditor(myCurve);
239   }
240   onSelectionChanged();
241   updateUndoRedo();
242 }
243
244 void CurveCreator_Widget::onSelectionChanged()
245 {
246   QList<ActionId> anEnabledAct;
247   if( myCurve ){
248     anEnabledAct << NEW_SECTION_ID;
249     QList<int> aSelSections = mySectionView->getSelectedSections();
250     QList< QPair< int, int > > aSelPoints = mySectionView->getSelectedPoints();
251     CurveCreator_TreeView::SelectionType aSelType = mySectionView->getSelectionType();
252     switch( aSelType ){
253     case CurveCreator_TreeView::ST_NOSEL:{
254       break;
255     }
256     case CurveCreator_TreeView::ST_SECTIONS:{
257       /*if( aSelSections[0] > 0 ){
258         anEnabledAct << UP_ID;
259       }*/
260       if( aSelSections.size() == 1 ){
261         anEnabledAct << ADDITION_MODE_ID << MODIFICATION_MODE_ID << DETECTION_MODE_ID;
262       }
263       if (myActionMap[ADDITION_MODE_ID]->isChecked()) {
264         mySection = -1;
265         myPointNum = -1;
266         QList<int> aSelSection = mySectionView->getSelectedSections();
267         if( aSelSection.size() > 0 ){
268           mySection = aSelSection[0];
269           myPointNum = myCurve->getNbPoints(mySection);
270         }
271       } else if (myActionMap[MODIFICATION_MODE_ID]->isChecked()) {
272         anEnabledAct << REMOVE_ID;
273         anEnabledAct << CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID << SET_SECTIONS_POLYLINE_ID << SET_SECTIONS_SPLINE_ID;
274         int aSectCnt = myCurve->getNbSections();
275         if( aSectCnt > 0 )
276           anEnabledAct << CLEAR_ALL_ID;
277         if( aSectCnt > 1 )
278           anEnabledAct << JOIN_ALL_ID;
279         if( aSelSections.size() > 1 ){
280           anEnabledAct << JOIN_ID;
281         }
282       } else if (myActionMap[DETECTION_MODE_ID]->isChecked()) {
283       } else { //no active mode
284       }
285       /*if( aSelSections[ aSelSections.size() - 1 ] < ( myCurve->getNbSections() - 1 ) ){
286         anEnabledAct << DOWN_ID;
287       }*/
288       break;
289     }
290     /*case CurveCreator_TreeView::ST_POINTS_ONE_SECTION:{
291       if( aSelPoints[0].second > 0 ){
292         anEnabledAct << UP_ID;
293       }
294       int aLastIndex = aSelPoints.size()-1;
295       int aSect = aSelPoints[0].first;
296       if( aSelPoints[aLastIndex].second < (myCurve->getNbPoints(aSect) - 1)){
297         anEnabledAct << DOWN_ID;
298       }
299       if( aSelPoints.size() == 1){
300         anEnabledAct << INSERT_POINT_BEFORE_ID << INSERT_POINT_AFTER_ID;
301       }
302       break;
303     }*/
304
305     }
306     
307     /*int aSelObjsCnt = aSelPoints.size() + aSelSections.size();
308     if( aSelObjsCnt > 0 ){
309       anEnabledAct << REMOVE_ID;
310     }
311     if( (myCurve->getNbSections() + myCurve->getNbPoints()) > 0 ){
312       anEnabledAct << REMOVE_ALL_ID;
313     }*/
314     if( myCurve->getNbSections() > 1 ){
315       anEnabledAct << JOIN_ALL_ID;
316     }
317   }
318   QList<ActionId> anIds = myActionMap.keys();
319   for( int i = 0 ; i < anIds.size() ; i++ ){
320     if( myActionMap.contains(anIds[i]) ){
321       if( anEnabledAct.contains(anIds[i]) ){
322         myActionMap[anIds[i]]->setEnabled(true);
323       }
324       else{
325         myActionMap[anIds[i]]->setEnabled(false);
326       }
327     }
328   }
329   emit selectionChanged();
330 }
331
332 void CurveCreator_Widget::onAdditionMode(bool checked)
333 {
334   if( !myEdit )
335     return;
336
337   SUIT_ViewWindow* aViewWindow = 0;
338   SUIT_Study* activeStudy = SUIT_Session::session()->activeApplication()->activeStudy();
339   if ( activeStudy )
340     aViewWindow = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
341   if ( aViewWindow == 0 )
342     return;
343   SUIT_ViewManager* aViewManager = aViewWindow->getViewManager();
344
345   if ( aViewManager->getType() == OCCViewer_Viewer::Type() ) {
346     if (checked) {
347       connect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
348              this, SLOT( onGetPointByClick( SUIT_ViewWindow*, QMouseEvent* ) ) );
349     } else {
350       disconnect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
351              this, SLOT( onGetPointByClick( SUIT_ViewWindow*, QMouseEvent* ) ) );
352       return;
353     }
354   }
355
356   mySection= -1;
357   myPointNum = -1;
358   QList<int> aSelSection = mySectionView->getSelectedSections();
359   if( aSelSection.size() > 0 ){
360     mySection = aSelSection[0];
361   }
362   else{
363     QList< QPair<int,int> > aSelPoints = mySectionView->getSelectedPoints();
364     if( aSelPoints.size() > 0 ){
365       mySection = aSelPoints[0].first;
366       myPointNum = aSelPoints[0].second + 1;
367     }
368   }
369 /*
370   QString aSectName;
371   if( mySection < 0 ){
372     mySection = myCurve->getNbSections() - 1;
373   }
374   aSectName = QString::fromStdString( myCurve->getSectionName(mySection));
375   if( myPointNum < 0 ){
376     myPointNum = myCurve->getNbPoints(mySection);
377   }
378   myNewPointEditor->clear();
379   myNewPointEditor->setEditMode(false);
380   myNewPointEditor->setSectionName(aSectName);
381   myNewPointEditor->setDimension(myCurve->getDimension());
382 */
383 //  emit subOperationStarted( myNewPointEditor );
384 }
385
386 void CurveCreator_Widget::onModificationMode(bool checked)
387 {
388   SUIT_ViewWindow* aViewWindow = 0;
389   SUIT_Study* activeStudy = SUIT_Session::session()->activeApplication()->activeStudy();
390   if ( activeStudy )
391     aViewWindow = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
392   if ( aViewWindow == 0 )
393     return;
394   SUIT_ViewManager* aViewManager = aViewWindow->getViewManager();
395   if ( aViewManager->getType() == OCCViewer_Viewer::Type() ) {
396     if (checked) {
397 //      connect( aViewManager, SIGNAL( mouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ),
398 //             this, SLOT( onPointSelect( SUIT_ViewWindow*, QMouseEvent* ) ) );
399       connect( aViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
400              this, SLOT( onPointDrag( SUIT_ViewWindow*, QMouseEvent* ) ) );
401     }
402     else {
403 //      disconnect( aViewManager, SIGNAL( mouseRelease( SUIT_ViewWindow*, QMouseEvent* ) ),
404 //             this, SLOT( onPointSelect( SUIT_ViewWindow*, QMouseEvent* ) ) );
405       disconnect( aViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
406              this, SLOT( onPointDrag( SUIT_ViewWindow*, QMouseEvent* ) ) );
407       return;
408     }
409   }
410 }
411
412 void CurveCreator_Widget::onDetectPoints(bool checked)
413 {
414 }
415
416 void CurveCreator_Widget::onModeChanged(bool checked)
417 {
418   if (checked) {
419     QAction* anAction = (QAction*)sender();
420     switch(myActionMap.key(anAction)) {
421       case ADDITION_MODE_ID:
422         if (myActionMap[MODIFICATION_MODE_ID]->isChecked())
423           myActionMap[MODIFICATION_MODE_ID]->trigger();
424         else if (myActionMap[DETECTION_MODE_ID]->isChecked())
425           myActionMap[DETECTION_MODE_ID]->trigger();
426         break;
427       case MODIFICATION_MODE_ID:
428         if (myActionMap[ADDITION_MODE_ID]->isChecked())
429           myActionMap[ADDITION_MODE_ID]->trigger();
430         else if (myActionMap[DETECTION_MODE_ID]->isChecked())
431           myActionMap[DETECTION_MODE_ID]->trigger();
432         break;
433       case DETECTION_MODE_ID:
434         if (myActionMap[ADDITION_MODE_ID]->isChecked())
435           myActionMap[ADDITION_MODE_ID]->trigger();
436         else if (myActionMap[MODIFICATION_MODE_ID]->isChecked())
437           myActionMap[MODIFICATION_MODE_ID]->trigger();
438         break;
439     }
440   }
441   onSelectionChanged();
442 }
443
444 void CurveCreator_Widget::onAddNewPoint(const CurveCreator::Coordinates& theCoords)
445 {
446   if( !myEdit )
447     return;
448 //  CurveCreator::Coordinates aCoords = myNewPointEditor->getCoordinates();
449   myEdit->insertPoints(theCoords, mySection, myPointNum );
450   mySectionView->pointsAdded( mySection, myPointNum );
451 //  myNewPointEditor->clear();
452   myPointNum++;
453   onSelectionChanged();
454   updateUndoRedo();
455 }
456
457 void CurveCreator_Widget::onNewSection()
458 {
459   if( !myEdit )
460     return;
461   myNewSectionEditor->clear();
462   myNewSectionEditor->setEditMode(false);
463   QString aSectName = QString( myCurve->getUnicSectionName().c_str() );
464   myNewSectionEditor->setSectionParameters(aSectName, true, CurveCreator::Polyline );
465   emit subOperationStarted( myNewSectionEditor );
466 }
467
468 void CurveCreator_Widget::onAddNewSection()
469 {
470   if( !myEdit )
471     return;
472   CurveCreator::Coordinates aCoords;
473   myEdit->addSection( myNewSectionEditor->getName().toStdString(), myNewSectionEditor->getSectionType(),
474                       myNewSectionEditor->isClosed(), aCoords  );
475   mySectionView->sectionAdded( mySection );
476   QString aNewName = QString(myCurve->getUnicSectionName().c_str());
477   myNewSectionEditor->setSectionName(aNewName);
478   mySection++;
479   onSelectionChanged();
480   updateUndoRedo();
481   onCancelSection();
482 }
483
484 void CurveCreator_Widget::onCancelPoint()
485 {
486   emit subOperationFinished( myNewPointEditor );
487 }
488
489 void CurveCreator_Widget::onCancelSection()
490 {
491   emit subOperationFinished( myNewSectionEditor );
492 }
493
494 QAction* CurveCreator_Widget::createAction( ActionId theId, const QString& theName, const QPixmap& theImage,
495                                             const QString& theToolTip, const QKeySequence& theShortcut )
496 {
497   QAction* anAct = new QAction(theName,this);
498   if( !theImage.isNull() ){
499     anAct->setIcon(theImage);
500   }
501   anAct->setShortcut(theShortcut);
502   anAct->setToolTip(theToolTip);
503   myActionMap[theId] = anAct;
504   return anAct;
505 }
506
507 QAction* CurveCreator_Widget::getAction(ActionId theId)
508 {
509   if( myActionMap.contains(theId) )
510     return myActionMap[theId];
511   return NULL;
512 }
513
514 void CurveCreator_Widget::onEditSection( int theSection )
515 {
516   if( !myEdit )
517     return;
518   mySection = theSection;
519   QString aSectName = QString::fromStdString( myCurve->getSectionName(theSection));
520   bool isClosed = myCurve->isClosed(theSection);
521   CurveCreator::Type aType = myCurve->getType(theSection);
522   myNewSectionEditor->setEditMode(true);
523   myNewSectionEditor->setSectionParameters( aSectName, isClosed, aType );
524
525   emit subOperationStarted( myNewSectionEditor );
526 }
527
528 void CurveCreator_Widget::onModifySection()
529 {
530   if( !myEdit )
531     return;
532   QString aName = myNewSectionEditor->getName();
533   bool isClosed = myNewSectionEditor->isClosed();
534   CurveCreator::Type aSectType = myNewSectionEditor->getSectionType();
535   myEdit->startOperation();
536   myEdit->setClosed( isClosed, mySection );
537   myEdit->setName( aName.toStdString(), mySection );
538   myEdit->setType( aSectType, mySection );
539   myEdit->finishOperation();
540   mySectionView->sectionChanged(mySection);
541   updateUndoRedo();
542   onCancelSection();
543 }
544
545 void CurveCreator_Widget::onEditPoint( int theSection, int thePoint )
546 {
547   if( !myNewPointEditor || !myEdit )
548     return;
549   mySection = theSection;
550   myPointNum = thePoint;
551   QString aSectName = QString::fromStdString( myCurve->getSectionName(theSection));
552   myNewPointEditor->setEditMode(true);
553   myNewPointEditor->setSectionName(aSectName);
554   myNewPointEditor->setDimension( myCurve->getDimension() );
555   CurveCreator::Coordinates aCoords = myCurve->getCoordinates(theSection,thePoint);
556   myNewPointEditor->setCoordinates(aCoords);
557   emit subOperationStarted( myNewPointEditor );
558 }
559
560 void CurveCreator_Widget::onModifyPoint()
561 {
562   if( !myEdit )
563     return;
564   CurveCreator::Coordinates aCoords = myNewPointEditor->getCoordinates();
565   myEdit->setCoordinates( aCoords, mySection, myPointNum );
566   mySectionView->pointDataChanged( mySection, myPointNum );
567   updateUndoRedo();
568   onCancelPoint();
569 }
570
571 void CurveCreator_Widget::onJoin()
572 {
573   if( !myEdit )
574     return;
575   QList<int> aSections = mySectionView->getSelectedSections();
576   if( aSections.size() == 0 ){
577     return;
578   }
579   int aMainSect = aSections[0];
580   int aMainSectSize = myCurve->getNbPoints(aMainSect);
581   myEdit->startOperation();
582   for( int i = 1 ; i < aSections.size() ; i++ ){
583     int aSectNum = aSections[i] - (i-1);
584     myEdit->join( aMainSect, aSectNum );
585     mySectionView->sectionsRemoved( aSectNum );
586   }
587   myEdit->finishOperation();
588   int aNewSectSize = myCurve->getNbPoints(aMainSect);
589   if( aNewSectSize != aMainSectSize )
590     mySectionView->pointsAdded( aMainSect, aMainSectSize, aNewSectSize-aMainSectSize );
591   updateUndoRedo();
592 }
593
594 void CurveCreator_Widget::onRemove()
595 {
596   if( !myEdit )
597     return;
598   QList< QPair<int,int> > aSelPoints = mySectionView->getSelectedPoints();
599   int aCurrSect=-1;
600   int aRemoveCnt = 0;
601   myEdit->startOperation();
602   for( int i = 0 ; i < aSelPoints.size() ; i++ ){
603     if( aCurrSect != aSelPoints[i].first ){
604       aRemoveCnt = 0;
605       aCurrSect = aSelPoints[i].first;
606     }
607     int aPntIndx = aSelPoints[i].second - aRemoveCnt;
608     myEdit->removePoints(aCurrSect,aPntIndx, 1);
609     mySectionView->pointsRemoved( aCurrSect, aPntIndx );
610     aRemoveCnt++;
611   }
612   QList<int> aSections = mySectionView->getSelectedSections();
613   for( int i = 0 ; i < aSections.size() ; i++ ){
614     int aSectNum = aSections[i] - (i);
615     myEdit->removeSection( aSectNum );
616     mySectionView->sectionsRemoved( aSectNum );
617   }
618   myEdit->finishOperation();
619   mySectionView->clearSelection();
620   updateUndoRedo();
621 }
622
623 void CurveCreator_Widget::onMoveUp()
624 {
625   if( !myEdit )
626     return;
627   if( mySectionView->getSelectionType() == CurveCreator_TreeView::ST_SECTIONS ){
628     //Move sections
629     QList<int> aSections = mySectionView->getSelectedSections();
630     for( int i = 0 ; i < aSections.size() ; i++ ){
631       int anIndx = aSections[i];
632       myEdit->moveSection( anIndx, anIndx-1);
633       mySectionView->sectionsSwapped( anIndx, -1 );
634     }
635   }
636   else{
637     //Move points
638     QList< QPair<int,int> > aPoints = mySectionView->getSelectedPoints();
639     for( int i = 0 ; i < aPoints.size() ; i++ ){
640       int aSection = aPoints[i].first;
641       int aPoint = aPoints[i].second;
642       myEdit->movePoint(aSection, aPoint, aPoint-2);
643       mySectionView->pointsSwapped( aSection, aPoint, -1 );
644     }
645   }
646   updateUndoRedo();
647 }
648
649 void CurveCreator_Widget::onMoveDown()
650 {
651   if( !myEdit )
652     return;
653   if( mySectionView->getSelectionType() == CurveCreator_TreeView::ST_SECTIONS ){
654     //Move sections
655     QList<int> aSections = mySectionView->getSelectedSections();
656     for( int i = aSections.size()-1 ; i >=0 ; i-- ){
657       int anIndx = aSections[i];
658       myEdit->moveSection( anIndx, anIndx+1);
659       mySectionView->sectionsSwapped( anIndx, 1 );
660     }
661   }
662   else{
663     //Move points
664     QList< QPair<int,int> > aPoints = mySectionView->getSelectedPoints();
665     for( int i = aPoints.size() - 1; i >= 0 ; i--  ){
666       int aSection = aPoints[i].first;
667       int aPoint = aPoints[i].second;
668       myEdit->movePoint(aSection, aPoint, aPoint+1);
669       mySectionView->pointsSwapped( aSection, aPoint, 1 );
670     }
671   }
672   updateUndoRedo();
673 }
674
675 void CurveCreator_Widget::onClearAll()
676 {
677   if( !myEdit )
678     return;
679   myEdit->clear();
680   mySectionView->reset();
681   onSelectionChanged();
682   updateUndoRedo();
683 }
684
685 void CurveCreator_Widget::onJoinAll()
686 {
687   if( !myEdit )
688     return;
689   myEdit->join();
690   mySectionView->reset();
691   onSelectionChanged();
692   updateUndoRedo();
693 }
694
695 void CurveCreator_Widget::onInsertSectionBefore()
696 {
697
698 }
699
700 void CurveCreator_Widget::onInsertSectionAfter()
701 {
702
703 }
704
705 void CurveCreator_Widget::onInsertPointBefore()
706 {
707
708 }
709
710 void CurveCreator_Widget::onInsertPointAfter()
711 {
712
713 }
714
715 void CurveCreator_Widget::onUndoSettings()
716 {
717
718 }
719
720 void CurveCreator_Widget::onSetSpline()
721 {
722   if( !myEdit )
723     return;
724   QList<int> aSelSections = mySectionView->getSelectedSections();
725   myEdit->startOperation();
726   for( int i = 0 ; i < aSelSections.size() ; i++ ){
727     myEdit->setType(CurveCreator::BSpline, aSelSections[i]);
728     mySectionView->sectionChanged(aSelSections[i]);
729   }
730   myEdit->finishOperation();
731   updateUndoRedo();
732 }
733
734 void CurveCreator_Widget::onSetPolyline()
735 {
736   if( !myEdit )
737     return;
738   myEdit->startOperation();
739   QList<int> aSelSections = mySectionView->getSelectedSections();
740   for( int i = 0 ; i < aSelSections.size() ; i++ ){
741     myEdit->setType(CurveCreator::Polyline, aSelSections[i]);
742     mySectionView->sectionChanged(aSelSections[i]);
743   }
744   myEdit->finishOperation();
745   updateUndoRedo();
746 }
747
748 void CurveCreator_Widget::onCloseSections()
749 {
750   if( !myEdit )
751     return;
752   myEdit->startOperation();
753   QList<int> aSelSections = mySectionView->getSelectedSections();
754   for( int i = 0 ; i < aSelSections.size() ; i++ ){
755     myEdit->setClosed(true, aSelSections[i]);
756     mySectionView->sectionChanged(aSelSections[i]);
757   }
758   myEdit->finishOperation();
759   updateUndoRedo();
760 }
761
762 void CurveCreator_Widget::onUncloseSections()
763 {
764   if( !myEdit )
765     return;
766   myEdit->startOperation();
767   QList<int> aSelSections = mySectionView->getSelectedSections();
768   for( int i = 0 ; i < aSelSections.size() ; i++ ){
769     myEdit->setClosed(false, aSelSections[i]);
770     mySectionView->sectionChanged(aSelSections[i]);
771   }
772   myEdit->finishOperation();
773   updateUndoRedo();
774 }
775
776 void CurveCreator_Widget::onUndo()
777 {
778     if( !myEdit )
779       return;
780     myEdit->undo();
781     mySectionView->reset();
782     updateUndoRedo();
783 }
784
785 void CurveCreator_Widget::onRedo()
786 {
787     if( !myEdit )
788       return;
789     myEdit->redo();
790     mySectionView->reset();
791     updateUndoRedo();
792 }
793
794 void CurveCreator_Widget::updateUndoRedo()
795 {
796     QAction* anAct = myActionMap[UNDO_ID];
797     if( anAct != 0 ){
798         if( myEdit->getNbUndo() != 0 ){
799             anAct->setEnabled(true);
800         }
801         else{
802             anAct->setDisabled(true);
803         }
804     }
805     anAct = myActionMap[REDO_ID];
806     if( anAct != 0 ){
807         if( myEdit->getNbRedo() != 0 ){
808             anAct->setEnabled(true);
809         }
810         else{
811             anAct->setDisabled(true);
812         }
813     }
814 }
815
816 void CurveCreator_Widget::onContextMenu( QPoint thePoint )
817 {
818   QList<ActionId> aContextActions;
819   aContextActions << CLEAR_ALL_ID << JOIN_ALL_ID << SEPARATOR_ID <<
820                      CLOSE_SECTIONS_ID << UNCLOSE_SECTIONS_ID << SET_SECTIONS_POLYLINE_ID <<
821                      SET_SECTIONS_SPLINE_ID;
822   QPoint aGlPoint = mySectionView->mapToGlobal(thePoint);
823   bool isVis = false;
824   QList<ActionId> aResAct;
825   for( int i = 0 ; i < aContextActions.size() ; i++ ){
826     if( aContextActions[i] != SEPARATOR_ID ){
827       if( myActionMap.contains(aContextActions[i]) ){
828         QAction* anAct = myActionMap[aContextActions[i]];
829         if( anAct->isEnabled() ){
830           aResAct << aContextActions[i];
831           isVis = true;
832         }
833       }
834     }
835     else{
836       aResAct << SEPARATOR_ID;
837     }
838   }
839   if( !isVis )
840     return;
841
842   QMenu* aMenu = new QMenu(this);
843   for( int i = 0 ; i < aResAct.size() ; i++ ){
844     if( aResAct[i] == SEPARATOR_ID ){
845       aMenu->addSeparator();
846     }
847     else{
848       QAction* anAct = myActionMap[aResAct[i]];
849       aMenu->insertAction(NULL, anAct);
850     }
851   }
852   aMenu->exec(aGlPoint);
853 }
854
855 QList<int> CurveCreator_Widget::getSelectedSections()
856 {
857   return mySectionView->getSelectedSections();
858 }
859
860 QList< QPair< int, int > > CurveCreator_Widget::getSelectedPoints()
861 {
862   return mySectionView->getSelectedPoints();
863 }
864
865 bool CurveCreator_Widget::isInstantSketchingEnabled() const
866 {
867   if( myNewPointEditor )
868     return myNewPointEditor->isInstantSketchingEnabled();
869   return false;
870 }
871
872 void CurveCreator_Widget::setInstantSketchingEnabled( const bool theState )
873 {
874   if( myNewPointEditor )
875     myNewPointEditor->setInstantSketchingEnabled( theState );
876 }
877
878 //=================================================================================
879 // function : GeometryGUI::onGetPointByClick()
880 // purpose  : Manage mouse press events in Additon mode
881 //=================================================================================
882 void CurveCreator_Widget::onGetPointByClick( SUIT_ViewWindow* theViewWindow, QMouseEvent* pe )
883 {
884   if ( myNewPointEditor && theViewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() &&
885        pe->modifiers() != Qt::ControlModifier ) {
886     OCCViewer_Viewer* anOCCViewer =
887       ( (OCCViewer_ViewManager*)( theViewWindow->getViewManager() ) )->getOCCViewer();
888     Handle(AIS_InteractiveContext) ic = anOCCViewer->getAISContext();
889
890     gp_Pnt aPnt;    
891
892     ic->InitSelected();
893     if ( pe->modifiers() == Qt::ShiftModifier )
894       ic->ShiftSelect();  // Append selection
895     else
896       ic->Select();       // New selection
897
898     /*TopoDS_Shape aShape;
899
900     ic->InitSelected();
901     if ( ic->MoreSelected() )
902       aShape = ic->SelectedShape();
903
904     if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
905       aPnt = BRep_Tool::Pnt( TopoDS::Vertex( ic->SelectedShape() ) );
906     else*/
907     {
908       OCCViewer_ViewPort3d* vp =  ((OCCViewer_ViewWindow*)theViewWindow)->getViewPort();
909       aPnt = GEOMUtils::ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
910     }
911     // set the coordinates into dialog
912     CurveCreator::Coordinates aCoords;
913     aCoords.push_back( aPnt.X() );
914     aCoords.push_back( aPnt.Y() );
915     if ( myCurve->getDimension() == 3 ) {
916       aCoords.push_back( aPnt.Z() );
917     }
918     onAddNewPoint(aCoords);
919 //    myNewPointEditor->setCoordinates( aCoords );
920   }
921 }
922
923 //=================================================================================
924 // function : GeometryGUI::onPointDrag()
925 // purpose  : Manage mouse move events in Modification mode
926 //=================================================================================
927 void CurveCreator_Widget::onPointDrag( SUIT_ViewWindow* theViewWindow, QMouseEvent* pe )
928 {
929   if ( !(pe->buttons() & Qt::LeftButton) )
930     return;
931   if ( (pe->pos() - myDragStartPosition).manhattanLength() < QApplication::startDragDistance() )
932     return;
933 /*  
934   QDrag *drag = new QDrag(this);
935   QMimeData *mimeData = new QMimeData;
936   
937   mimeData->setData(mimeType, data);
938   drag->setMimeData(mimeData);
939   
940   Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction);
941   */
942 }