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