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