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