Salome HOME
updated copyright message
[modules/geom.git] / src / MeasureGUI / MeasureGUI_NormaleDlg.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   : MeasureGUI_NormaleDlg.cxx
22 // Author : Julia DOROVSKIKH, Open CASCADE S.A.S.
23 //
24 #include "MeasureGUI.h"
25 #include "MeasureGUI_NormaleDlg.h"
26
27 #include <DlgRef.h>
28 #include <GEOMBase.h>
29 #include <GeometryGUI.h>
30
31 #include <SUIT_Session.h>
32 #include <SUIT_ResourceMgr.h>
33 #include <SalomeApp_Application.h>
34 #include <LightApp_SelectionMgr.h>
35
36 // OCCT Includes
37 #include <TopoDS_Shape.hxx>
38 #include <TopoDS.hxx>
39 #include <TopExp.hxx>
40 #include <TColStd_IndexedMapOfInteger.hxx>
41 #include <TopTools_IndexedMapOfShape.hxx>
42
43 #include <GEOMImpl_Types.hxx>
44
45 //=================================================================================
46 // class    : MeasureGUI_NormaleDlg()
47 // purpose  : Constructs a MeasureGUI_NormaleDlg which is a child of 'parent', with the
48 //            name 'name' and widget flags set to 'f'.
49 //            The dialog will by default be modeless, unless you set 'modal' to
50 //            TRUE to construct a modal dialog.
51 //=================================================================================
52 MeasureGUI_NormaleDlg::MeasureGUI_NormaleDlg (GeometryGUI* theGeometryGUI, QWidget* parent)
53   : GEOMBase_Skeleton(theGeometryGUI, parent, false)
54 {
55   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
56   QPixmap image0 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_NORMALE")));
57   QPixmap image1 (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
58
59   setWindowTitle(tr("GEOM_NORMALE_TITLE"));
60
61   /***************************************************************/
62   mainFrame()->GroupConstructors->setTitle(tr("GEOM_NORMALE"));
63   mainFrame()->RadioButton1->setIcon(image0);
64   mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
65   mainFrame()->RadioButton2->close();
66   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
67   mainFrame()->RadioButton3->close();
68
69   GroupArgs = new DlgRef_2Sel (centralWidget());
70   GroupArgs->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
71
72   GroupArgs->TextLabel1->setText(tr("GEOM_FACE"));
73   GroupArgs->TextLabel2->setText(tr("GEOM_POINT"));
74
75   GroupArgs->PushButton1->setIcon(image1);
76   GroupArgs->PushButton2->setIcon(image1);
77
78   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
79   layout->setMargin(0); layout->setSpacing(6);
80   layout->addWidget(GroupArgs);
81   /***************************************************************/
82
83   setHelpFileName("normal_page.html");
84
85   Init();
86 }
87
88 //=================================================================================
89 // function : ~MeasureGUI_NormaleDlg()
90 // purpose  : Destroys the object and frees any allocated resources
91 //=================================================================================
92 MeasureGUI_NormaleDlg::~MeasureGUI_NormaleDlg()
93 {
94 }
95
96 //=================================================================================
97 // function : Init()
98 // purpose  :
99 //=================================================================================
100 void MeasureGUI_NormaleDlg::Init()
101 {
102   // init variables
103   GroupArgs->LineEdit1->setReadOnly(true);
104   GroupArgs->LineEdit2->setReadOnly(true);
105
106   GroupArgs->LineEdit1->setText("");
107   GroupArgs->LineEdit2->setText("");
108   myFace = myPoint = GEOM::GEOM_Object::_nil();
109
110   // signals and slots connections
111   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
112   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
113
114   connect(GroupArgs->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
115   connect(GroupArgs->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
116
117   connect(GroupArgs->LineEdit1,   SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
118   connect(GroupArgs->LineEdit2,   SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
119
120   initName(tr("GEOM_VECTOR_NORMALE"));
121
122   GroupArgs->PushButton1->click();
123   SelectionIntoArgument();
124 }
125
126 //=================================================================================
127 // function : ClickOnOk()
128 // purpose  :
129 //=================================================================================
130 void MeasureGUI_NormaleDlg::ClickOnOk()
131 {
132   if (ClickOnApply())
133     ClickOnCancel();
134 }
135
136 //=================================================================================
137 // function : ClickOnApply()
138 // purpose  :
139 //=================================================================================
140 bool MeasureGUI_NormaleDlg::ClickOnApply()
141 {
142   if (!onAccept())
143     return false;
144
145   initName();
146   // activate first line edit
147   GroupArgs->PushButton1->click();
148   return true;
149 }
150
151 //=================================================================================
152 // function : SelectionIntoArgument()
153 // purpose  : Called when selection is changed or on dialog initialization or activation
154 //=================================================================================
155 void MeasureGUI_NormaleDlg::SelectionIntoArgument()
156 {
157   erasePreview();
158   myEditCurrentArgument->setText("");
159
160   if (myEditCurrentArgument == GroupArgs->LineEdit1) {
161     myFace = GEOM::GEOM_Object::_nil();
162   }
163   else if (myEditCurrentArgument == GroupArgs->LineEdit2) {
164     myPoint = GEOM::GEOM_Object::_nil();
165   }
166
167   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
168   SALOME_ListIO aSelList;
169   aSelMgr->selectedObjects(aSelList);
170
171   if (aSelList.Extent() != 1)
172     return;
173
174   // nbSel == 1
175   GEOM::GEOM_Object_var aSelectedObject =
176     GEOMBase::ConvertIOinGEOMObject( aSelList.First() );
177
178   if ( CORBA::is_nil(aSelectedObject) )
179     return;
180
181   QString aName = GEOMBase::GetName(aSelectedObject);
182
183   if (myEditCurrentArgument == GroupArgs->LineEdit1) {
184     TopoDS_Shape aShape;
185     if (GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE) && !aShape.IsNull())
186     {
187       TColStd_IndexedMapOfInteger aMap;
188       aSelMgr->GetIndexes(aSelList.First(), aMap);
189       if (aMap.Extent() == 1) // Local Selection
190       {
191         GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations();
192         int anIndex = aMap(1);
193         aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
194         aName += QString(":face_%1").arg(anIndex);
195       }
196       else // Global Selection
197       {
198         if (aShape.ShapeType() != TopAbs_FACE) {
199           aSelectedObject = GEOM::GEOM_Object::_nil();
200           aName = "";
201         }
202       }
203     }
204     myFace = aSelectedObject;
205     myEditCurrentArgument->setText(aName);
206
207     // clear selection
208     disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
209     myGeomGUI->getApp()->selectionMgr()->clearSelected();
210     connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
211             this, SLOT(SelectionIntoArgument()));
212
213     if (!myFace->_is_nil() && myPoint->_is_nil())
214       GroupArgs->PushButton2->click();
215   }
216   else if (myEditCurrentArgument == GroupArgs->LineEdit2) {
217     TopoDS_Shape aShape;
218     if (GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE) && !aShape.IsNull())
219     {
220       TColStd_IndexedMapOfInteger aMap;
221       aSelMgr->GetIndexes(aSelList.First(), aMap);
222       if (aMap.Extent() == 1) // Local Selection
223       {
224         GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations();
225         int anIndex = aMap(1);
226         aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
227         aName += QString(":vertex_%1").arg(anIndex);
228       }
229       else // Global Selection
230       {
231         if (aShape.ShapeType() != TopAbs_VERTEX) {
232           aSelectedObject = GEOM::GEOM_Object::_nil();
233           aName = "";
234         }
235       }
236     }
237     myPoint = aSelectedObject;
238     myEditCurrentArgument->setText(aName);
239
240     // clear selection
241     disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
242     myGeomGUI->getApp()->selectionMgr()->clearSelected();
243     connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
244             this, SLOT(SelectionIntoArgument()));
245
246     if (!myPoint->_is_nil() && myFace->_is_nil())
247       GroupArgs->PushButton1->click();
248   }
249
250   DISPLAY_PREVIEW_MACRO
251 }
252
253 //=================================================================================
254 // function : SetEditCurrentArgument()
255 // purpose  :
256 //=================================================================================
257 void MeasureGUI_NormaleDlg::SetEditCurrentArgument()
258 {
259   QPushButton* send = (QPushButton*)sender();
260
261   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
262   globalSelection(GEOM_FACE);
263
264   if (send == GroupArgs->PushButton1) {
265     myEditCurrentArgument = GroupArgs->LineEdit1;
266
267     GroupArgs->PushButton2->setDown(false);
268     GroupArgs->LineEdit2->setEnabled(false);
269
270     localSelection(TopAbs_FACE);
271   }
272   else if (send == GroupArgs->PushButton2) {
273     myEditCurrentArgument = GroupArgs->LineEdit2;
274
275     GroupArgs->PushButton1->setDown(false);
276     GroupArgs->LineEdit1->setEnabled(false);
277
278     localSelection(TopAbs_VERTEX);
279   }
280   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
281           this, SLOT(SelectionIntoArgument()));
282
283   // enable line edit
284   myEditCurrentArgument->setEnabled(true);
285   myEditCurrentArgument->setFocus();
286   // after setFocus(), because it will be setDown(false) when loses focus
287   send->setDown(true);
288
289   // seems we need it only to avoid preview disappearing, caused by selection mode change
290   DISPLAY_PREVIEW_MACRO
291 }
292
293 //=================================================================================
294 // function : LineEditReturnPressed()
295 // purpose  :
296 //=================================================================================
297 void MeasureGUI_NormaleDlg::LineEditReturnPressed()
298 {
299   QLineEdit* send = (QLineEdit*)sender();
300   if (send == GroupArgs->LineEdit1 ||
301       send == GroupArgs->LineEdit2) {
302     myEditCurrentArgument = send;
303     GEOMBase_Skeleton::LineEditReturnPressed();
304   }
305 }
306
307 //=================================================================================
308 // function : ActivateThisDialog()
309 // purpose  :
310 //=================================================================================
311 void MeasureGUI_NormaleDlg::ActivateThisDialog()
312 {
313   GEOMBase_Skeleton::ActivateThisDialog();
314   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
315            this, SLOT( SelectionIntoArgument() ) );
316
317   DISPLAY_PREVIEW_MACRO
318 }
319
320 //=================================================================================
321 // function : enterEvent()
322 // purpose  :
323 //=================================================================================
324 void MeasureGUI_NormaleDlg::enterEvent (QEvent*)
325 {
326   if (!mainFrame()->GroupConstructors->isEnabled())
327     ActivateThisDialog();
328 }
329
330 //=================================================================================
331 // function : createOperation
332 // purpose  :
333 //=================================================================================
334 GEOM::GEOM_IOperations_ptr MeasureGUI_NormaleDlg::createOperation()
335 {
336   return getGeomEngine()->GetIMeasureOperations();
337 }
338
339 //=================================================================================
340 // function : isValid
341 // purpose  :
342 //=================================================================================
343 bool MeasureGUI_NormaleDlg::isValid (QString&)
344 {
345   //return !CORBA::is_nil(myFace) && !CORBA::is_nil(myPoint);
346   return !CORBA::is_nil(myFace);
347 }
348
349 //=================================================================================
350 // function : execute
351 // purpose  :
352 //=================================================================================
353 bool MeasureGUI_NormaleDlg::execute (ObjectList& objects)
354 {
355   GEOM::GEOM_IMeasureOperations_var anOper = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() );
356   GEOM::GEOM_Object_var anObj = anOper->GetNormal(myFace, myPoint);
357
358   if (!anObj->_is_nil())
359     objects.push_back(anObj._retn());
360
361   return true;
362 }
363
364 //=================================================================================
365 // function : getSourceObjects
366 // purpose  : virtual method to get source objects
367 //=================================================================================
368 QList<GEOM::GeomObjPtr> MeasureGUI_NormaleDlg::getSourceObjects()
369 {
370   QList<GEOM::GeomObjPtr> res;
371   GEOM::GeomObjPtr aGeomObjPtr1(myFace), aGeomObjPtr2(myPoint);
372   res << aGeomObjPtr1 << aGeomObjPtr2;
373   return res;
374 }