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