1 // SMESH SMESHGUI : GUI for SMESH component
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SMESHGUI_InitMeshDlg.cxx
25 // Author : Nicolas REJNERI
29 #include "SMESHGUI_InitMeshDlg.h"
32 #include "SMESHGUI_Utils.h"
33 #include "SMESHGUI_HypothesesUtils.h"
35 #include "SMESH_TypeFilter.hxx"
36 #include "SMESH_NumberFilter.hxx"
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"
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"
52 #include "SVTK_ViewModel.h"
54 #include "utilities.h"
57 #include <qgroupbox.h>
59 #include <qlineedit.h>
60 #include <qpushbutton.h>
67 SMESH::SMESH_Mesh_var InitMesh (GEOM::GEOM_Object_ptr theShapeObject,
68 const char* theMeshName)
70 SMESH::SMESH_Mesh_var aMesh;
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);
81 catch (const SALOME::SALOME_Exception& S_ex) {
82 SalomeApp_Tools::QtCatchCorbaException(S_ex);
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 ) )
102 QPixmap image0 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
104 setName("SMESHGUI_InitMeshDlg");
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);
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);
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);
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);
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);
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);
155 SMESHGUI_InitMeshDlgLayout->addWidget(GroupC1, 1, 0);
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);
167 buttonOk = new QPushButton(tr("SMESH_BUT_OK"), GroupButtons, "buttonOk");
168 buttonOk->setAutoDefault(TRUE);
169 buttonOk->setDefault(TRUE);
170 GroupButtonsLayout->addWidget(buttonOk, 0, 0);
172 buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons, "buttonApply");
173 buttonApply->setAutoDefault(TRUE);
174 GroupButtonsLayout->addWidget(buttonApply, 0, 1);
176 GroupButtonsLayout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 2);
178 buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons, "buttonCancel");
179 buttonCancel->setAutoDefault(TRUE);
180 GroupButtonsLayout->addWidget(buttonCancel, 0, 3);
182 SMESHGUI_InitMeshDlgLayout->addWidget(GroupButtons, 2, 0);
184 /***************************************************************/
188 //=================================================================================
189 // function : ~SMESHGUI_InitMeshDlg()
190 // purpose : Destroys the object and frees any allocated resources
191 //=================================================================================
192 SMESHGUI_InitMeshDlg::~SMESHGUI_InitMeshDlg()
194 // no need to delete child widgets, Qt does it all for us
197 //=================================================================================
200 //=================================================================================
201 void SMESHGUI_InitMeshDlg::Init ()
203 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
205 //myGeomFilter = new SALOME_TypeFilter("GEOM");
206 TColStd_MapOfInteger allTypesMap;
207 for (int i = 0; i < 10; i++)
209 myGeomFilter = new SMESH_NumberFilter ("GEOM", TopAbs_SHAPE, 0, allTypesMap);
210 myAlgorithmFilter = new SMESH_TypeFilter (ALGORITHM);
211 myHypothesisFilter = new SMESH_TypeFilter (HYPOTHESIS);
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()));
218 connect(SelectButtonC1A1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
219 connect(SelectButtonC1A1Hyp, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
220 connect(SelectButtonC1A1Algo, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
222 connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
223 connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
224 connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
227 mySMESHGUI->DefineDlgPosition(this, x, y);
231 LineEdit_NameMesh->setText(GetDefaultMeshName());
232 LineEdit_NameMesh->setFocus();
233 myEditCurrentArgument = LineEditC1A1;
234 mySelectionMgr->clearFilters();
235 mySelectionMgr->installFilter(myGeomFilter);
237 SelectionIntoArgument();
239 UpdateControlState();
242 //=================================================================================
243 // function : ClickOnOk()
245 //=================================================================================
246 void SMESHGUI_InitMeshDlg::ClickOnOk()
248 if (this->ClickOnApply())
249 this->ClickOnCancel();
252 //=================================================================================
253 // function : ClickOnApply()
255 //=================================================================================
256 bool SMESHGUI_InitMeshDlg::ClickOnApply()
258 if (mySMESHGUI->isActiveStudyLocked())
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"));
268 if (myGeomShape->_is_nil() || !HypoList.count() || !AlgoList.count())
271 SUIT_OverrideCursor wc;
274 new SUIT_Operation (SUIT_Session::session()->activeApplication());
280 SMESH::SMESH_Mesh_var aMesh = SMESH::InitMesh(myGeomShape, myNameMesh);
282 if (!aMesh->_is_nil()) {
284 for (int i = 0; i < HypoList.count(); i++) {
285 _PTR(SObject) aHypSOClient =
286 SMESH::GetActiveStudyDocument()->FindObjectID(HypoList[i].latin1());
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)) {
302 for (int i = 0; i < AlgoList.count(); i++) {
303 _PTR(SObject) aHypSOClient =
304 SMESH::GetActiveStudyDocument()->FindObjectID(AlgoList[i].latin1());
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)) {
320 // commit transaction
322 mySMESHGUI->updateObjBrowser();
323 LineEdit_NameMesh->setText(GetDefaultMeshName());
327 //=================================================================================
328 // function : ClickOnCancel()
330 //=================================================================================
331 void SMESHGUI_InitMeshDlg::ClickOnCancel()
336 //=================================================================================
337 // function : SelectionIntoArgument()
338 // purpose : Called when selection as changed or other case
339 //=================================================================================
340 void SMESHGUI_InitMeshDlg::SelectionIntoArgument()
342 QString aString = "";
345 mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
347 int nbSel = SMESH::GetNameOfSelectedIObjects(mySelectionMgr, aString);
349 if (myEditCurrentArgument == LineEditC1A1) {
352 myGeomShape = GEOM::GEOM_Object::_nil();
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();
362 } else if (myEditCurrentArgument == LineEditC1A1Hyp) {
366 SALOME_ListIteratorOfListIO Itinit (aList);
367 for (; Itinit.More(); Itinit.Next()) {
368 HypoList.append(Itinit.Value()->getEntry());
371 aString = tr("%1 Hypothesis").arg(nbSel);
375 } else if (myEditCurrentArgument == LineEditC1A1Algo) {
379 SALOME_ListIteratorOfListIO Itinit (aList);
380 for (; Itinit.More(); Itinit.Next()) {
381 AlgoList.append(Itinit.Value()->getEntry());
384 aString = tr("%1 Algorithms").arg(nbSel);
391 myEditCurrentArgument->setText(aString);
393 UpdateControlState();
396 //=================================================================================
397 // function : SetEditCurrentArgument()
399 //=================================================================================
400 void SMESHGUI_InitMeshDlg::SetEditCurrentArgument()
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);
419 SelectionIntoArgument();
422 //=================================================================================
423 // function : DeactivateActiveDialog()
425 //=================================================================================
426 void SMESHGUI_InitMeshDlg::DeactivateActiveDialog()
428 if (GroupC1->isEnabled()) {
429 disconnect(mySelectionMgr, 0, this, 0);
430 GroupC1->setEnabled(false);
431 GroupButtons->setEnabled(false);
435 //=================================================================================
436 // function : ActivateThisDialog()
438 //=================================================================================
439 void SMESHGUI_InitMeshDlg::ActivateThisDialog()
441 mySMESHGUI->EmitSignalDeactivateDialog();
442 GroupC1->setEnabled(true);
443 GroupButtons->setEnabled(true);
444 connect (mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
447 //=================================================================================
448 // function : enterEvent()
450 //=================================================================================
451 void SMESHGUI_InitMeshDlg::enterEvent (QEvent* e)
453 if (!GroupC1->isEnabled())
454 ActivateThisDialog();
457 //=================================================================================
458 // function : closeEvent()
460 //=================================================================================
461 void SMESHGUI_InitMeshDlg::closeEvent (QCloseEvent* e)
463 disconnect(mySelectionMgr, 0, this, 0);
464 mySMESHGUI->ResetState();
465 mySelectionMgr->clearFilters();
466 QDialog::closeEvent(e);
469 //=================================================================================
470 // function : UpdateControlState()
472 //=================================================================================
473 void SMESHGUI_InitMeshDlg::UpdateControlState()
475 bool isEnabled = (!myGeomShape->_is_nil() && HypoList.count() && AlgoList.count());
477 buttonOk ->setEnabled(isEnabled);
478 buttonApply->setEnabled(isEnabled);
481 //=================================================================================
482 // function : GetDefaultMeshName()
483 // purpose : Generates default mesh name(Mesh_1, Mesh_2, etc.)
484 //=================================================================================
485 QString SMESHGUI_InitMeshDlg::GetDefaultMeshName()
487 _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
493 aMeshName = QString(tr("SMESH_OBJECT_MESH")) + "_" + QString::number(++aNumber);
494 obj = aStudy->FindObject(aMeshName.latin1());