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