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