Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[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   CheckBoxCommon = new QCheckBox(GroupArgs, "CheckBoxCommon");
154   CheckBoxCommon->setText(tr("CREATE_COMMON_GROUPS" ));
155   GroupArgsLayout->addMultiCellWidget(CheckBoxCommon, 2, 2, 0, 3);
156
157   CheckBoxMerge = new QCheckBox(GroupArgs, "CheckBoxMerge");
158   CheckBoxMerge->setText(tr("MERGE_NODES_AND_ELEMENTS" ));
159   GroupArgsLayout->addMultiCellWidget(CheckBoxMerge, 3, 3, 0, 3);
160
161   TextLabelTol = new QLabel (GroupArgs, "TextLabelTol");
162   TextLabelTol->setText(tr("SMESH_TOLERANCE"));
163   TextLabelTol->setAlignment(Qt::AlignCenter);
164   GroupArgsLayout->addMultiCellWidget(TextLabelTol, 4, 4, 0, 1);
165   SpinBoxTol = new SMESHGUI_SpinBox (GroupArgs, "SpinBoxTol");
166   SpinBoxTol->RangeStepAndValidator(0.0, COORD_MAX, 0.1, 6);
167   GroupArgsLayout->addMultiCellWidget(SpinBoxTol, 4, 4, 2, 3);
168
169   SMESHGUI_BuildCompoundDlgLayout->addWidget(GroupArgs, 2, 0);
170
171   /***************************************************************/
172   GroupButtons = new QGroupBox (this, "GroupButtons");
173   GroupButtons->setGeometry(QRect(10, 10, 281, 48));
174   GroupButtons->setTitle(tr("" ));
175   GroupButtons->setColumnLayout(0, Qt::Vertical);
176   GroupButtons->layout()->setSpacing(0);
177   GroupButtons->layout()->setMargin(0);
178   GroupButtonsLayout = new QGridLayout (GroupButtons->layout());
179   GroupButtonsLayout->setAlignment(Qt::AlignTop);
180   GroupButtonsLayout->setSpacing(6);
181   GroupButtonsLayout->setMargin(11);
182   buttonHelp = new QPushButton(GroupButtons, "buttonHelp");
183   buttonHelp->setText(tr("SMESH_BUT_HELP" ));
184   buttonHelp->setAutoDefault(TRUE);
185   GroupButtonsLayout->addWidget(buttonHelp, 0, 4);
186   buttonCancel = new QPushButton (GroupButtons, "buttonCancel");
187   buttonCancel->setText(tr("SMESH_BUT_CLOSE" ));
188   buttonCancel->setAutoDefault(TRUE);
189   GroupButtonsLayout->addWidget(buttonCancel, 0, 3);
190   buttonApply = new QPushButton (GroupButtons, "buttonApply");
191   buttonApply->setText(tr("SMESH_BUT_APPLY" ));
192   buttonApply->setAutoDefault(TRUE);
193   GroupButtonsLayout->addWidget(buttonApply, 0, 1);
194   QSpacerItem* spacer_9 = new QSpacerItem (20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
195   GroupButtonsLayout->addItem(spacer_9, 0, 2);
196   buttonOk = new QPushButton (GroupButtons, "buttonOk");
197   buttonOk->setText(tr("SMESH_BUT_OK" ));
198   buttonOk->setAutoDefault(TRUE);
199   buttonOk->setDefault(TRUE);
200   GroupButtonsLayout->addWidget(buttonOk, 0, 0);
201   SMESHGUI_BuildCompoundDlgLayout->addWidget(GroupButtons, 3, 0);
202
203   myHelpFileName = "building_compounds_page.html";
204
205   Init(); // Initialisations
206 }
207
208 //=================================================================================
209 // function : ~SMESHGUI_BuildCompoundDlg()
210 // purpose  : Destroys the object and frees any allocated resources
211 //=================================================================================
212 SMESHGUI_BuildCompoundDlg::~SMESHGUI_BuildCompoundDlg()
213 {
214   // no need to delete child widgets, Qt does it all for us
215 }
216
217 //=================================================================================
218 // function : Init()
219 // purpose  :
220 //=================================================================================
221 void SMESHGUI_BuildCompoundDlg::Init()
222 {
223   GroupName->show();
224   GroupArgs->show();
225   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
226
227   myMesh = SMESH::SMESH_Mesh::_nil();
228
229   myMeshFilter = new SMESH_TypeFilter (MESH);
230
231   myMeshArray = new SMESH::mesh_array();
232
233   // signals and slots connections
234   connect(buttonOk    , SIGNAL(clicked()), this, SLOT(ClickOnOk()));
235   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
236   connect(buttonApply , SIGNAL(clicked()), this, SLOT(ClickOnApply()));
237   connect(buttonHelp,   SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
238
239   connect(SelectButton, SIGNAL(clicked()), this, SLOT(SelectionIntoArgument()));
240
241   connect(CheckBoxMerge, SIGNAL(toggled(bool)), this, SLOT(onSelectMerge(bool)));
242
243   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
244
245   connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
246   connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs())       , this, SLOT(ClickOnCancel()));
247
248   this->show(); // displays Dialog
249
250   LineEditName->setText(GetDefaultName(tr("COMPOUND_MESH")));
251   LineEditMeshes->setFocus();
252
253   ComboBoxUnion->insertItem(tr("UNITE"));
254   ComboBoxUnion->insertItem(tr("RENAME"));
255   ComboBoxUnion->setCurrentItem(0);
256
257   CheckBoxMerge->setChecked(false);
258
259   TextLabelTol->setEnabled(CheckBoxMerge->isChecked());
260   SpinBoxTol->SetValue(1e-05);
261
262   SpinBoxTol->setEnabled(CheckBoxMerge->isChecked());
263
264   mySelectionMgr->clearFilters();
265   mySelectionMgr->installFilter(myMeshFilter);
266
267   SelectionIntoArgument();
268 }
269
270 //=================================================================================
271 // function : GetDefaultName()
272 // purpose  :
273 //=================================================================================
274 QString SMESHGUI_BuildCompoundDlg::GetDefaultName(const QString& theOperation)
275 {
276   QString aName = "";
277
278   // collect all object names of SMESH component
279   SalomeApp_Study* appStudy =
280     dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
281   if ( !appStudy ) return aName;
282   _PTR(Study) aStudy = appStudy->studyDS();
283
284   std::set<std::string> aSet;
285   _PTR(SComponent) aMeshCompo (aStudy->FindComponent("SMESH"));
286   if (aMeshCompo) {
287     _PTR(ChildIterator) it (aStudy->NewChildIterator(aMeshCompo));
288     _PTR(SObject) obj;
289     for (it->InitEx(true); it->More(); it->Next()) {
290       obj = it->Value();
291       aSet.insert(obj->GetName());
292     }
293   }
294
295   // build a unique name
296   int aNumber = 0;
297   bool isUnique = false;
298   while (!isUnique) {
299     aName = theOperation + "_" + QString::number(++aNumber);
300     isUnique = (aSet.count(aName.latin1()) == 0);
301   }
302
303   return aName;
304 }
305
306 //=================================================================================
307 // function : ClickOnApply()
308 // purpose  :
309 //=================================================================================
310 bool SMESHGUI_BuildCompoundDlg::ClickOnApply()
311 {
312   if (mySMESHGUI->isActiveStudyLocked())
313     return false;
314   if (!myMesh->_is_nil()) {
315     try {
316       QApplication::setOverrideCursor(Qt::waitCursor);
317       
318       SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
319       // concatenate meshes
320       SMESH::SMESH_Mesh_var aCompoundMesh;
321       if(CheckBoxCommon->isChecked())
322         aCompoundMesh = aSMESHGen->ConcatenateWithGroups(myMeshArray, 
323                                                          !(ComboBoxUnion->currentItem()), 
324                                                          CheckBoxMerge->isChecked(), 
325                                                          SpinBoxTol->GetValue());
326       else
327         aCompoundMesh = aSMESHGen->Concatenate(myMeshArray, 
328                                                !(ComboBoxUnion->currentItem()), 
329                                                CheckBoxMerge->isChecked(), 
330                                                SpinBoxTol->GetValue());
331      
332       SMESH::SetName( SMESH::FindSObject( aCompoundMesh ), LineEditName->text().latin1() );
333       QApplication::restoreOverrideCursor();
334       mySMESHGUI->updateObjBrowser();
335     } catch(...) {
336       return false;
337     }
338
339     LineEditName->setText(GetDefaultName(tr("COMPOUND_MESH")));
340
341     //mySelectionMgr->clearSelected();
342     SMESH::UpdateView();
343     return true;
344   }
345   return false;
346 }
347
348 //=================================================================================
349 // function : ClickOnOk()
350 // purpose  :
351 //=================================================================================
352 void SMESHGUI_BuildCompoundDlg::ClickOnOk()
353 {
354   if (ClickOnApply())
355     ClickOnCancel();
356 }
357
358 //=================================================================================
359 // function : ClickOnCancel()
360 // purpose  :
361 //=================================================================================
362 void SMESHGUI_BuildCompoundDlg::ClickOnCancel()
363 {
364   //mySelectionMgr->clearSelected();
365   mySelectionMgr->clearFilters();
366   disconnect(mySelectionMgr, 0, this, 0);
367   mySMESHGUI->ResetState();
368   reject();
369 }
370
371 //=================================================================================
372 // function : ClickOnHelp()
373 // purpose  :
374 //=================================================================================
375 void SMESHGUI_BuildCompoundDlg::ClickOnHelp()
376 {
377   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
378   if (app) 
379     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
380   else {
381     SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
382                            QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
383                            arg(app->resourceMgr()->stringValue("ExternalBrowser", "application")).arg(myHelpFileName),
384                            QObject::tr("BUT_OK"));
385   }
386 }
387
388 //=================================================================================
389 // function : SelectionIntoArgument()
390 // purpose  : Called when selection as changed or other case
391 //=================================================================================
392 void SMESHGUI_BuildCompoundDlg::SelectionIntoArgument()
393 {
394   if (!GroupButtons->isEnabled()) // inactive
395     return;
396
397   QString aString = "";
398
399   SALOME_ListIO aList;
400   mySelectionMgr->selectedObjects(aList);
401   int nbSel = SMESH::GetNameOfSelectedIObjects(mySelectionMgr, aString);
402
403   if (nbSel != 0) {
404     myMeshArray->length(nbSel);
405     for (int i = 0; nbSel != 0; i++, nbSel--) {
406       Handle(SALOME_InteractiveObject) IO = aList.First();
407       aList.RemoveFirst();
408       myMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(IO);
409       myMeshArray[i] = myMesh;
410     }
411   }
412   else {
413     myMesh = SMESH::SMESH_Mesh::_nil();
414     aString = "";
415   }
416
417   LineEditMeshes->setText(aString);
418
419   bool isEnabled = (!myMesh->_is_nil());
420   buttonOk->setEnabled(isEnabled);
421   buttonApply->setEnabled(isEnabled);
422 }
423
424 //=================================================================================
425 // function : DeactivateActiveDialog()
426 // purpose  :
427 //=================================================================================
428 void SMESHGUI_BuildCompoundDlg::DeactivateActiveDialog()
429 {
430   if (GroupConstructors->isEnabled()) {
431     GroupConstructors->setEnabled(false);
432     GroupName->setEnabled(false);
433     GroupArgs->setEnabled(false);
434     GroupButtons->setEnabled(false);
435     mySMESHGUI->ResetState();
436     mySMESHGUI->SetActiveDialogBox(0);
437   }
438 }
439
440 //=================================================================================
441 // function : ActivateThisDialog()
442 // purpose  :
443 //=================================================================================
444 void SMESHGUI_BuildCompoundDlg::ActivateThisDialog()
445 {
446   /* Emit a signal to deactivate the active dialog */
447   mySMESHGUI->EmitSignalDeactivateDialog();
448   GroupConstructors->setEnabled(true);
449   GroupName->setEnabled(true);
450   GroupArgs->setEnabled(true);
451   GroupButtons->setEnabled(true);
452
453   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
454   SelectionIntoArgument();
455 }
456
457 //=================================================================================
458 // function : enterEvent()
459 // purpose  :
460 //=================================================================================
461 void SMESHGUI_BuildCompoundDlg::enterEvent(QEvent* e)
462 {
463   if (GroupConstructors->isEnabled())
464     return;
465   ActivateThisDialog();
466 }
467
468 //=================================================================================
469 // function : closeEvent()
470 // purpose  :
471 //=================================================================================
472 void SMESHGUI_BuildCompoundDlg::closeEvent(QCloseEvent* e)
473 {
474   /* same than click on cancel button */
475   this->ClickOnCancel();
476 }
477
478 //=======================================================================
479 //function : hideEvent
480 //purpose  : caused by ESC key
481 //=======================================================================
482 void SMESHGUI_BuildCompoundDlg::hideEvent (QHideEvent * e)
483 {
484   if (!isMinimized())
485     ClickOnCancel();
486 }
487
488
489 //=================================================================================
490 // function : keyPressEvent()
491 // purpose  :
492 //=================================================================================
493 void SMESHGUI_BuildCompoundDlg::keyPressEvent( QKeyEvent* e )
494 {
495   QDialog::keyPressEvent( e );
496   if ( e->isAccepted() )
497     return;
498
499   if ( e->key() == Key_F1 )
500     {
501       e->accept();
502       ClickOnHelp();
503     }
504 }
505
506
507 //=================================================================================
508 // function : onSelectMerge()
509 // purpose  :
510 //=================================================================================
511 void SMESHGUI_BuildCompoundDlg::onSelectMerge(bool toMerge)
512 {
513   TextLabelTol->setEnabled(toMerge);
514   SpinBoxTol->setEnabled(toMerge);
515   
516 }