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