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