Salome HOME
Merge from V6_main 01/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     myMesh = SMESH::GetMeshByIO(anIO);
611   }
612
613   if (nbSel > 0) {
614     myActor = SMESH::FindActorByEntry(aList.First()->getEntry());
615     if (!myActor)
616       myActor = SMESH::FindActorByObject(myMesh);
617     SVTK_Selector* aSelector = SMESH::GetSelector();
618     Handle(VTKViewer_Filter) aFilter = aSelector->GetFilter(myFilterType);
619     if (!aFilter.IsNull())
620       aFilter->SetActor(myActor);
621   }
622   myBusy = false;
623
624   updateButtons();
625 }
626
627 //=======================================================================
628 // name    : SMESHGUI_MultiEditDlg::onDeactivate
629 // Purpose : SLOT called when dialog must be deativated
630 //=======================================================================
631 void SMESHGUI_MultiEditDlg::onDeactivate()
632 {
633   setEnabled(false);
634 }
635
636 //=======================================================================
637 // name    : SMESHGUI_MultiEditDlg::enterEvent
638 // Purpose : Event filter
639 //=======================================================================
640 void SMESHGUI_MultiEditDlg::enterEvent (QEvent*)
641 {
642   if (!isEnabled()) {
643     mySMESHGUI->EmitSignalDeactivateDialog();
644     setEnabled(true);
645     setSelectionMode();
646   }
647 }
648
649 //=======================================================================
650 // name    : SMESHGUI_MultiEditDlg::onFilterBtn
651 // Purpose : SLOT. Called when "Filter" button pressed.
652 //           Start "Selection filters" dialog
653 //=======================================================================
654 void SMESHGUI_MultiEditDlg::onFilterBtn()
655 {
656   if (myFilterDlg == 0) {
657     myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, entityType() ? SMESH::VOLUME : SMESH::FACE);
658     connect(myFilterDlg, SIGNAL(Accepted()), SLOT(onFilterAccepted()));
659   } else {
660     myFilterDlg->Init(entityType() ? SMESH::VOLUME : SMESH::FACE);
661   }
662
663   myFilterDlg->SetSelection();
664   myFilterDlg->SetMesh(myMesh);
665
666   myFilterDlg->show();
667 }
668
669 //=======================================================================
670 // name    : onFilterAccepted()
671 // Purpose : SLOT. Called when Filter dlg closed with OK button.
672 //            Uncheck "Select submesh" and "Select group" checkboxes
673 //=======================================================================
674 void SMESHGUI_MultiEditDlg::onFilterAccepted()
675 {
676   myIds.Clear();
677   for (int i = 0, n = myListBox->count(); i < n; i++)
678     myIds.Add(myListBox->item(i)->text().toInt());
679
680   emit ListContensChanged();
681
682   if (mySubmeshChk->isChecked() || myGroupChk->isChecked()) {
683     mySubmeshChk->blockSignals(true);
684     myGroupChk->blockSignals(true);
685     mySubmeshChk->setChecked(false);
686     myGroupChk->setChecked(false);
687     mySubmeshChk->blockSignals(false);
688     myGroupChk->blockSignals(false);
689   }
690   updateButtons();
691 }
692
693 //=======================================================================
694 // name    : SMESHGUI_MultiEditDlg::isIdValid
695 // Purpose : Verify whether Id of element satisfies to filters from viewer
696 //=======================================================================
697 bool SMESHGUI_MultiEditDlg::isIdValid (const int theId) const
698 {
699   SVTK_Selector* aSelector = SMESH::GetSelector();
700   Handle(SMESHGUI_Filter) aFilter =
701     Handle(SMESHGUI_Filter)::DownCast(aSelector->GetFilter(myFilterType));
702
703   return (!aFilter.IsNull() && aFilter->IsObjValid(theId));
704 }
705
706 //=======================================================================
707 // name    : SMESHGUI_MultiEditDlg::onAddBtn
708 // Purpose : SLOT. Called when "Add" button pressed.
709 //           Add selected in viewer entities in list box
710 //=======================================================================
711 void SMESHGUI_MultiEditDlg::onAddBtn()
712 {
713   const SALOME_ListIO& aList = mySelector->StoredIObjects();
714
715   int nbSelected = aList.Extent();
716   if (nbSelected == 0)
717     return;
718
719   TColStd_IndexedMapOfInteger toBeAdded;
720
721   if (!mySubmeshChk->isChecked() && !myGroupChk->isChecked()) {
722     if (nbSelected > 0)
723       mySelector->GetIndex(aList.First(),toBeAdded);
724   } else if (mySubmeshChk->isChecked()) {
725     SALOME_ListIteratorOfListIO anIter(aList);
726     for ( ; anIter.More(); anIter.Next()) {
727       SMESH::SMESH_subMesh_var aSubMesh =
728         SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIter.Value());
729       if (!aSubMesh->_is_nil()) {
730         if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
731           SMESH::long_array_var anIds = aSubMesh->GetElementsId();
732           for (int i = 0, n = anIds->length(); i < n; i++) {
733             if (isIdValid(anIds[ i ]))
734               toBeAdded.Add(anIds[ i ]);
735           }
736         }
737       }
738     }
739   } else if (myGroupChk->isChecked()) {
740     SALOME_ListIteratorOfListIO anIter(aList);
741     for ( ; anIter.More(); anIter.Next()) {
742       SMESH::SMESH_GroupBase_var aGroup =
743         SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(anIter.Value());
744       if (!aGroup->_is_nil() && ((aGroup->GetType() == SMESH::FACE && entityType() == 0) || 
745                                  (aGroup->GetType() == SMESH::VOLUME && entityType() == 1))) {
746         if (aGroup->GetMesh()->GetId() == myMesh->GetId()) {
747           SMESH::long_array_var anIds = aGroup->GetListOfID();
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 {
756   }
757
758   myBusy = true;
759   bool isGroupOrSubmesh = (mySubmeshChk->isChecked() || myGroupChk->isChecked());
760   mySubmeshChk->setChecked(false);
761   myGroupChk->setChecked(false);
762   QStringList items;
763   for(int i = 1; i <= toBeAdded.Extent(); i++)
764     if (myIds.Add(toBeAdded(i))) {
765       items.append(QString("%1").arg(toBeAdded(i)));
766     }
767   myListBox->addItems(items);
768   myListBox->selectAll();
769   myBusy = false;
770
771   emit ListContensChanged();
772
773   if (isGroupOrSubmesh)
774     onListSelectionChanged();
775
776   updateButtons();
777 }
778
779 //=======================================================================
780 // name    : SMESHGUI_MultiEditDlg::updateButtons
781 // Purpose : Enable/disable buttons of dialog in accordance with current state
782 //=======================================================================
783 void SMESHGUI_MultiEditDlg::updateButtons()
784 {
785   bool isOk = isValid(false);
786   myOkBtn->setEnabled(isOk);
787   myApplyBtn->setEnabled(isOk);
788
789   bool isListBoxNonEmpty = myListBox->count() > 0;
790   bool isToAll = myToAllChk->isChecked();
791   myFilterBtn->setEnabled(!isToAll);
792   myRemoveBtn->setEnabled(isListBoxNonEmpty && !isToAll);
793   mySortBtn->setEnabled(isListBoxNonEmpty &&!isToAll);
794
795   const SALOME_ListIO& aList = mySelector->StoredIObjects();
796
797   if (isToAll ||
798       myMesh->_is_nil() ||
799       aList.Extent() < 1 ||
800       (SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(aList.First())->_is_nil() &&
801        SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(aList.First())->_is_nil() &&
802        SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(aList.First())->_is_nil()))
803     myAddBtn->setEnabled(false);
804   else
805     myAddBtn->setEnabled(true);
806
807   mySubmeshChk->setEnabled(!isToAll);
808   mySubmeshBtn->setEnabled(mySubmeshChk->isChecked());
809   mySubmesh->setEnabled(mySubmeshChk->isChecked());
810
811   myGroupChk->setEnabled(!isToAll);
812   myGroupBtn->setEnabled(myGroupChk->isChecked());
813   myGroup->setEnabled(myGroupChk->isChecked());
814
815   if (!mySubmeshChk->isChecked())
816     mySubmesh->clear();
817   if (!myGroupChk->isChecked())
818     myGroup->clear();
819
820 }
821
822 //=======================================================================
823 // name    : SMESHGUI_MultiEditDlg::onRemoveBtn
824 // Purpose : SLOT. Called when "Remove" button pressed.
825 //           Remove selected in list box entities
826 //=======================================================================
827 void SMESHGUI_MultiEditDlg::onRemoveBtn()
828 {
829   myBusy = true;
830
831   QList<QListWidgetItem*> selItems = myListBox->selectedItems();
832   QListWidgetItem* item;
833   foreach(item, selItems)
834   {
835     myIds.Remove(item->text().toInt());
836     delete item;
837   }
838
839   myBusy = false;
840
841   emit ListContensChanged();
842   updateButtons();
843 }
844
845 //=======================================================================
846 // name    : SMESHGUI_MultiEditDlg::onSortListBtn
847 // Purpose : SLOT. Called when "Sort list" button pressed.
848 //           Sort entities of list box
849 //=======================================================================
850 void SMESHGUI_MultiEditDlg::onSortListBtn()
851 {
852   myBusy = true;
853
854   int i, k = myListBox->count();
855   if (k > 0)
856   {
857     QList<int> aSelected;
858     std::vector<int> anArray(k);
859     for (i = 0; i < k; i++)
860     {
861       int id = myListBox->item(i)->text().toInt();
862       anArray[ i ] = id;
863       if (myListBox->item(i)->isSelected())
864         aSelected.append(id);
865     }
866
867     std::sort(anArray.begin(), anArray.end());
868
869     myListBox->clear();
870     for (i = 0; i < k; i++) {
871       QListWidgetItem* item = new QListWidgetItem(QString::number(anArray[i]));
872       myListBox->addItem(item);
873       item->setSelected(aSelected.contains(anArray[i]));
874     }
875   }
876   myBusy = false;
877 }
878
879 //=======================================================================
880 // name    : SMESHGUI_MultiEditDlg::onListSelectionChanged
881 // Purpose : SLOT. Called when selection in list box changed.
882 //           Highlight in selected entities
883 //=======================================================================
884 void SMESHGUI_MultiEditDlg::onListSelectionChanged()
885 {
886   if (myActor == 0 || myBusy)
887     return;
888
889   if (mySubmeshChk->isChecked() || myGroupChk->isChecked())
890     return;
891
892   SMESH_Actor * anActor = SMESH::FindActorByObject(myMesh);
893   if (!anActor)
894     anActor = myActor;
895   TVisualObjPtr anObj = anActor->GetObject();
896
897   TColStd_MapOfInteger anIndexes;
898   int total = myListBox->count();
899   for (int i = 0; i < total; i++)
900   {
901     if (myListBox->item(i)->isSelected())
902     {
903       int anId = myListBox->item(i)->text().toInt();
904       if (anObj->GetElemVTKId(anId) >= 0) // avoid exception in hilight
905         anIndexes.Add(anId);
906     }
907   }
908
909   mySelector->AddOrRemoveIndex(anActor->getIO(),anIndexes,false);
910   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
911     aViewWindow->highlight(anActor->getIO(),true,true);
912 }
913
914 //=======================================================================
915 // name    : SMESHGUI_MultiEditDlg::onSubmeshChk
916 // Purpose : SLOT. Called when state of "SubMesh" check box changed.
917 //           Activate/deactivate selection of submeshes
918 //=======================================================================
919 void SMESHGUI_MultiEditDlg::onSubmeshChk()
920 {
921   bool isChecked = mySubmeshChk->isChecked();
922   mySubmeshBtn->setEnabled(isChecked);
923   mySubmesh->setEnabled(isChecked);
924   if (!isChecked)
925     mySubmesh->clear();
926   if (isChecked && myGroupChk->isChecked())
927       myGroupChk->setChecked(false);
928
929   setSelectionMode();
930 }
931
932 //=======================================================================
933 // name    : SMESHGUI_MultiEditDlg::onGroupChk
934 // Purpose : SLOT. Called when state of "Group" check box changed.
935 //           Activate/deactivate selection of groupes
936 //=======================================================================
937 void SMESHGUI_MultiEditDlg::onGroupChk()
938 {
939   bool isChecked = myGroupChk->isChecked();
940   myGroupBtn->setEnabled(isChecked);
941   myGroup->setEnabled(isChecked);
942   if (!isChecked)
943     myGroup->clear();
944   if (isChecked && mySubmeshChk->isChecked())
945       mySubmeshChk->setChecked(false);
946
947   setSelectionMode();
948 }
949
950 //=======================================================================
951 // name    : SMESHGUI_MultiEditDlg::onToAllChk
952 // Purpose : SLOT. Called when state of "Apply to all" check box changed.
953 //           Activate/deactivate selection
954 //=======================================================================
955 void SMESHGUI_MultiEditDlg::onToAllChk()
956 {
957   bool isChecked = myToAllChk->isChecked();
958
959   if (isChecked)
960     myListBox->clear();
961
962   myIds.Clear();
963
964   emit ListContensChanged();
965
966   updateButtons();
967   setSelectionMode();
968 }
969
970 //=======================================================================
971 // name    : SMESHGUI_MultiEditDlg::setSelectionMode
972 // Purpose : Set selection mode
973 //=======================================================================
974 void SMESHGUI_MultiEditDlg::setSelectionMode()
975 {
976   SMESH::RemoveFilters();
977
978   mySelectionMgr->clearSelected();
979   mySelectionMgr->clearFilters();
980
981   if (mySubmeshChk->isChecked()) {
982     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
983       aViewWindow->SetSelectionMode(ActorSelection);
984     mySelectionMgr->installFilter(new SMESH_TypeFilter(SMESH::SUBMESH));
985   }
986   else if (myGroupChk->isChecked()) {
987     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
988       aViewWindow->SetSelectionMode(ActorSelection);
989     mySelectionMgr->installFilter(new SMESH_TypeFilter(SMESH::GROUP));
990   }
991
992   if (entityType()) {
993     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
994       aViewWindow->SetSelectionMode(VolumeSelection);
995     SMESH::SetFilter(new SMESHGUI_VolumesFilter());
996   } else {
997     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
998       aViewWindow->SetSelectionMode(FaceSelection);
999     if (myFilterType == SMESH::TriaFilter)
1000       SMESH::SetFilter(new SMESHGUI_TriangleFilter());
1001     else if (myFilterType == SMESH::QuadFilter)
1002       SMESH::SetFilter(new SMESHGUI_QuadrangleFilter());
1003     else
1004       SMESH::SetFilter(new SMESHGUI_FacesFilter());
1005   }
1006 }
1007
1008 //=======================================================================
1009 // name    : SMESHGUI_MultiEditDlg::onApply
1010 // Purpose : SLOT. Called when "Apply" button clicked.
1011 //=======================================================================
1012 bool SMESHGUI_MultiEditDlg::onApply()
1013 {
1014   if (mySMESHGUI->isActiveStudyLocked())
1015     return false;
1016   if (!isValid(true))
1017     return false;
1018
1019   SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
1020   if (aMeshEditor->_is_nil())
1021     return false;
1022
1023   myBusy = true;
1024
1025   SUIT_OverrideCursor aWaitCursor;
1026
1027   SMESH::SMESH_IDSource_var obj;
1028   SMESH::long_array_var anIds = getIds(obj);
1029
1030   bool aResult = process(aMeshEditor, anIds.inout(), obj);
1031   if (aResult) {
1032     if (myActor) {
1033       SALOME_ListIO sel;
1034       mySelectionMgr->selectedObjects( sel );
1035       mySelector->ClearIndex();
1036       mySelectionMgr->setSelectedObjects( sel );
1037       SMESH::UpdateView();
1038       SMESHGUI::Modified();
1039     }
1040
1041     myListBox->clear();
1042     myIds.Clear();
1043     emit ListContensChanged();
1044
1045     updateButtons();
1046   }
1047
1048   myBusy = false;
1049   return aResult;
1050 }
1051
1052 //=======================================================================
1053 // name    : SMESHGUI_MultiEditDlg::on3d2dChanged
1054 // Purpose :
1055 //=======================================================================
1056 void SMESHGUI_MultiEditDlg::on3d2dChanged (int type)
1057 {
1058   if (myEntityType != type) {
1059     myEntityType = type;
1060
1061     myListBox->clear();
1062     myIds.Clear();
1063
1064     emit ListContensChanged();
1065
1066     if (type)
1067       myFilterType = SMESH::VolumeFilter;
1068     else
1069       myFilterType = SMESH::FaceFilter;
1070
1071     updateButtons();
1072     setSelectionMode();
1073   }
1074 }
1075
1076 //=======================================================================
1077 // name    : SMESHGUI_MultiEditDlg::entityType
1078 // Purpose :
1079 //=======================================================================
1080 int SMESHGUI_MultiEditDlg::entityType()
1081 {
1082   return myEntityType;
1083 }
1084
1085 //=================================================================================
1086 // function : keyPressEvent()
1087 // purpose  :
1088 //=================================================================================
1089 void SMESHGUI_MultiEditDlg::keyPressEvent( QKeyEvent* e )
1090 {
1091   QDialog::keyPressEvent( e );
1092   if ( e->isAccepted() )
1093     return;
1094
1095   if ( e->key() == Qt::Key_F1 ) {
1096     e->accept();
1097     onHelp();
1098   }
1099 }
1100
1101 /*!
1102  *  Class       : SMESHGUI_ChangeOrientationDlg
1103  *  Description : Modification of orientation of faces
1104  */
1105
1106 SMESHGUI_ChangeOrientationDlg
1107 ::SMESHGUI_ChangeOrientationDlg(SMESHGUI* theModule):
1108   SMESHGUI_MultiEditDlg(theModule, SMESH::FaceFilter, true)
1109 {
1110   setWindowTitle(tr("CAPTION"));
1111   myHelpFileName = "changing_orientation_of_elements_page.html";
1112 }
1113
1114 SMESHGUI_ChangeOrientationDlg::~SMESHGUI_ChangeOrientationDlg()
1115 {
1116 }
1117
1118 bool SMESHGUI_ChangeOrientationDlg::process (SMESH::SMESH_MeshEditor_ptr theEditor,
1119                                              const SMESH::long_array&    theIds,
1120                                              SMESH::SMESH_IDSource_ptr   obj)
1121 {
1122   if ( CORBA::is_nil( obj ))
1123     return theEditor->Reorient(theIds);
1124   else
1125     return theEditor->ReorientObject( obj );
1126 }
1127
1128 /*!
1129  *  Class       : SMESHGUI_UnionOfTrianglesDlg
1130  *  Description : Construction of quadrangles by automatic association of triangles
1131  */
1132
1133 SMESHGUI_UnionOfTrianglesDlg
1134 ::SMESHGUI_UnionOfTrianglesDlg(SMESHGUI* theModule):
1135   SMESHGUI_MultiEditDlg(theModule, SMESH::TriaFilter, false)
1136 {
1137   setWindowTitle(tr("CAPTION"));
1138
1139   myComboBoxFunctor->setEnabled(true);  
1140   myComboBoxFunctor->addItem(tr("AREA_ELEMENTS"));
1141   myComboBoxFunctor->addItem(tr("WARP_ELEMENTS")); // for quadrangles only
1142   myComboBoxFunctor->addItem(tr("TAPER_ELEMENTS")); // for quadrangles only
1143
1144   // Maximum angle
1145   QWidget* aMaxAngleGrp = new QWidget(myCriterionGrp);
1146   QHBoxLayout* aMaxAngleGrpLayout = new QHBoxLayout(aMaxAngleGrp);
1147   aMaxAngleGrpLayout->setMargin(0);
1148   aMaxAngleGrpLayout->setSpacing(SPACING);
1149
1150   QLabel* aLab = new QLabel (tr("MAXIMUM_ANGLE"), aMaxAngleGrp);
1151   myMaxAngleSpin = new SMESHGUI_SpinBox (aMaxAngleGrp);
1152   myMaxAngleSpin->RangeStepAndValidator(0, 180.0, 1.0, "angle_precision");
1153   myMaxAngleSpin->SetValue(30.0);
1154
1155   aMaxAngleGrpLayout->addWidget(aLab);
1156   aMaxAngleGrpLayout->addWidget(myMaxAngleSpin);
1157
1158   ((QVBoxLayout*)(myCriterionGrp->layout()))->addWidget(aMaxAngleGrp);
1159   myCriterionGrp->show();
1160
1161   myHelpFileName = "uniting_set_of_triangles_page.html";
1162 }
1163
1164 SMESHGUI_UnionOfTrianglesDlg::~SMESHGUI_UnionOfTrianglesDlg()
1165 {
1166 }
1167
1168 bool SMESHGUI_UnionOfTrianglesDlg::isValid (const bool theMess)
1169 {
1170   bool ok = SMESHGUI_MultiEditDlg::isValid( theMess );
1171   if( !ok )
1172     return false;
1173
1174   QString msg;
1175   ok = myMaxAngleSpin->isValid( msg, theMess );
1176   if( !ok ) {
1177     if( theMess ) {
1178       QString str( tr( "SMESH_INCORRECT_INPUT" ) );
1179       if ( !msg.isEmpty() )
1180         str += "\n" + msg;
1181       SUIT_MessageBox::critical( this, tr( "SMESH_ERROR" ), str );
1182     }
1183     return false;
1184   }
1185
1186   return ok;
1187 }
1188
1189 bool SMESHGUI_UnionOfTrianglesDlg::process (SMESH::SMESH_MeshEditor_ptr theEditor,
1190                                             const SMESH::long_array&    theIds,
1191                                             SMESH::SMESH_IDSource_ptr   obj)
1192 {
1193   {
1194     QStringList aParameters;
1195     aParameters << myMaxAngleSpin->text();
1196     myMesh->SetParameters( aParameters.join(":").toLatin1().constData() );
1197   }
1198   SMESH::NumericalFunctor_var aCriterion = getNumericalFunctor();
1199   double aMaxAngle = myMaxAngleSpin->GetValue() * M_PI / 180.0;
1200   bool ok;
1201   if ( CORBA::is_nil( obj ))
1202     ok = theEditor->TriToQuad(theIds, aCriterion, aMaxAngle);
1203   else
1204     ok = theEditor->TriToQuadObject(obj, aCriterion, aMaxAngle);
1205   return ok;
1206 }
1207
1208
1209 /*!
1210  *  Class       : SMESHGUI_CuttingOfQuadsDlg
1211  *  Description : Automatic splitting of quadrangles into triangles
1212  */
1213
1214 SMESHGUI_CuttingOfQuadsDlg
1215 ::SMESHGUI_CuttingOfQuadsDlg(SMESHGUI* theModule):
1216   SMESHGUI_MultiEditDlg(theModule, SMESH::QuadFilter, false)
1217 {
1218   setWindowTitle(tr("CAPTION"));
1219   myPreviewActor = 0;
1220
1221   myPreviewChk = new QCheckBox (tr("PREVIEW"), mySelGrp);
1222   QGridLayout* aLay = (QGridLayout*)(mySelGrp->layout());
1223   aLay->addWidget(myPreviewChk, aLay->rowCount(), 0, 1, aLay->columnCount());
1224
1225   myCriterionGrp->show();
1226   myChoiceWidget->show();
1227   myComboBoxFunctor->insertItem(0, tr("MIN_DIAG_ELEMENTS"));
1228   myComboBoxFunctor->setCurrentIndex(0);
1229   myComboBoxFunctor->setEnabled(false);
1230
1231   connect(myPreviewChk,      SIGNAL(stateChanged(int)),    this, SLOT(onPreviewChk()));
1232   connect(myGroupChoice,     SIGNAL(buttonClicked(int)),   this, SLOT(onCriterionRB()));
1233   connect(myComboBoxFunctor, SIGNAL(activated(int)),       this, SLOT(onPreviewChk()));
1234   connect(this,              SIGNAL(ListContensChanged()), this, SLOT(onPreviewChk()));
1235
1236   myHelpFileName = "cutting_quadrangles_page.html";
1237 }
1238
1239 SMESHGUI_CuttingOfQuadsDlg::~SMESHGUI_CuttingOfQuadsDlg()
1240 {
1241 }
1242
1243 void SMESHGUI_CuttingOfQuadsDlg::reject()
1244 {
1245   erasePreview();
1246   SMESHGUI_MultiEditDlg::reject();
1247 }
1248
1249 bool SMESHGUI_CuttingOfQuadsDlg::process (SMESH::SMESH_MeshEditor_ptr theEditor,
1250                                           const SMESH::long_array&    theIds,
1251                                           SMESH::SMESH_IDSource_ptr   obj)
1252 {
1253   bool hasObj = (! CORBA::is_nil( obj ));
1254   switch (myGroupChoice->checkedId()) {
1255   case 0: // use diagonal 1-3
1256     return hasObj ? theEditor->SplitQuadObject(obj, true) : theEditor->SplitQuad(theIds, true);
1257   case 1: // use diagonal 2-4
1258     return hasObj ? theEditor->SplitQuadObject(obj, false) : theEditor->SplitQuad(theIds, false);
1259   default: // use numeric functor
1260     break;
1261   }
1262
1263   SMESH::NumericalFunctor_var aCrit = getNumericalFunctor();
1264   return hasObj ? theEditor->QuadToTriObject(obj, aCrit) : theEditor->QuadToTri(theIds, aCrit);
1265 }
1266
1267 void SMESHGUI_CuttingOfQuadsDlg::onCriterionRB()
1268 {
1269   if (myGroupChoice->checkedId() == 2) // Use numeric functor
1270     myComboBoxFunctor->setEnabled(true);
1271   else
1272     myComboBoxFunctor->setEnabled(false);
1273
1274   onPreviewChk();
1275 }
1276
1277 void SMESHGUI_CuttingOfQuadsDlg::onPreviewChk()
1278 {
1279   myPreviewChk->isChecked() ? displayPreview() : erasePreview();
1280 }
1281
1282 void SMESHGUI_CuttingOfQuadsDlg::erasePreview()
1283 {
1284   if (myPreviewActor == 0)
1285     return;
1286
1287   if (SVTK_ViewWindow* vf = SMESH::GetCurrentVtkView()) {
1288     vf->RemoveActor(myPreviewActor);
1289     vf->Repaint();
1290   }
1291   myPreviewActor->Delete();
1292   myPreviewActor = 0;
1293 }
1294   
1295 void SMESHGUI_CuttingOfQuadsDlg::displayPreview()
1296 {
1297   if (myActor == 0)
1298     return;
1299
1300   if (myPreviewActor != 0)
1301     erasePreview();
1302
1303   // get Ids of elements
1304   SMESH::SMESH_IDSource_var obj;
1305   SMESH::long_array_var anElemIds = getIds(obj);
1306   if (anElemIds->length() == 0 && obj->_is_nil() )
1307     return;
1308
1309   SMDS_Mesh* aMesh = myActor->GetObject()->GetMesh();
1310   if (aMesh == 0)
1311     return;
1312
1313   // 0 - use diagonal 1-3, 1 - use diagonal 2-4, 2 - use numerical functor
1314   int aChoice = myGroupChoice->checkedId();
1315   SMESH::NumericalFunctor_var aCriterion  = SMESH::NumericalFunctor::_nil();
1316   SMESH::SMESH_MeshEditor_var aMeshEditor = SMESH::SMESH_MeshEditor::_nil();
1317   if (aChoice == 2) {
1318     aCriterion  = getNumericalFunctor();
1319     aMeshEditor = myMesh->GetMeshEditor();
1320     if (aMeshEditor->_is_nil())
1321       return;
1322   }
1323
1324   //Create grid
1325   vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
1326
1327   vtkIdType aNbCells = anElemIds->length() * 2;
1328   vtkIdType aCellsSize = 4 * aNbCells;
1329   vtkCellArray* aConnectivity = vtkCellArray::New();
1330   aConnectivity->Allocate(aCellsSize, 0);
1331
1332   vtkPoints* aPoints = vtkPoints::New();
1333   aPoints->SetNumberOfPoints(anElemIds->length() * 4);
1334
1335   vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
1336   aCellTypesArray->SetNumberOfComponents(1);
1337   aCellTypesArray->Allocate(aNbCells * aCellTypesArray->GetNumberOfComponents());
1338
1339   vtkIdList *anIdList = vtkIdList::New();
1340   anIdList->SetNumberOfIds(3);
1341
1342   TColStd_DataMapOfIntegerInteger anIdToVtk;
1343
1344   int aNodes[ 4 ];
1345   int nbPoints = -1;
1346   for (int i = 0, n = anElemIds->length(); i < n; i++)
1347   {
1348     const SMDS_MeshElement* anElem = aMesh->FindElement(anElemIds[ i ]);
1349     if (anElem == 0 || anElem->NbNodes() != 4)
1350       continue;
1351
1352     SMDS_ElemIteratorPtr anIter = anElem->nodesIterator();
1353     int k = 0;
1354     while (anIter->more()) {
1355       const SMDS_MeshNode* aNode = static_cast<const SMDS_MeshNode*>(anIter->next());
1356       if (aNode)
1357       {
1358         if (!anIdToVtk.IsBound(aNode->GetID()))
1359         {
1360           aPoints->SetPoint(++nbPoints, aNode->X(), aNode->Y(), aNode->Z());
1361           anIdToVtk.Bind(aNode->GetID(), nbPoints);
1362         }
1363
1364         aNodes[ k++ ] = aNode->GetID();
1365       }
1366     }
1367
1368     if (k != 4)
1369       continue;
1370
1371     bool isDiag13 = true;
1372     if (aChoice == 0) // use diagonal 1-3
1373     {
1374       isDiag13 = true;
1375     }
1376     else if (aChoice == 1) // use diagonal 2-4
1377     {
1378       isDiag13 = false;
1379     }
1380     else // use numerical functor
1381     {
1382       // compare two sets of possible triangles
1383       int diag = aMeshEditor->BestSplit(anElemIds[i], aCriterion);
1384       if (diag == 1) // 1-3
1385         isDiag13 = true;
1386       else if (diag == 2) // 2-4
1387         isDiag13 = false;
1388       else // error
1389         continue;
1390     }
1391
1392     if (isDiag13)
1393     {
1394       anIdList->SetId(0, anIdToVtk(aNodes[ 0 ]));
1395       anIdList->SetId(1, anIdToVtk(aNodes[ 1 ]));
1396       anIdList->SetId(2, anIdToVtk(aNodes[ 2 ]));
1397       aConnectivity->InsertNextCell(anIdList);
1398       aCellTypesArray->InsertNextValue(VTK_TRIANGLE);
1399
1400       anIdList->SetId(0, anIdToVtk(aNodes[ 2 ]));
1401       anIdList->SetId(1, anIdToVtk(aNodes[ 3 ]));
1402       anIdList->SetId(2, anIdToVtk(aNodes[ 0 ]));
1403       aConnectivity->InsertNextCell(anIdList);
1404       aCellTypesArray->InsertNextValue(VTK_TRIANGLE);
1405     }
1406     else
1407     {
1408       anIdList->SetId(0, anIdToVtk(aNodes[ 1 ]));
1409       anIdList->SetId(1, anIdToVtk(aNodes[ 2 ]));
1410       anIdList->SetId(2, anIdToVtk(aNodes[ 3 ]));
1411       aConnectivity->InsertNextCell(anIdList);
1412       aCellTypesArray->InsertNextValue(VTK_TRIANGLE);
1413
1414       anIdList->SetId(0, anIdToVtk(aNodes[ 3 ]));
1415       anIdList->SetId(1, anIdToVtk(aNodes[ 0 ]));
1416       anIdList->SetId(2, anIdToVtk(aNodes[ 1 ]));
1417       aConnectivity->InsertNextCell(anIdList);
1418       aCellTypesArray->InsertNextValue(VTK_TRIANGLE);
1419     }
1420   }
1421
1422   VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
1423   aCellLocationsArray->SetNumberOfComponents(1);
1424   aCellLocationsArray->SetNumberOfTuples(aNbCells);
1425
1426   aConnectivity->InitTraversal();
1427   for(vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell(npts, pts); idType++)
1428     aCellLocationsArray->SetValue(idType, aConnectivity->GetTraversalLocation(npts));
1429
1430   aGrid->SetPoints(aPoints);
1431   aGrid->SetCells(aCellTypesArray, aCellLocationsArray,aConnectivity);
1432
1433   // Create and display actor
1434   vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
1435   aMapper->SetInputData(aGrid);
1436
1437   myPreviewActor = SALOME_Actor::New();
1438   myPreviewActor->PickableOff();
1439   myPreviewActor->SetMapper(aMapper);
1440
1441   vtkProperty* aProp = vtkProperty::New();
1442   aProp->SetRepresentationToWireframe();
1443   aProp->SetColor(250, 0, 250);
1444   aProp->SetLineWidth(myActor->GetLineWidth() + 1);
1445   myPreviewActor->SetProperty(aProp);
1446
1447   SMESH::GetCurrentVtkView()->AddActor(myPreviewActor);
1448   SMESH::GetCurrentVtkView()->Repaint();
1449
1450   aProp->Delete();
1451   aPoints->Delete();
1452   aConnectivity->Delete();
1453   aGrid->Delete();
1454   aMapper->Delete();
1455   anIdList->Delete();
1456   aCellTypesArray->Delete();
1457   aCellLocationsArray->Delete();
1458 }
1459
1460 /*!
1461  *  Class       : SMESHGUI_CuttingIntoTetraDlg
1462  *  Description : Modification of orientation of faces
1463  */
1464
1465 SMESHGUI_CuttingIntoTetraDlg::SMESHGUI_CuttingIntoTetraDlg(SMESHGUI* theModule)
1466   : SMESHGUI_MultiEditDlg(theModule, SMESH::VolumeFilter, false)
1467 {
1468   setWindowTitle(tr("CAPTION"));
1469   myHelpFileName = "split_to_tetra_page.html";
1470   myEntityType = 1;
1471
1472   myToAllChk->setChecked( true ); //aplly to the whole mesh by default
1473
1474   bool hasHexa = true;//myMesh->_is_nil() ? false : myMesh->NbHexas();
1475
1476   if ( hasHexa )
1477   {
1478     myGroupChoice->button(0)->setText( tr("SPLIT_HEX_TO_5_TETRA"));
1479     myGroupChoice->button(1)->setText( tr("SPLIT_HEX_TO_6_TETRA"));
1480     myGroupChoice->button(2)->setText( tr("SPLIT_HEX_TO_24_TETRA"));
1481
1482     myCriterionGrp->setTitle( tr("SPLIT_METHOD"));
1483     myCriterionGrp->show();
1484     myComboBoxFunctor->hide();
1485     myChoiceWidget->show();
1486   }
1487   setSelectionMode();
1488   updateButtons();
1489 }
1490
1491 SMESHGUI_CuttingIntoTetraDlg::~SMESHGUI_CuttingIntoTetraDlg()
1492 {
1493 }
1494
1495 bool SMESHGUI_CuttingIntoTetraDlg::process (SMESH::SMESH_MeshEditor_ptr theEditor,
1496                                             const SMESH::long_array&    theIds,
1497                                             SMESH::SMESH_IDSource_ptr   theObj)
1498 {
1499   SMESH::SMESH_IDSource_var obj = theObj;
1500   if ( CORBA::is_nil( obj ))
1501     obj = theEditor->MakeIDSource( theIds, myEntityType ? SMESH::VOLUME : SMESH::FACE );
1502   try {
1503     theEditor->SplitVolumesIntoTetra( obj, myGroupChoice->checkedId()+1 );
1504   }
1505   catch ( const SALOME::SALOME_Exception& S_ex ) {
1506     SalomeApp_Tools::QtCatchCorbaException( S_ex );
1507     return false;
1508   }
1509   return true;
1510 }