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