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