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