Salome HOME
dc8a9b583b800146b48fd1d32b48c4595a0712b6
[modules/geom.git] / src / TransformationGUI / TransformationGUI_PositionDlg.cxx
1 //  GEOM GEOMGUI : GUI for Geometry component
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : TransformationGUI_PositionDlg.cxx
25 //  Author : Damien COQUERET
26 //  Module : GEOM
27 //  $Header$
28
29 #include "TransformationGUI_PositionDlg.h"
30
31 #include "SUIT_Desktop.h"
32 #include "SUIT_Session.h"
33 #include "SalomeApp_Application.h"
34 #include "SalomeApp_SelectionMgr.h"
35
36 #include <qlabel.h>
37 #include <qcheckbox.h>
38
39 #include "TColStd_MapOfInteger.hxx"
40
41 #include "GEOMImpl_Types.hxx"
42
43 #include "utilities.h"
44
45 using namespace std;
46
47 //=================================================================================
48 // class    : TransformationGUI_PositionDlg()
49 // purpose  : Constructs a TransformationGUI_PositionDlg 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 TransformationGUI_PositionDlg::TransformationGUI_PositionDlg(QWidget* parent,  const char* name, bool modal, WFlags fl)
55     :GEOMBase_Skeleton(parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
56 {
57   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_POSITION")));
58   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_POSITION2")));
59   QPixmap imageselect(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
60
61   setCaption(tr("GEOM_POSITION_TITLE"));
62
63   /***************************************************************/
64   GroupConstructors->setTitle(tr("GEOM_POSITION"));
65   RadioButton1->setPixmap(image0);
66   RadioButton2->setPixmap(image1);
67   RadioButton3->close(TRUE);
68  
69   Group1 = new DlgRef_3Sel3Spin1Check(this, "Group1");
70   Group1->SpinBox1->hide();
71   Group1->SpinBox2->hide();
72   Group1->SpinBox3->hide();
73   Group1->TextLabel4->hide();
74   Group1->TextLabel5->hide();
75   Group1->TextLabel6->hide();
76   Group1->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
77   Group1->TextLabel1->setText(tr("GEOM_OBJECTS"));
78   Group1->TextLabel2->setText(tr("GEOM_START_LCS"));
79   Group1->TextLabel3->setText(tr("GEOM_END_LCS"));
80   Group1->PushButton1->setPixmap(imageselect);
81   Group1->PushButton2->setPixmap(imageselect);
82   Group1->PushButton3->setPixmap(imageselect);
83   Group1->CheckBox1->setText(tr("GEOM_CREATE_COPY"));
84
85   Layout1->addWidget(Group1, 2, 0);
86   
87   /***************************************************************/
88   
89   Init();
90 }
91
92
93 //=================================================================================
94 // function : ~TransformationGUI_PositionDlg()
95 // purpose  : Destroys the object and frees any allocated resources
96 //=================================================================================
97 TransformationGUI_PositionDlg::~TransformationGUI_PositionDlg()
98 {  
99   /* no need to delete child widgets, Qt does it all for us */
100 }
101
102
103 //=================================================================================
104 // function : Init()
105 // purpose  :
106 //=================================================================================
107 void TransformationGUI_PositionDlg::Init()
108 {  
109   /* init variables */
110   Group1->LineEdit1->setReadOnly(true);
111   Group1->LineEdit2->setReadOnly(true);
112   Group1->LineEdit3->setReadOnly(true);
113   
114   myStartLCS = GEOM::GEOM_Object::_nil();
115   myEndLCS = GEOM::GEOM_Object::_nil();
116     
117   // Activate Create a Copy mode
118   Group1->CheckBox1->setChecked(true);
119   CreateCopyModeChanged(true);
120
121   /* signals and slots connections */
122   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
123   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
124   connect(GroupConstructors, SIGNAL(clicked(int)), SLOT(ConstructorsClicked(int)));
125
126   connect(Group1->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
127   connect(Group1->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
128   connect(Group1->PushButton3, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
129
130   connect(Group1->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
131   connect(Group1->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
132   connect(Group1->LineEdit3, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
133   
134   connect(Group1->CheckBox1, SIGNAL(toggled(bool)), this, SLOT(CreateCopyModeChanged(bool)));
135   
136   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
137           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
138
139   initName( tr( "GEOM_POSITION" ) );
140   ConstructorsClicked( 0 );
141 }
142
143
144
145 //=================================================================================
146 // function : ConstructorsClicked()
147 // purpose  : Radio button management
148 //=================================================================================
149 void TransformationGUI_PositionDlg::ConstructorsClicked(int constructorId)
150 {
151   disconnect( ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 0, this, 0 );
152   
153   globalSelection();
154   myEditCurrentArgument = Group1->LineEdit1;
155   Group1->LineEdit2->clear();
156   Group1->LineEdit3->clear();
157   myStartLCS = GEOM::GEOM_Object::_nil();
158   myEndLCS = GEOM::GEOM_Object::_nil();
159   
160   switch (constructorId)
161     {
162     case 0:
163       { 
164         Group1->LineEdit2->hide();
165         Group1->TextLabel2->hide();
166         Group1->PushButton2->hide();
167         break;
168       }
169     case 1:
170       {
171         Group1->LineEdit2->show();
172         Group1->TextLabel2->show();
173         Group1->PushButton2->show();
174         break;
175       }
176     }
177   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
178           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
179   SelectionIntoArgument();
180 }
181
182
183
184 //=================================================================================
185 // function : ClickOnOk()
186 // purpose  :
187 //=================================================================================
188 void TransformationGUI_PositionDlg::ClickOnOk()
189 {
190   if ( ClickOnApply() )
191     ClickOnCancel();
192 }
193
194
195 //=================================================================================
196 // function : ClickOnApply()
197 // purpose  :
198 //=================================================================================
199 bool TransformationGUI_PositionDlg::ClickOnApply()
200 {
201   if ( !onAccept(Group1->CheckBox1->isChecked()) )
202     return false;
203   
204   initName();
205   ConstructorsClicked( getConstructorId() );
206   return true;
207 }
208
209
210 //=======================================================================
211 // function : ClickOnCancel()
212 // purpose  :
213 //=======================================================================
214 void TransformationGUI_PositionDlg::ClickOnCancel()
215 {
216   GEOMBase_Skeleton::ClickOnCancel();
217 }
218
219
220 //=================================================================================
221 // function : SelectionIntoArgument()
222 // purpose  : Called when selection has changed
223 //=================================================================================
224 void TransformationGUI_PositionDlg::SelectionIntoArgument()
225 {
226   myEditCurrentArgument->setText("");
227   QString aName;
228
229   if(myEditCurrentArgument == Group1->LineEdit1) {
230     int aNbSel = GEOMBase::GetNameOfSelectedIObjects(selectedIO(), aName);
231     if(aNbSel < 1) {
232       myObjects.length(0);
233       displayPreview();
234       return;
235     }
236     GEOMBase::ConvertListOfIOInListOfGO(selectedIO(), myObjects);
237     if (!myObjects.length()) {
238       displayPreview();
239       return;
240     }
241   }
242   else if(myEditCurrentArgument == Group1->LineEdit2) {
243     myStartLCS = GEOM::GEOM_Object::_nil();
244     if(IObjectCount() != 1) {
245       displayPreview();
246       return;
247     }
248
249     Standard_Boolean testResult = Standard_False;
250     myStartLCS = GEOMBase::ConvertIOinGEOMObject(firstIObject(), testResult );
251     if(!testResult || CORBA::is_nil( myStartLCS )) {
252       displayPreview();
253       return;
254     }
255     aName = GEOMBase::GetName( myStartLCS );
256   }
257   else if(myEditCurrentArgument == Group1->LineEdit3) {
258     myEndLCS = GEOM::GEOM_Object::_nil();
259     if(IObjectCount() != 1) {
260       displayPreview();
261       return;
262     }
263     
264     Standard_Boolean testResult = Standard_False;
265     myEndLCS = GEOMBase::ConvertIOinGEOMObject(firstIObject(), testResult );
266     if(!testResult || CORBA::is_nil( myEndLCS )) {
267       displayPreview();
268       return;
269     }
270     aName = GEOMBase::GetName( myEndLCS );
271   }
272
273   myEditCurrentArgument->setText( aName );
274   displayPreview(); 
275 }
276
277
278 //=================================================================================
279 // function : LineEditReturnPressed()
280 // purpose  :
281 //=================================================================================
282 void TransformationGUI_PositionDlg::LineEditReturnPressed()
283 {
284   QLineEdit* send = (QLineEdit*)sender();
285   if(send == Group1->LineEdit1 || send == Group1->LineEdit2 || send == Group1->LineEdit3) {
286     myEditCurrentArgument = send;
287     GEOMBase_Skeleton::LineEditReturnPressed();
288   }
289 }
290
291
292 //=================================================================================
293 // function : SetEditCurrentArgument()
294 // purpose  :
295 //=================================================================================
296 void TransformationGUI_PositionDlg::SetEditCurrentArgument()
297 {
298   QPushButton* send = (QPushButton*)sender();
299   
300   if(send == Group1->PushButton1){
301     myEditCurrentArgument = Group1->LineEdit1;
302     globalSelection();
303   }
304   else if(send == Group1->PushButton2) {
305     myEditCurrentArgument = Group1->LineEdit2;
306     TColStd_MapOfInteger aMap;
307     aMap.Add( GEOM_PLANE );
308     aMap.Add( GEOM_MARKER );
309     globalSelection( aMap );
310   }
311   else if(send == Group1->PushButton3) {
312     myEditCurrentArgument = Group1->LineEdit3;
313     TColStd_MapOfInteger aMap;
314     aMap.Add( GEOM_PLANE );
315     aMap.Add( GEOM_MARKER );
316     globalSelection( aMap );
317   }
318
319   myEditCurrentArgument->setFocus();
320   SelectionIntoArgument();
321 }
322
323
324 //=================================================================================
325 // function : ActivateThisDialog()
326 // purpose  :
327 //=================================================================================
328 void TransformationGUI_PositionDlg::ActivateThisDialog()
329 {
330   GEOMBase_Skeleton::ActivateThisDialog();
331   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
332           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
333   ConstructorsClicked( getConstructorId() );
334 }
335
336
337 //=================================================================================
338 // function : DeactivateActiveDialog()
339 // purpose  : public slot to deactivate if active
340 //=================================================================================
341 void TransformationGUI_PositionDlg::DeactivateActiveDialog()
342 {
343   GEOMBase_Skeleton::DeactivateActiveDialog();
344 }
345
346
347 //=================================================================================
348 // function : enterEvent()
349 // purpose  : when mouse enter onto the QWidget
350 //=================================================================================
351 void TransformationGUI_PositionDlg::enterEvent(QEvent * e)
352 {
353   if(!GroupConstructors->isEnabled())
354     ActivateThisDialog();
355 }
356
357
358 //=================================================================================
359 // function : createOperation
360 // purpose  :
361 //=================================================================================
362 GEOM::GEOM_IOperations_ptr  TransformationGUI_PositionDlg::createOperation()
363 {
364   return getGeomEngine()->GetITransformOperations( getStudyId() );
365 }
366
367
368 //=================================================================================
369 // function : isValid
370 // purpose  :
371 //=================================================================================
372 bool  TransformationGUI_PositionDlg::isValid( QString& msg )
373 {
374   bool res;
375   if (getConstructorId() == 0)
376     res = !(myObjects.length() == 0 || myEndLCS->_is_nil());
377   else
378     res = !(myObjects.length() == 0 || myStartLCS->_is_nil() || myEndLCS->_is_nil());
379
380   return res;
381 }
382
383
384 //=================================================================================
385 // function : execute
386 // purpose  :
387 //=================================================================================
388 bool  TransformationGUI_PositionDlg::execute( ObjectList& objects )
389 {
390   bool res = false;
391   bool toCreateCopy = IsPreview() || Group1->CheckBox1->isChecked();
392   GEOM::GEOM_Object_var anObj;
393   
394   switch ( getConstructorId() )
395     {
396     case 0 :
397       {
398         for (int i = 0; i < myObjects.length(); i++) {
399           if (toCreateCopy)
400             anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->PositionShapeCopy( myObjects[i], myObjects[i], myEndLCS );
401           else
402             anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->PositionShape( myObjects[i], myObjects[i], myEndLCS );
403
404           if ( !anObj->_is_nil() )
405             objects.push_back( anObj._retn() );
406         }
407         res = true;
408         break;
409       }
410     case 1 :
411       {
412         for (int i = 0; i < myObjects.length(); i++) {
413           if (toCreateCopy)
414             anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->PositionShapeCopy( myObjects[i], myStartLCS, myEndLCS );
415           else
416             anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->PositionShape( myObjects[i], myStartLCS, myEndLCS );
417           if ( !anObj->_is_nil() )
418             objects.push_back( anObj._retn() );
419         }
420         res = true;
421         break;
422       }
423     }
424   
425   return res;
426 }
427
428 //=================================================================================
429 // function : closeEvent
430 // purpose  :
431 //=================================================================================
432 void  TransformationGUI_PositionDlg::closeEvent( QCloseEvent* e )
433 {
434   GEOMBase_Skeleton::closeEvent( e );
435 }
436
437
438 //=================================================================================
439 // function :  CreateCopyModeChanged()
440 // purpose  :
441 //=================================================================================
442 void TransformationGUI_PositionDlg::CreateCopyModeChanged(bool isCreateCopy)
443 {
444   this->GroupBoxName->setEnabled(isCreateCopy);
445 }