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