Salome HOME
0f8673eecdd6385a5ec34c0cf149d3dfd678788e
[modules/geom.git] / src / OperationGUI / OperationGUI_GetShapesOnShapeDlg.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // GEOM GEOMGUI : GUI for Geometry component
21 // File   : OperationGUI_GetShapesOnShapeDlg.cxx
22 // Author : Sergey KUUL, Open CASCADE S.A.S. (sergey.kuul@opencascade.com)
23 //
24 #include "OperationGUI_GetShapesOnShapeDlg.h"
25
26 #include <DlgRef.h>
27 #include <GeometryGUI.h>
28 #include <GEOMBase.h>
29
30 #include <SUIT_Session.h>
31 #include <SUIT_ResourceMgr.h>
32 #include <SalomeApp_Application.h>
33 #include <LightApp_SelectionMgr.h>
34
35 //==============================================================================
36 // class    : OperationGUI_GetShapesOnShapeDlg()
37 // purpose  : Constructs a OperationGUI_GetShapesOnShapeDlg which is a child of
38 //            'parent', with the name 'name' and widget flags set to 'f'.
39 //            The dialog will by default be modeless, unless you set 'modal' to
40 //            TRUE to construct a modal dialog.
41 //=================================================================================
42 OperationGUI_GetShapesOnShapeDlg::OperationGUI_GetShapesOnShapeDlg (GeometryGUI* theGeometryGUI,
43                                                                     QWidget* parent)
44   : GEOMBase_Skeleton(theGeometryGUI, parent, false)
45 {
46   SUIT_ResourceMgr* aResMgr = myGeomGUI->getApp()->resourceMgr();
47   QPixmap image0 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_SHAPES_ON_SHAPE")));
48   QPixmap image2 (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
49
50   setWindowTitle(tr("GEOM_SHAPES_ON_SHAPE_TITLE"));
51
52   /***************************************************************/
53   mainFrame()->GroupConstructors->setTitle(tr("GEOM_SHAPES_ON_SHAPE"));
54   mainFrame()->RadioButton1->setIcon(image0);
55   mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
56   mainFrame()->RadioButton2->close();
57   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
58   mainFrame()->RadioButton3->close();
59
60   GroupPoints = new DlgRef_2Sel2List(centralWidget());
61   //GroupPoints->GroupBox1->setTitle(tr("GEOM_PARTITION"));
62   GroupPoints->GroupBox1->setTitle("Input data");
63   GroupPoints->TextLabel1->setText(tr("GEOM_SHAPES_ON_SHAPE_ESHAPE"));
64   GroupPoints->TextLabel2->setText(tr("GEOM_SHAPES_ON_SHAPE_CSHAPE"));
65   GroupPoints->TextLabel3->setText(tr("GEOM_RECONSTRUCTION_LIMIT"));
66   GroupPoints->TextLabel4->setText(tr("GEOM_SHAPES_ON_SHAPE_STATE"));
67   GroupPoints->PushButton1->setIcon(image2);
68   GroupPoints->PushButton2->setIcon(image2);
69   GroupPoints->LineEdit1->setReadOnly(true);
70   GroupPoints->LineEdit2->setReadOnly(true);
71
72   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
73   layout->setMargin(0); layout->setSpacing(6);
74   layout->addWidget(GroupPoints);
75   /***************************************************************/
76
77   setHelpFileName("shapesonshape_page.html");
78
79   Init();
80 }
81
82 //=================================================================================
83 // function : ~OperationGUI_GetShapesOnShapeDlg
84 // purpose  : Destroys the object and frees any allocated resources
85 //=================================================================================
86 OperationGUI_GetShapesOnShapeDlg::~OperationGUI_GetShapesOnShapeDlg()
87 {
88 }
89
90 //=================================================================================
91 // function : Init()
92 // purpose  :
93 //=================================================================================
94 void OperationGUI_GetShapesOnShapeDlg::Init()
95 {
96   // type for sub-shape selection
97   GroupPoints->ComboBox1->addItem(tr("GEOM_RECONSTRUCTION_LIMIT_SOLID"));
98   // commented by skl for IPAL19949 - finder isn't allowed such type
99   //GroupPoints->ComboBox1->addItem(tr("GEOM_RECONSTRUCTION_LIMIT_SHELL"));
100   GroupPoints->ComboBox1->addItem(tr("GEOM_RECONSTRUCTION_LIMIT_FACE"));
101   // commented by skl for IPAL19949 - finder isn't allowed such type
102   //GroupPoints->ComboBox1->addItem(tr("GEOM_RECONSTRUCTION_LIMIT_WIRE"));
103   GroupPoints->ComboBox1->addItem(tr("GEOM_RECONSTRUCTION_LIMIT_EDGE"));
104   GroupPoints->ComboBox1->addItem(tr("GEOM_RECONSTRUCTION_LIMIT_VERTEX"));
105
106   // type for state selection
107   GroupPoints->ComboBox2->addItem(tr("GEOM_STATE_IN"));
108   GroupPoints->ComboBox2->addItem(tr("GEOM_STATE_OUT"));
109   GroupPoints->ComboBox2->addItem(tr("GEOM_STATE_ON"));
110   GroupPoints->ComboBox2->addItem(tr("GEOM_STATE_ONIN"));
111   GroupPoints->ComboBox2->addItem(tr("GEOM_STATE_ONOUT"));
112
113   GroupPoints->LineEdit1->clear();
114   GroupPoints->LineEdit2->clear();
115   myObject1 = myObject2 = GEOM::GEOM_Object::_nil();
116
117   showOnlyPreviewControl();
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   resize(100,100);
148 }
149
150 //=================================================================================
151 // function : ClickOnOk()
152 // purpose  :
153 //=================================================================================
154 void OperationGUI_GetShapesOnShapeDlg::ClickOnOk()
155 {
156   setIsApplyAndClose( true );
157   if (ClickOnApply())
158     ClickOnCancel();
159 }
160
161 //=================================================================================
162 // function : ClickOnApply()
163 // purpose  :
164 //=================================================================================
165 bool OperationGUI_GetShapesOnShapeDlg::ClickOnApply()
166 {
167   if (!onAccept())
168     return false;
169
170   initName();
171   // activate first line edit
172   GroupPoints->PushButton1->click();
173   return true;
174 }
175
176 //=================================================================================
177 // function : SelectionIntoArgument()
178 // purpose  : Called when selection is changed or on dialog initialization or activation
179 //=================================================================================
180 void OperationGUI_GetShapesOnShapeDlg::SelectionIntoArgument()
181 {
182   myEditCurrentArgument->setText("");
183
184   if      (myEditCurrentArgument == GroupPoints->LineEdit1) myObject1 = GEOM::GEOM_Object::_nil();
185   else if (myEditCurrentArgument == GroupPoints->LineEdit2) myObject2 = GEOM::GEOM_Object::_nil();
186
187   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
188   SALOME_ListIO aSelList;
189   aSelMgr->selectedObjects(aSelList);
190
191   QString aName = "";
192   int nbSel = GEOMBase::GetNameOfSelectedIObjects(aSelList, aName, true);
193
194   if (nbSel > 0) {
195     GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( aSelList.First() );
196     if ( GEOMBase::IsShape(aSelectedObject) ) {
197       myEditCurrentArgument->setText(aName);
198
199       // clear selection
200       disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
201       myGeomGUI->getApp()->selectionMgr()->clearSelected();
202       connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
203               this, SLOT(SelectionIntoArgument()));
204
205       if (myEditCurrentArgument == GroupPoints->LineEdit1) {
206         myObject1 = aSelectedObject;
207         if (myObject2->_is_nil())
208           GroupPoints->PushButton2->click();
209       }
210       else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
211         myObject2 = aSelectedObject;
212         if (myObject1->_is_nil())
213           GroupPoints->PushButton1->click();
214       }
215     }
216   }
217   processPreview();
218 }
219
220 //=================================================================================
221 // function : SetEditCurrentArgument()
222 // purpose  :
223 //=================================================================================
224 void OperationGUI_GetShapesOnShapeDlg::SetEditCurrentArgument()
225 {
226   QPushButton* send = (QPushButton*)sender();
227
228   if      (send == GroupPoints->PushButton1) {
229     myEditCurrentArgument = GroupPoints->LineEdit1;
230
231     GroupPoints->PushButton2->setDown(false);
232     GroupPoints->LineEdit2->setEnabled(false);
233   }
234   else if (send == GroupPoints->PushButton2) {
235     myEditCurrentArgument = GroupPoints->LineEdit2;
236
237     GroupPoints->PushButton1->setDown(false);
238     GroupPoints->LineEdit1->setEnabled(false);
239   }
240
241   // enable line edit
242   myEditCurrentArgument->setEnabled(true);
243   myEditCurrentArgument->setFocus();
244   // after setFocus(), because it will be setDown(false) when loses focus
245   send->setDown(true);
246 }
247
248 //=================================================================================
249 // function : LineEditReturnPressed()
250 // purpose  :
251 //=================================================================================
252 void OperationGUI_GetShapesOnShapeDlg::LineEditReturnPressed()
253 {
254   QLineEdit* send = (QLineEdit*)sender();
255   if (send == GroupPoints->LineEdit1 || send == GroupPoints->LineEdit2)
256   {
257     myEditCurrentArgument = send;
258     GEOMBase_Skeleton::LineEditReturnPressed();
259   }
260 }
261
262 //=================================================================================
263 // function : ActivateThisDialog()
264 // purpose  :
265 //=================================================================================
266 void OperationGUI_GetShapesOnShapeDlg::ActivateThisDialog()
267 {
268   GEOMBase_Skeleton::ActivateThisDialog();
269   globalSelection(GEOM_ALLSHAPES);
270   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
271            this, SLOT( SelectionIntoArgument() ) );
272   processPreview();
273 }
274
275 //=================================================================================
276 // function : enterEvent()
277 // purpose  : when mouse enter onto the QWidget
278 //=================================================================================
279 void OperationGUI_GetShapesOnShapeDlg::enterEvent (QEvent*)
280 {
281   if (!mainFrame()->GroupConstructors->isEnabled())
282     ActivateThisDialog();
283 }
284
285 //=================================================================================
286 // function : createOperation
287 // purpose  :
288 //=================================================================================
289 GEOM::GEOM_IOperations_ptr OperationGUI_GetShapesOnShapeDlg::createOperation()
290 {
291   return getGeomEngine()->GetIShapesOperations();
292 }
293
294 //=================================================================================
295 // function : isValid
296 // purpose  :
297 //=================================================================================
298 bool OperationGUI_GetShapesOnShapeDlg::isValid(QString&)
299 {
300   //Handle(SALOME_InteractiveObject) IO = firstIObject();
301   //GEOM::GEOM_Object_var anObject = GEOMBase::ConvertIOinGEOMObject( IO );
302   //if ( anObject->_is_nil() )
303   //  return false;
304
305   return !CORBA::is_nil(myObject1) && !CORBA::is_nil(myObject2);
306 }
307
308 //=================================================================================
309 // function : execute
310 // purpose  :
311 //=================================================================================
312 bool OperationGUI_GetShapesOnShapeDlg::execute (ObjectList& objects)
313 {
314   int aLimit = GroupPoints->ComboBox1->currentIndex();
315   switch (aLimit)
316   {
317   case 0:  aLimit = GEOM::SOLID ; break;
318   case 1:  aLimit = GEOM::FACE  ; break;
319   case 2:  aLimit = GEOM::EDGE  ; break;
320   case 3:  aLimit = GEOM::VERTEX; break;
321   default: aLimit = GEOM::SHAPE ; break;
322   }
323
324   GEOM::shape_state aState = GEOM::ST_IN; // todo: aState must be explicitly initialized to avoid warning (see below)
325   switch (GroupPoints->ComboBox2->currentIndex())
326   {
327     case 0:  aState = GEOM::ST_IN;    break;
328     case 1:  aState = GEOM::ST_OUT;   break;
329     case 2:  aState = GEOM::ST_ON;    break;
330     case 3:  aState = GEOM::ST_ONIN;  break;
331     case 4:  aState = GEOM::ST_ONOUT; break;
332     default: break;
333   }
334
335   GEOM::GEOM_IShapesOperations_var anOper = GEOM::GEOM_IShapesOperations::_narrow(getOperation());
336   GEOM::GEOM_Object_var anObj = anOper->GetShapesOnShapeAsCompound(myObject2, myObject1,
337                                                                    (CORBA::Short) aLimit,
338                                                                    aState); // todo: aState must be explicitly initialized to avoid warning (see above)
339
340   if (!anObj->_is_nil())
341     objects.push_back(anObj._retn());
342
343   return objects.size();
344 }
345
346 //=======================================================================
347 //function : ComboTextChanged
348 //purpose  :
349 //=======================================================================
350 void OperationGUI_GetShapesOnShapeDlg::ComboTextChanged()
351 {
352   // VRS ???? What is it for ??? commented for a while...
353   //bool IsEnabled = GroupPoints->ComboBox1->currentIndex() < 3;
354   processPreview();
355 }
356
357 //=================================================================================
358 // function : getSourceObjects
359 // purpose  : virtual method to get source objects
360 //=================================================================================
361 QList<GEOM::GeomObjPtr> OperationGUI_GetShapesOnShapeDlg::getSourceObjects()
362 {
363   QList<GEOM::GeomObjPtr> res;
364   GEOM::GeomObjPtr aGeomObjPtr1(myObject1), aGeomObjPtr2(myObject2);
365   res << aGeomObjPtr1 << aGeomObjPtr2;
366   return res;
367 }