]> SALOME platform Git repositories - modules/geom.git/blob - src/TransformationGUI/TransformationGUI_MirrorDlg.cxx
Salome HOME
345ce98fe0bfedc91b2b253473f0f5d31634c5ae
[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 <TopoDS_Shape.hxx>
37 #include <TopoDS_Edge.hxx>
38 #include <TopoDS.hxx>
39 #include <TopExp.hxx>
40 #include <TColStd_IndexedMapOfInteger.hxx>
41 #include <TopTools_IndexedMapOfShape.hxx>
42
43 #include <qcheckbox.h>
44 #include <qlabel.h>
45
46 #include "GEOMImpl_Types.hxx"
47
48 #include "utilities.h"
49
50 using namespace std;
51
52 //=================================================================================
53 // class    : TransformationGUI_MirrorDlg()
54 // purpose  : Constructs a TransformationGUI_MirrorDlg which is a child of 'parent', with the 
55 //            name 'name' and widget flags set to 'f'.
56 //            The dialog will by default be modeless, unless you set 'modal' to
57 //            TRUE to construct a modal dialog.
58 //=================================================================================
59 TransformationGUI_MirrorDlg::TransformationGUI_MirrorDlg(GeometryGUI* theGeometryGUI, QWidget* parent,
60                                                          const char* name, bool modal, WFlags fl)
61   :GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, WStyle_Customize |
62                      WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
63 {
64   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_MIRROR_POINT")));
65   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_MIRROR_AXE")));
66   QPixmap image2(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_MIRROR_PLANE")));
67   QPixmap image3(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
68
69   setCaption(tr("GEOM_MIRROR_TITLE"));
70
71   /***************************************************************/
72   GroupConstructors->setTitle(tr("GEOM_MIRROR"));
73   RadioButton1->setPixmap(image0);
74   RadioButton2->setPixmap(image1);
75   RadioButton3->setPixmap(image2);
76  
77   GroupPoints = new DlgRef_2Sel1Spin2Check(this, "GroupPoints");
78   GroupPoints->SpinBox_DX->hide();
79   GroupPoints->TextLabel3->hide();
80   GroupPoints->CheckButton2->hide();
81   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
82   GroupPoints->TextLabel1->setText(tr("GEOM_OBJECTS"));
83   GroupPoints->TextLabel2->setText(tr("GEOM_POINT_MIRROR"));
84   GroupPoints->TextLabel2->setFixedWidth(74);
85   GroupPoints->PushButton1->setPixmap(image3);
86   GroupPoints->PushButton2->setPixmap(image3);
87   GroupPoints->CheckButton1->setText(tr("GEOM_CREATE_COPY"));
88   Layout1->addWidget(GroupPoints, 2, 0);
89   /***************************************************************/
90
91   setHelpFileName("mirror_operation_page.html");  
92   
93   Init();
94 }
95
96
97 //=================================================================================
98 // function : ~TransformationGUI_MirrorDlg()
99 // purpose  : Destroys the object and frees any allocated resources
100 //=================================================================================
101 TransformationGUI_MirrorDlg::~TransformationGUI_MirrorDlg()
102 {  
103   /* no need to delete child widgets, Qt does it all for us */
104 }
105
106
107 //=================================================================================
108 // function : Init()
109 // purpose  :
110 //=================================================================================
111 void TransformationGUI_MirrorDlg::Init()
112 {  
113   /* init variables */
114   myEditCurrentArgument = GroupPoints->LineEdit1;
115   GroupPoints->LineEdit1->setReadOnly(true);
116   GroupPoints->LineEdit2->setReadOnly(true);
117   
118   myArgument = GEOM::GEOM_Object::_nil();
119     
120   // Activate Create a Copy mode
121   GroupPoints->CheckButton1->setChecked(true);
122   CreateCopyModeChanged(true);
123
124   /* signals and slots connections */
125   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
126   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
127   connect(GroupConstructors, SIGNAL(clicked(int)), SLOT(ConstructorsClicked(int)));
128
129   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
130   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
131
132   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
133   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
134   
135   connect(GroupPoints->CheckButton1, SIGNAL(toggled(bool)), this, SLOT(CreateCopyModeChanged(bool)));
136   
137   connect(myGeomGUI->getApp()->selectionMgr(), 
138           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
139
140   initName( tr( "GEOM_MIRROR" ) );
141   ConstructorsClicked( 0 );
142 }
143
144
145 //=================================================================================
146 // function : ConstructorsClicked()
147 // purpose  : Radio button management
148 //=================================================================================
149 void TransformationGUI_MirrorDlg::ConstructorsClicked(int constructorId)
150 {
151   disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
152   
153   globalSelection();
154   myEditCurrentArgument = GroupPoints->LineEdit1;
155   GroupPoints->LineEdit2->clear();
156   myArgument = GEOM::GEOM_Object::_nil();
157   
158   switch (constructorId)
159     {
160     case 0: /* mirror an object by point */
161       { 
162         GroupPoints->TextLabel2->setText(tr("GEOM_POINT_MIRROR"));
163         break;
164       }
165     case 1: /* mirror an object by axe */
166       {
167         GroupPoints->TextLabel2->setText(tr("GEOM_AXE_MIRROR"));
168         break;
169       } 
170     case 2: /* mirror an object by plane */
171       {
172         GroupPoints->TextLabel2->setText(tr("GEOM_PLANE_MIRROR"));
173         break;
174       }
175     }
176   connect(myGeomGUI->getApp()->selectionMgr(), 
177           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
178 }
179
180
181
182 //=================================================================================
183 // function : ClickOnOk()
184 // purpose  :
185 //=================================================================================
186 void TransformationGUI_MirrorDlg::ClickOnOk()
187 {
188   if ( ClickOnApply() )
189     ClickOnCancel();
190 }
191
192
193 //=================================================================================
194 // function : ClickOnApply()
195 // purpose  :
196 //=================================================================================
197 bool TransformationGUI_MirrorDlg::ClickOnApply()
198 {
199   if ( !onAccept(GroupPoints->CheckButton1->isChecked()) )
200     return false;
201   
202   initName();
203   ConstructorsClicked( getConstructorId() );
204   return true;
205 }
206
207
208 //=================================================================================
209 // function : SelectionIntoArgument()
210 // purpose  : Called when selection has changed
211 //=================================================================================
212 void TransformationGUI_MirrorDlg::SelectionIntoArgument()
213 {
214   myEditCurrentArgument->setText("");
215   QString aName;
216
217   if(myEditCurrentArgument == GroupPoints->LineEdit1)
218     {
219       int aNbSel = GEOMBase::GetNameOfSelectedIObjects(selectedIO(), aName);
220       if(aNbSel < 1)
221         {
222           myObjects.length(0);
223           return;
224         }
225       GEOMBase::ConvertListOfIOInListOfGO(selectedIO(), myObjects);
226       if (!myObjects.length())
227         return;
228       if(aNbSel != 1)
229         aName = tr("%1_objects").arg(aNbSel);
230     }
231   else if(myEditCurrentArgument == GroupPoints->LineEdit2)
232     {
233       if(IObjectCount() != 1)
234         {
235           myArgument = GEOM::GEOM_Object::_nil();
236           return;
237         }
238       Standard_Boolean testResult = Standard_False;
239       GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(firstIObject(), testResult );
240       myArgument = aSelectedObject;
241       if(!testResult || CORBA::is_nil( myArgument ))
242         return;
243
244       aName = GEOMBase::GetName( aSelectedObject );
245
246       if ( testResult && !aSelectedObject->_is_nil() )
247         {
248           TopoDS_Shape aShape;
249           if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() )
250             {
251               TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
252               if (getConstructorId() == 1)
253                 aNeedType = TopAbs_EDGE;
254               else if (getConstructorId() == 2)
255                 aNeedType = TopAbs_FACE;
256               
257               LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
258               TColStd_IndexedMapOfInteger aMap;
259               aSelMgr->GetIndexes( firstIObject(), aMap );
260               if ( aMap.Extent() == 1 )
261                 {
262                     int anIndex = aMap( 1 );
263                     if (aNeedType == TopAbs_VERTEX)
264                       aName += QString(":vertex_%1").arg(anIndex);
265                     else
266                       aName += QString(":edge_%1").arg(anIndex);
267                     
268                     //Find SubShape Object in Father
269                     GEOM::GEOM_Object_var aFindedObject = findObjectInFather(aSelectedObject, aName);
270                     
271                     if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
272                       GEOM::GEOM_IShapesOperations_var aShapesOp =
273                         getGeomEngine()->GetIShapesOperations( getStudyId() );
274                       myArgument = aShapesOp->GetSubShape(aSelectedObject, anIndex);
275                     }
276                     else
277                       myArgument = aFindedObject; // get Object from study
278                 }
279               else {
280                 if (aShape.ShapeType() != aNeedType) {
281                   myArgument = GEOM::GEOM_Object::_nil();
282                   aName = "";
283                 }
284               }
285             }
286         }
287     }
288   myEditCurrentArgument->setText( aName );
289    
290   displayPreview(); 
291 }
292
293
294 //=================================================================================
295 // function : LineEditReturnPressed()
296 // purpose  :
297 //=================================================================================
298 void TransformationGUI_MirrorDlg::LineEditReturnPressed()
299 {
300   QLineEdit* send = (QLineEdit*)sender();
301   if(send == GroupPoints->LineEdit1 ||
302      send == GroupPoints->LineEdit2)
303     {
304       myEditCurrentArgument = send;
305       GEOMBase_Skeleton::LineEditReturnPressed();
306     }
307 }
308
309
310 //=================================================================================
311 // function : SetEditCurrentArgument()
312 // purpose  :
313 //=================================================================================
314 void TransformationGUI_MirrorDlg::SetEditCurrentArgument()
315 {
316   QPushButton* send = (QPushButton*)sender();
317   globalSelection();
318   
319   if(send == GroupPoints->PushButton1){
320     myEditCurrentArgument = GroupPoints->LineEdit1;
321   }
322   else if(send == GroupPoints->PushButton2) {
323     myEditCurrentArgument = GroupPoints->LineEdit2;
324     switch(getConstructorId())
325       {
326       case 0:
327         {
328           localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
329           break;
330         }
331       case 1:
332         {
333           localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
334           break;
335         }
336       case 2:
337         {
338           globalSelection( GEOM_PLANE );
339           break;
340         }
341       }
342   }
343
344   myEditCurrentArgument->setFocus();
345   SelectionIntoArgument();
346 }
347
348
349 //=================================================================================
350 // function : ActivateThisDialog()
351 // purpose  :
352 //=================================================================================
353 void TransformationGUI_MirrorDlg::ActivateThisDialog()
354 {
355   GEOMBase_Skeleton::ActivateThisDialog();
356   connect(myGeomGUI->getApp()->selectionMgr(), 
357           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
358   ConstructorsClicked( getConstructorId() );
359 }
360
361
362 //=================================================================================
363 // function : enterEvent()
364 // purpose  : when mouse enter onto the QWidget
365 //=================================================================================
366 void TransformationGUI_MirrorDlg::enterEvent(QEvent * e)
367 {
368   if(!GroupConstructors->isEnabled())
369     ActivateThisDialog();
370 }
371
372
373 //=================================================================================
374 // function : createOperation
375 // purpose  :
376 //=================================================================================
377 GEOM::GEOM_IOperations_ptr  TransformationGUI_MirrorDlg::createOperation()
378 {
379   return getGeomEngine()->GetITransformOperations( getStudyId() );
380 }
381
382
383 //=================================================================================
384 // function : isValid
385 // purpose  :
386 //=================================================================================
387 bool  TransformationGUI_MirrorDlg::isValid( QString& msg )
388 {
389   return !(myObjects.length() == 0 || myArgument->_is_nil());
390 }
391
392
393 //=================================================================================
394 // function : execute
395 // purpose  :
396 //=================================================================================
397 bool  TransformationGUI_MirrorDlg::execute( ObjectList& objects )
398 {
399   bool res = false;
400   bool toCreateCopy = IsPreview() || GroupPoints->CheckButton1->isChecked();
401   
402   GEOM::GEOM_Object_var anObj;
403   
404   switch ( getConstructorId() ) 
405     {
406     case 0 :
407       {
408         if (toCreateCopy)
409           for (int i = 0; i < myObjects.length(); i++)
410             {
411               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->MirrorPointCopy( myObjects[i], myArgument );
412               if ( !anObj->_is_nil() )
413                 objects.push_back( anObj._retn() );
414             }
415         else
416           for (int i = 0; i < myObjects.length(); i++)
417             {
418               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->MirrorPoint( myObjects[i], myArgument );
419               if ( !anObj->_is_nil() )
420                 objects.push_back( anObj._retn() );
421             }
422         res = true;
423         break;
424       }
425     case 1 :
426       {
427         if (toCreateCopy)
428           for (int i = 0; i < myObjects.length(); i++)
429             {
430               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->MirrorAxisCopy( myObjects[i], myArgument );
431               if ( !anObj->_is_nil() )
432                 objects.push_back( anObj._retn() );
433             }
434         else
435           for (int i = 0; i < myObjects.length(); i++)
436             {
437               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->MirrorAxis( myObjects[i], myArgument );
438               if ( !anObj->_is_nil() )
439                 objects.push_back( anObj._retn() );
440             }
441         res = true;
442         break;
443       }
444     case 2:
445       {
446         if (toCreateCopy)
447            for (int i = 0; i < myObjects.length(); i++)
448             {
449               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->MirrorPlaneCopy( myObjects[i], myArgument );
450               if ( !anObj->_is_nil() )
451                 objects.push_back( anObj._retn() );
452             }
453         else
454            for (int i = 0; i < myObjects.length(); i++)
455             {
456               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->MirrorPlane( myObjects[i], myArgument );
457               if ( !anObj->_is_nil() )
458                 objects.push_back( anObj._retn() );
459             }
460         res = true;
461         break;
462       }
463     }
464   
465   return res;
466 }
467
468 //=================================================================================
469 // function : closeEvent
470 // purpose  :
471 //=================================================================================
472 void  TransformationGUI_MirrorDlg::closeEvent( QCloseEvent* e )
473 {
474   GEOMBase_Skeleton::closeEvent( e );
475 }
476
477
478 //=================================================================================
479 // function :  CreateCopyModeChanged()
480 // purpose  :
481 //=================================================================================
482 void TransformationGUI_MirrorDlg::CreateCopyModeChanged(bool isCreateCopy)
483 {
484   this->GroupBoxName->setEnabled(isCreateCopy);
485 }
486
487 //=================================================================================
488 // function : addSubshapeToStudy
489 // purpose  : virtual method to add new SubObjects if local selection
490 //=================================================================================
491 void TransformationGUI_MirrorDlg::addSubshapesToStudy()
492 {
493   bool toCreateCopy = IsPreview() || GroupPoints->CheckButton1->isChecked();
494   if (toCreateCopy) {
495     QMap<QString, GEOM::GEOM_Object_var> objMap;
496
497     switch (getConstructorId())
498       {
499       case 0:
500         objMap[GroupPoints->LineEdit2->text()] = myArgument;
501         break;
502       case 1:
503         objMap[GroupPoints->LineEdit2->text()] = myArgument;
504         break;
505       case 2:
506         return;
507       }
508     addSubshapesToFather( objMap );
509   }
510 }