Salome HOME
*** empty log message ***
[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 <GEOM_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 <GEOMImpl_Types.hxx>
38
39 //=================================================================================
40 // class    : TransformationGUI_MirrorDlg()
41 // purpose  : Constructs a TransformationGUI_MirrorDlg which is a child of 'parent', with the 
42 //            name 'name' and widget flags set to 'f'.
43 //            The dialog will by default be modeless, unless you set 'modal' to
44 //            TRUE to construct a modal dialog.
45 //=================================================================================
46 TransformationGUI_MirrorDlg::TransformationGUI_MirrorDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
47                                                           bool modal, Qt::WindowFlags fl )
48   : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
49 {
50   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_MIRROR_POINT" ) ) );
51   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_MIRROR_AXE" ) ) );
52   QPixmap image2( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_MIRROR_PLANE" ) ) );
53   QPixmap image3( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
54
55   setWindowTitle( tr( "GEOM_MIRROR_TITLE" ) );
56
57   /***************************************************************/
58   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_MIRROR" ) );
59   mainFrame()->RadioButton1->setIcon( image0 );
60   mainFrame()->RadioButton2->setIcon( image1 );
61   mainFrame()->RadioButton3->setIcon( image2 );
62  
63   GroupPoints = new DlgRef_2Sel1Spin2Check( centralWidget() );
64   GroupPoints->SpinBox_DX->hide();
65   GroupPoints->TextLabel3->hide();
66   GroupPoints->CheckButton2->hide();
67   GroupPoints->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
68   GroupPoints->TextLabel1->setText( tr( "GEOM_OBJECTS" ) );
69   GroupPoints->TextLabel2->setText( tr( "GEOM_POINT_MIRROR" ) );
70   GroupPoints->TextLabel2->setFixedWidth( 74 );
71   GroupPoints->PushButton1->setIcon( image3 );
72   GroupPoints->PushButton2->setIcon( image3 );
73   GroupPoints->CheckButton1->setText( tr( "GEOM_CREATE_COPY" ) );
74
75   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
76   layout->setMargin( 0 ); layout->setSpacing( 6 );
77   layout->addWidget( GroupPoints );
78   /***************************************************************/
79
80   setHelpFileName( "mirror_image.htm" );
81   
82   Init();
83 }
84
85
86 //=================================================================================
87 // function : ~TransformationGUI_MirrorDlg()
88 // purpose  : Destroys the object and frees any allocated resources
89 //=================================================================================
90 TransformationGUI_MirrorDlg::~TransformationGUI_MirrorDlg()
91 {  
92   /* no need to delete child widgets, Qt does it all for us */
93 }
94
95
96 //=================================================================================
97 // function : Init()
98 // purpose  :
99 //=================================================================================
100 void TransformationGUI_MirrorDlg::Init()
101 {  
102   /* init variables */
103   myEditCurrentArgument = GroupPoints->LineEdit1;
104   GroupPoints->LineEdit1->setReadOnly( true );
105   GroupPoints->LineEdit2->setReadOnly( true );
106   
107   myArgument = GEOM::GEOM_Object::_nil();
108     
109   // Activate Create a Copy mode
110   GroupPoints->CheckButton1->setChecked( true );
111   CreateCopyModeChanged( true );
112
113   /* signals and slots connections */
114   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
115   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
116
117   connect( this, SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
118
119   connect( GroupPoints->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
120   connect( GroupPoints->PushButton2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
121
122   connect( GroupPoints->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
123   connect( GroupPoints->LineEdit2,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
124   
125   connect( GroupPoints->CheckButton1, SIGNAL( toggled( bool ) ), this, SLOT( CreateCopyModeChanged( bool ) ) );
126   
127   connect( myGeomGUI->getApp()->selectionMgr(), 
128            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
129
130   initName( tr( "GEOM_MIRROR" ) );
131
132   ConstructorsClicked( 0 );
133 }
134
135
136 //=================================================================================
137 // function : ConstructorsClicked()
138 // purpose  : Radio button management
139 //=================================================================================
140 void TransformationGUI_MirrorDlg::ConstructorsClicked( int constructorId )
141 {
142   disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
143   
144   globalSelection();
145   myEditCurrentArgument = GroupPoints->LineEdit1;
146   GroupPoints->LineEdit2->clear();
147   myArgument = GEOM::GEOM_Object::_nil();
148   
149   switch ( constructorId ) {
150   case 0: /* mirror an object by point */
151     GroupPoints->TextLabel2->setText( tr( "GEOM_POINT_MIRROR" ) );
152     break;
153   case 1: /* mirror an object by axe */
154     GroupPoints->TextLabel2->setText( tr( "GEOM_AXE_MIRROR" ) );
155     break;
156   case 2: /* mirror an object by plane */
157     GroupPoints->TextLabel2->setText( tr( "GEOM_PLANE_MIRROR" ) );
158     break;
159   }
160
161   connect( myGeomGUI->getApp()->selectionMgr(), 
162            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
163 }
164
165
166
167 //=================================================================================
168 // function : ClickOnOk()
169 // purpose  :
170 //=================================================================================
171 void TransformationGUI_MirrorDlg::ClickOnOk()
172 {
173   if ( ClickOnApply() )
174     ClickOnCancel();
175 }
176
177
178 //=================================================================================
179 // function : ClickOnApply()
180 // purpose  :
181 //=================================================================================
182 bool TransformationGUI_MirrorDlg::ClickOnApply()
183 {
184   if ( !onAccept( GroupPoints->CheckButton1->isChecked() ) )
185     return false;
186   
187   initName();
188   ConstructorsClicked( getConstructorId() );
189   return true;
190 }
191
192
193 //=================================================================================
194 // function : SelectionIntoArgument()
195 // purpose  : Called when selection has changed
196 //=================================================================================
197 void TransformationGUI_MirrorDlg::SelectionIntoArgument()
198 {
199   myEditCurrentArgument->setText( "" );
200   QString aName;
201
202   if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
203     int aNbSel = GEOMBase::GetNameOfSelectedIObjects( selectedIO(), aName );
204     if ( aNbSel < 1 ) {
205       myObjects.length( 0 );
206       return;
207     }
208     GEOMBase::ConvertListOfIOInListOfGO( selectedIO(), myObjects );
209     if ( !myObjects.length() )
210       return;
211     if ( aNbSel != 1 )
212       aName = tr( "%1_objects" ).arg( aNbSel );
213   }
214   else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
215     if ( IObjectCount() != 1 ) {
216       myArgument = GEOM::GEOM_Object::_nil();
217       return;
218     }
219     Standard_Boolean testResult = Standard_False;
220     myArgument = GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
221     if ( !testResult || CORBA::is_nil( myArgument ) )
222       return;
223     aName = GEOMBase::GetName( myArgument );
224   }
225   myEditCurrentArgument->setText( aName );
226    
227   displayPreview(); 
228 }
229
230
231 //=================================================================================
232 // function : LineEditReturnPressed()
233 // purpose  :
234 //=================================================================================
235 void TransformationGUI_MirrorDlg::LineEditReturnPressed()
236 {
237   QLineEdit* send = (QLineEdit*)sender();
238   if ( send == GroupPoints->LineEdit1 ||
239        send == GroupPoints->LineEdit2 ) {
240     myEditCurrentArgument = send;
241     GEOMBase_Skeleton::LineEditReturnPressed();
242   }
243 }
244
245
246 //=================================================================================
247 // function : SetEditCurrentArgument()
248 // purpose  :
249 //=================================================================================
250 void TransformationGUI_MirrorDlg::SetEditCurrentArgument()
251 {
252   QPushButton* send = (QPushButton*)sender();
253   
254   if ( send == GroupPoints->PushButton1 ) {
255     myEditCurrentArgument = GroupPoints->LineEdit1;
256     globalSelection();
257   }
258   else if ( send == GroupPoints->PushButton2 ) {
259     myEditCurrentArgument = GroupPoints->LineEdit2;
260     switch ( getConstructorId() ) {
261     case 0:
262       globalSelection( GEOM_POINT );
263       break;
264     case 1:
265       globalSelection( GEOM_LINE );
266       break;
267     case 2:
268       globalSelection( GEOM_PLANE );
269       break;
270     }
271   }
272
273   myEditCurrentArgument->setFocus();
274   SelectionIntoArgument();
275 }
276
277
278 //=================================================================================
279 // function : ActivateThisDialog()
280 // purpose  :
281 //=================================================================================
282 void TransformationGUI_MirrorDlg::ActivateThisDialog()
283 {
284   GEOMBase_Skeleton::ActivateThisDialog();
285   connect( myGeomGUI->getApp()->selectionMgr(),
286            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
287   ConstructorsClicked( getConstructorId() );
288 }
289
290
291 //=================================================================================
292 // function : enterEvent()
293 // purpose  : when mouse enter onto the QWidget
294 //=================================================================================
295 void TransformationGUI_MirrorDlg::enterEvent( QEvent* )
296 {
297   if ( !mainFrame()->GroupConstructors->isEnabled() )
298     ActivateThisDialog();
299 }
300
301
302 //=================================================================================
303 // function : createOperation
304 // purpose  :
305 //=================================================================================
306 GEOM::GEOM_IOperations_ptr  TransformationGUI_MirrorDlg::createOperation()
307 {
308   return getGeomEngine()->GetITransformOperations( getStudyId() );
309 }
310
311
312 //=================================================================================
313 // function : isValid
314 // purpose  :
315 //=================================================================================
316 bool  TransformationGUI_MirrorDlg::isValid( QString& /*msg*/ )
317 {
318   return !( myObjects.length() == 0 || myArgument->_is_nil() );
319 }
320
321
322 //=================================================================================
323 // function : execute
324 // purpose  :
325 //=================================================================================
326 bool  TransformationGUI_MirrorDlg::execute( ObjectList& objects )
327 {
328   bool res = false;
329   bool toCreateCopy = IsPreview() || GroupPoints->CheckButton1->isChecked();
330   
331   GEOM::GEOM_Object_var anObj;
332   
333   switch ( getConstructorId() ) {
334   case 0:
335     {
336       if ( toCreateCopy ) {
337         for ( int i = 0; i < myObjects.length(); i++ ) {
338           anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->MirrorPointCopy( myObjects[i], myArgument );
339           if ( !anObj->_is_nil() )
340             objects.push_back( anObj._retn() );
341         }
342       }
343       else {
344         for ( int i = 0; i < myObjects.length(); i++ ) {
345           anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->MirrorPoint( myObjects[i], myArgument );
346           if ( !anObj->_is_nil() )
347             objects.push_back( anObj._retn() );
348         }
349       }
350       res = true;
351       break;
352     }
353   case 1:
354     {
355       if ( toCreateCopy ) {
356         for ( int i = 0; i < myObjects.length(); i++ ) {
357           anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->MirrorAxisCopy( myObjects[i], myArgument );
358           if ( !anObj->_is_nil() )
359             objects.push_back( anObj._retn() );
360         }
361       }
362       else {
363         for ( int i = 0; i < myObjects.length(); i++ ) {
364           anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->MirrorAxis( myObjects[i], myArgument );
365           if ( !anObj->_is_nil() )
366             objects.push_back( anObj._retn() );
367         }
368       }
369       res = true;
370       break;
371     }
372   case 2:
373     {
374       if ( toCreateCopy ) {
375         for ( int i = 0; i < myObjects.length(); i++ ) {
376             anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->MirrorPlaneCopy( myObjects[i], myArgument );
377             if ( !anObj->_is_nil() )
378               objects.push_back( anObj._retn() );
379         }
380       }
381       else {
382         for ( int i = 0; i < myObjects.length(); i++ ) {
383           anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->MirrorPlane( myObjects[i], myArgument );
384           if ( !anObj->_is_nil() )
385             objects.push_back( anObj._retn() );
386         }
387       }
388       res = true;
389       break;
390     }
391   }
392   
393   return res;
394 }
395
396 //=================================================================================
397 // function : closeEvent
398 // purpose  :
399 //=================================================================================
400 void  TransformationGUI_MirrorDlg::closeEvent( QCloseEvent* e )
401 {
402   GEOMBase_Skeleton::closeEvent( e );
403 }
404
405
406 //=================================================================================
407 // function :  CreateCopyModeChanged()
408 // purpose  :
409 //=================================================================================
410 void TransformationGUI_MirrorDlg::CreateCopyModeChanged( bool isCreateCopy )
411 {
412   mainFrame()->GroupBoxName->setEnabled( isCreateCopy );
413 }