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