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