Salome HOME
PAL9391
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_CreatePolyhedralVolumeDlg.cxx
1 //  SMESH SMESHGUI : GUI for SMESH component
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SMESHGUI_CreatePolyhedralVolumeDlg.cxx
25 //  Author : Michael ZORIN
26 //  Module : SMESH
27 //  $Header: 
28
29 #include "SMESHGUI_CreatePolyhedralVolumeDlg.h"
30
31 #include "SMESHGUI.h"
32 #include "SMESHGUI_Utils.h"
33 #include "SMESHGUI_VTKUtils.h"
34 #include "SMESHGUI_MeshUtils.h"
35 #include "SMESHGUI_IdValidator.h"
36 #include "SMESH_ActorUtils.h"
37 #include "SMESHGUI_SpinBox.h"
38 #include "SMESH_Actor.h"
39 #include "SMESH_TypeFilter.hxx"
40 #include "SMDS_Mesh.hxx"
41
42 #include "VTKViewer_ViewWindow.h"
43
44 #include "SUIT_ResourceMgr.h"
45
46 #include "SalomeApp_Application.h"
47 #include "SalomeApp_Study.h"
48 #include "SUIT_Desktop.h"
49 #include "SUIT_MessageBox.h"
50 #include "SalomeApp_SelectionMgr.h"
51 #include "utilities.h"
52
53 #include "SVTK_ViewWindow.h"
54 #include "SVTK_Selector.h"
55
56 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
57 #include <TColStd_ListOfInteger.hxx>
58 #include <TColStd_ListIteratorOfListOfInteger.hxx>
59
60 #include <vtkCell.h>
61 #include <vtkIdList.h>
62 #include <vtkIntArray.h>
63 #include <vtkCellArray.h>
64 #include <vtkUnsignedCharArray.h>
65 #include <vtkUnstructuredGrid.h>
66 #include <vtkDataSetMapper.h>
67
68 // QT Includes
69 #include <qapplication.h>
70 #include <qbuttongroup.h>
71 #include <qgroupbox.h>
72 #include <qlabel.h>
73 #include <qlineedit.h>
74 #include <qpushbutton.h>
75 #include <qradiobutton.h>
76 #include <qcheckbox.h>
77 #include <qlayout.h>
78 #include <qspinbox.h> 
79 #include <qpixmap.h>
80 #include <qlistbox.h>
81
82 // IDL Headers
83 #include "SALOMEconfig.h"
84 #include CORBA_SERVER_HEADER(SMESH_Group)
85
86 using namespace std;
87
88 namespace SMESH{
89
90 class TPolySimulation{
91     SVTK_ViewWindow* myViewWindow;
92
93     SALOME_Actor *myPreviewActor;
94     vtkDataSetMapper* myMapper;
95     vtkUnstructuredGrid* myGrid;
96
97   public:
98
99     TPolySimulation(SalomeApp_Application* app)
100     {
101       SUIT_ViewManager* mgr = app->activeViewManager();
102       myViewWindow = mgr ? dynamic_cast<SVTK_ViewWindow*>( mgr->getActiveView() ) : NULL;
103
104       myGrid = vtkUnstructuredGrid::New();
105   
106       // Create and display actor
107       myMapper = vtkDataSetMapper::New();
108       myMapper->SetInput( myGrid );
109
110       myPreviewActor = SALOME_Actor::New();
111       myPreviewActor->PickableOff();
112       myPreviewActor->VisibilityOff();
113       myPreviewActor->SetMapper( myMapper );
114
115       float anRGB[3];
116       vtkProperty* aProp = vtkProperty::New();
117       GetColor( "SMESH", "fill_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 170, 255 ) );
118       aProp->SetColor(anRGB[0],anRGB[1],anRGB[2]);
119       myPreviewActor->SetProperty( aProp );
120       aProp->Delete();
121
122       vtkProperty* aBackProp = vtkProperty::New();
123       GetColor( "SMESH", "backface_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 0, 255 ) );
124       aBackProp->SetColor(anRGB[0],anRGB[1],anRGB[2]);
125       myPreviewActor->SetBackfaceProperty( aBackProp );
126       aBackProp->Delete();
127
128       myViewWindow->AddActor( myPreviewActor );
129
130     }
131
132
133     typedef std::vector<vtkIdType> TVTKIds;
134     void SetPosition(SMESH_Actor* theActor, 
135                      vtkIdType theType, 
136                      const TVTKIds& theIds,
137                      bool theReset=true)
138     {
139       vtkUnstructuredGrid *aGrid = theActor->GetUnstructuredGrid();
140       myGrid->SetPoints(aGrid->GetPoints());
141
142       if (theReset) ResetGrid(theReset);
143       
144       vtkIdList *anIds = vtkIdList::New();
145
146       for (int i = 0, iEnd = theIds.size(); i < iEnd; i++)
147         anIds->InsertId(i,theIds[i]);
148
149       myGrid->InsertNextCell(theType,anIds);
150       if(theIds.size()!=0){
151         myGrid->InsertNextCell(theType,anIds);
152         myGrid->Modified();
153       }
154         
155       anIds->Delete();
156
157       SetVisibility(true);
158
159     }
160   
161     void ResetGrid(bool theReset=true){
162       if (theReset) myGrid->Reset();
163     }
164
165     void SetVisibility(bool theVisibility){
166       myPreviewActor->SetVisibility(theVisibility);
167       RepaintCurrentView();
168     }
169
170
171     ~TPolySimulation(){
172         if( myViewWindow )
173           myViewWindow->RemoveActor(myPreviewActor);
174
175       myPreviewActor->Delete();
176
177       myMapper->RemoveAllInputs();
178       myMapper->Delete();
179
180       myGrid->Delete();
181     }
182
183   };
184 }
185
186 //=================================================================================
187 // class    : SMESHGUI_CreatePolyhedralVolumeDlgDlg()
188 // purpose  : 
189 //=================================================================================
190 SMESHGUI_CreatePolyhedralVolumeDlg::SMESHGUI_CreatePolyhedralVolumeDlg( SMESHGUI* theModule, const char* name,
191                                                                         bool modal, WFlags fl )
192   : QDialog( SMESH::GetDesktop( theModule ), name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | Qt::WDestructiveClose),
193     mySMESHGUI( theModule ),
194     mySelectionMgr( SMESH::GetSelectionMgr( theModule ) )
195 {
196   QPixmap image0( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH",tr("ICON_SELECT")));
197
198   if ( !name )
199     setName( "SMESHGUI_CreatePolyhedralVolumeDlg" );
200   resize( 300, 185 ); 
201   setCaption( tr( "SMESH_CREATE_POLYHEDRAL_VOLUME_TITLE" ) );
202   setSizeGripEnabled( TRUE );
203   SMESHGUI_CreatePolyhedralVolumeDlgLayout = new QGridLayout( this ); 
204   SMESHGUI_CreatePolyhedralVolumeDlgLayout->setSpacing( 6 );
205   SMESHGUI_CreatePolyhedralVolumeDlgLayout->setMargin( 11 );
206
207   /***************************************************************/
208   GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
209   GroupConstructors->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)5, (QSizePolicy::SizeType)0, 0, 0, GroupConstructors->sizePolicy().hasHeightForWidth() ) );
210   GroupConstructors->setTitle( tr( "SMESH_ELEMENTS_TYPE" ) );
211   GroupConstructors->setExclusive( TRUE );
212   GroupConstructors->setColumnLayout(0, Qt::Vertical );
213   GroupConstructors->layout()->setSpacing( 0 );
214   GroupConstructors->layout()->setMargin( 0 );
215   GroupConstructorsLayout = new QGridLayout( GroupConstructors->layout() );
216   GroupConstructorsLayout->setAlignment( Qt::AlignTop );
217   GroupConstructorsLayout->setSpacing( 6 );
218   GroupConstructorsLayout->setMargin( 11 );
219   RadioButton1= new QRadioButton( GroupConstructors, "RadioButton1" );
220   RadioButton1->setText( tr( "MESH_NODE"  ) );
221   GroupConstructorsLayout->addWidget( RadioButton1, 0, 0 );
222   RadioButton2= new QRadioButton( GroupConstructors, "RadioButton2" );
223   RadioButton2->setText( tr( "SMESH_FACE"  ) );
224   GroupConstructorsLayout->addWidget( RadioButton2, 0, 2  );
225   SMESHGUI_CreatePolyhedralVolumeDlgLayout->addWidget( GroupConstructors, 0, 0 );
226   
227   /***************************************************************/
228   GroupButtons = new QGroupBox( this, "GroupButtons" );
229   GroupButtons->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)7, (QSizePolicy::SizeType)0, 0, 0, GroupButtons->sizePolicy().hasHeightForWidth() ) );
230   GroupButtons->setGeometry( QRect( 10, 10, 281, 48 ) ); 
231   GroupButtons->setTitle( tr( ""  ) );
232   GroupButtons->setColumnLayout(0, Qt::Vertical );
233   GroupButtons->layout()->setSpacing( 0 );
234   GroupButtons->layout()->setMargin( 0 );
235   GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
236   GroupButtonsLayout->setAlignment( Qt::AlignTop );
237   GroupButtonsLayout->setSpacing( 6 );
238   GroupButtonsLayout->setMargin( 11 );
239   buttonCancel = new QPushButton( GroupButtons, "cancel" );
240   buttonCancel->setText( tr( "SMESH_BUT_CLOSE"  ) );
241   buttonCancel->setAutoDefault( TRUE );
242   GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
243   buttonApply = new QPushButton( GroupButtons, "apply" );
244   buttonApply->setText( tr( "SMESH_BUT_APPLY"  ) );
245   buttonApply->setAutoDefault( TRUE );
246   GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
247   QSpacerItem* spacer_9 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
248   GroupButtonsLayout->addItem( spacer_9, 0, 2 );
249   buttonOk = new QPushButton( GroupButtons, "ok" );
250   buttonOk->setText( tr( "SMESH_BUT_OK"  ) );
251   buttonOk->setAutoDefault( TRUE );
252   buttonOk->setDefault( TRUE );
253   GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
254   SMESHGUI_CreatePolyhedralVolumeDlgLayout->addWidget( GroupButtons, 2, 0 );
255
256   /***************************************************************/
257   GroupContent = new QGroupBox( this, "GroupContent" );
258   GroupContent->setTitle( tr( "SMESH_CONTENT" ) );
259   GroupContent->setColumnLayout(0, Qt::Vertical );
260   GroupContent->layout()->setSpacing( 0 );
261   GroupContent->layout()->setMargin( 0 );
262   GroupContentLayout = new QGridLayout( GroupContent->layout() );
263   GroupContentLayout->setAlignment( Qt::AlignTop );
264   GroupContentLayout->setSpacing( 6 );
265   GroupContentLayout->setMargin( 11 );
266
267   QFrame* anIds = new QFrame(GroupContent, "anIds");
268   QGridLayout* aLayout = new QGridLayout(anIds, 1, 3);
269   aLayout->setSpacing(6);
270   aLayout->setAutoAdd(false);
271   
272   TextLabelIds = new QLabel( anIds, "TextLabelIds" );
273   TextLabelIds->setText( tr( "SMESH_ID_NODES" ) );
274   TextLabelIds->setFixedWidth(74);
275   aLayout->addWidget( TextLabelIds, 0, 0 );
276
277   SelectElementsButton  = new QPushButton( anIds, "SelectElementsButton" );
278   SelectElementsButton->setText( tr( "" ) );
279   SelectElementsButton->setPixmap( image0 );
280   SelectElementsButton->setToggleButton( FALSE );
281   aLayout->addWidget( SelectElementsButton, 0, 1 );
282
283   LineEditElements  = new QLineEdit( anIds, "LineEditElements" );
284   LineEditElements->setValidator( new SMESHGUI_IdValidator( this, "validator" ));
285   aLayout->addWidget( LineEditElements, 0, 2 );
286   
287   GroupContentLayout->addMultiCellWidget(anIds, 0, 0, 0, 1);
288
289   myFacesByNodesLabel = new QLabel(GroupContent, "faces by nodes label");
290   myFacesByNodesLabel->setText(tr("FACES_BY_NODES"));
291   GroupContentLayout->addWidget( myFacesByNodesLabel, 1, 0 );
292
293   myFacesByNodes = new QListBox(GroupContent, "faces by nodes list");
294   myFacesByNodes->setSelectionMode(QListBox::Extended);
295   myFacesByNodes->setMinimumHeight(150);
296   GroupContentLayout->addMultiCellWidget( myFacesByNodes, 2, 4, 0, 0 );
297
298   AddButton = new QPushButton(GroupContent, "add");
299   AddButton->setText(tr("SMESH_BUT_ADD"));
300   AddButton->setMaximumWidth(85);
301   GroupContentLayout->addWidget( AddButton, 2, 1 );
302
303   RemoveButton = new QPushButton(GroupContent, "remove");
304   RemoveButton->setText(tr("SMESH_BUT_REMOVE"));
305   RemoveButton->setMaximumWidth(85);
306   GroupContentLayout->addWidget( RemoveButton, 3, 1 );
307
308   Preview = new QCheckBox( GroupContent, "Preview" );
309   Preview->setText( tr( "SMESH_POLYEDRE_PREVIEW"  ) );
310   GroupContentLayout->addWidget( Preview , 5, 0 );
311
312   mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
313   
314   SMESHGUI_CreatePolyhedralVolumeDlgLayout->addWidget( GroupContent, 1, 0 );
315   
316   GroupContent->show();
317   RadioButton1->setChecked( TRUE );
318  
319   mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
320   
321   Init();
322 }
323
324 //=================================================================================
325 // function : ~SMESHGUI_CreatePolyhedralVolumeDlg()
326 // purpose  : Destroys the object and frees any allocated resources
327 //=================================================================================
328 SMESHGUI_CreatePolyhedralVolumeDlg::~SMESHGUI_CreatePolyhedralVolumeDlg()
329 {
330   // no need to delete child widgets, Qt does it all for us
331   delete mySimulation;
332 }
333
334 static bool busy = false;
335 //=================================================================================
336 // function : Init()
337 // purpose  :
338 //=================================================================================
339 void SMESHGUI_CreatePolyhedralVolumeDlg::Init()
340 {
341   myEditCurrentArgument = LineEditElements;
342   mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
343
344   myNbOkElements = 0;
345   myActor = 0;
346
347   mySimulation = new SMESH::TPolySimulation( dynamic_cast<SalomeApp_Application*>( mySMESHGUI->application() ) );
348
349   /* signals and slots connections */
350   connect(buttonOk, SIGNAL( clicked() ),     SLOT( ClickOnOk() ) );
351   connect(buttonCancel, SIGNAL( clicked() ), SLOT( ClickOnCancel() ) ) ;
352   connect(buttonApply, SIGNAL( clicked() ),  SLOT(ClickOnApply() ) );
353
354   connect( GroupConstructors, SIGNAL(clicked(int) ), SLOT( ConstructorsClicked(int) ) );
355   connect(SelectElementsButton, SIGNAL( clicked() ), SLOT( SetEditCurrentArgument() ) ) ;
356   connect(LineEditElements, SIGNAL( textChanged(const QString&) ), SLOT(onTextChange(const QString&)));
357
358   connect(myFacesByNodes, SIGNAL(selectionChanged()), this, SLOT(onListSelectionChanged()));
359   connect(AddButton, SIGNAL(clicked()), this, SLOT(onAdd()));
360   connect(RemoveButton, SIGNAL(clicked()), this, SLOT(onRemove()));
361   
362   connect( mySMESHGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
363   connect( mySelectionMgr, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
364   connect( Preview, SIGNAL(toggled(bool)), this, SLOT(ClickOnPreview(bool)));
365   /* to close dialog if study change */
366   connect( mySMESHGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
367   
368   /* Move widget on the botton right corner of main widget */
369   int x, y ;
370   mySMESHGUI->DefineDlgPosition( this, x, y ) ;
371   this->move( x, y ) ;
372   this->show() ; /* displays Dialog */
373
374   ConstructorsClicked(0);
375   SelectionIntoArgument();
376 }
377
378
379 //=================================================================================
380 // function : ConstructorsClicked()
381 // purpose  : Radio button management
382 //=================================================================================
383 void SMESHGUI_CreatePolyhedralVolumeDlg::ConstructorsClicked(int constructorId)
384 {
385   //disconnect(mySelectionMgr, 0, this, 0);
386
387   SALOME_ListIO aList;
388   mySelectionMgr->setSelectedObjects( aList );
389   myEditCurrentArgument->clear();
390   myNbOkElements = 0;
391   buttonApply->setEnabled(false);
392   buttonOk->setEnabled(false);
393   mySimulation->SetVisibility(false);
394
395   switch(constructorId)
396     {
397     case 0 :
398       { 
399         if ( myActor ){
400           myActor->SetPointRepresentation(true);
401           myActor->SetEntityMode(SMESH_Actor::eVolumes);
402           myActor->SetRepresentation(SMESH_Actor::eSurface);
403         }
404         else
405           SMESH::SetPointRepresentation(true);
406         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
407           aViewWindow->SetSelectionMode(NodeSelection);
408         
409         AddButton->setEnabled(false);
410         RemoveButton->setEnabled(false);
411         TextLabelIds->setText( tr( "SMESH_ID_NODES" ) );
412         myFacesByNodesLabel->show();
413         myFacesByNodes->clear();
414         myFacesByNodes->show();
415         AddButton->show();
416         RemoveButton->show();
417         Preview->show();
418         break;
419       }
420     case 1 :
421       {
422         if( myActor ){
423           myActor->SetPointRepresentation(false);
424           myActor->SetEntityMode(SMESH_Actor::eFaces);
425           myActor->SetEntityMode(SMESH_Actor::eVolumes);
426           myActor->SetRepresentation(SMESH_Actor::eSurface);
427         } else {
428           SMESH::SetPointRepresentation(false);
429         }
430         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
431           aViewWindow->SetSelectionMode(FaceSelection);
432         
433         TextLabelIds->setText( tr( "SMESH_ID_FACES" ) );
434         myFacesByNodesLabel->hide();
435         myFacesByNodes->hide();
436         AddButton->hide();
437         RemoveButton->hide();
438         Preview->show();
439         break;
440       }
441     }
442   
443   //connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
444 }
445
446 //=================================================================================
447 // function : ClickOnPreview()
448 // purpose  :
449 //=================================================================================
450 void SMESHGUI_CreatePolyhedralVolumeDlg::ClickOnPreview(bool theToggled){
451   Preview->setChecked(theToggled);
452   displaySimulation();
453 }
454
455 //=================================================================================
456 // function : ClickOnApply()
457 // purpose  :
458 //=================================================================================
459 void SMESHGUI_CreatePolyhedralVolumeDlg::ClickOnApply()
460 {
461   if ( myNbOkElements>0 && !mySMESHGUI->isActiveStudyLocked())
462     {
463       busy = true;
464       if (GetConstructorId() == 0)
465         {
466           SMESH::long_array_var anIdsOfNodes = new SMESH::long_array;
467           SMESH::long_array_var aQuantities  = new SMESH::long_array;
468
469           aQuantities->length( myFacesByNodes->count() );
470
471           TColStd_ListOfInteger aNodesIds;
472
473           QListBoxItem* anItem;
474           int aNbQuantities = 0;
475           for (anItem = myFacesByNodes->firstItem(); anItem != 0; anItem = anItem->next()) {
476             QStringList anIds = QStringList::split(" ", anItem->text());
477             int aNbNodesInFace = 0;
478             for (QStringList::iterator it = anIds.begin(); it != anIds.end(); ++it, ++aNbNodesInFace)
479               aNodesIds.Append( (*it).toInt() );
480
481             aQuantities[aNbQuantities++] = aNbNodesInFace;
482           }
483
484           anIdsOfNodes->length(aNodesIds.Extent());
485
486           int aNbIdsOfNodes = 0;
487           TColStd_ListIteratorOfListOfInteger It;
488           It.Initialize(aNodesIds);
489           for(;It.More();It.Next())
490             anIdsOfNodes[aNbIdsOfNodes++] = It.Value();
491             
492           try{
493             SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
494             QApplication::setOverrideCursor(Qt::waitCursor);
495             aMeshEditor->AddPolyhedralVolume(anIdsOfNodes, aQuantities);
496             QApplication::restoreOverrideCursor();
497           }catch(SALOME::SALOME_Exception& exc){
498             INFOS("Follow exception was cought:\n\t"<<exc.details.text);
499           }catch(std::exception& exc){
500             INFOS("Follow exception was cought:\n\t"<<exc.what());
501           }catch(...){
502             INFOS("Unknown exception was cought !!!");
503           }
504         }
505       else if (GetConstructorId() == 1)
506         {
507           SMESH::long_array_var anIdsOfFaces = new SMESH::long_array;
508           
509           QStringList aListId = QStringList::split( " ", myEditCurrentArgument->text() );
510           anIdsOfFaces->length(aListId.count());
511           for ( int i = 0; i < aListId.count(); i++ )
512             anIdsOfFaces[i] = aListId[i].toInt();
513           
514           try{
515             SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
516             QApplication::setOverrideCursor(Qt::waitCursor);
517             aMeshEditor->AddPolyhedralVolumeByFaces(anIdsOfFaces);
518             QApplication::restoreOverrideCursor();
519           }catch(SALOME::SALOME_Exception& exc){
520             INFOS("Follow exception was cought:\n\t"<<exc.details.text);
521           }catch(std::exception& exc){
522             INFOS("Follow exception was cought:\n\t"<<exc.what());
523           }catch(...){
524             INFOS("Unknown exception was cought !!!");
525           }
526         }
527       
528       SALOME_ListIO aList;
529       mySelectionMgr->setSelectedObjects( aList );
530       mySimulation->SetVisibility(false);
531       SMESH::UpdateView();
532       ConstructorsClicked( GetConstructorId() );
533       busy = false;
534     }
535 }
536
537 //=================================================================================
538 // function : ClickOnOk()
539 // purpose  :
540 //=================================================================================
541 void SMESHGUI_CreatePolyhedralVolumeDlg::ClickOnOk()
542 {
543   ClickOnApply() ;
544   ClickOnCancel() ;
545 }
546
547         
548 //=================================================================================
549 // function : ClickOnCancel()
550 // purpose  :
551 //=================================================================================
552 void SMESHGUI_CreatePolyhedralVolumeDlg::ClickOnCancel()
553 {
554   mySelectionMgr->clearFilters();
555   SALOME_ListIO aList;
556   mySelectionMgr->setSelectedObjects( aList );
557   SMESH::SetPointRepresentation(false);
558   mySimulation->SetVisibility(false);
559   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
560     aViewWindow->SetSelectionMode( ActorSelection );
561   disconnect( mySelectionMgr, 0, this, 0 );
562   mySMESHGUI->ResetState() ;
563   reject() ;
564 }
565
566 //=======================================================================
567 //function : onTextChange
568 //purpose  : 
569 //=======================================================================
570
571 void SMESHGUI_CreatePolyhedralVolumeDlg::onTextChange(const QString& theNewText)
572 {
573   if ( busy ) return;
574   busy = true;
575
576   mySimulation->SetVisibility(false);
577
578   SMDS_Mesh* aMesh = 0;
579   if ( myActor )
580     aMesh = myActor->GetObject()->GetMesh();
581
582   if (GetConstructorId() == 0)
583   {
584     if ( aMesh ) {
585       TColStd_MapOfInteger newIndices;
586       
587       QStringList aListId = QStringList::split( " ", theNewText, false);
588       for ( int i = 0; i < aListId.count(); i++ ) {
589         const SMDS_MeshNode * n = aMesh->FindNode( aListId[ i ].toInt() );
590         if ( n ) {
591           newIndices.Add(n->GetID());
592           myNbOkElements++;
593         }
594       }
595       
596       mySelector->AddOrRemoveIndex( myActor->getIO(), newIndices, false );
597       
598       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
599         aViewWindow->highlight( myActor->getIO(), true, true );
600       
601       if ( myNbOkElements>0 && aListId.count()>=3)
602         AddButton->setEnabled(true);
603       else
604         AddButton->setEnabled(false);
605       
606       displaySimulation();
607     }
608   } else if (GetConstructorId() == 1)
609   {
610     myNbOkElements = 0;
611     buttonOk->setEnabled( false );
612     buttonApply->setEnabled( false );
613       
614       // check entered ids of faces and hilight them
615     QStringList aListId;
616     if ( aMesh ) {
617       TColStd_MapOfInteger newIndices;
618       
619       aListId = QStringList::split( " ", theNewText, false);
620
621       for ( int i = 0; i < aListId.count(); i++ ) {
622         const SMDS_MeshElement * e = aMesh->FindElement( aListId[ i ].toInt() );
623         if ( e ) {
624           newIndices.Add(e->GetID());
625           myNbOkElements++;  
626         }
627       }
628
629       mySelector->AddOrRemoveIndex( myActor->getIO(), newIndices, false );
630       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
631         aViewWindow->highlight( myActor->getIO(), true, true );
632       
633       if ( myNbOkElements ) {
634         if (aListId.count()>1){ 
635           buttonOk->setEnabled( true );
636           buttonApply->setEnabled( true );
637         }
638         else{
639           buttonOk->setEnabled( false );
640           buttonApply->setEnabled( false );
641         }
642         displaySimulation();
643       }
644     }
645   }
646   busy = false;
647 }
648
649 //=================================================================================
650 // function : SelectionIntoArgument()
651 // purpose  : Called when selection as changed or other case
652 //=================================================================================
653 void SMESHGUI_CreatePolyhedralVolumeDlg::SelectionIntoArgument()
654 {
655   if ( busy ) return;
656   
657   // clear
658   
659   if (GetConstructorId() == 1 || myFacesByNodes->count() <= 1)
660     {
661       myNbOkElements = 0;
662       AddButton->setEnabled(false);
663       buttonOk->setEnabled( false );
664       buttonApply->setEnabled( false );
665     }
666
667   myActor = 0;
668
669   busy = true;
670   myEditCurrentArgument->setText( "" );
671   busy = false;
672   if ( !GroupButtons->isEnabled() ) // inactive
673     return;
674   
675   mySimulation->SetVisibility(false);
676   
677   // get selected mesh
678   
679   SALOME_ListIO selected;
680   mySelectionMgr->selectedObjects( selected );
681   int nbSel = selected.Extent();
682   if(nbSel != 1){
683     return;
684   }
685   
686   myMesh = SMESH::GetMeshByIO( selected.First() );
687   if ( myMesh->_is_nil() )
688     return;
689   
690   myActor = SMESH::FindActorByObject(myMesh);
691   if ( !myActor )
692     return;
693   
694   // get selected nodes/faces
695   QString aString = "";
696   switch(GetConstructorId()){
697   case 0:{
698     int anbNodes = SMESH::GetNameOfSelectedNodes(mySelector, myActor->getIO(), aString);
699     if (anbNodes >= 3)
700       AddButton->setEnabled(true);
701     else if (anbNodes < 3){
702       AddButton->setEnabled(false);
703     }
704     busy = true;
705     myEditCurrentArgument->setText( aString );
706     busy = false;
707     break;
708   }
709   case 1:{
710     // get selected faces
711     int aNbFaces = SMESH::GetNameOfSelectedElements(mySelector, myActor->getIO(), aString);
712     if (aNbFaces<=1){
713       buttonOk->setEnabled( false );
714       buttonApply->setEnabled( false );
715     } else {
716       buttonOk->setEnabled( true );
717       buttonApply->setEnabled( true );
718     }
719     busy = true;
720     myEditCurrentArgument->setText( aString );
721     busy = false;
722     
723     // OK
724     myNbOkElements = 1;
725     break;
726   }
727   default: return;
728   }
729   
730   displaySimulation();
731 }
732
733 //=======================================================================
734 //function : displaySimulation
735 //purpose  : 
736 //=======================================================================
737 void SMESHGUI_CreatePolyhedralVolumeDlg::displaySimulation()
738 {
739   if ( (myNbOkElements || AddButton->isEnabled()) && GroupButtons->isEnabled() && myActor)
740     {
741       SMESH::TPolySimulation::TVTKIds aVTKIds;
742       vtkIdType aType = VTK_CONVEX_POINT_SET ;
743       if (GetConstructorId() == 0){
744         if(!Preview->isChecked()) myActor->SetEntityMode(SMESH_Actor::eFaces);
745         else myActor->SetEntityMode(SMESH_Actor::eVolumes);
746         if (!AddButton->isEnabled()){
747           QListBoxItem* anItem;
748           mySimulation->ResetGrid(true);
749           for (anItem = myFacesByNodes->firstItem(); anItem != 0; anItem = anItem->next()) {
750             QStringList anIds = QStringList::split(" ", anItem->text());
751             SMESH::TPolySimulation::TVTKIds aVTKIds_faces;
752             for (QStringList::iterator it = anIds.begin(); it != anIds.end(); ++it){
753               vtkIdType aId = myActor->GetObject()->GetNodeVTKId( (*it).toInt() ) ;
754               aVTKIds.push_back(aId);
755               aVTKIds_faces.push_back(aId);
756             }
757             if(!Preview->isChecked()){
758               aType = VTK_POLYGON;
759               mySimulation->SetPosition(myActor, aType, aVTKIds_faces,false);
760             }
761           }
762           if(myFacesByNodes->count() == 0){
763             mySimulation->SetVisibility(false);
764           } else {
765             mySimulation->SetVisibility(true);
766           }
767           if(Preview->isChecked()){
768             mySimulation->SetPosition(myActor, aType, aVTKIds);
769           }
770         } else {
771           // add ids from edit line
772           QStringList anEditIds = QStringList::split( " ", myEditCurrentArgument->text(), false);
773           myActor->SetEntityMode(SMESH_Actor::eFaces);
774           for ( int i = 0; i < anEditIds.count(); i++ )
775             aVTKIds.push_back( myActor->GetObject()->GetNodeVTKId( anEditIds[ i ].toInt() ));
776           aType = VTK_POLYGON;
777           mySimulation->SetPosition(myActor, aType, aVTKIds);
778         }
779       }else if(GetConstructorId() == 1){
780         SMDS_Mesh* aMesh = 0;
781         if ( myActor ){
782           aMesh = myActor->GetObject()->GetMesh();
783           if (Preview->isChecked())
784             myActor->SetEntityMode(SMESH_Actor::eVolumes);
785           else
786             myActor->SetEntityMode(SMESH_Actor::eFaces);
787         }
788         if ( aMesh ) {
789           QStringList aListId = QStringList::split( " ", myEditCurrentArgument->text(), false);
790           for ( int i = 0; i < aListId.count(); i++ )
791             {
792               const SMDS_MeshElement * anElem = aMesh->FindElement( aListId[ i ].toInt() );
793               if ( !anElem )
794                 return;
795               
796               SMDS_ElemIteratorPtr anIter = anElem->nodesIterator();
797               SMESH::TPolySimulation::TVTKIds aVTKIds_faces;
798               while( anIter->more() )
799                 if ( const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next() ){
800                   vtkIdType aId = myActor->GetObject()->GetNodeVTKId( aNode->GetID() ) ;
801                   aVTKIds.push_back(aId);
802                   aVTKIds_faces.push_back(aId);
803                 }
804               if(!Preview->isChecked()){
805                 aType = VTK_POLYGON;
806                 mySimulation->SetPosition(myActor, aType, aVTKIds_faces);
807               }
808             }
809           if(Preview->isChecked())
810             mySimulation->SetPosition(myActor, aType, aVTKIds);
811         }
812       }
813       SMESH::UpdateView();
814     }
815 }
816
817 //=================================================================================
818 // function : SetEditCurrentArgument()
819 // purpose  :
820 //=================================================================================
821 void SMESHGUI_CreatePolyhedralVolumeDlg::SetEditCurrentArgument()
822 {
823   QPushButton* send = (QPushButton*)sender();
824   if(send == SelectElementsButton) {
825     LineEditElements->setFocus() ;
826     myEditCurrentArgument = LineEditElements;
827   }
828   SelectionIntoArgument();
829 }
830
831 //=================================================================================
832 // function : DeactivateActiveDialog()
833 // purpose  :
834 //=================================================================================
835 void SMESHGUI_CreatePolyhedralVolumeDlg::DeactivateActiveDialog()
836 {
837   if ( GroupConstructors->isEnabled() ) {
838     GroupConstructors->setEnabled(false) ;
839     GroupContent->setEnabled(false) ;
840     GroupButtons->setEnabled(false) ;
841     mySimulation->SetVisibility(false);
842     mySMESHGUI->ResetState() ;    
843     mySMESHGUI->SetActiveDialogBox(0) ;
844   }
845 }
846
847
848 //=================================================================================
849 // function : ActivateThisDialog()
850 // purpose  :
851 //=================================================================================
852 void SMESHGUI_CreatePolyhedralVolumeDlg::ActivateThisDialog()
853 {
854   /* Emit a signal to deactivate the active dialog */
855   mySMESHGUI->EmitSignalDeactivateDialog() ;   
856   GroupConstructors->setEnabled(true) ;
857   GroupContent->setEnabled(true) ;
858   GroupButtons->setEnabled(true) ;
859   
860   mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
861
862   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
863     aViewWindow->SetSelectionMode( FaceSelection );
864   SelectionIntoArgument();
865 }
866
867
868 //=================================================================================
869 // function : enterEvent()
870 // purpose  :
871 //=================================================================================
872 void SMESHGUI_CreatePolyhedralVolumeDlg::enterEvent(QEvent* e)
873 {
874   if ( GroupConstructors->isEnabled() )
875     return ;  
876   ActivateThisDialog() ;
877 }
878
879
880 //=================================================================================
881 // function : closeEvent()
882 // purpose  :
883 //=================================================================================
884 void SMESHGUI_CreatePolyhedralVolumeDlg::closeEvent( QCloseEvent* e )
885 {
886   /* same than click on cancel button */
887   this->ClickOnCancel() ;
888 }
889
890
891 //=======================================================================
892 //function : hideEvent
893 //purpose  : caused by ESC key
894 //=======================================================================
895
896 void SMESHGUI_CreatePolyhedralVolumeDlg::hideEvent ( QHideEvent * e )
897 {
898   if ( !isMinimized() )
899     ClickOnCancel();
900 }
901
902
903 //=================================================================================
904 // function : GetConstructorId()
905 // purpose  : 
906 //=================================================================================
907 int SMESHGUI_CreatePolyhedralVolumeDlg::GetConstructorId()
908
909   if ( GroupConstructors != NULL && GroupConstructors->selected() != NULL )
910     return GroupConstructors->id( GroupConstructors->selected() );
911   return -1;
912 }
913
914 //=================================================================================
915 // function : onAdd()
916 // purpose  :
917 //=================================================================================
918 void SMESHGUI_CreatePolyhedralVolumeDlg::onAdd()
919 {
920   SALOME_ListIO selected;
921   mySelectionMgr->selectedObjects( selected );
922   int aNbSel = selected.Extent();
923   if (aNbSel == 0 || !myActor || myMesh->_is_nil()) return;
924
925   busy = true;
926
927   if ( !(myEditCurrentArgument->text().isEmpty()) )
928     {
929       myFacesByNodes->insertItem(myEditCurrentArgument->text());
930       //myFacesByNodes->setSelected(myFacesByNodes->count() - 1, true);
931       myNbOkElements = 1;
932       myEditCurrentArgument->clear();
933       AddButton->setEnabled(false);
934       buttonOk->setEnabled( true );
935       if(myFacesByNodes->count()>1) buttonApply->setEnabled( true );
936     }
937   busy = false;
938   onListSelectionChanged();
939   displaySimulation();
940 }
941
942 //=================================================================================
943 // function : onRemove()
944 // purpose  :
945 //=================================================================================
946 void SMESHGUI_CreatePolyhedralVolumeDlg::onRemove()
947 {
948   busy = true;
949   for (int i = myFacesByNodes->count(); i > 0; i--) {
950     if (myFacesByNodes->isSelected(i-1)) {
951       myFacesByNodes->removeItem(i-1);
952       myNbOkElements = 1;
953     }
954   }
955   if (myFacesByNodes->count() < 1){
956     RemoveButton->setEnabled(false);
957     buttonOk->setEnabled( false );
958     buttonApply->setEnabled( false );
959   } 
960   else if (myFacesByNodes->count() == 1){
961     buttonOk->setEnabled( false );
962     buttonApply->setEnabled( false );
963   }
964   busy = false;
965   displaySimulation();
966 }
967
968 //=================================================================================
969 // function : onListSelectionChanged()
970 // purpose  : Called when selection in element list is changed
971 //=================================================================================
972 void SMESHGUI_CreatePolyhedralVolumeDlg::onListSelectionChanged()
973 {
974   if (busy || !myActor) return;
975   busy = true;
976   bool isSelected=false;
977   SALOME_ListIO aList;
978   mySelectionMgr->setSelectedObjects( aList );
979   TColStd_MapOfInteger aIndexes;
980   QListBoxItem* anItem;
981   for (anItem = myFacesByNodes->firstItem(); anItem != 0; anItem = anItem->next()) {
982     if (anItem->isSelected()) {
983       isSelected = true;
984       QStringList anIds = QStringList::split(" ", anItem->text());
985       for (QStringList::iterator it = anIds.begin(); it != anIds.end(); ++it)
986         aIndexes.Add((*it).toInt());
987     }
988   }
989   if(isSelected) RemoveButton->setEnabled(true);
990   else RemoveButton->setEnabled(false);
991   mySelector->AddOrRemoveIndex(myActor->getIO(), aIndexes, true );
992   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
993     aViewWindow->highlight( myActor->getIO(), true, true );
994   mySelectionMgr->clearFilters(); 
995   aList.Append( myActor->getIO() );
996   mySelectionMgr->setSelectedObjects( aList );
997   
998   busy = false;
999 }