Salome HOME
DCQ : Merge with Ecole_Ete_a6.
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_AddSubMeshDlg.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_AddSubMeshDlg.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SMESH
27 //  $Header$
28
29 using namespace std;
30 #include "SMESHGUI_AddSubMeshDlg.h"
31 #include "SMESHGUI.h"
32 #include "SALOME_ListIteratorOfListIO.hxx"
33
34 #include "QAD_Application.h"
35 #include "QAD_Desktop.h"
36 #include "QAD_MessageBox.h"
37 #include "QAD_WaitCursor.h"
38 #include "QAD_Operation.h"
39
40 #include "utilities.h"
41
42 // QT Includes
43 #include <qgroupbox.h>
44 #include <qlabel.h>
45 #include <qlineedit.h>
46 #include <qpushbutton.h>
47 #include <qlayout.h>
48 #include <qpixmap.h>
49
50 //=================================================================================
51 // class    : SMESHGUI_AddSubMeshDlg()
52 // purpose  : Constructs a SMESHGUI_AddSubMeshDlg which is a child of 'parent', with the 
53 //            name 'name' and widget flags set to 'f'.
54 //            The dialog will by default be modeless, unless you set 'modal' to
55 //            TRUE to construct a modal dialog.
56 //=================================================================================
57 SMESHGUI_AddSubMeshDlg::SMESHGUI_AddSubMeshDlg( QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl )
58     : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose )
59 {
60     QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH",tr("ICON_SELECT")));
61     if ( !name )
62         setName( "SMESHGUI_AddSubMeshDlg" );
63     setCaption( tr( "SMESH_ADD_SUBMESH"  ) );
64     setSizeGripEnabled( TRUE );
65     QGridLayout* SMESHGUI_AddSubMeshDlgLayout = new QGridLayout( this ); 
66     SMESHGUI_AddSubMeshDlgLayout->setSpacing( 6 );
67     SMESHGUI_AddSubMeshDlgLayout->setMargin( 11 );
68     
69     /***************************************************************/
70     GroupC1 = new QGroupBox( tr( "SMESH_ARGUMENTS" ), this, "GroupC1" );
71     GroupC1->setColumnLayout(0, Qt::Vertical );
72     GroupC1->layout()->setSpacing( 0 );
73     GroupC1->layout()->setMargin( 0 );
74     QGridLayout* GroupC1Layout = new QGridLayout( GroupC1->layout() );
75     GroupC1Layout->setAlignment( Qt::AlignTop );
76     GroupC1Layout->setSpacing( 6 );
77     GroupC1Layout->setMargin( 11 );
78
79     TextLabel_NameMesh = new QLabel(  tr( "SMESH_NAME" ), GroupC1, "TextLabel_NameMesh" );
80     GroupC1Layout->addWidget( TextLabel_NameMesh, 0, 0 );
81     LineEdit_NameMesh = new QLineEdit( GroupC1, "LineEdit_NameMesh" );
82     GroupC1Layout->addWidget( LineEdit_NameMesh, 0, 2 );
83
84     TextLabelC1A1 = new QLabel( tr( "SMESH_OBJECT_MESH" ), GroupC1, "TextLabelC1A1" );
85     GroupC1Layout->addWidget( TextLabelC1A1, 1, 0 );
86     SelectButtonC1A1 = new QPushButton( GroupC1, "SelectButtonC1A1" );
87     SelectButtonC1A1->setPixmap( image0 );
88     SelectButtonC1A1->setToggleButton( FALSE );
89     GroupC1Layout->addWidget( SelectButtonC1A1, 1, 1 );
90     LineEditC1A1 = new QLineEdit( GroupC1, "LineEditC1A1" );
91     GroupC1Layout->addWidget( LineEditC1A1, 1, 2 );
92
93     TextLabelC1A2 = new QLabel( tr( "SMESH_OBJECT_GEOM" ), GroupC1, "TextLabelC1A2" );
94     GroupC1Layout->addWidget( TextLabelC1A2, 2, 0 );
95     SelectButtonC1A2 = new QPushButton( GroupC1, "SelectButtonC1A2" );
96     SelectButtonC1A2->setPixmap( image0 );
97     SelectButtonC1A2->setToggleButton( FALSE );
98     GroupC1Layout->addWidget( SelectButtonC1A2, 2, 1 );
99     LineEditC1A2 = new QLineEdit( GroupC1, "LineEditC1A2" );
100     GroupC1Layout->addWidget( LineEditC1A2, 2, 2 );
101
102     TextLabelC1A1Hyp = new QLabel( tr( "SMESH_OBJECT_HYPOTHESIS" ), GroupC1, "TextLabelC1A1Hyp" );
103     GroupC1Layout->addWidget( TextLabelC1A1Hyp, 3, 0 );
104     SelectButtonC1A1Hyp = new QPushButton( GroupC1, "SelectButtonC1A1Hyp" );
105     SelectButtonC1A1Hyp->setPixmap( image0 );
106     GroupC1Layout->addWidget( SelectButtonC1A1Hyp, 3, 1 );
107     LineEditC1A1Hyp = new QLineEdit( GroupC1, "LineEditC1A1Hyp" );
108     GroupC1Layout->addWidget( LineEditC1A1Hyp, 3, 2 );
109
110     TextLabelC1A1Algo = new QLabel( tr( "SMESH_OBJECT_ALGORITHM" ), GroupC1, "TextLabelC1A1Algo" );
111     GroupC1Layout->addWidget( TextLabelC1A1Algo, 4, 0 );
112     SelectButtonC1A1Algo = new QPushButton( GroupC1, "SelectButtonC1A1Algo" );
113     SelectButtonC1A1Algo->setPixmap( image0 );
114     GroupC1Layout->addWidget( SelectButtonC1A1Algo, 4, 1 );
115     LineEditC1A1Algo = new QLineEdit( GroupC1, "LineEditC1A1Algo" );
116     GroupC1Layout->addWidget( LineEditC1A1Algo, 4, 2 );
117
118     SMESHGUI_AddSubMeshDlgLayout->addWidget( GroupC1, 1, 0 );
119
120     /***************************************************************/
121     GroupButtons = new QGroupBox( this, "GroupButtons" );
122     GroupButtons->setColumnLayout(0, Qt::Vertical );
123     GroupButtons->layout()->setSpacing( 0 );
124     GroupButtons->layout()->setMargin( 0 );
125     QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
126     GroupButtonsLayout->setAlignment( Qt::AlignTop );
127     GroupButtonsLayout->setSpacing( 6 );
128     GroupButtonsLayout->setMargin( 11 );
129
130     buttonOk = new QPushButton( tr( "SMESH_BUT_OK" ), GroupButtons, "buttonOk" );
131     buttonOk->setAutoDefault( TRUE );
132     buttonOk->setDefault( TRUE );
133     GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
134
135     buttonApply = new QPushButton( tr( "SMESH_BUT_APPLY" ), GroupButtons, "buttonApply" );
136     buttonApply->setAutoDefault( TRUE );
137     GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
138
139     GroupButtonsLayout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 2 );
140
141     buttonCancel = new QPushButton( tr( "SMESH_BUT_CLOSE" ), GroupButtons, "buttonCancel" );
142     buttonCancel->setAutoDefault( TRUE );
143     GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
144
145     SMESHGUI_AddSubMeshDlgLayout->addWidget( GroupButtons, 2, 0 );
146
147     /***************************************************************/
148     Init( Sel ) ;
149 }
150
151
152 //=================================================================================
153 // function : ~SMESHGUI_AddSubMeshDlg()
154 // purpose  : Destroys the object and frees any allocated resources
155 //=================================================================================
156 SMESHGUI_AddSubMeshDlg::~SMESHGUI_AddSubMeshDlg()
157 {
158     // no need to delete child widgets, Qt does it all for us
159 }
160
161
162 //=================================================================================
163 // function : Init()
164 // purpose  :
165 //=================================================================================
166 void SMESHGUI_AddSubMeshDlg::Init( SALOME_Selection* Sel )
167 {
168   mySelection = Sel;
169   mySMESHGUI = SMESHGUI::GetSMESHGUI() ;
170   mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
171
172   myGeomFilter       = new SALOME_TypeFilter( "GEOM" );
173   myMeshFilter       = new SMESH_TypeFilter( MESH );
174   myAlgorithmFilter  = new SMESH_TypeFilter( ALGORITHM );
175   myHypothesisFilter = new SMESH_TypeFilter( HYPOTHESIS );
176
177   /* signals and slots connections */
178   connect( buttonOk,     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
179   connect( buttonApply,  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
180   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
181
182   connect( SelectButtonC1A1,     SIGNAL (clicked() ),  this, SLOT( SetEditCurrentArgument() ) ) ;
183   connect( SelectButtonC1A2,     SIGNAL (clicked() ),  this, SLOT( SetEditCurrentArgument() ) ) ;
184   connect( SelectButtonC1A1Hyp,  SIGNAL (clicked() ),  this, SLOT( SetEditCurrentArgument() ) ) ;
185   connect( SelectButtonC1A1Algo, SIGNAL (clicked() ),  this, SLOT( SetEditCurrentArgument() ) ) ;
186
187   connect( mySelection, SIGNAL( currentSelectionChanged() ),      this, SLOT( SelectionIntoArgument() ) );
188   connect( mySMESHGUI,  SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
189   connect( mySMESHGUI,  SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) ) ;
190   
191   int x, y ;
192   mySMESHGUI->DefineDlgPosition( this, x, y ) ;
193   this->move( x, y ) ;
194   this->show() ; 
195
196   LineEdit_NameMesh->setText( tr( "SMESH_SUBMESH" ) );
197   LineEdit_NameMesh->setFocus() ;
198   myEditCurrentArgument = LineEditC1A1 ;        
199   mySelection->ClearFilters() ;   
200   mySelection->AddFilter( myMeshFilter ) ;
201
202   SelectionIntoArgument();
203 }
204
205 //=================================================================================
206 // function : ClickOnOk()
207 // purpose  :
208 //=================================================================================
209 void SMESHGUI_AddSubMeshDlg::ClickOnOk()
210 {
211   if ( this->ClickOnApply() )
212     this->ClickOnCancel() ;
213 }
214
215 //=================================================================================
216 // function : ClickOnApply()
217 // purpose  :
218 //=================================================================================
219 bool SMESHGUI_AddSubMeshDlg::ClickOnApply()
220 {
221   QString myNameSubMesh = LineEdit_NameMesh->text().stripWhiteSpace();
222   if ( myNameSubMesh.isEmpty() ) {
223     QAD_MessageBox::warn1( this, tr( "SMESH_WRN_WARNING" ), tr( "SMESH_WRN_EMPTY_NAME" ), tr( "SMESH_BUT_OK" ) );
224     return false;
225   }
226   
227   if ( myMesh->_is_nil() || myGeomShape->_is_nil() || ( !HypoList.count() && !AlgoList.count() ) )
228     return false;
229
230   SALOMEDS::SObject_var aMeshSO = mySMESHGUI->GetStudyAPI().FindObject( myMesh );
231   GEOM::GEOM_Shape_var myMainShape = mySMESHGUI->GetStudyAPI().GetShapeOnMeshOrSubMesh( aMeshSO );
232   if ( myMainShape->_is_nil() )
233     return false;
234
235   QAD_WaitCursor wc;
236
237   QAD_Operation* op = new QAD_Operation( mySMESHGUI->GetActiveStudy() );
238
239   // start transaction
240   op->start();
241   
242   // create submesh
243   SMESH::SMESH_subMesh_var aSubMesh = mySMESHGUI->AddSubMesh( myMesh, myGeomShape, myNameSubMesh ) ;
244   
245   if ( !aSubMesh->_is_nil() ) {
246     // assign hypotheses
247     for( int i = 0; i < HypoList.count(); i++ ) {
248       SALOMEDS::SObject_var aHypSO = mySMESHGUI->GetStudy()->FindObjectID( HypoList[i] );
249       if ( !aHypSO->_is_nil() ) {
250         CORBA::Object_var anObject = aHypSO->GetObject();
251         if ( !CORBA::is_nil( anObject ) ) {
252           SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
253           if ( !aHyp->_is_nil() )
254             if ( !mySMESHGUI->AddHypothesisOnSubMesh( aSubMesh, aHyp ) ) {
255               // abort transaction
256               op->abort();
257               return false;
258           }
259         }
260       }
261     }
262     // assign algorithms
263     for( int i = 0; i < AlgoList.count(); i++ ) {
264       SALOMEDS::SObject_var aHypSO = mySMESHGUI->GetStudy()->FindObjectID( AlgoList[i] );
265       if ( !aHypSO->_is_nil() ) {
266         CORBA::Object_var anObject = aHypSO->GetObject();
267         if ( !CORBA::is_nil( anObject ) ) {
268           SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
269           if ( !aHyp->_is_nil() )
270             if ( !mySMESHGUI->AddAlgorithmOnSubMesh( aSubMesh, aHyp ) ) {
271               // abort transaction
272               op->abort();
273               return false;
274           }
275         }
276       }
277     }
278   }
279   // commit transaction
280   op->finish();
281   return true;
282 }
283
284
285 //=================================================================================
286 // function : ClickOnCancel()
287 // purpose  :
288 //=================================================================================
289 void SMESHGUI_AddSubMeshDlg::ClickOnCancel()
290 {
291   close();
292 }
293
294 static bool IsFatherOf( SALOMEDS::SObject_ptr SO, SALOMEDS::SObject_ptr fatherSO ) {
295   if ( !SO->_is_nil() && !fatherSO->_is_nil() ) {
296     SALOMEDS::SObject_var aSO = SO->GetFather();
297     while( strlen( aSO->GetID() ) >= strlen( fatherSO->GetID() ) ) {
298       if ( QString( aSO->GetID() ) == QString( fatherSO->GetID() ) )
299         return true;
300       aSO = aSO->GetFather();
301     }
302   }
303   return false;
304 }
305
306 //=================================================================================
307 // function : SelectionIntoArgument()
308 // purpose  : Called when selection as changed or other case
309 //=================================================================================
310 void SMESHGUI_AddSubMeshDlg::SelectionIntoArgument()
311 {
312   SMESHGUI_StudyAPI myStudyAPI = mySMESHGUI->GetStudyAPI();
313   QString aString = ""; 
314
315   int nbSel = mySMESHGUI->GetNameOfSelectedIObjects(mySelection, aString) ;
316
317   if ( myEditCurrentArgument == LineEditC1A1 ) {
318     // mesh
319     if ( nbSel != 1 ) {
320       myMesh = SMESH::SMESH_Mesh::_nil();
321       aString = "";
322     } 
323     else {
324       Standard_Boolean testResult ;
325       Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
326       myMesh = mySMESHGUI->ConvertIOinMesh(IO, testResult) ;
327       if( !testResult ) {
328         myMesh = SMESH::SMESH_Mesh::_nil();
329         aString = "";
330       }
331     }
332     myGeomShape = GEOM::GEOM_Shape::_nil();
333     LineEditC1A2->setText( "" );
334   }
335   else if ( myEditCurrentArgument == LineEditC1A2 ) {
336     // geom shape
337     if ( nbSel != 1 ) {
338       myGeomShape = GEOM::GEOM_Shape::_nil();
339       aString = "";
340     } 
341     else {
342       Standard_Boolean testResult ;
343       Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
344       myGeomShape = mySMESHGUI->ConvertIOinGEOMShape(IO, testResult) ;
345       if( !testResult ) {
346         myGeomShape = GEOM::GEOM_Shape::_nil();
347         aString = "";
348       }
349       if ( !myMesh->_is_nil() ) {
350         SALOMEDS::SObject_var aMeshSO = myStudyAPI.FindObject( myMesh );
351         GEOM::GEOM_Shape_var aMainGeomShape = myStudyAPI.GetShapeOnMeshOrSubMesh( aMeshSO );
352         SALOMEDS::SObject_var aMainGeomShapeSO = myStudyAPI.FindObject( aMainGeomShape );
353         if ( aMainGeomShapeSO->_is_nil() || !IsFatherOf(  mySMESHGUI->GetStudy()->FindObjectID( IO->getEntry() ), aMainGeomShapeSO ) ) {
354           myGeomShape = GEOM::GEOM_Shape::_nil();
355           aString = "";
356         }         
357       }
358     }
359   } 
360   else if ( myEditCurrentArgument == LineEditC1A1Hyp ) {
361     // hypotheses
362     HypoList.clear();
363     if ( nbSel >= 1 ) {
364       SALOME_ListIteratorOfListIO Itinit( mySelection->StoredIObjects() );
365       for ( ; Itinit.More(); Itinit.Next() ) {
366         HypoList.append( Itinit.Value()->getEntry() );
367       }
368       if ( nbSel > 1 )
369         aString = tr( "%1 Hypothesis" ).arg( nbSel ) ;
370     }
371     else {
372       aString = "";
373     }
374   }
375   else if ( myEditCurrentArgument == LineEditC1A1Algo ) {
376     // algorithms
377     AlgoList.clear(); 
378     if ( nbSel >= 1 ) {
379       SALOME_ListIteratorOfListIO Itinit( mySelection->StoredIObjects() );
380       for ( ; Itinit.More(); Itinit.Next() ) {
381         AlgoList.append( Itinit.Value()->getEntry() );
382       }
383       if ( nbSel > 1 )
384         aString = tr( "%1 Algorithms" ).arg( nbSel ) ;
385     }
386     else {
387       aString = "";
388     }
389   }
390   
391   myEditCurrentArgument->setText(aString) ;
392
393   UpdateControlState();
394 }
395
396
397 //=================================================================================
398 // function : SetEditCurrentArgument()
399 // purpose  :
400 //=================================================================================
401 void SMESHGUI_AddSubMeshDlg::SetEditCurrentArgument()
402 {
403   QPushButton* send = (QPushButton*)sender();
404   if(send == SelectButtonC1A1) {
405     LineEditC1A1->setFocus() ;
406     myEditCurrentArgument = LineEditC1A1;
407     mySelection->ClearFilters() ;         
408     mySelection->AddFilter(myMeshFilter) ;
409   } else if (send == SelectButtonC1A2) {
410     LineEditC1A2->setFocus() ;
411     myEditCurrentArgument = LineEditC1A2;
412     mySelection->ClearFilters() ;
413     mySelection->AddFilter(myGeomFilter) ;
414   } else if( send == SelectButtonC1A1Hyp ) {
415     LineEditC1A1Hyp->setFocus() ;
416     myEditCurrentArgument = LineEditC1A1Hyp ;
417     mySelection->ClearFilters() ;         
418     mySelection->AddFilter(myHypothesisFilter) ;
419   } else if( send == SelectButtonC1A1Algo ) {
420     LineEditC1A1Algo->setFocus() ;
421     myEditCurrentArgument = LineEditC1A1Algo ;
422     mySelection->ClearFilters() ;         
423     mySelection->AddFilter(myAlgorithmFilter) ;
424   }
425   SelectionIntoArgument() ;
426 }
427
428 //=================================================================================
429 // function : DeactivateActiveDialog()
430 // purpose  :
431 //=================================================================================
432 void SMESHGUI_AddSubMeshDlg::DeactivateActiveDialog()
433 {
434   if ( GroupC1->isEnabled() ) {
435     disconnect( mySelection, 0, this, 0 );
436     GroupC1->setEnabled(false) ;
437     GroupButtons->setEnabled(false) ;
438   }
439 }
440
441
442 //=================================================================================
443 // function : ActivateThisDialog()
444 // purpose  :
445 //=================================================================================
446 void SMESHGUI_AddSubMeshDlg::ActivateThisDialog()
447 {
448   mySMESHGUI->EmitSignalDeactivateDialog() ;   
449   GroupC1->setEnabled(true) ;
450   GroupButtons->setEnabled(true) ;
451   connect ( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
452 }
453
454
455 //=================================================================================
456 // function : enterEvent()
457 // purpose  :
458 //=================================================================================
459 void SMESHGUI_AddSubMeshDlg::enterEvent(QEvent* e)
460 {
461   if ( !GroupC1->isEnabled() )
462     ActivateThisDialog() ;
463 }
464
465
466 //=================================================================================
467 // function : closeEvent()
468 // purpose  :
469 //=================================================================================
470 void SMESHGUI_AddSubMeshDlg::closeEvent( QCloseEvent* e )
471 {
472   disconnect( mySelection, 0, this, 0 );
473   mySMESHGUI->ResetState() ;
474   mySelection->ClearFilters() ;
475   QDialog::closeEvent( e );
476 }
477
478 //=================================================================================
479 // function : UpdateControlState()
480 // purpose  :
481 //=================================================================================
482 void SMESHGUI_AddSubMeshDlg::UpdateControlState()
483 {
484   bool isEnabled = ( !myMesh->_is_nil() && !myGeomShape->_is_nil() && ( HypoList.count() || AlgoList.count() ) );
485   bool isImportedMesh = false;
486   if ( !myMesh->_is_nil() ) {
487     SALOMEDS::SObject_var aMeshSO = mySMESHGUI->GetStudyAPI().FindObject( myMesh );
488     GEOM::GEOM_Shape_var myGeomShape = mySMESHGUI->GetStudyAPI().GetShapeOnMeshOrSubMesh( aMeshSO );
489     isImportedMesh = myGeomShape->_is_nil();
490   }
491
492   buttonOk   ->setEnabled( isEnabled && !isImportedMesh );
493   buttonApply->setEnabled( isEnabled && !isImportedMesh );
494 }
495
496
497
498
499