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