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