1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // SMESH SMESHGUI : GUI for SMESH component
23 // File : SMESHGUI_BuildCompoundDlg.cxx
24 // Author : Alexander KOVALEV, Open CASCADE S.A.S.
27 #include "SMESHGUI_BuildCompoundDlg.h"
30 #include "SMESHGUI_Utils.h"
31 #include "SMESHGUI_SpinBox.h"
32 #include "SMESHGUI_VTKUtils.h"
34 #include <SMESH_TypeFilter.hxx>
36 // SALOME GUI includes
37 #include <SUIT_Desktop.h>
38 #include <SUIT_Session.h>
39 #include <SUIT_MessageBox.h>
40 #include <SUIT_ResourceMgr.h>
41 #include <SalomeApp_Study.h>
42 #include <SUIT_OverrideCursor.h>
44 #include <LightApp_Application.h>
45 #include <LightApp_SelectionMgr.h>
46 #include <SALOME_ListIO.hxx>
49 #include <QApplication>
53 #include <QPushButton>
54 #include <QRadioButton>
55 #include <QHBoxLayout>
56 #include <QVBoxLayout>
57 #include <QGridLayout>
61 #include <QButtonGroup>
69 //To disable automatic genericobj management, the following line should be commented.
70 //Otherwise, it should be uncommented. Refer to KERNEL_SRC/src/SALOMEDSImpl/SALOMEDSImpl_AttributeIOR.cxx
71 #define WITHGENERICOBJ
73 //=================================================================================
74 // name : SMESHGUI_BuildCompoundDlg
76 //=================================================================================
77 SMESHGUI_BuildCompoundDlg::SMESHGUI_BuildCompoundDlg( SMESHGUI* theModule )
78 : QDialog(SMESH::GetDesktop(theModule)),
79 mySMESHGUI(theModule),
80 mySelectionMgr(SMESH::GetSelectionMgr(theModule))
83 setAttribute(Qt::WA_DeleteOnClose, true);
84 setWindowTitle(tr("SMESH_BUILD_COMPOUND_TITLE"));
86 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
87 QPixmap image0 (aResMgr->loadPixmap("SMESH", tr("ICON_DLG_BUILD_COMPOUND_MESH")));
88 QPixmap image1 (aResMgr->loadPixmap("SMESH", tr("ICON_SELECT")));
90 setSizeGripEnabled(true);
92 QVBoxLayout* aTopLayout = new QVBoxLayout(this);
93 aTopLayout->setSpacing(SPACING);
94 aTopLayout->setMargin(MARGIN);
96 /***************************************************************/
97 GroupConstructors = new QGroupBox(tr("COMPOUND"), this);
98 QButtonGroup* ButtonGroup = new QButtonGroup(this);
99 QHBoxLayout* GroupConstructorsLayout = new QHBoxLayout(GroupConstructors);
100 GroupConstructorsLayout->setSpacing(SPACING);
101 GroupConstructorsLayout->setMargin(MARGIN);
103 Constructor1 = new QRadioButton(GroupConstructors);
104 Constructor1->setIcon(image0);
105 Constructor1->setChecked(true);
106 GroupConstructorsLayout->addWidget(Constructor1);
107 ButtonGroup->addButton(Constructor1, 0);
109 /***************************************************************/
110 GroupName = new QGroupBox(tr("RESULT_NAME"), this);
111 QHBoxLayout* GroupNameLayout = new QHBoxLayout(GroupName);
112 GroupNameLayout->setSpacing(SPACING);
113 GroupNameLayout->setMargin(MARGIN);
115 TextLabelName = new QLabel(tr("SMESH_NAME"), GroupName);
116 LineEditName = new QLineEdit(GroupName);
118 GroupNameLayout->addWidget(TextLabelName);
119 GroupNameLayout->addWidget(LineEditName);
121 /***************************************************************/
122 GroupArgs = new QGroupBox(tr("SMESH_ARGUMENTS"), this);
123 QGridLayout* GroupArgsLayout = new QGridLayout(GroupArgs);
124 GroupArgsLayout->setSpacing(SPACING);
125 GroupArgsLayout->setMargin(MARGIN);
127 TextLabelMeshes = new QLabel(tr("MESHES"), GroupArgs);
128 SelectButton = new QPushButton(GroupArgs);
129 SelectButton->setIcon(image1);
130 SelectButton->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
131 LineEditMeshes = new QLineEdit(GroupArgs);
132 LineEditMeshes->setReadOnly(true);
134 TextLabelUnion = new QLabel(tr("PROCESSING_IDENTICAL_GROUPS"), GroupArgs);
135 ComboBoxUnion = new QComboBox(GroupArgs);
137 CheckBoxCommon = new QCheckBox(tr("CREATE_COMMON_GROUPS"), GroupArgs);
139 CheckBoxMerge = new QCheckBox(tr("MERGE_NODES_AND_ELEMENTS"), GroupArgs);
141 TextLabelTol = new QLabel(tr("SMESH_TOLERANCE"), GroupArgs);
142 TextLabelTol->setAlignment(Qt::AlignCenter);
143 SpinBoxTol = new SMESHGUI_SpinBox(GroupArgs);
144 SpinBoxTol->RangeStepAndValidator(0.0, COORD_MAX, 0.00001, "len_tol_precision" );
146 GroupArgsLayout->addWidget(TextLabelMeshes, 0, 0);
147 GroupArgsLayout->addWidget(SelectButton, 0, 1);
148 GroupArgsLayout->addWidget(LineEditMeshes, 0, 2, 1, 2);
149 GroupArgsLayout->addWidget(TextLabelUnion, 1, 0, 1, 3);
150 GroupArgsLayout->addWidget(ComboBoxUnion, 1, 3);
151 GroupArgsLayout->addWidget(CheckBoxCommon, 2, 0, 1, 4);
152 GroupArgsLayout->addWidget(CheckBoxMerge, 3, 0, 1, 4);
153 GroupArgsLayout->addWidget(TextLabelTol, 4, 0, 1, 2);
154 GroupArgsLayout->addWidget(SpinBoxTol, 4, 2, 1, 2);
156 /***************************************************************/
157 GroupButtons = new QGroupBox(this);
158 QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
159 GroupButtonsLayout->setSpacing(SPACING);
160 GroupButtonsLayout->setMargin(MARGIN);
162 buttonOk = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), GroupButtons);
163 buttonOk->setAutoDefault(true);
164 buttonOk->setDefault(true);
165 buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
166 buttonApply->setAutoDefault(true);
167 buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
168 buttonCancel->setAutoDefault(true);
169 buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
170 buttonHelp->setAutoDefault(true);
172 GroupButtonsLayout->addWidget(buttonOk);
173 GroupButtonsLayout->addSpacing(10);
174 GroupButtonsLayout->addWidget(buttonApply);
175 GroupButtonsLayout->addSpacing(10);
176 GroupButtonsLayout->addStretch();
177 GroupButtonsLayout->addWidget(buttonCancel);
178 GroupButtonsLayout->addWidget(buttonHelp);
180 /***************************************************************/
181 aTopLayout->addWidget(GroupConstructors);
182 aTopLayout->addWidget(GroupName);
183 aTopLayout->addWidget(GroupArgs);
184 aTopLayout->addWidget(GroupButtons);
186 myHelpFileName = "building_compounds_page.html";
188 Init(); // Initialisations
191 //=================================================================================
192 // function : ~SMESHGUI_BuildCompoundDlg()
193 // purpose : Destroys the object and frees any allocated resources
194 //=================================================================================
195 SMESHGUI_BuildCompoundDlg::~SMESHGUI_BuildCompoundDlg()
199 //=================================================================================
202 //=================================================================================
203 void SMESHGUI_BuildCompoundDlg::Init()
205 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
207 myMesh = SMESH::SMESH_Mesh::_nil();
209 myMeshFilter = new SMESH_TypeFilter (MESH);
211 myMeshArray = new SMESH::mesh_array();
213 // signals and slots connections
214 connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
215 connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
216 connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
217 connect(buttonHelp, SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
219 connect(SelectButton, SIGNAL(clicked()), this, SLOT(SelectionIntoArgument()));
221 connect(CheckBoxMerge, SIGNAL(toggled(bool)), this, SLOT(onSelectMerge(bool)));
223 connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
225 connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
226 connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
228 LineEditName->setText(GetDefaultName(tr("COMPOUND_MESH")));
229 LineEditMeshes->setFocus();
231 ComboBoxUnion->addItem(tr("UNITE"));
232 ComboBoxUnion->addItem(tr("RENAME"));
233 ComboBoxUnion->setCurrentIndex(0);
235 CheckBoxMerge->setChecked(false);
237 TextLabelTol->setEnabled(CheckBoxMerge->isChecked());
238 SpinBoxTol->SetValue(1e-05);
240 SpinBoxTol->setEnabled(CheckBoxMerge->isChecked());
242 mySelectionMgr->clearFilters();
243 mySelectionMgr->installFilter(myMeshFilter);
245 SelectionIntoArgument();
248 //=================================================================================
249 // function : GetDefaultName()
251 //=================================================================================
252 QString SMESHGUI_BuildCompoundDlg::GetDefaultName(const QString& theOperation)
256 // collect all object names of SMESH component
257 SalomeApp_Study* appStudy =
258 dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
259 if ( !appStudy ) return aName;
260 _PTR(Study) aStudy = appStudy->studyDS();
262 std::set<std::string> aSet;
263 _PTR(SComponent) aMeshCompo (aStudy->FindComponent("SMESH"));
265 _PTR(ChildIterator) it (aStudy->NewChildIterator(aMeshCompo));
267 for (it->InitEx(true); it->More(); it->Next()) {
269 aSet.insert(obj->GetName());
273 // build a unique name
275 bool isUnique = false;
277 aName = theOperation + "_" + QString::number(++aNumber);
278 isUnique = (aSet.count(aName.toLatin1().data()) == 0);
284 //=================================================================================
285 // function : ClickOnApply()
287 //=================================================================================
288 bool SMESHGUI_BuildCompoundDlg::ClickOnApply()
290 if (mySMESHGUI->isActiveStudyLocked())
296 SMESH::SMESH_Mesh_var aCompoundMesh;
298 if (!myMesh->_is_nil()) {
299 QStringList aParameters;
300 aParameters << (CheckBoxMerge->isChecked() ? SpinBoxTol->text() : QString(" "));
302 SUIT_OverrideCursor aWaitCursor;
304 SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
305 // concatenate meshes
306 if(CheckBoxCommon->isChecked())
307 aCompoundMesh = aSMESHGen->ConcatenateWithGroups(myMeshArray,
308 !(ComboBoxUnion->currentIndex()),
309 CheckBoxMerge->isChecked(),
310 SpinBoxTol->GetValue());
312 aCompoundMesh = aSMESHGen->Concatenate(myMeshArray,
313 !(ComboBoxUnion->currentIndex()),
314 CheckBoxMerge->isChecked(),
315 SpinBoxTol->GetValue());
317 aCompoundMesh->SetParameters( aParameters.join(":").toLatin1().constData() );
319 SMESH::SetName( SMESH::FindSObject( aCompoundMesh ), LineEditName->text() );
320 mySMESHGUI->updateObjBrowser();
325 LineEditName->setText(GetDefaultName(tr("COMPOUND_MESH")));
327 // IPAL21468 Compound is hidden after creation.
328 if ( SMESHGUI::automaticUpdate() ) {
329 mySelectionMgr->clearSelected();
332 _PTR(SObject) aSO = SMESH::FindSObject(aCompoundMesh.in());
333 if ( SMESH_Actor* anActor = SMESH::CreateActor(aSO->GetStudy(), aSO->GetID().c_str()) )
334 SMESH::DisplayActor(SMESH::GetActiveWindow(), anActor);
337 SMESHGUI::Modified();
339 #ifdef WITHGENERICOBJ
340 // obj has been published in study. Its refcount has been incremented.
341 // It is safe to decrement its refcount
342 // so that it will be destroyed when the entry in study will be removed
343 if (!CORBA::is_nil(aCompoundMesh))
344 aCompoundMesh->Destroy();
352 //=================================================================================
353 // function : ClickOnOk()
355 //=================================================================================
356 void SMESHGUI_BuildCompoundDlg::ClickOnOk()
362 //=================================================================================
363 // function : ClickOnCancel()
365 //=================================================================================
366 void SMESHGUI_BuildCompoundDlg::ClickOnCancel()
368 //mySelectionMgr->clearSelected();
369 mySelectionMgr->clearFilters();
370 disconnect(mySelectionMgr, 0, this, 0);
371 mySMESHGUI->ResetState();
375 //=================================================================================
376 // function : ClickOnHelp()
378 //=================================================================================
379 void SMESHGUI_BuildCompoundDlg::ClickOnHelp()
381 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
383 app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
385 SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
386 tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
387 arg(app->resourceMgr()->stringValue("ExternalBrowser",
389 arg(myHelpFileName));
393 //=================================================================================
394 // function : SelectionIntoArgument()
395 // purpose : Called when selection as changed or other case
396 //=================================================================================
397 void SMESHGUI_BuildCompoundDlg::SelectionIntoArgument()
399 if (!GroupButtons->isEnabled()) // inactive
402 QString aString = "";
405 mySelectionMgr->selectedObjects(aList);
406 int nbSel = SMESH::GetNameOfSelectedIObjects(mySelectionMgr, aString);
409 myMeshArray->length(nbSel);
410 for (int i = 0; nbSel != 0; i++, nbSel--) {
411 Handle(SALOME_InteractiveObject) IO = aList.First();
413 myMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(IO);
414 myMeshArray[i] = myMesh;
418 myMesh = SMESH::SMESH_Mesh::_nil();
422 LineEditMeshes->setText(aString);
424 bool isEnabled = (!myMesh->_is_nil());
425 buttonOk->setEnabled(isEnabled);
426 buttonApply->setEnabled(isEnabled);
429 //=================================================================================
430 // function : DeactivateActiveDialog()
432 //=================================================================================
433 void SMESHGUI_BuildCompoundDlg::DeactivateActiveDialog()
435 if (GroupConstructors->isEnabled()) {
436 GroupConstructors->setEnabled(false);
437 GroupName->setEnabled(false);
438 GroupArgs->setEnabled(false);
439 GroupButtons->setEnabled(false);
440 mySMESHGUI->ResetState();
441 mySMESHGUI->SetActiveDialogBox(0);
445 //=================================================================================
446 // function : ActivateThisDialog()
448 //=================================================================================
449 void SMESHGUI_BuildCompoundDlg::ActivateThisDialog()
451 /* Emit a signal to deactivate the active dialog */
452 mySMESHGUI->EmitSignalDeactivateDialog();
453 GroupConstructors->setEnabled(true);
454 GroupName->setEnabled(true);
455 GroupArgs->setEnabled(true);
456 GroupButtons->setEnabled(true);
458 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
459 SelectionIntoArgument();
462 //=================================================================================
463 // function : enterEvent()
465 //=================================================================================
466 void SMESHGUI_BuildCompoundDlg::enterEvent( QEvent* )
468 if (GroupConstructors->isEnabled())
470 ActivateThisDialog();
473 //=================================================================================
474 // function : closeEvent()
476 //=================================================================================
477 void SMESHGUI_BuildCompoundDlg::closeEvent( QCloseEvent* )
479 /* same than click on cancel button */
483 //=======================================================================
484 //function : hideEvent
485 //purpose : caused by ESC key
486 //=======================================================================
487 void SMESHGUI_BuildCompoundDlg::hideEvent( QHideEvent* )
494 //=================================================================================
495 // function : keyPressEvent()
497 //=================================================================================
498 void SMESHGUI_BuildCompoundDlg::keyPressEvent( QKeyEvent* e )
500 QDialog::keyPressEvent( e );
501 if ( e->isAccepted() )
504 if ( e->key() == Qt::Key_F1 ) {
511 //=================================================================================
512 // function : onSelectMerge()
514 //=================================================================================
515 void SMESHGUI_BuildCompoundDlg::onSelectMerge(bool toMerge)
517 TextLabelTol->setEnabled(toMerge);
518 SpinBoxTol->setEnabled(toMerge);
520 SpinBoxTol->SetValue(1e-05);
523 //=================================================================================
524 // function : isValid
526 //=================================================================================
527 bool SMESHGUI_BuildCompoundDlg::isValid()
531 if(CheckBoxMerge->isChecked())
532 ok = SpinBoxTol->isValid( msg, true );
535 QString str( tr( "SMESH_INCORRECT_INPUT" ) );
536 if ( !msg.isEmpty() )
538 SUIT_MessageBox::critical( this, tr( "SMESH_ERROR" ), str );