Salome HOME
d6fca90516bbdfd5d6a073d4bf13b1d0dd008811
[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 "QAD_Desktop.h"
32
33 #include <qcheckbox.h>
34
35 #include "GEOMImpl_Types.hxx"
36
37 #include "utilities.h"
38
39 using namespace std;
40
41 //=================================================================================
42 // class    : TransformationGUI_PositionDlg()
43 // purpose  : Constructs a TransformationGUI_PositionDlg 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 TransformationGUI_PositionDlg::TransformationGUI_PositionDlg(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_POSITION")));
52   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_POSITION2")));
53   QPixmap imageselect(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
54
55   setCaption(tr("GEOM_POSITION_TITLE"));
56
57   /***************************************************************/
58   GroupConstructors->setTitle(tr("GEOM_POSITION"));
59   RadioButton1->setPixmap(image0);
60   RadioButton2->setPixmap(image1);
61   RadioButton3->close(TRUE);
62  
63   Group1 = new DlgRef_3Sel3Spin1Check(this, "Group1");
64   Group1->SpinBox1->hide();
65   Group1->SpinBox2->hide();
66   Group1->SpinBox3->hide();
67   Group1->TextLabel4->hide();
68   Group1->TextLabel5->hide();
69   Group1->TextLabel6->hide();
70   Group1->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
71   Group1->TextLabel1->setText(tr("GEOM_OBJECTS"));
72   Group1->TextLabel2->setText(tr("GEOM_START_LCS"));
73   Group1->TextLabel3->setText(tr("GEOM_END_LCS"));
74   Group1->PushButton1->setPixmap(imageselect);
75   Group1->PushButton2->setPixmap(imageselect);
76   Group1->PushButton3->setPixmap(imageselect);
77   Group1->CheckBox1->setText(tr("GEOM_CREATE_COPY"));
78
79   Layout1->addWidget(Group1, 2, 0);
80   
81   /***************************************************************/
82   
83   Init();
84 }
85
86
87 //=================================================================================
88 // function : ~TransformationGUI_PositionDlg()
89 // purpose  : Destroys the object and frees any allocated resources
90 //=================================================================================
91 TransformationGUI_PositionDlg::~TransformationGUI_PositionDlg()
92 {  
93   /* no need to delete child widgets, Qt does it all for us */
94 }
95
96
97 //=================================================================================
98 // function : Init()
99 // purpose  :
100 //=================================================================================
101 void TransformationGUI_PositionDlg::Init()
102 {  
103   /* init variables */
104   Group1->LineEdit1->setReadOnly(true);
105   Group1->LineEdit2->setReadOnly(true);
106   Group1->LineEdit3->setReadOnly(true);
107   
108   myStartLCS = GEOM::GEOM_Object::_nil();
109   myEndLCS = GEOM::GEOM_Object::_nil();
110     
111   // Activate Create a Copy mode
112   Group1->CheckBox1->setChecked(true);
113   CreateCopyModeChanged(true);
114
115   /* signals and slots connections */
116   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
117   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
118   connect(GroupConstructors, SIGNAL(clicked(int)), SLOT(ConstructorsClicked(int)));
119
120   connect(Group1->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
121   connect(Group1->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
122   connect(Group1->PushButton3, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
123
124   connect(Group1->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
125   connect(Group1->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
126   connect(Group1->LineEdit3, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
127   
128   connect(Group1->CheckBox1, SIGNAL(toggled(bool)), this, SLOT(CreateCopyModeChanged(bool)));
129   
130   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
131
132   initName( tr( "GEOM_POSITION" ) );
133   ConstructorsClicked( 0 );
134 }
135
136
137
138 //=================================================================================
139 // function : ConstructorsClicked()
140 // purpose  : Radio button management
141 //=================================================================================
142 void TransformationGUI_PositionDlg::ConstructorsClicked(int constructorId)
143 {
144   disconnect( mySelection, 0, this, 0 );
145   
146   globalSelection();
147   myEditCurrentArgument = Group1->LineEdit1;
148   Group1->LineEdit2->clear();
149   Group1->LineEdit3->clear();
150   myStartLCS = GEOM::GEOM_Object::_nil();
151   myEndLCS = GEOM::GEOM_Object::_nil();
152   
153   switch (constructorId)
154     {
155     case 0:
156       { 
157         Group1->LineEdit2->hide();
158         Group1->TextLabel2->hide();
159         Group1->PushButton2->hide();
160         break;
161       }
162     case 1:
163       {
164         Group1->LineEdit2->show();
165         Group1->TextLabel2->show();
166         Group1->PushButton2->show();
167         break;
168       }
169     }
170   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
171   SelectionIntoArgument();
172 }
173
174
175
176 //=================================================================================
177 // function : ClickOnOk()
178 // purpose  :
179 //=================================================================================
180 void TransformationGUI_PositionDlg::ClickOnOk()
181 {
182   if ( ClickOnApply() )
183     ClickOnCancel();
184 }
185
186
187 //=================================================================================
188 // function : ClickOnApply()
189 // purpose  :
190 //=================================================================================
191 bool TransformationGUI_PositionDlg::ClickOnApply()
192 {
193   if ( !onAccept(Group1->CheckBox1->isChecked()) )
194     return false;
195   
196   initName();
197   ConstructorsClicked( getConstructorId() );
198   return true;
199 }
200
201
202 //=======================================================================
203 // function : ClickOnCancel()
204 // purpose  :
205 //=======================================================================
206 void TransformationGUI_PositionDlg::ClickOnCancel()
207 {
208   GEOMBase_Skeleton::ClickOnCancel();
209 }
210
211
212 //=================================================================================
213 // function : SelectionIntoArgument()
214 // purpose  : Called when selection has changed
215 //=================================================================================
216 void TransformationGUI_PositionDlg::SelectionIntoArgument()
217 {
218   myEditCurrentArgument->setText("");
219   QString aName;
220
221   if(myEditCurrentArgument == Group1->LineEdit1) {
222     int aNbSel = GEOMBase::GetNameOfSelectedIObjects(mySelection, aName);
223     if(aNbSel < 1) {
224       myObjects.length(0);
225       displayPreview();
226       return;
227     }
228     GEOMBase::ConvertListOfIOInListOfGO(mySelection->StoredIObjects(), myObjects);
229     if (!myObjects.length()) {
230       displayPreview();
231       return;
232     }
233     if(aNbSel != 1)
234       aName = tr("%1_objects").arg(aNbSel);
235   }
236   else if(myEditCurrentArgument == Group1->LineEdit2) {
237     myStartLCS = GEOM::GEOM_Object::_nil();
238     if(mySelection->IObjectCount() != 1) {
239       displayPreview();
240       return;
241     }
242
243     Standard_Boolean testResult = Standard_False;
244     myStartLCS = GEOMBase::ConvertIOinGEOMObject(mySelection->firstIObject(), testResult );
245     if(!testResult || CORBA::is_nil( myStartLCS )) {
246       displayPreview();
247       return;
248     }
249     aName = GEOMBase::GetName( myStartLCS );
250   }
251   else if(myEditCurrentArgument == Group1->LineEdit3) {
252     myEndLCS = GEOM::GEOM_Object::_nil();
253     if(mySelection->IObjectCount() != 1) {
254       displayPreview();
255       return;
256     }
257     
258     Standard_Boolean testResult = Standard_False;
259     myEndLCS = GEOMBase::ConvertIOinGEOMObject(mySelection->firstIObject(), testResult );
260     if(!testResult || CORBA::is_nil( myEndLCS )) {
261       displayPreview();
262       return;
263     }
264     aName = GEOMBase::GetName( myEndLCS );
265   }
266
267   myEditCurrentArgument->setText( aName );
268   displayPreview(); 
269 }
270
271
272 //=================================================================================
273 // function : LineEditReturnPressed()
274 // purpose  :
275 //=================================================================================
276 void TransformationGUI_PositionDlg::LineEditReturnPressed()
277 {
278   QLineEdit* send = (QLineEdit*)sender();
279   if(send == Group1->LineEdit1 || send == Group1->LineEdit2 || send == Group1->LineEdit3) {
280     myEditCurrentArgument = send;
281     GEOMBase_Skeleton::LineEditReturnPressed();
282   }
283 }
284
285
286 //=================================================================================
287 // function : SetEditCurrentArgument()
288 // purpose  :
289 //=================================================================================
290 void TransformationGUI_PositionDlg::SetEditCurrentArgument()
291 {
292   QPushButton* send = (QPushButton*)sender();
293   
294   if(send == Group1->PushButton1){
295     myEditCurrentArgument = Group1->LineEdit1;
296     globalSelection();
297   }
298   else if(send == Group1->PushButton2) {
299     myEditCurrentArgument = Group1->LineEdit2;
300     TColStd_MapOfInteger aMap;
301     aMap.Add( GEOM_PLANE );
302     aMap.Add( GEOM_MARKER );
303     globalSelection( aMap );
304   }
305   else if(send == Group1->PushButton3) {
306     myEditCurrentArgument = Group1->LineEdit3;
307     TColStd_MapOfInteger aMap;
308     aMap.Add( GEOM_PLANE );
309     aMap.Add( GEOM_MARKER );
310     globalSelection( aMap );
311   }
312
313   myEditCurrentArgument->setFocus();
314   SelectionIntoArgument();
315 }
316
317
318 //=================================================================================
319 // function : ActivateThisDialog()
320 // purpose  :
321 //=================================================================================
322 void TransformationGUI_PositionDlg::ActivateThisDialog()
323 {
324   GEOMBase_Skeleton::ActivateThisDialog();
325   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
326   ConstructorsClicked( getConstructorId() );
327 }
328
329
330 //=================================================================================
331 // function : DeactivateActiveDialog()
332 // purpose  : public slot to deactivate if active
333 //=================================================================================
334 void TransformationGUI_PositionDlg::DeactivateActiveDialog()
335 {
336   GEOMBase_Skeleton::DeactivateActiveDialog();
337 }
338
339
340 //=================================================================================
341 // function : enterEvent()
342 // purpose  : when mouse enter onto the QWidget
343 //=================================================================================
344 void TransformationGUI_PositionDlg::enterEvent(QEvent * e)
345 {
346   if(!GroupConstructors->isEnabled())
347     ActivateThisDialog();
348 }
349
350
351 //=================================================================================
352 // function : createOperation
353 // purpose  :
354 //=================================================================================
355 GEOM::GEOM_IOperations_ptr  TransformationGUI_PositionDlg::createOperation()
356 {
357   return getGeomEngine()->GetITransformOperations( getStudyId() );
358 }
359
360
361 //=================================================================================
362 // function : isValid
363 // purpose  :
364 //=================================================================================
365 bool  TransformationGUI_PositionDlg::isValid( QString& msg )
366 {
367   bool res;
368   if (getConstructorId() == 0)
369     res = !(myObjects.length() == 0 || myEndLCS->_is_nil());
370   else
371     res = !(myObjects.length() == 0 || myStartLCS->_is_nil() || myEndLCS->_is_nil());
372
373   return res;
374 }
375
376
377 //=================================================================================
378 // function : execute
379 // purpose  :
380 //=================================================================================
381 bool  TransformationGUI_PositionDlg::execute( ObjectList& objects )
382 {
383   bool res = false;
384   bool toCreateCopy = IsPreview() || Group1->CheckBox1->isChecked();
385   GEOM::GEOM_Object_var anObj;
386   
387   switch ( getConstructorId() )
388     {
389     case 0 :
390       {
391         for (int i = 0; i < myObjects.length(); i++) {
392           if (toCreateCopy)
393             anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->PositionShapeCopy( myObjects[i], myObjects[i], myEndLCS );
394           else
395             anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->PositionShape( myObjects[i], myObjects[i], myEndLCS );
396
397           if ( !anObj->_is_nil() )
398             objects.push_back( anObj._retn() );
399         }
400         res = true;
401         break;
402       }
403     case 1 :
404       {
405         for (int i = 0; i < myObjects.length(); i++) {
406           if (toCreateCopy)
407             anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->PositionShapeCopy( myObjects[i], myStartLCS, myEndLCS );
408           else
409             anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->PositionShape( myObjects[i], myStartLCS, myEndLCS );
410           if ( !anObj->_is_nil() )
411             objects.push_back( anObj._retn() );
412         }
413         res = true;
414         break;
415       }
416     }
417   
418   return res;
419 }
420
421 //=================================================================================
422 // function : closeEvent
423 // purpose  :
424 //=================================================================================
425 void  TransformationGUI_PositionDlg::closeEvent( QCloseEvent* e )
426 {
427   GEOMBase_Skeleton::closeEvent( e );
428 }
429
430
431 //=================================================================================
432 // function :  CreateCopyModeChanged()
433 // purpose  :
434 //=================================================================================
435 void TransformationGUI_PositionDlg::CreateCopyModeChanged(bool isCreateCopy)
436 {
437   this->GroupBoxName->setEnabled(isCreateCopy);
438 }