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