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