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