Salome HOME
Update mail address
[modules/geom.git] / src / TransformationGUI / TransformationGUI_MirrorDlg.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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 //  File   : TransformationGUI_MirrorDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 #include "TransformationGUI_MirrorDlg.h"
30
31 #include "SUIT_Desktop.h"
32 #include "SUIT_Session.h"
33 #include "SalomeApp_Application.h"
34 #include "LightApp_SelectionMgr.h"
35
36 #include <qcheckbox.h>
37 #include <qlabel.h>
38
39 #include "GEOMImpl_Types.hxx"
40
41 #include "utilities.h"
42
43 using namespace std;
44
45 //=================================================================================
46 // class    : TransformationGUI_MirrorDlg()
47 // purpose  : Constructs a TransformationGUI_MirrorDlg which is a child of 'parent', with the 
48 //            name 'name' and widget flags set to 'f'.
49 //            The dialog will by default be modeless, unless you set 'modal' to
50 //            TRUE to construct a modal dialog.
51 //=================================================================================
52 TransformationGUI_MirrorDlg::TransformationGUI_MirrorDlg(GeometryGUI* theGeometryGUI, QWidget* parent,
53                                                          const char* name, bool modal, WFlags fl)
54   :GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, WStyle_Customize |
55                      WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
56 {
57   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_MIRROR_POINT")));
58   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_MIRROR_AXE")));
59   QPixmap image2(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_MIRROR_PLANE")));
60   QPixmap image3(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
61
62   setCaption(tr("GEOM_MIRROR_TITLE"));
63
64   /***************************************************************/
65   GroupConstructors->setTitle(tr("GEOM_MIRROR"));
66   RadioButton1->setPixmap(image0);
67   RadioButton2->setPixmap(image1);
68   RadioButton3->setPixmap(image2);
69  
70   GroupPoints = new DlgRef_2Sel1Spin2Check(this, "GroupPoints");
71   GroupPoints->SpinBox_DX->hide();
72   GroupPoints->TextLabel3->hide();
73   GroupPoints->CheckButton2->hide();
74   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
75   GroupPoints->TextLabel1->setText(tr("GEOM_OBJECTS"));
76   GroupPoints->TextLabel2->setText(tr("GEOM_POINT_MIRROR"));
77   GroupPoints->TextLabel2->setFixedWidth(74);
78   GroupPoints->PushButton1->setPixmap(image3);
79   GroupPoints->PushButton2->setPixmap(image3);
80   GroupPoints->CheckButton1->setText(tr("GEOM_CREATE_COPY"));
81   Layout1->addWidget(GroupPoints, 2, 0);
82   /***************************************************************/
83
84   setHelpFileName("mirror_image.htm");  
85   
86   Init();
87 }
88
89
90 //=================================================================================
91 // function : ~TransformationGUI_MirrorDlg()
92 // purpose  : Destroys the object and frees any allocated resources
93 //=================================================================================
94 TransformationGUI_MirrorDlg::~TransformationGUI_MirrorDlg()
95 {  
96   /* no need to delete child widgets, Qt does it all for us */
97 }
98
99
100 //=================================================================================
101 // function : Init()
102 // purpose  :
103 //=================================================================================
104 void TransformationGUI_MirrorDlg::Init()
105 {  
106   /* init variables */
107   myEditCurrentArgument = GroupPoints->LineEdit1;
108   GroupPoints->LineEdit1->setReadOnly(true);
109   GroupPoints->LineEdit2->setReadOnly(true);
110   
111   myArgument = GEOM::GEOM_Object::_nil();
112     
113   // Activate Create a Copy mode
114   GroupPoints->CheckButton1->setChecked(true);
115   CreateCopyModeChanged(true);
116
117   /* signals and slots connections */
118   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
119   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
120   connect(GroupConstructors, SIGNAL(clicked(int)), SLOT(ConstructorsClicked(int)));
121
122   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
123   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
124
125   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
126   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
127   
128   connect(GroupPoints->CheckButton1, SIGNAL(toggled(bool)), this, SLOT(CreateCopyModeChanged(bool)));
129   
130   connect(myGeomGUI->getApp()->selectionMgr(), 
131           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
132
133   initName( tr( "GEOM_MIRROR" ) );
134   ConstructorsClicked( 0 );
135 }
136
137
138 //=================================================================================
139 // function : ConstructorsClicked()
140 // purpose  : Radio button management
141 //=================================================================================
142 void TransformationGUI_MirrorDlg::ConstructorsClicked(int constructorId)
143 {
144   disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
145   
146   globalSelection();
147   myEditCurrentArgument = GroupPoints->LineEdit1;
148   GroupPoints->LineEdit2->clear();
149   myArgument = GEOM::GEOM_Object::_nil();
150   
151   switch (constructorId)
152     {
153     case 0: /* mirror an object by point */
154       { 
155         GroupPoints->TextLabel2->setText(tr("GEOM_POINT_MIRROR"));
156         break;
157       }
158     case 1: /* mirror an object by axe */
159       {
160         GroupPoints->TextLabel2->setText(tr("GEOM_AXE_MIRROR"));
161         break;
162       } 
163     case 2: /* mirror an object by plane */
164       {
165         GroupPoints->TextLabel2->setText(tr("GEOM_PLANE_MIRROR"));
166         break;
167       }
168     }
169   connect(myGeomGUI->getApp()->selectionMgr(), 
170           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
171 }
172
173
174
175 //=================================================================================
176 // function : ClickOnOk()
177 // purpose  :
178 //=================================================================================
179 void TransformationGUI_MirrorDlg::ClickOnOk()
180 {
181   if ( ClickOnApply() )
182     ClickOnCancel();
183 }
184
185
186 //=================================================================================
187 // function : ClickOnApply()
188 // purpose  :
189 //=================================================================================
190 bool TransformationGUI_MirrorDlg::ClickOnApply()
191 {
192   if ( !onAccept(GroupPoints->CheckButton1->isChecked()) )
193     return false;
194   
195   initName();
196   ConstructorsClicked( getConstructorId() );
197   return true;
198 }
199
200
201 //=================================================================================
202 // function : SelectionIntoArgument()
203 // purpose  : Called when selection has changed
204 //=================================================================================
205 void TransformationGUI_MirrorDlg::SelectionIntoArgument()
206 {
207   myEditCurrentArgument->setText("");
208   QString aName;
209
210   if(myEditCurrentArgument == GroupPoints->LineEdit1)
211     {
212       int aNbSel = GEOMBase::GetNameOfSelectedIObjects(selectedIO(), aName);
213       if(aNbSel < 1)
214         {
215           myObjects.length(0);
216           return;
217         }
218       GEOMBase::ConvertListOfIOInListOfGO(selectedIO(), myObjects);
219       if (!myObjects.length())
220         return;
221       if(aNbSel != 1)
222         aName = tr("%1_objects").arg(aNbSel);
223     }
224   else if(myEditCurrentArgument == GroupPoints->LineEdit2)
225     {
226       if(IObjectCount() != 1)
227         {
228           myArgument = GEOM::GEOM_Object::_nil();
229           return;
230         }
231       Standard_Boolean testResult = Standard_False;
232       myArgument = GEOMBase::ConvertIOinGEOMObject(firstIObject(), testResult );
233       if(!testResult || CORBA::is_nil( myArgument ))
234         return;
235       aName = GEOMBase::GetName( myArgument );
236     }
237   myEditCurrentArgument->setText( aName );
238    
239   displayPreview(); 
240 }
241
242
243 //=================================================================================
244 // function : LineEditReturnPressed()
245 // purpose  :
246 //=================================================================================
247 void TransformationGUI_MirrorDlg::LineEditReturnPressed()
248 {
249   QLineEdit* send = (QLineEdit*)sender();
250   if(send == GroupPoints->LineEdit1 ||
251      send == GroupPoints->LineEdit2)
252     {
253       myEditCurrentArgument = send;
254       GEOMBase_Skeleton::LineEditReturnPressed();
255     }
256 }
257
258
259 //=================================================================================
260 // function : SetEditCurrentArgument()
261 // purpose  :
262 //=================================================================================
263 void TransformationGUI_MirrorDlg::SetEditCurrentArgument()
264 {
265   QPushButton* send = (QPushButton*)sender();
266   
267   if(send == GroupPoints->PushButton1){
268     myEditCurrentArgument = GroupPoints->LineEdit1;
269     globalSelection();
270   }
271   else if(send == GroupPoints->PushButton2) {
272     myEditCurrentArgument = GroupPoints->LineEdit2;
273     switch(getConstructorId())
274       {
275       case 0:
276         {
277           globalSelection( GEOM_POINT );
278           break;
279         }
280       case 1:
281         {
282           globalSelection( GEOM_LINE );
283           break;
284         }
285       case 2:
286         {
287           globalSelection( GEOM_PLANE );
288           break;
289         }
290       }
291   }
292
293   myEditCurrentArgument->setFocus();
294   SelectionIntoArgument();
295 }
296
297
298 //=================================================================================
299 // function : ActivateThisDialog()
300 // purpose  :
301 //=================================================================================
302 void TransformationGUI_MirrorDlg::ActivateThisDialog()
303 {
304   GEOMBase_Skeleton::ActivateThisDialog();
305   connect(myGeomGUI->getApp()->selectionMgr(), 
306           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
307   ConstructorsClicked( getConstructorId() );
308 }
309
310
311 //=================================================================================
312 // function : enterEvent()
313 // purpose  : when mouse enter onto the QWidget
314 //=================================================================================
315 void TransformationGUI_MirrorDlg::enterEvent(QEvent * e)
316 {
317   if(!GroupConstructors->isEnabled())
318     ActivateThisDialog();
319 }
320
321
322 //=================================================================================
323 // function : createOperation
324 // purpose  :
325 //=================================================================================
326 GEOM::GEOM_IOperations_ptr  TransformationGUI_MirrorDlg::createOperation()
327 {
328   return getGeomEngine()->GetITransformOperations( getStudyId() );
329 }
330
331
332 //=================================================================================
333 // function : isValid
334 // purpose  :
335 //=================================================================================
336 bool  TransformationGUI_MirrorDlg::isValid( QString& msg )
337 {
338   return !(myObjects.length() == 0 || myArgument->_is_nil());
339 }
340
341
342 //=================================================================================
343 // function : execute
344 // purpose  :
345 //=================================================================================
346 bool  TransformationGUI_MirrorDlg::execute( ObjectList& objects )
347 {
348   bool res = false;
349   bool toCreateCopy = IsPreview() || GroupPoints->CheckButton1->isChecked();
350   
351   GEOM::GEOM_Object_var anObj;
352   
353   switch ( getConstructorId() ) 
354     {
355     case 0 :
356       {
357         if (toCreateCopy)
358           for (int i = 0; i < myObjects.length(); i++)
359             {
360               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->MirrorPointCopy( myObjects[i], myArgument );
361               if ( !anObj->_is_nil() )
362                 objects.push_back( anObj._retn() );
363             }
364         else
365           for (int i = 0; i < myObjects.length(); i++)
366             {
367               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->MirrorPoint( myObjects[i], myArgument );
368               if ( !anObj->_is_nil() )
369                 objects.push_back( anObj._retn() );
370             }
371         res = true;
372         break;
373       }
374     case 1 :
375       {
376         if (toCreateCopy)
377           for (int i = 0; i < myObjects.length(); i++)
378             {
379               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->MirrorAxisCopy( myObjects[i], myArgument );
380               if ( !anObj->_is_nil() )
381                 objects.push_back( anObj._retn() );
382             }
383         else
384           for (int i = 0; i < myObjects.length(); i++)
385             {
386               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->MirrorAxis( myObjects[i], myArgument );
387               if ( !anObj->_is_nil() )
388                 objects.push_back( anObj._retn() );
389             }
390         res = true;
391         break;
392       }
393     case 2:
394       {
395         if (toCreateCopy)
396            for (int i = 0; i < myObjects.length(); i++)
397             {
398               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->MirrorPlaneCopy( myObjects[i], myArgument );
399               if ( !anObj->_is_nil() )
400                 objects.push_back( anObj._retn() );
401             }
402         else
403            for (int i = 0; i < myObjects.length(); i++)
404             {
405               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->MirrorPlane( myObjects[i], myArgument );
406               if ( !anObj->_is_nil() )
407                 objects.push_back( anObj._retn() );
408             }
409         res = true;
410         break;
411       }
412     }
413   
414   return res;
415 }
416
417 //=================================================================================
418 // function : closeEvent
419 // purpose  :
420 //=================================================================================
421 void  TransformationGUI_MirrorDlg::closeEvent( QCloseEvent* e )
422 {
423   GEOMBase_Skeleton::closeEvent( e );
424 }
425
426
427 //=================================================================================
428 // function :  CreateCopyModeChanged()
429 // purpose  :
430 //=================================================================================
431 void TransformationGUI_MirrorDlg::CreateCopyModeChanged(bool isCreateCopy)
432 {
433   this->GroupBoxName->setEnabled(isCreateCopy);
434 }