]> SALOME platform Git repositories - modules/geom.git/blob - src/TransformationGUI/TransformationGUI_MirrorDlg.cxx
Salome HOME
Fix for bug IPAL18899 : 4x REGRESS: Dump python on the study leads to fatal error...
[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   GroupBoxPublish->show();
125
126   /* signals and slots connections */
127   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
128   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
129   connect(GroupConstructors, SIGNAL(clicked(int)), SLOT(ConstructorsClicked(int)));
130
131   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
132   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
133
134   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
135   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
136
137   connect(GroupPoints->CheckButton1, SIGNAL(toggled(bool)), this, SLOT(CreateCopyModeChanged(bool)));
138
139   connect(myGeomGUI->getApp()->selectionMgr(),
140           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
141
142   initName( tr( "GEOM_MIRROR" ) );
143   ConstructorsClicked( 0 );
144 }
145
146
147 //=================================================================================
148 // function : ConstructorsClicked()
149 // purpose  : Radio button management
150 //=================================================================================
151 void TransformationGUI_MirrorDlg::ConstructorsClicked(int constructorId)
152 {
153   disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
154
155   globalSelection();
156   myEditCurrentArgument = GroupPoints->LineEdit1;
157   GroupPoints->LineEdit2->clear();
158   myArgument = GEOM::GEOM_Object::_nil();
159
160   switch (constructorId)
161     {
162     case 0: /* mirror an object by point */
163       {
164         GroupPoints->TextLabel2->setText(tr("GEOM_POINT_MIRROR"));
165         break;
166       }
167     case 1: /* mirror an object by axe */
168       {
169         GroupPoints->TextLabel2->setText(tr("GEOM_AXE_MIRROR"));
170         break;
171       }
172     case 2: /* mirror an object by plane */
173       {
174         GroupPoints->TextLabel2->setText(tr("GEOM_PLANE_MIRROR"));
175         break;
176       }
177     }
178   connect(myGeomGUI->getApp()->selectionMgr(),
179           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
180 }
181
182
183
184 //=================================================================================
185 // function : ClickOnOk()
186 // purpose  :
187 //=================================================================================
188 void TransformationGUI_MirrorDlg::ClickOnOk()
189 {
190   if ( ClickOnApply() )
191     ClickOnCancel();
192 }
193
194
195 //=================================================================================
196 // function : ClickOnApply()
197 // purpose  :
198 //=================================================================================
199 bool TransformationGUI_MirrorDlg::ClickOnApply()
200 {
201   if ( !onAccept(GroupPoints->CheckButton1->isChecked()) )
202     return false;
203
204   initName();
205   ConstructorsClicked( getConstructorId() );
206   return true;
207 }
208
209
210 //=================================================================================
211 // function : SelectionIntoArgument()
212 // purpose  : Called when selection has changed
213 //=================================================================================
214 void TransformationGUI_MirrorDlg::SelectionIntoArgument()
215 {
216   myEditCurrentArgument->setText("");
217   QString aName;
218
219   if(myEditCurrentArgument == GroupPoints->LineEdit1)
220     {
221       int aNbSel = GEOMBase::GetNameOfSelectedIObjects(selectedIO(), aName);
222       if(aNbSel < 1)
223         {
224           myObjects.length(0);
225           return;
226         }
227       GEOMBase::ConvertListOfIOInListOfGO(selectedIO(), myObjects);
228       if (!myObjects.length())
229         return;
230       if(aNbSel != 1)
231         aName = tr("%1_objects").arg(aNbSel);
232     }
233   else if(myEditCurrentArgument == GroupPoints->LineEdit2)
234     {
235       if(IObjectCount() != 1)
236         {
237           myArgument = GEOM::GEOM_Object::_nil();
238           return;
239         }
240       Standard_Boolean testResult = Standard_False;
241       GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(firstIObject(), testResult );
242       myArgument = aSelectedObject;
243       if(!testResult || CORBA::is_nil( myArgument ))
244         return;
245
246       aName = GEOMBase::GetName( aSelectedObject );
247
248       if ( testResult && !aSelectedObject->_is_nil() )
249         {
250           TopoDS_Shape aShape;
251           if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() )
252             {
253               TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
254               if (getConstructorId() == 1)
255                 aNeedType = TopAbs_EDGE;
256               else if (getConstructorId() == 2)
257                 aNeedType = TopAbs_FACE;
258
259               LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
260               TColStd_IndexedMapOfInteger aMap;
261               aSelMgr->GetIndexes( firstIObject(), aMap );
262               if ( aMap.Extent() == 1 )
263                 {
264                     int anIndex = aMap( 1 );
265                     if (aNeedType == TopAbs_VERTEX)
266                       aName += QString(":vertex_%1").arg(anIndex);
267                     else
268                       aName += QString(":edge_%1").arg(anIndex);
269
270                     //Find SubShape Object in Father
271                     GEOM::GEOM_Object_var aFindedObject = findObjectInFather(aSelectedObject, aName);
272
273                     if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
274                       GEOM::GEOM_IShapesOperations_var aShapesOp =
275                         getGeomEngine()->GetIShapesOperations( getStudyId() );
276                       myArgument = aShapesOp->GetSubShape(aSelectedObject, anIndex);
277                     }
278                     else
279                       myArgument = aFindedObject; // get Object from study
280                 }
281               else {
282                 if (aShape.ShapeType() != aNeedType) {
283                   myArgument = GEOM::GEOM_Object::_nil();
284                   aName = "";
285                 }
286               }
287             }
288         }
289     }
290   myEditCurrentArgument->setText( aName );
291
292   displayPreview();
293 }
294
295
296 //=================================================================================
297 // function : LineEditReturnPressed()
298 // purpose  :
299 //=================================================================================
300 void TransformationGUI_MirrorDlg::LineEditReturnPressed()
301 {
302   QLineEdit* send = (QLineEdit*)sender();
303   if(send == GroupPoints->LineEdit1 ||
304      send == GroupPoints->LineEdit2)
305     {
306       myEditCurrentArgument = send;
307       GEOMBase_Skeleton::LineEditReturnPressed();
308     }
309 }
310
311
312 //=================================================================================
313 // function : SetEditCurrentArgument()
314 // purpose  :
315 //=================================================================================
316 void TransformationGUI_MirrorDlg::SetEditCurrentArgument()
317 {
318   QPushButton* send = (QPushButton*)sender();
319   globalSelection();
320
321   if(send == GroupPoints->PushButton1){
322     myEditCurrentArgument = GroupPoints->LineEdit1;
323   }
324   else if(send == GroupPoints->PushButton2) {
325     myEditCurrentArgument = GroupPoints->LineEdit2;
326     switch(getConstructorId())
327       {
328       case 0:
329         {
330           localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
331           break;
332         }
333       case 1:
334         {
335           localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
336           break;
337         }
338       case 2:
339         {
340           globalSelection( GEOM_PLANE );
341           break;
342         }
343       }
344   }
345
346   myEditCurrentArgument->setFocus();
347   SelectionIntoArgument();
348 }
349
350
351 //=================================================================================
352 // function : ActivateThisDialog()
353 // purpose  :
354 //=================================================================================
355 void TransformationGUI_MirrorDlg::ActivateThisDialog()
356 {
357   GEOMBase_Skeleton::ActivateThisDialog();
358   connect(myGeomGUI->getApp()->selectionMgr(),
359           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
360   ConstructorsClicked( getConstructorId() );
361 }
362
363
364 //=================================================================================
365 // function : enterEvent()
366 // purpose  : when mouse enter onto the QWidget
367 //=================================================================================
368 void TransformationGUI_MirrorDlg::enterEvent(QEvent * e)
369 {
370   if(!GroupConstructors->isEnabled())
371     ActivateThisDialog();
372 }
373
374
375 //=================================================================================
376 // function : createOperation
377 // purpose  :
378 //=================================================================================
379 GEOM::GEOM_IOperations_ptr  TransformationGUI_MirrorDlg::createOperation()
380 {
381   return getGeomEngine()->GetITransformOperations( getStudyId() );
382 }
383
384
385 //=================================================================================
386 // function : isValid
387 // purpose  :
388 //=================================================================================
389 bool  TransformationGUI_MirrorDlg::isValid( QString& msg )
390 {
391   return !(myObjects.length() == 0 || myArgument->_is_nil());
392 }
393
394
395 //=================================================================================
396 // function : execute
397 // purpose  :
398 //=================================================================================
399 bool  TransformationGUI_MirrorDlg::execute( ObjectList& objects )
400 {
401   bool res = false;
402   bool toCreateCopy = IsPreview() || GroupPoints->CheckButton1->isChecked();
403
404   GEOM::GEOM_Object_var anObj;
405
406   switch ( getConstructorId() )
407     {
408     case 0 :
409       {
410         if (toCreateCopy)
411           for (int i = 0; i < myObjects.length(); i++)
412             {
413               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->
414                 MirrorPointCopy( myObjects[i], myArgument );
415               if ( !anObj->_is_nil() )
416                 objects.push_back( anObj._retn() );
417             }
418         else
419           for (int i = 0; i < myObjects.length(); i++)
420             {
421               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->
422                 MirrorPoint( myObjects[i], myArgument );
423               if ( !anObj->_is_nil() )
424                 objects.push_back( anObj._retn() );
425             }
426         res = true;
427         break;
428       }
429     case 1 :
430       {
431         if (toCreateCopy)
432           for (int i = 0; i < myObjects.length(); i++)
433             {
434               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->
435                 MirrorAxisCopy( myObjects[i], myArgument );
436               if ( !anObj->_is_nil() )
437                 objects.push_back( anObj._retn() );
438             }
439         else
440           for (int i = 0; i < myObjects.length(); i++)
441             {
442               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->
443                 MirrorAxis( myObjects[i], myArgument );
444               if ( !anObj->_is_nil() )
445                 objects.push_back( anObj._retn() );
446             }
447         res = true;
448         break;
449       }
450     case 2:
451       {
452         if (toCreateCopy)
453            for (int i = 0; i < myObjects.length(); i++)
454             {
455               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->
456                 MirrorPlaneCopy( myObjects[i], myArgument );
457               if ( !anObj->_is_nil() )
458                 objects.push_back( anObj._retn() );
459             }
460         else
461            for (int i = 0; i < myObjects.length(); i++)
462             {
463               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->
464                 MirrorPlane( myObjects[i], myArgument );
465               if ( !anObj->_is_nil() )
466                 objects.push_back( anObj._retn() );
467             }
468         res = true;
469         break;
470       }
471     }
472
473   return res;
474 }
475
476 //=================================================================================
477 // function : restoreSubShapes
478 // purpose  :
479 //=================================================================================
480 void TransformationGUI_MirrorDlg::restoreSubShapes (SALOMEDS::Study_ptr   theStudy,
481                                                     SALOMEDS::SObject_ptr theSObject)
482 {
483   if (CheckBoxRestoreSS->isChecked()) {
484     // empty list of arguments means that all arguments should be restored
485     getGeomEngine()->RestoreSubShapesSO(theStudy, theSObject, GEOM::ListOfGO(),
486                                         /*theFindMethod=*/GEOM::FSM_Transformed,
487                                         /*theInheritFirstArg=*/true);
488   }
489 }
490
491 //=================================================================================
492 // function :  CreateCopyModeChanged()
493 // purpose  :
494 //=================================================================================
495 void TransformationGUI_MirrorDlg::CreateCopyModeChanged(bool isCreateCopy)
496 {
497   this->GroupBoxName->setEnabled(isCreateCopy);
498 }
499
500 //=================================================================================
501 // function : addSubshapeToStudy
502 // purpose  : virtual method to add new SubObjects if local selection
503 //=================================================================================
504 void TransformationGUI_MirrorDlg::addSubshapesToStudy()
505 {
506   bool toCreateCopy = IsPreview() || GroupPoints->CheckButton1->isChecked();
507   if (toCreateCopy) {
508     QMap<QString, GEOM::GEOM_Object_var> objMap;
509
510     switch (getConstructorId())
511       {
512       case 0:
513         objMap[GroupPoints->LineEdit2->text()] = myArgument;
514         break;
515       case 1:
516         objMap[GroupPoints->LineEdit2->text()] = myArgument;
517         break;
518       case 2:
519         return;
520       }
521     addSubshapesToFather( objMap );
522   }
523 }