Salome HOME
Remove OCC_LIBS from LDFLAGS
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_AddAlgorithmDlg.cxx
1 //  SMESH SMESHGUI : GUI for SMESH 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   : SMESHGUI_AddAlgorithmDlg.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SMESH
27 //  $Header$
28
29 using namespace std;
30 #include "SMESHGUI_AddAlgorithmDlg.h"
31 #include "SALOME_ListIteratorOfListIO.hxx"
32
33 #include "SMESHGUI.h"
34 #include "QAD_Application.h"
35 #include "QAD_Desktop.h"
36 #include "utilities.h"
37
38 // QT Includes
39 #include <qbuttongroup.h>
40 #include <qframe.h>
41 #include <qgroupbox.h>
42 #include <qlabel.h>
43 #include <qlineedit.h>
44 #include <qpushbutton.h>
45 #include <qradiobutton.h>
46 #include <qlayout.h>
47 #include <qvariant.h>
48 #include <qtooltip.h>
49 #include <qwhatsthis.h>
50 #include <qimage.h>
51 #include <qpixmap.h>
52 #include <qevent.h>
53
54
55 //=================================================================================
56 // class    : SMESHGUI_AddAlgorithmDlg()
57 // purpose  : Constructs a SMESHGUI_AddAlgorithmDlg which is a child of 'parent', with the 
58 //            name 'name' and widget flags set to 'f'.
59 //            The dialog will by default be modeless, unless you set 'modal' to
60 //            TRUE to construct a modal dialog.
61 //=================================================================================
62 SMESHGUI_AddAlgorithmDlg::SMESHGUI_AddAlgorithmDlg( QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl )
63     : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
64 {
65      QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH",tr("ICON_DLG_ADD_ALGORITHM")));
66      QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH",tr("ICON_SELECT")));
67
68     if ( !name )
69         setName( "SMESHGUI_AddAlgorithmDlg" );
70     resize( 322, 220 ); 
71     setCaption( tr( "SMESH_ADD_ALGORITHM_TITLE" ) );
72     setSizeGripEnabled( TRUE );
73
74     SMESHGUI_AddAlgorithmDlgLayout = new QGridLayout( this ); 
75     SMESHGUI_AddAlgorithmDlgLayout->setSpacing( 6 );
76     SMESHGUI_AddAlgorithmDlgLayout->setMargin( 11 );
77
78     /***************************************************************/
79     GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
80     GroupConstructors->setTitle( tr( "SMESH_ADD_ALGORITHM"  ) );
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     QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
90     GroupConstructorsLayout->addItem( spacer, 0, 1 );
91     Constructor1 = new QRadioButton( GroupConstructors, "Constructor1" );
92     Constructor1->setText( tr( ""  ) );
93     Constructor1->setPixmap( image0 );
94     Constructor1->setChecked( TRUE );
95     Constructor1->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0, Constructor1->sizePolicy().hasHeightForWidth() ) );
96     GroupConstructorsLayout->addWidget( Constructor1, 0, 0 );
97     SMESHGUI_AddAlgorithmDlgLayout->addWidget( GroupConstructors, 0, 0 );
98
99     /***************************************************************/
100     GroupConstructor1 = new QGroupBox( this, "GroupConstructor1" );
101     GroupConstructor1->setTitle( tr( "SMESH_ARGUMENTS"  ) );
102     GroupConstructor1->setColumnLayout(0, Qt::Vertical );
103     GroupConstructor1->layout()->setSpacing( 0 );
104     GroupConstructor1->layout()->setMargin( 0 );
105     GroupConstructor1Layout = new QGridLayout( GroupConstructor1->layout() );
106     GroupConstructor1Layout->setAlignment( Qt::AlignTop );
107     GroupConstructor1Layout->setSpacing( 6 );
108     GroupConstructor1Layout->setMargin( 11 );
109     LineEditC1A2Mesh = new QLineEdit( GroupConstructor1, "LineEditC1A2Mesh" );
110     LineEditC1A2Mesh->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0, LineEditC1A2Mesh->sizePolicy().hasHeightForWidth() ) );
111     GroupConstructor1Layout->addWidget( LineEditC1A2Mesh, 1, 2 );
112     LineEditC1A1Hyp = new QLineEdit( GroupConstructor1, "LineEditC1A1Hyp" );
113     LineEditC1A1Hyp->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0, LineEditC1A1Hyp->sizePolicy().hasHeightForWidth() ) );
114     GroupConstructor1Layout->addWidget( LineEditC1A1Hyp, 0, 2 );
115     SelectButtonC1A1Hyp = new QPushButton( GroupConstructor1, "SelectButtonC1A1Hyp" );
116     SelectButtonC1A1Hyp->setText( tr( ""  ) );
117     SelectButtonC1A1Hyp->setPixmap( image1 );
118     GroupConstructor1Layout->addWidget( SelectButtonC1A1Hyp, 0, 1 );
119     SelectButtonC1A2Mesh = new QPushButton( GroupConstructor1, "SelectButtonC1A2Mesh" );
120     SelectButtonC1A2Mesh->setText( tr( ""  ) );
121     SelectButtonC1A2Mesh->setPixmap( image1 );
122     GroupConstructor1Layout->addWidget( SelectButtonC1A2Mesh, 1, 1 );
123     TextLabelC1A2Mesh = new QLabel( GroupConstructor1, "TextLabelC1A2Mesh" );
124     TextLabelC1A2Mesh->setText( tr( "SMESH_OBJECT_MESHorSUBMESH" ) );
125     TextLabelC1A2Mesh->setMinimumSize( QSize( 50, 0 ) );
126     TextLabelC1A2Mesh->setFrameShape( QLabel::NoFrame );
127     TextLabelC1A2Mesh->setFrameShadow( QLabel::Plain );
128     GroupConstructor1Layout->addWidget( TextLabelC1A2Mesh, 1, 0 );
129     TextLabelC1A1Hyp = new QLabel( GroupConstructor1, "TextLabelC1A1Hyp" );
130     TextLabelC1A1Hyp->setText( tr( "SMESH_OBJECT_ALGORITHM" ) );
131     TextLabelC1A1Hyp->setMinimumSize( QSize( 50, 0 ) );
132     TextLabelC1A1Hyp->setFrameShape( QLabel::NoFrame );
133     TextLabelC1A1Hyp->setFrameShadow( QLabel::Plain );
134     GroupConstructor1Layout->addWidget( TextLabelC1A1Hyp, 0, 0 );
135     SMESHGUI_AddAlgorithmDlgLayout->addWidget( GroupConstructor1, 1, 0 );
136
137     /***************************************************************/
138     GroupButtons = new QGroupBox( this, "GroupButtons" );
139     GroupButtons->setGeometry( QRect( 10, 10, 281, 48 ) ); 
140     GroupButtons->setTitle( tr( ""  ) );
141     GroupButtons->setColumnLayout(0, Qt::Vertical );
142     GroupButtons->layout()->setSpacing( 0 );
143     GroupButtons->layout()->setMargin( 0 );
144     GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
145     GroupButtonsLayout->setAlignment( Qt::AlignTop );
146     GroupButtonsLayout->setSpacing( 6 );
147     GroupButtonsLayout->setMargin( 11 );
148     buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
149     buttonCancel->setText( tr( "SMESH_BUT_CLOSE"  ) );
150     buttonCancel->setAutoDefault( TRUE );
151     GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
152     buttonApply = new QPushButton( GroupButtons, "buttonApply" );
153     buttonApply->setText( tr( "SMESH_BUT_APPLY"  ) );
154     buttonApply->setAutoDefault( TRUE );
155     GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
156     QSpacerItem* spacer_1 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
157     GroupButtonsLayout->addItem( spacer_1, 0, 2 );
158     buttonOk = new QPushButton( GroupButtons, "buttonOk" );
159     buttonOk->setText( tr( "SMESH_BUT_OK"  ) );
160     buttonOk->setAutoDefault( TRUE );
161     buttonOk->setDefault( TRUE );
162     GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
163     SMESHGUI_AddAlgorithmDlgLayout->addWidget( GroupButtons, 2, 0 ); 
164
165     /* Initialisation */
166     Init( Sel ) ; 
167 }
168
169
170 //=================================================================================
171 // function : ~SMESHGUI_AddAlgorithmDlg()
172 // purpose  : Destroys the object and frees any allocated resources
173 //=================================================================================
174 SMESHGUI_AddAlgorithmDlg::~SMESHGUI_AddAlgorithmDlg()
175 {  
176   /* no need to delete child widgets, Qt does it all for us */
177   this->destroy(TRUE, TRUE) ;
178 }
179
180
181
182 //=================================================================================
183 // function : Init()
184 // purpose  :
185 //=================================================================================
186 void SMESHGUI_AddAlgorithmDlg::Init( SALOME_Selection* Sel )
187 {  
188   mySelection = Sel ;
189   myConstructorId = 0 ;
190   
191   mySMESHGUI = SMESHGUI::GetSMESHGUI() ;
192
193   GroupConstructor1->show();
194   myConstructorId = 0 ;
195   myEditCurrentArgument = LineEditC1A1Hyp ;     
196   Constructor1->setChecked( TRUE );
197   myOkMesh = myOkSubMesh = myOkAlgorithm = false ;
198
199   myAlgorithmFilter = new SMESH_TypeFilter( ALGORITHM );
200   myMeshOrSubMeshFilter = new SMESH_TypeFilter( MESHorSUBMESH );
201
202   mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
203
204   /* signals and slots connections */
205   connect( buttonOk, SIGNAL( clicked() ),     this, SLOT( ClickOnOk() ) );
206   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
207   connect( buttonApply, SIGNAL( clicked() ), this, SLOT(ClickOnApply() ) );
208   connect( GroupConstructors, SIGNAL(clicked(int) ), SLOT( ConstructorsClicked(int) ) );
209   connect( SelectButtonC1A1Hyp, SIGNAL (clicked() ),  this, SLOT( SetEditCurrentArgument() ) ) ;
210   connect( SelectButtonC1A2Mesh, SIGNAL (clicked() ),  this, SLOT( SetEditCurrentArgument() ) ) ;
211   connect( mySelection, SIGNAL( currentSelectionChanged() ),     this, SLOT( SelectionIntoArgument() ) );
212   connect( mySMESHGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
213   /* to close dialog if study change */
214   connect( mySMESHGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
215  
216   /* Move widget on the botton right corner of main widget */
217   int x, y ;
218   mySMESHGUI->DefineDlgPosition( this, x, y ) ;
219   this->move( x, y ) ;
220   this->show() ; /* Displays Dialog */ 
221
222   return ;
223 }
224
225
226
227 //=================================================================================
228 // function : ConstructorsClicked()
229 // purpose  : Radio button management
230 //=================================================================================
231 void SMESHGUI_AddAlgorithmDlg::ConstructorsClicked(int constructorId)
232 {
233   switch (constructorId)
234     {
235     case 0:
236       {
237         GroupConstructor1->show();
238         myConstructorId = constructorId ;
239         myEditCurrentArgument = LineEditC1A1Hyp ;
240         LineEditC1A2Mesh->setText(tr("")) ;
241         Constructor1->setChecked( TRUE );
242         myOkMesh = myOkSubMesh = myOkAlgorithm  = false ;
243         break;
244       }
245     }
246  return ;
247 }
248
249 //=================================================================================
250 // function : ClickOnOk()
251 // purpose  :
252 //=================================================================================
253 void SMESHGUI_AddAlgorithmDlg::ClickOnOk()
254 {
255   this->ClickOnApply() ;
256   this->ClickOnCancel() ;
257
258   return ;
259 }
260
261 //=================================================================================
262 // function : ClickOnApply()
263 // purpose  :
264 //=================================================================================
265 void SMESHGUI_AddAlgorithmDlg::ClickOnApply()
266 {
267   switch(myConstructorId)
268     { 
269     case 0 :
270       {
271         if(myOkMesh && myOkAlgorithm ) {
272           SALOME_ListIteratorOfListIO It( AlgoList );
273           for(;It.More();It.Next()) {
274             Handle(SALOME_InteractiveObject) IObject = It.Value();
275             Standard_Boolean testResult;
276             myAlgorithm = mySMESHGUI->ConvertIOinSMESHHypothesis(IObject, testResult) ;
277             if( testResult )
278               mySMESHGUI->AddAlgorithmOnMesh(myMesh, myAlgorithm) ;
279           }
280         } else if ( myOkSubMesh && myOkAlgorithm ) {
281           SALOME_ListIteratorOfListIO It( AlgoList );
282           for(;It.More();It.Next()) {
283             Handle(SALOME_InteractiveObject) IObject = It.Value();
284             Standard_Boolean testResult;
285             myAlgorithm = mySMESHGUI->ConvertIOinSMESHHypothesis(IObject, testResult) ;
286             if( testResult )
287               mySMESHGUI->AddAlgorithmOnSubMesh(mySubMesh, myAlgorithm) ;
288           }
289         }
290         break ;
291       }
292     }
293 }
294
295 //=================================================================================
296 // function : ClickOnCancel()
297 // purpose  :
298 //=================================================================================
299 void SMESHGUI_AddAlgorithmDlg::ClickOnCancel()
300 {
301   mySelection->ClearFilters() ;
302   disconnect( mySelection, 0, this, 0 );
303   mySMESHGUI->ResetState() ;
304   reject() ;
305   return ;
306 }
307
308
309
310 //=================================================================================
311 // function : SelectionIntoArgument()
312 // purpose  : Called when selection has changed
313 //=================================================================================
314 void SMESHGUI_AddAlgorithmDlg::SelectionIntoArgument()
315 {
316   myEditCurrentArgument->setText("") ;
317   QString aString = "";
318
319   int nbSel = mySMESHGUI->GetNameOfSelectedIObjects(mySelection, aString) ;  
320   
321   if ( myEditCurrentArgument == LineEditC1A2Mesh ) {
322     if ( nbSel == 1 ) {
323       Handle(SALOME_InteractiveObject) IObject = mySelection->firstIObject();
324       QString aName = IObject->getName();
325       Standard_Boolean testResult;
326       myOkMesh = true ;
327       myMesh = mySMESHGUI->ConvertIOinMesh(IObject, testResult) ;
328       if( !testResult ) {
329         myOkMesh = false;
330         myOkSubMesh = true;
331         mySubMesh = mySMESHGUI->ConvertIOinSubMesh(IObject, testResult) ;
332         if( !testResult ) {
333           myOkSubMesh = false;
334           return ;
335         }
336       }
337       LineEditC1A2Mesh->setText(aString) ;
338     }
339     else {
340       myOkMesh = myOkSubMesh = false ;
341       return ;
342     }
343   }
344   else if ( myEditCurrentArgument == LineEditC1A1Hyp ) {
345     if ( nbSel >= 1 ) {
346       AlgoList.Clear(); // = mySelection->StoredIObjects() ;
347       SALOME_ListIteratorOfListIO Itinit( mySelection->StoredIObjects() );
348       for (; Itinit.More(); Itinit.Next()) {
349         AlgoList.Append(Itinit.Value());
350       }
351       myOkAlgorithm = true ;
352       if (nbSel > 1)
353         aString = tr("%1 Algorithms").arg(nbSel) ;
354       LineEditC1A1Hyp->setText(aString) ;
355     }
356     else {
357       myOkAlgorithm = false ;
358       return ;
359     }
360   }
361   
362   return ; 
363 }
364
365
366 //=================================================================================
367 // function : SetEditCurrentArgument()
368 // purpose  :
369 //=================================================================================
370 void SMESHGUI_AddAlgorithmDlg::SetEditCurrentArgument()
371 {
372   QPushButton* send = (QPushButton*)sender();
373
374   switch (myConstructorId)
375     {
376     case 0: /* default constructor */
377       { 
378         if( send == SelectButtonC1A1Hyp ) {
379           LineEditC1A1Hyp->setFocus() ;
380           myEditCurrentArgument = LineEditC1A1Hyp ;
381           mySelection->ClearFilters() ;   
382           mySelection->AddFilter(myAlgorithmFilter) ;
383         }
384         else if(send == SelectButtonC1A2Mesh) {
385           LineEditC1A2Mesh->setFocus() ;
386           myEditCurrentArgument = LineEditC1A2Mesh;
387           mySelection->ClearFilters() ;
388           mySelection->AddFilter(myMeshOrSubMeshFilter) ;
389         }
390         SelectionIntoArgument() ;
391         break;
392       }
393     }
394   return ;
395 }
396
397
398
399 //=================================================================================
400 // function : LineEditReturnPressed()
401 // purpose  :
402 //=================================================================================
403 void SMESHGUI_AddAlgorithmDlg::LineEditReturnPressed()
404 {
405
406   return ;
407 }
408
409
410
411 //=================================================================================
412 // function : DeactivateActiveDialog()
413 // purpose  :
414 //=================================================================================
415 void SMESHGUI_AddAlgorithmDlg::DeactivateActiveDialog()
416 {
417   if ( GroupConstructors->isEnabled() ) {
418
419     GroupConstructors->setEnabled(false) ;
420     GroupConstructor1->setEnabled(false) ;
421     GroupButtons->setEnabled(false) ;  
422     disconnect( mySelection, 0, this, 0 );
423   }
424   return ;
425 }
426
427
428
429 //=================================================================================
430 // function : closeEvent()
431 // purpose  :
432 //=================================================================================
433 void SMESHGUI_AddAlgorithmDlg::closeEvent( QCloseEvent* e )
434 {
435   this->ClickOnCancel() ; /* same than click on cancel button */
436 }
437
438
439 //=================================================================================
440 // function : enterEvent()
441 // purpose  : when mouse enter onto the QWidget
442 //=================================================================================
443 void SMESHGUI_AddAlgorithmDlg::enterEvent( QEvent *  )
444 {
445   if ( GroupConstructors->isEnabled() )
446     return ;
447   ActivateThisDialog() ;
448 }
449
450
451
452 //=================================================================================
453 // function : ActivateThisDialog()
454 // purpose  :
455 //=================================================================================
456 void SMESHGUI_AddAlgorithmDlg::ActivateThisDialog()
457 {
458   /* Emit a signal to deactivate any active dialog */
459   mySMESHGUI->EmitSignalDeactivateDialog() ;
460   GroupConstructors->setEnabled(true) ;
461   GroupConstructor1->setEnabled(true) ;
462   GroupButtons->setEnabled(true) ;
463   
464   connect ( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
465   return ;
466 }