Salome HOME
NRI : Merge from V1_2.
[modules/geom.git] / src / GEOMGUI / GeometryGUI_PropertiesDlg.cxx
1 using namespace std;
2 //  File      : GeometryGUI_PropertiesDlg.cxx
3 //  Created   :
4 //  Author    : Lucien PIGNOLONI
5 //  Project   : SALOME
6 //  Module    : GeometryGUI
7 //  Copyright : OPEN CASCADE
8 //  $Header$
9
10 #include "GeometryGUI_PropertiesDlg.h"
11
12 #include "GeometryGUI.h"
13 #include "QAD_Application.h"
14 #include "QAD_Desktop.h"
15 #include "utilities.h"
16
17 #include <qbuttongroup.h>
18 #include <qgroupbox.h>
19 #include <qlabel.h>
20 #include <qlineedit.h>
21 #include <qpushbutton.h>
22 #include <qradiobutton.h>
23 #include <qlayout.h>
24 #include <qvariant.h>
25 #include <qtooltip.h>
26 #include <qwhatsthis.h>
27 #include <qimage.h>
28 #include <qpixmap.h>
29
30 #include <TopExp_Explorer.hxx>
31
32
33 //=================================================================================
34 // class    : GeometryGUI_PropertiesDlg()
35 // purpose  : Constructs a GeometryGUI_PropertiesDlg which is a child of 'parent', with the 
36 //            name 'name' and widget flags set to 'f'.
37 //            The dialog will by default be modeless, unless you set 'modal' to
38 //            TRUE to construct a modal dialog.
39 //=================================================================================
40 GeometryGUI_PropertiesDlg::GeometryGUI_PropertiesDlg( QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl )
41     : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
42 {
43     QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "GEOM",tr("ICON_DLG_BASICPROPERTIES")));
44     QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap( "GEOM",tr("ICON_SELECT")));
45
46     if ( !name )
47         setName( "GeometryGUI_PropertiesDlg" );
48     resize( 303, 275 ); 
49     setCaption( tr( "GEOM_PROPERTIES_TITLE"  ) );
50     setSizeGripEnabled( TRUE );
51     GeometryGUI_PropertiesDlgLayout = new QGridLayout( this ); 
52     GeometryGUI_PropertiesDlgLayout->setSpacing( 6 );
53     GeometryGUI_PropertiesDlgLayout->setMargin( 11 );
54     
55     /***************************************************************/
56     GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
57     GroupConstructors->setTitle( tr( "GEOM_PROPERTIES"  ) );
58     GroupConstructors->setExclusive( TRUE );
59     GroupConstructors->setColumnLayout(0, Qt::Vertical );
60     GroupConstructors->layout()->setSpacing( 0 );
61     GroupConstructors->layout()->setMargin( 0 );
62     GroupConstructorsLayout = new QGridLayout( GroupConstructors->layout() );
63     GroupConstructorsLayout->setAlignment( Qt::AlignTop );
64     GroupConstructorsLayout->setSpacing( 6 );
65     GroupConstructorsLayout->setMargin( 11 );
66     Constructor1 = new QRadioButton( GroupConstructors, "Constructor1" );
67     Constructor1->setText( tr( ""  ) );
68     Constructor1->setPixmap( image0 );
69     Constructor1->setChecked( TRUE );
70     Constructor1->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0, Constructor1->sizePolicy().hasHeightForWidth() ) );
71     Constructor1->setMinimumSize( QSize( 60, 0 ) );
72     GroupConstructorsLayout->addWidget( Constructor1, 0, 0 );
73     QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
74     GroupConstructorsLayout->addItem( spacer, 0, 1 );
75     GeometryGUI_PropertiesDlgLayout->addWidget( GroupConstructors, 0, 0 );
76
77     /***************************************************************/
78     GroupConstructor1 = new QGroupBox( this, "GroupConstructor1" );
79     GroupConstructor1->setTitle( tr( "GEOM_PROPERTIES_CONSTR"  ) );
80     GroupConstructor1->setColumnLayout(0, Qt::Vertical );
81     GroupConstructor1->layout()->setSpacing( 0 );
82     GroupConstructor1->layout()->setMargin( 0 );
83     GroupConstructor1Layout = new QGridLayout( GroupConstructor1->layout() );
84     GroupConstructor1Layout->setAlignment( Qt::AlignTop );
85     GroupConstructor1Layout->setSpacing( 6 );
86     GroupConstructor1Layout->setMargin( 11 );
87     LineEditC1A1 = new QLineEdit( GroupConstructor1, "LineEditC1A1" );
88     LineEditC1A1->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0, LineEditC1A1->sizePolicy().hasHeightForWidth() ) );
89     GroupConstructor1Layout->addWidget( LineEditC1A1, 0, 2 );
90     SelectButtonC1A1 = new QPushButton( GroupConstructor1, "SelectButtonC1A1" );
91     SelectButtonC1A1->setText( tr( ""  ) );
92     SelectButtonC1A1->setPixmap( image1 );
93     GroupConstructor1Layout->addWidget( SelectButtonC1A1, 0, 1 );
94     TextLabelC1A1 = new QLabel( GroupConstructor1, "TextLabelC1A1" );
95     TextLabelC1A1->setText( tr( "GEOM_OBJECT"  ) );
96     TextLabelC1A1->setMinimumSize( QSize( 50, 0 ) );
97     TextLabelC1A1->setFrameShape( QLabel::NoFrame );
98     TextLabelC1A1->setFrameShadow( QLabel::Plain );
99     GroupConstructor1Layout->addWidget( TextLabelC1A1, 0, 0 );
100     TextLabel_Length = new QLabel( GroupConstructor1, "TextLabel_Length" );
101     TextLabel_Length->setText( tr( "GEOM_LENGTH"  ) );
102     TextLabel_Length->setMinimumSize( QSize( 50, 0 ) );
103     TextLabel_Length->setFrameShape( QLabel::NoFrame );
104     TextLabel_Length->setFrameShadow( QLabel::Plain );
105     GroupConstructor1Layout->addWidget( TextLabel_Length, 1, 0 );
106     LineEdit_Length = new QLineEdit( GroupConstructor1, "LineEdit_Length" );
107     LineEdit_Length->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0, LineEdit_Length->sizePolicy().hasHeightForWidth() ) );
108     //    LineEdit_Length->setEnabled( FALSE );
109     LineEdit_Length->setReadOnly( TRUE );
110     GroupConstructor1Layout->addWidget( LineEdit_Length, 1, 2 );
111     LineEdit_Surface = new QLineEdit( GroupConstructor1, "LineEdit_Surface" );
112     LineEdit_Surface->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0, LineEdit_Surface->sizePolicy().hasHeightForWidth() ) );
113     //LineEdit_Surface->setEnabled( FALSE );
114     LineEdit_Surface->setReadOnly( TRUE );
115     GroupConstructor1Layout->addWidget( LineEdit_Surface, 2, 2 );
116     TextLabel_Surface = new QLabel( GroupConstructor1, "TextLabel_Surface" );
117     TextLabel_Surface->setText( tr( "GEOM_PROPERTIES_SURFACE"  ) );
118     TextLabel_Surface->setMinimumSize( QSize( 50, 0 ) );
119     TextLabel_Surface->setFrameShape( QLabel::NoFrame );
120     TextLabel_Surface->setFrameShadow( QLabel::Plain );
121     GroupConstructor1Layout->addWidget( TextLabel_Surface, 2, 0 );
122     TextLabel_Volume = new QLabel( GroupConstructor1, "TextLabel_Volume" );
123     TextLabel_Volume->setText( tr( "GEOM_PROPERTIES_VOLUME"  ) );
124     TextLabel_Volume->setMinimumSize( QSize( 50, 0 ) );
125     TextLabel_Volume->setFrameShape( QLabel::NoFrame );
126     TextLabel_Volume->setFrameShadow( QLabel::Plain );
127     GroupConstructor1Layout->addWidget( TextLabel_Volume, 3, 0 );
128     LineEdit_Volume = new QLineEdit( GroupConstructor1, "LineEdit_Volume" );
129     LineEdit_Volume->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0, LineEdit_Volume->sizePolicy().hasHeightForWidth() ) );
130     //LineEdit_Volume->setEnabled( FALSE );
131     LineEdit_Volume->setReadOnly( TRUE );
132     GroupConstructor1Layout->addWidget( LineEdit_Volume, 3, 2 );
133     GeometryGUI_PropertiesDlgLayout->addWidget( GroupConstructor1, 1, 0 );
134
135     /***************************************************************/
136     GroupButtons = new QGroupBox( this, "GroupButtons" );
137     GroupButtons->setGeometry( QRect( 10, 10, 281, 48 ) ); 
138     GroupButtons->setTitle( tr( ""  ) );
139     GroupButtons->setColumnLayout(0, Qt::Vertical );
140     GroupButtons->layout()->setSpacing( 0 );
141     GroupButtons->layout()->setMargin( 0 );
142     GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
143     GroupButtonsLayout->setAlignment( Qt::AlignTop );
144     GroupButtonsLayout->setSpacing( 6 );
145     GroupButtonsLayout->setMargin( 11 );
146     buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
147     buttonCancel->setText( tr( "GEOM_BUT_CLOSE"  ) );
148     buttonCancel->setAutoDefault( TRUE );
149     GroupButtonsLayout->addWidget( buttonCancel, 0, 1 );
150  
151     QSpacerItem* spacer_8 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
152     GroupButtonsLayout->addItem( spacer_8, 0, 0 );
153     QSpacerItem* spacer_9 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
154     GroupButtonsLayout->addItem( spacer_9, 0, 2 );
155
156     GeometryGUI_PropertiesDlgLayout->addWidget( GroupButtons, 2, 0 );
157     /***************************************************************/
158
159     Init(Sel) ; /* Initialisations */
160 }
161
162
163 //=================================================================================
164 // function : ~GeometryGUI_PropertiesDlg()
165 // purpose  : Destroys the object and frees any allocated resources
166 //=================================================================================
167 GeometryGUI_PropertiesDlg::~GeometryGUI_PropertiesDlg()
168 {
169     // no need to delete child widgets, Qt does it all for us
170 }
171
172
173 //=================================================================================
174 // function : Init()
175 // purpose  :
176 //=================================================================================
177 void GeometryGUI_PropertiesDlg::Init( SALOME_Selection* Sel )
178 {
179   myConstructorId = 0 ;
180   Constructor1->setChecked( TRUE );
181   myEditCurrentArgument = LineEditC1A1 ;        
182   mySelection = Sel;
183   myGeomGUI = GeometryGUI::GetGeometryGUI() ;
184   myGeomGUI->SetActiveDialogBox( (QDialog*)this ) ;
185   
186   // TODO : previous selection into argument ?
187
188   /* Filter definitions */
189   Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "GEOM");
190   myGeom = GEOM::GEOM_Gen::_narrow(comp);
191
192   /* signals and slots connections */
193   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
194   connect( GroupConstructors, SIGNAL(clicked(int) ), SLOT( ConstructorsClicked(int) ) );
195   connect( SelectButtonC1A1, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
196   connect( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
197   connect( LineEditC1A1, SIGNAL ( returnPressed() ), this, SLOT( LineEditReturnPressed() ) ) ;
198   
199   connect( myGeomGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
200   /* to close dialog if study change */
201   connect( myGeomGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
202
203   /* Move widget on the botton right corner of main widget */
204   int x, y ;
205   myGeomGUI->DefineDlgPosition( this, x, y ) ;
206   this->move( x, y ) ;
207   this->show() ; /* displays Dialog */
208   return ;
209 }
210
211
212 //=================================================================================
213 // function : ConstructorsClicked()
214 // purpose  : Radio button management
215 //=================================================================================
216 void GeometryGUI_PropertiesDlg::ConstructorsClicked(int constructorId)
217 {
218   return ;
219 }
220
221
222
223 //=================================================================================
224 // function : ClickOnCancel()
225 // purpose  :
226 //=================================================================================
227 void GeometryGUI_PropertiesDlg::ClickOnCancel()
228 {
229   myGeomGUI->EraseSimulationShape() ;
230   disconnect( mySelection, 0, this, 0 );
231   myGeomGUI->EraseSimulationShape() ; 
232   myGeomGUI->ResetState() ;
233   reject() ;
234   return ;
235 }
236
237
238 //=================================================================================
239 // function : SelectionIntoArgument()
240 // purpose  : Called when selection as changed or other case
241 //=================================================================================
242 void GeometryGUI_PropertiesDlg::SelectionIntoArgument()
243 {
244   myGeomGUI->EraseSimulationShape() ;
245   mySimulationTopoDs.Nullify() ;
246   
247   LineEdit_Length->setText("") ;
248   LineEdit_Surface->setText("") ;
249   LineEdit_Volume->setText("") ;
250   myEditCurrentArgument->setText("") ;
251
252   QString aString = ""; /* future the name of selection */
253
254   int nbSel = myGeomGUI->GetNameOfSelectedIObjects(mySelection, aString) ;
255   if ( nbSel != 1 ) {
256     return ;
257   }
258
259   /*  nbSel == 1  */ 
260   TopoDS_Shape S;
261   if( !myGeomGUI->GetTopoFromSelection(mySelection, S) )
262     return ;
263   
264   if( S.IsNull() || S.ShapeType() == TopAbs_VERTEX ) {
265     myEditCurrentArgument->setText( "" );
266     return ;
267   }
268  
269   LineEditC1A1->setText(aString) ;
270
271   /* Try to display of a cone simulation shape to show direction of a linear edge only in OCC viewer */
272   if( myGeomGUI->CreateArrowForLinearEdge( S, mySimulationTopoDs ) ) {
273     myGeomGUI->DisplaySimulationShape( mySimulationTopoDs ) ;
274   }
275   this->CalculateAndDisplayProperties(S) ;
276   
277   return ;
278 }
279
280
281 //=================================================================================
282 // function : SetEditCurrentArgument()
283 // purpose  :
284 //=================================================================================
285 void GeometryGUI_PropertiesDlg::SetEditCurrentArgument()
286 {
287   QPushButton* send = (QPushButton*)sender();
288   switch (myConstructorId)
289     {
290     case 0: /* default constructor */
291       { 
292         if(send == SelectButtonC1A1) {
293           LineEditC1A1->setFocus() ;
294           myEditCurrentArgument = LineEditC1A1;
295         }
296         SelectionIntoArgument() ;
297         break;
298       }
299     }
300   return ;
301 }
302
303
304
305 //=================================================================================
306 // function : LineEditReturnPressed()
307 // purpose  :
308 //=================================================================================
309 void GeometryGUI_PropertiesDlg::LineEditReturnPressed()
310 {  
311   QLineEdit* send = (QLineEdit*)sender();  
312   if( send == LineEditC1A1 )
313     myEditCurrentArgument = LineEditC1A1 ;
314   else
315     return ;
316   
317   /* User name of object input management                          */
318   /* If successfull the selection is changed and signal emitted... */
319   /* so SelectionIntoArgument() is automatically called.           */
320   const QString objectUserName = myEditCurrentArgument->text() ;
321   QWidget* thisWidget = (QWidget*)this ;
322   if( myGeomGUI->SelectionByNameInDialogs( thisWidget, objectUserName, mySelection ) ) {
323     myEditCurrentArgument->setText( objectUserName ) ;
324   }
325   return ;
326 }
327
328
329 //=================================================================================
330 // function : DeactivateActiveDialog()
331 // purpose  :
332 //=================================================================================
333 void GeometryGUI_PropertiesDlg::DeactivateActiveDialog()
334 {
335   if ( GroupConstructors->isEnabled() ) {    
336     disconnect( mySelection, 0, this, 0 );
337     GroupConstructors->setEnabled(false) ;
338     GroupConstructor1->setEnabled(false) ;
339     myGeomGUI->EraseSimulationShape() ;
340     GroupButtons->setEnabled(false) ;
341   }
342   return ;
343 }
344
345
346 //=================================================================================
347 // function : ActivateThisDialog()
348 // purpose  :
349 //=================================================================================
350 void GeometryGUI_PropertiesDlg::ActivateThisDialog()
351 {
352   /* Emit a signal to deactivate the active dialog */
353   myGeomGUI->EmitSignalDeactivateDialog() ;   
354   GroupConstructors->setEnabled(true) ;
355   GroupConstructor1->setEnabled(true) ;
356   GroupButtons->setEnabled(true) ;
357   connect( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
358   
359   if( !mySimulationTopoDs.IsNull() )
360     myGeomGUI->DisplaySimulationShape( mySimulationTopoDs ) ;
361   
362   return ;
363 }
364
365
366 //=================================================================================
367 // function : enterEvent()
368 // purpose  :
369 //=================================================================================
370 void GeometryGUI_PropertiesDlg::enterEvent(QEvent* e)
371 {
372   if ( GroupConstructors->isEnabled() )
373     return ;  
374   ActivateThisDialog() ;
375   return ;
376 }
377
378
379 //=================================================================================
380 // function : closeEvent()
381 // purpose  :
382 //=================================================================================
383 void GeometryGUI_PropertiesDlg::closeEvent( QCloseEvent* e )
384 {
385   /* same than click on cancel button */
386   this->ClickOnCancel() ;
387   return ;
388 }
389
390
391 //=================================================================================
392 // function : CalculateAndDisplayProperties()
393 // purpose  :
394 //=================================================================================
395 void GeometryGUI_PropertiesDlg::CalculateAndDisplayProperties(const TopoDS_Shape& S)
396 {
397   LineEdit_Length->setText("") ;
398   LineEdit_Surface->setText("") ;
399   LineEdit_Volume->setText("") ;
400   if( S.IsNull() ) 
401     return ;
402
403   Standard_Real result;
404   GProp_GProps LProps;
405   GProp_GProps SProps;
406   QString resString;
407
408   try 
409     {
410       BRepGProp::LinearProperties(S,LProps);
411       result = LProps.Mass();
412       if (!IsEqual( result, 0.0))
413         {       
414           resString = tr("%1").arg( result, 12, 'f', 6 ) ;
415           LineEdit_Length->setText(resString) ;
416         }
417
418       BRepGProp::SurfaceProperties(S, SProps);
419       result = SProps.Mass();
420       if (!IsEqual( result, 0.0))
421         {       
422           resString = tr("%1").arg( result, 12, 'f', 6 ) ;
423           LineEdit_Surface->setText(resString) ;
424         }
425         
426       result = 0.0;
427       if (S.ShapeType() < TopAbs_SHELL)
428         {
429           for( TopExp_Explorer Exp(S,TopAbs_SOLID); Exp.More(); Exp.Next() ) 
430             { 
431               GProp_GProps VProps;
432               BRepGProp::VolumeProperties(Exp.Current(), VProps);
433               result += VProps.Mass();
434             }
435         }
436       if (!IsEqual( result, 0.0 ))
437         {       
438           resString = tr("%1").arg( result, 12, 'f', 6 ) ;
439           LineEdit_Volume->setText(resString) ;
440         }
441     }
442   catch(Standard_Failure) 
443     {
444       MESSAGE("Catch intercepted in CalculateAndDisplayProperties()" << endl ) ;
445     }
446   return ;
447 }