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