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