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