Salome HOME
598c5f47bd1a6c56dd961d15640c3eaf32d231df
[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 point 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   for(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(string((*i)->GetEntry()) == string(theNewPoints[j]->GetEntry())){
240         found = true;
241       }
242     }
243     if(!found){
244       theOldPoints.remove(*i);
245       return;
246     }
247   }
248 }
249
250 //=================================================================================
251 // function : SelectionIntoArgument()
252 // purpose  : Called when selection as changed or other case
253 //=================================================================================
254 void BasicGUI_CurveDlg::SelectionIntoArgument()
255 {
256   myEditCurrentArgument->setText("");
257
258   Standard_Boolean aRes = Standard_False;
259   int i = 0;
260   int IOC = IObjectCount();
261   bool is_append = myPoints->length() < IOC; // if true - add point, else remove
262   myPoints->length( IOC ); // this length may be greater than number of objects,
263                            // that will actually be put into myPoints
264   for ( SALOME_ListIteratorOfListIO anIt( selectedIO() ); anIt.More(); anIt.Next() )
265     {
266       GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( anIt.Value(), aRes );
267       if ( !CORBA::is_nil( aSelectedObject ) && aRes )
268         {
269           //TopoDS_Shape aPointShape;
270           //if ( myGeomBase->GetShape( aSelectedObject, aPointShape, TopAbs_VERTEX ) )
271           int pos = isPointInList(myOrderedSel,aSelectedObject);
272           if(is_append && pos==-1)
273             myOrderedSel.push_back(aSelectedObject);
274           myPoints[i++] = aSelectedObject;
275         }
276     }
277
278   myPoints->length( i ); // this is the right length, smaller of equal to the previously set
279   if(IOC == 0)
280     myOrderedSel.clear();
281   else
282     removeUnnecessaryPnt(myOrderedSel,myPoints);
283
284   if(myOrderedSel.size() == myPoints->length()){
285     int k=0;
286     for (list<GEOM::GEOM_Object_var>::iterator j=myOrderedSel.begin();j!=myOrderedSel.end();j++)
287       myPoints[k++] = *j;
288   } else {
289     cout << "ERROR: Ordered sequence size != selection sequence size! ("<<myOrderedSel.size()<<"!="<<myPoints->length()<<")"<<endl;
290   }
291   if ( i )
292     GroupPoints->LineEdit1->setText( QString::number( i ) + "_" + tr( "GEOM_POINT" ) + tr( "_S_" ) );
293   
294   displayPreview(); 
295 }
296
297
298 //=================================================================================
299 // function : ActivateThisDialog()
300 // purpose  :
301 //=================================================================================
302 void BasicGUI_CurveDlg::ActivateThisDialog()
303 {
304   GEOMBase_Skeleton::ActivateThisDialog();
305   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
306           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
307
308   // myGeometryGUI->SetState( 0 );
309
310   globalSelection( GEOM_POINT );
311   ConstructorsClicked( getConstructorId() );
312 }
313
314 //=================================================================================
315 // function : DeactivateActiveDialog()
316 // purpose  : public slot to deactivate if active
317 //=================================================================================
318 void BasicGUI_CurveDlg::DeactivateActiveDialog()
319 {
320   // myGeometryGUI->SetState( -1 );
321   GEOMBase_Skeleton::DeactivateActiveDialog();
322 }
323
324 //=================================================================================
325 // function : enterEvent()
326 // purpose  :
327 //=================================================================================
328 void BasicGUI_CurveDlg::enterEvent(QEvent* e)
329 {
330   if ( !GroupConstructors->isEnabled() )
331     ActivateThisDialog();
332 }
333
334 //=================================================================================
335 // function : createOperation
336 // purpose  :
337 //=================================================================================
338 GEOM::GEOM_IOperations_ptr BasicGUI_CurveDlg::createOperation()
339 {
340   return myGeometryGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
341 }
342
343 //=================================================================================
344 // function : isValid
345 // purpose  :
346 //=================================================================================
347 bool BasicGUI_CurveDlg::isValid( QString& msg )
348 {
349   return myPoints->length() > 1;
350 }
351
352 //=================================================================================
353 // function : execute
354 // purpose  :
355 //=================================================================================
356 bool BasicGUI_CurveDlg::execute( ObjectList& objects )
357 {
358   bool res = false;
359
360   GEOM::GEOM_Object_var anObj;
361
362   switch ( getConstructorId() )
363   {
364   case 0 :
365     anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakePolyline( myPoints );
366     res = true;
367     break;
368   case 1 :
369     anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeSplineBezier( myPoints );
370     res = true;
371     break;
372   case 2 :
373     anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeSplineInterpolation( myPoints );
374     res = true;
375     break;
376   }
377
378   if ( !anObj->_is_nil() )
379     objects.push_back( anObj._retn() );
380
381   return res;
382 }
383
384 //=================================================================================
385 // function : closeEvent
386 // purpose  :
387 //=================================================================================
388 void BasicGUI_CurveDlg::closeEvent( QCloseEvent* e )
389 {
390   // myGeometryGUI->SetState( -1 );
391   GEOMBase_Skeleton::closeEvent( e );
392 }
393