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