1 // Copyright (C) 2007-2013 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 // File : SMESHGUI_BuildCompoundDlg.cxx
23 // Author : Alexander KOVALEV, Open CASCADE S.A.S.
26 #include "SMESHGUI_BuildCompoundDlg.h"
29 #include "SMESHGUI_Utils.h"
30 #include "SMESHGUI_SpinBox.h"
31 #include "SMESHGUI_VTKUtils.h"
33 #include <SMESH_TypeFilter.hxx>
35 // SALOME GUI includes
36 #include <SUIT_Desktop.h>
37 #include <SUIT_Session.h>
38 #include <SUIT_MessageBox.h>
39 #include <SUIT_ResourceMgr.h>
40 #include <SalomeApp_Study.h>
41 #include <SUIT_OverrideCursor.h>
43 #include <LightApp_Application.h>
44 #include <LightApp_SelectionMgr.h>
45 #include <SALOME_ListIO.hxx>
48 #include <QApplication>
52 #include <QPushButton>
53 #include <QRadioButton>
54 #include <QHBoxLayout>
55 #include <QVBoxLayout>
56 #include <QGridLayout>
60 #include <QButtonGroup>
68 //To disable automatic genericobj management, the following line should be commented.
69 //Otherwise, it should be uncommented. Refer to KERNEL_SRC/src/SALOMEDSImpl/SALOMEDSImpl_AttributeIOR.cxx
70 #define WITHGENERICOBJ
72 //=================================================================================
73 // name : SMESHGUI_BuildCompoundDlg
75 //=================================================================================
76 SMESHGUI_BuildCompoundDlg::SMESHGUI_BuildCompoundDlg( SMESHGUI* theModule )
77 : QDialog(SMESH::GetDesktop(theModule)),
78 mySMESHGUI(theModule),
79 mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
80 myIsApplyAndClose( false )
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 (SMESH::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(reject()));
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(reject()));
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())
300 QStringList aParameters;
301 aParameters << (CheckBoxMerge->isChecked() ? SpinBoxTol->text() : QString(" "));
303 QStringList anEntryList;
305 SUIT_OverrideCursor aWaitCursor;
307 myMeshArray[0]->SetParameters( aParameters.join(":").toLatin1().constData() );
309 SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
310 // concatenate meshes
311 if(CheckBoxCommon->isChecked())
312 aCompoundMesh = aSMESHGen->ConcatenateWithGroups(myMeshArray,
313 !(ComboBoxUnion->currentIndex()),
314 CheckBoxMerge->isChecked(),
315 SpinBoxTol->GetValue());
317 aCompoundMesh = aSMESHGen->Concatenate(myMeshArray,
318 !(ComboBoxUnion->currentIndex()),
319 CheckBoxMerge->isChecked(),
320 SpinBoxTol->GetValue());
322 _PTR(SObject) aSO = SMESH::FindSObject( aCompoundMesh );
324 SMESH::SetName( aSO, LineEditName->text() );
325 anEntryList.append( aSO->GetID().c_str() );
327 mySMESHGUI->updateObjBrowser();
332 LineEditName->setText(GetDefaultName(tr("COMPOUND_MESH")));
334 // IPAL21468 Compound is hidden after creation.
335 if ( SMESHGUI::automaticUpdate() ) {
336 mySelectionMgr->clearSelected();
339 _PTR(SObject) aSO = SMESH::FindSObject(aCompoundMesh.in());
340 if ( SMESH_Actor* anActor = SMESH::CreateActor(aSO->GetStudy(), aSO->GetID().c_str()) )
341 SMESH::DisplayActor(SMESH::GetActiveWindow(), anActor);
344 if( LightApp_Application* anApp =
345 dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
346 anApp->browseObjects( anEntryList, isApplyAndClose() );
348 SMESHGUI::Modified();
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();
361 //=================================================================================
362 // function : ClickOnOk()
364 //=================================================================================
365 void SMESHGUI_BuildCompoundDlg::ClickOnOk()
367 setIsApplyAndClose( true );
372 //=================================================================================
373 // function : reject()
375 //=================================================================================
376 void SMESHGUI_BuildCompoundDlg::reject()
378 //mySelectionMgr->clearSelected();
379 mySelectionMgr->clearFilters();
380 disconnect(mySelectionMgr, 0, this, 0);
381 mySMESHGUI->ResetState();
385 //=================================================================================
386 // function : ClickOnHelp()
388 //=================================================================================
389 void SMESHGUI_BuildCompoundDlg::ClickOnHelp()
391 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
393 app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
395 SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
396 tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
397 arg(app->resourceMgr()->stringValue("ExternalBrowser",
399 arg(myHelpFileName));
403 //=================================================================================
404 // function : SelectionIntoArgument()
405 // purpose : Called when selection as changed or other case
406 //=================================================================================
407 void SMESHGUI_BuildCompoundDlg::SelectionIntoArgument()
409 if (!GroupButtons->isEnabled()) // inactive
412 QString aString = "";
415 mySelectionMgr->selectedObjects(aList);
416 int nbSel = SMESH::GetNameOfSelectedIObjects(mySelectionMgr, aString);
419 myMeshArray->length(nbSel);
420 for (int i = 0; nbSel != 0; i++, nbSel--) {
421 Handle(SALOME_InteractiveObject) IO = aList.First();
423 myMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(IO);
424 myMeshArray[i] = myMesh;
428 myMesh = SMESH::SMESH_Mesh::_nil();
432 LineEditMeshes->setText(aString);
434 bool isEnabled = (!myMesh->_is_nil());
435 buttonOk->setEnabled(isEnabled);
436 buttonApply->setEnabled(isEnabled);
439 //=================================================================================
440 // function : DeactivateActiveDialog()
442 //=================================================================================
443 void SMESHGUI_BuildCompoundDlg::DeactivateActiveDialog()
445 if (GroupConstructors->isEnabled()) {
446 GroupConstructors->setEnabled(false);
447 GroupName->setEnabled(false);
448 GroupArgs->setEnabled(false);
449 GroupButtons->setEnabled(false);
450 mySMESHGUI->ResetState();
451 mySMESHGUI->SetActiveDialogBox(0);
455 //=================================================================================
456 // function : ActivateThisDialog()
458 //=================================================================================
459 void SMESHGUI_BuildCompoundDlg::ActivateThisDialog()
461 /* Emit a signal to deactivate the active dialog */
462 mySMESHGUI->EmitSignalDeactivateDialog();
463 GroupConstructors->setEnabled(true);
464 GroupName->setEnabled(true);
465 GroupArgs->setEnabled(true);
466 GroupButtons->setEnabled(true);
468 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
469 SelectionIntoArgument();
472 //=================================================================================
473 // function : enterEvent()
475 //=================================================================================
476 void SMESHGUI_BuildCompoundDlg::enterEvent( QEvent* )
478 if (GroupConstructors->isEnabled())
480 ActivateThisDialog();
483 //=================================================================================
484 // function : keyPressEvent()
486 //=================================================================================
487 void SMESHGUI_BuildCompoundDlg::keyPressEvent( QKeyEvent* e )
489 QDialog::keyPressEvent( e );
490 if ( e->isAccepted() )
493 if ( e->key() == Qt::Key_F1 ) {
500 //=================================================================================
501 // function : onSelectMerge()
503 //=================================================================================
504 void SMESHGUI_BuildCompoundDlg::onSelectMerge(bool toMerge)
506 TextLabelTol->setEnabled(toMerge);
507 SpinBoxTol->setEnabled(toMerge);
509 SpinBoxTol->SetValue(1e-05);
512 //=================================================================================
513 // function : isValid
515 //=================================================================================
516 bool SMESHGUI_BuildCompoundDlg::isValid()
520 if(CheckBoxMerge->isChecked())
521 ok = SpinBoxTol->isValid( msg, true );
524 QString str( tr( "SMESH_INCORRECT_INPUT" ) );
525 if ( !msg.isEmpty() )
527 SUIT_MessageBox::critical( this, tr( "SMESH_ERROR" ), str );
533 //================================================================
534 // function : setIsApplyAndClose
535 // Purpose : Set value of the flag indicating that the dialog is
536 // accepted by Apply & Close button
537 //================================================================
538 void SMESHGUI_BuildCompoundDlg::setIsApplyAndClose( const bool theFlag )
540 myIsApplyAndClose = theFlag;
543 //================================================================
544 // function : isApplyAndClose
545 // Purpose : Get value of the flag indicating that the dialog is
546 // accepted by Apply & Close button
547 //================================================================
548 bool SMESHGUI_BuildCompoundDlg::isApplyAndClose() const
550 return myIsApplyAndClose;