Salome HOME
23080: [CEA 1497] Do not merge a middle node in quadratic with the extreme nodes...
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_AddMeshElementDlg.cxx
1 // Copyright (C) 2007-2015  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   Reverse = (myElementType == SMDSAbs_Face || myElementType == SMDSAbs_Volume ) ? new QCheckBox(tr("SMESH_REVERSE"), GroupC1) : 0;
417
418   DiameterSpinBox = ( myGeomType == SMDSEntity_Ball ) ? new SMESHGUI_SpinBox(GroupC1) : 0;
419   QLabel* diameterLabel = DiameterSpinBox ? new QLabel( tr("BALL_DIAMETER"),GroupC1) : 0;
420
421   GroupC1Layout->addWidget(TextLabelC1A1,    0, 0);
422   GroupC1Layout->addWidget(SelectButtonC1A1, 0, 1);
423   GroupC1Layout->addWidget(LineEditC1A1,     0, 2);
424   if ( Reverse ) {
425     GroupC1Layout->addWidget(Reverse, 1, 0, 1, 3);
426   }
427   if ( DiameterSpinBox ) {
428     GroupC1Layout->addWidget(diameterLabel,   1, 0);
429     GroupC1Layout->addWidget(DiameterSpinBox, 1, 1, 1, 2);
430
431     DiameterSpinBox->RangeStepAndValidator( 1e-7, 1e+9, 0.1 );
432     DiameterSpinBox->SetValue( SMESH::GetFloat("SMESH:ball_elem_diameter", 1) );
433     connect( DiameterSpinBox, SIGNAL( valueChanged ( double ) ), this, SLOT( onDiameterChanged( ) ) );
434   }
435   /* Add to group ************************************************/
436   GroupGroups = new QGroupBox( tr( "SMESH_ADD_TO_GROUP" ), this );
437   GroupGroups->setCheckable( true );
438   QHBoxLayout* GroupGroupsLayout = new QHBoxLayout(GroupGroups);
439   GroupGroupsLayout->setSpacing(SPACING);
440   GroupGroupsLayout->setMargin(MARGIN);
441
442   TextLabel_GroupName = new QLabel( tr( "SMESH_GROUP" ), GroupGroups );
443   ComboBox_GroupName = new QComboBox( GroupGroups );
444   ComboBox_GroupName->setEditable( true );
445   ComboBox_GroupName->setInsertPolicy( QComboBox::NoInsert );
446
447   GroupGroupsLayout->addWidget( TextLabel_GroupName );
448   GroupGroupsLayout->addWidget( ComboBox_GroupName, 1 );
449
450   /* Apply etc ***************************************************/
451   GroupButtons = new QGroupBox(this);
452   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
453   GroupButtonsLayout->setSpacing(SPACING);
454   GroupButtonsLayout->setMargin(MARGIN);
455
456   buttonOk = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), GroupButtons);
457   buttonOk->setAutoDefault(true);
458   buttonOk->setDefault(true);
459   buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
460   buttonApply->setAutoDefault(true);
461   buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
462   buttonCancel->setAutoDefault(true);
463   buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
464   buttonHelp->setAutoDefault(true);
465
466   GroupButtonsLayout->addWidget(buttonOk);
467   GroupButtonsLayout->addSpacing(10);
468   GroupButtonsLayout->addWidget(buttonApply);
469   GroupButtonsLayout->addSpacing(10);
470   GroupButtonsLayout->addStretch();
471   GroupButtonsLayout->addWidget(buttonCancel);
472   GroupButtonsLayout->addWidget(buttonHelp);
473
474   /***************************************************************/
475   aTopLayout->addWidget(GroupConstructors);
476   aTopLayout->addWidget(GroupC1);
477   aTopLayout->addWidget(GroupGroups);
478   aTopLayout->addWidget(GroupButtons);
479
480   Init(); /* Initialisations */
481 }
482
483 //=================================================================================
484 // function : ~SMESHGUI_AddMeshElementDlg()
485 // purpose  : Destroys the object and frees any allocated resources
486 //=================================================================================
487 SMESHGUI_AddMeshElementDlg::~SMESHGUI_AddMeshElementDlg()
488 {
489   delete mySimulation;
490 }
491
492 //=================================================================================
493 // function : Init()
494 // purpose  :
495 //=================================================================================
496 void SMESHGUI_AddMeshElementDlg::Init()
497 {
498   GroupC1->show();
499   Constructor1->setChecked(true);
500   myEditCurrentArgument = LineEditC1A1;
501   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
502
503   /* reset "Add to group" control */
504   GroupGroups->setChecked( false );
505   //GroupGroups->setVisible( myElementType != SMDSAbs_0DElement );
506
507   myNbOkNodes = 0;
508   myActor = 0;
509
510   /* signals and slots connections */
511   connect(buttonOk,        SIGNAL(clicked()),                     SLOT(ClickOnOk()));
512   connect(buttonCancel,    SIGNAL(clicked()),                     SLOT(reject()));
513   connect(buttonApply,     SIGNAL(clicked()),                     SLOT(ClickOnApply()));
514   connect(buttonHelp,      SIGNAL(clicked()),                     SLOT(ClickOnHelp()));
515
516   connect(SelectButtonC1A1,SIGNAL(clicked()),                     SLOT(SetEditCurrentArgument()));
517   connect(LineEditC1A1,    SIGNAL(textChanged(const QString&)),   SLOT(onTextChange(const QString&)));
518   connect(mySMESHGUI,      SIGNAL(SignalDeactivateActiveDialog()),SLOT(DeactivateActiveDialog()));
519
520   connect(mySelectionMgr,  SIGNAL(currentSelectionChanged()),     SLOT(SelectionIntoArgument()));
521   /* to close dialog if study frame change */
522   connect(mySMESHGUI,      SIGNAL(SignalStudyFrameChanged()),     SLOT(reject()));
523   connect(mySMESHGUI,      SIGNAL(SignalCloseAllDialogs()),       SLOT(reject()));
524   connect(mySMESHGUI,      SIGNAL(SignalActivatedViewManager()),  SLOT(onOpenView()));
525   connect(mySMESHGUI,      SIGNAL(SignalCloseView()),             SLOT(onCloseView()));
526
527   if (Reverse)
528     connect(Reverse,       SIGNAL(stateChanged(int)),             SLOT(CheckBox(int)));
529
530   // set selection mode
531   SMESH::SetPointRepresentation(true);
532
533   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
534     aViewWindow->SetSelectionMode( NodeSelection );
535
536   myBusy = false;
537
538   SelectionIntoArgument();
539 }
540
541 //=================================================================================
542 // function : ClickOnApply()
543 // purpose  :
544 //=================================================================================
545 void SMESHGUI_AddMeshElementDlg::ClickOnApply()
546 {
547   if( !isValid() )
548     return;
549
550   if (myNbOkNodes && !mySMESHGUI->isActiveStudyLocked()) {
551     myBusy = true;
552     QStringList aListId = myEditCurrentArgument->text().split(" ", QString::SkipEmptyParts);
553     SMESH::long_array_var anArrayOfIndices = new SMESH::long_array;
554     anArrayOfIndices->length(aListId.count());
555     const std::vector<int>& revIndex = SMDS_MeshCell::reverseSmdsOrder( myGeomType );
556     if ( Reverse && Reverse->isChecked() && !revIndex.empty() )
557       for (int i = 0; i < aListId.count(); i++)
558         anArrayOfIndices[i] = aListId[ revIndex[i] ].toInt();
559     else if ( Reverse && Reverse->isChecked() && revIndex.empty() ) // polygon
560       for (int i = 0; i < aListId.count(); i++)
561         anArrayOfIndices[i] = aListId[ aListId.count()-1 - i ].toInt();
562     else
563       for (int i = 0; i < aListId.count(); i++)
564         anArrayOfIndices[i] = aListId[ i ].toInt();
565
566     bool addToGroup = GroupGroups->isChecked();
567     QString aGroupName;
568
569     SMESH::SMESH_GroupBase_var aGroup;
570     int idx = 0;
571     if( addToGroup ) {
572       aGroupName = ComboBox_GroupName->currentText();
573       for ( int i = 1; i <= ComboBox_GroupName->count(); i++ ) {
574         QString aName = ComboBox_GroupName->itemText( i );
575         if ( aGroupName == aName && ( i == ComboBox_GroupName->currentIndex() || idx == 0 ) )
576           idx = i;
577       }
578       if ( idx > 0 && idx <= myGroups.count() ) {
579         SMESH::SMESH_GroupOnGeom_var aGeomGroup = SMESH::SMESH_GroupOnGeom::_narrow( myGroups[idx-1] );
580         if ( !aGeomGroup->_is_nil() ) {
581           int res = SUIT_MessageBox::question( this, tr( "SMESH_WRN_WARNING" ),
582                                                tr( "MESH_STANDALONE_GRP_CHOSEN" ).arg( aGroupName ),
583                                                tr( "SMESH_BUT_YES" ), tr( "SMESH_BUT_NO" ), 0, 1 );
584           if ( res == 1 ) return;
585         }
586         SMESH::SMESH_GroupOnFilter_var aFilterGroup = SMESH::SMESH_GroupOnFilter::_narrow( myGroups[idx-1] );
587         if ( !aFilterGroup->_is_nil() ) {
588           int res = SUIT_MessageBox::question( this, tr( "SMESH_WRN_WARNING" ),
589                                                tr( "MESH_FILTER_GRP_CHOSEN" ).arg( aGroupName ),
590                                                tr( "SMESH_BUT_YES" ), tr( "SMESH_BUT_NO" ), 0, 1 );
591           if ( res == 1 ) return;
592         }
593         aGroup = myGroups[idx-1];
594       }
595     }
596
597     SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
598     SMESH::long_array_var anIdList = new SMESH::long_array;
599     anIdList->length( 1 );
600     anIdList[0] = -1;
601     //const bool onlyNodesInMesh = ( myMesh->NbElements() == 0 );
602     int nbElemsBefore = 0;
603
604     switch (myElementType) {
605     case SMDSAbs_0DElement:
606       nbElemsBefore = myMesh->Nb0DElements();
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         nbElemsBefore = myMesh->NbBalls();
614         anIdList->length( anArrayOfIndices->length() );
615         for ( size_t i = 0; i < anArrayOfIndices->length(); ++i )
616           anIdList[i] = aMeshEditor->AddBall(anArrayOfIndices[i],
617                                              DiameterSpinBox->GetValue());
618       }
619       break;
620     case SMDSAbs_Edge:
621       nbElemsBefore = myMesh->NbEdges();
622       anIdList[0] = aMeshEditor->AddEdge(anArrayOfIndices.inout()); break;
623     case SMDSAbs_Face:
624       nbElemsBefore = myMesh->NbFaces();
625       if ( myIsPoly )
626         anIdList[0] = aMeshEditor->AddPolygonalFace(anArrayOfIndices.inout());
627       else
628         anIdList[0] = aMeshEditor->AddFace(anArrayOfIndices.inout());
629       break;
630     default:
631       nbElemsBefore = myMesh->NbVolumes();
632       anIdList[0] = aMeshEditor->AddVolume(anArrayOfIndices.inout()); break;
633     }
634
635     if ( anIdList[0] > 0 && addToGroup && !aGroupName.isEmpty() ) {
636       SMESH::SMESH_Group_var aGroupUsed;
637       if ( aGroup->_is_nil() ) {
638         // create new group
639         aGroupUsed = SMESH::AddGroup( myMesh, (SMESH::ElementType)myElementType, aGroupName );
640         if ( !aGroupUsed->_is_nil() ) {
641           myGroups.append(SMESH::SMESH_GroupBase::_duplicate(aGroupUsed));
642           ComboBox_GroupName->addItem( aGroupName );
643         }
644       }
645       else {
646         SMESH::SMESH_GroupOnGeom_var     aGeomGroup = SMESH::SMESH_GroupOnGeom::_narrow( aGroup );
647         SMESH::SMESH_GroupOnFilter_var aFilterGroup = SMESH::SMESH_GroupOnFilter::_narrow( aGroup );
648         if ( !aGeomGroup->_is_nil() ) {
649           aGroupUsed = myMesh->ConvertToStandalone( aGeomGroup );
650           if ( !aGroupUsed->_is_nil() && idx > 0 ) {
651             myGroups[idx-1] = SMESH::SMESH_GroupBase::_duplicate(aGroupUsed);
652             SMESHGUI::GetSMESHGUI()->getApp()->updateObjectBrowser();
653           }
654         }
655         else if ( !aFilterGroup->_is_nil() ) {
656           aGroupUsed = myMesh->ConvertToStandalone( aFilterGroup );
657           if ( !aGroupUsed->_is_nil() && idx > 0 ) {
658             myGroups[idx-1] = SMESH::SMESH_GroupBase::_duplicate(aGroupUsed);
659             SMESHGUI::GetSMESHGUI()->getApp()->updateObjectBrowser();
660           }
661         }
662         else
663           aGroupUsed = SMESH::SMESH_Group::_narrow( aGroup );
664       }
665
666       if ( !aGroupUsed->_is_nil() )
667         aGroupUsed->Add( anIdList.inout() );
668     }
669
670     SALOME_ListIO aList; aList.Append( myActor->getIO() );
671     mySelector->ClearIndex();
672     mySelectionMgr->setSelectedObjects( aList, false );
673
674     mySimulation->SetVisibility(false);
675     // if ( onlyNodesInMesh )
676     //   myActor->SetRepresentation( SMESH_Actor::eEdge ); // wireframe
677     if ( nbElemsBefore == 0  )
678     {
679       // 1st element of the type has been added, update actor to show this entity
680       unsigned int aMode = myActor->GetEntityMode();
681       switch ( myElementType ) {
682       case SMDSAbs_Edge:
683         myActor->SetRepresentation(SMESH_Actor::eEdge);
684         myActor->SetEntityMode( aMode |= SMESH_Actor::eEdges ); break;
685       case SMDSAbs_Face:
686         myActor->SetRepresentation(SMESH_Actor::eSurface);
687         myActor->SetEntityMode( aMode |= SMESH_Actor::eFaces ); break;
688       case SMDSAbs_Volume:
689         myActor->SetRepresentation(SMESH_Actor::eSurface);
690         myActor->SetEntityMode( aMode |= SMESH_Actor::eVolumes ); break;
691       }
692     }
693     SMESH::UpdateView();
694
695     buttonOk->setEnabled(false);
696     buttonApply->setEnabled(false);
697
698     myEditCurrentArgument->setText("");
699
700     myBusy = false;
701
702     SMESHGUI::Modified();
703   }
704 }
705
706 //=================================================================================
707 // function : ClickOnOk()
708 // purpose  :
709 //=================================================================================
710 void SMESHGUI_AddMeshElementDlg::ClickOnOk()
711 {
712   ClickOnApply();
713   reject();
714 }
715
716 //=================================================================================
717 // function : reject()
718 // purpose  :
719 //=================================================================================
720 void SMESHGUI_AddMeshElementDlg::reject()
721 {
722   mySimulation->SetVisibility(false);
723   SMESH::SetPointRepresentation(false);
724   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
725     aViewWindow->SetSelectionMode( ActorSelection );
726   disconnect(mySelectionMgr, 0, this, 0);
727   mySMESHGUI->ResetState();
728   QDialog::reject();
729 }
730
731 //=================================================================================
732 // function : ClickOnHelp()
733 // purpose  :
734 //=================================================================================
735 void SMESHGUI_AddMeshElementDlg::ClickOnHelp()
736 {
737   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
738   if (app)
739     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""),
740                              myHelpFileName);
741   else {
742     QString platform;
743 #ifdef WIN32
744     platform = "winapplication";
745 #else
746     platform = "application";
747 #endif
748     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
749                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
750                              arg(app->resourceMgr()->stringValue("ExternalBrowser",
751                                                                  platform)).
752                              arg(myHelpFileName));
753   }
754 }
755
756 //=================================================================================
757 // function : onTextChange()
758 // purpose  :
759 //=================================================================================
760 void SMESHGUI_AddMeshElementDlg::onTextChange (const QString& theNewText)
761 {
762   if (myBusy) return;
763   myBusy = true;
764
765   myNbOkNodes = 0;
766
767   buttonOk->setEnabled(false);
768   buttonApply->setEnabled(false);
769
770   mySimulation->SetVisibility(false);
771
772   // hilight entered nodes
773   SMDS_Mesh* aMesh = 0;
774   if (myActor)
775     aMesh = myActor->GetObject()->GetMesh();
776
777   if (aMesh) {
778     TColStd_MapOfInteger newIndices;
779
780     QStringList aListId = theNewText.split(" ", QString::SkipEmptyParts);
781     bool allOk = true;
782     for (int i = 0; i < aListId.count(); i++) {
783       if( const SMDS_MeshNode * n = aMesh->FindNode( aListId[ i ].toInt() ) )
784         {
785           newIndices.Add( n->GetID() );
786           myNbOkNodes++;
787         }
788       else
789         allOk = false;  
790     }
791
792     mySelector->AddOrRemoveIndex( myActor->getIO(), newIndices, false );
793     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
794       aViewWindow->highlight( myActor->getIO(), true, true );
795
796     myNbOkNodes = ( allOk && ( myNbNodes == aListId.count() || myNbNodes == 1 ));
797
798     if (myIsPoly)
799       {
800         if ( !allOk || myElementType != SMDSAbs_Face || aListId.count() < 3 )
801           myNbOkNodes = 0;
802         else
803           myNbOkNodes = aListId.count();
804       }
805   }
806
807   if(myNbOkNodes) {
808     buttonOk->setEnabled(true);
809     buttonApply->setEnabled(true);
810     displaySimulation();
811   }
812
813   myBusy = false;
814 }
815
816 //=================================================================================
817 // function : SelectionIntoArgument()
818 // purpose  : Called when selection has changed
819 //=================================================================================
820 void SMESHGUI_AddMeshElementDlg::SelectionIntoArgument()
821 {
822   if (myBusy) return;
823
824   // clear
825   myNbOkNodes = 0;
826   myActor = 0;
827
828   myBusy = true;
829   myEditCurrentArgument->setText("");
830   myBusy = false;
831
832   if (!GroupButtons->isEnabled()) // inactive
833     return;
834
835   buttonOk->setEnabled(false);
836   buttonApply->setEnabled(false);
837
838   mySimulation->SetVisibility(false);
839   //  SMESH::SetPointRepresentation(true);
840
841   QString aCurrentEntry = myEntry;
842
843   // get selected mesh
844   SALOME_ListIO aList;
845   mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
846
847   if (aList.Extent() != 1)
848     return;
849
850   Handle(SALOME_InteractiveObject) anIO = aList.First();
851   myEntry = anIO->getEntry();
852   myMesh = SMESH::GetMeshByIO(anIO);
853   if (myMesh->_is_nil())
854     return;
855
856   // process groups
857   if ( !myMesh->_is_nil() && myEntry != aCurrentEntry ) {
858     myGroups.clear();
859     ComboBox_GroupName->clear();
860     ComboBox_GroupName->addItem( QString() );
861     SMESH::ListOfGroups aListOfGroups = *myMesh->GetGroups();
862     for ( int i = 0, n = aListOfGroups.length(); i < n; i++ ) {
863       SMESH::SMESH_GroupBase_var aGroup = aListOfGroups[i];
864       if ( !aGroup->_is_nil() && aGroup->GetType() == (SMESH::ElementType)myElementType ) {
865         QString aGroupName( aGroup->GetName() );
866         if ( !aGroupName.isEmpty() ) {
867           myGroups.append(SMESH::SMESH_GroupBase::_duplicate(aGroup));
868           ComboBox_GroupName->addItem( aGroupName );
869         }
870       }
871     }
872   }
873
874   myActor = SMESH::FindActorByEntry(anIO->getEntry());
875   if (!myActor)
876     return;
877
878   // get selected nodes
879   QString aString = "";
880   int nbNodes = SMESH::GetNameOfSelectedNodes(mySelector,myActor->getIO(),aString);
881   myBusy = true;
882   myEditCurrentArgument->setText(aString);
883   myBusy = false;
884   if (myIsPoly && myElementType == SMDSAbs_Face && nbNodes >= 3 ) {
885     myNbNodes = nbNodes;
886   } else if (myNbNodes != nbNodes && myNbNodes != 1) {
887     return;
888   }
889
890   // OK
891   myNbOkNodes = nbNodes;
892
893   buttonOk->setEnabled(true);
894   buttonApply->setEnabled(true);
895
896   displaySimulation();
897 }
898
899 //=================================================================================
900 // function : displaySimulation()
901 // purpose  :
902 //=================================================================================
903 void SMESHGUI_AddMeshElementDlg::displaySimulation()
904 {
905   if (myNbOkNodes && GroupButtons->isEnabled()) {
906     SMESH::TElementSimulation::TVTKIds anIds;
907     QStringList aListId = myEditCurrentArgument->text().split(" ", QString::SkipEmptyParts);
908     for (int i = 0; i < aListId.count(); i++)
909       anIds.push_back(myActor->GetObject()->GetNodeVTKId(aListId[ i ].toInt()));
910
911     if (Reverse && Reverse->isChecked())
912     {
913       const std::vector<int>& i = SMDS_MeshCell::reverseSmdsOrder( myGeomType );
914       if ( i.empty() ) // polygon
915         std::reverse( anIds.begin(), anIds.end() );
916       else
917         SMDS_MeshCell::applyInterlace( i, anIds );
918     }
919
920     vtkIdType aType = SMDS_MeshCell::toVtkType( myGeomType );
921     if(aType == VTK_POLY_VERTEX) {
922       mySimulation->SetBallPosition(myActor,anIds,DiameterSpinBox->GetValue());
923     } else {
924       mySimulation->SetPosition(myActor,aType,anIds);
925     }
926     SMESH::UpdateView();
927   }
928 }
929
930 //=================================================================================
931 // function : SetEditCurrentArgument()
932 // purpose  :
933 //=================================================================================
934 void SMESHGUI_AddMeshElementDlg::SetEditCurrentArgument()
935 {
936   QPushButton* send = (QPushButton*)sender();
937   if (send == SelectButtonC1A1) {
938     LineEditC1A1->setFocus();
939     myEditCurrentArgument = LineEditC1A1;
940   }
941   SelectionIntoArgument();
942 }
943
944 //=================================================================================
945 // function : DeactivateActiveDialog()
946 // purpose  :
947 //=================================================================================
948 void SMESHGUI_AddMeshElementDlg::DeactivateActiveDialog()
949 {
950   if (GroupConstructors->isEnabled()) {
951     GroupConstructors->setEnabled(false);
952     GroupC1->setEnabled(false);
953     GroupButtons->setEnabled(false);
954     mySimulation->SetVisibility(false);
955     mySMESHGUI->ResetState();
956     mySMESHGUI->SetActiveDialogBox(0);
957   }
958 }
959
960 //=================================================================================
961 // function : ActivateThisDialog()
962 // purpose  :
963 //=================================================================================
964 void SMESHGUI_AddMeshElementDlg::ActivateThisDialog()
965 {
966   /* Emit a signal to deactivate the active dialog */
967   mySMESHGUI->EmitSignalDeactivateDialog();
968
969   GroupConstructors->setEnabled(true);
970   GroupC1->setEnabled(true);
971   GroupButtons->setEnabled(true);
972
973   SMESH::SetPointRepresentation(true);
974
975   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
976     aViewWindow->SetSelectionMode( NodeSelection );
977   SelectionIntoArgument();
978 }
979
980 //=================================================================================
981 // function : enterEvent()
982 // purpose  :
983 //=================================================================================
984 void SMESHGUI_AddMeshElementDlg::enterEvent (QEvent*)
985 {
986   if ( !GroupConstructors->isEnabled() ) {
987     SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI );
988     if ( aViewWindow && !mySelector && !mySimulation) {
989       mySelector = aViewWindow->GetSelector();
990       mySimulation = new SMESH::TElementSimulation(
991         dynamic_cast<SalomeApp_Application*>( mySMESHGUI->application() ) );
992     }
993     ActivateThisDialog();
994   }
995 }
996
997 //=================================================================================
998 // function : CheckBox()
999 // purpose  :
1000 //=================================================================================
1001 void SMESHGUI_AddMeshElementDlg::CheckBox (int state)
1002 {
1003   if (!myNbOkNodes)
1004     return;
1005
1006   if (state >= 0) {
1007     mySimulation->SetVisibility(false);
1008     displaySimulation();
1009   }
1010 }
1011
1012 //=================================================================================
1013 // function : keyPressEvent()
1014 // purpose  :
1015 //=================================================================================
1016 void SMESHGUI_AddMeshElementDlg::keyPressEvent( QKeyEvent* e )
1017 {
1018   QDialog::keyPressEvent( e );
1019   if ( e->isAccepted() )
1020     return;
1021
1022   if ( e->key() == Qt::Key_F1 ) {
1023     e->accept();
1024     ClickOnHelp();
1025   }
1026 }
1027
1028 //=================================================================================
1029 // function : onDiameterChanged()
1030 // purpose  :
1031 //=================================================================================
1032 void SMESHGUI_AddMeshElementDlg::onDiameterChanged(){
1033   displaySimulation();
1034 }
1035
1036 //=================================================================================
1037 // function : onOpenView()
1038 // purpose  :
1039 //=================================================================================
1040 void SMESHGUI_AddMeshElementDlg::onOpenView()
1041 {
1042   if ( mySelector && mySimulation ) {
1043     mySimulation->SetVisibility(false);
1044     SMESH::SetPointRepresentation(false);
1045   }
1046   else {
1047     mySelector = SMESH::GetViewWindow( mySMESHGUI )->GetSelector();
1048     mySimulation = new SMESH::TElementSimulation(
1049       dynamic_cast<SalomeApp_Application*>( mySMESHGUI->application() ) );
1050     ActivateThisDialog();
1051   }
1052 }
1053
1054 //=================================================================================
1055 // function : onCloseView()
1056 // purpose  :
1057 //=================================================================================
1058 void SMESHGUI_AddMeshElementDlg::onCloseView()
1059 {
1060   DeactivateActiveDialog();
1061   mySelector = 0;
1062   delete mySimulation;
1063   mySimulation = 0;
1064 }
1065
1066 //=================================================================================
1067 // function : isValid()
1068 // purpose  :
1069 //=================================================================================
1070 bool SMESHGUI_AddMeshElementDlg::isValid()
1071 {
1072   if( GroupGroups->isChecked() && ComboBox_GroupName->currentText().isEmpty() ) {
1073     SUIT_MessageBox::warning( this, tr( "SMESH_WRN_WARNING" ), tr( "GROUP_NAME_IS_EMPTY" ) );
1074     return false;
1075   }
1076   return true;
1077 }