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