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)),
81 myIsApplyAndClose( false )
84 setAttribute(Qt::WA_DeleteOnClose, true);
85 setWindowTitle(tr("SMESH_BUILD_COMPOUND_TITLE"));
87 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
88 QPixmap image0 (aResMgr->loadPixmap("SMESH", tr("ICON_DLG_BUILD_COMPOUND_MESH")));
89 QPixmap image1 (aResMgr->loadPixmap("SMESH", tr("ICON_SELECT")));
91 setSizeGripEnabled(true);
93 QVBoxLayout* aTopLayout = new QVBoxLayout(this);
94 aTopLayout->setSpacing(SPACING);
95 aTopLayout->setMargin(MARGIN);
97 /***************************************************************/
98 GroupConstructors = new QGroupBox(tr("COMPOUND"), this);
99 QButtonGroup* ButtonGroup = new QButtonGroup(this);
100 QHBoxLayout* GroupConstructorsLayout = new QHBoxLayout(GroupConstructors);
101 GroupConstructorsLayout->setSpacing(SPACING);
102 GroupConstructorsLayout->setMargin(MARGIN);
104 Constructor1 = new QRadioButton(GroupConstructors);
105 Constructor1->setIcon(image0);
106 Constructor1->setChecked(true);
107 GroupConstructorsLayout->addWidget(Constructor1);
108 ButtonGroup->addButton(Constructor1, 0);
110 /***************************************************************/
111 GroupName = new QGroupBox(tr("RESULT_NAME"), this);
112 QHBoxLayout* GroupNameLayout = new QHBoxLayout(GroupName);
113 GroupNameLayout->setSpacing(SPACING);
114 GroupNameLayout->setMargin(MARGIN);
116 TextLabelName = new QLabel(tr("SMESH_NAME"), GroupName);
117 LineEditName = new QLineEdit(GroupName);
119 GroupNameLayout->addWidget(TextLabelName);
120 GroupNameLayout->addWidget(LineEditName);
122 /***************************************************************/
123 GroupArgs = new QGroupBox(tr("SMESH_ARGUMENTS"), this);
124 QGridLayout* GroupArgsLayout = new QGridLayout(GroupArgs);
125 GroupArgsLayout->setSpacing(SPACING);
126 GroupArgsLayout->setMargin(MARGIN);
128 TextLabelMeshes = new QLabel(tr("MESHES"), GroupArgs);
129 SelectButton = new QPushButton(GroupArgs);
130 SelectButton->setIcon(image1);
131 SelectButton->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
132 LineEditMeshes = new QLineEdit(GroupArgs);
133 LineEditMeshes->setReadOnly(true);
135 TextLabelUnion = new QLabel(tr("PROCESSING_IDENTICAL_GROUPS"), GroupArgs);
136 ComboBoxUnion = new QComboBox(GroupArgs);
138 CheckBoxCommon = new QCheckBox(tr("CREATE_COMMON_GROUPS"), GroupArgs);
140 CheckBoxMerge = new QCheckBox(tr("MERGE_NODES_AND_ELEMENTS"), GroupArgs);
142 TextLabelTol = new QLabel(tr("SMESH_TOLERANCE"), GroupArgs);
143 TextLabelTol->setAlignment(Qt::AlignCenter);
144 SpinBoxTol = new SMESHGUI_SpinBox(GroupArgs);
145 SpinBoxTol->RangeStepAndValidator(0.0, COORD_MAX, 0.00001, "len_tol_precision" );
147 GroupArgsLayout->addWidget(TextLabelMeshes, 0, 0);
148 GroupArgsLayout->addWidget(SelectButton, 0, 1);
149 GroupArgsLayout->addWidget(LineEditMeshes, 0, 2, 1, 2);
150 GroupArgsLayout->addWidget(TextLabelUnion, 1, 0, 1, 3);
151 GroupArgsLayout->addWidget(ComboBoxUnion, 1, 3);
152 GroupArgsLayout->addWidget(CheckBoxCommon, 2, 0, 1, 4);
153 GroupArgsLayout->addWidget(CheckBoxMerge, 3, 0, 1, 4);
154 GroupArgsLayout->addWidget(TextLabelTol, 4, 0, 1, 2);
155 GroupArgsLayout->addWidget(SpinBoxTol, 4, 2, 1, 2);
157 /***************************************************************/
158 GroupButtons = new QGroupBox(this);
159 QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
160 GroupButtonsLayout->setSpacing(SPACING);
161 GroupButtonsLayout->setMargin(MARGIN);
163 buttonOk = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), GroupButtons);
164 buttonOk->setAutoDefault(true);
165 buttonOk->setDefault(true);
166 buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
167 buttonApply->setAutoDefault(true);
168 buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
169 buttonCancel->setAutoDefault(true);
170 buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
171 buttonHelp->setAutoDefault(true);
173 GroupButtonsLayout->addWidget(buttonOk);
174 GroupButtonsLayout->addSpacing(10);
175 GroupButtonsLayout->addWidget(buttonApply);
176 GroupButtonsLayout->addSpacing(10);
177 GroupButtonsLayout->addStretch();
178 GroupButtonsLayout->addWidget(buttonCancel);
179 GroupButtonsLayout->addWidget(buttonHelp);
181 /***************************************************************/
182 aTopLayout->addWidget(GroupConstructors);
183 aTopLayout->addWidget(GroupName);
184 aTopLayout->addWidget(GroupArgs);
185 aTopLayout->addWidget(GroupButtons);
187 myHelpFileName = "building_compounds_page.html";
189 Init(); // Initialisations
192 //=================================================================================
193 // function : ~SMESHGUI_BuildCompoundDlg()
194 // purpose : Destroys the object and frees any allocated resources
195 //=================================================================================
196 SMESHGUI_BuildCompoundDlg::~SMESHGUI_BuildCompoundDlg()
200 //=================================================================================
203 //=================================================================================
204 void SMESHGUI_BuildCompoundDlg::Init()
206 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
208 myMesh = SMESH::SMESH_Mesh::_nil();
210 myMeshFilter = new SMESH_TypeFilter (MESH);
212 myMeshArray = new SMESH::mesh_array();
214 // signals and slots connections
215 connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
216 connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
217 connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
218 connect(buttonHelp, SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
220 connect(SelectButton, SIGNAL(clicked()), this, SLOT(SelectionIntoArgument()));
222 connect(CheckBoxMerge, SIGNAL(toggled(bool)), this, SLOT(onSelectMerge(bool)));
224 connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
226 connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
227 connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
229 LineEditName->setText(GetDefaultName(tr("COMPOUND_MESH")));
230 LineEditMeshes->setFocus();
232 ComboBoxUnion->addItem(tr("UNITE"));
233 ComboBoxUnion->addItem(tr("RENAME"));
234 ComboBoxUnion->setCurrentIndex(0);
236 CheckBoxMerge->setChecked(false);
238 TextLabelTol->setEnabled(CheckBoxMerge->isChecked());
239 SpinBoxTol->SetValue(1e-05);
241 SpinBoxTol->setEnabled(CheckBoxMerge->isChecked());
243 mySelectionMgr->clearFilters();
244 mySelectionMgr->installFilter(myMeshFilter);
246 SelectionIntoArgument();
249 //=================================================================================
250 // function : GetDefaultName()
252 //=================================================================================
253 QString SMESHGUI_BuildCompoundDlg::GetDefaultName(const QString& theOperation)
257 // collect all object names of SMESH component
258 SalomeApp_Study* appStudy =
259 dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
260 if ( !appStudy ) return aName;
261 _PTR(Study) aStudy = appStudy->studyDS();
263 std::set<std::string> aSet;
264 _PTR(SComponent) aMeshCompo (aStudy->FindComponent("SMESH"));
266 _PTR(ChildIterator) it (aStudy->NewChildIterator(aMeshCompo));
268 for (it->InitEx(true); it->More(); it->Next()) {
270 aSet.insert(obj->GetName());
274 // build a unique name
276 bool isUnique = false;
278 aName = theOperation + "_" + QString::number(++aNumber);
279 isUnique = (aSet.count(aName.toLatin1().data()) == 0);
285 //=================================================================================
286 // function : ClickOnApply()
288 //=================================================================================
289 bool SMESHGUI_BuildCompoundDlg::ClickOnApply()
291 if (mySMESHGUI->isActiveStudyLocked())
297 SMESH::SMESH_Mesh_var aCompoundMesh;
299 if (!myMesh->_is_nil()) {
300 QStringList aParameters;
301 aParameters << (CheckBoxMerge->isChecked() ? SpinBoxTol->text() : QString(" "));
302 QStringList anEntryList;
304 SUIT_OverrideCursor aWaitCursor;
306 SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
307 // concatenate meshes
308 if(CheckBoxCommon->isChecked())
309 aCompoundMesh = aSMESHGen->ConcatenateWithGroups(myMeshArray,
310 !(ComboBoxUnion->currentIndex()),
311 CheckBoxMerge->isChecked(),
312 SpinBoxTol->GetValue());
314 aCompoundMesh = aSMESHGen->Concatenate(myMeshArray,
315 !(ComboBoxUnion->currentIndex()),
316 CheckBoxMerge->isChecked(),
317 SpinBoxTol->GetValue());
319 aCompoundMesh->SetParameters( aParameters.join(":").toLatin1().constData() );
321 _PTR(SObject) aSO = SMESH::FindSObject( aCompoundMesh );
323 SMESH::SetName( aSO, LineEditName->text() );
324 anEntryList.append( aSO->GetID().c_str() );
326 mySMESHGUI->updateObjBrowser();
331 LineEditName->setText(GetDefaultName(tr("COMPOUND_MESH")));
333 // IPAL21468 Compound is hidden after creation.
334 if ( SMESHGUI::automaticUpdate() ) {
335 mySelectionMgr->clearSelected();
338 _PTR(SObject) aSO = SMESH::FindSObject(aCompoundMesh.in());
339 if ( SMESH_Actor* anActor = SMESH::CreateActor(aSO->GetStudy(), aSO->GetID().c_str()) )
340 SMESH::DisplayActor(SMESH::GetActiveWindow(), anActor);
343 if( LightApp_Application* anApp =
344 dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
345 anApp->browseObjects( anEntryList, isApplyAndClose() );
347 SMESHGUI::Modified();
349 #ifdef WITHGENERICOBJ
350 // obj has been published in study. Its refcount has been incremented.
351 // It is safe to decrement its refcount
352 // so that it will be destroyed when the entry in study will be removed
353 if (!CORBA::is_nil(aCompoundMesh))
354 aCompoundMesh->UnRegister();
362 //=================================================================================
363 // function : ClickOnOk()
365 //=================================================================================
366 void SMESHGUI_BuildCompoundDlg::ClickOnOk()
368 setIsApplyAndClose( true );
373 //=================================================================================
374 // function : ClickOnCancel()
376 //=================================================================================
377 void SMESHGUI_BuildCompoundDlg::ClickOnCancel()
379 //mySelectionMgr->clearSelected();
380 mySelectionMgr->clearFilters();
381 disconnect(mySelectionMgr, 0, this, 0);
382 mySMESHGUI->ResetState();
386 //=================================================================================
387 // function : ClickOnHelp()
389 //=================================================================================
390 void SMESHGUI_BuildCompoundDlg::ClickOnHelp()
392 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
394 app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
396 SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
397 tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
398 arg(app->resourceMgr()->stringValue("ExternalBrowser",
400 arg(myHelpFileName));
404 //=================================================================================
405 // function : SelectionIntoArgument()
406 // purpose : Called when selection as changed or other case
407 //=================================================================================
408 void SMESHGUI_BuildCompoundDlg::SelectionIntoArgument()
410 if (!GroupButtons->isEnabled()) // inactive
413 QString aString = "";
416 mySelectionMgr->selectedObjects(aList);
417 int nbSel = SMESH::GetNameOfSelectedIObjects(mySelectionMgr, aString);
420 myMeshArray->length(nbSel);
421 for (int i = 0; nbSel != 0; i++, nbSel--) {
422 Handle(SALOME_InteractiveObject) IO = aList.First();
424 myMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(IO);
425 myMeshArray[i] = myMesh;
429 myMesh = SMESH::SMESH_Mesh::_nil();
433 LineEditMeshes->setText(aString);
435 bool isEnabled = (!myMesh->_is_nil());
436 buttonOk->setEnabled(isEnabled);
437 buttonApply->setEnabled(isEnabled);
440 //=================================================================================
441 // function : DeactivateActiveDialog()
443 //=================================================================================
444 void SMESHGUI_BuildCompoundDlg::DeactivateActiveDialog()
446 if (GroupConstructors->isEnabled()) {
447 GroupConstructors->setEnabled(false);
448 GroupName->setEnabled(false);
449 GroupArgs->setEnabled(false);
450 GroupButtons->setEnabled(false);
451 mySMESHGUI->ResetState();
452 mySMESHGUI->SetActiveDialogBox(0);
456 //=================================================================================
457 // function : ActivateThisDialog()
459 //=================================================================================
460 void SMESHGUI_BuildCompoundDlg::ActivateThisDialog()
462 /* Emit a signal to deactivate the active dialog */
463 mySMESHGUI->EmitSignalDeactivateDialog();
464 GroupConstructors->setEnabled(true);
465 GroupName->setEnabled(true);
466 GroupArgs->setEnabled(true);
467 GroupButtons->setEnabled(true);
469 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
470 SelectionIntoArgument();
473 //=================================================================================
474 // function : enterEvent()
476 //=================================================================================
477 void SMESHGUI_BuildCompoundDlg::enterEvent( QEvent* )
479 if (GroupConstructors->isEnabled())
481 ActivateThisDialog();
484 //=================================================================================
485 // function : closeEvent()
487 //=================================================================================
488 void SMESHGUI_BuildCompoundDlg::closeEvent( QCloseEvent* )
490 /* same than click on cancel button */
494 //=======================================================================
495 //function : hideEvent
496 //purpose : caused by ESC key
497 //=======================================================================
498 void SMESHGUI_BuildCompoundDlg::hideEvent( QHideEvent* )
505 //=================================================================================
506 // function : keyPressEvent()
508 //=================================================================================
509 void SMESHGUI_BuildCompoundDlg::keyPressEvent( QKeyEvent* e )
511 QDialog::keyPressEvent( e );
512 if ( e->isAccepted() )
515 if ( e->key() == Qt::Key_F1 ) {
522 //=================================================================================
523 // function : onSelectMerge()
525 //=================================================================================
526 void SMESHGUI_BuildCompoundDlg::onSelectMerge(bool toMerge)
528 TextLabelTol->setEnabled(toMerge);
529 SpinBoxTol->setEnabled(toMerge);
531 SpinBoxTol->SetValue(1e-05);
534 //=================================================================================
535 // function : isValid
537 //=================================================================================
538 bool SMESHGUI_BuildCompoundDlg::isValid()
542 if(CheckBoxMerge->isChecked())
543 ok = SpinBoxTol->isValid( msg, true );
546 QString str( tr( "SMESH_INCORRECT_INPUT" ) );
547 if ( !msg.isEmpty() )
549 SUIT_MessageBox::critical( this, tr( "SMESH_ERROR" ), str );
555 //================================================================
556 // function : setIsApplyAndClose
557 // Purpose : Set value of the flag indicating that the dialog is
558 // accepted by Apply & Close button
559 //================================================================
560 void SMESHGUI_BuildCompoundDlg::setIsApplyAndClose( const bool theFlag )
562 myIsApplyAndClose = theFlag;
565 //================================================================
566 // function : isApplyAndClose
567 // Purpose : Get value of the flag indicating that the dialog is
568 // accepted by Apply & Close button
569 //================================================================
570 bool SMESHGUI_BuildCompoundDlg::isApplyAndClose() const
572 return myIsApplyAndClose;