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