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