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