Salome HOME
Update copyright notes (for 2010)
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_AddMeshElementDlg.cxx
1 //  Copyright (C) 2007-2010  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.
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_Utils.h"
32 #include "SMESHGUI_VTKUtils.h"
33 #include "SMESHGUI_MeshUtils.h"
34 #include "SMESHGUI_GroupUtils.h"
35 #include "SMESHGUI_IdValidator.h"
36
37 #include <SMESH_Actor.h>
38 #include <SMESH_ActorUtils.h>
39 #include <SMESH_FaceOrientationFilter.h>
40 #include <SMDS_Mesh.hxx>
41
42 // SALOME GUI inclues
43 #include <SUIT_Desktop.h>
44 #include <SUIT_Session.h>
45 #include <SUIT_ResourceMgr.h>
46 #include <SUIT_MessageBox.h>
47 #include <SUIT_ViewManager.h>
48 #include <LightApp_SelectionMgr.h>
49 #include <SALOME_ListIO.hxx>
50 #include <SalomeApp_Application.h>
51 #include <SVTK_ViewModel.h>
52 #include <SVTK_ViewWindow.h>
53
54 // IDL incldues
55 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
56
57 // OCCT includes
58 #include <TColStd_MapOfInteger.hxx>
59
60 // VTK includes
61 #include <vtkCell.h>
62 #include <vtkIdList.h>
63 #include <vtkUnstructuredGrid.h>
64 #include <vtkDataSetMapper.h>
65 #include <vtkPolyDataMapper.h>
66 #include <vtkProperty.h>
67
68 // Qt includes
69 #include <QComboBox>
70 #include <QGroupBox>
71 #include <QLabel>
72 #include <QLineEdit>
73 #include <QPushButton>
74 #include <QRadioButton>
75 #include <QHBoxLayout>
76 #include <QVBoxLayout>
77 #include <QGridLayout>
78 #include <QVariant>
79 #include <QCheckBox>
80 #include <QKeyEvent>
81 #include <QButtonGroup>
82
83 #define SPACING 6
84 #define MARGIN  11
85
86 namespace SMESH
87 {
88   class TElementSimulation
89   {
90     SalomeApp_Application* myApplication;
91     SUIT_ViewWindow* myViewWindow;
92     SVTK_ViewWindow* myVTKViewWindow;
93
94     SALOME_Actor* myPreviewActor;
95     vtkDataSetMapper* myMapper;
96     vtkUnstructuredGrid* myGrid;
97
98     SALOME_Actor* myFaceOrientation;
99     vtkPolyDataMapper* myFaceOrientationDataMapper;
100     SMESH_FaceOrientationFilter* myFaceOrientationFilter;
101
102   public:
103     TElementSimulation (SalomeApp_Application* theApplication)
104     {
105       myApplication = theApplication;
106       SUIT_ViewManager* mgr = theApplication->activeViewManager();
107       if (!mgr) return;
108       myViewWindow = mgr->getActiveView();
109       myVTKViewWindow = GetVtkViewWindow(myViewWindow);
110
111       myGrid = vtkUnstructuredGrid::New();
112
113       // Create and display actor
114       myMapper = vtkDataSetMapper::New();
115       myMapper->SetInput(myGrid);
116
117       myPreviewActor = SALOME_Actor::New();
118       myPreviewActor->PickableOff();
119       myPreviewActor->VisibilityOff();
120       myPreviewActor->SetMapper(myMapper);
121
122       vtkFloatingPointType anRGB[3];
123       vtkProperty* aProp = vtkProperty::New();
124       GetColor( "SMESH", "fill_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 170, 255 ) );
125       aProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
126       myPreviewActor->SetProperty( aProp );
127       aProp->Delete();
128
129       vtkProperty* aBackProp = vtkProperty::New();
130       GetColor( "SMESH", "backface_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 0, 255 ) );
131       aBackProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
132       myPreviewActor->SetBackfaceProperty( aBackProp );
133       aBackProp->Delete();
134
135       myVTKViewWindow->AddActor(myPreviewActor);
136
137       // Orientation of faces
138       myFaceOrientationFilter = SMESH_FaceOrientationFilter::New();
139       myFaceOrientationFilter->SetInput(myGrid);
140
141       myFaceOrientationDataMapper = vtkPolyDataMapper::New();
142       myFaceOrientationDataMapper->SetInput(myFaceOrientationFilter->GetOutput());
143
144       myFaceOrientation = SALOME_Actor::New();
145       myFaceOrientation->PickableOff();
146       myFaceOrientation->VisibilityOff();
147       myFaceOrientation->SetMapper(myFaceOrientationDataMapper);
148
149       vtkProperty* anOrientationProp = vtkProperty::New();
150       GetColor( "SMESH", "orientation_color", anRGB[0], anRGB[1], anRGB[2], QColor( 255, 255, 255 ) );
151       anOrientationProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
152       myFaceOrientation->SetProperty( anOrientationProp );
153       anOrientationProp->Delete();
154
155       myVTKViewWindow->AddActor(myFaceOrientation);
156     }
157
158     typedef std::vector<vtkIdType> TVTKIds;
159     void SetPosition (SMESH_Actor* theActor,
160                       vtkIdType theType,
161                       const TVTKIds& theIds)
162     {
163       vtkUnstructuredGrid *aGrid = theActor->GetUnstructuredGrid();
164       myGrid->SetPoints(aGrid->GetPoints());
165
166       const int* aConn = NULL;
167       switch (theType) {
168       case VTK_TETRA:
169         {
170           static int anIds[] = {0,2,1,3};
171           aConn = anIds;
172           break;
173         }
174       case VTK_PYRAMID:
175         {
176           static int anIds[] = {0,3,2,1,4};
177           aConn = anIds;
178           break;
179         }
180       case VTK_HEXAHEDRON:
181         {
182           static int anIds[] = {0,3,2,1,4,7,6,5};
183           aConn = anIds;
184           break;
185         }
186       }
187
188       myGrid->Reset();
189       vtkIdList *anIds = vtkIdList::New();
190
191       if(aConn)
192         for (int i = 0, iEnd = theIds.size(); i < iEnd; i++)
193           anIds->InsertId(i,theIds[aConn[i]]);
194       else
195         for (int i = 0, iEnd = theIds.size(); i < iEnd; i++)
196           anIds->InsertId(i,theIds[i]);
197
198       myGrid->InsertNextCell(theType,anIds);
199       anIds->Delete();
200
201       myGrid->Modified();
202
203       SetVisibility(true, theActor->GetFacesOriented());
204     }
205
206
207     void SetVisibility (bool theVisibility, bool theShowOrientation = false)
208     {
209       myPreviewActor->SetVisibility(theVisibility);
210       myFaceOrientation->SetVisibility(theShowOrientation);
211       RepaintCurrentView();
212     }
213
214
215     ~TElementSimulation()
216     {
217       if (FindVtkViewWindow(myApplication->activeViewManager(), myViewWindow)) {
218         myVTKViewWindow->RemoveActor(myPreviewActor);
219         myVTKViewWindow->RemoveActor(myFaceOrientation);
220       }
221       myPreviewActor->Delete();
222       myFaceOrientation->Delete();
223
224       myMapper->RemoveAllInputs();
225       myMapper->Delete();
226
227       myFaceOrientationFilter->Delete();
228
229       myFaceOrientationDataMapper->RemoveAllInputs();
230       myFaceOrientationDataMapper->Delete();
231
232       myGrid->Delete();
233     }
234   };
235 }
236
237 //=================================================================================
238 // function : SMESHGUI_AddMeshElementDlg()
239 // purpose  : constructor
240 //=================================================================================
241 SMESHGUI_AddMeshElementDlg::SMESHGUI_AddMeshElementDlg( SMESHGUI* theModule,
242                                                         SMDSAbs_ElementType ElementType,
243                                                         int nbNodes )
244   : QDialog( SMESH::GetDesktop( theModule ) ),
245     mySMESHGUI( theModule ),
246     mySelectionMgr( SMESH::GetSelectionMgr( theModule ) )
247 {
248   setModal( false );
249   setAttribute( Qt::WA_DeleteOnClose, true );
250
251   SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>
252     (SUIT_Session::session()->activeApplication());
253   myIsPoly = false;
254   mySimulation = new SMESH::TElementSimulation (anApp);
255   mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
256
257   // verify nb nodes and type
258   myNbNodes = nbNodes;
259   myElementType = ElementType;
260   switch (ElementType) {
261   case SMDSAbs_0DElement:
262     if (myNbNodes != 1)
263       myNbNodes = 1;
264     break;
265   case SMDSAbs_Face:
266     //     if (myNbNodes != 3 && myNbNodes != 4)
267     //       myNbNodes = 3;
268     //     break;
269   case SMDSAbs_Volume:
270     //     if (myNbNodes != 4 && myNbNodes != 8) //(nbNodes < 4 || nbNodes > 8 || nbNodes == 7)
271     //       myNbNodes = 4;
272     break;
273   default:
274     myElementType = SMDSAbs_Edge;
275     myNbNodes = 2;
276   }
277
278   QString elemName;
279   if (myNbNodes == 1) {
280     elemName = "ELEM0D";
281     myHelpFileName = "adding_nodes_and_elements_page.html#adding_0delems_anchor";
282   }
283   else if (myNbNodes == 2) {
284     elemName = "EDGE";
285     myHelpFileName = "adding_nodes_and_elements_page.html#adding_edges_anchor";
286   }
287   else if (myNbNodes == 3) {
288     elemName = "TRIANGLE";
289     myHelpFileName = "adding_nodes_and_elements_page.html#adding_triangles_anchor";
290   }
291   else if (myNbNodes == 4) {
292     if (myElementType == SMDSAbs_Face) {
293       elemName = "QUADRANGLE";
294       myHelpFileName = "adding_nodes_and_elements_page.html#adding_quadrangles_anchor";
295     }
296     else {
297       elemName = "TETRAS";
298       myHelpFileName = "adding_nodes_and_elements_page.html#adding_tetrahedrons_anchor";
299     }
300   }
301   else if (myNbNodes == 8) {
302     elemName = "HEXAS";
303     myHelpFileName = "adding_nodes_and_elements_page.html#adding_hexahedrons_anchor";
304   }
305   else if (myElementType == SMDSAbs_Face) {
306     elemName = "POLYGON";
307     myIsPoly = true;
308     myHelpFileName = "adding_nodes_and_elements_page.html#adding_polygons_anchor";
309   }
310   else if (myElementType == SMDSAbs_Volume) {
311     myHelpFileName = "adding_nodes_and_elements_page.html#adding_polyhedrons_anchor";
312   }
313
314   QString iconName      = tr(QString("ICON_DLG_%1").arg(elemName).toLatin1().data());
315   QString buttonGrTitle = tr(QString("SMESH_%1").arg(elemName).toLatin1().data());
316   QString caption       = tr(QString("SMESH_ADD_%1_TITLE").arg(elemName).toLatin1().data());
317   QString grBoxTitle    = tr(QString("SMESH_ADD_%1").arg(elemName).toLatin1().data());
318
319   QPixmap image0 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", iconName));
320   QPixmap image1 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
321
322   setWindowTitle(caption);
323   setSizeGripEnabled(true);
324
325   QVBoxLayout* aTopLayout = new QVBoxLayout(this);
326   aTopLayout->setSpacing(SPACING);
327   aTopLayout->setMargin(MARGIN);
328
329   /***************************************************************/
330   GroupConstructors = new QGroupBox(buttonGrTitle, this);
331   QButtonGroup* ButtonGroup = new QButtonGroup(this);
332   QHBoxLayout* GroupConstructorsLayout = new QHBoxLayout(GroupConstructors);
333   GroupConstructorsLayout->setSpacing(SPACING);
334   GroupConstructorsLayout->setMargin(MARGIN);
335
336   Constructor1 = new QRadioButton(GroupConstructors);
337   Constructor1->setIcon(image0);
338   Constructor1->setChecked(true);
339
340   GroupConstructorsLayout->addWidget(Constructor1);
341   ButtonGroup->addButton( Constructor1, 0 );
342
343   /***************************************************************/
344   GroupC1 = new QGroupBox(grBoxTitle, this);
345   QGridLayout* GroupC1Layout = new QGridLayout(GroupC1);
346   GroupC1Layout->setSpacing(SPACING);
347   GroupC1Layout->setMargin(MARGIN);
348
349   TextLabelC1A1 = new QLabel(tr("SMESH_ID_NODES"), GroupC1);
350   SelectButtonC1A1 = new QPushButton(GroupC1);
351   SelectButtonC1A1->setIcon(image1);
352   LineEditC1A1 = new QLineEdit(GroupC1);
353   //  LineEditC1A1->setReadOnly(true);
354   if (!myIsPoly)
355     LineEditC1A1->setValidator(new SMESHGUI_IdValidator(this, myNbNodes));
356
357   Reverse = myElementType == SMDSAbs_Face ? new QCheckBox(tr("SMESH_REVERSE"), GroupC1) : 0;
358
359   GroupC1Layout->addWidget(TextLabelC1A1,    0, 0);
360   GroupC1Layout->addWidget(SelectButtonC1A1, 0, 1);
361   GroupC1Layout->addWidget(LineEditC1A1,     0, 2);
362   if ( Reverse ) GroupC1Layout->addWidget(Reverse, 1, 0, 1, 3);
363
364   /***************************************************************/
365   GroupGroups = new QGroupBox( tr( "SMESH_ADD_TO_GROUP" ), this );
366   GroupGroups->setCheckable( true );
367   QHBoxLayout* GroupGroupsLayout = new QHBoxLayout(GroupGroups);
368   GroupGroupsLayout->setSpacing(SPACING);
369   GroupGroupsLayout->setMargin(MARGIN);
370
371   TextLabel_GroupName = new QLabel( tr( "SMESH_GROUP" ), GroupGroups );
372   ComboBox_GroupName = new QComboBox( GroupGroups );
373   ComboBox_GroupName->setEditable( true );
374
375   GroupGroupsLayout->addWidget( TextLabel_GroupName );
376   GroupGroupsLayout->addWidget( ComboBox_GroupName, 1 );
377
378   /***************************************************************/
379   GroupButtons = new QGroupBox(this);
380   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
381   GroupButtonsLayout->setSpacing(SPACING);
382   GroupButtonsLayout->setMargin(MARGIN);
383
384   buttonOk = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), GroupButtons);
385   buttonOk->setAutoDefault(true);
386   buttonOk->setDefault(true);
387   buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
388   buttonApply->setAutoDefault(true);
389   buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
390   buttonCancel->setAutoDefault(true);
391   buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
392   buttonHelp->setAutoDefault(true);
393
394   GroupButtonsLayout->addWidget(buttonOk);
395   GroupButtonsLayout->addSpacing(10);
396   GroupButtonsLayout->addWidget(buttonApply);
397   GroupButtonsLayout->addSpacing(10);
398   GroupButtonsLayout->addStretch();
399   GroupButtonsLayout->addWidget(buttonCancel);
400   GroupButtonsLayout->addWidget(buttonHelp);
401
402   /***************************************************************/
403   aTopLayout->addWidget(GroupConstructors);
404   aTopLayout->addWidget(GroupC1);
405   aTopLayout->addWidget(GroupGroups);
406   aTopLayout->addWidget(GroupButtons);
407
408   Init(); /* Initialisations */
409 }
410
411 //=================================================================================
412 // function : ~SMESHGUI_AddMeshElementDlg()
413 // purpose  : Destroys the object and frees any allocated resources
414 //=================================================================================
415 SMESHGUI_AddMeshElementDlg::~SMESHGUI_AddMeshElementDlg()
416 {
417   delete mySimulation;
418 }
419
420 //=================================================================================
421 // function : Init()
422 // purpose  :
423 //=================================================================================
424 void SMESHGUI_AddMeshElementDlg::Init()
425 {
426   GroupC1->show();
427   Constructor1->setChecked(true);
428   myEditCurrentArgument = LineEditC1A1;
429   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
430
431   /* reset "Add to group" control */
432   GroupGroups->setChecked( false );
433   GroupGroups->setVisible( myElementType != SMDSAbs_0DElement );
434
435   myNbOkNodes = 0;
436   myActor = 0;
437
438   /* signals and slots connections */
439   connect(buttonOk, SIGNAL(clicked()),     SLOT(ClickOnOk()));
440   connect(buttonCancel, SIGNAL(clicked()), SLOT(ClickOnCancel()));
441   connect(buttonApply, SIGNAL(clicked()),  SLOT(ClickOnApply()));
442   connect(buttonHelp, SIGNAL(clicked()),   SLOT(ClickOnHelp()));
443
444   connect(SelectButtonC1A1, SIGNAL(clicked()), SLOT(SetEditCurrentArgument()));
445   connect(LineEditC1A1, SIGNAL(textChanged(const QString&)), SLOT(onTextChange(const QString&)));
446   connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()), SLOT(DeactivateActiveDialog()));
447   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(SelectionIntoArgument()));
448   /* to close dialog if study frame change */
449   connect(mySMESHGUI, SIGNAL (SignalStudyFrameChanged()), SLOT(ClickOnCancel()));
450
451   if (Reverse)
452     connect(Reverse, SIGNAL(stateChanged(int)), SLOT(CheckBox(int)));
453
454   // set selection mode
455   SMESH::SetPointRepresentation(true);
456
457   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
458     aViewWindow->SetSelectionMode( NodeSelection );
459
460   myBusy = false;
461
462   SelectionIntoArgument();
463 }
464
465 //=================================================================================
466 // function : ClickOnApply()
467 // purpose  :
468 //=================================================================================
469 void SMESHGUI_AddMeshElementDlg::ClickOnApply()
470 {
471   if( !isValid() )
472     return;
473
474   if (myNbOkNodes && !mySMESHGUI->isActiveStudyLocked()) {
475     myBusy = true;
476     SMESH::long_array_var anArrayOfIndices = new SMESH::long_array;
477     anArrayOfIndices->length(myNbNodes);
478     bool reverse = (Reverse && Reverse->isChecked());
479     QStringList aListId = myEditCurrentArgument->text().split(" ", QString::SkipEmptyParts);
480     for (int i = 0; i < aListId.count(); i++)
481       if (reverse)
482         anArrayOfIndices[i] = aListId[ myNbNodes - i - 1 ].toInt();
483       else
484         anArrayOfIndices[i] = aListId[ i ].toInt();
485
486     long anElemId = -1;
487     SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
488     switch (myElementType) {
489     case SMDSAbs_0DElement:
490       anElemId = aMeshEditor->Add0DElement(anArrayOfIndices[0]); break;
491     case SMDSAbs_Edge:
492       anElemId = aMeshEditor->AddEdge(anArrayOfIndices.inout()); break;
493     case SMDSAbs_Face: {
494       if(myIsPoly)
495         anElemId = aMeshEditor->AddPolygonalFace(anArrayOfIndices.inout());
496       else
497         anElemId = aMeshEditor->AddFace(anArrayOfIndices.inout());
498       break;
499     }
500     case SMDSAbs_Volume:
501       anElemId = aMeshEditor->AddVolume(anArrayOfIndices.inout()); break;
502     default:;
503     }
504
505     if( anElemId != -1 && GroupGroups->isChecked() ) {
506       SMESH::SMESH_Group_var aGroup;
507       QString aGroupName = ComboBox_GroupName->currentText();
508       SMESH::ListOfGroups aListOfGroups = *myMesh->GetGroups();
509       for( int i = 0, n = aListOfGroups.length(); i < n; i++ ) {
510         SMESH::SMESH_GroupBase_var aGroupBase = aListOfGroups[i];
511         if( !aGroupBase->_is_nil() ) {
512           SMESH::SMESH_Group_var aRefGroup = SMESH::SMESH_Group::_narrow( aGroupBase );
513           if( !aRefGroup->_is_nil() ) {
514             QString aRefGroupName( aRefGroup->GetName() );
515             if( aRefGroupName == aGroupName ) {
516               aGroup = aRefGroup; // // add node to existing group
517               break;
518             }
519           }
520         }
521       }
522       if( aGroup->_is_nil() ) // create new group
523         aGroup = SMESH::AddGroup( myMesh, (SMESH::ElementType)myElementType, aGroupName );
524
525       if( !aGroup->_is_nil() ) {
526         SMESH::long_array_var anIdList = new SMESH::long_array;
527         anIdList->length( 1 );
528         anIdList[0] = anElemId;
529         aGroup->Add( anIdList.inout() );
530       }
531     }
532
533     SALOME_ListIO aList; aList.Append( myActor->getIO() );
534     mySelector->ClearIndex();
535     mySelectionMgr->setSelectedObjects( aList, false );
536
537     SMESH::UpdateView();
538     mySimulation->SetVisibility(false);
539
540     buttonOk->setEnabled(false);
541     buttonApply->setEnabled(false);
542
543     myEditCurrentArgument->setText("");
544
545     myBusy = false;
546   }
547 }
548
549 //=================================================================================
550 // function : ClickOnOk()
551 // purpose  :
552 //=================================================================================
553 void SMESHGUI_AddMeshElementDlg::ClickOnOk()
554 {
555   ClickOnApply();
556   ClickOnCancel();
557 }
558
559 //=================================================================================
560 // function : ClickOnCancel()
561 // purpose  :
562 //=================================================================================
563 void SMESHGUI_AddMeshElementDlg::ClickOnCancel()
564 {
565   //mySelectionMgr->clearSelected();
566   mySimulation->SetVisibility(false);
567   SMESH::SetPointRepresentation(false);
568   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
569     aViewWindow->SetSelectionMode( ActorSelection );
570   disconnect(mySelectionMgr, 0, this, 0);
571   mySMESHGUI->ResetState();
572   reject();
573 }
574
575 //=================================================================================
576 // function : ClickOnHelp()
577 // purpose  :
578 //=================================================================================
579 void SMESHGUI_AddMeshElementDlg::ClickOnHelp()
580 {
581   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
582   if (app)
583     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""),
584                              myHelpFileName);
585   else {
586     QString platform;
587 #ifdef WIN32
588     platform = "winapplication";
589 #else
590     platform = "application";
591 #endif
592     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
593                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
594                              arg(app->resourceMgr()->stringValue("ExternalBrowser",
595                                                                  platform)).
596                              arg(myHelpFileName));
597   }
598 }
599
600 //=================================================================================
601 // function : onTextChange()
602 // purpose  :
603 //=================================================================================
604 void SMESHGUI_AddMeshElementDlg::onTextChange (const QString& theNewText)
605 {
606   if (myBusy) return;
607   myBusy = true;
608
609   myNbOkNodes = 0;
610
611   buttonOk->setEnabled(false);
612   buttonApply->setEnabled(false);
613
614   mySimulation->SetVisibility(false);
615
616   // hilight entered nodes
617   SMDS_Mesh* aMesh = 0;
618   if (myActor)
619     aMesh = myActor->GetObject()->GetMesh();
620
621   if (aMesh) {
622     TColStd_MapOfInteger newIndices;
623
624     QStringList aListId = theNewText.split(" ", QString::SkipEmptyParts);
625     bool allOk = true;
626     for (int i = 0; i < aListId.count(); i++) {
627       if( const SMDS_MeshNode * n = aMesh->FindNode( aListId[ i ].toInt() ) )
628         {
629           newIndices.Add( n->GetID() );
630           myNbOkNodes++;
631         }
632       else
633         allOk = false;  
634     }
635
636     mySelector->AddOrRemoveIndex( myActor->getIO(), newIndices, false );
637     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
638       aViewWindow->highlight( myActor->getIO(), true, true );
639
640     myNbOkNodes = ( allOk && myNbNodes == aListId.count() );
641
642     if (myIsPoly)
643       {
644         if ( !allOk || myElementType != SMDSAbs_Face || aListId.count() < 3 )
645           myNbOkNodes = 0;
646         else
647           myNbOkNodes = aListId.count();
648       }
649   }
650
651   if(myNbOkNodes) {
652     buttonOk->setEnabled(true);
653     buttonApply->setEnabled(true);
654     displaySimulation();
655   }
656
657   myBusy = false;
658 }
659
660 //=================================================================================
661 // function : SelectionIntoArgument()
662 // purpose  : Called when selection has changed
663 //=================================================================================
664 void SMESHGUI_AddMeshElementDlg::SelectionIntoArgument()
665 {
666   if (myBusy) return;
667
668   // clear
669   myNbOkNodes = 0;
670   myActor = 0;
671
672   myBusy = true;
673   myEditCurrentArgument->setText("");
674   myBusy = false;
675
676   if (!GroupButtons->isEnabled()) // inactive
677     return;
678
679   buttonOk->setEnabled(false);
680   buttonApply->setEnabled(false);
681
682   mySimulation->SetVisibility(false);
683   //  SMESH::SetPointRepresentation(true);
684
685   QString aCurrentEntry = myEntry;
686
687   // get selected mesh
688   SALOME_ListIO aList;
689   mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
690
691   if (aList.Extent() != 1)
692     return;
693
694   Handle(SALOME_InteractiveObject) anIO = aList.First();
695   myEntry = anIO->getEntry();
696   myMesh = SMESH::GetMeshByIO(anIO);
697   if (myMesh->_is_nil())
698     return;
699
700   // process groups
701   if ( !myMesh->_is_nil() && myEntry != aCurrentEntry ) {
702     ComboBox_GroupName->clear();
703     ComboBox_GroupName->addItem( QString() );
704     SMESH::ListOfGroups aListOfGroups = *myMesh->GetGroups();
705     for ( int i = 0, n = aListOfGroups.length(); i < n; i++ ) {
706       SMESH::SMESH_GroupBase_var aGroupBase = aListOfGroups[i];
707       if ( !aGroupBase->_is_nil() && aGroupBase->GetType() == (SMESH::ElementType)myElementType ) {
708         SMESH::SMESH_Group_var aGroup = SMESH::SMESH_Group::_narrow( aGroupBase );
709         if ( !aGroup->_is_nil() ) {
710           QString aGroupName( aGroup->GetName() );
711           if ( !aGroupName.isEmpty() )
712             ComboBox_GroupName->addItem( aGroupName );
713         }
714       }
715     }
716   }
717
718   myActor = SMESH::FindActorByEntry(anIO->getEntry());
719   if (!myActor)
720     return;
721
722   // get selected nodes
723   QString aString = "";
724   int nbNodes = SMESH::GetNameOfSelectedNodes(mySelector,myActor->getIO(),aString);
725   myBusy = true;
726   myEditCurrentArgument->setText(aString);
727   myBusy = false;
728   if (myIsPoly && myElementType == SMDSAbs_Face && nbNodes >= 3 ) {
729     myNbNodes = nbNodes;
730   } else if (myNbNodes != nbNodes) {
731     return;
732   }
733
734   // OK
735   myNbOkNodes = nbNodes;
736
737   buttonOk->setEnabled(true);
738   buttonApply->setEnabled(true);
739
740   displaySimulation();
741 }
742
743 //=================================================================================
744 // function : displaySimulation()
745 // purpose  :
746 //=================================================================================
747 void SMESHGUI_AddMeshElementDlg::displaySimulation()
748 {
749   if (myNbOkNodes && GroupButtons->isEnabled()) {
750     SMESH::TElementSimulation::TVTKIds anIds;
751     QStringList aListId = myEditCurrentArgument->text().split(" ", QString::SkipEmptyParts);
752     for (int i = 0; i < aListId.count(); i++)
753       anIds.push_back(myActor->GetObject()->GetNodeVTKId(aListId[ i ].toInt()));
754
755     if (Reverse && Reverse->isChecked())
756       reverse(anIds.begin(),anIds.end());
757
758     vtkIdType aType = 0;
759     if (myIsPoly)
760       switch ( myElementType ) {
761       case SMDSAbs_Face  : aType = VTK_POLYGON; break;
762       default: return;
763       }
764     else {
765       switch (myNbNodes) {
766       case 2: aType = VTK_LINE; break;
767       case 3: aType = VTK_TRIANGLE; break;
768       case 4: aType = myElementType == SMDSAbs_Face ? VTK_QUAD : VTK_TETRA; break;
769       case 8: aType = VTK_HEXAHEDRON; break;
770       default: return;
771       }
772     }
773
774     mySimulation->SetPosition(myActor,aType,anIds);
775     SMESH::UpdateView();
776   }
777 }
778
779 //=================================================================================
780 // function : SetEditCurrentArgument()
781 // purpose  :
782 //=================================================================================
783 void SMESHGUI_AddMeshElementDlg::SetEditCurrentArgument()
784 {
785   QPushButton* send = (QPushButton*)sender();
786   if (send == SelectButtonC1A1) {
787     LineEditC1A1->setFocus();
788     myEditCurrentArgument = LineEditC1A1;
789   }
790   SelectionIntoArgument();
791 }
792
793 //=================================================================================
794 // function : DeactivateActiveDialog()
795 // purpose  :
796 //=================================================================================
797 void SMESHGUI_AddMeshElementDlg::DeactivateActiveDialog()
798 {
799   if (GroupConstructors->isEnabled()) {
800     GroupConstructors->setEnabled(false);
801     GroupC1->setEnabled(false);
802     GroupButtons->setEnabled(false);
803     mySimulation->SetVisibility(false);
804     mySMESHGUI->ResetState();
805     mySMESHGUI->SetActiveDialogBox(0);
806   }
807 }
808
809 //=================================================================================
810 // function : ActivateThisDialog()
811 // purpose  :
812 //=================================================================================
813 void SMESHGUI_AddMeshElementDlg::ActivateThisDialog()
814 {
815   /* Emit a signal to deactivate the active dialog */
816   mySMESHGUI->EmitSignalDeactivateDialog();
817
818   GroupConstructors->setEnabled(true);
819   GroupC1->setEnabled(true);
820   GroupButtons->setEnabled(true);
821
822   SMESH::SetPointRepresentation(true);
823
824   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
825     aViewWindow->SetSelectionMode( NodeSelection );
826   SelectionIntoArgument();
827 }
828
829 //=================================================================================
830 // function : enterEvent()
831 // purpose  :
832 //=================================================================================
833 void SMESHGUI_AddMeshElementDlg::enterEvent (QEvent*)
834 {
835   if (GroupConstructors->isEnabled())
836     return;
837   ActivateThisDialog();
838 }
839
840 //=================================================================================
841 // function : closeEvent()
842 // purpose  :
843 //=================================================================================
844 void SMESHGUI_AddMeshElementDlg::closeEvent (QCloseEvent*)
845 {
846   /* same than click on cancel button */
847   ClickOnCancel();
848 }
849
850 //=================================================================================
851 // function : hideEvent()
852 // purpose  : caused by ESC key
853 //=================================================================================
854 void SMESHGUI_AddMeshElementDlg::hideEvent (QHideEvent*)
855 {
856   if (!isMinimized())
857     ClickOnCancel();
858 }
859
860 //=================================================================================
861 // function : CheckBox()
862 // purpose  :
863 //=================================================================================
864 void SMESHGUI_AddMeshElementDlg::CheckBox (int state)
865 {
866   if (!myNbOkNodes)
867     return;
868
869   if (state >= 0) {
870     mySimulation->SetVisibility(false);
871     displaySimulation();
872   }
873 }
874
875 //=================================================================================
876 // function : keyPressEvent()
877 // purpose  :
878 //=================================================================================
879 void SMESHGUI_AddMeshElementDlg::keyPressEvent( QKeyEvent* e )
880 {
881   QDialog::keyPressEvent( e );
882   if ( e->isAccepted() )
883     return;
884
885   if ( e->key() == Qt::Key_F1 ) {
886     e->accept();
887     ClickOnHelp();
888   }
889 }
890
891 //=================================================================================
892 // function : isValid
893 // purpose  :
894 //=================================================================================
895 bool SMESHGUI_AddMeshElementDlg::isValid()
896 {
897   if( GroupGroups->isChecked() && ComboBox_GroupName->currentText().isEmpty() ) {
898     SUIT_MessageBox::warning( this, tr( "SMESH_WRN_WARNING" ), tr( "GROUP_NAME_IS_EMPTY" ) );
899     return false;
900   }
901   return true;
902 }