]> SALOME platform Git repositories - modules/geom.git/blob - src/BasicGUI/BasicGUI_CurveDlg.cxx
Salome HOME
Merge from V5_1_2_BR branch (14 July 2009)
[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_1Sel( 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   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
82   layout->setMargin( 0 ); layout->setSpacing( 6 );
83   layout->addWidget( GroupPoints );
84   /***************************************************************/
85
86   setHelpFileName( "create_curve_page.html" );
87
88   Init();
89 }
90
91
92 //=================================================================================
93 // function : ~BasicGUI_CurveDlg()
94 // purpose  : Destroys the object and frees any allocated resources
95 //=================================================================================
96 BasicGUI_CurveDlg::~BasicGUI_CurveDlg()
97 {
98 }
99
100
101 //=================================================================================
102 // function : Init()
103 // purpose  :
104 //=================================================================================
105 void BasicGUI_CurveDlg::Init()
106 {
107   /* init variables */
108   myEditCurrentArgument = GroupPoints->LineEdit1;
109
110   myPoints = new GEOM::ListOfGO();
111   myPoints->length( 0 );
112
113   globalSelection(); // close local contexts, if any
114   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
115
116   /* signals and slots connections */
117   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog( ) ) );
118   connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
119   
120   connect( buttonOk(),     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
121   connect( buttonApply(),  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
122
123   connect( this,           SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
124
125   connect( GroupPoints->PushButton1, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
126   connect( GroupPoints->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
127
128   connect( myGeomGUI->getApp()->selectionMgr(), 
129            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) ) ;
130  
131   initName( tr( "GEOM_CURVE" ) );
132   resize(100,100);
133   ConstructorsClicked( 0 );
134 }
135
136 //=================================================================================
137 // function : ConstructorsClicked()
138 // purpose  :
139 //=================================================================================
140 void BasicGUI_CurveDlg::ConstructorsClicked( int id )
141 {
142   QString aTitle = tr( id == 0 ? "GEOM_POLYLINE" : id == 1 ? "GEOM_BEZIER" : "GEOM_INTERPOL" );
143   mainFrame()->GroupConstructors->setTitle( aTitle );
144         
145   myPoints = new GEOM::ListOfGO();
146   myPoints->length( 0 );  
147
148   myEditCurrentArgument->setText( "" );
149 }
150
151
152 //=================================================================================
153 // function : SetEditCurrentArgument()
154 // purpose  :
155 //=================================================================================
156 void BasicGUI_CurveDlg::SetEditCurrentArgument()
157 {
158   if ( sender() == GroupPoints->PushButton1 ) 
159     myEditCurrentArgument = GroupPoints->LineEdit1;
160   myEditCurrentArgument->setFocus();
161   SelectionIntoArgument();
162 }
163
164
165 //=================================================================================
166 // function : LineEditReturnPressed()
167 // purpose  :
168 //=================================================================================
169 void BasicGUI_CurveDlg::LineEditReturnPressed()
170 {
171   if ( sender() == GroupPoints->LineEdit1 ) 
172   {
173     myEditCurrentArgument = GroupPoints->LineEdit1;
174     GEOMBase_Skeleton::LineEditReturnPressed();
175   }
176 }
177
178 //=================================================================================
179 // function : ClickOnOk()
180 // purpose  :
181 //=================================================================================
182 void BasicGUI_CurveDlg::ClickOnOk()
183 {
184   if ( ClickOnApply() )
185     ClickOnCancel();
186 }
187
188 //=================================================================================
189 // function : ClickOnApply()
190 // purpose  :
191 //=================================================================================
192 bool BasicGUI_CurveDlg::ClickOnApply()
193 {
194   if ( !onAccept() )
195     return false;
196
197   initName();
198   globalSelection(); // close local contexts, if any
199   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
200   return true;
201 }
202
203 //=================================================================================
204 /*! function : isPointInList()
205  *  purpose  : Check is point (theObject) in the list \a thePoints.
206  * \author enk
207  * \retval -1, if point not in list, else 1 in list
208  */
209 //=================================================================================
210 static int isPointInList( std::list<GEOM::GEOM_Object_var>& thePoints,
211                           GEOM::GEOM_Object_var& theObject )
212 {
213   int len = thePoints.size();
214   
215   if ( len < 1 ) {
216     return -1;
217   }
218   
219   for ( std::list<GEOM::GEOM_Object_var>::iterator i = thePoints.begin(); i != thePoints.end(); i++ ) {
220     if ( std::string( (*i)->GetEntry() ) == std::string( theObject->GetEntry() ) ) {
221       return 1;
222     }
223   }
224
225   return -1;
226 }
227 //=================================================================================
228 /*! function : removeUnnecessaryPnt()
229  *  purpose  : Remove unnecessary points from list \a theOldPoints
230  * \author enk
231  * \li \a theOldPoints - ordered sequence with unnecessary point
232  * \li \a theNewPoints - not ordered sequence with necessary points
233  */
234 //=================================================================================
235 static void removeUnnecessaryPnt( std::list<GEOM::GEOM_Object_var>& theOldPoints,
236                                   GEOM::ListOfGO_var& theNewPoints )
237 {
238   std::list<GEOM::GEOM_Object_var> objs_to_remove;
239   for ( std::list<GEOM::GEOM_Object_var>::iterator i = theOldPoints.begin(); i != theOldPoints.end(); i++ ) {
240     bool found = false;
241     for ( int j = 0; j < theNewPoints->length() && !found ; j++ ) {
242       if ( std::string( (*i)->GetEntry() ) == std::string( theNewPoints[j]->GetEntry() ) ) {
243         found = true;
244       }
245     }
246     if ( !found ) {
247       objs_to_remove.push_back( *i );
248       //cout << "removed: " << (*i)->GetEntry() << endl;
249     }
250   }
251   for ( std::list<GEOM::GEOM_Object_var>::iterator i = objs_to_remove.begin(); i != objs_to_remove.end(); i++ ) {
252     theOldPoints.remove( *i );
253   }
254 }
255
256 //=================================================================================
257 // function : SelectionIntoArgument()
258 // purpose  : Called when selection as changed or other case
259 //=================================================================================
260 void BasicGUI_CurveDlg::SelectionIntoArgument()
261 {
262   myEditCurrentArgument->setText( "" );
263
264   Standard_Boolean aRes = Standard_False;
265
266   SalomeApp_Application* app = myGeomGUI->getApp();
267   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
268   _PTR(Study) aDStudy = appStudy->studyDS();
269   GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations(getStudyId());
270
271   int anIndex;
272   TopoDS_Shape aShape;
273   TColStd_IndexedMapOfInteger aMapIndexes;
274   GEOM::GEOM_Object_var anObject;
275   std::list<GEOM::GEOM_Object_var> aList;
276   LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
277   SALOME_ListIO selected;
278   aSelMgr->selectedObjects(selected, QString::null, false);
279
280   int IOC = selected.Extent();
281   // bool is_append = myPoints->length() < IOC; // if true - add point, else remove
282   // myPoints->length( IOC ); // this length may be greater than number of objects,
283                            // that will actually be put into myPoints
284   
285   for (SALOME_ListIteratorOfListIO anIt (selected); anIt.More(); anIt.Next()) {
286     GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(anIt.Value(), aRes);
287     if (!CORBA::is_nil(aSelectedObject) && aRes) {
288       if (GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE) && !aShape.IsNull()) {
289         aSelMgr->GetIndexes(anIt.Value(), aMapIndexes);
290         
291         if (aMapIndexes.Extent() > 0) {
292           for (int ii = 1; ii <= aMapIndexes.Extent(); ii++) {
293             anIndex = aMapIndexes(ii);
294             QString aName = GEOMBase::GetName( aSelectedObject );
295             aName = aName + ":vertex_" + QString::number( anIndex );
296             anObject = aShapesOp->GetSubShape( aSelectedObject, anIndex );
297             //Find Object in study
298             _PTR(SObject) obj ( aDStudy->FindObjectID( anIt.Value()->getEntry() ) );
299             bool inStudy = false;
300             _PTR(ChildIterator) iit( aDStudy->NewChildIterator( obj ) );
301             for (; iit->More() && !inStudy; iit->Next()) {
302               _PTR(SObject) child( iit->Value() );
303               QString aChildName = child->GetName().c_str();
304               if ( aChildName == aName ) {
305                 inStudy = true;
306                 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject( iit->Value() );
307                 anObject = GEOM::GEOM_Object::_narrow( corbaObj );
308               }
309             }
310             
311             if ( !inStudy )
312               GeometryGUI::GetGeomGen()->AddInStudy( GeometryGUI::ClientStudyToStudy( aDStudy ),
313                                                      anObject, aName.toLatin1().data(), aSelectedObject );
314             
315             int pos = isPointInList( myOrderedSel, anObject );
316             if ( pos == -1 ) {
317               myOrderedSel.push_back( anObject );
318             }
319             //              if (!inStudy)
320             aList.push_back(anObject);
321           }
322         } 
323         else { // aMap.Extent() == 0
324           int pos = isPointInList( myOrderedSel, aSelectedObject );
325           if ( pos == -1 )
326             myOrderedSel.push_back( aSelectedObject );
327           aList.push_back( aSelectedObject );
328         } 
329       }
330     }
331   }
332   
333   myPoints->length( aList.size()  );  
334
335   int k = 0;
336   for ( std::list<GEOM::GEOM_Object_var>::iterator j = aList.begin(); j != aList.end(); j++ )
337     myPoints[k++] = *j;
338
339   if ( IOC == 0 )
340     myOrderedSel.clear();
341   else
342     removeUnnecessaryPnt( myOrderedSel, myPoints );
343
344   // if ( myOrderedSel.size() == myPoints->length() ) {
345   myPoints->length( myOrderedSel.size()  );  
346   k = 0;
347   for ( std::list<GEOM::GEOM_Object_var>::iterator j = myOrderedSel.begin(); j != myOrderedSel.end(); j++ )
348     myPoints[k++] = *j;
349   //  }
350
351   if ( myPoints->length() > 0  )
352     GroupPoints->LineEdit1->setText( QString::number( myPoints->length() ) + "_" + tr( "GEOM_POINT" ) + tr( "_S_" ) );
353    
354 displayPreview(); 
355 }
356
357
358 //=================================================================================
359 // function : ActivateThisDialog()
360 // purpose  :
361 //=================================================================================
362 void BasicGUI_CurveDlg::ActivateThisDialog()
363 {
364   GEOMBase_Skeleton::ActivateThisDialog();
365   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
366            this, SLOT( SelectionIntoArgument() ) );
367
368   globalSelection(); // close local contexts, if any
369   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
370
371   ConstructorsClicked( getConstructorId() );
372 }
373
374 //=================================================================================
375 // function : DeactivateActiveDialog()
376 // purpose  : public slot to deactivate if active
377 //=================================================================================
378 void BasicGUI_CurveDlg::DeactivateActiveDialog()
379 {
380   // myGeomGUI->SetState( -1 );
381   GEOMBase_Skeleton::DeactivateActiveDialog();
382 }
383
384 //=================================================================================
385 // function : enterEvent()
386 // purpose  :
387 //=================================================================================
388 void BasicGUI_CurveDlg::enterEvent( QEvent* )
389 {
390   if ( !mainFrame()->GroupConstructors->isEnabled() )
391     ActivateThisDialog();
392 }
393
394 //=================================================================================
395 // function : createOperation
396 // purpose  :
397 //=================================================================================
398 GEOM::GEOM_IOperations_ptr BasicGUI_CurveDlg::createOperation()
399 {
400   return myGeomGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
401 }
402
403 //=================================================================================
404 // function : isValid
405 // purpose  :
406 //=================================================================================
407 bool BasicGUI_CurveDlg::isValid( QString& msg )
408 {
409   return myPoints->length() > 1;
410 }
411
412 //=================================================================================
413 // function : execute
414 // purpose  :
415 //=================================================================================
416 bool BasicGUI_CurveDlg::execute( ObjectList& objects )
417 {
418   bool res = false;
419
420   GEOM::GEOM_Object_var anObj;
421
422   switch ( getConstructorId() ) {
423   case 0 :
424     anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakePolyline( myPoints );
425     res = true;
426     break;
427   case 1 :
428     anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeSplineBezier( myPoints );
429     res = true;
430     break;
431   case 2 :
432     anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeSplineInterpolation( myPoints );
433     res = true;
434     break;
435   }
436
437   if ( !anObj->_is_nil() )
438     objects.push_back( anObj._retn() );
439
440   return res;
441 }