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