Salome HOME
Merge from V6_5_BR 05/06/2012
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_BuildCompoundDlg.cxx
1 // Copyright (C) 2007-2012  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 //  File   : SMESHGUI_BuildCompoundDlg.cxx
23 //  Author : Alexander KOVALEV, Open CASCADE S.A.S.
24 //  SMESH includes
25
26 #include "SMESHGUI_BuildCompoundDlg.h"
27
28 #include "SMESHGUI.h"
29 #include "SMESHGUI_Utils.h"
30 #include "SMESHGUI_SpinBox.h"
31 #include "SMESHGUI_VTKUtils.h"
32
33 #include <SMESH_TypeFilter.hxx>
34
35 // SALOME GUI includes
36 #include <SUIT_Desktop.h>
37 #include <SUIT_Session.h>
38 #include <SUIT_MessageBox.h>
39 #include <SUIT_ResourceMgr.h>
40 #include <SalomeApp_Study.h>
41 #include <SUIT_OverrideCursor.h>
42
43 #include <LightApp_Application.h>
44 #include <LightApp_SelectionMgr.h>
45 #include <SALOME_ListIO.hxx>
46
47 // Qt includes
48 #include <QApplication>
49 #include <QGroupBox>
50 #include <QLabel>
51 #include <QLineEdit>
52 #include <QPushButton>
53 #include <QRadioButton>
54 #include <QHBoxLayout>
55 #include <QVBoxLayout>
56 #include <QGridLayout>
57 #include <QCheckBox>
58 #include <QComboBox>
59 #include <QKeyEvent>
60 #include <QButtonGroup>
61
62 // STL includes
63 #include <set>
64
65 #define SPACING 6
66 #define MARGIN  11
67
68 //To disable automatic genericobj management, the following line should be commented.
69 //Otherwise, it should be uncommented. Refer to KERNEL_SRC/src/SALOMEDSImpl/SALOMEDSImpl_AttributeIOR.cxx
70 #define WITHGENERICOBJ
71
72 //=================================================================================
73 // name    : SMESHGUI_BuildCompoundDlg
74 // Purpose :
75 //=================================================================================
76 SMESHGUI_BuildCompoundDlg::SMESHGUI_BuildCompoundDlg( SMESHGUI* theModule )
77   : QDialog(SMESH::GetDesktop(theModule)),
78     mySMESHGUI(theModule),
79     mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
80     myIsApplyAndClose( false )
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   {
300     QStringList aParameters;
301     aParameters << (CheckBoxMerge->isChecked() ? SpinBoxTol->text() : QString(" "));
302
303     QStringList anEntryList;
304     try {
305       SUIT_OverrideCursor aWaitCursor;
306
307       myMeshArray[0]->SetParameters( aParameters.join(":").toLatin1().constData() );
308
309       SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
310       // concatenate meshes
311       if(CheckBoxCommon->isChecked())
312         aCompoundMesh = aSMESHGen->ConcatenateWithGroups(myMeshArray,
313                                                          !(ComboBoxUnion->currentIndex()),
314                                                          CheckBoxMerge->isChecked(),
315                                                          SpinBoxTol->GetValue());
316       else
317         aCompoundMesh = aSMESHGen->Concatenate(myMeshArray,
318                                                !(ComboBoxUnion->currentIndex()),
319                                                CheckBoxMerge->isChecked(),
320                                                SpinBoxTol->GetValue());
321
322       _PTR(SObject) aSO = SMESH::FindSObject( aCompoundMesh );
323       if( aSO ) {
324         SMESH::SetName( aSO, LineEditName->text() );
325         anEntryList.append( aSO->GetID().c_str() );
326       }
327       mySMESHGUI->updateObjBrowser();
328     } catch(...) {
329       return false;
330     }
331
332     LineEditName->setText(GetDefaultName(tr("COMPOUND_MESH")));
333
334     // IPAL21468 Compound is hidden after creation.
335     if ( SMESHGUI::automaticUpdate() ) {
336       mySelectionMgr->clearSelected();
337       SMESH::UpdateView();
338
339       _PTR(SObject) aSO = SMESH::FindSObject(aCompoundMesh.in());
340       if ( SMESH_Actor* anActor = SMESH::CreateActor(aSO->GetStudy(), aSO->GetID().c_str()) )
341         SMESH::DisplayActor(SMESH::GetActiveWindow(), anActor);
342     }// end IPAL21468
343
344     if( LightApp_Application* anApp =
345         dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
346       anApp->browseObjects( anEntryList, isApplyAndClose() );
347
348     SMESHGUI::Modified();
349
350 #ifdef WITHGENERICOBJ
351     // obj has been published in study. Its refcount has been incremented.
352     // It is safe to decrement its refcount
353     // so that it will be destroyed when the entry in study will be removed
354     if (!CORBA::is_nil(aCompoundMesh))
355       aCompoundMesh->UnRegister();
356 #endif
357
358     return true;
359   }
360   return false;
361 }
362
363 //=================================================================================
364 // function : ClickOnOk()
365 // purpose  :
366 //=================================================================================
367 void SMESHGUI_BuildCompoundDlg::ClickOnOk()
368 {
369   setIsApplyAndClose( true );
370   if (ClickOnApply())
371     ClickOnCancel();
372 }
373
374 //=================================================================================
375 // function : ClickOnCancel()
376 // purpose  :
377 //=================================================================================
378 void SMESHGUI_BuildCompoundDlg::ClickOnCancel()
379 {
380   //mySelectionMgr->clearSelected();
381   mySelectionMgr->clearFilters();
382   disconnect(mySelectionMgr, 0, this, 0);
383   mySMESHGUI->ResetState();
384   reject();
385 }
386
387 //=================================================================================
388 // function : ClickOnHelp()
389 // purpose  :
390 //=================================================================================
391 void SMESHGUI_BuildCompoundDlg::ClickOnHelp()
392 {
393   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
394   if (app)
395     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
396   else {
397     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
398                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
399                              arg(app->resourceMgr()->stringValue("ExternalBrowser",
400                                                                  "application")).
401                              arg(myHelpFileName));
402   }
403 }
404
405 //=================================================================================
406 // function : SelectionIntoArgument()
407 // purpose  : Called when selection as changed or other case
408 //=================================================================================
409 void SMESHGUI_BuildCompoundDlg::SelectionIntoArgument()
410 {
411   if (!GroupButtons->isEnabled()) // inactive
412     return;
413
414   QString aString = "";
415
416   SALOME_ListIO aList;
417   mySelectionMgr->selectedObjects(aList);
418   int nbSel = SMESH::GetNameOfSelectedIObjects(mySelectionMgr, aString);
419
420   if (nbSel != 0) {
421     myMeshArray->length(nbSel);
422     for (int i = 0; nbSel != 0; i++, nbSel--) {
423       Handle(SALOME_InteractiveObject) IO = aList.First();
424       aList.RemoveFirst();
425       myMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(IO);
426       myMeshArray[i] = myMesh;
427     }
428   }
429   else {
430     myMesh = SMESH::SMESH_Mesh::_nil();
431     aString = "";
432   }
433
434   LineEditMeshes->setText(aString);
435
436   bool isEnabled = (!myMesh->_is_nil());
437   buttonOk->setEnabled(isEnabled);
438   buttonApply->setEnabled(isEnabled);
439 }
440
441 //=================================================================================
442 // function : DeactivateActiveDialog()
443 // purpose  :
444 //=================================================================================
445 void SMESHGUI_BuildCompoundDlg::DeactivateActiveDialog()
446 {
447   if (GroupConstructors->isEnabled()) {
448     GroupConstructors->setEnabled(false);
449     GroupName->setEnabled(false);
450     GroupArgs->setEnabled(false);
451     GroupButtons->setEnabled(false);
452     mySMESHGUI->ResetState();
453     mySMESHGUI->SetActiveDialogBox(0);
454   }
455 }
456
457 //=================================================================================
458 // function : ActivateThisDialog()
459 // purpose  :
460 //=================================================================================
461 void SMESHGUI_BuildCompoundDlg::ActivateThisDialog()
462 {
463   /* Emit a signal to deactivate the active dialog */
464   mySMESHGUI->EmitSignalDeactivateDialog();
465   GroupConstructors->setEnabled(true);
466   GroupName->setEnabled(true);
467   GroupArgs->setEnabled(true);
468   GroupButtons->setEnabled(true);
469
470   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
471   SelectionIntoArgument();
472 }
473
474 //=================================================================================
475 // function : enterEvent()
476 // purpose  :
477 //=================================================================================
478 void SMESHGUI_BuildCompoundDlg::enterEvent( QEvent* )
479 {
480   if (GroupConstructors->isEnabled())
481     return;
482   ActivateThisDialog();
483 }
484
485 //=================================================================================
486 // function : closeEvent()
487 // purpose  :
488 //=================================================================================
489 void SMESHGUI_BuildCompoundDlg::closeEvent( QCloseEvent* )
490 {
491   /* same than click on cancel button */
492   ClickOnCancel();
493 }
494
495 //=======================================================================
496 //function : hideEvent
497 //purpose  : caused by ESC key
498 //=======================================================================
499 void SMESHGUI_BuildCompoundDlg::hideEvent( QHideEvent* )
500 {
501   if (!isMinimized())
502     ClickOnCancel();
503 }
504
505
506 //=================================================================================
507 // function : keyPressEvent()
508 // purpose  :
509 //=================================================================================
510 void SMESHGUI_BuildCompoundDlg::keyPressEvent( QKeyEvent* e )
511 {
512   QDialog::keyPressEvent( e );
513   if ( e->isAccepted() )
514     return;
515
516   if ( e->key() == Qt::Key_F1 ) {
517     e->accept();
518     ClickOnHelp();
519   }
520 }
521
522
523 //=================================================================================
524 // function : onSelectMerge()
525 // purpose  :
526 //=================================================================================
527 void SMESHGUI_BuildCompoundDlg::onSelectMerge(bool toMerge)
528 {
529   TextLabelTol->setEnabled(toMerge);
530   SpinBoxTol->setEnabled(toMerge);
531   if(!toMerge)
532     SpinBoxTol->SetValue(1e-05);
533 }
534
535 //=================================================================================
536 // function : isValid
537 // purpose  :
538 //=================================================================================
539 bool SMESHGUI_BuildCompoundDlg::isValid()
540 {
541   QString msg;
542   bool ok=true;
543   if(CheckBoxMerge->isChecked())
544     ok = SpinBoxTol->isValid( msg, true );
545
546   if( !ok ) {
547     QString str( tr( "SMESH_INCORRECT_INPUT" ) );
548     if ( !msg.isEmpty() )
549       str += "\n" + msg;
550     SUIT_MessageBox::critical( this, tr( "SMESH_ERROR" ), str );
551     return false;
552   }
553   return true;
554 }
555
556 //================================================================
557 // function : setIsApplyAndClose
558 // Purpose  : Set value of the flag indicating that the dialog is
559 //            accepted by Apply & Close button
560 //================================================================
561 void SMESHGUI_BuildCompoundDlg::setIsApplyAndClose( const bool theFlag )
562 {
563   myIsApplyAndClose = theFlag;
564 }
565
566 //================================================================
567 // function : isApplyAndClose
568 // Purpose  : Get value of the flag indicating that the dialog is
569 //            accepted by Apply & Close button
570 //================================================================
571 bool SMESHGUI_BuildCompoundDlg::isApplyAndClose() const
572 {
573   return myIsApplyAndClose;
574 }