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