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