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