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