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