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