Salome HOME
Improve actions management
[modules/geom.git] / src / BasicGUI / BasicGUI_CurveDlg.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // GEOM GEOMGUI : GUI for Geometry component
23 // File   : BasicGUI_CurveDlg.cxx
24 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
25
26 #include "BasicGUI_CurveDlg.h"
27
28 #include <DlgRef.h>
29 #include <GeometryGUI.h>
30 #include <GEOMBase.h>
31
32 #include <SUIT_ResourceMgr.h>
33 #include <SUIT_Session.h>
34 #include <SalomeApp_Application.h>
35 #include <SalomeApp_Study.h>
36 #include <LightApp_SelectionMgr.h>
37
38 #include <SALOME_ListIteratorOfListIO.hxx>
39 #include <SALOME_ListIO.hxx>
40
41 #include <TopoDS_Shape.hxx>
42 #include <TopoDS.hxx>
43 #include <TopExp.hxx>
44 #include <TColStd_IndexedMapOfInteger.hxx>
45 #include <TopTools_IndexedMapOfShape.hxx>
46
47 #include <GEOMImpl_Types.hxx>
48
49 //=================================================================================
50 // class    : BasicGUI_CurveDlg()
51 // purpose  : Constructs a BasicGUI_CurveDlg which is a child of 'parent', with the
52 //            name 'name' and widget flags set to 'f'.
53 //            The dialog will by default be modeless, unless you set 'modal' to
54 //            TRUE to construct a modal dialog.
55 //=================================================================================
56 BasicGUI_CurveDlg::BasicGUI_CurveDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
57                                       bool modal, Qt::WindowFlags fl )
58   : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
59 {
60   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_POLYLINE" ) ) );
61   QPixmap image2( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_SPLINE" ) ) );
62   QPixmap image3( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_BEZIER" ) ) );
63   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
64
65   setWindowTitle( tr( "GEOM_CURVE_TITLE" ) );
66
67   /***************************************************************/
68   mainFrame()->RadioButton1->setIcon( image0 );
69   mainFrame()->RadioButton2->setIcon( image3 );
70   mainFrame()->RadioButton3->setIcon( image2 );
71
72   GroupPoints = new DlgRef_1Sel1Check( centralWidget() );
73
74   GroupPoints->GroupBox1->setTitle( tr( "GEOM_NODES" ) );
75   GroupPoints->TextLabel1->setText( tr( "GEOM_POINTS" ) );
76   GroupPoints->PushButton1->setIcon( image1 );
77   GroupPoints->PushButton1->setDown( true );
78
79   GroupPoints->LineEdit1->setReadOnly( true );
80
81   GroupPoints->CheckButton1->setText( tr( "GEOM_IS_CLOSED" ) );
82   GroupPoints->CheckButton1->setChecked(false);
83   GroupPoints->CheckButton1->hide();
84
85   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
86   layout->setMargin( 0 ); layout->setSpacing( 6 );
87   layout->addWidget( GroupPoints );
88   /***************************************************************/
89
90   setHelpFileName( "create_curve_page.html" );
91
92   Init();
93 }
94
95
96 //=================================================================================
97 // function : ~BasicGUI_CurveDlg()
98 // purpose  : Destroys the object and frees any allocated resources
99 //=================================================================================
100 BasicGUI_CurveDlg::~BasicGUI_CurveDlg()
101 {
102 }
103
104
105 //=================================================================================
106 // function : Init()
107 // purpose  :
108 //=================================================================================
109 void BasicGUI_CurveDlg::Init()
110 {
111   /* init variables */
112   myEditCurrentArgument = GroupPoints->LineEdit1;
113
114   myPoints = new GEOM::ListOfGO();
115   myPoints->length( 0 );
116
117   globalSelection(); // close local contexts, if any
118   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
119
120   /* signals and slots connections */
121   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog( ) ) );
122   connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
123
124   connect( buttonOk(),     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
125   connect( buttonApply(),  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
126
127   connect( this,           SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
128
129   connect( GroupPoints->PushButton1, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
130   connect( GroupPoints->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
131
132   connect( GroupPoints->CheckButton1,SIGNAL( toggled(bool) ),   this, SLOT( CheckButtonToggled() ) );
133
134   connect( myGeomGUI->getApp()->selectionMgr(),
135            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
136
137   initName( tr( "GEOM_CURVE" ) );
138   resize(100,100);
139   ConstructorsClicked( 0 );
140 }
141
142 //=================================================================================
143 // function : ConstructorsClicked()
144 // purpose  :
145 //=================================================================================
146 void BasicGUI_CurveDlg::ConstructorsClicked( int id )
147 {
148   QString aTitle = tr( id == 0 ? "GEOM_POLYLINE" : id == 1 ? "GEOM_BEZIER" : "GEOM_INTERPOL" );
149   mainFrame()->GroupConstructors->setTitle( aTitle );
150
151   if (id == 2) // b-spline
152     GroupPoints->CheckButton1->show();
153   else
154     GroupPoints->CheckButton1->hide();
155
156   myPoints = new GEOM::ListOfGO();
157   myPoints->length( 0 );
158
159   myEditCurrentArgument->setText( "" );
160 }
161
162
163 //=================================================================================
164 // function : SetEditCurrentArgument()
165 // purpose  :
166 //=================================================================================
167 void BasicGUI_CurveDlg::SetEditCurrentArgument()
168 {
169   if ( sender() == GroupPoints->PushButton1 )
170     myEditCurrentArgument = GroupPoints->LineEdit1;
171   myEditCurrentArgument->setFocus();
172   SelectionIntoArgument();
173 }
174
175
176 //=================================================================================
177 // function : LineEditReturnPressed()
178 // purpose  :
179 //=================================================================================
180 void BasicGUI_CurveDlg::LineEditReturnPressed()
181 {
182   if ( sender() == GroupPoints->LineEdit1 )
183   {
184     myEditCurrentArgument = GroupPoints->LineEdit1;
185     GEOMBase_Skeleton::LineEditReturnPressed();
186   }
187 }
188
189 //=================================================================================
190 // function : CheckButtonToggled()
191 // purpose  :
192 //=================================================================================
193 void BasicGUI_CurveDlg::CheckButtonToggled()
194 {
195   displayPreview();
196 }
197
198 //=================================================================================
199 // function : ClickOnOk()
200 // purpose  :
201 //=================================================================================
202 void BasicGUI_CurveDlg::ClickOnOk()
203 {
204   if ( ClickOnApply() )
205     ClickOnCancel();
206 }
207
208 //=================================================================================
209 // function : ClickOnApply()
210 // purpose  :
211 //=================================================================================
212 bool BasicGUI_CurveDlg::ClickOnApply()
213 {
214   if ( !onAccept() )
215     return false;
216
217   initName();
218   globalSelection(); // close local contexts, if any
219   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
220   return true;
221 }
222
223 //=================================================================================
224 /*! function : isPointInList()
225  *  purpose  : Check is point (theObject) in the list \a thePoints.
226  * \author enk
227  * \retval -1, if point not in list, else 1 in list
228  */
229 //=================================================================================
230 static int isPointInList( std::list<GEOM::GEOM_Object_var>& thePoints,
231                           GEOM::GEOM_Object_var& theObject )
232 {
233   int len = thePoints.size();
234
235   if ( len < 1 ) {
236     return -1;
237   }
238
239   for ( std::list<GEOM::GEOM_Object_var>::iterator i = thePoints.begin(); i != thePoints.end(); i++ ) {
240     if ( std::string( (*i)->GetEntry() ) == std::string( theObject->GetEntry() ) ) {
241       return 1;
242     }
243   }
244
245   return -1;
246 }
247 //=================================================================================
248 /*! function : removeUnnecessaryPnt()
249  *  purpose  : Remove unnecessary points from list \a theOldPoints
250  * \author enk
251  * \li \a theOldPoints - ordered sequence with unnecessary point
252  * \li \a theNewPoints - not ordered sequence with necessary points
253  */
254 //=================================================================================
255 static void removeUnnecessaryPnt( std::list<GEOM::GEOM_Object_var>& theOldPoints,
256                                   GEOM::ListOfGO_var& theNewPoints )
257 {
258   std::list<GEOM::GEOM_Object_var> objs_to_remove;
259   for ( std::list<GEOM::GEOM_Object_var>::iterator i = theOldPoints.begin(); i != theOldPoints.end(); i++ ) {
260     bool found = false;
261     for ( int j = 0; j < theNewPoints->length() && !found ; j++ ) {
262       if ( std::string( (*i)->GetEntry() ) == std::string( theNewPoints[j]->GetEntry() ) ) {
263         found = true;
264       }
265     }
266     if ( !found ) {
267       objs_to_remove.push_back( *i );
268       //cout << "removed: " << (*i)->GetEntry() << endl;
269     }
270   }
271   for ( std::list<GEOM::GEOM_Object_var>::iterator i = objs_to_remove.begin(); i != objs_to_remove.end(); i++ ) {
272     theOldPoints.remove( *i );
273   }
274 }
275
276 //=================================================================================
277 // function : SelectionIntoArgument()
278 // purpose  : Called when selection as changed or other case
279 //=================================================================================
280 void BasicGUI_CurveDlg::SelectionIntoArgument()
281 {
282   myEditCurrentArgument->setText( "" );
283
284   Standard_Boolean aRes = Standard_False;
285
286   SalomeApp_Application* app = myGeomGUI->getApp();
287   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
288   _PTR(Study) aDStudy = appStudy->studyDS();
289   GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations(getStudyId());
290
291   int anIndex;
292   TopoDS_Shape aShape;
293   TColStd_IndexedMapOfInteger aMapIndexes;
294   GEOM::GEOM_Object_var anObject;
295   std::list<GEOM::GEOM_Object_var> aList;
296   LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
297   SALOME_ListIO selected;
298   aSelMgr->selectedObjects(selected, QString::null, false);
299
300   int IOC = selected.Extent();
301   // bool is_append = myPoints->length() < IOC; // if true - add point, else remove
302   // myPoints->length( IOC ); // this length may be greater than number of objects,
303                            // that will actually be put into myPoints
304
305   for (SALOME_ListIteratorOfListIO anIt (selected); anIt.More(); anIt.Next()) {
306     GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(anIt.Value(), aRes);
307     if (!CORBA::is_nil(aSelectedObject) && aRes) {
308       if (GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE) && !aShape.IsNull()) {
309         aSelMgr->GetIndexes(anIt.Value(), aMapIndexes);
310
311         if (aMapIndexes.Extent() > 0) {
312           for (int ii = 1; ii <= aMapIndexes.Extent(); ii++) {
313             anIndex = aMapIndexes(ii);
314             QString aName = GEOMBase::GetName( aSelectedObject );
315             aName = aName + ":vertex_" + QString::number( anIndex );
316             anObject = aShapesOp->GetSubShape( aSelectedObject, anIndex );
317             //Find Object in study
318             _PTR(SObject) obj ( aDStudy->FindObjectID( anIt.Value()->getEntry() ) );
319             bool inStudy = false;
320             _PTR(ChildIterator) iit( aDStudy->NewChildIterator( obj ) );
321             for (; iit->More() && !inStudy; iit->Next()) {
322               _PTR(SObject) child( iit->Value() );
323               QString aChildName = child->GetName().c_str();
324               if ( aChildName == aName ) {
325                 inStudy = true;
326                 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject( iit->Value() );
327                 anObject = GEOM::GEOM_Object::_narrow( corbaObj );
328               }
329             }
330
331             if ( !inStudy )
332               GeometryGUI::GetGeomGen()->AddInStudy( GeometryGUI::ClientStudyToStudy( aDStudy ),
333                                                      anObject, aName.toLatin1().data(), aSelectedObject );
334
335             int pos = isPointInList( myOrderedSel, anObject );
336             if ( pos == -1 ) {
337               myOrderedSel.push_back( anObject );
338             }
339             //              if (!inStudy)
340             aList.push_back(anObject);
341           }
342         }
343         else { // aMap.Extent() == 0
344           int pos = isPointInList( myOrderedSel, aSelectedObject );
345           if ( pos == -1 )
346             myOrderedSel.push_back( aSelectedObject );
347           aList.push_back( aSelectedObject );
348         }
349       }
350     }
351   }
352
353   myPoints->length( aList.size()  );
354
355   int k = 0;
356   for ( std::list<GEOM::GEOM_Object_var>::iterator j = aList.begin(); j != aList.end(); j++ )
357     myPoints[k++] = *j;
358
359   if ( IOC == 0 )
360     myOrderedSel.clear();
361   else
362     removeUnnecessaryPnt( myOrderedSel, myPoints );
363
364   // if ( myOrderedSel.size() == myPoints->length() ) {
365   myPoints->length( myOrderedSel.size()  );
366   k = 0;
367   for ( std::list<GEOM::GEOM_Object_var>::iterator j = myOrderedSel.begin(); j != myOrderedSel.end(); j++ )
368     myPoints[k++] = *j;
369   //  }
370
371   if ( myPoints->length() > 0  )
372     GroupPoints->LineEdit1->setText( QString::number( myPoints->length() ) + "_" + tr( "GEOM_POINT" ) + tr( "_S_" ) );
373
374   displayPreview();
375 }
376
377
378 //=================================================================================
379 // function : ActivateThisDialog()
380 // purpose  :
381 //=================================================================================
382 void BasicGUI_CurveDlg::ActivateThisDialog()
383 {
384   GEOMBase_Skeleton::ActivateThisDialog();
385   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
386            this, SLOT( SelectionIntoArgument() ) );
387
388   globalSelection(); // close local contexts, if any
389   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
390
391   ConstructorsClicked( getConstructorId() );
392 }
393
394 //=================================================================================
395 // function : DeactivateActiveDialog()
396 // purpose  : public slot to deactivate if active
397 //=================================================================================
398 void BasicGUI_CurveDlg::DeactivateActiveDialog()
399 {
400   // myGeomGUI->SetState( -1 );
401   GEOMBase_Skeleton::DeactivateActiveDialog();
402 }
403
404 //=================================================================================
405 // function : enterEvent()
406 // purpose  :
407 //=================================================================================
408 void BasicGUI_CurveDlg::enterEvent( QEvent* )
409 {
410   if ( !mainFrame()->GroupConstructors->isEnabled() )
411     ActivateThisDialog();
412 }
413
414 //=================================================================================
415 // function : createOperation
416 // purpose  :
417 //=================================================================================
418 GEOM::GEOM_IOperations_ptr BasicGUI_CurveDlg::createOperation()
419 {
420   return myGeomGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
421 }
422
423 //=================================================================================
424 // function : isValid
425 // purpose  :
426 //=================================================================================
427 bool BasicGUI_CurveDlg::isValid( QString& msg )
428 {
429   return myPoints->length() > 1;
430 }
431
432 //=================================================================================
433 // function : execute
434 // purpose  :
435 //=================================================================================
436 bool BasicGUI_CurveDlg::execute( ObjectList& objects )
437 {
438   bool res = false;
439
440   GEOM::GEOM_Object_var anObj;
441
442   GEOM::GEOM_ICurvesOperations_var anOper = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() );
443
444   switch ( getConstructorId() ) {
445   case 0 :
446     anObj = anOper->MakePolyline( myPoints );
447     res = true;
448     break;
449   case 1 :
450     anObj = anOper->MakeSplineBezier( myPoints );
451     res = true;
452     break;
453   case 2 :
454     anObj = anOper->MakeSplineInterpolation( myPoints, GroupPoints->CheckButton1->isChecked() );
455     res = true;
456     break;
457   }
458
459   if ( !anObj->_is_nil() )
460     objects.push_back( anObj._retn() );
461
462   return res;
463 }