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