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