Salome HOME
778af7b67a5633e418c9b3eaf109f9282450ecaa
[modules/geom.git] / src / OperationGUI / OperationGUI_GetShapesOnShapeDlg.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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 // GEOM GEOMGUI : GUI for Geometry component
23 // File   : OperationGUI_GetShapesOnShapeDlg.cxx
24 // Author : Sergey KUUL, Open CASCADE S.A.S. (sergey.kuul@opencascade.com)
25 //
26 #include "OperationGUI_GetShapesOnShapeDlg.h"
27
28 #include <DlgRef.h>
29 #include <GeometryGUI.h>
30 #include <GEOMBase.h>
31
32 #include <SUIT_Session.h>
33 #include <SUIT_ResourceMgr.h>
34 #include <SalomeApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36
37 //==============================================================================
38 // class    : OperationGUI_GetShapesOnShapeDlg()
39 // purpose  : Constructs a OperationGUI_GetShapesOnShapeDlg which is a child of
40 //            'parent', with the name 'name' and widget flags set to 'f'.
41 //            The dialog will by default be modeless, unless you set 'modal' to
42 //            TRUE to construct a modal dialog.
43 //=================================================================================
44 OperationGUI_GetShapesOnShapeDlg::OperationGUI_GetShapesOnShapeDlg (GeometryGUI* theGeometryGUI,
45                                                                     QWidget* parent)
46   : GEOMBase_Skeleton(theGeometryGUI, parent, false)
47 {
48   SUIT_ResourceMgr* aResMgr = myGeomGUI->getApp()->resourceMgr();
49   QPixmap image0 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_SHAPES_ON_SHAPE")));
50   QPixmap image2 (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
51
52   setWindowTitle(tr("GEOM_SHAPES_ON_SHAPE_TITLE"));
53
54   /***************************************************************/
55   mainFrame()->GroupConstructors->setTitle(tr("GEOM_SHAPES_ON_SHAPE"));
56   mainFrame()->RadioButton1->setIcon(image0);
57   mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
58   mainFrame()->RadioButton2->close();
59   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
60   mainFrame()->RadioButton3->close();
61
62   GroupPoints = new DlgRef_2Sel2List(centralWidget());
63   //GroupPoints->GroupBox1->setTitle(tr("GEOM_PARTITION"));
64   GroupPoints->GroupBox1->setTitle("Input data");
65   GroupPoints->TextLabel1->setText(tr("GEOM_SHAPES_ON_SHAPE_ESHAPE"));
66   GroupPoints->TextLabel2->setText(tr("GEOM_SHAPES_ON_SHAPE_CSHAPE"));
67   GroupPoints->TextLabel3->setText(tr("GEOM_RECONSTRUCTION_LIMIT"));
68   GroupPoints->TextLabel4->setText(tr("GEOM_SHAPES_ON_SHAPE_STATE"));
69   GroupPoints->PushButton1->setIcon(image2);
70   GroupPoints->PushButton2->setIcon(image2);
71   GroupPoints->LineEdit1->setReadOnly(true);
72   GroupPoints->LineEdit2->setReadOnly(true);
73
74   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
75   layout->setMargin(0); layout->setSpacing(6);
76   layout->addWidget(GroupPoints);
77   /***************************************************************/
78
79   setHelpFileName("shapesonshape_page.html");
80
81   Init();
82 }
83
84 //=================================================================================
85 // function : ~OperationGUI_GetShapesOnShapeDlg
86 // purpose  : Destroys the object and frees any allocated resources
87 //=================================================================================
88 OperationGUI_GetShapesOnShapeDlg::~OperationGUI_GetShapesOnShapeDlg()
89 {
90 }
91
92 //=================================================================================
93 // function : Init()
94 // purpose  :
95 //=================================================================================
96 void OperationGUI_GetShapesOnShapeDlg::Init()
97 {
98   // type for sub shape selection
99   GroupPoints->ComboBox1->addItem(tr("GEOM_RECONSTRUCTION_LIMIT_SOLID"));
100   // commented by skl for IPAL19949 - finder isn't allowed such type
101   //GroupPoints->ComboBox1->addItem(tr("GEOM_RECONSTRUCTION_LIMIT_SHELL"));
102   GroupPoints->ComboBox1->addItem(tr("GEOM_RECONSTRUCTION_LIMIT_FACE"));
103   // commented by skl for IPAL19949 - finder isn't allowed such type
104   //GroupPoints->ComboBox1->addItem(tr("GEOM_RECONSTRUCTION_LIMIT_WIRE"));
105   GroupPoints->ComboBox1->addItem(tr("GEOM_RECONSTRUCTION_LIMIT_EDGE"));
106   GroupPoints->ComboBox1->addItem(tr("GEOM_RECONSTRUCTION_LIMIT_VERTEX"));
107
108   // type for state selection
109   GroupPoints->ComboBox2->addItem(tr("GEOM_STATE_IN"));
110   GroupPoints->ComboBox2->addItem(tr("GEOM_STATE_OUT"));
111   GroupPoints->ComboBox2->addItem(tr("GEOM_STATE_ON"));
112   GroupPoints->ComboBox2->addItem(tr("GEOM_STATE_ONIN"));
113   GroupPoints->ComboBox2->addItem(tr("GEOM_STATE_ONOUT"));
114
115   GroupPoints->LineEdit1->clear();
116   GroupPoints->LineEdit2->clear();
117   myObject1 = myObject2 = GEOM::GEOM_Object::_nil();
118
119   // signals and slots connections
120   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
121   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
122
123   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
124   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
125
126   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
127   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
128
129   connect(GroupPoints->ComboBox1, SIGNAL(activated(int)), this, SLOT(ComboTextChanged()));
130   connect(GroupPoints->ComboBox2, SIGNAL(activated(int)), this, SLOT(ComboTextChanged()));
131
132   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
133           this, SLOT(SelectionIntoArgument()));
134
135   initName(mainFrame()->GroupConstructors->title());
136
137   GroupPoints->TextLabel3->show();
138   GroupPoints->ComboBox1->show();
139   GroupPoints->ComboBox1->setCurrentIndex(0);
140   GroupPoints->ComboBox2->show();
141   GroupPoints->ComboBox2->setCurrentIndex(0);
142
143   globalSelection(GEOM_ALLSHAPES);
144
145   GroupPoints->PushButton1->click();
146   SelectionIntoArgument();
147 }
148
149 //=================================================================================
150 // function : ClickOnOk()
151 // purpose  :
152 //=================================================================================
153 void OperationGUI_GetShapesOnShapeDlg::ClickOnOk()
154 {
155   if (ClickOnApply())
156     ClickOnCancel();
157 }
158
159 //=================================================================================
160 // function : ClickOnApply()
161 // purpose  :
162 //=================================================================================
163 bool OperationGUI_GetShapesOnShapeDlg::ClickOnApply()
164 {
165   if (!onAccept())
166     return false;
167
168   initName();
169   // activate first line edit
170   GroupPoints->PushButton1->click();
171   return true;
172 }
173
174 //=================================================================================
175 // function : SelectionIntoArgument()
176 // purpose  : Called when selection is changed or on dialog initialization or activation
177 //=================================================================================
178 void OperationGUI_GetShapesOnShapeDlg::SelectionIntoArgument()
179 {
180   myEditCurrentArgument->setText("");
181
182   if      (myEditCurrentArgument == GroupPoints->LineEdit1) myObject1 = GEOM::GEOM_Object::_nil();
183   else if (myEditCurrentArgument == GroupPoints->LineEdit2) myObject2 = GEOM::GEOM_Object::_nil();
184
185   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
186   SALOME_ListIO aSelList;
187   aSelMgr->selectedObjects(aSelList);
188
189   QString aName = "";
190   int nbSel = GEOMBase::GetNameOfSelectedIObjects(aSelList, aName, true);
191
192   if (nbSel > 0) {
193     Standard_Boolean aRes = Standard_False;
194     GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), aRes);
195     if (!CORBA::is_nil(aSelectedObject) && aRes && GEOMBase::IsShape(aSelectedObject)) {
196       myEditCurrentArgument->setText(aName);
197
198       // clear selection
199       disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
200       myGeomGUI->getApp()->selectionMgr()->clearSelected();
201       connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
202               this, SLOT(SelectionIntoArgument()));
203
204       if (myEditCurrentArgument == GroupPoints->LineEdit1) {
205         myObject1 = aSelectedObject;
206         if (myObject2->_is_nil())
207           GroupPoints->PushButton2->click();
208       }
209       else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
210         myObject2 = aSelectedObject;
211         if (myObject1->_is_nil())
212           GroupPoints->PushButton1->click();
213       }
214     }
215   }
216 }
217
218 //=================================================================================
219 // function : SetEditCurrentArgument()
220 // purpose  :
221 //=================================================================================
222 void OperationGUI_GetShapesOnShapeDlg::SetEditCurrentArgument()
223 {
224   QPushButton* send = (QPushButton*)sender();
225
226   if      (send == GroupPoints->PushButton1) {
227     myEditCurrentArgument = GroupPoints->LineEdit1;
228
229     GroupPoints->PushButton2->setDown(false);
230     GroupPoints->LineEdit2->setEnabled(false);
231   }
232   else if (send == GroupPoints->PushButton2) {
233     myEditCurrentArgument = GroupPoints->LineEdit2;
234
235     GroupPoints->PushButton1->setDown(false);
236     GroupPoints->LineEdit1->setEnabled(false);
237   }
238
239   // enable line edit
240   myEditCurrentArgument->setEnabled(true);
241   myEditCurrentArgument->setFocus();
242   // after setFocus(), because it will be setDown(false) when loses focus
243   send->setDown(true);
244 }
245
246 //=================================================================================
247 // function : LineEditReturnPressed()
248 // purpose  :
249 //=================================================================================
250 void OperationGUI_GetShapesOnShapeDlg::LineEditReturnPressed()
251 {
252   QLineEdit* send = (QLineEdit*)sender();
253   if (send == GroupPoints->LineEdit1 || send == GroupPoints->LineEdit2)
254   {
255     myEditCurrentArgument = send;
256     GEOMBase_Skeleton::LineEditReturnPressed();
257   }
258 }
259
260 //=================================================================================
261 // function : ActivateThisDialog()
262 // purpose  :
263 //=================================================================================
264 void OperationGUI_GetShapesOnShapeDlg::ActivateThisDialog()
265 {
266   GEOMBase_Skeleton::ActivateThisDialog();
267   globalSelection(GEOM_ALLSHAPES);
268   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
269            this, SLOT( SelectionIntoArgument() ) );
270 }
271
272 //=================================================================================
273 // function : enterEvent()
274 // purpose  : when mouse enter onto the QWidget
275 //=================================================================================
276 void OperationGUI_GetShapesOnShapeDlg::enterEvent (QEvent*)
277 {
278   if (!mainFrame()->GroupConstructors->isEnabled())
279     ActivateThisDialog();
280 }
281
282 //=================================================================================
283 // function : createOperation
284 // purpose  :
285 //=================================================================================
286 GEOM::GEOM_IOperations_ptr OperationGUI_GetShapesOnShapeDlg::createOperation()
287 {
288   return getGeomEngine()->GetIShapesOperations(getStudyId());
289 }
290
291 //=================================================================================
292 // function : isValid
293 // purpose  :
294 //=================================================================================
295 bool OperationGUI_GetShapesOnShapeDlg::isValid(QString&)
296 {
297   //Handle(SALOME_InteractiveObject) IO = firstIObject();
298   //Standard_Boolean testResult;
299   //GEOM::GEOM_Object_var anObject = GEOMBase::ConvertIOinGEOMObject(IO, testResult);
300   //if (!testResult || anObject->_is_nil())
301   //  return false;
302
303   return !CORBA::is_nil(myObject1) && !CORBA::is_nil(myObject2);
304 }
305
306 //=================================================================================
307 // function : execute
308 // purpose  :
309 //=================================================================================
310 bool OperationGUI_GetShapesOnShapeDlg::execute (ObjectList& objects)
311 {
312   int aLimit = GroupPoints->ComboBox1->currentIndex();
313   switch (aLimit)
314   {
315   case 0:  aLimit = GEOM::SOLID ; break;
316   case 1:  aLimit = GEOM::FACE  ; break;
317   case 2:  aLimit = GEOM::EDGE  ; break;
318   case 3:  aLimit = GEOM::VERTEX; break;
319   default: aLimit = GEOM::SHAPE ; break;
320   }
321
322   GEOM::shape_state aState;
323   switch (GroupPoints->ComboBox2->currentIndex())
324   {
325     case 0:  aState = GEOM::ST_IN;    break;
326     case 1:  aState = GEOM::ST_OUT;   break;
327     case 2:  aState = GEOM::ST_ON;    break;
328     case 3:  aState = GEOM::ST_ONIN;  break;
329     case 4:  aState = GEOM::ST_ONOUT; break;
330     default: break;
331   }
332
333   GEOM::GEOM_Object_var anObj =
334     GEOM::GEOM_IShapesOperations::_narrow(getOperation())->
335     GetShapesOnShapeAsCompound(myObject2, myObject1,
336                                (CORBA::Short) aLimit,
337                                aState);
338
339   if (!anObj->_is_nil())
340     objects.push_back(anObj._retn());
341
342   return objects.size();
343 }
344
345 //=======================================================================
346 //function : ComboTextChanged
347 //purpose  :
348 //=======================================================================
349 void OperationGUI_GetShapesOnShapeDlg::ComboTextChanged()
350 {
351   // VRS ???? What is it for ??? commented for a while...
352   //bool IsEnabled = GroupPoints->ComboBox1->currentIndex() < 3;
353 }