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