Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_MultiEditDlg.cxx
1 //  SMESH SMESHGUI : GUI for SMESH component
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //
24 //  File   : SMESHGUI_MultiEditDlg.cxx
25 //  Author : Sergey LITONIN
26 //  Module : SMESH
27
28 #include "SMESHGUI_MultiEditDlg.h"
29
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_Filter.h"
32 #include "SMESHGUI_FilterDlg.h"
33 #include "SMESHGUI_Utils.h"
34 #include "SMESHGUI_VTKUtils.h"
35 #include "SMESHGUI_MeshUtils.h"
36 #include "SMESHGUI_FilterUtils.h"
37 #include "SMESHGUI_SpinBox.h"
38
39 #include "SMESH_Actor.h"
40 #include "SMESH_TypeFilter.hxx"
41 #include "SMDS_Mesh.hxx"
42 #include "SMDS_MeshElement.hxx"
43
44 #include "SUIT_ResourceMgr.h"
45 #include "SUIT_Desktop.h"
46
47 #include "LightApp_SelectionMgr.h"
48 #include "SALOME_ListIO.hxx"
49 #include "SALOME_ListIteratorOfListIO.hxx"
50
51 #include "SVTK_Selector.h"
52 #include "SVTK_ViewModel.h"
53 #include "SVTK_ViewWindow.h"
54
55 // OCCT Includes
56 #include <Precision.hxx>
57 #include <TColStd_IndexedMapOfInteger.hxx>
58 #include <TColStd_DataMapOfIntegerInteger.hxx>
59 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
60
61 // VTK Includes
62 #include <vtkCell3D.h>
63 #include <vtkQuad.h>
64 #include <vtkTriangle.h>
65 #include <vtkPolygon.h>
66 #include <vtkConvexPointSet.h>
67 #include <vtkIdList.h>
68 #include <vtkIntArray.h>
69 #include <vtkCellArray.h>
70 #include <vtkUnsignedCharArray.h>
71 #include <vtkUnstructuredGrid.h>
72 #include <vtkDataSetMapper.h>
73 #include <vtkProperty.h>
74
75 // QT Includes
76 #include <qframe.h>
77 #include <qlabel.h>
78 #include <qlayout.h>
79 #include <qlistbox.h>
80 #include <qcheckbox.h>
81 #include <qcombobox.h>
82 #include <qgroupbox.h>
83 #include <qlineedit.h>
84 #include <qpushbutton.h>
85 #include <qapplication.h>
86 #include <qradiobutton.h>
87 #include <qhbuttongroup.h>
88
89 // IDL Headers
90 #include "SALOMEconfig.h"
91 #include CORBA_SERVER_HEADER(SMESH_Group)
92
93 #define SPACING 5
94 #define MARGIN  10
95
96 /*!
97  *  Class       : SMESHGUI_MultiEditDlg
98  *  Description : Description : Inversion of the diagonal of a pseudo-quadrangle formed by
99  *                2 neighboring triangles with 1 common edge
100  */
101
102 //=======================================================================
103 // name    : SMESHGUI_MultiEditDlg::SMESHGUI_MultiEditDlg
104 // Purpose : Constructor
105 //=======================================================================
106 SMESHGUI_MultiEditDlg
107 ::SMESHGUI_MultiEditDlg(SMESHGUI* theModule,
108                         const int theMode,
109                         const bool the3d2d,
110                         const char* theName):
111   QDialog(SMESH::GetDesktop(theModule),
112           theName,
113           false,
114           WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose),
115     mySelector(SMESH::GetViewWindow(theModule)->GetSelector()),
116     mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
117     mySMESHGUI(theModule)
118 {
119   myFilterDlg = 0;
120   myEntityType = 0;
121
122   myFilterType = theMode;
123   QVBoxLayout* aDlgLay = new QVBoxLayout(this, MARGIN, SPACING);
124
125   QFrame* aMainFrame = createMainFrame  (this, the3d2d);
126   QFrame* aBtnFrame  = createButtonFrame(this);
127
128   aDlgLay->addWidget(aMainFrame);
129   aDlgLay->addWidget(aBtnFrame);
130
131   aDlgLay->setStretchFactor(aMainFrame, 1);
132   aDlgLay->setStretchFactor(aBtnFrame, 0);
133   Init();
134 }
135
136 //=======================================================================
137 // name    : SMESHGUI_MultiEditDlg::createMainFrame
138 // Purpose : Create frame containing dialog's input fields
139 //=======================================================================
140 QFrame* SMESHGUI_MultiEditDlg::createMainFrame (QWidget* theParent, const bool the3d2d)
141 {
142   QGroupBox* aMainGrp = new QGroupBox(1, Qt::Horizontal, theParent);
143   aMainGrp->setFrameStyle(QFrame::NoFrame);
144   aMainGrp->setInsideMargin(0);
145
146   QPixmap aPix (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
147
148   // "Selected cells" group
149   mySelGrp = new QGroupBox(1, Qt::Horizontal,  aMainGrp);
150
151   myEntityTypeGrp = 0;
152   if (the3d2d) {
153     myEntityTypeGrp = new QHButtonGroup(tr("SMESH_ELEMENTS_TYPE"), mySelGrp);
154     (new QRadioButton(tr("SMESH_FACE"),   myEntityTypeGrp))->setChecked(true);
155     (new QRadioButton(tr("SMESH_VOLUME"), myEntityTypeGrp));
156     myEntityType = myEntityTypeGrp->id(myEntityTypeGrp->selected());
157   }
158
159   QFrame* aFrame = new QFrame(mySelGrp);
160
161   myListBox = new QListBox(aFrame);
162   myListBox->setSelectionMode(QListBox::Extended);
163   myListBox->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding));
164   myListBox->installEventFilter(this);
165
166   myFilterBtn = new QPushButton(tr("FILTER")   , aFrame);
167   myAddBtn    = new QPushButton(tr("ADD")      , aFrame);
168   myRemoveBtn = new QPushButton(tr("REMOVE")   , aFrame);
169   mySortBtn   = new QPushButton(tr("SORT_LIST"), aFrame);
170
171   QGridLayout* aLay = new QGridLayout(aFrame, 5, 2, 0, 5);
172   aLay->addMultiCellWidget(myListBox, 0, 4, 0, 0);
173   aLay->addWidget(myFilterBtn, 0, 1);
174   aLay->addWidget(myAddBtn, 1, 1);
175   aLay->addWidget(myRemoveBtn, 2, 1);
176   aLay->addWidget(mySortBtn, 3, 1);
177
178   QSpacerItem* aSpacer = new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
179   aLay->addItem(aSpacer, 4, 1);
180
181   myToAllChk = new QCheckBox(tr("TO_ALL"), mySelGrp);
182
183   // Split/Join criterion group
184   myCriterionGrp = new QGroupBox(3, Qt::Vertical, tr("SPLIT_JOIN_CRITERION"), aMainGrp);
185
186   myGroupChoice = new QButtonGroup(3, Qt::Vertical, myCriterionGrp);
187   myGroupChoice->setInsideMargin(0);
188   myGroupChoice->setFrameStyle(QFrame::NoFrame);
189   (new QRadioButton(tr("USE_DIAGONAL_1_3"), myGroupChoice))->setChecked(true);
190   (new QRadioButton(tr("USE_DIAGONAL_2_4"), myGroupChoice));
191   (new QRadioButton(tr("USE_NUMERIC_FUNC"), myGroupChoice));
192
193   myComboBoxFunctor = new QComboBox(myCriterionGrp);
194   myComboBoxFunctor->insertItem(tr("ASPECTRATIO_ELEMENTS"));
195   myComboBoxFunctor->insertItem(tr("MINIMUMANGLE_ELEMENTS"));
196   myComboBoxFunctor->insertItem(tr("SKEW_ELEMENTS"));
197   myComboBoxFunctor->insertItem(tr("AREA_ELEMENTS"));
198   //myComboBoxFunctor->insertItem(tr("LENGTH2D_EDGES")); // for existing elements only
199   //myComboBoxFunctor->insertItem(tr("MULTI2D_BORDERS")); // for existing elements only
200   myComboBoxFunctor->setCurrentItem(0);
201
202   myCriterionGrp->hide();
203   myGroupChoice->hide();
204   myComboBoxFunctor->setEnabled(false);
205
206   // "Select from" group
207   QGroupBox* aGrp = new QGroupBox(3, Qt::Horizontal, tr("SELECT_FROM"), aMainGrp);
208
209   mySubmeshChk = new QCheckBox(tr("SMESH_SUBMESH"), aGrp);
210   mySubmeshBtn = new QPushButton(aGrp);
211   mySubmesh = new QLineEdit(aGrp);
212   mySubmesh->setReadOnly(true);
213   mySubmeshBtn->setPixmap(aPix);
214
215   myGroupChk = new QCheckBox(tr("SMESH_GROUP"), aGrp);
216   myGroupBtn = new QPushButton(aGrp);
217   myGroup = new QLineEdit(aGrp);
218   myGroup->setReadOnly(true);
219   myGroupBtn->setPixmap(aPix);
220
221   return aMainGrp;
222 }
223
224 //=======================================================================
225 // name    : SMESHGUI_MultiEditDlg::createButtonFrame
226 // Purpose : Create frame containing buttons
227 //=======================================================================
228 QFrame* SMESHGUI_MultiEditDlg::createButtonFrame (QWidget* theParent)
229 {
230   QFrame* aFrame = new QFrame (theParent);
231   aFrame->setFrameStyle(QFrame::Box | QFrame::Sunken);
232
233   myOkBtn     = new QPushButton (tr("SMESH_BUT_OK"   ), aFrame);
234   myApplyBtn  = new QPushButton (tr("SMESH_BUT_APPLY"), aFrame);
235   myCloseBtn  = new QPushButton (tr("SMESH_BUT_CLOSE"), aFrame);
236
237   QSpacerItem* aSpacer = new QSpacerItem (0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
238
239   QHBoxLayout* aLay = new QHBoxLayout (aFrame, MARGIN, SPACING);
240
241   aLay->addWidget(myOkBtn);
242   aLay->addWidget(myApplyBtn);
243   aLay->addItem(aSpacer);
244   aLay->addWidget(myCloseBtn);
245
246   return aFrame;
247 }
248
249 //=======================================================================
250 // name    : SMESHGUI_MultiEditDlg::isValid
251 // Purpose : Verify validity of input data
252 //=======================================================================
253 bool SMESHGUI_MultiEditDlg::isValid (const bool /*theMess*/) const
254 {
255   return (!myMesh->_is_nil() &&
256           (myListBox->count() > 0 || (myToAllChk->isChecked() && myActor)));
257 }
258
259 //=======================================================================
260 // name    : SMESHGUI_MultiEditDlg::~SMESHGUI_MultiEditDlg
261 // Purpose : Destructor
262 //=======================================================================
263 SMESHGUI_MultiEditDlg::~SMESHGUI_MultiEditDlg()
264 {
265   if (myFilterDlg != 0)
266   {
267     myFilterDlg->reparent(0, QPoint());
268     delete myFilterDlg;
269   }
270 }
271
272 //=======================================================================
273 // name    : SMESHGUI_MultiEditDlg::eventFilter
274 // Purpose : event filter
275 //=======================================================================
276 bool SMESHGUI_MultiEditDlg::eventFilter (QObject* object, QEvent* event)
277 {
278   if (object == myListBox && event->type() == QEvent::KeyPress) {
279     QKeyEvent* ke = (QKeyEvent*)event;
280     if (ke->key() == Key_Delete)
281       onRemoveBtn();
282   }
283   return QDialog::eventFilter(object, event);
284 }
285
286 //=======================================================================
287 // name    : SMESHGUI_MultiEditDlg::getNumericalFunctor
288 // Purpose :
289 //=======================================================================
290 SMESH::NumericalFunctor_ptr SMESHGUI_MultiEditDlg::getNumericalFunctor()
291 {
292   SMESH::NumericalFunctor_var aNF = SMESH::NumericalFunctor::_nil();
293
294   SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
295   if (aFilterMgr->_is_nil())
296     return aNF._retn();
297
298   if (myComboBoxFunctor->currentText() == tr("ASPECTRATIO_ELEMENTS"))
299     aNF = aFilterMgr->CreateAspectRatio();
300   else if (myComboBoxFunctor->currentText() == tr("WARP_ELEMENTS"))
301     aNF = aFilterMgr->CreateWarping();
302   else if (myComboBoxFunctor->currentText() == tr("MINIMUMANGLE_ELEMENTS"))
303     aNF = aFilterMgr->CreateMinimumAngle();
304   else if (myComboBoxFunctor->currentText() == tr("TAPER_ELEMENTS"))
305     aNF = aFilterMgr->CreateTaper();
306   else if (myComboBoxFunctor->currentText() == tr("SKEW_ELEMENTS"))
307     aNF = aFilterMgr->CreateSkew();
308   else if (myComboBoxFunctor->currentText() == tr("AREA_ELEMENTS"))
309     aNF = aFilterMgr->CreateArea();
310   else if (myComboBoxFunctor->currentText() == tr("LENGTH2D_EDGES"))
311     aNF = aFilterMgr->CreateLength2D();
312   else if (myComboBoxFunctor->currentText() == tr("MULTI2D_BORDERS"))
313     aNF = aFilterMgr->CreateMultiConnection2D();
314   else ;
315
316   return aNF._retn();
317 }
318
319 //=======================================================================
320 // name    : SMESHGUI_MultiEditDlg::Init
321 // Purpose : Init dialog fields, connect signals and slots, show dialog
322 //=======================================================================
323 void SMESHGUI_MultiEditDlg::Init()
324 {
325   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
326   myListBox->clear();
327   myIds.Clear();
328   myBusy = false;
329   myActor = 0;
330   emit ListContensChanged();
331
332   // main buttons
333   connect(myOkBtn,    SIGNAL(clicked()), SLOT(onOk()));
334   connect(myCloseBtn, SIGNAL(clicked()), SLOT(onClose()));
335   connect(myApplyBtn, SIGNAL(clicked()), SLOT(onApply()));
336
337   // selection and SMESHGUI
338   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
339   connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
340   connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(onClose()));
341
342   // dialog controls
343   connect(myFilterBtn, SIGNAL(clicked()), SLOT(onFilterBtn()  ));
344   connect(myAddBtn   , SIGNAL(clicked()), SLOT(onAddBtn()     ));
345   connect(myRemoveBtn, SIGNAL(clicked()), SLOT(onRemoveBtn()  ));
346   connect(mySortBtn  , SIGNAL(clicked()), SLOT(onSortListBtn()));
347
348   connect(mySubmeshChk, SIGNAL(stateChanged(int)), SLOT(onSubmeshChk()));
349   connect(myGroupChk  , SIGNAL(stateChanged(int)), SLOT(onGroupChk()  ));
350   connect(myToAllChk  , SIGNAL(stateChanged(int)), SLOT(onToAllChk()  ));
351
352   if (myEntityTypeGrp)
353     connect(myEntityTypeGrp, SIGNAL(clicked(int)), SLOT(on3d2dChanged(int)));
354
355   connect(myListBox, SIGNAL(selectionChanged()), SLOT(onListSelectionChanged()));
356
357   onSelectionDone();
358
359   // set selection mode
360   setSelectionMode();
361   updateButtons();
362 }
363
364 //=======================================================================
365 // name    : SMESHGUI_MultiEditDlg::onOk
366 // Purpose : SLOT called when "Ok" button pressed.
367 //           Assign filters VTK viewer and close dialog
368 //=======================================================================
369 void SMESHGUI_MultiEditDlg::onOk()
370 {
371   if (onApply())
372     onClose();
373 }
374
375 //=======================================================================
376 // name    : SMESHGUI_MultiEditDlg::getIds
377 // Purpose : Retrive identifiers from list box
378 //=======================================================================
379 SMESH::long_array_var SMESHGUI_MultiEditDlg::getIds()
380 {
381   SMESH::long_array_var anIds = new SMESH::long_array;
382
383   if (myToAllChk->isChecked())
384   {
385     myIds.Clear();
386     SMESH_Actor * anActor = SMESH::FindActorByObject(myMesh);
387     if (!anActor)
388       anActor = myActor;
389     if (anActor != 0)
390     {
391       // skl 07.02.2006
392       SMDS_Mesh* aMesh = myActor->GetObject()->GetMesh();
393       if( myFilterType == SMESHGUI_TriaFilter || 
394           myFilterType == SMESHGUI_QuadFilter ||
395           myFilterType == SMESHGUI_FaceFilter ) {
396         SMDS_FaceIteratorPtr it = aMesh->facesIterator();
397         while(it->more()) {
398           const SMDS_MeshFace* f = it->next();
399           if(myFilterType == SMESHGUI_FaceFilter) {
400             myIds.Add(f->GetID());
401           }
402           else if( myFilterType==SMESHGUI_TriaFilter &&
403                    ( f->NbNodes()==3 || f->NbNodes()==6 ) ) {
404             myIds.Add(f->GetID());
405           }
406           else if( myFilterType==SMESHGUI_QuadFilter &&
407                    ( f->NbNodes()==4 || f->NbNodes()==8 ) ) {
408             myIds.Add(f->GetID());
409           }
410         }
411       }
412       else if(myFilterType == SMESHGUI_VolumeFilter) {
413         SMDS_VolumeIteratorPtr it = aMesh->volumesIterator();
414         while(it->more()) {
415           const SMDS_MeshVolume* f = it->next();
416           myIds.Add(f->GetID());
417         }
418       }
419       /* commented by skl 07.02.2006
420       TVisualObjPtr aVisualObj = anActor->GetObject();
421       vtkUnstructuredGrid* aGrid = aVisualObj->GetUnstructuredGrid();
422       if (aGrid != 0) {
423         for (int i = 0, n = aGrid->GetNumberOfCells(); i < n; i++) {
424           vtkCell* aCell = aGrid->GetCell(i);
425           if (aCell != 0) {
426             vtkTriangle* aTri = vtkTriangle::SafeDownCast(aCell);
427             vtkQuad*     aQua = vtkQuad::SafeDownCast(aCell);
428             vtkPolygon*  aPG  = vtkPolygon::SafeDownCast(aCell);
429
430             vtkCell3D*   a3d  = vtkCell3D::SafeDownCast(aCell);
431             vtkConvexPointSet* aPH = vtkConvexPointSet::SafeDownCast(aCell);
432
433             if (aTri && myFilterType == SMESHGUI_TriaFilter ||
434                 aQua && myFilterType == SMESHGUI_QuadFilter ||
435                 (aTri || aQua || aPG) && myFilterType == SMESHGUI_FaceFilter ||
436                 (a3d || aPH) && myFilterType == SMESHGUI_VolumeFilter) {
437               int anObjId = aVisualObj->GetElemObjId(i);
438               myIds.Add(anObjId);
439             }
440           }
441         }
442       }
443       */
444     }
445   }
446
447   anIds->length(myIds.Extent());
448   TColStd_MapIteratorOfMapOfInteger anIter(myIds);
449   for (int i = 0; anIter.More(); anIter.Next() )
450   {
451     anIds[ i++ ] = anIter.Key();
452   }
453   return anIds._retn();
454 }
455
456 //=======================================================================
457 // name    : SMESHGUI_MultiEditDlg::onClose
458 // Purpose : SLOT called when "Close" button pressed. Close dialog
459 //=======================================================================
460 void SMESHGUI_MultiEditDlg::onClose()
461 {
462   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
463     aViewWindow->SetSelectionMode(ActorSelection);
464   disconnect(mySelectionMgr, 0, this, 0);
465   disconnect(mySMESHGUI, 0, this, 0);
466   mySMESHGUI->ResetState();
467
468   SMESH::RemoveFilters();
469   SMESH::SetPickable();
470
471   //mySelectionMgr->clearSelected();
472   mySelectionMgr->clearFilters();
473
474   reject();
475 }
476
477 //=======================================================================
478 // name    : SMESHGUI_MultiEditDlg::onSelectionDone
479 // Purpose : SLOT called when selection changed
480 //=======================================================================
481 void SMESHGUI_MultiEditDlg::onSelectionDone()
482 {
483   if (myBusy || !isEnabled()) return;
484   myBusy = true;
485
486   const SALOME_ListIO& aList = mySelector->StoredIObjects();
487
488   int nbSel = aList.Extent();
489   myListBox->clearSelection();
490
491   if (mySubmeshChk->isChecked() || myGroupChk->isChecked()) {
492     QLineEdit* aNameEdit = mySubmeshChk->isChecked() ? mySubmesh : myGroup;
493     if (nbSel == 1) {
494       Handle(SALOME_InteractiveObject) anIO = aList.First();
495       QString aName = "";
496       SMESH::GetNameOfSelectedIObjects(mySelectionMgr, aName);
497       anIO.IsNull() ? aNameEdit->clear() : aNameEdit->setText(aName);
498
499       if (mySubmeshChk->isChecked()) {
500         SMESH::SMESH_subMesh_var aSubMesh =
501           SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIO);
502         if (!aSubMesh->_is_nil())
503           myMesh = aSubMesh->GetFather();
504       } else {
505         SMESH::SMESH_GroupBase_var aGroup =
506           SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(anIO);
507         if (!aGroup->_is_nil())
508           myMesh = aGroup->GetMesh();
509       }
510     } else if (nbSel > 1) {
511       QString aStr = mySubmeshChk->isChecked() ?
512         tr("SMESH_SUBMESH_SELECTED") : tr("SMESH_GROUP_SELECTED");
513       aNameEdit->setText(aStr.arg(nbSel));
514     } else {
515       aNameEdit->clear();
516     }
517   } else if (nbSel == 1) {
518     QString aListStr = "";
519     Handle(SALOME_InteractiveObject) anIO = aList.First();
520     int aNbItems = SMESH::GetNameOfSelectedElements(mySelector,anIO,aListStr);
521     if (aNbItems > 0) {
522       QStringList anElements = QStringList::split(" ", aListStr);
523       QListBoxItem* anItem = 0;
524       for (QStringList::iterator it = anElements.begin(); it != anElements.end(); ++it) {
525         anItem = myListBox->findItem(*it, Qt::ExactMatch);
526         if (anItem) myListBox->setSelected(anItem, true);
527       }
528     }
529
530     myMesh = SMESH::GetMeshByIO(anIO);
531   }
532
533   if (nbSel == 1) {
534     myActor = SMESH::FindActorByEntry(aList.First()->getEntry());
535     if (!myActor)
536       myActor = SMESH::FindActorByObject(myMesh);
537     SVTK_Selector* aSelector = SMESH::GetSelector();
538     Handle(VTKViewer_Filter) aFilter = aSelector->GetFilter(myFilterType);
539     if (!aFilter.IsNull())
540       aFilter->SetActor(myActor);
541   }
542   myBusy = false;
543
544   updateButtons();
545 }
546
547 //=======================================================================
548 // name    : SMESHGUI_MultiEditDlg::onDeactivate
549 // Purpose : SLOT called when dialog must be deativated
550 //=======================================================================
551 void SMESHGUI_MultiEditDlg::onDeactivate()
552 {
553   setEnabled(false);
554 }
555
556 //=======================================================================
557 // name    : SMESHGUI_MultiEditDlg::enterEvent
558 // Purpose : Event filter
559 //=======================================================================
560 void SMESHGUI_MultiEditDlg::enterEvent (QEvent*)
561 {
562   if (!isEnabled()) {
563     mySMESHGUI->EmitSignalDeactivateDialog();
564     setEnabled(true);
565     setSelectionMode();
566   }
567 }
568
569 //=======================================================================
570 // name    : SMESHGUI_MultiEditDlg::closeEvent
571 // Purpose :
572 //=======================================================================
573 void SMESHGUI_MultiEditDlg::closeEvent (QCloseEvent*)
574 {
575   onClose();
576 }
577 //=======================================================================
578 // name    : SMESHGUI_MultiEditDlg::hideEvent
579 // Purpose : caused by ESC key
580 //=======================================================================
581 void SMESHGUI_MultiEditDlg::hideEvent (QHideEvent*)
582 {
583   if (!isMinimized())
584     onClose();
585 }
586
587 //=======================================================================
588 // name    : SMESHGUI_MultiEditDlg::onFilterBtn
589 // Purpose : SLOT. Called when "Filter" button pressed.
590 //           Start "Selection filters" dialog
591 //=======================================================================
592 void SMESHGUI_MultiEditDlg::onFilterBtn()
593 {
594   if (myFilterDlg == 0) {
595     myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, entityType() ? SMESH::VOLUME : SMESH::FACE);
596     connect(myFilterDlg, SIGNAL(Accepted()), SLOT(onFilterAccepted()));
597   } else {
598     myFilterDlg->Init(entityType() ? SMESH::VOLUME : SMESH::FACE);
599   }
600
601   myFilterDlg->SetSelection();
602   myFilterDlg->SetMesh(myMesh);
603   myFilterDlg->SetSourceWg(myListBox);
604
605   myFilterDlg->show();
606 }
607
608 //=======================================================================
609 // name    : onFilterAccepted()
610 // Purpose : SLOT. Called when Filter dlg closed with OK button.
611 //            Uncheck "Select submesh" and "Select group" checkboxes
612 //=======================================================================
613 void SMESHGUI_MultiEditDlg::onFilterAccepted()
614 {
615   myIds.Clear();
616   for (int i = 0, n = myListBox->count(); i < n; i++)
617     myIds.Add(myListBox->text(i).toInt());
618
619   emit ListContensChanged();
620
621   if (mySubmeshChk->isChecked() || myGroupChk->isChecked()) {
622     mySubmeshChk->blockSignals(true);
623     myGroupChk->blockSignals(true);
624     mySubmeshChk->setChecked(false);
625     myGroupChk->setChecked(false);
626     mySubmeshChk->blockSignals(false);
627     myGroupChk->blockSignals(false);
628   }
629   updateButtons();
630 }
631
632 //=======================================================================
633 // name    : SMESHGUI_MultiEditDlg::isIdValid
634 // Purpose : Verify whether Id of element satisfies to filters from viewer
635 //=======================================================================
636 bool SMESHGUI_MultiEditDlg::isIdValid (const int theId) const
637 {
638   SVTK_Selector* aSelector = SMESH::GetSelector();
639   Handle(SMESHGUI_Filter) aFilter =
640     Handle(SMESHGUI_Filter)::DownCast(aSelector->GetFilter(myFilterType));
641
642   return (!aFilter.IsNull() && aFilter->IsObjValid(theId));
643 }
644
645 //=======================================================================
646 // name    : SMESHGUI_MultiEditDlg::onAddBtn
647 // Purpose : SLOT. Called when "Add" button pressed.
648 //           Add selected in viewer entities in list box
649 //=======================================================================
650 void SMESHGUI_MultiEditDlg::onAddBtn()
651 {
652   const SALOME_ListIO& aList = mySelector->StoredIObjects();
653
654   int nbSelected = aList.Extent();
655   if (nbSelected == 0)
656     return;
657
658   TColStd_IndexedMapOfInteger toBeAdded;
659
660   if (!mySubmeshChk->isChecked() && !myGroupChk->isChecked()) {
661     if (nbSelected == 1)
662       mySelector->GetIndex(aList.First(),toBeAdded);
663   } else if (mySubmeshChk->isChecked()) {
664     SALOME_ListIteratorOfListIO anIter(aList);
665     for (; anIter.More(); anIter.Next()) {
666       SMESH::SMESH_subMesh_var aSubMesh =
667         SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIter.Value());
668       if (!aSubMesh->_is_nil()) {
669         if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
670           SMESH::long_array_var anIds = aSubMesh->GetElementsId();
671           for (int i = 0, n = anIds->length(); i < n; i++) {
672             if (isIdValid(anIds[ i ]))
673               toBeAdded.Add(anIds[ i ]);
674           }
675         }
676       }
677     }
678   } else if (myGroupChk->isChecked()) {
679     SALOME_ListIteratorOfListIO anIter(aList);
680     for (; anIter.More(); anIter.Next()) {
681       SMESH::SMESH_GroupBase_var aGroup =
682         SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(anIter.Value());
683       if (!aGroup->_is_nil() && (aGroup->GetType() == SMESH::FACE &&
684                                  entityType() == 0 || aGroup->GetType() == SMESH::VOLUME &&
685                                  entityType() == 1)) {
686         if (aGroup->GetMesh()->GetId() == myMesh->GetId()) {
687           SMESH::long_array_var anIds = aGroup->GetListOfID();
688           for (int i = 0, n = anIds->length(); i < n; i++) {
689             if (isIdValid(anIds[ i ]))
690               toBeAdded.Add(anIds[ i ]);
691           }
692         }
693       }
694     }
695   } else {
696   }
697
698   myBusy = true;
699   bool isGroupOrSubmesh = (mySubmeshChk->isChecked() || myGroupChk->isChecked());
700   mySubmeshChk->setChecked(false);
701   myGroupChk->setChecked(false);
702   for(int i = 1; i <= toBeAdded.Extent(); i++)
703     if (myIds.Add(toBeAdded(i))) {
704       QListBoxItem * item = new QListBoxText(QString("%1").arg(toBeAdded(i)));
705       myListBox->insertItem(item);
706       myListBox->setSelected(item, true);
707     }
708   myBusy = false;
709
710   emit ListContensChanged();
711
712   if (isGroupOrSubmesh)
713     onListSelectionChanged();
714
715   updateButtons();
716 }
717
718 //=======================================================================
719 // name    : SMESHGUI_MultiEditDlg::updateButtons
720 // Purpose : Enable/disable buttons of dialog in accordance with current state
721 //=======================================================================
722 void SMESHGUI_MultiEditDlg::updateButtons()
723 {
724   bool isOk = isValid(false);
725   myOkBtn->setEnabled(isOk);
726   myApplyBtn->setEnabled(isOk);
727
728   bool isListBoxNonEmpty = myListBox->count() > 0;
729   bool isToAll = myToAllChk->isChecked();
730   myFilterBtn->setEnabled(!isToAll);
731   myRemoveBtn->setEnabled(isListBoxNonEmpty && !isToAll);
732   mySortBtn->setEnabled(isListBoxNonEmpty &&!isToAll);
733
734   const SALOME_ListIO& aList = mySelector->StoredIObjects();
735
736   if (isToAll ||
737       myMesh->_is_nil() ||
738       aList.Extent() != 1 ||
739       (SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(aList.First())->_is_nil() &&
740        SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(aList.First())->_is_nil() &&
741        SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(aList.First())->_is_nil()))
742     myAddBtn->setEnabled(false);
743   else
744     myAddBtn->setEnabled(true);
745
746   mySubmeshChk->setEnabled(!isToAll);
747   mySubmeshBtn->setEnabled(mySubmeshChk->isChecked());
748   mySubmesh->setEnabled(mySubmeshChk->isChecked());
749
750   myGroupChk->setEnabled(!isToAll);
751   myGroupBtn->setEnabled(myGroupChk->isChecked());
752   myGroup->setEnabled(myGroupChk->isChecked());
753
754   if (!mySubmeshChk->isChecked())
755     mySubmesh->clear();
756   if (!myGroupChk->isChecked())
757     myGroup->clear();
758
759 }
760
761 //=======================================================================
762 // name    : SMESHGUI_MultiEditDlg::onRemoveBtn
763 // Purpose : SLOT. Called when "Remove" button pressed.
764 //           Remove selected in list box entities
765 //=======================================================================
766 void SMESHGUI_MultiEditDlg::onRemoveBtn()
767 {
768   myBusy = true;
769
770   for (int i = 0, n = myListBox->count(); i < n; i++)
771   {
772     for (int i = myListBox->count(); i > 0; i--) {
773       if (myListBox->isSelected(i - 1))
774       {
775         int anId = myListBox->text(i - 1).toInt();
776         myIds.Remove(anId);
777         myIds.Remove(anId);
778               myListBox->removeItem(i-1);
779       }
780     }
781   }
782   myBusy = false;
783
784   emit ListContensChanged();
785   updateButtons();
786 }
787
788 //=======================================================================
789 // name    : SMESHGUI_MultiEditDlg::onSortListBtn
790 // Purpose : SLOT. Called when "Sort list" button pressed.
791 //           Sort entities of list box
792 //=======================================================================
793 void SMESHGUI_MultiEditDlg::onSortListBtn()
794 {
795   myBusy = true;
796
797   int i, k = myListBox->count();
798   if (k > 0)
799   {
800     QStringList aSelected;
801     std::vector<int> anArray(k);
802     QListBoxItem* anItem;
803     for (anItem = myListBox->firstItem(), i = 0; anItem != 0; anItem = anItem->next(), i++)
804     {
805       anArray[ i ] = anItem->text().toInt();
806       if (anItem->isSelected())
807         aSelected.append(anItem->text());
808     }
809
810     std::sort(anArray.begin(), anArray.end());
811     myListBox->clear();
812     for (i = 0; i < k; i++)
813       myListBox->insertItem(QString::number(anArray[ i ]));
814
815     for (QStringList::iterator it = aSelected.begin(); it != aSelected.end(); ++it)
816     {
817       anItem = myListBox->findItem(*it, Qt::ExactMatch);
818       if (anItem)
819         myListBox->setSelected(anItem, true);
820     }
821   }
822   myBusy = false;
823 }
824
825 //=======================================================================
826 // name    : SMESHGUI_MultiEditDlg::onListSelectionChanged
827 // Purpose : SLOT. Called when selection in list box changed.
828 //           Highlight in selected entities
829 //=======================================================================
830 void SMESHGUI_MultiEditDlg::onListSelectionChanged()
831 {
832   if (myActor == 0 || myBusy)
833     return;
834
835   if (mySubmeshChk->isChecked() || myGroupChk->isChecked())
836     return;
837
838   SMESH_Actor * anActor = SMESH::FindActorByObject(myMesh);
839   if (!anActor)
840     anActor = myActor;
841   TVisualObjPtr anObj = anActor->GetObject();
842
843   TColStd_MapOfInteger anIndexes;
844   for (QListBoxItem* anItem = myListBox->firstItem(); anItem != 0; anItem = anItem->next())
845   {
846     if (anItem->isSelected())
847     {
848       int anId = anItem->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(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(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 == SMESHGUI_TriaFilter)
945       SMESH::SetFilter(new SMESHGUI_TriangleFilter());
946     else if (myFilterType == SMESHGUI_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   SMESH::long_array_var anIds = getIds();
971
972   bool aResult = process(aMeshEditor, anIds.inout());
973   if (aResult) {
974     if (myActor) {
975       SALOME_ListIO sel;
976       mySelectionMgr->selectedObjects( sel );
977       mySelector->ClearIndex();
978       mySelectionMgr->setSelectedObjects( sel );
979       SMESH::UpdateView();
980     }
981
982     myListBox->clear();
983     myIds.Clear();
984     emit ListContensChanged();
985
986     updateButtons();
987   }
988
989   myBusy = false;
990   return aResult;
991 }
992
993 //=======================================================================
994 // name    : SMESHGUI_MultiEditDlg::on3d2dChanged
995 // Purpose :
996 //=======================================================================
997 void SMESHGUI_MultiEditDlg::on3d2dChanged (int type)
998 {
999   if (myEntityType != type) {
1000     myEntityType = type;
1001
1002     myListBox->clear();
1003     myIds.Clear();
1004
1005     emit ListContensChanged();
1006
1007     if (type)
1008       myFilterType = SMESHGUI_VolumeFilter;
1009     else
1010       myFilterType = SMESHGUI_FaceFilter;
1011
1012     updateButtons();
1013     setSelectionMode();
1014   }
1015 }
1016
1017 //=======================================================================
1018 // name    : SMESHGUI_MultiEditDlg::entityType
1019 // Purpose :
1020 //=======================================================================
1021 int SMESHGUI_MultiEditDlg::entityType()
1022 {
1023   return myEntityType;
1024 }
1025
1026 /*!
1027  *  Class       : SMESHGUI_ChangeOrientationDlg
1028  *  Description : Modification of orientation of faces
1029  */
1030
1031 SMESHGUI_ChangeOrientationDlg
1032 ::SMESHGUI_ChangeOrientationDlg(SMESHGUI* theModule,
1033                                 const char* theName):
1034   SMESHGUI_MultiEditDlg(theModule, SMESHGUI_FaceFilter, true, theName)
1035 {
1036   setCaption(tr("CAPTION"));
1037 }
1038
1039 SMESHGUI_ChangeOrientationDlg::~SMESHGUI_ChangeOrientationDlg()
1040 {
1041 }
1042
1043 bool SMESHGUI_ChangeOrientationDlg::process (SMESH::SMESH_MeshEditor_ptr theEditor,
1044                                              const SMESH::long_array&    theIds)
1045 {
1046   return theEditor->Reorient(theIds);
1047 }
1048
1049 /*!
1050  *  Class       : SMESHGUI_UnionOfTrianglesDlg
1051  *  Description : Construction of quadrangles by automatic association of triangles
1052  */
1053
1054 SMESHGUI_UnionOfTrianglesDlg
1055 ::SMESHGUI_UnionOfTrianglesDlg(SMESHGUI* theModule,
1056                                const char* theName):
1057   SMESHGUI_MultiEditDlg(theModule, SMESHGUI_TriaFilter, false, theName)
1058 {
1059   setCaption(tr("CAPTION"));
1060
1061   myComboBoxFunctor->setEnabled(true);
1062   myComboBoxFunctor->insertItem(tr("WARP_ELEMENTS")); // for quadrangles only
1063   myComboBoxFunctor->insertItem(tr("TAPER_ELEMENTS")); // for quadrangles only
1064
1065   // Maximum angle
1066   QGroupBox* aMaxAngleGrp = new QGroupBox (2, Qt::Horizontal, myCriterionGrp);
1067   aMaxAngleGrp->setInsideMargin(0);
1068   aMaxAngleGrp->setFrameStyle(QFrame::NoFrame);
1069   new QLabel (tr("MAXIMUM_ANGLE"), aMaxAngleGrp);
1070   myMaxAngleSpin = new SMESHGUI_SpinBox (aMaxAngleGrp);
1071   myMaxAngleSpin->RangeStepAndValidator(0, 180.0, 1.0, 3);
1072   myMaxAngleSpin->SetValue(30.0);
1073
1074   myCriterionGrp->show();
1075 }
1076
1077 SMESHGUI_UnionOfTrianglesDlg::~SMESHGUI_UnionOfTrianglesDlg()
1078 {
1079 }
1080
1081 bool SMESHGUI_UnionOfTrianglesDlg::process (SMESH::SMESH_MeshEditor_ptr theEditor,
1082                                             const SMESH::long_array&    theIds)
1083 {
1084   SMESH::NumericalFunctor_var aCriterion = getNumericalFunctor();
1085   double aMaxAngle = myMaxAngleSpin->GetValue() * PI / 180.0;
1086   return theEditor->TriToQuad(theIds, aCriterion, aMaxAngle);
1087 }
1088
1089
1090 /*!
1091  *  Class       : SMESHGUI_CuttingOfQuadsDlg
1092  *  Description : Automatic splitting of quadrangles into triangles
1093  */
1094
1095 SMESHGUI_CuttingOfQuadsDlg
1096 ::SMESHGUI_CuttingOfQuadsDlg(SMESHGUI* theModule,
1097                              const char* theName):
1098   SMESHGUI_MultiEditDlg(theModule, SMESHGUI_QuadFilter, false, theName)
1099 {
1100   setCaption(tr("CAPTION"));
1101   myPreviewActor = 0;
1102
1103   myPreviewChk = new QCheckBox (tr("PREVIEW"), mySelGrp);
1104
1105   myCriterionGrp->show();
1106   myGroupChoice->show();
1107   myComboBoxFunctor->setEnabled(false);
1108
1109   connect(myPreviewChk     , SIGNAL(stateChanged(int))   , this, SLOT(onPreviewChk()));
1110   connect(myGroupChoice    , SIGNAL(clicked(int))        , this, SLOT(onCriterionRB()));
1111   connect(myComboBoxFunctor, SIGNAL(activated(int))      , this, SLOT(onPreviewChk()));
1112   connect(this             , SIGNAL(ListContensChanged()), this, SLOT(onPreviewChk()));
1113 }
1114
1115 SMESHGUI_CuttingOfQuadsDlg::~SMESHGUI_CuttingOfQuadsDlg()
1116 {
1117 }
1118
1119 void SMESHGUI_CuttingOfQuadsDlg::onClose()
1120 {
1121   erasePreview();
1122   SMESHGUI_MultiEditDlg::onClose();
1123 }
1124
1125 bool SMESHGUI_CuttingOfQuadsDlg::process (SMESH::SMESH_MeshEditor_ptr theEditor,
1126                                           const SMESH::long_array&    theIds)
1127 {
1128   switch (myGroupChoice->id(myGroupChoice->selected())) {
1129   case 0: // use diagonal 1-3
1130     return theEditor->SplitQuad(theIds, true);
1131   case 1: // use diagonal 2-4
1132     return theEditor->SplitQuad(theIds, false);
1133   default: // use numeric functor
1134     break;
1135   }
1136
1137   SMESH::NumericalFunctor_var aCriterion = getNumericalFunctor();
1138   return theEditor->QuadToTri(theIds, aCriterion);
1139 }
1140
1141 void SMESHGUI_CuttingOfQuadsDlg::onCriterionRB()
1142 {
1143   if (myGroupChoice->id(myGroupChoice->selected()) == 2) // Use numeric functor
1144     myComboBoxFunctor->setEnabled(true);
1145   else
1146     myComboBoxFunctor->setEnabled(false);
1147
1148   onPreviewChk();
1149 }
1150
1151 void SMESHGUI_CuttingOfQuadsDlg::onPreviewChk()
1152 {
1153   myPreviewChk->isChecked() ? displayPreview() : erasePreview();
1154 }
1155
1156 void SMESHGUI_CuttingOfQuadsDlg::erasePreview()
1157 {
1158   if (myPreviewActor == 0)
1159     return;
1160
1161   if (SVTK_ViewWindow* vf = SMESH::GetCurrentVtkView()) {
1162     vf->RemoveActor(myPreviewActor);
1163     vf->Repaint();
1164   }
1165   myPreviewActor->Delete();
1166   myPreviewActor = 0;
1167 }
1168   
1169 void SMESHGUI_CuttingOfQuadsDlg::displayPreview()
1170 {
1171   if (myActor == 0)
1172     return;
1173
1174   if (myPreviewActor != 0)
1175     erasePreview();
1176
1177   // get Ids of elements
1178   SMESH::long_array_var anElemIds = getIds();
1179   if (getIds()->length() == 0)
1180     return;
1181
1182   SMDS_Mesh* aMesh = myActor->GetObject()->GetMesh();
1183   if (aMesh == 0)
1184     return;
1185
1186   // 0 - use diagonal 1-3, 1 - use diagonal 2-4, 2 - use numerical functor
1187   int aChoice = myGroupChoice->id(myGroupChoice->selected());
1188   SMESH::NumericalFunctor_var aCriterion  = SMESH::NumericalFunctor::_nil();
1189   SMESH::SMESH_MeshEditor_var aMeshEditor = SMESH::SMESH_MeshEditor::_nil();
1190   if (aChoice == 2) {
1191     aCriterion  = getNumericalFunctor();
1192     aMeshEditor = myMesh->GetMeshEditor();
1193     if (aMeshEditor->_is_nil())
1194       return;
1195   }
1196
1197   //Create grid
1198   vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
1199
1200   vtkIdType aNbCells = anElemIds->length() * 2;
1201   vtkIdType aCellsSize = 4 * aNbCells;
1202   vtkCellArray* aConnectivity = vtkCellArray::New();
1203   aConnectivity->Allocate(aCellsSize, 0);
1204
1205   vtkPoints* aPoints = vtkPoints::New();
1206   aPoints->SetNumberOfPoints(anElemIds->length() * 4);
1207
1208   vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
1209   aCellTypesArray->SetNumberOfComponents(1);
1210   aCellTypesArray->Allocate(aNbCells * aCellTypesArray->GetNumberOfComponents());
1211
1212   vtkIdList *anIdList = vtkIdList::New();
1213   anIdList->SetNumberOfIds(3);
1214
1215   TColStd_DataMapOfIntegerInteger anIdToVtk;
1216
1217   int aNodes[ 4 ];
1218   int nbPoints = -1;
1219   for (int i = 0, n = anElemIds->length(); i < n; i++)
1220   {
1221     const SMDS_MeshElement* anElem = aMesh->FindElement(anElemIds[ i ]);
1222     if (anElem == 0 || anElem->NbNodes() != 4)
1223       continue;
1224
1225     SMDS_ElemIteratorPtr anIter = anElem->nodesIterator();
1226     int k = 0;
1227     while (anIter->more()) {
1228       const SMDS_MeshNode* aNode = static_cast<const SMDS_MeshNode*>(anIter->next());
1229       if (aNode)
1230       {
1231         if (!anIdToVtk.IsBound(aNode->GetID()))
1232         {
1233           aPoints->SetPoint(++nbPoints, aNode->X(), aNode->Y(), aNode->Z());
1234           anIdToVtk.Bind(aNode->GetID(), nbPoints);
1235         }
1236
1237         aNodes[ k++ ] = aNode->GetID();
1238       }
1239     }
1240
1241     if (k != 4)
1242       continue;
1243
1244     bool isDiag13 = true;
1245     if (aChoice == 0) // use diagonal 1-3
1246     {
1247       isDiag13 = true;
1248     }
1249     else if (aChoice == 1) // use diagonal 2-4
1250     {
1251       isDiag13 = false;
1252     }
1253     else // use numerical functor
1254     {
1255       // compare two sets of possible triangles
1256       int diag = aMeshEditor->BestSplit(anElemIds[i], aCriterion);
1257       if (diag == 1) // 1-3
1258         isDiag13 = true;
1259       else if (diag == 2) // 2-4
1260         isDiag13 = false;
1261       else // error
1262         continue;
1263     }
1264
1265     if (isDiag13)
1266     {
1267       anIdList->SetId(0, anIdToVtk(aNodes[ 0 ]));
1268       anIdList->SetId(1, anIdToVtk(aNodes[ 1 ]));
1269       anIdList->SetId(2, anIdToVtk(aNodes[ 2 ]));
1270       aConnectivity->InsertNextCell(anIdList);
1271       aCellTypesArray->InsertNextValue(VTK_TRIANGLE);
1272
1273       anIdList->SetId(0, anIdToVtk(aNodes[ 2 ]));
1274       anIdList->SetId(1, anIdToVtk(aNodes[ 3 ]));
1275       anIdList->SetId(2, anIdToVtk(aNodes[ 0 ]));
1276       aConnectivity->InsertNextCell(anIdList);
1277       aCellTypesArray->InsertNextValue(VTK_TRIANGLE);
1278     }
1279     else
1280     {
1281       anIdList->SetId(0, anIdToVtk(aNodes[ 1 ]));
1282       anIdList->SetId(1, anIdToVtk(aNodes[ 2 ]));
1283       anIdList->SetId(2, anIdToVtk(aNodes[ 3 ]));
1284       aConnectivity->InsertNextCell(anIdList);
1285       aCellTypesArray->InsertNextValue(VTK_TRIANGLE);
1286
1287       anIdList->SetId(0, anIdToVtk(aNodes[ 3 ]));
1288       anIdList->SetId(1, anIdToVtk(aNodes[ 0 ]));
1289       anIdList->SetId(2, anIdToVtk(aNodes[ 1 ]));
1290       aConnectivity->InsertNextCell(anIdList);
1291       aCellTypesArray->InsertNextValue(VTK_TRIANGLE);
1292     }
1293   }
1294
1295   vtkIntArray* aCellLocationsArray = vtkIntArray::New();
1296   aCellLocationsArray->SetNumberOfComponents(1);
1297   aCellLocationsArray->SetNumberOfTuples(aNbCells);
1298
1299   aConnectivity->InitTraversal();
1300   for(vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell(npts, pts); idType++)
1301     aCellLocationsArray->SetValue(idType, aConnectivity->GetTraversalLocation(npts));
1302
1303   aGrid->SetPoints(aPoints);
1304   aGrid->SetCells(aCellTypesArray, aCellLocationsArray,aConnectivity);
1305
1306   // Create and display actor
1307   vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
1308   aMapper->SetInput(aGrid);
1309
1310   myPreviewActor = SALOME_Actor::New();
1311   myPreviewActor->PickableOff();
1312   myPreviewActor->SetMapper(aMapper);
1313
1314   vtkProperty* aProp = vtkProperty::New();
1315   aProp->SetRepresentationToWireframe();
1316   aProp->SetColor(250, 0, 250);
1317   aProp->SetLineWidth(myActor->GetLineWidth() + 1);
1318   myPreviewActor->SetProperty(aProp);
1319
1320   SMESH::GetCurrentVtkView()->AddActor(myPreviewActor);
1321   SMESH::GetCurrentVtkView()->Repaint();
1322
1323   aProp->Delete();
1324   aPoints->Delete();
1325   aConnectivity->Delete();
1326   aGrid->Delete();
1327   aMapper->Delete();
1328   anIdList->Delete();
1329   aCellTypesArray->Delete();
1330   aCellLocationsArray->Delete();
1331 }