Salome HOME
Merge with version on tag OCC-V2_1_0d
[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 "QAD_Desktop.h"
31 #include "QAD_Config.h"
32 #include "utilities.h"
33
34 #include "SALOME_ListIteratorOfListIO.hxx"
35 #include "SALOME_ListIO.hxx"
36
37 #include "GEOMImpl_Types.hxx"
38
39 using namespace std;
40
41 //=================================================================================
42 // class    : BasicGUI_CurveDlg()
43 // purpose  : Constructs a BasicGUI_CurveDlg which is a child of 'parent', with the 
44 //            name 'name' and widget flags set to 'f'.
45 //            The dialog will by default be modeless, unless you set 'modal' to
46 //            TRUE to construct a modal dialog.
47 //=================================================================================
48 BasicGUI_CurveDlg::BasicGUI_CurveDlg(QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl)
49   :GEOMBase_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
50 {
51   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_POLYLINE")));
52   QPixmap image2(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_SPLINE")));
53   QPixmap image3(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_BEZIER")));
54
55   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
56
57   setCaption(tr("GEOM_CURVE_TITLE"));
58   
59   /***************************************************************/
60   RadioButton1->setPixmap( image0 );
61   RadioButton2->setPixmap( image3 );
62   RadioButton3->setPixmap( image2 );
63
64   GroupPoints = new DlgRef_1Sel_QTD( this, "GroupPoints" );
65   GroupPoints->GroupBox1->setTitle( tr( "GEOM_NODES" ) );
66   GroupPoints->TextLabel1->setText( tr("GEOM_POINTS") );
67   GroupPoints->PushButton1->setPixmap(image1);
68   
69   GroupPoints->LineEdit1->setReadOnly( true );
70
71   Layout1->addWidget(GroupPoints, 2, 0);
72   /***************************************************************/
73
74   Init();
75 }
76
77
78 //=================================================================================
79 // function : ~BasicGUI_CurveDlg()
80 // purpose  : Destroys the object and frees any allocated resources
81 //=================================================================================
82 BasicGUI_CurveDlg::~BasicGUI_CurveDlg()
83 {
84 }
85
86
87 //=================================================================================
88 // function : Init()
89 // purpose  :
90 //=================================================================================
91 void BasicGUI_CurveDlg::Init()
92 {
93   /* init variables */
94   myEditCurrentArgument = GroupPoints->LineEdit1;
95
96   myPoints = new GEOM::ListOfGO();
97   myPoints->length( 0 );
98
99   globalSelection( GEOM_POINT );
100
101   /* signals and slots connections */
102   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
103   connect(myGeomGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
104   connect(myGeomGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
105   
106   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
107   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
108   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
109
110   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
111   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
112
113   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
114
115   initName( tr( "GEOM_CURVE" ) );
116   ConstructorsClicked( 0 );
117 }
118
119 //=================================================================================
120 // function : ConstructorsClicked()
121 // purpose  :
122 //=================================================================================
123 void BasicGUI_CurveDlg::ConstructorsClicked( int id )
124 {
125   QString aTitle = tr( id == 0 ? "GEOM_POLYLINE" : id == 1 ? "GEOM_BEZIER" : "GEOM_INTERPOL" );
126   GroupConstructors->setTitle( aTitle );
127         
128         myPoints = new GEOM::ListOfGO();
129   myPoints->length( 0 );  
130
131   myEditCurrentArgument->setText("");
132 }
133
134
135 //=================================================================================
136 // function : SetEditCurrentArgument()
137 // purpose  :
138 //=================================================================================
139 void BasicGUI_CurveDlg::SetEditCurrentArgument()
140 {
141   if ( sender() == GroupPoints->PushButton1 ) 
142     myEditCurrentArgument = GroupPoints->LineEdit1;
143   myEditCurrentArgument->setFocus();
144   SelectionIntoArgument();
145 }
146
147
148 //=================================================================================
149 // function : LineEditReturnPressed()
150 // purpose  :
151 //=================================================================================
152 void BasicGUI_CurveDlg::LineEditReturnPressed()
153 {
154   if ( sender() == GroupPoints->LineEdit1 ) 
155   {
156     myEditCurrentArgument = GroupPoints->LineEdit1;
157     GEOMBase_Skeleton::LineEditReturnPressed();
158   }
159 }
160
161 //=================================================================================
162 // function : ClickOnOk()
163 // purpose  :
164 //=================================================================================
165 void BasicGUI_CurveDlg::ClickOnOk()
166 {
167   if ( ClickOnApply() )
168     ClickOnCancel();
169 }
170
171 //=================================================================================
172 // function : ClickOnApply()
173 // purpose  :
174 //=================================================================================
175 bool BasicGUI_CurveDlg::ClickOnApply()
176 {
177   if ( !onAccept() )
178     return false;
179
180   initName();
181   ConstructorsClicked( getConstructorId() );
182   return true;
183 }
184
185 //=======================================================================
186 // function : ClickOnCancel()
187 // purpose  :
188 //=======================================================================
189 void BasicGUI_CurveDlg::ClickOnCancel()
190 {
191   GEOMBase_Skeleton::ClickOnCancel();
192 }
193
194 //=================================================================================
195 // function : SelectionIntoArgument()
196 // purpose  : Called when selection as changed or other case
197 //=================================================================================
198 void BasicGUI_CurveDlg::SelectionIntoArgument()
199 {
200   myEditCurrentArgument->setText("");
201
202   Standard_Boolean aRes = Standard_False;
203   int i = 0;
204   myPoints->length( mySelection->IObjectCount() ); // this length may be greater than number of objects,
205                                                    // that will actually be put into myPoints
206   for ( SALOME_ListIteratorOfListIO anIt( mySelection->StoredIObjects() ); anIt.More(); anIt.Next() )
207   {
208     GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( anIt.Value(), aRes );
209     if ( !CORBA::is_nil( aSelectedObject ) && aRes )
210     {
211       //TopoDS_Shape aPointShape;
212       //if ( myGeomBase->GetShape( aSelectedObject, aPointShape, TopAbs_VERTEX ) )
213                         myPoints[i++] = aSelectedObject;
214     }
215   }
216   myPoints->length( i ); // this is the right length, smaller of equal to the previously set
217   if ( i )
218     GroupPoints->LineEdit1->setText( QString::number( i ) + "_" + tr( "GEOM_POINT" ) + tr( "_S_" ) );
219
220   displayPreview(); 
221 }
222
223
224 //=================================================================================
225 // function : ActivateThisDialog()
226 // purpose  :
227 //=================================================================================
228 void BasicGUI_CurveDlg::ActivateThisDialog()
229 {
230   GEOMBase_Skeleton::ActivateThisDialog();
231   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
232
233   myGeomGUI->SetState( 0 );
234
235   globalSelection( GEOM_POINT );
236   ConstructorsClicked( getConstructorId() );
237 }
238
239 //=================================================================================
240 // function : DeactivateActiveDialog()
241 // purpose  : public slot to deactivate if active
242 //=================================================================================
243 void BasicGUI_CurveDlg::DeactivateActiveDialog()
244 {
245   myGeomGUI->SetState( -1 );
246   GEOMBase_Skeleton::DeactivateActiveDialog();
247 }
248
249 //=================================================================================
250 // function : enterEvent()
251 // purpose  :
252 //=================================================================================
253 void BasicGUI_CurveDlg::enterEvent(QEvent* e)
254 {
255   if ( !GroupConstructors->isEnabled() )
256     ActivateThisDialog();
257 }
258
259 //=================================================================================
260 // function : createOperation
261 // purpose  :
262 //=================================================================================
263 GEOM::GEOM_IOperations_ptr BasicGUI_CurveDlg::createOperation()
264 {
265   return getGeomEngine()->GetICurvesOperations( getStudyId() );
266 }
267
268 //=================================================================================
269 // function : isValid
270 // purpose  :
271 //=================================================================================
272 bool BasicGUI_CurveDlg::isValid( QString& msg )
273 {
274   return myPoints->length() > 1;
275 }
276
277 //=================================================================================
278 // function : execute
279 // purpose  :
280 //=================================================================================
281 bool BasicGUI_CurveDlg::execute( ObjectList& objects )
282 {
283   bool res = false;
284
285   GEOM::GEOM_Object_var anObj;
286
287   switch ( getConstructorId() )
288   {
289   case 0 :
290     anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakePolyline( myPoints );
291     res = true;
292     break;
293   case 1 :
294     anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeSplineBezier( myPoints );
295     res = true;
296     break;
297   case 2 :
298     anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeSplineInterpolation( myPoints );
299     res = true;
300     break;
301   }
302
303   if ( !anObj->_is_nil() )
304     objects.push_back( anObj._retn() );
305
306   return res;
307 }
308
309 //=================================================================================
310 // function : closeEvent
311 // purpose  :
312 //=================================================================================
313 void BasicGUI_CurveDlg::closeEvent( QCloseEvent* e )
314 {
315   myGeomGUI->SetState( -1 );
316   GEOMBase_Skeleton::closeEvent( e );
317 }
318