Salome HOME
The selections of elements corrected.
[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       TColStd_MapOfInteger newIndices;
587       
588       QStringList aListId = QStringList::split( " ", theNewText, false);
589       for ( int i = 0; i < aListId.count(); i++ ) {
590         const SMDS_MeshNode * n = aMesh->FindNode( aListId[ i ].toInt() );
591         if ( n ) {
592           newIndices.Add(n->GetID());
593           myNbOkElements++;
594         }
595       }
596       
597       mySelector->AddOrRemoveIndex( myActor->getIO(), newIndices, false );
598       myViewWindow->highlight( myActor->getIO(), true, true );
599       
600       if ( myNbOkElements>0 && aListId.count()>=3)
601         AddButton->setEnabled(true);
602       else
603         AddButton->setEnabled(false);
604       
605       displaySimulation();
606     }
607   } else if (GetConstructorId() == 1)
608   {
609     myNbOkElements = 0;
610     buttonOk->setEnabled( false );
611     buttonApply->setEnabled( false );
612       
613       // check entered ids of faces and hilight them
614     QStringList aListId;
615     if ( aMesh ) {
616       TColStd_MapOfInteger newIndices;
617       
618       aListId = QStringList::split( " ", theNewText, false);
619
620       for ( int i = 0; i < aListId.count(); i++ ) {
621         const SMDS_MeshElement * e = aMesh->FindElement( aListId[ i ].toInt() );
622         if ( e ) {
623           newIndices.Add(e->GetID());
624           myNbOkElements++;  
625         }
626       }
627
628       mySelector->AddOrRemoveIndex( myActor->getIO(), newIndices, false );
629       myViewWindow->highlight( myActor->getIO(), true, true );
630       
631       if ( myNbOkElements ) {
632         if (aListId.count()>1){ 
633           buttonOk->setEnabled( true );
634           buttonApply->setEnabled( true );
635         }
636         else{
637           buttonOk->setEnabled( false );
638           buttonApply->setEnabled( false );
639         }
640         displaySimulation();
641       }
642     }
643   }
644   busy = false;
645 }
646
647 //=================================================================================
648 // function : SelectionIntoArgument()
649 // purpose  : Called when selection as changed or other case
650 //=================================================================================
651 void SMESHGUI_CreatePolyhedralVolumeDlg::SelectionIntoArgument()
652 {
653   if ( busy ) return;
654   
655   // clear
656   
657   if (GetConstructorId() == 1 || myFacesByNodes->count() <= 1)
658     {
659       myNbOkElements = 0;
660       AddButton->setEnabled(false);
661       buttonOk->setEnabled( false );
662       buttonApply->setEnabled( false );
663     }
664
665   myActor = 0;
666
667   busy = true;
668   myEditCurrentArgument->setText( "" );
669   busy = false;
670   if ( !GroupButtons->isEnabled() ) // inactive
671     return;
672   
673   mySimulation->SetVisibility(false);
674   
675   // get selected mesh
676   
677   SALOME_ListIO selected;
678   mySelectionMgr->selectedObjects( selected );
679   int nbSel = selected.Extent();
680   if(nbSel != 1){
681     return;
682   }
683   
684   myMesh = SMESH::GetMeshByIO( selected.First() );
685   if ( myMesh->_is_nil() )
686     return;
687   
688   myActor = SMESH::FindActorByObject(myMesh);
689   if ( !myActor )
690     return;
691   
692   // get selected nodes/faces
693   QString aString = "";
694   switch(GetConstructorId()){
695   case 0:{
696     int anbNodes = SMESH::GetNameOfSelectedNodes(mySelector, myActor->getIO(), aString);
697     if (anbNodes >= 3)
698       AddButton->setEnabled(true);
699     else if (anbNodes < 3){
700       AddButton->setEnabled(false);
701     }
702     busy = true;
703     myEditCurrentArgument->setText( aString );
704     busy = false;
705     break;
706   }
707   case 1:{
708     // get selected faces
709     int aNbFaces = SMESH::GetNameOfSelectedElements(mySelector, myActor->getIO(), aString);
710     if (aNbFaces<=1){
711       buttonOk->setEnabled( false );
712       buttonApply->setEnabled( false );
713     } else {
714       buttonOk->setEnabled( true );
715       buttonApply->setEnabled( true );
716     }
717     busy = true;
718     myEditCurrentArgument->setText( aString );
719     busy = false;
720     
721     // OK
722     myNbOkElements = 1;
723     break;
724   }
725   default: return;
726   }
727   
728   displaySimulation();
729 }
730
731 //=======================================================================
732 //function : displaySimulation
733 //purpose  : 
734 //=======================================================================
735 void SMESHGUI_CreatePolyhedralVolumeDlg::displaySimulation()
736 {
737   if ( (myNbOkElements || AddButton->isEnabled()) && GroupButtons->isEnabled() && myActor)
738     {
739       SMESH::TPolySimulation::TVTKIds aVTKIds;
740       vtkIdType aType = VTK_CONVEX_POINT_SET ;
741       if (GetConstructorId() == 0){
742         if(!Preview->isChecked()) myActor->SetEntityMode(SMESH_Actor::eFaces);
743         else myActor->SetEntityMode(SMESH_Actor::eVolumes);
744         if (!AddButton->isEnabled()){
745           QListBoxItem* anItem;
746           mySimulation->ResetGrid(true);
747           for (anItem = myFacesByNodes->firstItem(); anItem != 0; anItem = anItem->next()) {
748             QStringList anIds = QStringList::split(" ", anItem->text());
749             SMESH::TPolySimulation::TVTKIds aVTKIds_faces;
750             for (QStringList::iterator it = anIds.begin(); it != anIds.end(); ++it){
751               vtkIdType aId = myActor->GetObject()->GetNodeVTKId( (*it).toInt() ) ;
752               aVTKIds.push_back(aId);
753               aVTKIds_faces.push_back(aId);
754             }
755             if(!Preview->isChecked()){
756               aType = VTK_POLYGON;
757               mySimulation->SetPosition(myActor, aType, aVTKIds_faces,false);
758             }
759           }
760           if(myFacesByNodes->count() == 0){
761             mySimulation->SetVisibility(false);
762           } else {
763             mySimulation->SetVisibility(true);
764           }
765           if(Preview->isChecked()){
766             mySimulation->SetPosition(myActor, aType, aVTKIds);
767           }
768         } else {
769           // add ids from edit line
770           QStringList anEditIds = QStringList::split( " ", myEditCurrentArgument->text(), false);
771           myActor->SetEntityMode(SMESH_Actor::eFaces);
772           for ( int i = 0; i < anEditIds.count(); i++ )
773             aVTKIds.push_back( myActor->GetObject()->GetNodeVTKId( anEditIds[ i ].toInt() ));
774           aType = VTK_POLYGON;
775           mySimulation->SetPosition(myActor, aType, aVTKIds);
776         }
777       }else if(GetConstructorId() == 1){
778         SMDS_Mesh* aMesh = 0;
779         if ( myActor ){
780           aMesh = myActor->GetObject()->GetMesh();
781           if (Preview->isChecked())
782             myActor->SetEntityMode(SMESH_Actor::eVolumes);
783           else
784             myActor->SetEntityMode(SMESH_Actor::eFaces);
785         }
786         if ( aMesh ) {
787           QStringList aListId = QStringList::split( " ", myEditCurrentArgument->text(), false);
788           for ( int i = 0; i < aListId.count(); i++ )
789             {
790               const SMDS_MeshElement * anElem = aMesh->FindElement( aListId[ i ].toInt() );
791               if ( !anElem )
792                 return;
793               
794               SMDS_ElemIteratorPtr anIter = anElem->nodesIterator();
795               SMESH::TPolySimulation::TVTKIds aVTKIds_faces;
796               while( anIter->more() )
797                 if ( const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next() ){
798                   vtkIdType aId = myActor->GetObject()->GetNodeVTKId( aNode->GetID() ) ;
799                   aVTKIds.push_back(aId);
800                   aVTKIds_faces.push_back(aId);
801                 }
802               if(!Preview->isChecked()){
803                 aType = VTK_POLYGON;
804                 mySimulation->SetPosition(myActor, aType, aVTKIds_faces);
805               }
806             }
807           if(Preview->isChecked())
808             mySimulation->SetPosition(myActor, aType, aVTKIds);
809         }
810       }
811       SMESH::UpdateView();
812     }
813 }
814
815 //=================================================================================
816 // function : SetEditCurrentArgument()
817 // purpose  :
818 //=================================================================================
819 void SMESHGUI_CreatePolyhedralVolumeDlg::SetEditCurrentArgument()
820 {
821   QPushButton* send = (QPushButton*)sender();
822   if(send == SelectElementsButton) {
823     LineEditElements->setFocus() ;
824     myEditCurrentArgument = LineEditElements;
825   }
826   SelectionIntoArgument();
827 }
828
829 //=================================================================================
830 // function : DeactivateActiveDialog()
831 // purpose  :
832 //=================================================================================
833 void SMESHGUI_CreatePolyhedralVolumeDlg::DeactivateActiveDialog()
834 {
835   if ( GroupConstructors->isEnabled() ) {
836     GroupConstructors->setEnabled(false) ;
837     GroupContent->setEnabled(false) ;
838     GroupButtons->setEnabled(false) ;
839     mySimulation->SetVisibility(false);
840     mySMESHGUI->ResetState() ;    
841     mySMESHGUI->SetActiveDialogBox(0) ;
842   }
843 }
844
845
846 //=================================================================================
847 // function : ActivateThisDialog()
848 // purpose  :
849 //=================================================================================
850 void SMESHGUI_CreatePolyhedralVolumeDlg::ActivateThisDialog()
851 {
852   /* Emit a signal to deactivate the active dialog */
853   mySMESHGUI->EmitSignalDeactivateDialog() ;   
854   GroupConstructors->setEnabled(true) ;
855   GroupContent->setEnabled(true) ;
856   GroupButtons->setEnabled(true) ;
857   
858   mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
859
860   myViewWindow->SetSelectionMode( FaceSelection );
861   SelectionIntoArgument();
862 }
863
864
865 //=================================================================================
866 // function : enterEvent()
867 // purpose  :
868 //=================================================================================
869 void SMESHGUI_CreatePolyhedralVolumeDlg::enterEvent(QEvent* e)
870 {
871   if ( GroupConstructors->isEnabled() )
872     return ;  
873   ActivateThisDialog() ;
874 }
875
876
877 //=================================================================================
878 // function : closeEvent()
879 // purpose  :
880 //=================================================================================
881 void SMESHGUI_CreatePolyhedralVolumeDlg::closeEvent( QCloseEvent* e )
882 {
883   /* same than click on cancel button */
884   this->ClickOnCancel() ;
885 }
886
887
888 //=======================================================================
889 //function : hideEvent
890 //purpose  : caused by ESC key
891 //=======================================================================
892
893 void SMESHGUI_CreatePolyhedralVolumeDlg::hideEvent ( QHideEvent * e )
894 {
895   if ( !isMinimized() )
896     ClickOnCancel();
897 }
898
899
900 //=================================================================================
901 // function : GetConstructorId()
902 // purpose  : 
903 //=================================================================================
904 int SMESHGUI_CreatePolyhedralVolumeDlg::GetConstructorId()
905
906   if ( GroupConstructors != NULL && GroupConstructors->selected() != NULL )
907     return GroupConstructors->id( GroupConstructors->selected() );
908   return -1;
909 }
910
911 //=================================================================================
912 // function : onAdd()
913 // purpose  :
914 //=================================================================================
915 void SMESHGUI_CreatePolyhedralVolumeDlg::onAdd()
916 {
917   SALOME_ListIO selected;
918   mySelectionMgr->selectedObjects( selected );
919   int aNbSel = selected.Extent();
920   if (aNbSel == 0 || !myActor || myMesh->_is_nil()) return;
921
922   busy = true;
923
924   if ( !(myEditCurrentArgument->text().isEmpty()) )
925     {
926       myFacesByNodes->insertItem(myEditCurrentArgument->text());
927       //myFacesByNodes->setSelected(myFacesByNodes->count() - 1, true);
928       myNbOkElements = 1;
929       myEditCurrentArgument->clear();
930       AddButton->setEnabled(false);
931       buttonOk->setEnabled( true );
932       if(myFacesByNodes->count()>1) buttonApply->setEnabled( true );
933     }
934   busy = false;
935   onListSelectionChanged();
936   displaySimulation();
937 }
938
939 //=================================================================================
940 // function : onRemove()
941 // purpose  :
942 //=================================================================================
943 void SMESHGUI_CreatePolyhedralVolumeDlg::onRemove()
944 {
945   busy = true;
946   for (int i = myFacesByNodes->count(); i > 0; i--) {
947     if (myFacesByNodes->isSelected(i-1)) {
948       myFacesByNodes->removeItem(i-1);
949       myNbOkElements = 1;
950     }
951   }
952   if (myFacesByNodes->count() < 1){
953     RemoveButton->setEnabled(false);
954     buttonOk->setEnabled( false );
955     buttonApply->setEnabled( false );
956   } 
957   else if (myFacesByNodes->count() == 1){
958     buttonOk->setEnabled( false );
959     buttonApply->setEnabled( false );
960   }
961   busy = false;
962   displaySimulation();
963 }
964
965 //=================================================================================
966 // function : onListSelectionChanged()
967 // purpose  : Called when selection in element list is changed
968 //=================================================================================
969 void SMESHGUI_CreatePolyhedralVolumeDlg::onListSelectionChanged()
970 {
971   if (busy || !myActor) return;
972   busy = true;
973   bool isSelected=false;
974   SALOME_ListIO aList;
975   mySelectionMgr->setSelectedObjects( aList );
976   TColStd_MapOfInteger aIndexes;
977   QListBoxItem* anItem;
978   for (anItem = myFacesByNodes->firstItem(); anItem != 0; anItem = anItem->next()) {
979     if (anItem->isSelected()) {
980       isSelected = true;
981       QStringList anIds = QStringList::split(" ", anItem->text());
982       for (QStringList::iterator it = anIds.begin(); it != anIds.end(); ++it)
983         aIndexes.Add((*it).toInt());
984     }
985   }
986   if(isSelected) RemoveButton->setEnabled(true);
987   else RemoveButton->setEnabled(false);
988   mySelector->AddOrRemoveIndex(myActor->getIO(), aIndexes, true );
989   myViewWindow->highlight( myActor->getIO(), true, true );
990   mySelectionMgr->clearFilters(); 
991   aList.Append( myActor->getIO() );
992   mySelectionMgr->setSelectedObjects( aList );
993   
994   busy = false;
995 }