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