Salome HOME
Copyrights update 2015.
[modules/geom.git] / src / MeasureGUI / MeasureGUI_BndBoxDlg.cxx
1 // Copyright (C) 2007-2015  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, or (at your option) any later version.
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 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : MeasureGUI_BndBoxDlg.cxx
25 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
26
27 #include "MeasureGUI_BndBoxDlg.h"
28 #include "MeasureGUI_Widgets.h"
29
30 #include "GeometryGUI.h"
31
32 #include <GEOMBase.h>
33 #include <DlgRef.h>
34
35 #include <BRepPrimAPI_MakeBox.hxx>
36
37 #include <SUIT_Session.h>
38 #include <SUIT_ResourceMgr.h>
39 #include <SUIT_OverrideCursor.h>
40 #include <SalomeApp_Application.h>
41 #include <SalomeApp_Tools.h>
42 #include <LightApp_SelectionMgr.h>
43
44 //=================================================================================
45 // class    : MeasureGUI_BndBoxDlg()
46 // purpose  : Constructs a MeasureGUI_BndBoxDlg 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_BndBoxDlg::MeasureGUI_BndBoxDlg (GeometryGUI* GUI, QWidget* parent)
52   : GEOMBase_Skeleton(GUI, parent)
53 {
54   QPixmap image0 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_BOUNDING_BOX")));
55   QPixmap image1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
56
57   setWindowTitle(tr("GEOM_BNDBOX_TITLE"));
58
59   /***************************************************************/
60
61   mainFrame()->GroupConstructors->setTitle(tr("GEOM_BNDBOX"));
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   myGrp = new MeasureGUI_1Sel6LineEdit (centralWidget());
69   myGrp->GroupBox1->setTitle( tr( "GEOM_BNDBOX_OBJDIM" ) );
70   myGrp->TextLabel1->setText( tr( "GEOM_OBJECT" ) );
71   myGrp->TextLabel2->setText( tr( "GEOM_MIN" ) );
72   myGrp->TextLabel3->setText( tr( "GEOM_MAX" ) );
73   myGrp->TextLabel4->setText( tr( "GEOM_X" ) );
74   myGrp->TextLabel5->setText( tr( "GEOM_Y" ) );
75   myGrp->TextLabel6->setText( tr( "GEOM_Z" ) );
76   myGrp->LineEdit11->setReadOnly( true );
77   myGrp->LineEdit12->setReadOnly( true );
78   myGrp->LineEdit21->setReadOnly( true );
79   myGrp->LineEdit22->setReadOnly( true );
80   myGrp->LineEdit31->setReadOnly( true );
81   myGrp->LineEdit32->setReadOnly( true );
82   myGrp->LineEdit11->setMinimumWidth(120);
83   myGrp->LineEdit12->setMinimumWidth(120);
84   myGrp->LineEdit21->setMinimumWidth(120);
85   myGrp->LineEdit22->setMinimumWidth(120);
86   myGrp->LineEdit31->setMinimumWidth(120);
87   myGrp->LineEdit32->setMinimumWidth(120);
88   myGrp->PushButton1->setIcon( image1 );
89   myGrp->LineEdit1->setReadOnly( true );
90
91   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
92   layout->setMargin( 0 ); layout->setSpacing( 6 );
93   layout->addWidget( myGrp );
94
95   /***************************************************************/
96
97   myHelpFileName = "bounding_box_page.html";
98
99   /* Initialisation */
100   Init();
101 }
102
103 //=================================================================================
104 // function : ~MeasureGUI_BndBoxDlg()
105 // purpose  : Destroys the object and frees any allocated resources
106 //=================================================================================
107 MeasureGUI_BndBoxDlg::~MeasureGUI_BndBoxDlg()
108 {
109 }
110
111 //=================================================================================
112 // function : Init()
113 // purpose  :
114 //=================================================================================
115 void MeasureGUI_BndBoxDlg::Init()
116 {
117   myEditCurrentArgument = myGrp->LineEdit1;
118
119   connect(buttonOk(),         SIGNAL(clicked()), this, SLOT(ClickOnOk()));
120   connect(buttonApply(),      SIGNAL(clicked()), this, SLOT(ClickOnApply()));
121   connect(myGrp->PushButton1, SIGNAL(clicked()), this, SLOT(SelectionIntoArgument()));
122
123   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
124           this, SLOT(SelectionIntoArgument()));
125
126   initName(tr("GEOM_BNDBOX"));
127   activateSelection();
128   SelectionIntoArgument();
129 }
130
131 //=================================================================================
132 // function : ClickOnOk()
133 // purpose  :
134 //=================================================================================
135 void MeasureGUI_BndBoxDlg::ClickOnOk()
136 {
137   if (ClickOnApply())
138     ClickOnCancel();
139 }
140
141 //=================================================================================
142 // function : ClickOnApply()
143 // purpose  :
144 //=================================================================================
145 bool MeasureGUI_BndBoxDlg::ClickOnApply()
146 {
147   if (!onAccept())
148     return false;
149
150   initName();
151   activateSelection();
152   return true;
153 }
154
155 //=================================================================================
156 // function : ActivateThisDialog()
157 // purpose  :
158 //=================================================================================
159 void MeasureGUI_BndBoxDlg::ActivateThisDialog()
160 {
161   GEOMBase_Skeleton::ActivateThisDialog();
162
163   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
164           this, SLOT(SelectionIntoArgument()));
165
166   activateSelection();
167   redisplayPreview();
168 }
169
170 //=================================================================================
171 // function : enterEvent()
172 // purpose  :
173 //=================================================================================
174 void MeasureGUI_BndBoxDlg::enterEvent(QEvent*)
175 {
176   if (!mainFrame()->GroupConstructors->isEnabled())
177     ActivateThisDialog();
178 }
179
180 //=================================================================================
181 // function : processObject
182 // purpose  :
183 //=================================================================================
184 void MeasureGUI_BndBoxDlg::processObject()
185 {
186   double aXMin, aXMax, aYMin, aYMax, aZMin, aZMax;
187
188   if (!getParameters(aXMin, aXMax, aYMin, aYMax, aZMin, aZMax)) {
189     myEditCurrentArgument->setText("");
190     myGrp->LineEdit11->setText("");
191     myGrp->LineEdit12->setText("");
192     myGrp->LineEdit21->setText("");
193     myGrp->LineEdit22->setText("");
194     myGrp->LineEdit31->setText("");
195     myGrp->LineEdit32->setText("");
196   }
197   else {
198     SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
199     int aPrecision = resMgr->integerValue( "Geometry", "length_precision", 6 );
200
201     myGrp->LineEdit11->setText( DlgRef::PrintDoubleValue( aXMin, aPrecision ) );
202     myGrp->LineEdit12->setText( DlgRef::PrintDoubleValue( aXMax, aPrecision ) );
203
204     myGrp->LineEdit21->setText( DlgRef::PrintDoubleValue( aYMin, aPrecision ) );
205     myGrp->LineEdit22->setText( DlgRef::PrintDoubleValue( aYMax, aPrecision ) );
206
207     myGrp->LineEdit31->setText( DlgRef::PrintDoubleValue( aZMin, aPrecision ) );
208     myGrp->LineEdit32->setText( DlgRef::PrintDoubleValue( aZMax, aPrecision ) );
209   }
210 }
211
212 //=================================================================================
213 // function : getParameters
214 // purpose  :
215 //=================================================================================
216 bool MeasureGUI_BndBoxDlg::getParameters (double& theXmin, double& theXmax,
217                                           double& theYmin, double& theYmax,
218                                           double& theZmin, double& theZmax)
219 {
220   if (!myObj)
221     return false;
222
223   GEOM::GEOM_IMeasureOperations_var anOper = GEOM::GEOM_IMeasureOperations::_narrow(getOperation());
224   anOper->GetBoundingBox(myObj.get(), true, theXmin, theXmax, theYmin, theYmax, theZmin, theZmax);
225
226   return anOper->IsDone();
227 }
228
229 //=================================================================================
230 // function : buildPrs
231 // purpose  :
232 //=================================================================================
233 SALOME_Prs* MeasureGUI_BndBoxDlg::buildPrs()
234 {
235   double aXMin, aYMin, aZMin, aXMax, aYMax, aZMax;
236
237   if (!getParameters(aXMin, aXMax, aYMin, aYMax, aZMin, aZMax))
238     return 0;
239
240   TopoDS_Shape aShape = BRepPrimAPI_MakeBox(gp_Pnt(aXMin, aYMin, aZMin),
241                                             gp_Pnt(aXMax, aYMax, aZMax)).Shape();
242
243   return !aShape.IsNull() ? getDisplayer()->BuildPrs(aShape) : 0;
244 }
245
246 //=================================================================================
247 // function : SelectionIntoArgument()
248 // purpose  : Called when selection as changed or other case
249 //=================================================================================
250 void MeasureGUI_BndBoxDlg::SelectionIntoArgument()
251 {
252   myEditCurrentArgument->setText("");
253   myObj.nullify();
254
255   QList<TopAbs_ShapeEnum> aTypes;
256   aTypes << TopAbs_VERTEX << TopAbs_EDGE << TopAbs_WIRE << TopAbs_FACE << TopAbs_SHELL << TopAbs_SOLID << TopAbs_COMPSOLID << TopAbs_COMPOUND << TopAbs_SHAPE;
257   myObj = getSelected( aTypes );
258
259   if ( !myObj ) {
260     processObject();
261     erasePreview();
262     return;
263   }
264
265   myEditCurrentArgument->setText(GEOMBase::GetName(myObj.get()));
266   processObject();
267   redisplayPreview();
268 }
269
270 //=================================================================================
271 // function : createOperation
272 // purpose  :
273 //=================================================================================
274 GEOM::GEOM_IOperations_ptr MeasureGUI_BndBoxDlg::createOperation()
275 {
276   return getGeomEngine()->GetIMeasureOperations(getStudyId());
277 }
278
279 //=================================================================================
280 // function : isValid
281 // purpose  :
282 //=================================================================================
283 bool MeasureGUI_BndBoxDlg::isValid (QString&)
284 {
285   return myObj;
286 }
287
288 //=================================================================================
289 // function : execute
290 // purpose  :
291 //=================================================================================
292 bool MeasureGUI_BndBoxDlg::execute (ObjectList& objects)
293 {
294   GEOM::GEOM_IMeasureOperations_var anOper = GEOM::GEOM_IMeasureOperations::_narrow(getOperation());
295   GEOM::GEOM_Object_var anObj = anOper->MakeBoundingBox(myObj.get(), true);
296
297   if (!anObj->_is_nil())
298     objects.push_back(anObj._retn());
299
300   return true;
301 }
302
303 //=================================================================================
304 // function : activateSelection()
305 // purpose  :
306 //=================================================================================
307 void MeasureGUI_BndBoxDlg::activateSelection()
308 {
309   globalSelection( GEOM_ALLSHAPES );
310   std::list<int> needTypes;
311   needTypes.push_back( TopAbs_SHAPE ), needTypes.push_back( TopAbs_VERTEX ), needTypes.push_back( TopAbs_EDGE ); 
312   needTypes.push_back( TopAbs_WIRE ), needTypes.push_back( TopAbs_FACE ), needTypes.push_back( TopAbs_SHELL );
313   needTypes.push_back( TopAbs_SOLID ), needTypes.push_back( TopAbs_COMPSOLID ), needTypes.push_back( TopAbs_COMPOUND );
314   localSelection(GEOM::GEOM_Object::_nil(), needTypes );
315 }
316
317 //=================================================================================
318 // function : redisplayPreview()
319 // purpose  :
320 //=================================================================================
321 void MeasureGUI_BndBoxDlg::redisplayPreview()
322 {
323   QString aMess;
324   if (!isValid(aMess)) {
325     erasePreview(true);
326     return;
327   }
328
329   erasePreview(false);
330
331   try {
332     SUIT_OverrideCursor();
333
334     getDisplayer()->SetColor(Quantity_NOC_VIOLET);
335     getDisplayer()->SetToActivate(false);
336     
337     if (SALOME_Prs* aPrs = buildPrs())
338       displayPreview(aPrs);
339   }
340   catch (const SALOME::SALOME_Exception& e) {
341     SalomeApp_Tools::QtCatchCorbaException(e);
342   }
343   catch (...) {
344   }
345 }
346
347 //=================================================================================
348 // function : addSubshapeToStudy
349 // purpose  : virtual method to add new SubObjects if local selection
350 //=================================================================================
351 void MeasureGUI_BndBoxDlg::addSubshapesToStudy()
352 {
353   GEOMBase::PublishSubObject( myObj.get() );
354 }