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