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