Salome HOME
Integrated to V7_main: 0022366: EDF SMESH: Create Mesh dialog box improvement: create...
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_MultiEditDlg.cxx
1 // Copyright (C) 2007-2013  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 // File   : SMESHGUI_MultiEditDlg.cxx
24 // Author : Sergey LITONIN, Open CASCADE S.A.S.
25 // SMESH includes
26
27 #include "SMESHGUI_MultiEditDlg.h"
28
29 #include "SMESHGUI.h"
30 #include "SMESHGUI_Filter.h"
31 #include "SMESHGUI_FilterDlg.h"
32 #include "SMESHGUI_Utils.h"
33 #include "SMESHGUI_VTKUtils.h"
34 #include "SMESHGUI_MeshUtils.h"
35 #include "SMESHGUI_FilterUtils.h"
36 #include "SMESHGUI_SpinBox.h"
37 #include "SMESHGUI_MeshEditPreview.h"
38
39 #include <SMESH_Actor.h>
40 #include <SMESH_TypeFilter.hxx>
41 #include <SMDS_Mesh.hxx>
42
43 // SALOME GUI includes
44 #include <SUIT_Desktop.h>
45 #include <SUIT_MessageBox.h>
46 #include <SUIT_OverrideCursor.h>
47 #include <SUIT_ResourceMgr.h>
48 #include <SUIT_Session.h>
49
50 #include <LightApp_SelectionMgr.h>
51 #include <LightApp_Application.h>
52 #include <SALOME_ListIO.hxx>
53 #include <SALOME_ListIteratorOfListIO.hxx>
54 #include <SalomeApp_Tools.h>
55
56 #include <SVTK_Selector.h>
57 #include <SVTK_ViewWindow.h>
58 #include <VTKViewer_CellLocationsArray.h>
59
60 // OCCT includes
61 #include <TColStd_IndexedMapOfInteger.hxx>
62 #include <TColStd_DataMapOfIntegerInteger.hxx>
63 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
64
65 // VTK includes
66 #include <vtkIdList.h>
67 #include <vtkCellArray.h>
68 #include <vtkUnsignedCharArray.h>
69 #include <vtkUnstructuredGrid.h>
70 #include <vtkDataSetMapper.h>
71 #include <vtkProperty.h>
72
73 // Qt includes
74 #include <QLabel>
75 #include <QVBoxLayout>
76 #include <QHBoxLayout>
77 #include <QGridLayout>
78 #include <QListWidget>
79 #include <QCheckBox>
80 #include <QComboBox>
81 #include <QGroupBox>
82 #include <QLineEdit>
83 #include <QPushButton>
84 #include <QButtonGroup>
85 #include <QRadioButton>
86 #include <QKeyEvent>
87
88 // IDL includes
89 #include <SALOMEconfig.h>
90 #include CORBA_SERVER_HEADER(SMESH_Group)
91
92 #define SPACING 6
93 #define MARGIN  11
94
95 /*!
96  *  Class       : SMESHGUI_MultiEditDlg
97  *  Description : Description : Inversion of the diagonal of a pseudo-quadrangle formed by
98  *                2 neighboring triangles with 1 common edge
99  */
100
101 //=======================================================================
102 // name    : SMESHGUI_MultiEditDlg::SMESHGUI_MultiEditDlg
103 // Purpose : Constructor
104 //=======================================================================
105 SMESHGUI_MultiEditDlg
106 ::SMESHGUI_MultiEditDlg(SMESHGUI* theModule,
107                         const int theMode,
108                         const bool the3d2d):
109   SMESHGUI_PreviewDlg(theModule),
110   mySelector(SMESH::GetViewWindow(theModule)->GetSelector()),
111   mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
112   mySMESHGUI(theModule)
113 {
114   setModal(false);
115   setAttribute(Qt::WA_DeleteOnClose, true);
116
117   myFilterDlg = 0;
118   myEntityType = 0;
119
120   myFilterType = theMode;
121   QVBoxLayout* aDlgLay = new QVBoxLayout(this);
122   aDlgLay->setMargin(MARGIN);
123   aDlgLay->setSpacing(SPACING);
124
125   QWidget* aMainFrame = createMainFrame  (this, the3d2d);
126   QWidget* aBtnFrame  = createButtonFrame(this);
127
128   aDlgLay->addWidget(aMainFrame);
129   aDlgLay->addWidget(aBtnFrame);
130
131   Init();
132 }
133
134 //=======================================================================
135 // name    : SMESHGUI_MultiEditDlg::createMainFrame
136 // Purpose : Create frame containing dialog's input fields
137 //=======================================================================
138 QWidget* SMESHGUI_MultiEditDlg::createMainFrame (QWidget* theParent, const bool the3d2d)
139 {
140   QWidget* aMainGrp = new QWidget(theParent);
141   QVBoxLayout* aMainGrpLayout = new QVBoxLayout(aMainGrp);
142   aMainGrpLayout->setMargin(0);
143   aMainGrpLayout->setSpacing(SPACING);
144
145   QPixmap aPix (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
146
147   // "Selected cells" group
148   mySelGrp = new QGroupBox(aMainGrp);
149   
150   QGridLayout* mySelGrpLayout = new QGridLayout(mySelGrp);
151   mySelGrpLayout->setMargin(MARGIN);
152   mySelGrpLayout->setSpacing(SPACING);
153
154   myEntityTypeGrp = 0;
155   if (the3d2d) {
156     QGroupBox* aEntityTypeGrp = new QGroupBox(tr("SMESH_ELEMENTS_TYPE"), mySelGrp);
157     myEntityTypeGrp = new QButtonGroup(mySelGrp);
158     QHBoxLayout* aEntityLayout = new QHBoxLayout(aEntityTypeGrp);
159     aEntityLayout->setMargin(MARGIN);
160     aEntityLayout->setSpacing(SPACING);
161
162     QRadioButton* aFaceRb = new QRadioButton(tr("SMESH_FACE"), aEntityTypeGrp);
163     QRadioButton* aVolumeRb = new QRadioButton(tr("SMESH_VOLUME"), aEntityTypeGrp);
164
165     aEntityLayout->addWidget(aFaceRb);
166     aEntityLayout->addWidget(aVolumeRb);
167
168     myEntityTypeGrp->addButton(aFaceRb, 0);
169     myEntityTypeGrp->addButton(aVolumeRb, 1);
170     aFaceRb->setChecked(true);
171     myEntityType = myEntityTypeGrp->checkedId();
172
173     mySelGrpLayout->addWidget(aEntityTypeGrp, 0, 0, 1, 2);
174   }
175
176   myListBox = new QListWidget(mySelGrp);
177   myListBox->setSelectionMode(QListWidget::ExtendedSelection);
178   myListBox->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
179   myListBox->installEventFilter(this);
180
181   myFilterBtn = new QPushButton(tr("FILTER"),    mySelGrp);
182   myAddBtn    = new QPushButton(tr("ADD"),       mySelGrp);
183   myRemoveBtn = new QPushButton(tr("REMOVE"),    mySelGrp);
184   mySortBtn   = new QPushButton(tr("SORT_LIST"), mySelGrp);
185
186   int row = mySelGrpLayout->rowCount();
187   mySelGrpLayout->addWidget(myListBox,   row,   0, 6, 1);
188   mySelGrpLayout->addWidget(myFilterBtn, row,   1);
189   mySelGrpLayout->addWidget(myAddBtn,    row+2, 1);
190   mySelGrpLayout->addWidget(myRemoveBtn, row+3, 1);
191   mySelGrpLayout->addWidget(mySortBtn,   row+5, 1);
192   mySelGrpLayout->setRowMinimumHeight(row+1, 10);
193   mySelGrpLayout->setRowMinimumHeight(row+4, 10);
194   mySelGrpLayout->setRowStretch(row+1, 5);
195   mySelGrpLayout->setRowStretch(row+4, 5);
196
197   myToAllChk = new QCheckBox(tr("TO_ALL"), mySelGrp);
198   mySelGrpLayout->addWidget(myToAllChk, mySelGrpLayout->rowCount(), 0, 
199                             1, mySelGrpLayout->columnCount());
200
201   // Split/Join criterion group
202   myCriterionGrp = new QGroupBox(tr("SPLIT_JOIN_CRITERION"), aMainGrp);
203   QVBoxLayout* aCriterionLayout = new QVBoxLayout(myCriterionGrp);
204   aCriterionLayout->setMargin(MARGIN);
205   aCriterionLayout->setSpacing(SPACING);
206   
207   myChoiceWidget = new QWidget(myCriterionGrp);
208   myGroupChoice = new QButtonGroup(myChoiceWidget);
209   QVBoxLayout* aGroupChoiceLayout = new QVBoxLayout(myChoiceWidget);
210   aGroupChoiceLayout->setMargin(0);
211   aGroupChoiceLayout->setSpacing(SPACING);
212
213   QRadioButton* aDiag13RB  = new QRadioButton(tr("USE_DIAGONAL_1_3"), myChoiceWidget);
214   QRadioButton* aDiag24RB  = new QRadioButton(tr("USE_DIAGONAL_2_4"), myChoiceWidget);
215   QRadioButton* aNumFuncRB = new QRadioButton(tr("USE_NUMERIC_FUNC"), myChoiceWidget);
216
217   aGroupChoiceLayout->addWidget(aDiag13RB);
218   aGroupChoiceLayout->addWidget(aDiag24RB);
219   aGroupChoiceLayout->addWidget(aNumFuncRB);
220   myGroupChoice->addButton(aDiag13RB,  0);
221   myGroupChoice->addButton(aDiag24RB,  1);
222   myGroupChoice->addButton(aNumFuncRB, 2);
223   aDiag13RB->setChecked(true);
224
225   myComboBoxFunctor = new QComboBox(myCriterionGrp);
226   myComboBoxFunctor->addItem(tr("ASPECTRATIO_ELEMENTS"));
227   myComboBoxFunctor->addItem(tr("MINIMUMANGLE_ELEMENTS"));
228   myComboBoxFunctor->addItem(tr("SKEW_ELEMENTS"));
229   //myComboBoxFunctor->addItem(tr("AREA_ELEMENTS"));
230   //myComboBoxFunctor->addItem(tr("LENGTH2D_EDGES")); // for existing elements only
231   //myComboBoxFunctor->addItem(tr("MULTI2D_BORDERS")); // for existing elements only
232   myComboBoxFunctor->setCurrentIndex(0);
233
234   aCriterionLayout->addWidget(myChoiceWidget);
235   aCriterionLayout->addWidget(myComboBoxFunctor);
236
237   myCriterionGrp->hide();
238   myChoiceWidget->hide();
239   myComboBoxFunctor->setEnabled(false);
240
241   // "Select from" group
242   QGroupBox* aGrp = new QGroupBox(tr("SELECT_FROM"), aMainGrp);
243   QGridLayout* aGrpLayout = new QGridLayout(aGrp);
244   aGrpLayout->setMargin(MARGIN);
245   aGrpLayout->setSpacing(SPACING);
246
247   mySubmeshChk = new QCheckBox(tr("SMESH_SUBMESH"), aGrp);
248   mySubmeshBtn = new QPushButton(aGrp);
249   mySubmesh = new QLineEdit(aGrp);
250   mySubmesh->setReadOnly(true);
251   mySubmeshBtn->setIcon(aPix);
252
253   myGroupChk = new QCheckBox(tr("SMESH_GROUP"), aGrp);
254   myGroupBtn = new QPushButton(aGrp);
255   myGroup = new QLineEdit(aGrp);
256   myGroup->setReadOnly(true);
257   myGroupBtn->setIcon(aPix);
258
259   aGrpLayout->addWidget(mySubmeshChk,0,0);
260   aGrpLayout->addWidget(mySubmeshBtn,0,1);
261   aGrpLayout->addWidget(mySubmesh,0,2);
262   aGrpLayout->addWidget(myGroupChk,1,0);
263   aGrpLayout->addWidget(myGroupBtn,1,1);
264   aGrpLayout->addWidget(myGroup,1,2);
265   
266   aMainGrpLayout->addWidget(mySelGrp);
267   aMainGrpLayout->addWidget(myCriterionGrp);
268   aMainGrpLayout->addWidget(aGrp);
269
270   return aMainGrp;
271 }
272
273 //=======================================================================
274 // name    : SMESHGUI_MultiEditDlg::createButtonFrame
275 // Purpose : Create frame containing buttons
276 //=======================================================================
277 QWidget* SMESHGUI_MultiEditDlg::createButtonFrame (QWidget* theParent)
278 {
279   QGroupBox* aFrame = new QGroupBox(theParent);
280
281   myOkBtn     = new QPushButton (tr("SMESH_BUT_APPLY_AND_CLOSE"), aFrame);
282   myApplyBtn  = new QPushButton (tr("SMESH_BUT_APPLY"), aFrame);
283   myCloseBtn  = new QPushButton (tr("SMESH_BUT_CLOSE"), aFrame);
284   myHelpBtn   = new QPushButton (tr("SMESH_BUT_HELP"),  aFrame);
285
286   QHBoxLayout* aLay = new QHBoxLayout (aFrame);
287   aLay->setMargin(MARGIN);
288   aLay->setSpacing(SPACING);
289
290   aLay->addWidget(myOkBtn);
291   aLay->addSpacing(10);
292   aLay->addWidget(myApplyBtn);
293   aLay->addSpacing(10);
294   aLay->addStretch();
295   aLay->addWidget(myCloseBtn);
296   aLay->addWidget(myHelpBtn);
297
298   return aFrame;
299 }
300
301 //=======================================================================
302 // name    : SMESHGUI_MultiEditDlg::isValid
303 // Purpose : Verify validity of input data
304 //=======================================================================
305 bool SMESHGUI_MultiEditDlg::isValid (const bool /*theMess*/)
306 {
307   return (!myMesh->_is_nil() &&
308           (myListBox->count() > 0 || (myToAllChk->isChecked()/* && myActor*/)));
309 }
310
311 //=======================================================================
312 // name    : SMESHGUI_MultiEditDlg::~SMESHGUI_MultiEditDlg
313 // Purpose : Destructor
314 //=======================================================================
315 SMESHGUI_MultiEditDlg::~SMESHGUI_MultiEditDlg()
316 {
317   if (myFilterDlg != 0)
318   {
319     myFilterDlg->setParent(0);
320     delete myFilterDlg;
321   }
322 }
323
324 //=======================================================================
325 // name    : SMESHGUI_MultiEditDlg::eventFilter
326 // Purpose : event filter
327 //=======================================================================
328 bool SMESHGUI_MultiEditDlg::eventFilter (QObject* object, QEvent* event)
329 {
330   if (object == myListBox && event->type() == QEvent::KeyPress) {
331     QKeyEvent* ke = (QKeyEvent*)event;
332     if (ke->key() == Qt::Key_Delete)
333       onRemoveBtn();
334   }
335   return QDialog::eventFilter(object, event);
336 }
337
338 //=======================================================================
339 // name    : SMESHGUI_MultiEditDlg::getNumericalFunctor
340 // Purpose :
341 //=======================================================================
342 SMESH::NumericalFunctor_ptr SMESHGUI_MultiEditDlg::getNumericalFunctor()
343 {
344   SMESH::NumericalFunctor_var aNF = SMESH::NumericalFunctor::_nil();
345
346   SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
347   if (aFilterMgr->_is_nil())
348     return aNF._retn();
349
350   if (myComboBoxFunctor->currentText() == tr("ASPECTRATIO_ELEMENTS"))
351     aNF = aFilterMgr->CreateAspectRatio();
352   else if (myComboBoxFunctor->currentText() == tr("WARP_ELEMENTS"))
353     aNF = aFilterMgr->CreateWarping();
354   else if (myComboBoxFunctor->currentText() == tr("MINIMUMANGLE_ELEMENTS"))
355     aNF = aFilterMgr->CreateMinimumAngle();
356   else if (myComboBoxFunctor->currentText() == tr("TAPER_ELEMENTS"))
357     aNF = aFilterMgr->CreateTaper();
358   else if (myComboBoxFunctor->currentText() == tr("SKEW_ELEMENTS"))
359     aNF = aFilterMgr->CreateSkew();
360   else if (myComboBoxFunctor->currentText() == tr("AREA_ELEMENTS"))
361     aNF = aFilterMgr->CreateArea();
362   else if (myComboBoxFunctor->currentText() == tr("LENGTH2D_EDGES"))
363     aNF = aFilterMgr->CreateLength2D();
364   else if (myComboBoxFunctor->currentText() == tr("MULTI2D_BORDERS"))
365     aNF = aFilterMgr->CreateMultiConnection2D();
366   else if (myComboBoxFunctor->currentText() == tr("MIN_DIAG_ELEMENTS"))
367     aNF = aFilterMgr->CreateMaxElementLength2D();
368   else;
369
370   return aNF._retn();
371 }
372
373 //=======================================================================
374 // name    : SMESHGUI_MultiEditDlg::Init
375 // Purpose : Init dialog fields, connect signals and slots, show dialog
376 //=======================================================================
377 void SMESHGUI_MultiEditDlg::Init()
378 {
379   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
380   myListBox->clear();
381   myIds.Clear();
382   myBusy = false;
383   myActor = 0;
384   emit ListContensChanged();
385
386   // main buttons
387   connect(myOkBtn,    SIGNAL(clicked()), SLOT(onOk()));
388   connect(myCloseBtn, SIGNAL(clicked()), SLOT(reject()));
389   connect(myApplyBtn, SIGNAL(clicked()), SLOT(onApply()));
390   connect(myHelpBtn,  SIGNAL(clicked()), SLOT(onHelp()));
391
392   // selection and SMESHGUI
393   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
394   connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
395   connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(reject()));
396
397   // dialog controls
398   connect(myFilterBtn, SIGNAL(clicked()), SLOT(onFilterBtn()  ));
399   connect(myAddBtn,    SIGNAL(clicked()), SLOT(onAddBtn()     ));
400   connect(myRemoveBtn, SIGNAL(clicked()), SLOT(onRemoveBtn()  ));
401   connect(mySortBtn,   SIGNAL(clicked()), SLOT(onSortListBtn()));
402
403   connect(mySubmeshChk, SIGNAL(stateChanged(int)), SLOT(onSubmeshChk()));
404   connect(myGroupChk,   SIGNAL(stateChanged(int)), SLOT(onGroupChk()  ));
405   connect(myToAllChk,   SIGNAL(stateChanged(int)), SLOT(onToAllChk()  ));
406
407   if (myEntityTypeGrp)
408     connect(myEntityTypeGrp, SIGNAL(buttonClicked(int)), SLOT(on3d2dChanged(int)));
409
410   connect(myListBox, SIGNAL(itemSelectionChanged()), SLOT(onListSelectionChanged()));
411
412   onSelectionDone();
413
414   // set selection mode
415   setSelectionMode();
416   updateButtons();
417 }
418
419 //=======================================================================
420 // name    : SMESHGUI_MultiEditDlg::onOk
421 // Purpose : SLOT called when "Ok" button pressed.
422 //           Assign filters VTK viewer and close dialog
423 //=======================================================================
424 void SMESHGUI_MultiEditDlg::onOk()
425 {
426   if (onApply())
427     reject();
428 }
429
430 //=======================================================================
431 // name    : SMESHGUI_MultiEditDlg::getIds
432 // Purpose : Retrive identifiers from list box or the whole object
433 //=======================================================================
434
435 SMESH::long_array_var SMESHGUI_MultiEditDlg::getIds(SMESH::SMESH_IDSource_var& obj)
436 {
437   SMESH::long_array_var anIds = new SMESH::long_array;
438
439   if (myToAllChk->isChecked())
440   {
441     myIds.Clear();
442     obj = SMESH::SMESH_IDSource::_narrow( myMesh );
443   }
444
445   anIds->length(myIds.Extent());
446   TColStd_MapIteratorOfMapOfInteger anIter(myIds);
447   for (int i = 0; anIter.More(); anIter.Next() )
448   {
449     anIds[ i++ ] = anIter.Key();
450   }
451   return anIds._retn();
452 }
453
454 //=======================================================================
455 // name    : SMESHGUI_MultiEditDlg::reject
456 // Purpose : SLOT called when "Close" button pressed. Close dialog
457 //=======================================================================
458 void SMESHGUI_MultiEditDlg::reject()
459 {
460   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
461     aViewWindow->SetSelectionMode(ActorSelection);
462   disconnect(mySelectionMgr, 0, this, 0);
463   disconnect(mySMESHGUI, 0, this, 0);
464   mySMESHGUI->ResetState();
465
466   SMESH::RemoveFilters();
467   SMESH::SetPickable();
468
469   //mySelectionMgr->clearSelected();
470   mySelectionMgr->clearFilters();
471
472   QDialog::reject();
473 }
474
475 //=================================================================================
476 // function : onHelp()
477 // purpose  :
478 //=================================================================================
479 void SMESHGUI_MultiEditDlg::onHelp()
480 {
481   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
482   if (app) 
483     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
484   else {
485     QString platform;
486 #ifdef WIN32
487     platform = "winapplication";
488 #else
489     platform = "application";
490 #endif
491     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
492                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
493                              arg(app->resourceMgr()->stringValue("ExternalBrowser", 
494                                                                  platform)).
495                              arg(myHelpFileName));
496   }
497 }
498
499 //=======================================================================
500 // name    : SMESHGUI_MultiEditDlg::onSelectionDone
501 // Purpose : SLOT called when selection changed
502 //=======================================================================
503 void SMESHGUI_MultiEditDlg::onSelectionDone()
504 {
505   if (myBusy || !isEnabled()) return;
506   myBusy = true;
507
508   const SALOME_ListIO& aList = mySelector->StoredIObjects();
509
510   int nbSel = aList.Extent();
511   myListBox->clearSelection();
512
513   if (mySubmeshChk->isChecked() || myGroupChk->isChecked()) {
514     QLineEdit* aNameEdit = mySubmeshChk->isChecked() ? mySubmesh : myGroup;
515     if (nbSel == 1) {
516       Handle(SALOME_InteractiveObject) anIO = aList.First();
517       QString aName = "";
518       SMESH::GetNameOfSelectedIObjects(mySelectionMgr, aName);
519       anIO.IsNull() ? aNameEdit->clear() : aNameEdit->setText(aName);
520
521       if (mySubmeshChk->isChecked()) {
522         SMESH::SMESH_subMesh_var aSubMesh =
523           SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIO);
524         if (!aSubMesh->_is_nil())
525           myMesh = aSubMesh->GetFather();
526       } else {
527         SMESH::SMESH_GroupBase_var aGroup =
528           SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(anIO);
529         if (!aGroup->_is_nil())
530           myMesh = aGroup->GetMesh();
531       }
532     } else if (nbSel > 1) {
533       QString aStr = mySubmeshChk->isChecked() ?
534         tr("SMESH_SUBMESH_SELECTED") : tr("SMESH_GROUP_SELECTED");
535       aNameEdit->setText(aStr.arg(nbSel));
536     } else {
537       aNameEdit->clear();
538     }
539   } else if (nbSel > 0) {
540     QString aListStr = "";
541     Handle(SALOME_InteractiveObject) anIO = aList.First();
542     int aNbItems = SMESH::GetNameOfSelectedElements(mySelector,anIO,aListStr);
543     if (aNbItems > 0) {
544       QStringList anElements = aListStr.split(" ", QString::SkipEmptyParts);
545       for (QStringList::iterator it = anElements.begin(); it != anElements.end(); ++it) {
546         QList<QListWidgetItem*> items = myListBox->findItems(*it, Qt::MatchExactly);
547         QListWidgetItem* anItem;
548         foreach(anItem, items)
549           anItem->setSelected(true);
550       }
551     }
552     SMESH::SMESH_Mesh_var aSelMesh = SMESH::GetMeshByIO(anIO);
553     if (!aSelMesh->_is_nil())
554       myMesh = aSelMesh;
555   }
556
557   if (nbSel > 0) {
558     myActor = SMESH::FindActorByEntry(aList.First()->getEntry());
559     if (!myActor)
560       myActor = SMESH::FindActorByObject(myMesh);
561     SVTK_Selector* aSelector = SMESH::GetSelector();
562     Handle(VTKViewer_Filter) aFilter = aSelector->GetFilter(myFilterType);
563     if (!aFilter.IsNull())
564       aFilter->SetActor(myActor);
565   }
566   myBusy = false;
567
568   updateButtons();
569 }
570
571 //=======================================================================
572 // name    : SMESHGUI_MultiEditDlg::onDeactivate
573 // Purpose : SLOT called when dialog must be deativated
574 //=======================================================================
575 void SMESHGUI_MultiEditDlg::onDeactivate()
576 {
577   setEnabled(false);
578 }
579
580 //=======================================================================
581 // name    : SMESHGUI_MultiEditDlg::enterEvent
582 // Purpose : Event filter
583 //=======================================================================
584 void SMESHGUI_MultiEditDlg::enterEvent (QEvent*)
585 {
586   if (!isEnabled()) {
587     mySMESHGUI->EmitSignalDeactivateDialog();
588     setEnabled(true);
589     setSelectionMode();
590   }
591 }
592
593 //=======================================================================
594 // name    : SMESHGUI_MultiEditDlg::onFilterBtn
595 // Purpose : SLOT. Called when "Filter" button pressed.
596 //           Start "Selection filters" dialog
597 //=======================================================================
598 void SMESHGUI_MultiEditDlg::onFilterBtn()
599 {
600   if (myFilterDlg == 0) {
601     myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, entityType() ? SMESH::VOLUME : SMESH::FACE);
602     connect(myFilterDlg, SIGNAL(Accepted()), SLOT(onFilterAccepted()));
603   } else {
604     myFilterDlg->Init(entityType() ? SMESH::VOLUME : SMESH::FACE);
605   }
606
607   myFilterDlg->SetSelection();
608   myFilterDlg->SetMesh(myMesh);
609   myFilterDlg->SetSourceWg(myListBox, false);
610
611   myFilterDlg->show();
612 }
613
614 //=======================================================================
615 // name    : onFilterAccepted()
616 // Purpose : SLOT. Called when Filter dlg closed with OK button.
617 //            Uncheck "Select submesh" and "Select group" checkboxes
618 //=======================================================================
619 void SMESHGUI_MultiEditDlg::onFilterAccepted()
620 {
621   myIds.Clear();
622   for (int i = 0, n = myListBox->count(); i < n; i++)
623     myIds.Add(myListBox->item(i)->text().toInt());
624
625   emit ListContensChanged();
626
627   if (mySubmeshChk->isChecked() || myGroupChk->isChecked()) {
628     mySubmeshChk->blockSignals(true);
629     myGroupChk->blockSignals(true);
630     mySubmeshChk->setChecked(false);
631     myGroupChk->setChecked(false);
632     mySubmeshChk->blockSignals(false);
633     myGroupChk->blockSignals(false);
634   }
635   updateButtons();
636 }
637
638 //=======================================================================
639 // name    : SMESHGUI_MultiEditDlg::isIdValid
640 // Purpose : Verify whether Id of element satisfies to filters from viewer
641 //=======================================================================
642 bool SMESHGUI_MultiEditDlg::isIdValid (const int theId) const
643 {
644   SVTK_Selector* aSelector = SMESH::GetSelector();
645   Handle(SMESHGUI_Filter) aFilter =
646     Handle(SMESHGUI_Filter)::DownCast(aSelector->GetFilter(myFilterType));
647
648   return (!aFilter.IsNull() && aFilter->IsObjValid(theId));
649 }
650
651 //=======================================================================
652 // name    : SMESHGUI_MultiEditDlg::onAddBtn
653 // Purpose : SLOT. Called when "Add" button pressed.
654 //           Add selected in viewer entities in list box
655 //=======================================================================
656 void SMESHGUI_MultiEditDlg::onAddBtn()
657 {
658   const SALOME_ListIO& aList = mySelector->StoredIObjects();
659
660   int nbSelected = aList.Extent();
661   if (nbSelected == 0)
662     return;
663
664   TColStd_IndexedMapOfInteger toBeAdded;
665
666   if (!mySubmeshChk->isChecked() && !myGroupChk->isChecked()) {
667     if (nbSelected > 0)
668       mySelector->GetIndex(aList.First(),toBeAdded);
669   } else if (mySubmeshChk->isChecked()) {
670     SALOME_ListIteratorOfListIO anIter(aList);
671     for ( ; anIter.More(); anIter.Next()) {
672       SMESH::SMESH_subMesh_var aSubMesh =
673         SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIter.Value());
674       if (!aSubMesh->_is_nil()) {
675         if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
676           SMESH::long_array_var anIds = aSubMesh->GetElementsId();
677           for (int i = 0, n = anIds->length(); i < n; i++) {
678             if (isIdValid(anIds[ i ]))
679               toBeAdded.Add(anIds[ i ]);
680           }
681         }
682       }
683     }
684   } else if (myGroupChk->isChecked()) {
685     SALOME_ListIteratorOfListIO anIter(aList);
686     for ( ; anIter.More(); anIter.Next()) {
687       SMESH::SMESH_GroupBase_var aGroup =
688         SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(anIter.Value());
689       if (!aGroup->_is_nil() && ((aGroup->GetType() == SMESH::FACE && entityType() == 0) || 
690                                  (aGroup->GetType() == SMESH::VOLUME && entityType() == 1))) {
691         if (aGroup->GetMesh()->GetId() == myMesh->GetId()) {
692           SMESH::long_array_var anIds = aGroup->GetListOfID();
693           for (int i = 0, n = anIds->length(); i < n; i++) {
694             if (isIdValid(anIds[ i ]))
695               toBeAdded.Add(anIds[ i ]);
696           }
697         }
698       }
699     }
700   } else {
701   }
702
703   myBusy = true;
704   bool isGroupOrSubmesh = (mySubmeshChk->isChecked() || myGroupChk->isChecked());
705   mySubmeshChk->setChecked(false);
706   myGroupChk->setChecked(false);
707   QStringList items;
708   for(int i = 1; i <= toBeAdded.Extent(); i++)
709     if (myIds.Add(toBeAdded(i))) {
710       items.append(QString("%1").arg(toBeAdded(i)));
711     }
712   myListBox->addItems(items);
713   myListBox->selectAll();
714   myBusy = false;
715
716   emit ListContensChanged();
717
718   if (isGroupOrSubmesh)
719     onListSelectionChanged();
720
721   updateButtons();
722 }
723
724 //=======================================================================
725 // name    : SMESHGUI_MultiEditDlg::updateButtons
726 // Purpose : Enable/disable buttons of dialog in accordance with current state
727 //=======================================================================
728 void SMESHGUI_MultiEditDlg::updateButtons()
729 {
730   bool isOk = isValid(false);
731   myOkBtn->setEnabled(isOk);
732   myApplyBtn->setEnabled(isOk);
733
734   bool isListBoxNonEmpty = myListBox->count() > 0;
735   bool isToAll = myToAllChk->isChecked();
736   myFilterBtn->setEnabled(!isToAll);
737   myRemoveBtn->setEnabled(isListBoxNonEmpty && !isToAll);
738   mySortBtn->setEnabled(isListBoxNonEmpty &&!isToAll);
739
740   const SALOME_ListIO& aList = mySelector->StoredIObjects();
741
742   if (isToAll ||
743       myMesh->_is_nil() ||
744       aList.Extent() < 1 ||
745       (SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(aList.First())->_is_nil() &&
746        SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(aList.First())->_is_nil() &&
747        SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(aList.First())->_is_nil()))
748     myAddBtn->setEnabled(false);
749   else
750     myAddBtn->setEnabled(true);
751
752   mySubmeshChk->setEnabled(!isToAll);
753   mySubmeshBtn->setEnabled(mySubmeshChk->isChecked());
754   mySubmesh->setEnabled(mySubmeshChk->isChecked());
755
756   myGroupChk->setEnabled(!isToAll);
757   myGroupBtn->setEnabled(myGroupChk->isChecked());
758   myGroup->setEnabled(myGroupChk->isChecked());
759
760   if (!mySubmeshChk->isChecked())
761     mySubmesh->clear();
762   if (!myGroupChk->isChecked())
763     myGroup->clear();
764
765 }
766
767 //=======================================================================
768 // name    : SMESHGUI_MultiEditDlg::onRemoveBtn
769 // Purpose : SLOT. Called when "Remove" button pressed.
770 //           Remove selected in list box entities
771 //=======================================================================
772 void SMESHGUI_MultiEditDlg::onRemoveBtn()
773 {
774   myBusy = true;
775
776   QList<QListWidgetItem*> selItems = myListBox->selectedItems();
777   QListWidgetItem* item;
778   foreach(item, selItems)
779   {
780     myIds.Remove(item->text().toInt());
781     delete item;
782   }
783
784   myBusy = false;
785
786   emit ListContensChanged();
787   updateButtons();
788 }
789
790 //=======================================================================
791 // name    : SMESHGUI_MultiEditDlg::onSortListBtn
792 // Purpose : SLOT. Called when "Sort list" button pressed.
793 //           Sort entities of list box
794 //=======================================================================
795 void SMESHGUI_MultiEditDlg::onSortListBtn()
796 {
797   myBusy = true;
798
799   int i, k = myListBox->count();
800   if (k > 0)
801   {
802     QList<int> aSelected;
803     std::vector<int> anArray(k);
804     for (i = 0; i < k; i++)
805     {
806       int id = myListBox->item(i)->text().toInt();
807       anArray[ i ] = id;
808       if (myListBox->item(i)->isSelected())
809         aSelected.append(id);
810     }
811
812     std::sort(anArray.begin(), anArray.end());
813
814     myListBox->clear();
815     for (i = 0; i < k; i++) {
816       QListWidgetItem* item = new QListWidgetItem(QString::number(anArray[i]));
817       myListBox->addItem(item);
818       item->setSelected(aSelected.contains(anArray[i]));
819     }
820   }
821   myBusy = false;
822 }
823
824 //=======================================================================
825 // name    : SMESHGUI_MultiEditDlg::onListSelectionChanged
826 // Purpose : SLOT. Called when selection in list box changed.
827 //           Highlight in selected entities
828 //=======================================================================
829 void SMESHGUI_MultiEditDlg::onListSelectionChanged()
830 {
831   if (myActor == 0 || myBusy)
832     return;
833
834   if (mySubmeshChk->isChecked() || myGroupChk->isChecked())
835     return;
836
837   SMESH_Actor * anActor = SMESH::FindActorByObject(myMesh);
838   if (!anActor)
839     anActor = myActor;
840   TVisualObjPtr anObj = anActor->GetObject();
841
842   TColStd_MapOfInteger anIndexes;
843   int total = myListBox->count();
844   for (int i = 0; i < total; i++)
845   {
846     if (myListBox->item(i)->isSelected())
847     {
848       int anId = myListBox->item(i)->text().toInt();
849       if (anObj->GetElemVTKId(anId) >= 0) // avoid exception in hilight
850         anIndexes.Add(anId);
851     }
852   }
853
854   mySelector->AddOrRemoveIndex(anActor->getIO(),anIndexes,false);
855   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
856     aViewWindow->highlight(anActor->getIO(),true,true);
857 }
858
859 //=======================================================================
860 // name    : SMESHGUI_MultiEditDlg::onSubmeshChk
861 // Purpose : SLOT. Called when state of "SubMesh" check box changed.
862 //           Activate/deactivate selection of submeshes
863 //=======================================================================
864 void SMESHGUI_MultiEditDlg::onSubmeshChk()
865 {
866   bool isChecked = mySubmeshChk->isChecked();
867   mySubmeshBtn->setEnabled(isChecked);
868   mySubmesh->setEnabled(isChecked);
869   if (!isChecked)
870     mySubmesh->clear();
871   if (isChecked && myGroupChk->isChecked())
872       myGroupChk->setChecked(false);
873
874   setSelectionMode();
875 }
876
877 //=======================================================================
878 // name    : SMESHGUI_MultiEditDlg::onGroupChk
879 // Purpose : SLOT. Called when state of "Group" check box changed.
880 //           Activate/deactivate selection of groupes
881 //=======================================================================
882 void SMESHGUI_MultiEditDlg::onGroupChk()
883 {
884   bool isChecked = myGroupChk->isChecked();
885   myGroupBtn->setEnabled(isChecked);
886   myGroup->setEnabled(isChecked);
887   if (!isChecked)
888     myGroup->clear();
889   if (isChecked && mySubmeshChk->isChecked())
890       mySubmeshChk->setChecked(false);
891
892   setSelectionMode();
893 }
894
895 //=======================================================================
896 // name    : SMESHGUI_MultiEditDlg::onToAllChk
897 // Purpose : SLOT. Called when state of "Apply to all" check box changed.
898 //           Activate/deactivate selection
899 //=======================================================================
900 void SMESHGUI_MultiEditDlg::onToAllChk()
901 {
902   bool isChecked = myToAllChk->isChecked();
903
904   if (isChecked)
905     myListBox->clear();
906
907   myIds.Clear();
908
909   emit ListContensChanged();
910
911   updateButtons();
912   setSelectionMode();
913 }
914
915 //=======================================================================
916 // name    : SMESHGUI_MultiEditDlg::setSelectionMode
917 // Purpose : Set selection mode
918 //=======================================================================
919 void SMESHGUI_MultiEditDlg::setSelectionMode()
920 {
921   SMESH::RemoveFilters();
922
923   //  mySelectionMgr->clearSelected();
924   mySelectionMgr->clearFilters();
925
926   if (mySubmeshChk->isChecked()) {
927     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
928       aViewWindow->SetSelectionMode(ActorSelection);
929     mySelectionMgr->installFilter(new SMESH_TypeFilter(SMESH::SUBMESH));
930   }
931   else if (myGroupChk->isChecked()) {
932     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
933       aViewWindow->SetSelectionMode(ActorSelection);
934     mySelectionMgr->installFilter(new SMESH_TypeFilter(SMESH::GROUP));
935   }
936
937   if (entityType()) {
938     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
939       aViewWindow->SetSelectionMode(VolumeSelection);
940     SMESH::SetFilter(new SMESHGUI_VolumesFilter());
941   } else {
942     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
943       aViewWindow->SetSelectionMode(FaceSelection);
944     if (myFilterType == SMESH::TriaFilter)
945       SMESH::SetFilter(new SMESHGUI_TriangleFilter());
946     else if (myFilterType == SMESH::QuadFilter)
947       SMESH::SetFilter(new SMESHGUI_QuadrangleFilter());
948     else
949       SMESH::SetFilter(new SMESHGUI_FacesFilter());
950   }
951 }
952
953 //=======================================================================
954 // name    : SMESHGUI_MultiEditDlg::onApply
955 // Purpose : SLOT. Called when "Apply" button clicked.
956 //=======================================================================
957 bool SMESHGUI_MultiEditDlg::onApply()
958 {
959   if (mySMESHGUI->isActiveStudyLocked())
960     return false;
961   if (!isValid(true))
962     return false;
963
964   SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
965   if (aMeshEditor->_is_nil())
966     return false;
967
968   myBusy = true;
969
970   SUIT_OverrideCursor aWaitCursor;
971
972   SMESH::SMESH_IDSource_var obj;
973   SMESH::long_array_var anIds = getIds(obj);
974
975   bool aResult = process(aMeshEditor, anIds.inout(), obj);
976   if (aResult) {
977     if (myActor) {
978       SALOME_ListIO sel;
979       mySelectionMgr->selectedObjects( sel );
980       mySelector->ClearIndex();
981       mySelectionMgr->setSelectedObjects( sel );
982       SMESH::UpdateView();
983       SMESHGUI::Modified();
984     }
985
986     myListBox->clear();
987     myIds.Clear();
988     emit ListContensChanged();
989
990     updateButtons();
991   }
992
993   myBusy = false;
994   return aResult;
995 }
996
997 //=======================================================================
998 // name    : SMESHGUI_MultiEditDlg::on3d2dChanged
999 // Purpose :
1000 //=======================================================================
1001 void SMESHGUI_MultiEditDlg::on3d2dChanged (int type)
1002 {
1003   if (myEntityType != type) {
1004     myEntityType = type;
1005
1006     myListBox->clear();
1007     myIds.Clear();
1008
1009     emit ListContensChanged();
1010
1011     if (type)
1012       myFilterType = SMESH::VolumeFilter;
1013     else
1014       myFilterType = SMESH::FaceFilter;
1015
1016     updateButtons();
1017     setSelectionMode();
1018   }
1019 }
1020
1021 //=======================================================================
1022 // name    : SMESHGUI_MultiEditDlg::entityType
1023 // Purpose :
1024 //=======================================================================
1025 int SMESHGUI_MultiEditDlg::entityType()
1026 {
1027   return myEntityType;
1028 }
1029
1030 //=================================================================================
1031 // function : keyPressEvent()
1032 // purpose  :
1033 //=================================================================================
1034 void SMESHGUI_MultiEditDlg::keyPressEvent( QKeyEvent* e )
1035 {
1036   QDialog::keyPressEvent( e );
1037   if ( e->isAccepted() )
1038     return;
1039
1040   if ( e->key() == Qt::Key_F1 ) {
1041     e->accept();
1042     onHelp();
1043   }
1044 }
1045
1046 /*!
1047  *  Class       : SMESHGUI_ChangeOrientationDlg
1048  *  Description : Modification of orientation of faces
1049  */
1050
1051 SMESHGUI_ChangeOrientationDlg
1052 ::SMESHGUI_ChangeOrientationDlg(SMESHGUI* theModule):
1053   SMESHGUI_MultiEditDlg(theModule, SMESH::FaceFilter, true)
1054 {
1055   setWindowTitle(tr("CAPTION"));
1056   myHelpFileName = "changing_orientation_of_elements_page.html";
1057 }
1058
1059 SMESHGUI_ChangeOrientationDlg::~SMESHGUI_ChangeOrientationDlg()
1060 {
1061 }
1062
1063 bool SMESHGUI_ChangeOrientationDlg::process (SMESH::SMESH_MeshEditor_ptr theEditor,
1064                                              const SMESH::long_array&    theIds,
1065                                              SMESH::SMESH_IDSource_ptr   obj)
1066 {
1067   if ( CORBA::is_nil( obj ))
1068     return theEditor->Reorient(theIds);
1069   else
1070     return theEditor->ReorientObject( obj );
1071 }
1072
1073 /*!
1074  *  Class       : SMESHGUI_UnionOfTrianglesDlg
1075  *  Description : Construction of quadrangles by automatic association of triangles
1076  */
1077
1078 SMESHGUI_UnionOfTrianglesDlg
1079 ::SMESHGUI_UnionOfTrianglesDlg(SMESHGUI* theModule):
1080   SMESHGUI_MultiEditDlg(theModule, SMESH::TriaFilter, false)
1081 {
1082   setWindowTitle(tr("CAPTION"));
1083
1084   //Preview check box
1085   myPreviewCheckBox = new QCheckBox(tr("PREVIEW"), mySelGrp);
1086   QGridLayout* aLay = (QGridLayout*)(mySelGrp->layout());
1087   aLay->addWidget(myPreviewCheckBox, aLay->rowCount(), 0, 1, aLay->columnCount());
1088
1089   myComboBoxFunctor->setEnabled(true);  
1090   myComboBoxFunctor->addItem(tr("AREA_ELEMENTS"));
1091   myComboBoxFunctor->addItem(tr("WARP_ELEMENTS")); // for quadrangles only
1092   myComboBoxFunctor->addItem(tr("TAPER_ELEMENTS")); // for quadrangles only
1093
1094   // Maximum angle
1095   QWidget* aMaxAngleGrp = new QWidget(myCriterionGrp);
1096   QHBoxLayout* aMaxAngleGrpLayout = new QHBoxLayout(aMaxAngleGrp);
1097   aMaxAngleGrpLayout->setMargin(0);
1098   aMaxAngleGrpLayout->setSpacing(SPACING);
1099
1100   QLabel* aLab = new QLabel (tr("MAXIMUM_ANGLE"), aMaxAngleGrp);
1101   myMaxAngleSpin = new SMESHGUI_SpinBox (aMaxAngleGrp);
1102   myMaxAngleSpin->RangeStepAndValidator(0, 180.0, 1.0, "angle_precision");
1103   myMaxAngleSpin->SetValue(30.0);
1104
1105   aMaxAngleGrpLayout->addWidget(aLab);
1106   aMaxAngleGrpLayout->addWidget(myMaxAngleSpin);
1107
1108   ((QVBoxLayout*)(myCriterionGrp->layout()))->addWidget(aMaxAngleGrp);
1109   myCriterionGrp->show();
1110
1111   connect(myComboBoxFunctor, SIGNAL(activated(int)),       this, SLOT(toDisplaySimulation()));
1112   connect(myMaxAngleSpin,    SIGNAL(valueChanged(int)),    this, SLOT(toDisplaySimulation()));
1113   connect(this,              SIGNAL(ListContensChanged()), this, SLOT(toDisplaySimulation()));
1114   connectPreviewControl(); //To Connect preview check box
1115
1116   myPreviewCheckBox->setChecked(false);
1117   onDisplaySimulation(false);
1118
1119   myHelpFileName = "uniting_set_of_triangles_page.html";
1120 }
1121
1122 SMESHGUI_UnionOfTrianglesDlg::~SMESHGUI_UnionOfTrianglesDlg()
1123 {
1124 }
1125
1126 bool SMESHGUI_UnionOfTrianglesDlg::isValid (const bool theMess)
1127 {
1128   bool ok = SMESHGUI_MultiEditDlg::isValid( theMess );
1129   if( !ok )
1130     return false;
1131
1132   QString msg;
1133   ok = myMaxAngleSpin->isValid( msg, theMess );
1134   if( !ok ) {
1135     if( theMess ) {
1136       QString str( tr( "SMESH_INCORRECT_INPUT" ) );
1137       if ( !msg.isEmpty() )
1138         str += "\n" + msg;
1139       SUIT_MessageBox::critical( this, tr( "SMESH_ERROR" ), str );
1140     }
1141     return false;
1142   }
1143
1144   return ok;
1145 }
1146
1147 bool SMESHGUI_UnionOfTrianglesDlg::process (SMESH::SMESH_MeshEditor_ptr theEditor,
1148                                             const SMESH::long_array&    theIds,
1149                                             SMESH::SMESH_IDSource_ptr   obj)
1150 {
1151   {
1152     QStringList aParameters;
1153     aParameters << myMaxAngleSpin->text();
1154     myMesh->SetParameters( aParameters.join(":").toLatin1().constData() );
1155   }
1156   SMESH::NumericalFunctor_var aCriterion = getNumericalFunctor();
1157   double aMaxAngle = myMaxAngleSpin->GetValue() * M_PI / 180.0;
1158   bool ok;
1159   if ( CORBA::is_nil( obj ))
1160     ok = theEditor->TriToQuad(theIds, aCriterion, aMaxAngle);
1161   else
1162     ok = theEditor->TriToQuadObject(obj, aCriterion, aMaxAngle);
1163   return ok;
1164 }
1165   
1166 void SMESHGUI_UnionOfTrianglesDlg::onDisplaySimulation( bool toDisplayPreview )
1167 {
1168   if ( myPreviewCheckBox->isChecked() && toDisplayPreview ) {
1169     if ( isValid( true ) ) {
1170       try{
1171         SUIT_OverrideCursor aWaitCursor;
1172         // get Ids of elements
1173         SMESH::SMESH_IDSource_var obj;
1174         SMESH::long_array_var anElemIds = getIds( obj );
1175
1176         SMESH::NumericalFunctor_var aCriterion  = getNumericalFunctor();
1177         SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditPreviewer();
1178
1179         double aMaxAngle = myMaxAngleSpin->GetValue() * M_PI / 180.0;
1180       
1181         if ( CORBA::is_nil( obj ) )
1182           aMeshEditor->TriToQuad( anElemIds.inout(), aCriterion, aMaxAngle );
1183         else
1184           aMeshEditor->TriToQuadObject( obj, aCriterion, aMaxAngle );
1185       
1186         SMESH::MeshPreviewStruct_var aMeshPreviewStruct = aMeshEditor->GetPreviewData();
1187
1188         vtkProperty* aProp = vtkProperty::New();
1189         aProp->SetRepresentationToWireframe();
1190         aProp->SetColor( 250, 0, 250 );
1191         aProp->SetLineWidth( SMESH::GetFloat( "SMESH:element_width", 1 ) + 3 );
1192         mySimulation->GetActor()->SetProperty( aProp );
1193         aProp->Delete();
1194
1195         mySimulation->SetData( aMeshPreviewStruct._retn() );
1196       } catch ( ... ) {
1197         hidePreview();
1198       }
1199     } else {
1200       hidePreview();
1201     }
1202   } else {
1203     hidePreview();
1204   }
1205 }
1206
1207 /*!
1208  *  Class       : SMESHGUI_CuttingOfQuadsDlg
1209  *  Description : Automatic splitting of quadrangles into triangles
1210  */
1211
1212 SMESHGUI_CuttingOfQuadsDlg
1213 ::SMESHGUI_CuttingOfQuadsDlg(SMESHGUI* theModule):
1214   SMESHGUI_MultiEditDlg(theModule, SMESH::QuadFilter, false)
1215 {
1216   setWindowTitle(tr("CAPTION"));
1217   myPreviewActor = 0;
1218
1219   myPreviewChk = new QCheckBox (tr("PREVIEW"), mySelGrp);
1220   QGridLayout* aLay = (QGridLayout*)(mySelGrp->layout());
1221   aLay->addWidget(myPreviewChk, aLay->rowCount(), 0, 1, aLay->columnCount());
1222
1223   // "split to 4 tria" option
1224
1225   QRadioButton* to4TriaRB = new QRadioButton(tr("TO_4_TRIA"), myChoiceWidget);
1226   ((QVBoxLayout*)(myCriterionGrp->layout()))->insertWidget(0, to4TriaRB);
1227   myGroupChoice->addButton(to4TriaRB, 3);
1228   to4TriaRB->setChecked(true);
1229   onCriterionRB();
1230
1231   myCriterionGrp->show();
1232   myChoiceWidget->show();
1233   myComboBoxFunctor->insertItem(0, tr("MIN_DIAG_ELEMENTS"));
1234   myComboBoxFunctor->setCurrentIndex(0);
1235   myComboBoxFunctor->setEnabled(false);
1236
1237   connect(myPreviewChk,      SIGNAL(stateChanged(int)),    this, SLOT(onPreviewChk()));
1238   connect(myGroupChoice,     SIGNAL(buttonClicked(int)),   this, SLOT(onCriterionRB()));
1239   connect(myComboBoxFunctor, SIGNAL(activated(int)),       this, SLOT(onPreviewChk()));
1240   connect(this,              SIGNAL(ListContensChanged()), this, SLOT(onPreviewChk()));
1241
1242   myHelpFileName = "cutting_quadrangles_page.html";
1243 }
1244
1245 SMESHGUI_CuttingOfQuadsDlg::~SMESHGUI_CuttingOfQuadsDlg()
1246 {
1247 }
1248
1249 void SMESHGUI_CuttingOfQuadsDlg::reject()
1250 {
1251   erasePreview();
1252   SMESHGUI_MultiEditDlg::reject();
1253 }
1254
1255 bool SMESHGUI_CuttingOfQuadsDlg::process (SMESH::SMESH_MeshEditor_ptr theEditor,
1256                                           const SMESH::long_array&    theIds,
1257                                           SMESH::SMESH_IDSource_ptr   obj)
1258 {
1259   bool hasObj = (! CORBA::is_nil( obj ));
1260   switch (myGroupChoice->checkedId()) {
1261   case 0: // use diagonal 1-3
1262     return hasObj ? theEditor->SplitQuadObject(obj, true) : theEditor->SplitQuad(theIds, true);
1263   case 1: // use diagonal 2-4
1264     return hasObj ? theEditor->SplitQuadObject(obj, false) : theEditor->SplitQuad(theIds, false);
1265   case 3: // split to 4 tria
1266     {
1267       if ( hasObj )
1268         return theEditor->QuadTo4Tri( obj ), true;
1269       SMESH::SMESH_IDSource_wrap elems = theEditor->MakeIDSource( theIds, SMESH::FACE );
1270       theEditor->QuadTo4Tri( elems );
1271       return true;
1272     }
1273   default: // use numeric functor
1274     break;
1275   }
1276
1277   SMESH::NumericalFunctor_var aCrit = getNumericalFunctor();
1278   return hasObj ? theEditor->QuadToTriObject(obj, aCrit) : theEditor->QuadToTri(theIds, aCrit);
1279 }
1280
1281 void SMESHGUI_CuttingOfQuadsDlg::onCriterionRB()
1282 {
1283   if (myGroupChoice->checkedId() == 2) // Use numeric functor
1284     myComboBoxFunctor->setEnabled(true);
1285   else
1286     myComboBoxFunctor->setEnabled(false);
1287
1288   if (myGroupChoice->checkedId() == 3) // To 4 tria
1289   {
1290     if ( myPreviewChk->isChecked() )
1291       myPreviewChk->setChecked( false );
1292     myPreviewChk->setEnabled( false );
1293   }
1294   else
1295   {
1296     myPreviewChk->setEnabled( true );
1297   }
1298   onPreviewChk();
1299 }
1300
1301 void SMESHGUI_CuttingOfQuadsDlg::onPreviewChk()
1302 {
1303   myPreviewChk->isChecked() ? displayPreview() : erasePreview();
1304 }
1305
1306 void SMESHGUI_CuttingOfQuadsDlg::erasePreview()
1307 {
1308   if (myPreviewActor == 0)
1309     return;
1310
1311   if (SVTK_ViewWindow* vf = SMESH::GetCurrentVtkView()) {
1312     vf->RemoveActor(myPreviewActor);
1313     vf->Repaint();
1314   }
1315   myPreviewActor->Delete();
1316   myPreviewActor = 0;
1317 }
1318   
1319 void SMESHGUI_CuttingOfQuadsDlg::displayPreview()
1320 {
1321   if (myActor == 0)
1322     return;
1323
1324   if (myPreviewActor != 0)
1325     erasePreview();
1326
1327   SUIT_OverrideCursor aWaitCursor;
1328   // get Ids of elements
1329   SMESH::SMESH_IDSource_var obj;
1330   SMESH::long_array_var anElemIds = getIds(obj);
1331   if (anElemIds->length() == 0 && obj->_is_nil() )
1332     return;
1333
1334   SMDS_Mesh* aMesh = myActor->GetObject()->GetMesh();
1335   if (aMesh == 0)
1336     return;
1337
1338   // 0 - use diagonal 1-3, 1 - use diagonal 2-4, 2 - use numerical functor
1339   int aChoice = myGroupChoice->checkedId();
1340   SMESH::NumericalFunctor_var aCriterion  = SMESH::NumericalFunctor::_nil();
1341   SMESH::SMESH_MeshEditor_var aMeshEditor = SMESH::SMESH_MeshEditor::_nil();
1342   if (aChoice == 2) {
1343     aCriterion  = getNumericalFunctor();
1344     aMeshEditor = myMesh->GetMeshEditor();
1345     if (aMeshEditor->_is_nil())
1346       return;
1347   }
1348
1349   if ( anElemIds->length() == 0 ) {
1350     anElemIds = obj->GetIDs();
1351   }
1352
1353   //Create grid
1354   vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
1355
1356   vtkIdType aNbCells = anElemIds->length() * 2;
1357   vtkIdType aCellsSize = 4 * aNbCells;
1358   vtkCellArray* aConnectivity = vtkCellArray::New();
1359   aConnectivity->Allocate(aCellsSize, 0);
1360
1361   vtkPoints* aPoints = vtkPoints::New();
1362   aPoints->SetNumberOfPoints(anElemIds->length() * 4);
1363
1364   vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
1365   aCellTypesArray->SetNumberOfComponents(1);
1366   aCellTypesArray->Allocate(aNbCells * aCellTypesArray->GetNumberOfComponents());
1367
1368   vtkIdList *anIdList = vtkIdList::New();
1369   anIdList->SetNumberOfIds(3);
1370
1371   TColStd_DataMapOfIntegerInteger anIdToVtk;
1372
1373   int aNodes[ 4 ];
1374   int nbPoints = -1;
1375   for (int i = 0, n = anElemIds->length(); i < n; i++)
1376   {
1377     const SMDS_MeshElement* anElem = aMesh->FindElement(anElemIds[ i ]);
1378     if (anElem == 0 || anElem->NbNodes() != 4)
1379       continue;
1380
1381     SMDS_ElemIteratorPtr anIter = anElem->nodesIterator();
1382     int k = 0;
1383     while (anIter->more()) {
1384       const SMDS_MeshNode* aNode = static_cast<const SMDS_MeshNode*>(anIter->next());
1385       if (aNode)
1386       {
1387         if (!anIdToVtk.IsBound(aNode->GetID()))
1388         {
1389           aPoints->SetPoint(++nbPoints, aNode->X(), aNode->Y(), aNode->Z());
1390           anIdToVtk.Bind(aNode->GetID(), nbPoints);
1391         }
1392
1393         aNodes[ k++ ] = aNode->GetID();
1394       }
1395     }
1396
1397     if (k != 4)
1398       continue;
1399
1400     bool isDiag13 = true;
1401     if (aChoice == 0) // use diagonal 1-3
1402     {
1403       isDiag13 = true;
1404     }
1405     else if (aChoice == 1) // use diagonal 2-4
1406     {
1407       isDiag13 = false;
1408     }
1409     else // use numerical functor
1410     {
1411       // compare two sets of possible triangles
1412       int diag = aMeshEditor->BestSplit(anElemIds[i], aCriterion);
1413       if (diag == 1) // 1-3
1414         isDiag13 = true;
1415       else if (diag == 2) // 2-4
1416         isDiag13 = false;
1417       else // error
1418         continue;
1419     }
1420
1421     if (isDiag13)
1422     {
1423       anIdList->SetId(0, anIdToVtk(aNodes[ 0 ]));
1424       anIdList->SetId(1, anIdToVtk(aNodes[ 1 ]));
1425       anIdList->SetId(2, anIdToVtk(aNodes[ 2 ]));
1426       aConnectivity->InsertNextCell(anIdList);
1427       aCellTypesArray->InsertNextValue(VTK_TRIANGLE);
1428
1429       anIdList->SetId(0, anIdToVtk(aNodes[ 2 ]));
1430       anIdList->SetId(1, anIdToVtk(aNodes[ 3 ]));
1431       anIdList->SetId(2, anIdToVtk(aNodes[ 0 ]));
1432       aConnectivity->InsertNextCell(anIdList);
1433       aCellTypesArray->InsertNextValue(VTK_TRIANGLE);
1434     }
1435     else
1436     {
1437       anIdList->SetId(0, anIdToVtk(aNodes[ 1 ]));
1438       anIdList->SetId(1, anIdToVtk(aNodes[ 2 ]));
1439       anIdList->SetId(2, anIdToVtk(aNodes[ 3 ]));
1440       aConnectivity->InsertNextCell(anIdList);
1441       aCellTypesArray->InsertNextValue(VTK_TRIANGLE);
1442
1443       anIdList->SetId(0, anIdToVtk(aNodes[ 3 ]));
1444       anIdList->SetId(1, anIdToVtk(aNodes[ 0 ]));
1445       anIdList->SetId(2, anIdToVtk(aNodes[ 1 ]));
1446       aConnectivity->InsertNextCell(anIdList);
1447       aCellTypesArray->InsertNextValue(VTK_TRIANGLE);
1448     }
1449   }
1450
1451   VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
1452   aCellLocationsArray->SetNumberOfComponents(1);
1453   aCellLocationsArray->SetNumberOfTuples(aNbCells);
1454
1455   aConnectivity->InitTraversal();
1456   for(vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell(npts, pts); idType++)
1457     aCellLocationsArray->SetValue(idType, aConnectivity->GetTraversalLocation(npts));
1458
1459   aGrid->SetPoints(aPoints);
1460   aGrid->SetCells(aCellTypesArray, aCellLocationsArray,aConnectivity);
1461
1462   // Create and display actor
1463   vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
1464   aMapper->SetInputData(aGrid);
1465
1466   myPreviewActor = SALOME_Actor::New();
1467   myPreviewActor->PickableOff();
1468   myPreviewActor->SetMapper(aMapper);
1469
1470   vtkProperty* aProp = vtkProperty::New();
1471   aProp->SetRepresentationToWireframe();
1472   aProp->SetColor(250, 0, 250);
1473   aProp->SetLineWidth(myActor->GetLineWidth() + 1);
1474   myPreviewActor->SetProperty(aProp);
1475
1476   SMESH::GetCurrentVtkView()->AddActor(myPreviewActor);
1477   SMESH::GetCurrentVtkView()->Repaint();
1478
1479   aProp->Delete();
1480   aPoints->Delete();
1481   aConnectivity->Delete();
1482   aGrid->Delete();
1483   aMapper->Delete();
1484   anIdList->Delete();
1485   aCellTypesArray->Delete();
1486   aCellLocationsArray->Delete();
1487 }
1488
1489 /*!
1490  *  Class       : SMESHGUI_CuttingIntoTetraDlg
1491  *  Description : Modification of orientation of faces
1492  */
1493
1494 SMESHGUI_CuttingIntoTetraDlg::SMESHGUI_CuttingIntoTetraDlg(SMESHGUI* theModule)
1495   : SMESHGUI_MultiEditDlg(theModule, SMESH::VolumeFilter, false)
1496 {
1497   setWindowTitle(tr("CAPTION"));
1498   myHelpFileName = "split_to_tetra_page.html";
1499   myEntityType = 1;
1500
1501   myToAllChk->setChecked( true ); //aplly to the whole mesh by default
1502
1503   bool hasHexa = true;//myMesh->_is_nil() ? false : myMesh->NbHexas();
1504
1505   if ( hasHexa )
1506   {
1507     myGroupChoice->button(0)->setText( tr("SPLIT_HEX_TO_5_TETRA"));
1508     myGroupChoice->button(1)->setText( tr("SPLIT_HEX_TO_6_TETRA"));
1509     myGroupChoice->button(2)->setText( tr("SPLIT_HEX_TO_24_TETRA"));
1510
1511     myCriterionGrp->setTitle( tr("SPLIT_METHOD"));
1512     myCriterionGrp->show();
1513     myComboBoxFunctor->hide();
1514     myChoiceWidget->show();
1515   }
1516   setSelectionMode();
1517   updateButtons();
1518 }
1519
1520 SMESHGUI_CuttingIntoTetraDlg::~SMESHGUI_CuttingIntoTetraDlg()
1521 {
1522 }
1523
1524 bool SMESHGUI_CuttingIntoTetraDlg::process (SMESH::SMESH_MeshEditor_ptr theEditor,
1525                                             const SMESH::long_array&    theIds,
1526                                             SMESH::SMESH_IDSource_ptr   theObj)
1527 {
1528   SMESH::SMESH_IDSource_wrap obj = theObj;
1529   if ( CORBA::is_nil( obj ))
1530     obj = theEditor->MakeIDSource( theIds, myEntityType ? SMESH::VOLUME : SMESH::FACE );
1531   else
1532     obj->Register();
1533   try {
1534     theEditor->SplitVolumesIntoTetra( obj, myGroupChoice->checkedId()+1 );
1535   }
1536   catch ( const SALOME::SALOME_Exception& S_ex ) {
1537     SalomeApp_Tools::QtCatchCorbaException( S_ex );
1538     return false;
1539   }
1540   catch(...) {
1541     return false;
1542   }
1543   return true;
1544 }