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