Salome HOME
Merge from V4_1_0_maintainance branch (from tag mergeto_BR_QT4_Dev_08Jul08)
[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             _PTR(ChildIterator) iit( aDStudy->NewChildIterator( obj ) );
299             for (; iit->More() && !inStudy; iit->Next()) {
300               _PTR(SObject) child( iit->Value() );
301               QString aChildName = child->GetName().c_str();
302               if ( aChildName == aName ) {
303                 inStudy = true;
304                 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject( iit->Value() );
305                 anObject = GEOM::GEOM_Object::_narrow( corbaObj );
306               }
307             }
308             
309             if ( !inStudy )
310               GeometryGUI::GetGeomGen()->AddInStudy( GeometryGUI::ClientStudyToStudy( aDStudy ),
311                                                      anObject, aName.toLatin1().data(), aSelectedObject );
312             
313             int pos = isPointInList( myOrderedSel, anObject );
314             if ( pos == -1 ) {
315               myOrderedSel.push_back( anObject );
316             }
317             //              if (!inStudy)
318             aList.push_back(anObject);
319           }
320         } 
321         else { // aMap.Extent() == 0
322           int pos = isPointInList( myOrderedSel, aSelectedObject );
323           if ( pos == -1 )
324             myOrderedSel.push_back( aSelectedObject );
325           aList.push_back( aSelectedObject );
326         } 
327       }
328     }
329   }
330   
331   myPoints->length( aList.size()  );  
332
333   int k = 0;
334   for ( std::list<GEOM::GEOM_Object_var>::iterator j = aList.begin(); j != aList.end(); j++ )
335     myPoints[k++] = *j;
336
337   if ( IOC == 0 )
338     myOrderedSel.clear();
339   else
340     removeUnnecessaryPnt( myOrderedSel, myPoints );
341
342   // if ( myOrderedSel.size() == myPoints->length() ) {
343   myPoints->length( myOrderedSel.size()  );  
344   k = 0;
345   for ( std::list<GEOM::GEOM_Object_var>::iterator j = myOrderedSel.begin(); j != myOrderedSel.end(); j++ )
346     myPoints[k++] = *j;
347   //  }
348
349   if ( myPoints->length() > 0  )
350     GroupPoints->LineEdit1->setText( QString::number( myPoints->length() ) + "_" + tr( "GEOM_POINT" ) + tr( "_S_" ) );
351    
352 displayPreview(); 
353 }
354
355
356 //=================================================================================
357 // function : ActivateThisDialog()
358 // purpose  :
359 //=================================================================================
360 void BasicGUI_CurveDlg::ActivateThisDialog()
361 {
362   GEOMBase_Skeleton::ActivateThisDialog();
363   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
364            this, SLOT( SelectionIntoArgument() ) );
365
366   globalSelection(); // close local contexts, if any
367   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
368
369   ConstructorsClicked( getConstructorId() );
370 }
371
372 //=================================================================================
373 // function : DeactivateActiveDialog()
374 // purpose  : public slot to deactivate if active
375 //=================================================================================
376 void BasicGUI_CurveDlg::DeactivateActiveDialog()
377 {
378   // myGeomGUI->SetState( -1 );
379   GEOMBase_Skeleton::DeactivateActiveDialog();
380 }
381
382 //=================================================================================
383 // function : enterEvent()
384 // purpose  :
385 //=================================================================================
386 void BasicGUI_CurveDlg::enterEvent( QEvent* )
387 {
388   if ( !mainFrame()->GroupConstructors->isEnabled() )
389     ActivateThisDialog();
390 }
391
392 //=================================================================================
393 // function : createOperation
394 // purpose  :
395 //=================================================================================
396 GEOM::GEOM_IOperations_ptr BasicGUI_CurveDlg::createOperation()
397 {
398   return myGeomGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
399 }
400
401 //=================================================================================
402 // function : isValid
403 // purpose  :
404 //=================================================================================
405 bool BasicGUI_CurveDlg::isValid( QString& msg )
406 {
407   return myPoints->length() > 1;
408 }
409
410 //=================================================================================
411 // function : execute
412 // purpose  :
413 //=================================================================================
414 bool BasicGUI_CurveDlg::execute( ObjectList& objects )
415 {
416   bool res = false;
417
418   GEOM::GEOM_Object_var anObj;
419
420   switch ( getConstructorId() ) {
421   case 0 :
422     anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakePolyline( myPoints );
423     res = true;
424     break;
425   case 1 :
426     anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeSplineBezier( myPoints );
427     res = true;
428     break;
429   case 2 :
430     anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeSplineInterpolation( myPoints );
431     res = true;
432     break;
433   }
434
435   if ( !anObj->_is_nil() )
436     objects.push_back( anObj._retn() );
437
438   return res;
439 }