1 // Copyright (C) 2007-2022 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, or (at your option) any later version.
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 enum { NEW_MESH_ID, APPEND_TO_ID };
70 //=================================================================================
71 // name : SMESHGUI_BuildCompoundDlg
73 //=================================================================================
74 SMESHGUI_BuildCompoundDlg::SMESHGUI_BuildCompoundDlg( SMESHGUI* theModule )
75 : QDialog(SMESH::GetDesktop(theModule)),
76 mySMESHGUI(theModule),
77 mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
78 myIsApplyAndClose( false )
81 setAttribute(Qt::WA_DeleteOnClose, true);
82 setWindowTitle(tr("SMESH_BUILD_COMPOUND_TITLE"));
84 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
85 QPixmap image0 (aResMgr->loadPixmap("SMESH", tr("ICON_DLG_BUILD_COMPOUND_MESH")));
86 QPixmap image1 (aResMgr->loadPixmap("SMESH", tr("ICON_SELECT")));
88 setSizeGripEnabled(true);
90 QVBoxLayout* aTopLayout = new QVBoxLayout(this);
91 aTopLayout->setSpacing(SPACING);
92 aTopLayout->setMargin(MARGIN);
94 /***************************************************************/
95 GroupConstructors = new QGroupBox(tr("COMPOUND"), this);
96 QButtonGroup* ButtonGroup = new QButtonGroup(this);
97 QHBoxLayout* GroupConstructorsLayout = new QHBoxLayout(GroupConstructors);
98 GroupConstructorsLayout->setSpacing(SPACING);
99 GroupConstructorsLayout->setMargin(MARGIN);
101 Constructor1 = new QRadioButton(GroupConstructors);
102 Constructor1->setIcon(image0);
103 Constructor1->setChecked(true);
104 GroupConstructorsLayout->addWidget(Constructor1);
105 ButtonGroup->addButton(Constructor1, 0);
107 /***************************************************************/
108 GroupResult = new QGroupBox(tr("RESULT_NAME"), this);
109 QGridLayout* GroupResultLayout = new QGridLayout(GroupResult);
110 GroupResultLayout->setSpacing(SPACING);
111 GroupResultLayout->setMargin(MARGIN);
113 QRadioButton* newMeshRadioBtn = new QRadioButton( tr("NEW_MESH_NAME"), GroupResult );
114 QRadioButton* appendToRadioBtn = new QRadioButton( tr("MESH_APPEND_TO"), GroupResult );
115 LineEditNewName = new QLineEdit(GroupResult);
116 LineEditAppendTo = new QLineEdit(GroupResult);
117 SelectButtonAppendTo = new QPushButton(GroupResult);
118 SelectButtonAppendTo->setIcon(image1);
119 SelectButtonAppendTo->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
120 ResultButtonGroup = new QButtonGroup( GroupResult );
121 ResultButtonGroup->addButton( newMeshRadioBtn, NEW_MESH_ID );
122 ResultButtonGroup->addButton( appendToRadioBtn, APPEND_TO_ID );
123 newMeshRadioBtn->setChecked( true );
125 GroupResultLayout->addWidget( newMeshRadioBtn, 0, 0, 1, 2 );
126 GroupResultLayout->addWidget( LineEditNewName, 0, 2 );
127 GroupResultLayout->addWidget( appendToRadioBtn, 1, 0 );
128 GroupResultLayout->addWidget( SelectButtonAppendTo, 1, 1 );
129 GroupResultLayout->addWidget( LineEditAppendTo, 1, 2 );
131 /***************************************************************/
132 GroupArgs = new QGroupBox(tr("SMESH_ARGUMENTS"), this);
133 QGridLayout* GroupArgsLayout = new QGridLayout(GroupArgs);
134 GroupArgsLayout->setSpacing(SPACING);
135 GroupArgsLayout->setMargin(MARGIN);
137 TextLabelMeshes = new QLabel(tr("MESHES"), GroupArgs);
138 SelectButton = new QPushButton(GroupArgs);
139 SelectButton->setIcon(image1);
140 SelectButton->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
141 LineEditMeshes = new QLineEdit(GroupArgs);
142 LineEditMeshes->setReadOnly(true);
144 TextLabelUnion = new QLabel(tr("PROCESSING_IDENTICAL_GROUPS"), GroupArgs);
145 ComboBoxUnion = new QComboBox(GroupArgs);
147 CheckBoxCommon = new QCheckBox(tr("CREATE_COMMON_GROUPS"), GroupArgs);
149 CheckBoxMerge = new QCheckBox(tr("MERGE_NODES_AND_ELEMENTS"), GroupArgs);
151 TextLabelTol = new QLabel(tr("SMESH_TOLERANCE"), GroupArgs);
152 //TextLabelTol->setAlignment(Qt::AlignCenter);
153 SpinBoxTol = new SMESHGUI_SpinBox(GroupArgs);
154 SpinBoxTol->RangeStepAndValidator(0.0, COORD_MAX, 0.00001, "len_tol_precision" );
156 GroupArgsLayout->addWidget(TextLabelMeshes, 0, 0);
157 GroupArgsLayout->addWidget(SelectButton, 0, 1);
158 GroupArgsLayout->addWidget(LineEditMeshes, 0, 2, 1, 2);
159 GroupArgsLayout->addWidget(TextLabelUnion, 1, 0, 1, 3);
160 GroupArgsLayout->addWidget(ComboBoxUnion, 1, 3);
161 GroupArgsLayout->addWidget(CheckBoxCommon, 2, 0, 1, 4);
162 GroupArgsLayout->addWidget(CheckBoxMerge, 3, 0, 1, 4);
163 GroupArgsLayout->addWidget(TextLabelTol, 4, 0);
164 GroupArgsLayout->addWidget(SpinBoxTol, 4, 1, 1, 3);
166 /***************************************************************/
167 GroupButtons = new QGroupBox(this);
168 QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
169 GroupButtonsLayout->setSpacing(SPACING);
170 GroupButtonsLayout->setMargin(MARGIN);
172 buttonOk = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), GroupButtons);
173 buttonOk->setAutoDefault(true);
174 buttonOk->setDefault(true);
175 buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
176 buttonApply->setAutoDefault(true);
177 buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
178 buttonCancel->setAutoDefault(true);
179 buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
180 buttonHelp->setAutoDefault(true);
182 GroupButtonsLayout->addWidget(buttonOk);
183 GroupButtonsLayout->addSpacing(10);
184 GroupButtonsLayout->addWidget(buttonApply);
185 GroupButtonsLayout->addSpacing(10);
186 GroupButtonsLayout->addStretch();
187 GroupButtonsLayout->addWidget(buttonCancel);
188 GroupButtonsLayout->addWidget(buttonHelp);
190 /***************************************************************/
191 aTopLayout->addWidget(GroupConstructors);
192 aTopLayout->addWidget(GroupResult);
193 aTopLayout->addWidget(GroupArgs);
194 aTopLayout->addWidget(GroupButtons);
196 myHelpFileName = "building_compounds.html";
198 Init(); // Initialisations
201 //=================================================================================
202 // function : ~SMESHGUI_BuildCompoundDlg()
203 // purpose : Destroys the object and frees any allocated resources
204 //=================================================================================
205 SMESHGUI_BuildCompoundDlg::~SMESHGUI_BuildCompoundDlg()
209 //=================================================================================
212 //=================================================================================
213 void SMESHGUI_BuildCompoundDlg::Init()
215 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
217 myMeshToAppendTo = SMESH::SMESH_Mesh::_nil();
218 myMeshArray = new SMESH::ListOfIDSources();
220 myMeshFilter = new SMESH_TypeFilter (SMESH::IDSOURCE);
221 myAppendToFilter = new SMESH_TypeFilter (SMESH::MESH);
223 // signals and slots connections
224 connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
225 connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
226 connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
227 connect(buttonHelp, SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
229 connect(ResultButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(onResultTypeChange(int)));
231 connect(SelectButtonAppendTo, SIGNAL(clicked()), this, SLOT(onSelectionButton()));
232 connect(SelectButton, SIGNAL(clicked()), this, SLOT(onSelectionButton()));
234 connect(CheckBoxMerge, SIGNAL(toggled(bool)), this, SLOT(onSelectMerge(bool)));
236 connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
238 connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
239 connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(reject()));
241 LineEditNewName->setText(GetDefaultName(tr("COMPOUND_MESH")));
243 ComboBoxUnion->addItem(tr("UNITE"));
244 ComboBoxUnion->addItem(tr("RENAME"));
245 ComboBoxUnion->setCurrentIndex(0);
247 CheckBoxMerge->setChecked(true);
249 TextLabelTol->setEnabled(CheckBoxMerge->isChecked());
250 SpinBoxTol->SetValue(1e-05);
252 SpinBoxTol->setEnabled(CheckBoxMerge->isChecked());
254 onResultTypeChange( ResultButtonGroup->checkedId() );
259 //=================================================================================
260 // function : GetDefaultName()
262 //=================================================================================
263 QString SMESHGUI_BuildCompoundDlg::GetDefaultName(const QString& theOperation)
267 // collect all object names of SMESH component
268 _PTR(Study) aStudy = SMESH::getStudy();
270 std::set<std::string> aSet;
271 _PTR(SComponent) aMeshCompo (aStudy->FindComponent("SMESH"));
273 _PTR(ChildIterator) it (aStudy->NewChildIterator(aMeshCompo));
275 for (it->InitEx(true); it->More(); it->Next()) {
277 aSet.insert(obj->GetName());
281 // build a unique name
283 bool isUnique = false;
285 aName = theOperation + "_" + QString::number(++aNumber);
286 isUnique = (aSet.count(aName.toUtf8().data()) == 0);
292 //=================================================================================
293 // function : ClickOnApply()
295 //=================================================================================
296 bool SMESHGUI_BuildCompoundDlg::ClickOnApply()
298 if (SMESHGUI::isStudyLocked())
304 SUIT_OverrideCursor aWaitCursor;
306 SMESH::SMESH_Mesh_var aMesh;
308 int nbMeshes = myMeshArray->length() + ( !myMeshToAppendTo->_is_nil() );
312 QStringList aParameters;
313 aParameters << (CheckBoxMerge->isChecked() ? SpinBoxTol->text() : QString(" "));
315 QStringList anEntryList;
318 aMesh = myMeshArray[0]->GetMesh();
319 aMesh->SetParameters( aParameters.join(":").toUtf8().constData() );
321 SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
322 // concatenate meshes
323 if(CheckBoxCommon->isChecked())
324 aMesh = aSMESHGen->ConcatenateWithGroups(myMeshArray,
325 !(ComboBoxUnion->currentIndex()),
326 CheckBoxMerge->isChecked(),
327 SpinBoxTol->GetValue(),
330 aMesh = aSMESHGen->Concatenate(myMeshArray,
331 !(ComboBoxUnion->currentIndex()),
332 CheckBoxMerge->isChecked(),
333 SpinBoxTol->GetValue(),
336 _PTR(SObject) aSO = SMESH::FindSObject( aMesh );
338 if ( myMeshToAppendTo->_is_nil() )
339 SMESH::SetName( aSO, LineEditNewName->text() );
340 anEntryList.append( aSO->GetID().c_str() );
342 mySMESHGUI->updateObjBrowser();
347 LineEditNewName->setText(GetDefaultName(tr("COMPOUND_MESH")));
349 // IPAL21468 Compound is hidden after creation.
350 if ( SMESHGUI::automaticUpdate() ) {
351 mySelectionMgr->clearSelected();
354 _PTR(SObject) aSO = SMESH::FindSObject(aMesh.in());
355 if ( SMESH_Actor* anActor = SMESH::CreateActor( aSO->GetID().c_str()) ) {
356 SMESH::DisplayActor(SMESH::GetActiveWindow(), anActor);
361 if( LightApp_Application* anApp =
362 dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
363 anApp->browseObjects( anEntryList, isApplyAndClose() );
365 SMESHGUI::Modified();
372 //=================================================================================
373 // function : ClickOnOk()
375 //=================================================================================
376 void SMESHGUI_BuildCompoundDlg::ClickOnOk()
378 setIsApplyAndClose( true );
383 //=================================================================================
384 // function : reject()
386 //=================================================================================
387 void SMESHGUI_BuildCompoundDlg::reject()
389 //mySelectionMgr->clearSelected();
390 mySelectionMgr->clearFilters();
391 disconnect(mySelectionMgr, 0, this, 0);
392 mySMESHGUI->ResetState();
396 //=================================================================================
397 // function : ClickOnHelp()
399 //=================================================================================
400 void SMESHGUI_BuildCompoundDlg::ClickOnHelp()
402 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
404 app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
406 SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
407 tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
408 arg(app->resourceMgr()->stringValue("ExternalBrowser",
410 arg(myHelpFileName));
414 //=================================================================================
415 // function : SelectionIntoArgument()
416 // purpose : Called when selection as changed or other case
417 //=================================================================================
418 void SMESHGUI_BuildCompoundDlg::SelectionIntoArgument()
420 if (!GroupButtons->isEnabled()) // inactive
423 QString aString = "";
426 mySelectionMgr->selectedObjects(aList);
427 int nbSel = SMESH::GetNameOfSelectedIObjects(mySelectionMgr, aString);
429 bool toAppend = ( CurrentLineEdit == LineEditAppendTo );
430 bool isOk = toAppend ? ( nbSel == 1 ) : ( nbSel > 0 );
436 myMeshToAppendTo = SMESH::SMESH_Mesh::_nil();
438 myMeshToAppendTo = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>( aList.First() );
442 myMeshArray->length( nbSel );
443 for ( int i = 0; !aList.IsEmpty(); i++ ) {
444 myMeshArray[i] = SMESH::IObjectToInterface<SMESH::SMESH_IDSource>(aList.First());
448 CurrentLineEdit->setText(aString);
451 if ( ResultButtonGroup->checkedId() == NEW_MESH_ID )
452 isEnabled = ( myMeshArray->length() > 1 );
454 isEnabled = ( myMeshArray->length() > 0 &&
455 !myMeshToAppendTo->_is_nil() &&
456 LineEditAppendTo->text() != LineEditMeshes->text() );
457 buttonOk ->setEnabled( isEnabled );
458 buttonApply->setEnabled( isEnabled );
461 //=================================================================================
462 // function : DeactivateActiveDialog()
464 //=================================================================================
465 void SMESHGUI_BuildCompoundDlg::DeactivateActiveDialog()
467 if (GroupConstructors->isEnabled()) {
468 GroupConstructors->setEnabled(false);
469 GroupResult->setEnabled(false);
470 GroupArgs->setEnabled(false);
471 GroupButtons->setEnabled(false);
472 mySMESHGUI->ResetState();
473 mySMESHGUI->SetActiveDialogBox(0);
477 //=================================================================================
478 // function : ActivateThisDialog()
480 //=================================================================================
481 void SMESHGUI_BuildCompoundDlg::ActivateThisDialog()
483 /* Emit a signal to deactivate the active dialog */
484 mySMESHGUI->EmitSignalDeactivateDialog();
485 GroupConstructors->setEnabled(true);
486 GroupResult->setEnabled(true);
487 GroupArgs->setEnabled(true);
488 GroupButtons->setEnabled(true);
490 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
491 SelectionIntoArgument();
494 //=================================================================================
495 // function : enterEvent()
497 //=================================================================================
498 void SMESHGUI_BuildCompoundDlg::enterEvent( QEvent* )
500 if (GroupConstructors->isEnabled())
502 ActivateThisDialog();
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 : onResultTypeChange
537 //=======================================================================
539 void SMESHGUI_BuildCompoundDlg::onResultTypeChange( int buttonID )
541 LineEditNewName ->setEnabled( buttonID == NEW_MESH_ID );
542 SelectButtonAppendTo->setEnabled( buttonID == APPEND_TO_ID );
543 LineEditAppendTo ->setEnabled( buttonID == APPEND_TO_ID );
545 if ( CurrentLineEdit == LineEditAppendTo && buttonID == NEW_MESH_ID )
546 onSelectionButton(); // to select into myMeshArray
548 if ( buttonID == NEW_MESH_ID )
550 myMeshToAppendTo = SMESH::SMESH_Mesh::_nil();
551 LineEditAppendTo->setText("");
555 // activate selection of myMeshToAppendTo
556 SelectButtonAppendTo->click();
557 LineEditAppendTo->setFocus();
561 //=======================================================================
562 //function : onSelectionButton
564 //=======================================================================
566 void SMESHGUI_BuildCompoundDlg::onSelectionButton()
568 mySelectionMgr->clearFilters();
569 if ( sender() == SelectButtonAppendTo )
571 mySelectionMgr->installFilter( myAppendToFilter );
572 CurrentLineEdit = LineEditAppendTo;
576 mySelectionMgr->installFilter( myMeshFilter );
577 CurrentLineEdit = LineEditMeshes;
579 CurrentLineEdit->setFocus();
581 SelectionIntoArgument();
584 //=================================================================================
585 // function : isValid
587 //=================================================================================
588 bool SMESHGUI_BuildCompoundDlg::isValid()
592 if(CheckBoxMerge->isChecked())
593 ok = SpinBoxTol->isValid( msg, true );
596 QString str( tr( "SMESH_INCORRECT_INPUT" ) );
597 if ( !msg.isEmpty() )
599 SUIT_MessageBox::critical( this, tr( "SMESH_ERROR" ), str );
605 //================================================================
606 // function : setIsApplyAndClose
607 // Purpose : Set value of the flag indicating that the dialog is
608 // accepted by Apply & Close button
609 //================================================================
610 void SMESHGUI_BuildCompoundDlg::setIsApplyAndClose( const bool theFlag )
612 myIsApplyAndClose = theFlag;
615 //================================================================
616 // function : isApplyAndClose
617 // Purpose : Get value of the flag indicating that the dialog is
618 // accepted by Apply & Close button
619 //================================================================
620 bool SMESHGUI_BuildCompoundDlg::isApplyAndClose() const
622 return myIsApplyAndClose;