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