Salome HOME
Merge from BR_Dev_For_4_0 branch (from tag mergeto_BR_QT4_Dev_17Jan08)
[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   /* 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( Group1->PushButton1, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
129   connect( Group1->PushButton2, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
130   connect( Group1->PushButton3, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
131
132   connect( Group1->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
133   connect( Group1->LineEdit2,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
134   connect( Group1->LineEdit3,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
135   
136   connect( Group1->CheckBox1,   SIGNAL( toggled( bool ) ), this, SLOT( CreateCopyModeChanged( bool ) ) );
137   
138   connect( myGeomGUI->getApp()->selectionMgr(), 
139            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
140
141   initName( tr( "GEOM_POSITION" ) );
142
143   ConstructorsClicked( 0 );
144 }
145
146
147
148 //=================================================================================
149 // function : ConstructorsClicked()
150 // purpose  : Radio button management
151 //=================================================================================
152 void TransformationGUI_PositionDlg::ConstructorsClicked( int constructorId )
153 {
154   disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
155   
156   globalSelection();
157   myEditCurrentArgument = Group1->LineEdit1;
158   Group1->LineEdit2->clear();
159   Group1->LineEdit3->clear();
160   myStartLCS = GEOM::GEOM_Object::_nil();
161   myEndLCS = GEOM::GEOM_Object::_nil();
162   
163   switch ( constructorId ) {
164   case 0:
165     Group1->LineEdit2->hide();
166     Group1->TextLabel2->hide();
167     Group1->PushButton2->hide();
168     break;
169   case 1:
170     Group1->LineEdit2->show();
171     Group1->TextLabel2->show();
172     Group1->PushButton2->show();
173     break;
174   }
175   
176   qApp->processEvents();
177   updateGeometry();
178   resize( minimumSize() );
179
180   connect( myGeomGUI->getApp()->selectionMgr(),
181            SIGNAL( currentSelectionChanged() ), this, SLOT(SelectionIntoArgument() ) );
182   SelectionIntoArgument();
183 }
184
185
186 //=================================================================================
187 // function : ClickOnOk()
188 // purpose  :
189 //=================================================================================
190 void TransformationGUI_PositionDlg::ClickOnOk()
191 {
192   if ( ClickOnApply() )
193     ClickOnCancel();
194 }
195
196
197 //=================================================================================
198 // function : ClickOnApply()
199 // purpose  :
200 //=================================================================================
201 bool TransformationGUI_PositionDlg::ClickOnApply()
202 {
203   if ( !onAccept( Group1->CheckBox1->isChecked() ) )
204     return false;
205   
206   initName();
207   ConstructorsClicked( getConstructorId() );
208   return true;
209 }
210
211
212 //=================================================================================
213 // function : SelectionIntoArgument()
214 // purpose  : Called when selection has changed
215 //=================================================================================
216 void TransformationGUI_PositionDlg::SelectionIntoArgument()
217 {
218   myEditCurrentArgument->setText( "" );
219   QString aName;
220
221   if ( myEditCurrentArgument == Group1->LineEdit1 ) {
222     int aNbSel = GEOMBase::GetNameOfSelectedIObjects( selectedIO(), aName );
223     if ( aNbSel < 1 ) {
224       myObjects.length( 0 );
225       displayPreview();
226       return;
227     }
228     GEOMBase::ConvertListOfIOInListOfGO( selectedIO(), myObjects );
229     if ( !myObjects.length() ) {
230       displayPreview();
231       return;
232     }
233   }
234   else if ( myEditCurrentArgument == Group1->LineEdit2 ) {
235     myStartLCS = GEOM::GEOM_Object::_nil();
236     if ( IObjectCount() != 1 ) {
237       displayPreview();
238       return;
239     }
240
241     Standard_Boolean testResult = Standard_False;
242     myStartLCS = GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
243     if ( !testResult || CORBA::is_nil( myStartLCS ) ) {
244       displayPreview();
245       return;
246     }
247     aName = GEOMBase::GetName( myStartLCS );
248   }
249   else if ( myEditCurrentArgument == Group1->LineEdit3 ) {
250     myEndLCS = GEOM::GEOM_Object::_nil();
251     if ( IObjectCount() != 1 ) {
252       displayPreview();
253       return;
254     }
255     
256     Standard_Boolean testResult = Standard_False;
257     myEndLCS = GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
258     if ( !testResult || CORBA::is_nil( myEndLCS ) ) {
259       displayPreview();
260       return;
261     }
262     aName = GEOMBase::GetName( myEndLCS );
263   }
264
265   myEditCurrentArgument->setText( aName );
266   displayPreview(); 
267 }
268
269
270 //=================================================================================
271 // function : LineEditReturnPressed()
272 // purpose  :
273 //=================================================================================
274 void TransformationGUI_PositionDlg::LineEditReturnPressed()
275 {
276   QLineEdit* send = (QLineEdit*)sender();
277   if ( send == Group1->LineEdit1 || send == Group1->LineEdit2 || 
278        send == Group1->LineEdit3 ) {
279     myEditCurrentArgument = send;
280     GEOMBase_Skeleton::LineEditReturnPressed();
281   }
282 }
283
284
285 //=================================================================================
286 // function : SetEditCurrentArgument()
287 // purpose  :
288 //=================================================================================
289 void TransformationGUI_PositionDlg::SetEditCurrentArgument()
290 {
291   QPushButton* send = (QPushButton*)sender();
292   
293   if ( send == Group1->PushButton1 ) {
294     myEditCurrentArgument = Group1->LineEdit1;
295     globalSelection();
296   }
297   else if ( send == Group1->PushButton2 ) {
298     myEditCurrentArgument = Group1->LineEdit2;
299     TColStd_MapOfInteger aMap;
300     aMap.Add( GEOM_PLANE );
301     aMap.Add( GEOM_MARKER );
302     globalSelection( aMap );
303   }
304   else if ( send == Group1->PushButton3 ) {
305     myEditCurrentArgument = Group1->LineEdit3;
306     TColStd_MapOfInteger aMap;
307     aMap.Add( GEOM_PLANE );
308     aMap.Add( GEOM_MARKER );
309     globalSelection( aMap );
310   }
311
312   myEditCurrentArgument->setFocus();
313   SelectionIntoArgument();
314 }
315
316
317 //=================================================================================
318 // function : ActivateThisDialog()
319 // purpose  :
320 //=================================================================================
321 void TransformationGUI_PositionDlg::ActivateThisDialog()
322 {
323   GEOMBase_Skeleton::ActivateThisDialog();
324   connect( myGeomGUI->getApp()->selectionMgr(), 
325            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
326   ConstructorsClicked( getConstructorId() );
327 }
328
329
330 //=================================================================================
331 // function : enterEvent()
332 // purpose  : when mouse enter onto the QWidget
333 //=================================================================================
334 void TransformationGUI_PositionDlg::enterEvent( QEvent* )
335 {
336   if ( !mainFrame()->GroupConstructors->isEnabled() )
337     ActivateThisDialog();
338 }
339
340
341 //=================================================================================
342 // function : createOperation
343 // purpose  :
344 //=================================================================================
345 GEOM::GEOM_IOperations_ptr  TransformationGUI_PositionDlg::createOperation()
346 {
347   return getGeomEngine()->GetITransformOperations( getStudyId() );
348 }
349
350
351 //=================================================================================
352 // function : isValid
353 // purpose  :
354 //=================================================================================
355 bool TransformationGUI_PositionDlg::isValid( QString& /*msg*/ )
356 {
357   bool res;
358   if ( getConstructorId() == 0 )
359     res = !( myObjects.length() == 0 || myEndLCS->_is_nil() );
360   else
361     res = !( myObjects.length() == 0 || myStartLCS->_is_nil() || myEndLCS->_is_nil() );
362
363   return res;
364 }
365
366
367 //=================================================================================
368 // function : execute
369 // purpose  :
370 //=================================================================================
371 bool TransformationGUI_PositionDlg::execute( ObjectList& objects )
372 {
373   bool res = false;
374   bool toCreateCopy = IsPreview() || Group1->CheckBox1->isChecked();
375   GEOM::GEOM_Object_var anObj;
376   
377   switch ( getConstructorId() ) {
378   case 0:
379     {
380       for ( int i = 0; i < myObjects.length(); i++ ) {
381         if ( toCreateCopy )
382           anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->PositionShapeCopy( myObjects[i], myObjects[i], myEndLCS );
383         else
384           anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->PositionShape( myObjects[i], myObjects[i], myEndLCS );
385         
386         if ( !anObj->_is_nil() )
387           objects.push_back( anObj._retn() );
388       }
389       res = true;
390       break;
391     }
392   case 1:
393     {
394       for ( int i = 0; i < myObjects.length(); i++ ) {
395         if ( toCreateCopy )
396           anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->PositionShapeCopy( myObjects[i], myStartLCS, myEndLCS );
397         else
398           anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->PositionShape( myObjects[i], myStartLCS, myEndLCS );
399         if ( !anObj->_is_nil() )
400           objects.push_back( anObj._retn() );
401       }
402       res = true;
403       break;
404     }
405   }
406   
407   return res;
408 }
409
410
411 //=================================================================================
412 // function :  CreateCopyModeChanged()
413 // purpose  :
414 //=================================================================================
415 void TransformationGUI_PositionDlg::CreateCopyModeChanged( bool isCreateCopy )
416 {
417   mainFrame()->GroupBoxName->setEnabled( isCreateCopy );
418 }