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