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