Salome HOME
Merge from V5_1_4_BR 07/05/2010
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_BuildCompoundDlg.cxx
1 //  Copyright (C) 2007-2010  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
23 // SMESH SMESHGUI : GUI for SMESH component
24 // File   : SMESHGUI_BuildCompoundDlg.cxx
25 // Author : Alexander KOVALEV, Open CASCADE S.A.S.
26 // SMESH includes
27 //
28 #include "SMESHGUI_BuildCompoundDlg.h"
29
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_Utils.h"
32 #include "SMESHGUI_SpinBox.h"
33 #include "SMESHGUI_VTKUtils.h"
34
35 #include <SMESH_TypeFilter.hxx>
36
37 // SALOME GUI includes
38 #include <SUIT_Desktop.h>
39 #include <SUIT_Session.h>
40 #include <SUIT_MessageBox.h>
41 #include <SUIT_ResourceMgr.h>
42 #include <SalomeApp_Study.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     mySMESHGUI(theModule),
77     mySelectionMgr(SMESH::GetSelectionMgr(theModule))
78 {
79   setModal(false);
80   setAttribute(Qt::WA_DeleteOnClose, true);
81   setWindowTitle(tr("SMESH_BUILD_COMPOUND_TITLE"));
82
83   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
84   QPixmap image0 (aResMgr->loadPixmap("SMESH", tr("ICON_DLG_BUILD_COMPOUND_MESH")));
85   QPixmap image1 (aResMgr->loadPixmap("SMESH", tr("ICON_SELECT")));
86
87   setSizeGripEnabled(true);
88
89   QVBoxLayout* aTopLayout = new QVBoxLayout(this);
90   aTopLayout->setSpacing(SPACING);
91   aTopLayout->setMargin(MARGIN);
92
93   /***************************************************************/
94   GroupConstructors = new QGroupBox(tr("COMPOUND"), this);
95   QButtonGroup* ButtonGroup = new QButtonGroup(this);
96   QHBoxLayout* GroupConstructorsLayout = new QHBoxLayout(GroupConstructors);
97   GroupConstructorsLayout->setSpacing(SPACING);
98   GroupConstructorsLayout->setMargin(MARGIN);
99
100   Constructor1 = new QRadioButton(GroupConstructors);
101   Constructor1->setIcon(image0);
102   Constructor1->setChecked(true);
103   GroupConstructorsLayout->addWidget(Constructor1);
104   ButtonGroup->addButton(Constructor1, 0);
105
106   /***************************************************************/
107   GroupName = new QGroupBox(tr("RESULT_NAME"), this);
108   QHBoxLayout* GroupNameLayout = new QHBoxLayout(GroupName);
109   GroupNameLayout->setSpacing(SPACING);
110   GroupNameLayout->setMargin(MARGIN);
111
112   TextLabelName = new QLabel(tr("SMESH_NAME"), GroupName);
113   LineEditName = new QLineEdit(GroupName);
114
115   GroupNameLayout->addWidget(TextLabelName);
116   GroupNameLayout->addWidget(LineEditName);
117
118   /***************************************************************/
119   GroupArgs = new QGroupBox(tr("SMESH_ARGUMENTS"), this);
120   QGridLayout* GroupArgsLayout = new QGridLayout(GroupArgs);
121   GroupArgsLayout->setSpacing(SPACING);
122   GroupArgsLayout->setMargin(MARGIN);
123
124   TextLabelMeshes = new QLabel(tr("MESHES"), GroupArgs);
125   SelectButton = new QPushButton(GroupArgs);
126   SelectButton->setIcon(image1);
127   SelectButton->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
128   LineEditMeshes = new QLineEdit(GroupArgs);
129   LineEditMeshes->setReadOnly(true);
130
131   TextLabelUnion = new QLabel(tr("PROCESSING_IDENTICAL_GROUPS"), GroupArgs);
132   ComboBoxUnion = new QComboBox(GroupArgs);
133
134   CheckBoxCommon = new QCheckBox(tr("CREATE_COMMON_GROUPS"), GroupArgs);
135
136   CheckBoxMerge = new QCheckBox(tr("MERGE_NODES_AND_ELEMENTS"), GroupArgs);
137
138   TextLabelTol = new QLabel(tr("SMESH_TOLERANCE"), GroupArgs);
139   TextLabelTol->setAlignment(Qt::AlignCenter);
140   SpinBoxTol = new SMESHGUI_SpinBox(GroupArgs);
141   SpinBoxTol->RangeStepAndValidator(0.0, COORD_MAX, 0.00001, "len_tol_precision" );
142
143   GroupArgsLayout->addWidget(TextLabelMeshes, 0, 0);
144   GroupArgsLayout->addWidget(SelectButton,    0, 1);
145   GroupArgsLayout->addWidget(LineEditMeshes,  0, 2, 1, 2);
146   GroupArgsLayout->addWidget(TextLabelUnion,  1, 0, 1, 3); 
147   GroupArgsLayout->addWidget(ComboBoxUnion,   1, 3);
148   GroupArgsLayout->addWidget(CheckBoxCommon,  2, 0, 1, 4);
149   GroupArgsLayout->addWidget(CheckBoxMerge,   3, 0, 1, 4);
150   GroupArgsLayout->addWidget(TextLabelTol,    4, 0, 1, 2);
151   GroupArgsLayout->addWidget(SpinBoxTol,      4, 2, 1, 2);
152
153   /***************************************************************/
154   GroupButtons = new QGroupBox(this);
155   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
156   GroupButtonsLayout->setSpacing(SPACING);
157   GroupButtonsLayout->setMargin(MARGIN);
158
159   buttonOk = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), GroupButtons);
160   buttonOk->setAutoDefault(true);
161   buttonOk->setDefault(true);
162   buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
163   buttonApply->setAutoDefault(true);
164   buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
165   buttonCancel->setAutoDefault(true);
166   buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
167   buttonHelp->setAutoDefault(true);
168
169   GroupButtonsLayout->addWidget(buttonOk);
170   GroupButtonsLayout->addSpacing(10);
171   GroupButtonsLayout->addWidget(buttonApply);
172   GroupButtonsLayout->addSpacing(10);
173   GroupButtonsLayout->addStretch();
174   GroupButtonsLayout->addWidget(buttonCancel);
175   GroupButtonsLayout->addWidget(buttonHelp);
176
177   /***************************************************************/
178   aTopLayout->addWidget(GroupConstructors);
179   aTopLayout->addWidget(GroupName);
180   aTopLayout->addWidget(GroupArgs);
181   aTopLayout->addWidget(GroupButtons);
182
183   myHelpFileName = "building_compounds_page.html";
184
185   Init(); // Initialisations
186 }
187
188 //=================================================================================
189 // function : ~SMESHGUI_BuildCompoundDlg()
190 // purpose  : Destroys the object and frees any allocated resources
191 //=================================================================================
192 SMESHGUI_BuildCompoundDlg::~SMESHGUI_BuildCompoundDlg()
193 {
194 }
195
196 //=================================================================================
197 // function : Init()
198 // purpose  :
199 //=================================================================================
200 void SMESHGUI_BuildCompoundDlg::Init()
201 {
202   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
203
204   myMesh = SMESH::SMESH_Mesh::_nil();
205
206   myMeshFilter = new SMESH_TypeFilter (MESH);
207
208   myMeshArray = new SMESH::mesh_array();
209
210   // signals and slots connections
211   connect(buttonOk,     SIGNAL(clicked()), this, SLOT(ClickOnOk()));
212   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
213   connect(buttonApply,  SIGNAL(clicked()), this, SLOT(ClickOnApply()));
214   connect(buttonHelp,   SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
215
216   connect(SelectButton, SIGNAL(clicked()), this, SLOT(SelectionIntoArgument()));
217
218   connect(CheckBoxMerge, SIGNAL(toggled(bool)), this, SLOT(onSelectMerge(bool)));
219
220   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
221
222   connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
223   connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()),        this, SLOT(ClickOnCancel()));
224
225   LineEditName->setText(GetDefaultName(tr("COMPOUND_MESH")));
226   LineEditMeshes->setFocus();
227
228   ComboBoxUnion->addItem(tr("UNITE"));
229   ComboBoxUnion->addItem(tr("RENAME"));
230   ComboBoxUnion->setCurrentIndex(0);
231
232   CheckBoxMerge->setChecked(false);
233
234   TextLabelTol->setEnabled(CheckBoxMerge->isChecked());
235   SpinBoxTol->SetValue(1e-05);
236
237   SpinBoxTol->setEnabled(CheckBoxMerge->isChecked());
238
239   mySelectionMgr->clearFilters();
240   mySelectionMgr->installFilter(myMeshFilter);
241
242   SelectionIntoArgument();
243 }
244
245 //=================================================================================
246 // function : GetDefaultName()
247 // purpose  :
248 //=================================================================================
249 QString SMESHGUI_BuildCompoundDlg::GetDefaultName(const QString& theOperation)
250 {
251   QString aName = "";
252
253   // collect all object names of SMESH component
254   SalomeApp_Study* appStudy =
255     dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
256   if ( !appStudy ) return aName;
257   _PTR(Study) aStudy = appStudy->studyDS();
258
259   std::set<std::string> aSet;
260   _PTR(SComponent) aMeshCompo (aStudy->FindComponent("SMESH"));
261   if (aMeshCompo) {
262     _PTR(ChildIterator) it (aStudy->NewChildIterator(aMeshCompo));
263     _PTR(SObject) obj;
264     for (it->InitEx(true); it->More(); it->Next()) {
265       obj = it->Value();
266       aSet.insert(obj->GetName());
267     }
268   }
269
270   // build a unique name
271   int aNumber = 0;
272   bool isUnique = false;
273   while (!isUnique) {
274     aName = theOperation + "_" + QString::number(++aNumber);
275     isUnique = (aSet.count(aName.toLatin1().data()) == 0);
276   }
277
278   return aName;
279 }
280
281 //=================================================================================
282 // function : ClickOnApply()
283 // purpose  :
284 //=================================================================================
285 bool SMESHGUI_BuildCompoundDlg::ClickOnApply()
286 {
287   if (mySMESHGUI->isActiveStudyLocked())
288     return false;
289
290   if (!isValid())
291     return false;
292
293   SMESH::SMESH_Mesh_var aCompoundMesh;
294
295   if (!myMesh->_is_nil()) {
296     QStringList aParameters;
297     aParameters << (CheckBoxMerge->isChecked() ? SpinBoxTol->text() : QString(" "));
298     try {
299       SUIT_OverrideCursor aWaitCursor;
300
301       SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
302       // concatenate meshes
303       if(CheckBoxCommon->isChecked())
304         aCompoundMesh = aSMESHGen->ConcatenateWithGroups(myMeshArray, 
305                                                          !(ComboBoxUnion->currentIndex()), 
306                                                          CheckBoxMerge->isChecked(), 
307                                                          SpinBoxTol->GetValue());
308       else
309         aCompoundMesh = aSMESHGen->Concatenate(myMeshArray, 
310                                                !(ComboBoxUnion->currentIndex()), 
311                                                CheckBoxMerge->isChecked(), 
312                                                SpinBoxTol->GetValue());
313      
314       aCompoundMesh->SetParameters( aParameters.join(":").toLatin1().constData() );
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   QString msg;
518   bool ok=true;
519   if(CheckBoxMerge->isChecked())
520     ok = SpinBoxTol->isValid( msg, true );
521
522   if( !ok ) {
523     QString str( tr( "SMESH_INCORRECT_INPUT" ) );
524     if ( !msg.isEmpty() )
525       str += "\n" + msg;
526     SUIT_MessageBox::critical( this, tr( "SMESH_ERROR" ), str );
527     return false;
528   }
529   return true;
530 }