Salome HOME
PR: synchro V6_main tag mergeto_V7_main_11Feb13
[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 (SMESH::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     // obj has been published in study. Its refcount has been incremented.
351     // It is safe to decrement its refcount
352     // so that it will be destroyed when the entry in study will be removed
353     if (!CORBA::is_nil(aCompoundMesh))
354       aCompoundMesh->UnRegister();
355
356     return true;
357   }
358   return false;
359 }
360
361 //=================================================================================
362 // function : ClickOnOk()
363 // purpose  :
364 //=================================================================================
365 void SMESHGUI_BuildCompoundDlg::ClickOnOk()
366 {
367   setIsApplyAndClose( true );
368   if (ClickOnApply())
369     ClickOnCancel();
370 }
371
372 //=================================================================================
373 // function : ClickOnCancel()
374 // purpose  :
375 //=================================================================================
376 void SMESHGUI_BuildCompoundDlg::ClickOnCancel()
377 {
378   //mySelectionMgr->clearSelected();
379   mySelectionMgr->clearFilters();
380   disconnect(mySelectionMgr, 0, this, 0);
381   mySMESHGUI->ResetState();
382   reject();
383 }
384
385 //=================================================================================
386 // function : ClickOnHelp()
387 // purpose  :
388 //=================================================================================
389 void SMESHGUI_BuildCompoundDlg::ClickOnHelp()
390 {
391   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
392   if (app)
393     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
394   else {
395     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
396                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
397                              arg(app->resourceMgr()->stringValue("ExternalBrowser",
398                                                                  "application")).
399                              arg(myHelpFileName));
400   }
401 }
402
403 //=================================================================================
404 // function : SelectionIntoArgument()
405 // purpose  : Called when selection as changed or other case
406 //=================================================================================
407 void SMESHGUI_BuildCompoundDlg::SelectionIntoArgument()
408 {
409   if (!GroupButtons->isEnabled()) // inactive
410     return;
411
412   QString aString = "";
413
414   SALOME_ListIO aList;
415   mySelectionMgr->selectedObjects(aList);
416   int nbSel = SMESH::GetNameOfSelectedIObjects(mySelectionMgr, aString);
417
418   if (nbSel != 0) {
419     myMeshArray->length(nbSel);
420     for (int i = 0; nbSel != 0; i++, nbSel--) {
421       Handle(SALOME_InteractiveObject) IO = aList.First();
422       aList.RemoveFirst();
423       myMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(IO);
424       myMeshArray[i] = myMesh;
425     }
426   }
427   else {
428     myMesh = SMESH::SMESH_Mesh::_nil();
429     aString = "";
430   }
431
432   LineEditMeshes->setText(aString);
433
434   bool isEnabled = (!myMesh->_is_nil());
435   buttonOk->setEnabled(isEnabled);
436   buttonApply->setEnabled(isEnabled);
437 }
438
439 //=================================================================================
440 // function : DeactivateActiveDialog()
441 // purpose  :
442 //=================================================================================
443 void SMESHGUI_BuildCompoundDlg::DeactivateActiveDialog()
444 {
445   if (GroupConstructors->isEnabled()) {
446     GroupConstructors->setEnabled(false);
447     GroupName->setEnabled(false);
448     GroupArgs->setEnabled(false);
449     GroupButtons->setEnabled(false);
450     mySMESHGUI->ResetState();
451     mySMESHGUI->SetActiveDialogBox(0);
452   }
453 }
454
455 //=================================================================================
456 // function : ActivateThisDialog()
457 // purpose  :
458 //=================================================================================
459 void SMESHGUI_BuildCompoundDlg::ActivateThisDialog()
460 {
461   /* Emit a signal to deactivate the active dialog */
462   mySMESHGUI->EmitSignalDeactivateDialog();
463   GroupConstructors->setEnabled(true);
464   GroupName->setEnabled(true);
465   GroupArgs->setEnabled(true);
466   GroupButtons->setEnabled(true);
467
468   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
469   SelectionIntoArgument();
470 }
471
472 //=================================================================================
473 // function : enterEvent()
474 // purpose  :
475 //=================================================================================
476 void SMESHGUI_BuildCompoundDlg::enterEvent( QEvent* )
477 {
478   if (GroupConstructors->isEnabled())
479     return;
480   ActivateThisDialog();
481 }
482
483 //=================================================================================
484 // function : closeEvent()
485 // purpose  :
486 //=================================================================================
487 void SMESHGUI_BuildCompoundDlg::closeEvent( QCloseEvent* )
488 {
489   /* same than click on cancel button */
490   ClickOnCancel();
491 }
492
493 //=======================================================================
494 //function : hideEvent
495 //purpose  : caused by ESC key
496 //=======================================================================
497 void SMESHGUI_BuildCompoundDlg::hideEvent( QHideEvent* )
498 {
499   if (!isMinimized())
500     ClickOnCancel();
501 }
502
503
504 //=================================================================================
505 // function : keyPressEvent()
506 // purpose  :
507 //=================================================================================
508 void SMESHGUI_BuildCompoundDlg::keyPressEvent( QKeyEvent* e )
509 {
510   QDialog::keyPressEvent( e );
511   if ( e->isAccepted() )
512     return;
513
514   if ( e->key() == Qt::Key_F1 ) {
515     e->accept();
516     ClickOnHelp();
517   }
518 }
519
520
521 //=================================================================================
522 // function : onSelectMerge()
523 // purpose  :
524 //=================================================================================
525 void SMESHGUI_BuildCompoundDlg::onSelectMerge(bool toMerge)
526 {
527   TextLabelTol->setEnabled(toMerge);
528   SpinBoxTol->setEnabled(toMerge);
529   if(!toMerge)
530     SpinBoxTol->SetValue(1e-05);
531 }
532
533 //=================================================================================
534 // function : isValid
535 // purpose  :
536 //=================================================================================
537 bool SMESHGUI_BuildCompoundDlg::isValid()
538 {
539   QString msg;
540   bool ok=true;
541   if(CheckBoxMerge->isChecked())
542     ok = SpinBoxTol->isValid( msg, true );
543
544   if( !ok ) {
545     QString str( tr( "SMESH_INCORRECT_INPUT" ) );
546     if ( !msg.isEmpty() )
547       str += "\n" + msg;
548     SUIT_MessageBox::critical( this, tr( "SMESH_ERROR" ), str );
549     return false;
550   }
551   return true;
552 }
553
554 //================================================================
555 // function : setIsApplyAndClose
556 // Purpose  : Set value of the flag indicating that the dialog is
557 //            accepted by Apply & Close button
558 //================================================================
559 void SMESHGUI_BuildCompoundDlg::setIsApplyAndClose( const bool theFlag )
560 {
561   myIsApplyAndClose = theFlag;
562 }
563
564 //================================================================
565 // function : isApplyAndClose
566 // Purpose  : Get value of the flag indicating that the dialog is
567 //            accepted by Apply & Close button
568 //================================================================
569 bool SMESHGUI_BuildCompoundDlg::isApplyAndClose() const
570 {
571   return myIsApplyAndClose;
572 }