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