]> SALOME platform Git repositories - modules/geom.git/blob - src/BasicGUI/BasicGUI_CurveDlg.cxx
Salome HOME
NPAL18111: Bad shape build by partition.
[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 //
22 //
23 //  File   : BasicGUI_CurveDlg.cxx
24 //  Author : Nicolas REJNERI
25 //  Module : GEOM
26 //  $Header$
27
28 #include "BasicGUI_CurveDlg.h"
29
30 #include "SUIT_Desktop.h"
31 #include "SUIT_Session.h"
32 #include "SalomeApp_Application.h"
33 #include "LightApp_SelectionMgr.h"
34
35 #include <qlabel.h>
36
37 #include "utilities.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 using namespace std;
51 #include <string>
52
53 //=================================================================================
54 // class    : BasicGUI_CurveDlg()
55 // purpose  : Constructs a BasicGUI_CurveDlg which is a child of 'parent', with the 
56 //            name 'name' and widget flags set to 'f'.
57 //            The dialog will by default be modeless, unless you set 'modal' to
58 //            TRUE to construct a modal dialog.
59 //=================================================================================
60 BasicGUI_CurveDlg::BasicGUI_CurveDlg(GeometryGUI* theGeometryGUI, QWidget* parent,
61                                      const char* name, bool modal, WFlags fl)
62   :GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, WStyle_Customize |
63                      WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
64 {
65   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_POLYLINE")));
66   QPixmap image2(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_SPLINE")));
67   QPixmap image3(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_BEZIER")));
68
69   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
70
71   setCaption(tr("GEOM_CURVE_TITLE"));
72
73   /***************************************************************/
74   RadioButton1->setPixmap( image0 );
75   RadioButton2->setPixmap( image3 );
76   RadioButton3->setPixmap( image2 );
77
78   GroupPoints = new DlgRef_1Sel_QTD( this, "GroupPoints" );
79   GroupPoints->GroupBox1->setTitle( tr( "GEOM_NODES" ) );
80   GroupPoints->TextLabel1->setText( tr("GEOM_POINTS") );
81   GroupPoints->PushButton1->setPixmap(image1);
82
83   GroupPoints->LineEdit1->setReadOnly( true );
84
85   Layout1->addWidget(GroupPoints, 2, 0);
86   /***************************************************************/
87
88   setHelpFileName("curve.htm");
89
90   Init();
91 }
92
93
94 //=================================================================================
95 // function : ~BasicGUI_CurveDlg()
96 // purpose  : Destroys the object and frees any allocated resources
97 //=================================================================================
98 BasicGUI_CurveDlg::~BasicGUI_CurveDlg()
99 {
100 }
101
102
103 //=================================================================================
104 // function : Init()
105 // purpose  :
106 //=================================================================================
107 void BasicGUI_CurveDlg::Init()
108 {
109   /* init variables */
110   myEditCurrentArgument = GroupPoints->LineEdit1;
111
112   myPoints = new GEOM::ListOfGO();
113   myPoints->length( 0 );
114
115   //  globalSelection( GEOM_POINT );
116   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
117
118   /* signals and slots connections */
119   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
120   connect(myGeomGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
121   connect(myGeomGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
122   
123   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
124   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
125   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
126
127   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
128   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
129
130   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
131           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
132
133   initName( tr( "GEOM_CURVE" ) );
134   ConstructorsClicked( 0 );
135 }
136
137 //=================================================================================
138 // function : ConstructorsClicked()
139 // purpose  :
140 //=================================================================================
141 void BasicGUI_CurveDlg::ConstructorsClicked( int id )
142 {
143   QString aTitle = tr( id == 0 ? "GEOM_POLYLINE" : id == 1 ? "GEOM_BEZIER" : "GEOM_INTERPOL" );
144   GroupConstructors->setTitle( aTitle );
145         
146         myPoints = new GEOM::ListOfGO();
147   myPoints->length( 0 );  
148
149   myEditCurrentArgument->setText("");
150 }
151
152
153 //=================================================================================
154 // function : SetEditCurrentArgument()
155 // purpose  :
156 //=================================================================================
157 void BasicGUI_CurveDlg::SetEditCurrentArgument()
158 {
159   if ( sender() == GroupPoints->PushButton1 ) 
160     myEditCurrentArgument = GroupPoints->LineEdit1;
161   myEditCurrentArgument->setFocus();
162   SelectionIntoArgument();
163 }
164
165
166 //=================================================================================
167 // function : LineEditReturnPressed()
168 // purpose  :
169 //=================================================================================
170 void BasicGUI_CurveDlg::LineEditReturnPressed()
171 {
172   if ( sender() == GroupPoints->LineEdit1 ) 
173   {
174     myEditCurrentArgument = GroupPoints->LineEdit1;
175     GEOMBase_Skeleton::LineEditReturnPressed();
176   }
177 }
178
179 //=================================================================================
180 // function : ClickOnOk()
181 // purpose  :
182 //=================================================================================
183 void BasicGUI_CurveDlg::ClickOnOk()
184 {
185   if ( ClickOnApply() )
186     ClickOnCancel();
187 }
188
189 //=================================================================================
190 // function : ClickOnApply()
191 // purpose  :
192 //=================================================================================
193 bool BasicGUI_CurveDlg::ClickOnApply()
194 {
195   if ( !onAccept() )
196     return false;
197
198   initName();
199   ConstructorsClicked( getConstructorId() );
200   return true;
201 }
202
203 //=======================================================================
204 // function : ClickOnCancel()
205 // purpose  :
206 //=======================================================================
207 void BasicGUI_CurveDlg::ClickOnCancel()
208 {
209   GEOMBase_Skeleton::ClickOnCancel();
210 }
211
212 //=================================================================================
213 /*! function : isPointInList()
214  *  purpose  : Check is point (theObject) in the list \a thePoints.
215  * \author enk
216  * \retval -1, if point not in list, else 1 in list
217  */
218 //=================================================================================
219 static int isPointInList(list<GEOM::GEOM_Object_var>& thePoints,
220                          GEOM::GEOM_Object_var& theObject)
221 {
222   int len = thePoints.size();
223   
224   if(len<1){
225     return -1;
226   }
227   
228   for(list<GEOM::GEOM_Object_var>::iterator i=thePoints.begin();i!=thePoints.end();i++)
229     if (string((*i)->GetEntry()) == string(theObject->GetEntry())){
230       return 1;
231     }
232
233   return -1;
234 }
235 //=================================================================================
236 /*! function : removeUnnecessaryPnt()
237  *  purpose  : Remove unnecessary points from list \a theOldPoints
238  * \author enk
239  * \li \a theOldPoints - ordered sequence with unnecessary point
240  * \li \a theNewPoints - not ordered sequence with necessary points
241  */
242 //=================================================================================
243 static void removeUnnecessaryPnt(list<GEOM::GEOM_Object_var>& theOldPoints,
244                                  GEOM::ListOfGO_var& theNewPoints)
245 {
246   list<GEOM::GEOM_Object_var> objs_to_remove;
247   for(list<GEOM::GEOM_Object_var>::iterator i=theOldPoints.begin();i!=theOldPoints.end();i++){
248     bool found = false;
249     for (int j=0;j<theNewPoints->length() && !found ; j++){
250       if(string((*i)->GetEntry()) == string(theNewPoints[j]->GetEntry())){
251         found = true;
252       }
253     }
254     if(!found){
255       objs_to_remove.push_back(*i);
256       //cout << "removed: " << (*i)->GetEntry() << endl;
257     }
258   }
259   for(list<GEOM::GEOM_Object_var>::iterator i=objs_to_remove.begin();i!=objs_to_remove.end();i++){
260     theOldPoints.remove(*i);
261   }
262 }
263
264 //=================================================================================
265 // function : SelectionIntoArgument()
266 // purpose  : Called when selection as changed or other case
267 //=================================================================================
268 void BasicGUI_CurveDlg::SelectionIntoArgument()
269 {
270   myEditCurrentArgument->setText("");
271
272   Standard_Boolean aRes = Standard_False;
273   int i = 0;
274   int IOC = IObjectCount();
275   bool is_append = myPoints->length() < IOC; // if true - add point, else remove
276   myPoints->length( IOC ); // this length may be greater than number of objects,
277                            // that will actually be put into myPoints
278   for ( SALOME_ListIteratorOfListIO anIt( selectedIO() ); anIt.More(); anIt.Next() )
279     {
280       GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( anIt.Value(), aRes );
281       if ( !CORBA::is_nil( aSelectedObject ) && aRes )
282         {
283           //TopoDS_Shape aPointShape;
284           //if ( myGeomBase->GetShape( aSelectedObject, aPointShape, TopAbs_VERTEX ) )
285
286           TopoDS_Shape aShape;
287           if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() )
288             {
289               LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
290               TColStd_IndexedMapOfInteger aMap;
291               aSelMgr->GetIndexes( anIt.Value(), aMap );
292               if ( aMap.Extent() == 1 )
293                 {
294                   GEOM::GEOM_IShapesOperations_var aShapesOp =
295                     getGeomEngine()->GetIShapesOperations( getStudyId() );
296                   int anIndex = aMap( 1 );
297                   TopTools_IndexedMapOfShape aShapes;
298                   TopExp::MapShapes( aShape, aShapes );
299                   aShape = aShapes.FindKey( anIndex );
300                   aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
301                   aSelMgr->clearSelected();
302                 }
303           }
304
305           int pos = isPointInList(myOrderedSel,aSelectedObject);
306           if(is_append && pos==-1)
307           myOrderedSel.push_back(aSelectedObject);
308
309           myPoints[i++] = aSelectedObject;
310         }
311     }
312
313   myPoints->length( i ); // this is the right length, smaller of equal to the previously set
314   if(IOC == 0)
315     myOrderedSel.clear();
316   else
317     removeUnnecessaryPnt(myOrderedSel,myPoints);
318
319   if(myOrderedSel.size() == myPoints->length()){
320     int k=0;
321     for (list<GEOM::GEOM_Object_var>::iterator j=myOrderedSel.begin();j!=myOrderedSel.end();j++)
322       myPoints[k++] = *j;
323   } else {
324     //cout << "ERROR: Ordered sequence size != selection sequence size! ("<<myOrderedSel.size()<<"!="<<myPoints->length()<<")"<<endl;
325   }
326   if ( i )
327     GroupPoints->LineEdit1->setText( QString::number( i ) + "_" + tr( "GEOM_POINT" ) + tr( "_S_" ) );
328   
329   displayPreview(); 
330 }
331
332
333 //=================================================================================
334 // function : ActivateThisDialog()
335 // purpose  :
336 //=================================================================================
337 void BasicGUI_CurveDlg::ActivateThisDialog()
338 {
339   GEOMBase_Skeleton::ActivateThisDialog();
340   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
341           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
342
343   // myGeomGUI->SetState( 0 );
344
345   //  globalSelection( GEOM_POINT );
346   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
347   ConstructorsClicked( getConstructorId() );
348 }
349
350 //=================================================================================
351 // function : DeactivateActiveDialog()
352 // purpose  : public slot to deactivate if active
353 //=================================================================================
354 void BasicGUI_CurveDlg::DeactivateActiveDialog()
355 {
356   // myGeomGUI->SetState( -1 );
357   GEOMBase_Skeleton::DeactivateActiveDialog();
358 }
359
360 //=================================================================================
361 // function : enterEvent()
362 // purpose  :
363 //=================================================================================
364 void BasicGUI_CurveDlg::enterEvent(QEvent* e)
365 {
366   if ( !GroupConstructors->isEnabled() )
367     ActivateThisDialog();
368 }
369
370 //=================================================================================
371 // function : createOperation
372 // purpose  :
373 //=================================================================================
374 GEOM::GEOM_IOperations_ptr BasicGUI_CurveDlg::createOperation()
375 {
376   return myGeomGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
377 }
378
379 //=================================================================================
380 // function : isValid
381 // purpose  :
382 //=================================================================================
383 bool BasicGUI_CurveDlg::isValid( QString& msg )
384 {
385   return myPoints->length() > 1;
386 }
387
388 //=================================================================================
389 // function : execute
390 // purpose  :
391 //=================================================================================
392 bool BasicGUI_CurveDlg::execute( ObjectList& objects )
393 {
394   bool res = false;
395
396   GEOM::GEOM_Object_var anObj;
397
398   switch ( getConstructorId() )
399   {
400   case 0 :
401     anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakePolyline( myPoints );
402     res = true;
403     break;
404   case 1 :
405     anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeSplineBezier( myPoints );
406     res = true;
407     break;
408   case 2 :
409     anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeSplineInterpolation( myPoints );
410     res = true;
411     break;
412   }
413
414   if ( !anObj->_is_nil() )
415     objects.push_back( anObj._retn() );
416
417   return res;
418 }
419
420 //=================================================================================
421 // function : closeEvent
422 // purpose  :
423 //=================================================================================
424 void BasicGUI_CurveDlg::closeEvent( QCloseEvent* e )
425 {
426   GEOMBase_Skeleton::closeEvent( e );
427 }
428