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