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