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