Salome HOME
imn/auto-reorder_of_nodes_ver2
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_AddMeshElementDlg.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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 // SMESH SMESHGUI : GUI for SMESH component
24 // File   : SMESHGUI_AddMeshElementDlg.cxx
25 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
26 //  SMESH includes
27 //
28 #include "SMESHGUI_AddMeshElementDlg.h"
29
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_GroupUtils.h"
32 #include "SMESHGUI_IdValidator.h"
33 #include "SMESHGUI_MeshUtils.h"
34 #include "SMESHGUI_SpinBox.h"
35 #include "SMESHGUI_Utils.h"
36 #include "SMESHGUI_VTKUtils.h"
37
38 #include <SMESH_Actor.h>
39 #include <SMESH_ActorUtils.h>
40 #include <SMESH_FaceOrientationFilter.h>
41 #include <SMDS_Mesh.hxx>
42
43 // SALOME GUI inclues
44 #include <SUIT_Desktop.h>
45 #include <SUIT_Session.h>
46 #include <SUIT_ResourceMgr.h>
47 #include <SUIT_MessageBox.h>
48 #include <SUIT_ViewManager.h>
49 #include <LightApp_SelectionMgr.h>
50 #include <SALOME_ListIO.hxx>
51 #include <SalomeApp_Application.h>
52 #include <SVTK_ViewModel.h>
53 #include <SVTK_ViewWindow.h>
54 #include <VTKViewer_PolyDataMapper.h>
55 #include <SVTK_Renderer.h>
56 #include <Qtx.h>
57
58
59 // IDL incldues
60 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
61
62 // OCCT includes
63 #include <TColStd_MapOfInteger.hxx>
64
65 // VTK includes
66 #include <vtkCell.h>
67 #include <vtkIdList.h>
68 #include <vtkUnstructuredGrid.h>
69 #include <vtkDataSetMapper.h>
70 #include <vtkPolyDataMapper.h>
71 #include <vtkProperty.h>
72 #include <vtkCellData.h>
73
74 // Qt includes
75 #include <QComboBox>
76 #include <QGroupBox>
77 #include <QLabel>
78 #include <QLineEdit>
79 #include <QPushButton>
80 #include <QRadioButton>
81 #include <QHBoxLayout>
82 #include <QVBoxLayout>
83 #include <QGridLayout>
84 #include <QVariant>
85 #include <QCheckBox>
86 #include <QKeyEvent>
87 #include <QButtonGroup>
88
89 #define SPACING 6
90 #define MARGIN  11
91
92 namespace SMESH
93 {
94   class TElementSimulation
95   {
96     SalomeApp_Application* myApplication;
97     SUIT_ViewWindow* myViewWindow;
98     SVTK_ViewWindow* myVTKViewWindow;
99
100     SALOME_Actor* myPreviewActor;
101     vtkDataSetMapper* myMapper;
102     vtkUnstructuredGrid* myGrid;
103     
104     SALOME_Actor* myBallActor;
105     VTKViewer_PolyDataMapper* myBallMapper;
106     vtkPolyData* myBallPolyData; 
107
108     SALOME_Actor* myFaceOrientation;
109     vtkPolyDataMapper* myFaceOrientationDataMapper;
110     SMESH_FaceOrientationFilter* myFaceOrientationFilter;
111
112   public:
113     TElementSimulation (SalomeApp_Application* theApplication)
114     {
115       myApplication = theApplication;
116       SUIT_ViewManager* mgr = theApplication->activeViewManager();
117       if (!mgr) return;
118       myViewWindow = mgr->getActiveView();
119       myVTKViewWindow = GetVtkViewWindow(myViewWindow);
120
121       myGrid = vtkUnstructuredGrid::New();
122
123       // Create and display actor
124       myMapper = vtkDataSetMapper::New();
125       myMapper->SetInputData(myGrid);
126
127       myPreviewActor = SALOME_Actor::New();
128       myPreviewActor->PickableOff();
129       myPreviewActor->VisibilityOff();
130       myPreviewActor->SetMapper(myMapper);
131
132       QColor ffc, bfc;
133       int delta;
134
135       vtkProperty* aProp = vtkProperty::New();
136       SMESH::GetColor( "SMESH", "preview_color", ffc, delta, "0, 255, 0|-100" ) ;
137       aProp->SetColor( ffc.red() / 255. , ffc.green() / 255. , ffc.blue() / 255. );
138       myPreviewActor->SetProperty( aProp );
139       aProp->Delete();
140
141       vtkProperty* aBackProp = vtkProperty::New();
142       bfc = Qtx::mainColorToSecondary(ffc, delta);
143       aBackProp->SetColor( bfc.red() / 255. , bfc.green() / 255. , bfc.blue() / 255. );
144       myPreviewActor->SetBackfaceProperty( aBackProp );
145       aBackProp->Delete();
146
147       myVTKViewWindow->AddActor(myPreviewActor);
148
149       // Orientation of faces
150       myFaceOrientationFilter = SMESH_FaceOrientationFilter::New();
151       myFaceOrientationFilter->SetInputData(myGrid);
152
153       myFaceOrientationDataMapper = vtkPolyDataMapper::New();
154       myFaceOrientationDataMapper->SetInputConnection(myFaceOrientationFilter->GetOutputPort());
155
156       myFaceOrientation = SALOME_Actor::New();
157       myFaceOrientation->PickableOff();
158       myFaceOrientation->VisibilityOff();
159       myFaceOrientation->SetMapper(myFaceOrientationDataMapper);
160
161       vtkProperty* anOrientationProp = vtkProperty::New();
162       double anRGB[3];
163       GetColor( "SMESH", "orientation_color", anRGB[0], anRGB[1], anRGB[2], QColor( 255, 255, 255 ) );
164       anOrientationProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
165       myFaceOrientation->SetProperty( anOrientationProp );
166       anOrientationProp->Delete();
167       myVTKViewWindow->AddActor(myFaceOrientation);
168
169       // Preview for the balls
170       vtkProperty* aBallProp = vtkProperty::New();
171       aBallProp->SetColor(ffc.red() / 255. , ffc.green() / 255. , ffc.blue() / 255.);
172       double aBallElemSize = SMESH::GetFloat("SMESH:ball_elem_size",10);
173       aBallProp->SetPointSize(aBallElemSize);
174
175       myBallPolyData = vtkPolyData::New();
176       myBallPolyData->Allocate();
177
178       myBallMapper = VTKViewer_PolyDataMapper::New();
179       myBallMapper->SetInputData(myBallPolyData);
180       myBallMapper->SetBallEnabled(true);
181
182       myBallActor = SALOME_Actor::New();
183       myBallActor->PickableOff();
184       myBallActor->SetVisibility(false);
185       myBallActor->SetProperty(aBallProp);
186       myBallActor->SetMapper(myBallMapper);
187       aBallProp->Delete();
188       
189       myVTKViewWindow->AddActor(myBallActor);
190     }
191
192     typedef std::vector<vtkIdType> TVTKIds;
193     void SetPosition (SMESH_Actor* theActor,
194                       vtkIdType    theType,
195                       TVTKIds&     theIds)
196     {
197       vtkUnstructuredGrid *aGrid = theActor->GetUnstructuredGrid();
198       myGrid->SetPoints(aGrid->GetPoints());
199       myGrid->Reset();      
200
201       const std::vector<int>& interlace = SMDS_MeshCell::toVtkOrder( VTKCellType( theType ));
202       SMDS_MeshCell::applyInterlace( interlace, theIds );
203
204       vtkIdList *anIds = vtkIdList::New();
205       for (int i = 0, iEnd = theIds.size(); i < iEnd; i++)
206         anIds->InsertId(i,theIds[i]);
207
208       myGrid->InsertNextCell(theType,anIds);
209       anIds->Delete();
210
211       myGrid->Modified();
212
213       SetVisibility(true, theActor->GetFacesOriented(), false);
214     }
215
216     void SetBallPosition(SMESH_Actor* theActor,TVTKIds& theIds, double theDiameter) {
217       vtkUnstructuredGrid *aGrid = theActor->GetUnstructuredGrid();
218       myBallPolyData->Reset();
219       myBallPolyData->DeleteCells();
220       myBallPolyData->SetPoints(aGrid->GetPoints());
221       
222       vtkDataArray* aScalars = vtkDataArray::CreateDataArray(VTK_DOUBLE);
223       aScalars->SetNumberOfComponents(1);
224       aScalars->SetNumberOfTuples(theIds.size());
225       myBallPolyData->GetCellData()->SetScalars(aScalars);
226       aScalars->Delete();
227
228       vtkIdList *anIds = vtkIdList::New();
229       anIds->SetNumberOfIds(1);
230       for (int i = 0, iEnd = theIds.size(); i < iEnd; i++){
231         anIds->InsertId(0,theIds[i]);
232         vtkIdType anId = myBallPolyData->InsertNextCell(VTK_POLY_VERTEX,anIds);
233         double d = theDiameter * theActor->GetBallScale();
234         aScalars->SetTuple(anId,&d);
235         anIds->Reset();
236       }
237       
238       anIds->Delete();
239       myBallPolyData->Modified();
240       SetVisibility (false, false, true);
241     }
242
243     void SetVisibility (bool theVisibility, bool theShowOrientation = false, bool theShowBalls = false)
244     {
245       myPreviewActor->SetVisibility(theVisibility);
246       myFaceOrientation->SetVisibility(theShowOrientation);
247       myBallActor->SetVisibility(theShowBalls);
248       RepaintCurrentView();
249     }
250
251
252     ~TElementSimulation()
253     {
254       myMapper->RemoveAllInputs();
255       myFaceOrientationDataMapper->RemoveAllInputs();
256       myBallMapper->RemoveAllInputs();
257
258       if (FindVtkViewWindow(myApplication->activeViewManager(), myViewWindow)) {
259         myVTKViewWindow->RemoveActor(myPreviewActor,false,false);
260         myVTKViewWindow->RemoveActor(myFaceOrientation,false,false);
261         myVTKViewWindow->RemoveActor(myBallActor,false,false);
262       }
263
264       myMapper->Delete();
265       myGrid->Delete();
266       myPreviewActor->Delete();
267             
268       myFaceOrientationFilter->Delete();      
269       myFaceOrientationDataMapper->Delete();
270       myFaceOrientation->Delete();
271
272       myBallMapper->Delete();
273       myBallPolyData->Delete();
274       myBallActor->Delete();
275     }
276   };
277 }
278
279 //=================================================================================
280 // function : SMESHGUI_AddMeshElementDlg()
281 // purpose  : constructor
282 //=================================================================================
283 SMESHGUI_AddMeshElementDlg::SMESHGUI_AddMeshElementDlg( SMESHGUI*          theModule,
284                                                         SMDSAbs_EntityType ElementType)
285   : QDialog( SMESH::GetDesktop( theModule ) ),
286     mySMESHGUI( theModule ),
287     mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
288     myBusy ( false )
289 {
290   setModal( false );
291   setAttribute( Qt::WA_DeleteOnClose, true );
292
293   SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>
294     (SUIT_Session::session()->activeApplication());
295   myIsPoly = false;
296   mySimulation = new SMESH::TElementSimulation (anApp);
297   mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
298   myGeomType = ElementType;
299   myElementType = SMDSAbs_Volume;
300
301   // verify nb nodes and type
302   QString elemName;
303   switch ( myGeomType ) {
304   case SMDSEntity_0D:
305     myNbNodes = 1;
306     myElementType = SMDSAbs_0DElement;
307     elemName = "ELEM0D";
308     myHelpFileName = "adding_nodes_and_elements_page.html#adding_0delems_anchor";
309     break;
310   case SMDSEntity_Ball:
311     myNbNodes = 1;
312     myElementType = SMDSAbs_Ball;
313     elemName = "BALL";
314     myHelpFileName = "adding_nodes_and_elements_page.html#adding_ball_anchor";
315     break;
316   case SMDSEntity_Edge:
317     myNbNodes = 2;
318     myElementType = SMDSAbs_Edge;
319     elemName = "EDGE";
320     myHelpFileName = "adding_nodes_and_elements_page.html#adding_edges_anchor";
321     break;
322   case SMDSEntity_Triangle:
323     myNbNodes = 3;
324     elemName = "TRIANGLE";
325     myElementType = SMDSAbs_Face;
326     myHelpFileName = "adding_nodes_and_elements_page.html#adding_triangles_anchor";
327     break;
328   case SMDSEntity_Quadrangle:
329     myNbNodes = 4;
330     myElementType = SMDSAbs_Face;
331     elemName = "QUADRANGLE";
332     myHelpFileName = "adding_nodes_and_elements_page.html#adding_quadrangles_anchor";
333     break;
334   case SMDSEntity_Polygon:
335     myNbNodes = 0;
336     myElementType = SMDSAbs_Face;
337     elemName = "POLYGON";
338     myIsPoly = true;
339     myHelpFileName = "adding_nodes_and_elements_page.html#adding_polygons_anchor";
340     break;
341   case SMDSEntity_Tetra:
342     myNbNodes = 4;
343     elemName = "TETRAS";
344     myHelpFileName = "adding_nodes_and_elements_page.html#adding_tetrahedrons_anchor";
345     break;
346   case SMDSEntity_Pyramid:
347     myNbNodes = 5;
348     elemName = "PYRAMID";
349     myHelpFileName = "adding_nodes_and_elements_page.html#adding_pyramids_anchor";
350     break;
351   case SMDSEntity_Hexa:
352     myNbNodes = 8;
353     elemName = "HEXAS";
354     myHelpFileName = "adding_nodes_and_elements_page.html#adding_hexahedrons_anchor";
355     break;
356   case SMDSEntity_Penta:
357     myNbNodes = 6;
358     elemName = "PENTA";
359     myHelpFileName = "adding_nodes_and_elements_page.html#adding_pentahedrons_anchor";
360     break;
361   case SMDSEntity_Hexagonal_Prism:
362     myNbNodes = 12;
363     elemName = "OCTA";
364     myHelpFileName = "adding_nodes_and_elements_page.html#adding_octahedrons_anchor";
365     break;
366   default:
367     myNbNodes = 2;
368     elemName = "EDGE";
369     myHelpFileName = "adding_nodes_and_elements_page.html#adding_edges_anchor";
370   }
371
372   QString iconName      = tr(QString("ICON_DLG_%1").arg(elemName).toLatin1().data());
373   QString buttonGrTitle = tr(QString("SMESH_%1").arg(elemName).toLatin1().data());
374   QString caption       = tr(QString("SMESH_ADD_%1_TITLE").arg(elemName).toLatin1().data());
375   QString grBoxTitle    = tr(QString("SMESH_ADD_%1").arg(elemName).toLatin1().data());
376
377   QPixmap image0 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", iconName));
378   QPixmap image1 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
379
380   setWindowTitle(caption);
381   setSizeGripEnabled(true);
382
383   QVBoxLayout* aTopLayout = new QVBoxLayout(this);
384   aTopLayout->setSpacing(SPACING);
385   aTopLayout->setMargin(MARGIN);
386
387   /* Constructor *************************************************/
388   GroupConstructors = new QGroupBox(buttonGrTitle, this);
389   QButtonGroup* ButtonGroup = new QButtonGroup(this);
390   QHBoxLayout* GroupConstructorsLayout = new QHBoxLayout(GroupConstructors);
391   GroupConstructorsLayout->setSpacing(SPACING);
392   GroupConstructorsLayout->setMargin(MARGIN);
393
394   Constructor1 = new QRadioButton(GroupConstructors);
395   Constructor1->setIcon(image0);
396   Constructor1->setChecked(true);
397
398   GroupConstructorsLayout->addWidget(Constructor1);
399   ButtonGroup->addButton( Constructor1, 0 );
400
401   /* Nodes & Reverse *********************************************/
402   GroupC1 = new QGroupBox(grBoxTitle, this);
403   QGridLayout* GroupC1Layout = new QGridLayout(GroupC1);
404   GroupC1Layout->setSpacing(SPACING);
405   GroupC1Layout->setMargin(MARGIN);
406
407   TextLabelC1A1 = new QLabel(tr("SMESH_ID_NODES"), GroupC1);
408   SelectButtonC1A1 = new QPushButton(GroupC1);
409   SelectButtonC1A1->setIcon(image1);
410   LineEditC1A1 = new QLineEdit(GroupC1);
411   LineEditC1A1->setValidator
412     (new SMESHGUI_IdValidator(this, ( myIsPoly || myNbNodes == 1 ) ? 1000 : myNbNodes));
413
414   Reverse = (myElementType == SMDSAbs_Face || myElementType == SMDSAbs_Volume ) ? new QCheckBox(tr("SMESH_REVERSE"), GroupC1) : 0;
415
416   AutomaticPresentation = (myGeomType == SMDSEntity_Quadrangle || myGeomType == SMDSEntity_Polygon ||
417                            myGeomType == SMDSEntity_Pyramid    || myGeomType == SMDSEntity_Hexa    ||
418                            myGeomType == SMDSEntity_Penta      || myGeomType == SMDSEntity_Hexagonal_Prism ) ? new QCheckBox(tr("SMESH_AUTOMATIC_PRESENTATION"), GroupC1) : 0;
419   if ( AutomaticPresentation ) {
420     GetNextPresentationButton = new QPushButton(tr("SMESH_BUT_GET_NEXT_SHAPE"), GroupC1);
421     GetNextPresentationButton->setAutoDefault(false);
422   }
423   DiameterSpinBox = ( myGeomType == SMDSEntity_Ball ) ? new SMESHGUI_SpinBox(GroupC1) : 0;
424   QLabel* diameterLabel = DiameterSpinBox ? new QLabel( tr("BALL_DIAMETER"),GroupC1) : 0;
425
426   GroupC1Layout->addWidget(TextLabelC1A1,    0, 0);
427   GroupC1Layout->addWidget(SelectButtonC1A1, 0, 1);
428   GroupC1Layout->addWidget(LineEditC1A1,     0, 2);
429   if ( AutomaticPresentation ) {
430     AutomaticPresentation->setChecked(true);
431     GroupC1Layout->addWidget(AutomaticPresentation, 1, 0, 1, 2);
432     GroupC1Layout->addWidget(GetNextPresentationButton, 1, 2, 1, 1);
433   }
434   if ( Reverse ) {
435     GroupC1Layout->addWidget(Reverse, 2, 0, 1, 3);
436   }
437   if ( DiameterSpinBox ) {
438     GroupC1Layout->addWidget(diameterLabel,   1, 0);
439     GroupC1Layout->addWidget(DiameterSpinBox, 1, 1, 1, 2);
440
441     DiameterSpinBox->RangeStepAndValidator( 1e-7, 1e+9, 0.1 );
442     DiameterSpinBox->SetValue( 1. );
443     connect( DiameterSpinBox, SIGNAL( valueChanged ( double ) ), this, SLOT( onDiameterChanged( ) ) );
444   }
445   /* Add to group ************************************************/
446   GroupGroups = new QGroupBox( tr( "SMESH_ADD_TO_GROUP" ), this );
447   GroupGroups->setCheckable( true );
448   QHBoxLayout* GroupGroupsLayout = new QHBoxLayout(GroupGroups);
449   GroupGroupsLayout->setSpacing(SPACING);
450   GroupGroupsLayout->setMargin(MARGIN);
451
452   TextLabel_GroupName = new QLabel( tr( "SMESH_GROUP" ), GroupGroups );
453   ComboBox_GroupName = new QComboBox( GroupGroups );
454   ComboBox_GroupName->setEditable( true );
455   ComboBox_GroupName->setInsertPolicy( QComboBox::NoInsert );
456
457   GroupGroupsLayout->addWidget( TextLabel_GroupName );
458   GroupGroupsLayout->addWidget( ComboBox_GroupName, 1 );
459
460   /* Apply etc ***************************************************/
461   GroupButtons = new QGroupBox(this);
462   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
463   GroupButtonsLayout->setSpacing(SPACING);
464   GroupButtonsLayout->setMargin(MARGIN);
465
466   buttonOk = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), GroupButtons);
467   buttonOk->setAutoDefault(true);
468   buttonOk->setDefault(true);
469   buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
470   buttonApply->setAutoDefault(true);
471   buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
472   buttonCancel->setAutoDefault(true);
473   buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
474   buttonHelp->setAutoDefault(true);
475
476   GroupButtonsLayout->addWidget(buttonOk);
477   GroupButtonsLayout->addSpacing(10);
478   GroupButtonsLayout->addWidget(buttonApply);
479   GroupButtonsLayout->addSpacing(10);
480   GroupButtonsLayout->addStretch();
481   GroupButtonsLayout->addWidget(buttonCancel);
482   GroupButtonsLayout->addWidget(buttonHelp);
483
484   /***************************************************************/
485   aTopLayout->addWidget(GroupConstructors);
486   aTopLayout->addWidget(GroupC1);
487   aTopLayout->addWidget(GroupGroups);
488   aTopLayout->addWidget(GroupButtons);
489
490   Init(); /* Initialisations */
491 }
492
493 //=================================================================================
494 // function : ~SMESHGUI_AddMeshElementDlg()
495 // purpose  : Destroys the object and frees any allocated resources
496 //=================================================================================
497 SMESHGUI_AddMeshElementDlg::~SMESHGUI_AddMeshElementDlg()
498 {
499   delete mySimulation;
500 }
501
502 //=================================================================================
503 // function : Init()
504 // purpose  :
505 //=================================================================================
506 void SMESHGUI_AddMeshElementDlg::Init()
507 {
508   GroupC1->show();
509   Constructor1->setChecked(true);
510   myEditCurrentArgument = LineEditC1A1;
511   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
512
513   /* reset "Add to group" control */
514   GroupGroups->setChecked( false );
515   //GroupGroups->setVisible( myElementType != SMDSAbs_0DElement );
516
517   myNbOkNodes = 0;
518   myActor = 0;
519
520   /* signals and slots connections */
521   connect(buttonOk,                SIGNAL(clicked()),                     SLOT(ClickOnOk()));
522   connect(buttonCancel,            SIGNAL(clicked()),                     SLOT(reject()));
523   connect(buttonApply,             SIGNAL(clicked()),                     SLOT(ClickOnApply()));
524   connect(buttonHelp,              SIGNAL(clicked()),                     SLOT(ClickOnHelp()));
525
526   connect(SelectButtonC1A1,        SIGNAL(clicked()),                     SLOT(SetEditCurrentArgument()));
527   connect(LineEditC1A1,            SIGNAL(textChanged(const QString&)),   SLOT(onTextChange(const QString&)));
528   connect(mySMESHGUI,              SIGNAL(SignalDeactivateActiveDialog()),SLOT(DeactivateActiveDialog()));
529   connect(mySelectionMgr,          SIGNAL(currentSelectionChanged()),     SLOT(SelectionIntoArgument()));
530   /* to close dialog if study frame change */
531   connect(mySMESHGUI,              SIGNAL(SignalStudyFrameChanged()),     SLOT(reject()));
532   connect(mySMESHGUI,              SIGNAL(SignalCloseAllDialogs()),       SLOT(reject()));
533
534   if (Reverse)
535     connect(Reverse,               SIGNAL(stateChanged(int)),             SLOT(CheckBox(int)));
536   if (AutomaticPresentation) {
537     connect(AutomaticPresentation, SIGNAL(stateChanged(int)),             SLOT(SelectionIntoArgument()));
538     connect(GetNextPresentationButton, SIGNAL(clicked()),                 SLOT(GetNextShapePresentation()));
539   }
540   // set selection mode
541   SMESH::SetPointRepresentation(true);
542
543   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
544     aViewWindow->SetSelectionMode( NodeSelection );
545
546   myBusy = false;
547
548   SelectionIntoArgument();
549 }
550
551 //=================================================================================
552 // function : ClickOnApply()
553 // purpose  :
554 //=================================================================================
555 void SMESHGUI_AddMeshElementDlg::ClickOnApply()
556 {
557   if( !isValid() )
558     return;
559
560   if (myNbOkNodes && !mySMESHGUI->isActiveStudyLocked()) {
561     myBusy = true;
562     QStringList aListId = myEditCurrentArgument->text().split(" ", QString::SkipEmptyParts);
563     SMESH::long_array_var anArrayOfIndices = new SMESH::long_array;
564     anArrayOfIndices->length(aListId.count());
565     const std::vector<int>& revIndex = SMDS_MeshCell::reverseSmdsOrder( myGeomType );
566     if ( Reverse && Reverse->isChecked() && !revIndex.empty() )
567       for (int i = 0; i < aListId.count(); i++)
568         anArrayOfIndices[i] = aListId[ revIndex[i] ].toInt();
569     else if ( Reverse && Reverse->isChecked() && revIndex.empty() ) // polygon
570       for (int i = 0; i < aListId.count(); i++)
571         anArrayOfIndices[i] = aListId[ aListId.count()-1 - i ].toInt();
572     else
573       for (int i = 0; i < aListId.count(); i++)
574         anArrayOfIndices[i] = aListId[ i ].toInt();
575
576     bool addToGroup = GroupGroups->isChecked();
577     QString aGroupName;
578
579     SMESH::SMESH_GroupBase_var aGroup;
580     int idx = 0;
581     if( addToGroup ) {
582       aGroupName = ComboBox_GroupName->currentText();
583       for ( int i = 1; i <= ComboBox_GroupName->count(); i++ ) {
584         QString aName = ComboBox_GroupName->itemText( i );
585         if ( aGroupName == aName && ( i == ComboBox_GroupName->currentIndex() || idx == 0 ) )
586           idx = i;
587       }
588       if ( idx > 0 && idx <= myGroups.count() ) {
589         SMESH::SMESH_GroupOnGeom_var aGeomGroup = SMESH::SMESH_GroupOnGeom::_narrow( myGroups[idx-1] );
590         if ( !aGeomGroup->_is_nil() ) {
591           int res = SUIT_MessageBox::question( this, tr( "SMESH_WRN_WARNING" ),
592                                                tr( "MESH_STANDALONE_GRP_CHOSEN" ).arg( aGroupName ),
593                                                tr( "SMESH_BUT_YES" ), tr( "SMESH_BUT_NO" ), 0, 1 );
594           if ( res == 1 ) return;
595         }
596         aGroup = myGroups[idx-1];
597       }
598     }
599
600     SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
601     SMESH::long_array_var anIdList = new SMESH::long_array;
602     anIdList->length( 1 );
603     anIdList[0] = -1;
604
605     switch (myElementType) {
606     case SMDSAbs_0DElement:
607       anIdList->length( anArrayOfIndices->length() );
608       for ( size_t i = 0; i < anArrayOfIndices->length(); ++i )
609         anIdList[i] = aMeshEditor->Add0DElement(anArrayOfIndices[i]);
610       break;
611     case SMDSAbs_Ball:
612       if ( myGeomType == SMDSEntity_Ball ) {
613         anIdList->length( anArrayOfIndices->length() );
614         for ( size_t i = 0; i < anArrayOfIndices->length(); ++i )
615           anIdList[i] = aMeshEditor->AddBall(anArrayOfIndices[i],
616                                              DiameterSpinBox->GetValue());
617       }
618       break;
619     case SMDSAbs_Edge:
620       anIdList[0] = aMeshEditor->AddEdge(anArrayOfIndices.inout()); break;
621     case SMDSAbs_Face:
622       if ( myIsPoly )
623         anIdList[0] = aMeshEditor->AddPolygonalFace(anArrayOfIndices.inout());
624       else
625         anIdList[0] = aMeshEditor->AddFace(anArrayOfIndices.inout());
626       break;
627     default:
628       anIdList[0] = aMeshEditor->AddVolume(anArrayOfIndices.inout()); break;
629     }
630
631     if ( anIdList[0] > 0 && addToGroup && !aGroupName.isEmpty() ) {
632       SMESH::SMESH_Group_var aGroupUsed;
633       if ( aGroup->_is_nil() ) {
634         // create new group 
635         aGroupUsed = SMESH::AddGroup( myMesh, (SMESH::ElementType)myElementType, aGroupName );
636         if ( !aGroupUsed->_is_nil() ) {
637           myGroups.append(SMESH::SMESH_GroupBase::_duplicate(aGroupUsed));
638           ComboBox_GroupName->addItem( aGroupName );
639         }
640       }
641       else {
642         SMESH::SMESH_GroupOnGeom_var aGeomGroup = SMESH::SMESH_GroupOnGeom::_narrow( aGroup );
643         if ( !aGeomGroup->_is_nil() ) {
644           aGroupUsed = myMesh->ConvertToStandalone( aGeomGroup );
645           if ( !aGroupUsed->_is_nil() && idx > 0 ) {
646             myGroups[idx-1] = SMESH::SMESH_GroupBase::_duplicate(aGroupUsed);
647             SMESHGUI::GetSMESHGUI()->getApp()->updateObjectBrowser();
648           }
649         }
650         else
651           aGroupUsed = SMESH::SMESH_Group::_narrow( aGroup );
652       }
653
654       if ( !aGroupUsed->_is_nil() )
655         aGroupUsed->Add( anIdList.inout() );
656     }
657
658     SALOME_ListIO aList; aList.Append( myActor->getIO() );
659     mySelector->ClearIndex();
660     mySelectionMgr->setSelectedObjects( aList, false );
661
662     mySimulation->SetVisibility(false);
663     SMESH::UpdateView();
664     
665     buttonOk->setEnabled(false);
666     buttonApply->setEnabled(false);
667     if ( AutomaticPresentation )
668       GetNextPresentationButton->setEnabled(false);
669
670     myEditCurrentArgument->setText("");
671
672     myBusy = false;
673
674     SMESHGUI::Modified();
675   }
676 }
677
678 //=================================================================================
679 // function : ClickOnOk()
680 // purpose  :
681 //=================================================================================
682 void SMESHGUI_AddMeshElementDlg::ClickOnOk()
683 {
684   ClickOnApply();
685   reject();
686 }
687
688 //=================================================================================
689 // function : reject()
690 // purpose  :
691 //=================================================================================
692 void SMESHGUI_AddMeshElementDlg::reject()
693 {
694   mySimulation->SetVisibility(false);
695   SMESH::SetPointRepresentation(false);
696   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
697     aViewWindow->SetSelectionMode( ActorSelection );
698   disconnect(mySelectionMgr, 0, this, 0);
699   mySMESHGUI->ResetState();
700   QDialog::reject();
701 }
702
703 //=================================================================================
704 // function : ClickOnHelp()
705 // purpose  :
706 //=================================================================================
707 void SMESHGUI_AddMeshElementDlg::ClickOnHelp()
708 {
709   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
710   if (app)
711     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""),
712                              myHelpFileName);
713   else {
714     QString platform;
715 #ifdef WIN32
716     platform = "winapplication";
717 #else
718     platform = "application";
719 #endif
720     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
721                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
722                              arg(app->resourceMgr()->stringValue("ExternalBrowser",
723                                                                  platform)).
724                              arg(myHelpFileName));
725   }
726 }
727
728 //=================================================================================
729 // function : onTextChange()
730 // purpose  :
731 //=================================================================================
732 void SMESHGUI_AddMeshElementDlg::onTextChange (const QString& theNewText)
733 {
734   if (myBusy) return;
735   myBusy = true;
736
737   myNbOkNodes = 0;
738
739   buttonOk->setEnabled(false);
740   buttonApply->setEnabled(false);
741   if ( AutomaticPresentation )
742     GetNextPresentationButton->setEnabled(false);
743
744   mySimulation->SetVisibility(false);
745
746   // hilight entered nodes
747   SMDS_Mesh* aMesh = 0;
748   if (myActor)
749     aMesh = myActor->GetObject()->GetMesh();
750
751   if (aMesh) {
752     TColStd_MapOfInteger newIndices;
753
754     QStringList aListId = theNewText.split(" ", QString::SkipEmptyParts);
755     bool allOk = true;
756     for (int i = 0; i < aListId.count(); i++) {
757       if( const SMDS_MeshNode * n = aMesh->FindNode( aListId[ i ].toInt() ) )
758         {
759           newIndices.Add( n->GetID() );
760           myNbOkNodes++;
761         }
762       else
763         allOk = false;  
764     }
765
766     mySelector->AddOrRemoveIndex( myActor->getIO(), newIndices, false );
767     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
768       aViewWindow->highlight( myActor->getIO(), true, true );
769
770     myNbOkNodes = ( allOk && ( myNbNodes == aListId.count() || myNbNodes == 1 ));
771
772     if (myIsPoly)
773       {
774         if ( !allOk || myElementType != SMDSAbs_Face || aListId.count() < 3 )
775           myNbOkNodes = 0;
776         else
777           myNbOkNodes = aListId.count();
778       }
779   }
780
781   if(myNbOkNodes) {
782     buttonOk->setEnabled(true);
783     buttonApply->setEnabled(true);
784     if ( AutomaticPresentation && AutomaticPresentation->isChecked() )
785       GetNextPresentationButton->setEnabled(true);
786     displaySimulation();
787   }
788
789   myBusy = false;
790 }
791
792 //=================================================================================
793 // function : SelectionIntoArgument()
794 // purpose  : Called when selection has changed
795 //=================================================================================
796 void SMESHGUI_AddMeshElementDlg::SelectionIntoArgument()
797 {
798   if (myBusy) return;
799
800   // clear
801   myNbOkNodes = 0;
802   myActor = 0;
803
804   myBusy = true;
805   QString anOldEditArgument = myEditCurrentArgument->text();
806   myEditCurrentArgument->setText("");
807   myBusy = false;
808
809   if (!GroupButtons->isEnabled()) // inactive
810     return;
811
812   buttonOk->setEnabled(false);
813   buttonApply->setEnabled(false);
814   if ( AutomaticPresentation )
815     GetNextPresentationButton->setEnabled(false);
816
817   mySimulation->SetVisibility(false);
818   //  SMESH::SetPointRepresentation(true);
819
820   QString aCurrentEntry = myEntry;
821
822   // get selected mesh
823   SALOME_ListIO aList;
824   mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
825
826   if (aList.Extent() != 1)
827     return;
828
829   Handle(SALOME_InteractiveObject) anIO = aList.First();
830   myEntry = anIO->getEntry();
831   myMesh = SMESH::GetMeshByIO(anIO);
832   if (myMesh->_is_nil())
833     return;
834
835   // process groups
836   if ( !myMesh->_is_nil() && myEntry != aCurrentEntry ) {
837     myGroups.clear();
838     ComboBox_GroupName->clear();
839     ComboBox_GroupName->addItem( QString() );
840     SMESH::ListOfGroups aListOfGroups = *myMesh->GetGroups();
841     for ( int i = 0, n = aListOfGroups.length(); i < n; i++ ) {
842       SMESH::SMESH_GroupBase_var aGroup = aListOfGroups[i];
843       if ( !aGroup->_is_nil() && aGroup->GetType() == (SMESH::ElementType)myElementType ) {
844         QString aGroupName( aGroup->GetName() );
845         if ( !aGroupName.isEmpty() ) {
846           myGroups.append(SMESH::SMESH_GroupBase::_duplicate(aGroup));
847           ComboBox_GroupName->addItem( aGroupName );
848         }
849       }
850     }
851   }
852
853   myActor = SMESH::FindActorByEntry(anIO->getEntry());
854   if (!myActor)
855     return;
856
857   // get selected nodes
858   QString aString = "";
859   int nbNodes = 0;
860   while ( aString == "" || anOldEditArgument == aString ) {
861     if ( AutomaticPresentation && AutomaticPresentation->isChecked() ) {
862       nbNodes = SMESH::GetNameOfSelectedSortedNodes( myGeomType , mySelector, myActor, myShift, aString );
863     }
864     else
865       nbNodes = SMESH::GetNameOfSelectedNodes( mySelector, myActor->getIO(), aString );
866     if ( aString!= "" && myNbNodes == nbNodes && anOldEditArgument == aString && AutomaticPresentation && AutomaticPresentation->isChecked()) {
867       myShift++;
868       if ( myShift > nbNodes ) {
869         myEditCurrentArgument->setText(aString);
870         myShift = 0;
871         break;
872       }
873       continue;
874     }
875     myBusy = true;
876     myEditCurrentArgument->setText(aString);
877     myBusy = false;
878     if (myIsPoly && myElementType == SMDSAbs_Face && nbNodes >= 3 )
879       myNbNodes = nbNodes;
880     else if (myNbNodes != nbNodes && myNbNodes != 1) {
881       myShift = 0;
882       return;
883     }
884     if ( !AutomaticPresentation || !AutomaticPresentation->isChecked() ||
885        ( myIsPoly && nbNodes < 3 ) )
886       break;
887   }
888
889   // OK
890   myNbOkNodes = nbNodes;
891
892   buttonOk->setEnabled(true);
893   buttonApply->setEnabled(true);
894   if ( AutomaticPresentation && AutomaticPresentation->isChecked() )
895     GetNextPresentationButton->setEnabled(true);
896
897   displaySimulation();
898 }
899
900 //=================================================================================
901 // function : displaySimulation()
902 // purpose  :
903 //=================================================================================
904 void SMESHGUI_AddMeshElementDlg::displaySimulation()
905 {
906   if (myNbOkNodes && GroupButtons->isEnabled()) {
907     SMESH::TElementSimulation::TVTKIds anIds;
908     QStringList aListId = myEditCurrentArgument->text().split(" ", QString::SkipEmptyParts);
909     for (int i = 0; i < aListId.count(); i++)
910       anIds.push_back(myActor->GetObject()->GetNodeVTKId(aListId[ i ].toInt()));
911
912     if (Reverse && Reverse->isChecked())
913     {
914       const std::vector<int>& i = SMDS_MeshCell::reverseSmdsOrder( myGeomType );
915       if ( i.empty() ) // polygon
916         std::reverse( anIds.begin(), anIds.end() );
917       else
918         SMDS_MeshCell::applyInterlace( i, anIds );
919     }
920
921     vtkIdType aType = SMDS_MeshCell::toVtkType( myGeomType );
922     if(aType == VTK_POLY_VERTEX) {
923       mySimulation->SetBallPosition(myActor,anIds,DiameterSpinBox->GetValue());
924     } else {
925       mySimulation->SetPosition(myActor,aType,anIds);
926     }
927     SMESH::UpdateView();
928   }
929 }
930
931 //=================================================================================
932 // function : SetEditCurrentArgument()
933 // purpose  :
934 //=================================================================================
935 void SMESHGUI_AddMeshElementDlg::SetEditCurrentArgument()
936 {
937   QPushButton* send = (QPushButton*)sender();
938   if (send == SelectButtonC1A1) {
939     LineEditC1A1->setFocus();
940     myEditCurrentArgument = LineEditC1A1;
941   }
942   SelectionIntoArgument();
943 }
944
945 //=================================================================================
946 // function : GetNextShapePresentation()
947 // purpose  :
948 //=================================================================================
949 void SMESHGUI_AddMeshElementDlg::GetNextShapePresentation()
950 {
951   myShift++;
952   SetEditCurrentArgument();
953 }
954
955 //=================================================================================
956 // function : DeactivateActiveDialog()
957 // purpose  :
958 //=================================================================================
959 void SMESHGUI_AddMeshElementDlg::DeactivateActiveDialog()
960 {
961   if (GroupConstructors->isEnabled()) {
962     GroupConstructors->setEnabled(false);
963     GroupC1->setEnabled(false);
964     GroupButtons->setEnabled(false);
965     mySimulation->SetVisibility(false);
966     mySMESHGUI->ResetState();
967     mySMESHGUI->SetActiveDialogBox(0);
968   }
969 }
970
971 //=================================================================================
972 // function : ActivateThisDialog()
973 // purpose  :
974 //=================================================================================
975 void SMESHGUI_AddMeshElementDlg::ActivateThisDialog()
976 {
977   /* Emit a signal to deactivate the active dialog */
978   mySMESHGUI->EmitSignalDeactivateDialog();
979
980   GroupConstructors->setEnabled(true);
981   GroupC1->setEnabled(true);
982   GroupButtons->setEnabled(true);
983
984   SMESH::SetPointRepresentation(true);
985
986   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
987     aViewWindow->SetSelectionMode( NodeSelection );
988   SelectionIntoArgument();
989 }
990
991 //=================================================================================
992 // function : enterEvent()
993 // purpose  :
994 //=================================================================================
995 void SMESHGUI_AddMeshElementDlg::enterEvent (QEvent*)
996 {
997   if (GroupConstructors->isEnabled())
998     return;
999   ActivateThisDialog();
1000 }
1001
1002 //=================================================================================
1003 // function : CheckBox()
1004 // purpose  :
1005 //=================================================================================
1006 void SMESHGUI_AddMeshElementDlg::CheckBox (int state)
1007 {
1008   if (!myNbOkNodes)
1009     return;
1010
1011   if (state >= 0) {
1012     mySimulation->SetVisibility(false);
1013     displaySimulation();
1014   }
1015 }
1016
1017 //=================================================================================
1018 // function : keyPressEvent()
1019 // purpose  :
1020 //=================================================================================
1021 void SMESHGUI_AddMeshElementDlg::keyPressEvent( QKeyEvent* e )
1022 {
1023   QDialog::keyPressEvent( e );
1024   if ( e->isAccepted() )
1025     return;
1026
1027   if ( e->key() == Qt::Key_F1 ) {
1028     e->accept();
1029     ClickOnHelp();
1030   }
1031 }
1032
1033 //=================================================================================
1034 // function : isValid
1035 // purpose  :
1036 //=================================================================================
1037 void SMESHGUI_AddMeshElementDlg::onDiameterChanged(){
1038   displaySimulation();
1039 }
1040
1041 //=================================================================================
1042 // function : isValid
1043 // purpose  :
1044 //=================================================================================
1045 bool SMESHGUI_AddMeshElementDlg::isValid()
1046 {
1047   if( GroupGroups->isChecked() && ComboBox_GroupName->currentText().isEmpty() ) {
1048     SUIT_MessageBox::warning( this, tr( "SMESH_WRN_WARNING" ), tr( "GROUP_NAME_IS_EMPTY" ) );
1049     return false;
1050   }
1051   return true;
1052 }