Salome HOME
Update for 19962 issue: MakePipeBiNormalAlongAxis implementation.
[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
25 //  Module : GEOM
26 //  $Header$
27 //
28 #include "MeasureGUI_NormaleDlg.h"
29
30 #include "SUIT_Session.h"
31 #include "SalomeApp_Application.h"
32 #include "LightApp_SelectionMgr.h"
33
34 #include <TopoDS_Shape.hxx>
35 #include <TopoDS.hxx>
36 #include <TopExp.hxx>
37 #include <TColStd_IndexedMapOfInteger.hxx>
38 #include <TopTools_IndexedMapOfShape.hxx>
39 #include <qlabel.h>
40
41 #include "GEOMImpl_Types.hxx"
42
43 #include "utilities.h"
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                                               const char* name, bool modal, WFlags fl)
54   :GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, WStyle_Customize |
55                      WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
56 {
57   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
58   QPixmap image0 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_NORMALE")));
59   QPixmap image1 (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
60
61   setCaption(tr("GEOM_NORMALE_TITLE"));
62
63   /***************************************************************/
64   GroupConstructors->setTitle(tr("GEOM_NORMALE"));
65   RadioButton1->setPixmap(image0);
66   RadioButton2->close(TRUE);
67   RadioButton3->close(TRUE);
68
69   GroupArgs = new DlgRef_2Sel_QTD (this, "GroupArgs");
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->setPixmap(image1);
76   GroupArgs->PushButton2->setPixmap(image1);
77
78   Layout1->addWidget(GroupArgs, 2, 0);
79   /***************************************************************/
80
81   setHelpFileName("using_measurement_tools_page.html#normale_anchor");
82
83   Init();
84 }
85
86 //=================================================================================
87 // function : ~MeasureGUI_NormaleDlg()
88 // purpose  : Destroys the object and frees any allocated resources
89 //=================================================================================
90 MeasureGUI_NormaleDlg::~MeasureGUI_NormaleDlg()
91 {
92   // no need to delete child widgets, Qt does it all for us
93 }
94
95 //=================================================================================
96 // function : Init()
97 // purpose  :
98 //=================================================================================
99 void MeasureGUI_NormaleDlg::Init()
100 {
101   /* init variables */
102   GroupArgs->LineEdit1->setReadOnly(true);
103   GroupArgs->LineEdit2->setReadOnly(true);
104
105   myFace = GEOM::GEOM_Object::_nil();
106   myPoint = GEOM::GEOM_Object::_nil();
107
108   myEditCurrentArgument = GroupArgs->LineEdit1;
109   globalSelection(GEOM_FACE);
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   connect(myGeomGUI->getApp()->selectionMgr(),
122           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
123
124   initName(tr("GEOM_VECTOR_NORMALE"));
125
126   //ConstructorsClicked(0);
127   SelectionIntoArgument();
128
129   /* displays Dialog */
130   GroupArgs->show();
131   this->show();
132 }
133
134 //=================================================================================
135 // function : ClickOnOk()
136 // purpose  :
137 //=================================================================================
138 void MeasureGUI_NormaleDlg::ClickOnOk()
139 {
140   if (ClickOnApply())
141     ClickOnCancel();
142 }
143
144 //=================================================================================
145 // function : ClickOnApply()
146 // purpose  :
147 //=================================================================================
148 bool MeasureGUI_NormaleDlg::ClickOnApply()
149 {
150   if (!onAccept())
151     return false;
152
153   initName();
154   return true;
155 }
156
157 //=================================================================================
158 // function : SelectionIntoArgument()
159 // purpose  : Called when selection as changed or other case
160 //=================================================================================
161 void MeasureGUI_NormaleDlg::SelectionIntoArgument()
162 {
163   erasePreview();
164   myEditCurrentArgument->setText("");
165
166   if (myEditCurrentArgument == GroupArgs->LineEdit1) {
167     myFace = GEOM::GEOM_Object::_nil();
168   }
169   else if (myEditCurrentArgument == GroupArgs->LineEdit2) {
170     myPoint = GEOM::GEOM_Object::_nil();
171   }
172
173   if (IObjectCount() != 1)
174     return;
175
176   // nbSel == 1
177   Standard_Boolean testResult = Standard_False;
178   GEOM::GEOM_Object_var aSelectedObject =
179     GEOMBase::ConvertIOinGEOMObject(firstIObject(), testResult);
180
181   if (!testResult)
182     return;
183
184   QString aName = GEOMBase::GetName(aSelectedObject);
185
186   if (myEditCurrentArgument == GroupArgs->LineEdit1) {
187     TopoDS_Shape aShape;
188     if (GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE) && !aShape.IsNull())
189     {
190       LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
191       TColStd_IndexedMapOfInteger aMap;
192       aSelMgr->GetIndexes(firstIObject(), aMap);
193       if (aMap.Extent() == 1) // Local Selection
194       {
195         GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations(getStudyId());
196         int anIndex = aMap( 1 );
197         aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
198         aName += QString(":face_%1").arg(anIndex);
199       }
200       else // Global Selection
201       {
202         if (aShape.ShapeType() != TopAbs_FACE) {
203           aSelectedObject = GEOM::GEOM_Object::_nil();
204           aName = "";
205         }
206       }
207     }
208     myFace = aSelectedObject;
209   }
210   else if (myEditCurrentArgument == GroupArgs->LineEdit2) {
211     TopoDS_Shape aShape;
212     if (GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE) && !aShape.IsNull())
213     {
214       LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
215       TColStd_IndexedMapOfInteger aMap;
216       aSelMgr->GetIndexes(firstIObject(), aMap);
217       if (aMap.Extent() == 1) // Local Selection
218       {
219         GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations(getStudyId());
220         int anIndex = aMap( 1 );
221         aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
222         aName += QString(":vertex_%1").arg(anIndex);
223       }
224       else // Global Selection
225       {
226         if (aShape.ShapeType() != TopAbs_VERTEX) {
227           aSelectedObject = GEOM::GEOM_Object::_nil();
228           aName = "";
229         }
230       }
231     }
232     myPoint = aSelectedObject;
233   }
234
235   myEditCurrentArgument->setText( aName );
236
237   displayPreview();
238 }
239
240 //=================================================================================
241 // function : LineEditReturnPressed()
242 // purpose  :
243 //=================================================================================
244 void MeasureGUI_NormaleDlg::LineEditReturnPressed()
245 {
246   QLineEdit* send = (QLineEdit*)sender();
247   if (send == GroupArgs->LineEdit1 ||
248       send == GroupArgs->LineEdit2)
249   {
250     myEditCurrentArgument = send;
251     GEOMBase_Skeleton::LineEditReturnPressed();
252   }
253 }
254
255 //=================================================================================
256 // function : SetEditCurrentArgument()
257 // purpose  :
258 //=================================================================================
259 void MeasureGUI_NormaleDlg::SetEditCurrentArgument()
260 {
261   QPushButton* send = (QPushButton*)sender();
262   globalSelection(GEOM_FACE);
263
264   if (send == GroupArgs->PushButton1) {
265     myEditCurrentArgument = GroupArgs->LineEdit1;
266     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_FACE );
267   }
268   else if (send == GroupArgs->PushButton2) {
269     myEditCurrentArgument = GroupArgs->LineEdit2;
270     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
271   }
272
273   myEditCurrentArgument->setFocus();
274   SelectionIntoArgument();
275 }
276
277 //=================================================================================
278 // function : ActivateThisDialog()
279 // purpose  :
280 //=================================================================================
281 void MeasureGUI_NormaleDlg::ActivateThisDialog()
282 {
283   GEOMBase_Skeleton::ActivateThisDialog();
284
285   SelectionIntoArgument();
286 }
287
288 //=================================================================================
289 // function : enterEvent()
290 // purpose  :
291 //=================================================================================
292 void MeasureGUI_NormaleDlg::enterEvent (QEvent* e)
293 {
294   if (!GroupConstructors->isEnabled())
295     ActivateThisDialog();
296 }
297
298 //=================================================================================
299 // function : createOperation
300 // purpose  :
301 //=================================================================================
302 GEOM::GEOM_IOperations_ptr MeasureGUI_NormaleDlg::createOperation()
303 {
304   return getGeomEngine()->GetIMeasureOperations(getStudyId());
305 }
306
307 //=================================================================================
308 // function : isValid
309 // purpose  :
310 //=================================================================================
311 bool MeasureGUI_NormaleDlg::isValid (QString&)
312 {
313   //return !CORBA::is_nil(myFace) && !CORBA::is_nil(myPoint);
314   return !CORBA::is_nil(myFace);
315 }
316
317 //=================================================================================
318 // function : execute
319 // purpose  :
320 //=================================================================================
321 bool MeasureGUI_NormaleDlg::execute (ObjectList& objects)
322 {
323   GEOM::GEOM_Object_var anObj =
324     GEOM::GEOM_IMeasureOperations::_narrow(getOperation())->GetNormal(myFace, myPoint);
325
326   if (!anObj->_is_nil())
327     objects.push_back(anObj._retn());
328
329   return true;
330 }