Salome HOME
DCQ : Merge with Ecole_Ete_a6.
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_EditHypothesesDlg.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_EditHypothesesDlg.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SMESH
27 //  $Header$
28
29 using namespace std;
30 #include "SMESHGUI_EditHypothesesDlg.h"
31 #include "SMESHGUI.h"
32 #include "SALOME_ListIteratorOfListIO.hxx"
33
34 #include "QAD_Application.h"
35 #include "QAD_Desktop.h"
36 #include "QAD_WaitCursor.h"
37 #include "QAD_Operation.h"
38
39 #include "utilities.h"
40
41 // QT Includes
42 #include <qgroupbox.h>
43 #include <qlabel.h>
44 #include <qlineedit.h>
45 #include <qpushbutton.h>
46 #include <qlayout.h>
47 #include <qpixmap.h>
48
49 //VRV: porting on Qt 3.0.5
50 #if QT_VERSION >= 0x030005
51 #include <qlistbox.h>
52 #endif
53 //VRV: porting on Qt 3.0.5
54
55 class ListBoxIOR : public QListBoxText
56 {
57 public:
58   enum { RTTI_IOR = 1000 };
59
60 public:
61   ListBoxIOR( QListBox* listbox, 
62               const char* ior,
63               const QString& text = QString::null)
64   : QListBoxText( listbox, text ), myIOR( ior ) {}
65   virtual ~ListBoxIOR() {};
66   virtual int rtti() const { return RTTI_IOR; }
67   const char* GetIOR() { return myIOR.c_str(); }
68
69 private:
70   string myIOR;
71 };
72
73 #define ALLOW_CHANGE_SHAPE 0
74
75 int findItem( QListBox* listBox, const string& ior )
76 {
77   for ( int i = 0; i < listBox->count(); i++ ) {
78     if ( listBox->item( i )->rtti() == ListBoxIOR::RTTI_IOR ) {
79       ListBoxIOR* anItem = ( ListBoxIOR* )( listBox->item( i ) );
80       if ( anItem && ior == string( anItem->GetIOR() ) )
81         return i;
82     }
83   }
84   return -1;
85 }
86
87 //=================================================================================
88 // class    : SMESHGUI_EditHypothesesDlg()
89 // purpose  : Constructs a SMESHGUI_EditHypothesesDlg which is a child of 'parent', with the 
90 //            name 'name' and widget flags set to 'f'.
91 //            The dialog will by default be modeless, unless you set 'modal' to
92 //            TRUE to construct a modal dialog.
93 //=================================================================================
94 SMESHGUI_EditHypothesesDlg::SMESHGUI_EditHypothesesDlg( QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl )
95   : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose ),
96     myImportedMesh( false )
97 {
98     QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH",tr("ICON_SELECT")));
99     if ( !name )
100         setName( "SMESHGUI_EditHypothesesDlg" );
101     setCaption( tr( "SMESH_EDIT_HYPOTHESES"  ) );
102     setSizeGripEnabled( TRUE );
103     QGridLayout* SMESHGUI_EditHypothesesDlgLayout = new QGridLayout( this ); 
104     SMESHGUI_EditHypothesesDlgLayout->setSpacing( 6 );
105     SMESHGUI_EditHypothesesDlgLayout->setMargin( 11 );
106
107     /***************************************************************/
108     GroupC1 = new QGroupBox( tr( "SMESH_ARGUMENTS" ), this, "GroupC1" );
109     GroupC1->setColumnLayout(0, Qt::Vertical );
110     GroupC1->layout()->setSpacing( 0 );
111     GroupC1->layout()->setMargin( 0 );
112     QGridLayout* GroupC1Layout = new QGridLayout( GroupC1->layout() );
113     GroupC1Layout->setAlignment( Qt::AlignTop );
114     GroupC1Layout->setSpacing( 6 );
115     GroupC1Layout->setMargin( 11 );
116
117     TextLabelC1A1 = new QLabel( tr( "SMESH_OBJECT_MESHorSUBMESH" ), GroupC1, "TextLabelC1A1" );
118     GroupC1Layout->addWidget( TextLabelC1A1, 0, 0 );
119     SelectButtonC1A1 = new QPushButton( GroupC1, "SelectButtonC1A1" );
120     SelectButtonC1A1->setPixmap( image0 );
121     GroupC1Layout->addWidget( SelectButtonC1A1, 0, 1 );
122     LineEditC1A1 = new QLineEdit( GroupC1, "LineEditC1A1" );
123     GroupC1Layout->addWidget( LineEditC1A1, 0, 2 );
124
125     TextLabelC1A2 = new QLabel( tr( "SMESH_OBJECT_GEOM" ), GroupC1, "TextLabelC1A2" );
126     GroupC1Layout->addWidget( TextLabelC1A2, 1, 0 );
127     SelectButtonC1A2 = new QPushButton( GroupC1, "SelectButtonC1A2" );
128     SelectButtonC1A2->setPixmap( image0 );
129     SelectButtonC1A2->setToggleButton( FALSE );
130     GroupC1Layout->addWidget( SelectButtonC1A2, 1, 1 );
131     LineEditC1A2 = new QLineEdit( GroupC1, "LineEditC1A2" );
132     GroupC1Layout->addWidget( LineEditC1A2, 1, 2 );
133
134     SMESHGUI_EditHypothesesDlgLayout->addWidget( GroupC1, 0, 0 );
135
136     /***************************************************************/
137     GroupHypotheses = new QGroupBox( tr( "SMESH_HYPOTHESES" ), this, "GroupHypotheses" );
138     GroupHypotheses->setColumnLayout(0, Qt::Vertical );
139     GroupHypotheses->layout()->setSpacing( 0 );
140     GroupHypotheses->layout()->setMargin( 0 );
141     QGridLayout* grid_3 = new QGridLayout( GroupHypotheses->layout() );
142     grid_3->setAlignment( Qt::AlignTop );
143     grid_3->setSpacing( 6 );
144     grid_3->setMargin( 11 );
145
146     TextHypDefinition = new QLabel( tr( "SMESH_AVAILABLE" ), GroupHypotheses, "TextHypDefinition" );
147     grid_3->addWidget( TextHypDefinition, 0, 0 );
148
149     ListHypDefinition = new QListBox( GroupHypotheses, "ListHypDefinition" );
150     ListHypDefinition->setMinimumSize( 100, 100 );
151     grid_3->addWidget( ListHypDefinition, 1, 0 );
152
153     TextHypAssignation = new QLabel( tr( "SMESH_EDIT_USED" ), GroupHypotheses, "TextHypAssignation" );
154     grid_3->addWidget( TextHypAssignation, 0, 1 );
155
156     ListHypAssignation = new QListBox( GroupHypotheses, "ListHypAssignation" );
157     ListHypAssignation->setMinimumSize( 100, 100 );
158     grid_3->addWidget( ListHypAssignation, 1, 1 );
159
160     SMESHGUI_EditHypothesesDlgLayout->addWidget( GroupHypotheses, 1, 0 );
161
162     /***************************************************************/
163     GroupAlgorithms = new QGroupBox( tr( "SMESH_ADD_ALGORITHM" ), this, "GroupAlgorithms" );
164     GroupAlgorithms->setColumnLayout(0, Qt::Vertical );
165     GroupAlgorithms->layout()->setSpacing( 0 );
166     GroupAlgorithms->layout()->setMargin( 0 );
167     QGridLayout* grid_4 = new QGridLayout( GroupAlgorithms->layout() );
168     grid_4->setAlignment( Qt::AlignTop );
169     grid_4->setSpacing( 6 );
170     grid_4->setMargin( 11 );
171
172     TextAlgoDefinition = new QLabel( tr( "SMESH_AVAILABLE" ), GroupAlgorithms, "TextAlgoDefinition" );
173     grid_4->addWidget( TextAlgoDefinition, 0, 0 );
174
175     ListAlgoDefinition = new QListBox( GroupAlgorithms, "ListAlgoDefinition" );
176     ListAlgoDefinition->setMinimumSize( 100, 100 );
177     grid_4->addWidget( ListAlgoDefinition, 1, 0 );
178
179     TextAlgoAssignation = new QLabel( tr( "SMESH_EDIT_USED" ), GroupAlgorithms, "TextAlgoAssignation" );
180     grid_4->addWidget( TextAlgoAssignation, 0, 1 );
181
182     ListAlgoAssignation = new QListBox( GroupAlgorithms, "ListAlgoAssignation" );
183     ListAlgoAssignation ->setMinimumSize( 100, 100 );
184     grid_4->addWidget( ListAlgoAssignation, 1, 1 );
185
186     SMESHGUI_EditHypothesesDlgLayout->addWidget( GroupAlgorithms, 2, 0 );
187
188     /***************************************************************/
189     GroupButtons = new QGroupBox( this, "GroupButtons" );
190     GroupButtons->setColumnLayout(0, Qt::Vertical );
191     GroupButtons->layout()->setSpacing( 0 );
192     GroupButtons->layout()->setMargin( 0 );
193     QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
194     GroupButtonsLayout->setAlignment( Qt::AlignTop );
195     GroupButtonsLayout->setSpacing( 6 );
196     GroupButtonsLayout->setMargin( 11 );
197
198     buttonOk = new QPushButton( tr( "SMESH_BUT_OK" ), GroupButtons, "buttonOk" );
199     buttonOk->setAutoDefault( TRUE );
200     buttonOk->setDefault( FALSE );
201     GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
202
203     buttonApply = new QPushButton( tr( "SMESH_BUT_APPLY" ), GroupButtons, "buttonApply" );
204     buttonApply->setAutoDefault( TRUE );
205     buttonApply->setDefault( FALSE );
206     GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
207
208     GroupButtonsLayout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 2 );
209
210     buttonCancel = new QPushButton( tr( "SMESH_BUT_CLOSE" ), GroupButtons, "buttonCancel" );
211     buttonCancel->setAutoDefault( TRUE );
212     buttonCancel->setDefault( TRUE );
213     buttonCancel->setEnabled( TRUE ) ;
214     GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
215
216     SMESHGUI_EditHypothesesDlgLayout->addWidget( GroupButtons, 4, 0 );
217
218     /***************************************************************/
219     Init(Sel) ;
220 }
221
222
223 //=================================================================================
224 // function : ~SMESHGUI_EditHypothesesDlg()
225 // purpose  : Destroys the object and frees any allocated resources
226 //=================================================================================
227 SMESHGUI_EditHypothesesDlg::~SMESHGUI_EditHypothesesDlg()
228 {
229     // no need to delete child widgets, Qt does it all for us
230 }
231
232
233 //=================================================================================
234 // function : Init()
235 // purpose  :
236 //=================================================================================
237 void SMESHGUI_EditHypothesesDlg::Init( SALOME_Selection* Sel )
238 {
239   mySelection = Sel;
240   mySMESHGUI = SMESHGUI::GetSMESHGUI() ;
241   mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
242
243   InitHypDefinition();
244   InitAlgoDefinition();
245
246   myGeomFilter = new SALOME_TypeFilter( "GEOM" );
247   myMeshOrSubMeshFilter = new SMESH_TypeFilter( MESHorSUBMESH );
248
249   myGeomShape = GEOM::GEOM_Shape::_nil();
250   myMesh = SMESH::SMESH_Mesh::_nil();
251   mySubMesh = SMESH::SMESH_subMesh::_nil();
252
253   /* signals and slots connections */
254   connect( buttonOk,     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
255   connect( buttonApply,  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
256   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) );
257
258   connect( SelectButtonC1A1, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
259   connect( SelectButtonC1A2, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
260
261   connect( mySelection, SIGNAL( currentSelectionChanged() ),      this, SLOT( SelectionIntoArgument() ) );
262   connect( mySMESHGUI,  SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
263   connect( mySMESHGUI,  SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) ) ;
264  
265   connect( ListHypAssignation,  SIGNAL( doubleClicked(QListBoxItem*) ), this, SLOT( removeItem(QListBoxItem*) ) );
266   connect( ListAlgoAssignation, SIGNAL( doubleClicked(QListBoxItem*) ), this, SLOT( removeItem(QListBoxItem*) ) );
267  
268   connect( ListHypDefinition,  SIGNAL( doubleClicked(QListBoxItem*) ), this, SLOT( addItem(QListBoxItem*) ) );
269   connect( ListAlgoDefinition, SIGNAL( doubleClicked(QListBoxItem*) ), this, SLOT( addItem(QListBoxItem*) ) );
270
271   int x, y ;
272   mySMESHGUI->DefineDlgPosition( this, x, y ) ;
273   this->move( x, y ) ;
274   this->show() ; 
275
276   LineEditC1A1->setFocus() ;
277   myEditCurrentArgument = LineEditC1A1;
278   mySelection->ClearFilters() ;   
279   mySelection->AddFilter(myMeshOrSubMeshFilter) ;
280
281   SelectionIntoArgument();
282
283   UpdateControlState();
284 }
285
286
287 //=================================================================================
288 // function : ClickOnOk()
289 // purpose  :
290 //=================================================================================
291 void SMESHGUI_EditHypothesesDlg::ClickOnOk()
292 {
293   if ( ClickOnApply() )
294     ClickOnCancel() ;
295 }
296
297 //=================================================================================
298 // function : ClickOnApply()
299 // purpose  :
300 //=================================================================================
301 bool SMESHGUI_EditHypothesesDlg::ClickOnApply()
302 {
303   bool aRes = false;
304
305   QAD_WaitCursor wc;
306
307   QAD_Operation* op = new QAD_Operation( mySMESHGUI->GetActiveStudy() );
308
309   // start transaction
310   op->start();
311   
312   if ( !myMesh->_is_nil() )
313     aRes = StoreMesh();
314   else if ( !mySubMesh->_is_nil() )
315     aRes = StoreSubMesh();
316
317   if ( aRes )
318     // commit transaction
319     op->finish();
320   else
321     // abort transaction
322     op->abort();
323
324   return aRes;
325 }
326
327
328 //=================================================================================
329 // function : ClickOnCancel()
330 // purpose  :
331 //=================================================================================
332 void SMESHGUI_EditHypothesesDlg::ClickOnCancel()
333 {
334   close();
335 }
336
337
338 //=================================================================================
339 // function : SelectionIntoArgument()
340 // purpose  : Called when selection as changed or other case
341 //=================================================================================
342 void SMESHGUI_EditHypothesesDlg::SelectionIntoArgument()
343 {
344   QString aString = ""; 
345
346   int nbSel = mySMESHGUI->GetNameOfSelectedIObjects(mySelection, aString) ;
347   
348   if ( myEditCurrentArgument == LineEditC1A1 ) {
349     if ( nbSel != 1 ) {
350       myMesh      = SMESH::SMESH_Mesh::_nil();
351       mySubMesh   = SMESH::SMESH_subMesh::_nil();
352       aString     = "";
353     } else {
354       Standard_Boolean testResult ;
355       Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
356       myMesh = mySMESHGUI->ConvertIOinMesh(IO, testResult) ;
357       if( !testResult ) {
358         myMesh = SMESH::SMESH_Mesh::_nil();
359         mySubMesh = mySMESHGUI->ConvertIOinSubMesh(IO, testResult) ;
360         if( !testResult ) {
361           mySubMesh = SMESH::SMESH_subMesh::_nil();
362           aString = "";
363         }
364       }
365     }
366     myEditCurrentArgument->setText( aString );
367     
368     myGeomShape = GEOM::GEOM_Shape::_nil(); // InitGeom() will try to retrieve a shape from myMesh or mySubMesh
369     InitGeom();
370     
371     myImportedMesh = myGeomShape->_is_nil();
372     
373     InitHypAssignation();
374     InitAlgoAssignation();
375   } 
376   else if ( myEditCurrentArgument == LineEditC1A2 ) {
377     if ( nbSel != 1 )
378       myGeomShape = GEOM::GEOM_Shape::_nil();
379     else {
380       Standard_Boolean testResult ;
381       Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
382       myGeomShape = mySMESHGUI->ConvertIOinGEOMShape(IO, testResult) ;
383       if( !testResult )
384         myGeomShape = GEOM::GEOM_Shape::_nil();
385     }
386     InitGeom();
387   } 
388
389   UpdateControlState();
390 }
391
392
393 //=================================================================================
394 // function : SetEditCurrentArgument()
395 // purpose  :
396 //=================================================================================
397 void SMESHGUI_EditHypothesesDlg::SetEditCurrentArgument()
398 {
399   QPushButton* send = (QPushButton*)sender();
400   if(send == SelectButtonC1A1) {
401     LineEditC1A1->setFocus() ;
402     myEditCurrentArgument = LineEditC1A1;
403     mySelection->ClearFilters() ;   
404     mySelection->AddFilter(myMeshOrSubMeshFilter) ;
405   } else if (send == SelectButtonC1A2) {
406     LineEditC1A2->setFocus() ;
407     myEditCurrentArgument = LineEditC1A2;
408     mySelection->ClearFilters() ;
409     mySelection->AddFilter(myGeomFilter) ;
410   } 
411   SelectionIntoArgument() ;
412 }
413
414 //=================================================================================
415 // function : DeactivateActiveDialog()
416 // purpose  :
417 //=================================================================================
418 void SMESHGUI_EditHypothesesDlg::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_EditHypothesesDlg::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_EditHypothesesDlg::enterEvent(QEvent* e)
446 {
447   if ( !GroupC1->isEnabled() )
448     ActivateThisDialog();
449 }
450
451
452 //=================================================================================
453 // function : closeEvent()
454 // purpose  :
455 //=================================================================================
456 void SMESHGUI_EditHypothesesDlg::closeEvent( QCloseEvent* e )
457 {
458   disconnect( mySelection, 0, this, 0 );
459   mySMESHGUI->ResetState() ;
460   mySelection->ClearFilters() ;
461   QDialog::closeEvent( e );
462 }
463
464
465 //=================================================================================
466 // function : removeItem()
467 // purpose  :
468 //=================================================================================
469 void SMESHGUI_EditHypothesesDlg::removeItem(QListBoxItem* item)
470 {
471   const QObject* aSender = sender();
472
473   if (!item) return;
474
475   if ( aSender == ListHypAssignation ) {
476     ListHypAssignation->removeItem( ListHypAssignation->index( item ) );
477   } 
478   else if ( aSender == ListAlgoAssignation ) {
479     ListAlgoAssignation->removeItem( ListAlgoAssignation->index( item ) );
480   }
481
482   UpdateControlState();
483 }
484
485
486 //=================================================================================
487 // function : addItem()
488 // purpose  :
489 //=================================================================================
490 void SMESHGUI_EditHypothesesDlg::addItem(QListBoxItem* item)
491 {
492   const QObject* aSender = sender();
493
494   if (!item) return;
495
496   ListBoxIOR* i = 0;
497   if ( item->rtti() == ListBoxIOR::RTTI_IOR )
498     i = (ListBoxIOR*)item;
499   if (!i) return;
500   
501   bool isFound = false;
502
503   if ( aSender == ListHypDefinition ) {
504     for ( int j = 0, n = ListHypAssignation->count(); !isFound && j < n; j++ ) {
505       if ( ListHypAssignation->item( j )->rtti() == ListBoxIOR::RTTI_IOR ) {
506         ListBoxIOR* anItem = (ListBoxIOR*)ListHypAssignation->item( j );
507         isFound = !strcmp( anItem->GetIOR(), i->GetIOR() );
508       }
509     }
510     if ( !isFound )
511       ListBoxIOR* anItem = new ListBoxIOR( ListHypAssignation, 
512                                            strdup( i->GetIOR() ), 
513                                            strdup( i->text().latin1() ) );
514   }
515   else if ( aSender == ListAlgoDefinition ) {
516     for ( int j = 0, n = ListAlgoAssignation->count(); !isFound && j < n; j++ ) {
517       if ( ListAlgoAssignation->item( j )->rtti() == ListBoxIOR::RTTI_IOR ) {
518         ListBoxIOR* anItem = (ListBoxIOR*)ListAlgoAssignation->item( j );
519         isFound = !strcmp( anItem->GetIOR(), i->GetIOR() );
520       }
521     }
522     if ( !isFound )
523       ListBoxIOR* anItem = new ListBoxIOR( ListAlgoAssignation, 
524                                            strdup( i->GetIOR() ), 
525                                            strdup( i->text().latin1() ) );
526   }
527
528   UpdateControlState();
529 }
530
531
532 //=================================================================================
533 // function : InitHypDefinition()
534 // purpose  :
535 //=================================================================================
536 void SMESHGUI_EditHypothesesDlg::InitHypDefinition()
537 {
538   ListHypDefinition->clear();
539
540   SALOMEDS::SComponent_var father = mySMESHGUI->GetStudy()->FindComponent("MESH");
541   if ( father->_is_nil() )
542     return;
543
544   SALOMEDS::SObject_var           HypothesisRoot;
545   SALOMEDS::GenericAttribute_var  anAttr;
546   SALOMEDS::AttributeName_var     aName;
547   SALOMEDS::AttributeIOR_var      anIOR;
548
549   int Tag_HypothesisRoot = 1;
550   if (father->FindSubObject (1, HypothesisRoot)) {
551     SALOMEDS::ChildIterator_var it = mySMESHGUI->GetStudy()->NewChildIterator(HypothesisRoot);
552     for (; it->More();it->Next()) {
553       SALOMEDS::SObject_var Obj = it->Value();
554       if (Obj->FindAttribute(anAttr, "AttributeName") ) {
555         aName = SALOMEDS::AttributeName::_narrow(anAttr);
556         if (Obj->FindAttribute(anAttr, "AttributeIOR")) {
557           anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
558           ListBoxIOR* anItem = new ListBoxIOR( ListHypDefinition, 
559                                                anIOR->Value(), 
560                                                aName->Value() );
561         }
562       }
563     }
564   }
565 }
566
567 //=================================================================================
568 // function : InitHypAssignation()
569 // purpose  :
570 //=================================================================================
571 void SMESHGUI_EditHypothesesDlg::InitHypAssignation()
572 {
573   MESSAGE ( " InitHypAssignation " << myMesh->_is_nil() )
574   MESSAGE ( " InitHypAssignation " << mySubMesh->_is_nil() )
575
576   myMapOldHypos.clear();
577   ListHypAssignation->clear();
578   if ( myImportedMesh )
579     return;
580
581   SALOMEDS::SObject_var             aMorSM, AHR, aRef;
582   SALOMEDS::GenericAttribute_var    anAttr;
583   SALOMEDS::AttributeName_var       aName;
584   SALOMEDS::AttributeIOR_var        anIOR;
585   SMESHGUI_StudyAPI                 myStudyAPI = mySMESHGUI->GetStudyAPI();
586
587   if ( !myMesh->_is_nil() )
588     aMorSM = myStudyAPI.FindObject( myMesh );
589   else if ( !mySubMesh->_is_nil() )
590     aMorSM = myStudyAPI.FindObject( mySubMesh );
591
592   if ( !aMorSM->_is_nil() && aMorSM->FindSubObject (2, AHR)) {
593     SALOMEDS::ChildIterator_var it = mySMESHGUI->GetStudy()->NewChildIterator(AHR);
594     for (; it->More();it->Next()) {
595       SALOMEDS::SObject_var Obj = it->Value();
596       if ( Obj->ReferencedObject(aRef) ) {
597         if (aRef->FindAttribute(anAttr, "AttributeName") ) {
598           aName = SALOMEDS::AttributeName::_narrow(anAttr);
599           if (aRef->FindAttribute(anAttr, "AttributeIOR")) {
600             anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
601             ListBoxIOR* anItem = new ListBoxIOR( ListHypAssignation, 
602                                                  anIOR->Value(), 
603                                                  aName->Value() );
604             myMapOldHypos[ anIOR->Value() ] = ListHypAssignation->index( anItem );
605           }
606         }
607       }
608     }
609   }
610 }
611
612 //=================================================================================
613 // function : InitAlgoDefinition()
614 // purpose  :
615 //=================================================================================
616 void SMESHGUI_EditHypothesesDlg::InitAlgoDefinition()
617 {
618   ListAlgoDefinition->clear();
619
620   SALOMEDS::SComponent_var father = mySMESHGUI->GetStudy()->FindComponent("MESH");
621   if ( father->_is_nil() )
622     return;
623
624   SALOMEDS::SObject_var          AlgorithmsRoot;
625   SALOMEDS::GenericAttribute_var anAttr;
626   SALOMEDS::AttributeName_var    aName;
627   SALOMEDS::AttributeIOR_var     anIOR;
628
629   if (father->FindSubObject (2, AlgorithmsRoot)) {
630     SALOMEDS::ChildIterator_var it = mySMESHGUI->GetStudy()->NewChildIterator(AlgorithmsRoot);
631     for (; it->More();it->Next()) {
632       SALOMEDS::SObject_var Obj = it->Value();
633       if (Obj->FindAttribute(anAttr, "AttributeName") ) {
634         aName = SALOMEDS::AttributeName::_narrow(anAttr);
635         if (Obj->FindAttribute(anAttr, "AttributeIOR")) {
636           anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
637           ListBoxIOR* anItem = new ListBoxIOR( ListAlgoDefinition, 
638                                                anIOR->Value(), 
639                                                aName->Value() );
640         }
641       }
642     }
643   }
644 }
645
646
647 //=================================================================================
648 // function : InitAlgoAssignation()
649 // purpose  :
650 //=================================================================================
651 void SMESHGUI_EditHypothesesDlg::InitAlgoAssignation()
652 {
653   MESSAGE ( " InitAlgoAssignation " << myMesh->_is_nil() )
654   MESSAGE ( " InitAlgoAssignation " << mySubMesh->_is_nil() )
655
656   myMapOldAlgos.clear();
657   ListAlgoAssignation->clear();
658   if ( myImportedMesh )
659     return;
660
661   SALOMEDS::SObject_var             aMorSM, AHR, aRef;
662   SALOMEDS::GenericAttribute_var    anAttr;
663   SALOMEDS::AttributeName_var       aName;
664   SALOMEDS::AttributeIOR_var        anIOR;
665   SMESHGUI_StudyAPI                 myStudyAPI = mySMESHGUI->GetStudyAPI();  
666
667   if ( !myMesh->_is_nil() )
668     aMorSM = myStudyAPI.FindObject( myMesh );
669   else if ( !mySubMesh->_is_nil() )
670     aMorSM = myStudyAPI.FindObject( mySubMesh );
671
672   if ( !aMorSM->_is_nil() && aMorSM->FindSubObject (3, AHR)) {
673     SALOMEDS::ChildIterator_var it = mySMESHGUI->GetStudy()->NewChildIterator(AHR);
674     for (; it->More();it->Next()) {
675       SALOMEDS::SObject_var Obj = it->Value();
676       if ( Obj->ReferencedObject(aRef) ) {
677         if (aRef->FindAttribute(anAttr, "AttributeName") ) {
678           aName = SALOMEDS::AttributeName::_narrow(anAttr);
679           if (aRef->FindAttribute(anAttr, "AttributeIOR")) {
680             anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
681             ListBoxIOR* anItem = new ListBoxIOR( ListAlgoAssignation, 
682                                                  anIOR->Value(), 
683                                                  aName->Value() );
684             myMapOldAlgos[ anIOR->Value() ] = ListAlgoAssignation->index( anItem );
685           }
686         }
687       }
688     }
689   }
690 }
691
692 //=================================================================================
693 // function : InitGeom()
694 // purpose  :
695 //=================================================================================
696 void SMESHGUI_EditHypothesesDlg::InitGeom()
697 {
698   LineEditC1A2->setText("") ;
699   SMESHGUI_StudyAPI myStudyAPI = mySMESHGUI->GetStudyAPI();
700
701   if ( myGeomShape->_is_nil() && !myMesh->_is_nil() ) {
702     SALOMEDS::SObject_var aMesh = myStudyAPI.FindObject( myMesh );
703     if ( !aMesh->_is_nil() )
704       myGeomShape = myStudyAPI.GetShapeOnMeshOrSubMesh(aMesh);
705   }
706   if ( myGeomShape->_is_nil() && !mySubMesh->_is_nil() ) {
707     SALOMEDS::SObject_var aSubMesh = myStudyAPI.FindObject( mySubMesh );
708     if ( !aSubMesh->_is_nil() )
709       myGeomShape = myStudyAPI.GetShapeOnMeshOrSubMesh(aSubMesh);
710   }
711   
712   SALOMEDS::GenericAttribute_var    anAttr;
713   SALOMEDS::AttributeName_var       aName;
714   if ( !myGeomShape->_is_nil() && (!myMesh->_is_nil() || !mySubMesh->_is_nil()) ) {
715     SALOMEDS::SObject_var aSO = mySMESHGUI->GetStudy()->FindObjectIOR( myGeomShape->Name() );
716     if ( !aSO->_is_nil() ) {
717       if (aSO->FindAttribute(anAttr, "AttributeName") ) {
718         aName = SALOMEDS::AttributeName::_narrow(anAttr);
719         LineEditC1A2->setText( QString(aName->Value()) ) ;
720       }
721     }
722   }
723 }
724
725 //=================================================================================
726 // function : UpdateControlState()
727 // purpose  :
728 //=================================================================================
729 void SMESHGUI_EditHypothesesDlg::UpdateControlState()
730 {
731   bool isEnabled = ( !myMesh   ->_is_nil() && !myGeomShape->_is_nil() &&   ListHypAssignation->count() && ListAlgoAssignation->count() ) ||
732                    ( !mySubMesh->_is_nil() && !myGeomShape->_is_nil() && ( ListHypAssignation->count() || ListAlgoAssignation->count() ) );
733
734   buttonOk   ->setEnabled( isEnabled && !myImportedMesh );
735   buttonApply->setEnabled( isEnabled && !myImportedMesh );
736
737   SelectButtonC1A2   ->setEnabled( ALLOW_CHANGE_SHAPE && !myImportedMesh );
738   LineEditC1A2       ->setEnabled( ALLOW_CHANGE_SHAPE && !myImportedMesh );
739   ListHypDefinition  ->setEnabled( !myImportedMesh );
740   ListHypAssignation ->setEnabled( !myImportedMesh );
741   ListAlgoDefinition ->setEnabled( !myImportedMesh );
742   ListAlgoAssignation->setEnabled( !myImportedMesh );
743 }
744
745 //=================================================================================
746 // function : StoreMesh()
747 // purpose  :
748 //=================================================================================
749 bool SMESHGUI_EditHypothesesDlg::StoreMesh()
750 {
751   SMESHGUI_StudyAPI myStudyAPI = mySMESHGUI->GetStudyAPI();
752   MapIOR anOldHypos, aNewHypos;
753   if ( myGeomShape->_is_nil() )
754     return false;
755   // 1. Check whether the geometric shape has changed
756   SALOMEDS::SObject_var aMeshSO = myStudyAPI.FindObject( myMesh );
757   GEOM::GEOM_Shape_var aIniGeomShape = myStudyAPI.GetShapeOnMeshOrSubMesh( aMeshSO );
758   bool bShapeChanged = aIniGeomShape->_is_nil() || !aIniGeomShape->_is_equivalent( myGeomShape );
759   if ( bShapeChanged ) {
760     // VSR : TODO : Set new shape - not supported yet by SMESH engine
761     // 1) remove all old hypotheses and algorithms and also submeshes
762     // 2) set new shape
763   }
764   MapIOR::iterator it;
765   // 2. remove not used hypotheses from the mesh
766   for ( it = myMapOldHypos.begin(); it != myMapOldHypos.end(); ++it ) {
767     string ior = it->first;
768     int index = findItem( ListHypAssignation, ior );
769     if ( index < 0 ) {
770       CORBA::Object_var anObject = myStudyAPI.StringToIOR( ior.c_str() );
771       if ( !CORBA::is_nil( anObject ) ) {
772         SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
773         if ( !aHyp->_is_nil() )
774           if ( !mySMESHGUI->RemoveHypothesisOrAlgorithmOnMesh( aMeshSO, aHyp ) )
775             return false;
776       }
777     }
778   }
779   // 3. remove not used algorithms from the mesh
780   for ( it = myMapOldAlgos.begin(); it != myMapOldAlgos.end(); ++it ) {
781     string ior = it->first;
782     int index = findItem( ListAlgoAssignation, ior );
783     if ( index < 0 ) {
784       CORBA::Object_var anObject = myStudyAPI.StringToIOR( ior.c_str() );
785       if ( !CORBA::is_nil( anObject ) ) {
786         SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
787         if ( !aHyp->_is_nil() )
788           if ( !mySMESHGUI->RemoveHypothesisOrAlgorithmOnMesh( aMeshSO, aHyp ) )
789             return false;
790       }
791     }
792   }
793   // 4. Add new hypotheses
794   for ( int i = 0; i < ListHypAssignation->count(); i++ ) {
795     if ( ListHypAssignation->item( i )->rtti() == ListBoxIOR::RTTI_IOR ) {
796       ListBoxIOR* anItem = ( ListBoxIOR* )( ListHypAssignation->item( i ) );
797       if ( anItem ) {
798         string ior = anItem->GetIOR();
799         if ( myMapOldHypos.find( ior ) == myMapOldHypos.end() ) {
800           CORBA::Object_var anObject = myStudyAPI.StringToIOR( ior.c_str() );
801           if ( !CORBA::is_nil( anObject ) ) {
802             SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
803             if ( !aHyp->_is_nil() )
804               if ( !mySMESHGUI->AddHypothesisOnMesh( myMesh, aHyp ) )
805                 return false;
806           }
807         }
808       }
809     }
810   }
811   // 4. Add new algorithms
812   for ( int i = 0; i < ListAlgoAssignation->count(); i++ ) {
813     if ( ListAlgoAssignation->item( i )->rtti() == ListBoxIOR::RTTI_IOR ) {
814       ListBoxIOR* anItem = ( ListBoxIOR* )( ListAlgoAssignation->item( i ) );
815       if ( anItem ) {
816         string ior = anItem->GetIOR();
817         if ( myMapOldAlgos.find( ior ) == myMapOldAlgos.end() ) {
818           CORBA::Object_var anObject = myStudyAPI.StringToIOR( ior.c_str() );
819           if ( !CORBA::is_nil( anObject ) ) {
820             SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
821             if ( !aHyp->_is_nil() )
822               if ( !mySMESHGUI->AddAlgorithmOnMesh( myMesh, aHyp ) )
823                 return false;
824           }
825         }
826       }
827     }
828   }
829   return true;
830 }
831
832 //=================================================================================
833 // function : StoreSubMesh()
834 // purpose  :
835 //=================================================================================
836 bool SMESHGUI_EditHypothesesDlg::StoreSubMesh()
837 {
838   SMESHGUI_StudyAPI myStudyAPI = mySMESHGUI->GetStudyAPI();
839   MapIOR anOldHypos, aNewHypos;
840   if ( myGeomShape->_is_nil() )
841     return false;
842   // 1. Check whether the geometric shape has changed
843   SALOMEDS::SObject_var aSubMeshSO = myStudyAPI.FindObject( mySubMesh );
844   GEOM::GEOM_Shape_var aIniGeomShape = myStudyAPI.GetShapeOnMeshOrSubMesh( aSubMeshSO );
845   bool bShapeChanged = aIniGeomShape->_is_nil() || !aIniGeomShape->_is_equivalent( myGeomShape );
846   if ( bShapeChanged ) {
847     // VSR : TODO : Set new shape - not supported yet by engine
848     // 1) remove all old hypotheses and algorithms
849     // 2) set new shape
850   }
851   MapIOR::iterator it;
852   // 2. remove not used hypotheses from the submesh
853   for ( it = myMapOldHypos.begin(); it != myMapOldHypos.end(); ++it ) {
854     string ior = it->first;
855     int index = findItem( ListHypAssignation, ior );
856     if ( index < 0 ) {
857       CORBA::Object_var anObject = myStudyAPI.StringToIOR( ior.c_str() );
858       if ( !CORBA::is_nil( anObject ) ) {
859         SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
860         if ( !aHyp->_is_nil() )
861           if ( !mySMESHGUI->RemoveHypothesisOrAlgorithmOnMesh( aSubMeshSO, aHyp ) )
862             return false;
863       }
864     }
865   }
866   // 3. remove not used algorithms from the submesh
867   for ( it = myMapOldAlgos.begin(); it != myMapOldAlgos.end(); ++it ) {
868     string ior = it->first;
869     int index = findItem( ListAlgoAssignation, ior );
870     if ( index < 0 ) {
871       CORBA::Object_var anObject = myStudyAPI.StringToIOR( ior.c_str() );
872       if ( !CORBA::is_nil( anObject ) ) {
873         SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
874         if ( !aHyp->_is_nil() )
875           if ( !mySMESHGUI->RemoveHypothesisOrAlgorithmOnMesh( aSubMeshSO, aHyp ) )
876             return false;
877       }
878     }
879   }
880   // 4. Add new hypotheses
881   for ( int i = 0; i < ListHypAssignation->count(); i++ ) {
882     if ( ListHypAssignation->item( i )->rtti() == ListBoxIOR::RTTI_IOR ) {
883       ListBoxIOR* anItem = ( ListBoxIOR* )( ListHypAssignation->item( i ) );
884       if ( anItem ) {
885         string ior = anItem->GetIOR();
886         if ( myMapOldHypos.find( ior ) == myMapOldHypos.end() ) {
887           CORBA::Object_var anObject = myStudyAPI.StringToIOR( ior.c_str() );
888           if ( !CORBA::is_nil( anObject ) ) {
889             SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
890             if ( !aHyp->_is_nil() )
891               if ( !mySMESHGUI->AddHypothesisOnSubMesh( mySubMesh, aHyp ) )
892                 return false;
893           }
894         }
895       }
896     }
897   }
898   // 4. Add new algorithms
899   for ( int i = 0; i < ListAlgoAssignation->count(); i++ ) {
900     if ( ListAlgoAssignation->item( i )->rtti() == ListBoxIOR::RTTI_IOR ) {
901       ListBoxIOR* anItem = ( ListBoxIOR* )( ListAlgoAssignation->item( i ) );
902       if ( anItem ) {
903         string ior = anItem->GetIOR();
904         if ( myMapOldAlgos.find( ior ) == myMapOldAlgos.end() ) {
905           CORBA::Object_var anObject = myStudyAPI.StringToIOR( ior.c_str() );
906           if ( !CORBA::is_nil( anObject ) ) {
907             SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
908             if ( !aHyp->_is_nil() )
909               if ( !mySMESHGUI->AddAlgorithmOnSubMesh( mySubMesh, aHyp ) )
910                 return false;
911           }
912         }
913       }
914     }
915   }
916   return true;
917 }