]> SALOME platform Git repositories - modules/smesh.git/blob - src/SMESHGUI/SMESHGUI_BuildCompoundDlg.cxx
Salome HOME
Dump Puthon extension
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_BuildCompoundDlg.cxx
1 // SMESH SMESHGUI : GUI for SMESH component
2 //
3 // Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 //
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. 
10 //
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. 
15 //
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 
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File   : SMESHGUI_BuildCompoundDlg.cxx
23 // Author : Alexander KOVALEV, Open CASCADE S.A.S.
24 //
25
26 // SMESH includes
27 #include "SMESHGUI_BuildCompoundDlg.h"
28
29 #include "SMESHGUI.h"
30 #include "SMESHGUI_Utils.h"
31 #include "SMESHGUI_SpinBox.h"
32 #include "SMESHGUI_VTKUtils.h"
33
34 #include <SMESH_TypeFilter.hxx>
35
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>
43
44 #include <LightApp_Application.h>
45 #include <LightApp_SelectionMgr.h>
46 #include <SALOME_ListIO.hxx>
47
48 // Qt includes
49 #include <QApplication>
50 #include <QGroupBox>
51 #include <QLabel>
52 #include <QLineEdit>
53 #include <QPushButton>
54 #include <QRadioButton>
55 #include <QHBoxLayout>
56 #include <QVBoxLayout>
57 #include <QGridLayout>
58 #include <QCheckBox>
59 #include <QComboBox>
60 #include <QKeyEvent>
61 #include <QButtonGroup>
62
63 // STL includes
64 #include <set>
65
66 #define SPACING 6
67 #define MARGIN  11
68
69 //=================================================================================
70 // name    : SMESHGUI_BuildCompoundDlg
71 // Purpose :
72 //=================================================================================
73 SMESHGUI_BuildCompoundDlg::SMESHGUI_BuildCompoundDlg( SMESHGUI* theModule )
74   : QDialog(SMESH::GetDesktop(theModule)),
75     mySMESHGUI(theModule),
76     mySelectionMgr(SMESH::GetSelectionMgr(theModule))
77 {
78   setModal(false);
79   setAttribute(Qt::WA_DeleteOnClose, true);
80   setWindowTitle(tr("SMESH_BUILD_COMPOUND_TITLE"));
81
82   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
83   QPixmap image0 (aResMgr->loadPixmap("SMESH", tr("ICON_DLG_BUILD_COMPOUND_MESH")));
84   QPixmap image1 (aResMgr->loadPixmap("SMESH", tr("ICON_SELECT")));
85
86   setSizeGripEnabled(true);
87
88   QVBoxLayout* aTopLayout = new QVBoxLayout(this);
89   aTopLayout->setSpacing(SPACING);
90   aTopLayout->setMargin(MARGIN);
91
92   /***************************************************************/
93   GroupConstructors = new QGroupBox(tr("COMPOUND"), this);
94   QButtonGroup* ButtonGroup = new QButtonGroup(this);
95   QHBoxLayout* GroupConstructorsLayout = new QHBoxLayout(GroupConstructors);
96   GroupConstructorsLayout->setSpacing(SPACING);
97   GroupConstructorsLayout->setMargin(MARGIN);
98
99   Constructor1 = new QRadioButton(GroupConstructors);
100   Constructor1->setIcon(image0);
101   Constructor1->setChecked(true);
102   GroupConstructorsLayout->addWidget(Constructor1);
103   ButtonGroup->addButton(Constructor1, 0);
104
105   /***************************************************************/
106   GroupName = new QGroupBox(tr("RESULT_NAME"), this);
107   QHBoxLayout* GroupNameLayout = new QHBoxLayout(GroupName);
108   GroupNameLayout->setSpacing(SPACING);
109   GroupNameLayout->setMargin(MARGIN);
110
111   TextLabelName = new QLabel(tr("SMESH_NAME"), GroupName);
112   LineEditName = new QLineEdit(GroupName);
113
114   GroupNameLayout->addWidget(TextLabelName);
115   GroupNameLayout->addWidget(LineEditName);
116
117   /***************************************************************/
118   GroupArgs = new QGroupBox(tr("SMESH_ARGUMENTS"), this);
119   QGridLayout* GroupArgsLayout = new QGridLayout(GroupArgs);
120   GroupArgsLayout->setSpacing(SPACING);
121   GroupArgsLayout->setMargin(MARGIN);
122
123   TextLabelMeshes = new QLabel(tr("MESHES"), GroupArgs);
124   SelectButton = new QPushButton(GroupArgs);
125   SelectButton->setIcon(image1);
126   SelectButton->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
127   LineEditMeshes = new QLineEdit(GroupArgs);
128   LineEditMeshes->setReadOnly(true);
129
130   TextLabelUnion = new QLabel(tr("PROCESSING_IDENTICAL_GROUPS"), GroupArgs);
131   ComboBoxUnion = new QComboBox(GroupArgs);
132
133   CheckBoxCommon = new QCheckBox(tr("CREATE_COMMON_GROUPS"), GroupArgs);
134
135   CheckBoxMerge = new QCheckBox(tr("MERGE_NODES_AND_ELEMENTS"), GroupArgs);
136
137   TextLabelTol = new QLabel(tr("SMESH_TOLERANCE"), GroupArgs);
138   TextLabelTol->setAlignment(Qt::AlignCenter);
139   SpinBoxTol = new SMESHGUI_SpinBox(GroupArgs);
140   SpinBoxTol->RangeStepAndValidator(0.0, COORD_MAX, 0.00001, 6);
141
142   GroupArgsLayout->addWidget(TextLabelMeshes, 0, 0);
143   GroupArgsLayout->addWidget(SelectButton,    0, 1);
144   GroupArgsLayout->addWidget(LineEditMeshes,  0, 2, 1, 2);
145   GroupArgsLayout->addWidget(TextLabelUnion,  1, 0, 1, 3); 
146   GroupArgsLayout->addWidget(ComboBoxUnion,   1, 3);
147   GroupArgsLayout->addWidget(CheckBoxCommon,  2, 0, 1, 4);
148   GroupArgsLayout->addWidget(CheckBoxMerge,   3, 0, 1, 4);
149   GroupArgsLayout->addWidget(TextLabelTol,    4, 0, 1, 2);
150   GroupArgsLayout->addWidget(SpinBoxTol,      4, 2, 1, 2);
151
152   /***************************************************************/
153   GroupButtons = new QGroupBox(this);
154   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
155   GroupButtonsLayout->setSpacing(SPACING);
156   GroupButtonsLayout->setMargin(MARGIN);
157
158   buttonOk = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), GroupButtons);
159   buttonOk->setAutoDefault(true);
160   buttonOk->setDefault(true);
161   buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
162   buttonApply->setAutoDefault(true);
163   buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
164   buttonCancel->setAutoDefault(true);
165   buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
166   buttonHelp->setAutoDefault(true);
167
168   GroupButtonsLayout->addWidget(buttonOk);
169   GroupButtonsLayout->addSpacing(10);
170   GroupButtonsLayout->addWidget(buttonApply);
171   GroupButtonsLayout->addSpacing(10);
172   GroupButtonsLayout->addStretch();
173   GroupButtonsLayout->addWidget(buttonCancel);
174   GroupButtonsLayout->addWidget(buttonHelp);
175
176   /***************************************************************/
177   aTopLayout->addWidget(GroupConstructors);
178   aTopLayout->addWidget(GroupName);
179   aTopLayout->addWidget(GroupArgs);
180   aTopLayout->addWidget(GroupButtons);
181
182   myHelpFileName = "building_compounds_page.html";
183
184   Init(); // Initialisations
185 }
186
187 //=================================================================================
188 // function : ~SMESHGUI_BuildCompoundDlg()
189 // purpose  : Destroys the object and frees any allocated resources
190 //=================================================================================
191 SMESHGUI_BuildCompoundDlg::~SMESHGUI_BuildCompoundDlg()
192 {
193 }
194
195 //=================================================================================
196 // function : Init()
197 // purpose  :
198 //=================================================================================
199 void SMESHGUI_BuildCompoundDlg::Init()
200 {
201   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
202
203   myMesh = SMESH::SMESH_Mesh::_nil();
204
205   myMeshFilter = new SMESH_TypeFilter (MESH);
206
207   myMeshArray = new SMESH::mesh_array();
208
209   // signals and slots connections
210   connect(buttonOk,     SIGNAL(clicked()), this, SLOT(ClickOnOk()));
211   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
212   connect(buttonApply,  SIGNAL(clicked()), this, SLOT(ClickOnApply()));
213   connect(buttonHelp,   SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
214
215   connect(SelectButton, SIGNAL(clicked()), this, SLOT(SelectionIntoArgument()));
216
217   connect(CheckBoxMerge, SIGNAL(toggled(bool)), this, SLOT(onSelectMerge(bool)));
218
219   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
220
221   connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
222   connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()),        this, SLOT(ClickOnCancel()));
223
224   LineEditName->setText(GetDefaultName(tr("COMPOUND_MESH")));
225   LineEditMeshes->setFocus();
226
227   ComboBoxUnion->addItem(tr("UNITE"));
228   ComboBoxUnion->addItem(tr("RENAME"));
229   ComboBoxUnion->setCurrentIndex(0);
230
231   CheckBoxMerge->setChecked(false);
232
233   TextLabelTol->setEnabled(CheckBoxMerge->isChecked());
234   SpinBoxTol->SetValue(1e-05);
235
236   SpinBoxTol->setEnabled(CheckBoxMerge->isChecked());
237
238   mySelectionMgr->clearFilters();
239   mySelectionMgr->installFilter(myMeshFilter);
240
241   SelectionIntoArgument();
242 }
243
244 //=================================================================================
245 // function : GetDefaultName()
246 // purpose  :
247 //=================================================================================
248 QString SMESHGUI_BuildCompoundDlg::GetDefaultName(const QString& theOperation)
249 {
250   QString aName = "";
251
252   // collect all object names of SMESH component
253   SalomeApp_Study* appStudy =
254     dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
255   if ( !appStudy ) return aName;
256   _PTR(Study) aStudy = appStudy->studyDS();
257
258   std::set<std::string> aSet;
259   _PTR(SComponent) aMeshCompo (aStudy->FindComponent("SMESH"));
260   if (aMeshCompo) {
261     _PTR(ChildIterator) it (aStudy->NewChildIterator(aMeshCompo));
262     _PTR(SObject) obj;
263     for (it->InitEx(true); it->More(); it->Next()) {
264       obj = it->Value();
265       aSet.insert(obj->GetName());
266     }
267   }
268
269   // build a unique name
270   int aNumber = 0;
271   bool isUnique = false;
272   while (!isUnique) {
273     aName = theOperation + "_" + QString::number(++aNumber);
274     isUnique = (aSet.count(aName.toLatin1().data()) == 0);
275   }
276
277   return aName;
278 }
279
280 //=================================================================================
281 // function : ClickOnApply()
282 // purpose  :
283 //=================================================================================
284 bool SMESHGUI_BuildCompoundDlg::ClickOnApply()
285 {
286   if (mySMESHGUI->isActiveStudyLocked())
287     return false;
288
289   if (!isValid())
290     return false;
291
292   if (!myMesh->_is_nil()) {
293     QStringList aParameters;
294     aParameters << SpinBoxTol->text();
295
296     try {
297       SUIT_OverrideCursor aWaitCursor;
298
299       SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
300       // concatenate meshes
301       SMESH::SMESH_Mesh_var aCompoundMesh;
302       if(CheckBoxCommon->isChecked())
303         aCompoundMesh = aSMESHGen->ConcatenateWithGroups(myMeshArray, 
304                                                          !(ComboBoxUnion->currentIndex()), 
305                                                          CheckBoxMerge->isChecked(), 
306                                                          SpinBoxTol->GetValue());
307       else
308         aCompoundMesh = aSMESHGen->Concatenate(myMeshArray, 
309                                                !(ComboBoxUnion->currentIndex()), 
310                                                CheckBoxMerge->isChecked(), 
311                                                SpinBoxTol->GetValue());
312      
313       aCompoundMesh->SetParameters( SMESHGUI::JoinObjectParameters(aParameters) );
314
315       SMESH::SetName( SMESH::FindSObject( aCompoundMesh ), LineEditName->text() );
316       mySMESHGUI->updateObjBrowser();
317     } catch(...) {
318       return false;
319     }
320
321     LineEditName->setText(GetDefaultName(tr("COMPOUND_MESH")));
322
323     //mySelectionMgr->clearSelected();
324     SMESH::UpdateView();
325     return true;
326   }
327   return false;
328 }
329
330 //=================================================================================
331 // function : ClickOnOk()
332 // purpose  :
333 //=================================================================================
334 void SMESHGUI_BuildCompoundDlg::ClickOnOk()
335 {
336   if (ClickOnApply())
337     ClickOnCancel();
338 }
339
340 //=================================================================================
341 // function : ClickOnCancel()
342 // purpose  :
343 //=================================================================================
344 void SMESHGUI_BuildCompoundDlg::ClickOnCancel()
345 {
346   //mySelectionMgr->clearSelected();
347   mySelectionMgr->clearFilters();
348   disconnect(mySelectionMgr, 0, this, 0);
349   mySMESHGUI->ResetState();
350   reject();
351 }
352
353 //=================================================================================
354 // function : ClickOnHelp()
355 // purpose  :
356 //=================================================================================
357 void SMESHGUI_BuildCompoundDlg::ClickOnHelp()
358 {
359   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
360   if (app) 
361     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
362   else {
363     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
364                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
365                              arg(app->resourceMgr()->stringValue("ExternalBrowser",
366                                                                  "application")).
367                              arg(myHelpFileName));
368   }
369 }
370
371 //=================================================================================
372 // function : SelectionIntoArgument()
373 // purpose  : Called when selection as changed or other case
374 //=================================================================================
375 void SMESHGUI_BuildCompoundDlg::SelectionIntoArgument()
376 {
377   if (!GroupButtons->isEnabled()) // inactive
378     return;
379
380   QString aString = "";
381
382   SALOME_ListIO aList;
383   mySelectionMgr->selectedObjects(aList);
384   int nbSel = SMESH::GetNameOfSelectedIObjects(mySelectionMgr, aString);
385
386   if (nbSel != 0) {
387     myMeshArray->length(nbSel);
388     for (int i = 0; nbSel != 0; i++, nbSel--) {
389       Handle(SALOME_InteractiveObject) IO = aList.First();
390       aList.RemoveFirst();
391       myMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(IO);
392       myMeshArray[i] = myMesh;
393     }
394   }
395   else {
396     myMesh = SMESH::SMESH_Mesh::_nil();
397     aString = "";
398   }
399
400   LineEditMeshes->setText(aString);
401
402   bool isEnabled = (!myMesh->_is_nil());
403   buttonOk->setEnabled(isEnabled);
404   buttonApply->setEnabled(isEnabled);
405 }
406
407 //=================================================================================
408 // function : DeactivateActiveDialog()
409 // purpose  :
410 //=================================================================================
411 void SMESHGUI_BuildCompoundDlg::DeactivateActiveDialog()
412 {
413   if (GroupConstructors->isEnabled()) {
414     GroupConstructors->setEnabled(false);
415     GroupName->setEnabled(false);
416     GroupArgs->setEnabled(false);
417     GroupButtons->setEnabled(false);
418     mySMESHGUI->ResetState();
419     mySMESHGUI->SetActiveDialogBox(0);
420   }
421 }
422
423 //=================================================================================
424 // function : ActivateThisDialog()
425 // purpose  :
426 //=================================================================================
427 void SMESHGUI_BuildCompoundDlg::ActivateThisDialog()
428 {
429   /* Emit a signal to deactivate the active dialog */
430   mySMESHGUI->EmitSignalDeactivateDialog();
431   GroupConstructors->setEnabled(true);
432   GroupName->setEnabled(true);
433   GroupArgs->setEnabled(true);
434   GroupButtons->setEnabled(true);
435
436   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
437   SelectionIntoArgument();
438 }
439
440 //=================================================================================
441 // function : enterEvent()
442 // purpose  :
443 //=================================================================================
444 void SMESHGUI_BuildCompoundDlg::enterEvent( QEvent* )
445 {
446   if (GroupConstructors->isEnabled())
447     return;
448   ActivateThisDialog();
449 }
450
451 //=================================================================================
452 // function : closeEvent()
453 // purpose  :
454 //=================================================================================
455 void SMESHGUI_BuildCompoundDlg::closeEvent( QCloseEvent* )
456 {
457   /* same than click on cancel button */
458   ClickOnCancel();
459 }
460
461 //=======================================================================
462 //function : hideEvent
463 //purpose  : caused by ESC key
464 //=======================================================================
465 void SMESHGUI_BuildCompoundDlg::hideEvent( QHideEvent* )
466 {
467   if (!isMinimized())
468     ClickOnCancel();
469 }
470
471
472 //=================================================================================
473 // function : keyPressEvent()
474 // purpose  :
475 //=================================================================================
476 void SMESHGUI_BuildCompoundDlg::keyPressEvent( QKeyEvent* e )
477 {
478   QDialog::keyPressEvent( e );
479   if ( e->isAccepted() )
480     return;
481
482   if ( e->key() == Qt::Key_F1 ) {
483     e->accept();
484     ClickOnHelp();
485   }
486 }
487
488
489 //=================================================================================
490 // function : onSelectMerge()
491 // purpose  :
492 //=================================================================================
493 void SMESHGUI_BuildCompoundDlg::onSelectMerge(bool toMerge)
494 {
495   TextLabelTol->setEnabled(toMerge);
496   SpinBoxTol->setEnabled(toMerge);
497 }
498
499 //=================================================================================
500 // function : isValid
501 // purpose  :
502 //=================================================================================
503 bool SMESHGUI_BuildCompoundDlg::isValid()
504 {
505   QString msg;
506   bool ok = SpinBoxTol->isValid( msg, true ) && ok;
507
508   if( !ok ) {
509     QString str( tr( "SMESH_INCORRECT_INPUT" ) );
510     if ( !msg.isEmpty() )
511       str += "\n" + msg;
512     SUIT_MessageBox::critical( this, tr( "SMESH_ERROR" ), str );
513     return false;
514   }
515   return true;
516 }