]> SALOME platform Git repositories - modules/geom.git/blob - src/TransformationGUI/TransformationGUI_MirrorDlg.cxx
Salome HOME
Eliminate useless GetSubShape() lines in the python dump
[modules/geom.git] / src / TransformationGUI / TransformationGUI_MirrorDlg.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : TransformationGUI_MirrorDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
26 //
27 #include "TransformationGUI_MirrorDlg.h"
28
29 #include <DlgRef.h>
30 #include <GeometryGUI.h>
31 #include <GEOMBase.h>
32
33 #include <SUIT_Session.h>
34 #include <SUIT_ResourceMgr.h>
35 #include <SalomeApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37
38 // OCCT Includes
39 #include <TopoDS_Shape.hxx>
40 #include <TopoDS_Edge.hxx>
41 #include <TopoDS.hxx>
42 #include <TopExp.hxx>
43 #include <TColStd_IndexedMapOfInteger.hxx>
44 #include <TopTools_IndexedMapOfShape.hxx>
45
46 #include <GEOMImpl_Types.hxx>
47
48 //=================================================================================
49 // class    : TransformationGUI_MirrorDlg()
50 // purpose  : Constructs a TransformationGUI_MirrorDlg which is a child of 'parent', with the
51 //            name 'name' and widget flags set to 'f'.
52 //            The dialog will by default be modeless, unless you set 'modal' to
53 //            TRUE to construct a modal dialog.
54 //=================================================================================
55 TransformationGUI_MirrorDlg::TransformationGUI_MirrorDlg (GeometryGUI* theGeometryGUI, QWidget* parent,
56                                                           bool modal, Qt::WindowFlags fl)
57   : GEOMBase_Skeleton(theGeometryGUI, parent, modal, fl),
58     myInitial(true)
59 {
60   SUIT_ResourceMgr* aResMgr = myGeomGUI->getApp()->resourceMgr();
61   QPixmap image0 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_MIRROR_POINT")));
62   QPixmap image1 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_MIRROR_AXE")));
63   QPixmap image2 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_MIRROR_PLANE")));
64   QPixmap image3 (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
65
66   setWindowTitle(tr("GEOM_MIRROR_TITLE"));
67
68   /***************************************************************/
69   mainFrame()->GroupConstructors->setTitle(tr("GEOM_MIRROR"));
70   mainFrame()->RadioButton1->setIcon(image0);
71   mainFrame()->RadioButton2->setIcon(image1);
72   mainFrame()->RadioButton3->setIcon(image2);
73
74   GroupPoints = new DlgRef_2Sel1Spin2Check(centralWidget());
75   GroupPoints->SpinBox_DX->hide();
76   GroupPoints->TextLabel3->hide();
77   GroupPoints->CheckButton2->hide();
78   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
79   GroupPoints->TextLabel1->setText(tr("GEOM_OBJECTS"));
80   GroupPoints->TextLabel2->setText(tr("GEOM_POINT_MIRROR"));
81   GroupPoints->TextLabel2->setFixedWidth(74);
82   GroupPoints->PushButton1->setIcon(image3);
83   GroupPoints->PushButton2->setIcon(image3);
84   GroupPoints->CheckButton1->setText(tr("GEOM_CREATE_COPY"));
85
86   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
87   layout->setMargin(0); layout->setSpacing(6);
88   layout->addWidget(GroupPoints);
89   /***************************************************************/
90
91   setHelpFileName("mirror_operation_page.html");
92
93   // Activate Create a Copy mode
94   GroupPoints->CheckButton1->setChecked(true);
95   CreateCopyModeChanged();
96
97   Init();
98 }
99
100 //=================================================================================
101 // function : ~TransformationGUI_MirrorDlg()
102 // purpose  : Destroys the object and frees any allocated resources
103 //=================================================================================
104 TransformationGUI_MirrorDlg::~TransformationGUI_MirrorDlg()
105 {
106   // no need to delete child widgets, Qt does it all for us
107 }
108
109 //=================================================================================
110 // function : Init()
111 // purpose  :
112 //=================================================================================
113 void TransformationGUI_MirrorDlg::Init()
114 {
115   // init variables
116   GroupPoints->LineEdit1->setReadOnly(true);
117   GroupPoints->LineEdit2->setReadOnly(true);
118
119   GroupPoints->LineEdit1->setText("");
120   GroupPoints->LineEdit2->setText("");
121
122   myObjects.clear();
123   myArgument.nullify();
124
125   mainFrame()->GroupBoxPublish->show();
126
127   // signals and slots connections
128   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
129   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
130
131   connect(this, SIGNAL(constructorsClicked(int)), this, SLOT(ConstructorsClicked(int)));
132
133   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
134   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
135
136   connect(GroupPoints->CheckButton1, SIGNAL(toggled(bool)), this, SLOT(CreateCopyModeChanged()));
137
138   initName(tr("GEOM_MIRROR"));
139
140   ConstructorsClicked(0);
141   resize(minimumSizeHint());
142 }
143
144 //=================================================================================
145 // function : ConstructorsClicked()
146 // purpose  : Radio button management
147 //=================================================================================
148 void TransformationGUI_MirrorDlg::ConstructorsClicked (int constructorId)
149 {
150   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
151
152   GroupPoints->LineEdit2->clear();
153   myArgument.nullify();
154
155   switch (constructorId) {
156   case 0: // mirror an object by point
157     GroupPoints->TextLabel2->setText(tr("GEOM_POINT_MIRROR"));
158     break;
159   case 1: // mirror an object by axe
160     GroupPoints->TextLabel2->setText(tr("GEOM_AXE_MIRROR"));
161     break;
162   case 2: // mirror an object by plane
163     GroupPoints->TextLabel2->setText(tr("GEOM_PLANE_MIRROR"));
164     break;
165   }
166
167   GroupPoints->PushButton1->click();
168
169   if (myInitial) {
170     myInitial = false;
171     SelectionIntoArgument();
172   }
173   else {
174     displayPreview();
175   }
176 }
177
178 //=================================================================================
179 // function : ClickOnOk()
180 // purpose  :
181 //=================================================================================
182 void TransformationGUI_MirrorDlg::ClickOnOk()
183 {
184   if (ClickOnApply())
185     ClickOnCancel();
186 }
187
188 //=================================================================================
189 // function : ClickOnApply()
190 // purpose  :
191 //=================================================================================
192 bool TransformationGUI_MirrorDlg::ClickOnApply()
193 {
194   if (!onAccept(GroupPoints->CheckButton1->isChecked()))
195     return false;
196
197   initName();
198
199   myObjects.clear();
200   myEditCurrentArgument = GroupPoints->LineEdit1;
201   myEditCurrentArgument->setText("");
202   myGeomGUI->getApp()->selectionMgr()->clearSelected();
203
204   // activate selection and connect selection manager
205   ConstructorsClicked(getConstructorId());
206   return true;
207 }
208
209 //=================================================================================
210 // function : SelectionIntoArgument()
211 // purpose  : Called when selection is changed or on dialog initialization or activation
212 //=================================================================================
213 void TransformationGUI_MirrorDlg::SelectionIntoArgument()
214 {
215   erasePreview();
216
217   if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
218     myObjects = getSelected( TopAbs_SHAPE, -1 );
219     if ( !myObjects.isEmpty() ) {
220       QString aName = myObjects.count() > 1 ? QString( "%1_objects").arg( myObjects.count() ) : GEOMBase::GetName( myObjects[0].get() );
221       myEditCurrentArgument->setText( aName );
222     }
223     else {
224       myEditCurrentArgument->setText("");
225     }
226   }
227   else {
228     TopAbs_ShapeEnum aNeedType = ( getConstructorId() == 1 ) ? 
229       TopAbs_EDGE : ( ( getConstructorId() == 2 ) ? TopAbs_FACE : TopAbs_VERTEX );
230     GEOM::GeomObjPtr aSelectedObject = getSelected( aNeedType );
231     TopoDS_Shape aShape;
232     if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) {
233       QString aName = GEOMBase::GetName( aSelectedObject.get() );
234       myEditCurrentArgument->setText( aName );
235       myArgument = aSelectedObject;
236       if ( myObjects.isEmpty() )
237         GroupPoints->PushButton1->click();
238     }
239     else {
240       if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
241         myArgument.nullify();
242       myEditCurrentArgument->setText("");
243     }
244   }
245
246   displayPreview();
247 }
248
249 //=================================================================================
250 // function : SetEditCurrentArgument()
251 // purpose  :
252 //=================================================================================
253 void TransformationGUI_MirrorDlg::SetEditCurrentArgument()
254 {
255   QPushButton* send = (QPushButton*)sender();
256
257   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
258   globalSelection();
259
260   if (send == GroupPoints->PushButton1) {
261     myEditCurrentArgument = GroupPoints->LineEdit1;
262
263     GroupPoints->PushButton2->setDown(false);
264     GroupPoints->LineEdit2->setEnabled(false);
265   }
266   else if (send == GroupPoints->PushButton2) {
267     myEditCurrentArgument = GroupPoints->LineEdit2;
268
269     switch (getConstructorId()) {
270     case 0:
271       localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
272       break;
273     case 1:
274       localSelection(GEOM::GEOM_Object::_nil(), TopAbs_EDGE);
275       break;
276     case 2:
277       globalSelection(GEOM_PLANE);
278       localSelection(GEOM::GEOM_Object::_nil(), TopAbs_FACE);
279       break;
280     }
281
282     GroupPoints->PushButton1->setDown(false);
283     GroupPoints->LineEdit1->setEnabled(false);
284   }
285   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
286           this, SLOT(SelectionIntoArgument()));
287
288   // enable line edit
289   myEditCurrentArgument->setEnabled(true);
290   myEditCurrentArgument->setFocus();
291   // after setFocus(), because it will be setDown(false) when loses focus
292   send->setDown(true);
293
294   // seems we need it only to avoid preview disappearing, caused by selection mode change
295   displayPreview();
296 }
297
298 //=================================================================================
299 // function : ActivateThisDialog()
300 // purpose  :
301 //=================================================================================
302 void TransformationGUI_MirrorDlg::ActivateThisDialog()
303 {
304   GEOMBase_Skeleton::ActivateThisDialog();
305   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
306            this, SLOT( SelectionIntoArgument() ) );
307
308   ConstructorsClicked( getConstructorId() );
309 }
310
311 //=================================================================================
312 // function : enterEvent()
313 // purpose  : when mouse enter onto the QWidget
314 //=================================================================================
315 void TransformationGUI_MirrorDlg::enterEvent (QEvent*)
316 {
317   if (!mainFrame()->GroupConstructors->isEnabled())
318     ActivateThisDialog();
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 // function : isValid
332 // purpose  :
333 //=================================================================================
334 bool TransformationGUI_MirrorDlg::isValid (QString& /*msg*/)
335 {
336   return !myObjects.isEmpty() && myArgument;
337 }
338
339 //=================================================================================
340 // function : execute
341 // purpose  :
342 //=================================================================================
343 bool  TransformationGUI_MirrorDlg::execute (ObjectList& objects)
344 {
345   bool res = false;
346   bool toCreateCopy = IsPreview() || GroupPoints->CheckButton1->isChecked();
347
348   GEOM::GEOM_Object_var anObj;
349
350   GEOM::GEOM_ITransformOperations_var anOper = GEOM::GEOM_ITransformOperations::_narrow(getOperation());
351
352   switch (getConstructorId()) {
353   case 0:
354     {
355       if (toCreateCopy) {
356         for (int i = 0; i < myObjects.count(); i++) {
357           anObj = anOper->MirrorPointCopy(myObjects[i].get(), myArgument.get());
358           if (!anObj->_is_nil())
359             objects.push_back(anObj._retn());
360         }
361       }
362       else {
363         for (int i = 0; i < myObjects.count(); i++) {
364           anObj = anOper->MirrorPoint(myObjects[i].get(), myArgument.get());
365           if (!anObj->_is_nil())
366             objects.push_back(anObj._retn());
367         }
368       }
369       res = true;
370       break;
371     }
372   case 1:
373     {
374       if (toCreateCopy) {
375         for (int i = 0; i < myObjects.count(); i++) {
376           anObj = anOper->MirrorAxisCopy(myObjects[i].get(), myArgument.get());
377           if (!anObj->_is_nil())
378             objects.push_back(anObj._retn());
379         }
380       }
381       else {
382         for (int i = 0; i < myObjects.count(); i++) {
383           anObj = anOper->MirrorAxis(myObjects[i].get(), myArgument.get());
384           if (!anObj->_is_nil())
385             objects.push_back(anObj._retn());
386         }
387       }
388       res = true;
389       break;
390     }
391   case 2:
392     {
393       if (toCreateCopy) {
394         for (int i = 0; i < myObjects.count(); i++) {
395             anObj = anOper->MirrorPlaneCopy(myObjects[i].get(), myArgument.get());
396             if (!anObj->_is_nil())
397               objects.push_back(anObj._retn());
398         }
399       }
400       else {
401         for (int i = 0; i < myObjects.count(); i++) {
402           anObj = anOper->MirrorPlane(myObjects[i].get(), myArgument.get());
403           if (!anObj->_is_nil())
404             objects.push_back(anObj._retn());
405         }
406       }
407       res = true;
408       break;
409     }
410   }
411
412   return res;
413 }
414
415 //=================================================================================
416 // function : restoreSubShapes
417 // purpose  :
418 //=================================================================================
419 void TransformationGUI_MirrorDlg::restoreSubShapes (SALOMEDS::Study_ptr   theStudy,
420                                                     SALOMEDS::SObject_ptr theSObject)
421 {
422   if (mainFrame()->CheckBoxRestoreSS->isChecked()) {
423     // empty list of arguments means that all arguments should be restored
424     getGeomEngine()->RestoreSubShapesSO(theStudy, theSObject, GEOM::ListOfGO(),
425                                         /*theFindMethod=*/GEOM::FSM_Transformed,
426                                         /*theInheritFirstArg=*/true,
427                                         mainFrame()->CheckBoxAddPrefix->isChecked());
428   }
429 }
430
431 //=================================================================================
432 // function :  CreateCopyModeChanged()
433 // purpose  :
434 //=================================================================================
435 void TransformationGUI_MirrorDlg::CreateCopyModeChanged()
436 {
437   mainFrame()->GroupBoxName->setEnabled(GroupPoints->CheckButton1->isChecked());
438 }
439
440 //=================================================================================
441 // function : addSubshapeToStudy
442 // purpose  : virtual method to add new SubObjects if local selection
443 //=================================================================================
444 void TransformationGUI_MirrorDlg::addSubshapesToStudy()
445 {
446   bool toCreateCopy = IsPreview() || GroupPoints->CheckButton1->isChecked();
447   if (toCreateCopy) {
448     switch (getConstructorId()) {
449     case 0:
450     case 1:
451     case 2:
452       GEOMBase::PublishSubObject( myArgument.get() );
453       break;
454     default:
455       break;
456     }
457   }
458 }