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