Salome HOME
a3b5804f52614d885a583162c96a77a347d16709
[modules/geom.git] / src / MeasureGUI / MeasureGUI_PropertiesDlg.cxx
1 // Copyright (C) 2007-2023  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_PropertiesDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
26 //
27 #include "MeasureGUI_PropertiesDlg.h"
28 #include "MeasureGUI_Widgets.h"
29
30 #include <GEOMImpl_Types.hxx>
31 #include <GEOMBase.h>
32 #include <DlgRef.h>
33
34 #include <SalomeApp_Tools.h>
35 #include <SUIT_Session.h>
36 #include <SUIT_ResourceMgr.h>
37
38 #include <TColStd_MapOfInteger.hxx>
39
40 #define DEFAULT_TOLERANCE_VALUE 1.e-6
41
42 //=================================================================================
43 // class    : MeasureGUI_PropertiesDlg()
44 // purpose  : Constructs a MeasureGUI_PropertiesDlg which is a child of 'parent', with the 
45 //            name 'name' and widget flags set to 'f'.
46 //            The dialog will by default be modeless, unless you set 'modal' to
47 //            true to construct a modal dialog.
48 //=================================================================================
49 MeasureGUI_PropertiesDlg::MeasureGUI_PropertiesDlg( GeometryGUI* GUI, QWidget* parent )
50   : MeasureGUI_Skeleton(GUI, parent),
51     myTolerance(0),
52     myLength(0),
53     mySurface(0),
54     myVolume(0)
55 {
56   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap(
57     "GEOM", tr( "ICON_DLG_BASICPROPERTIES" ) ) );
58   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap(
59     "GEOM", tr( "ICON_SELECT" ) ) );
60
61   setWindowTitle( tr( "GEOM_PROPERTIES_TITLE" ) );
62
63   /***************************************************************/
64   
65   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_PROPERTIES" ) );
66   mainFrame()->RadioButton1->setIcon( image0 );
67
68   QGroupBox *aGrpBox  =
69           new QGroupBox(tr("GEOM_PROPERTIES_CONSTR"), centralWidget());
70   QLabel    *anObjLbl = new QLabel(tr("GEOM_OBJECT"), aGrpBox);
71   QLabel    *aTolLbl  = new QLabel(tr("GEOM_TOLERANCE"), aGrpBox);
72   QLabel    *aLenLbl  = new QLabel(tr("GEOM_LENGTH"), aGrpBox);
73   QLabel    *aSurfLbl = new QLabel(tr("GEOM_PROPERTIES_SURFACE"), aGrpBox);
74   QLabel    *aVolLbl  = new QLabel(tr("GEOM_PROPERTIES_VOLUME"), aGrpBox);
75
76   mySelBtn    = new QPushButton(aGrpBox);
77   mySelBtn->setIcon(image1);
78   mySelEdit   = new QLineEdit(aGrpBox);
79   mySelEdit->setReadOnly(true);
80   myTolerance = new SalomeApp_DoubleSpinBox(aGrpBox);
81   myLength    = new QLineEdit(aGrpBox);
82   mySurface   = new QLineEdit(aGrpBox);
83   myVolume    = new QLineEdit(aGrpBox);
84   myLength->setReadOnly(true);
85   mySurface->setReadOnly(true);
86   myVolume->setReadOnly(true);
87
88   QGridLayout* aLayout = new QGridLayout(aGrpBox);
89
90   aLayout->setMargin(9);
91   aLayout->setSpacing(6);
92   aLayout->addWidget(anObjLbl,    0, 0);
93   aLayout->addWidget(aTolLbl,     1, 0);
94   aLayout->addWidget(aLenLbl,     2, 0);
95   aLayout->addWidget(aSurfLbl,    3, 0);
96   aLayout->addWidget(aVolLbl,     4, 0);
97   aLayout->addWidget(mySelBtn,    0, 1);
98   aLayout->addWidget(mySelEdit,   0, 2);
99   aLayout->addWidget(myTolerance, 1, 1, 1, 2);
100   aLayout->addWidget(myLength,    2, 1, 1, 2);
101   aLayout->addWidget(mySurface,   3, 1, 1, 2);
102   aLayout->addWidget(myVolume,    4, 1, 1, 2);
103
104   QVBoxLayout* aDlgLayout = new QVBoxLayout( centralWidget() );
105   aDlgLayout->setMargin( 0 ); aDlgLayout->setSpacing( 6 );
106   aDlgLayout->addWidget(aGrpBox);
107   
108   /***************************************************************/
109
110   myHelpFileName = "basic_prop_page.html";
111
112   /* Initialisation */
113   Init();
114 }
115
116
117 //=================================================================================
118 // function : ~MeasureGUI_PropertiesDlg()
119 // purpose  : Destroys the object and frees any allocated resources
120 //=================================================================================
121 MeasureGUI_PropertiesDlg::~MeasureGUI_PropertiesDlg()
122 {
123 }
124
125
126 //=================================================================================
127 // function : Init()
128 // purpose  :
129 //=================================================================================
130 void MeasureGUI_PropertiesDlg::Init()
131 {
132   mySelEdit->setMinimumSize(100, 0);
133
134   // Obtain precision from preferences
135   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
136   int aPrecision = resMgr->integerValue( "Geometry", "length_precision", 6 );
137
138   myTolerance->setPrecision(aPrecision);
139   // it's necessary to set decimals before the range setting,
140   // by default Qt rounds boundaries to 2 decimals at setRange
141   myTolerance->setDecimals(qAbs(aPrecision));
142   myTolerance->setRange(0., 1.);
143   myTolerance->setSingleStep(DEFAULT_TOLERANCE_VALUE);
144   myTolerance->setValue(DEFAULT_TOLERANCE_VALUE);
145
146   MeasureGUI_Skeleton::Init();
147
148   connect(myTolerance, SIGNAL(valueChanged(double)),
149           this, SLOT(toleranceChanged(double)));
150 }
151
152 //=================================================================================
153 // function : activateSelection
154 // purpose  :
155 //=================================================================================
156 void MeasureGUI_PropertiesDlg::activateSelection()
157 {
158   static TColStd_MapOfInteger aTypes;
159   if ( aTypes.IsEmpty() ) {
160     aTypes.Add( GEOM_COMPOUND );
161     //    aTypes.Add( TopAbs_COMPSOLID );
162     aTypes.Add( GEOM_SOLID );
163     aTypes.Add( GEOM_SHELL );
164     aTypes.Add( GEOM_FACE );
165     aTypes.Add( GEOM_WIRE );
166     aTypes.Add( GEOM_EDGE );
167   }
168
169   globalSelection( aTypes );
170
171   std::list<int> needTypes;
172   needTypes.push_back( TopAbs_EDGE ), needTypes.push_back( TopAbs_WIRE ),
173     needTypes.push_back( TopAbs_FACE ), needTypes.push_back( TopAbs_SHELL ),
174     needTypes.push_back( TopAbs_SOLID ), needTypes.push_back( TopAbs_COMPOUND );
175   localSelection( needTypes );
176 }
177
178 void MeasureGUI_PropertiesDlg::SelectionIntoArgument()
179 {
180   myObj.nullify();
181   QList<TopAbs_ShapeEnum> aTypes;
182   aTypes << TopAbs_EDGE << TopAbs_WIRE << TopAbs_FACE << TopAbs_SHELL
183          << TopAbs_SOLID << TopAbs_COMPSOLID << TopAbs_COMPOUND;
184   myObj = getSelected( aTypes );
185  
186   if (!myObj) {
187     mySelEdit->setText("");
188     processObject();
189     erasePreview();
190     return;
191   }
192
193   mySelEdit->setText(GEOMBase::GetName(myObj.get()));
194   processObject();
195   redisplayPreview();
196 }
197
198 //=================================================================================
199 // function : isValid
200 // purpose  :
201 //=================================================================================
202 bool MeasureGUI_PropertiesDlg::isValid(QString& msg)
203 {
204   return myTolerance->isValid(msg) && MeasureGUI_Skeleton::isValid(msg);
205 }
206
207 //=================================================================================
208 // function : processObject
209 // purpose  :
210 //=================================================================================
211 void MeasureGUI_PropertiesDlg::processObject()
212 {
213   double aLength, anArea, aVolume;
214   
215   if ( !getParameters( aLength, anArea, aVolume ) ) {
216     mySelEdit->setText( "" );
217     myLength->setText( "" );
218     mySurface->setText( "" );
219     myVolume->setText( "" );
220   }
221   else {
222     SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
223     int aPrecision = resMgr->integerValue( "Geometry", "length_precision", 6 );
224     myLength->setText( DlgRef::PrintDoubleValue( aLength, aPrecision ) );
225     mySurface->setText( DlgRef::PrintDoubleValue( anArea,  aPrecision ) );
226     myVolume->setText( DlgRef::PrintDoubleValue( aVolume, aPrecision ) );
227   }
228 }
229
230 //=================================================================================
231 // function : getParameters
232 // purpose  :
233 //=================================================================================
234 bool MeasureGUI_PropertiesDlg::getParameters( double& theLength,
235                                               double& theArea,
236                                               double& theVolume )
237 {
238   if ( !myObj )
239     return false;
240   else {
241     GEOM::GEOM_IMeasureOperations_var anOper = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() );
242     try {
243       anOper->GetBasicProperties( myObj.get(), myTolerance->value(), theLength, theArea, theVolume );
244     }
245     catch( const SALOME::SALOME_Exception& e ) {
246       SalomeApp_Tools::QtCatchCorbaException( e );
247       return false;
248     }
249
250     return anOper->IsDone();
251   }
252 }
253
254 //=================================================================================
255 // function : createOperation
256 // purpose  :
257 //=================================================================================
258 SALOME_Prs* MeasureGUI_PropertiesDlg::buildPrs()
259 {
260   SALOME_Prs* prs = 0;
261   TopoDS_Shape shape;
262
263   if ( GEOMBase::GetShape( myObj.get(), shape, TopAbs_EDGE ) &&
264        getDisplayer()->IsDisplayed( GEOMBase::GetEntry(myObj.get()) ) ) {
265     shape = GEOMBase::CreateArrowForLinearEdge( shape );
266     if ( !shape.IsNull() )
267       prs = getDisplayer()->BuildPrs( shape );
268   }
269   return prs;
270 }
271
272 //=================================================================================
273 // function : toleranceChanged
274 // purpose  :
275 //=================================================================================
276 void MeasureGUI_PropertiesDlg::toleranceChanged(double)
277 {
278   processObject();
279 }