Salome HOME
Prefernces.
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_CreatePolyhedralVolumeDlg.cxx
1 //  SMESH SMESHGUI : GUI for SMESH component
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SMESHGUI_CreatePolyhedralVolumeDlg.cxx
25 //  Author : Michael ZORIN
26 //  Module : SMESH
27 //  $Header: 
28
29 #include "SMESHGUI_CreatePolyhedralVolumeDlg.h"
30
31 #include "SMESHGUI.h"
32 #include "SMESHGUI_Utils.h"
33 #include "SMESHGUI_VTKUtils.h"
34 #include "SMESHGUI_MeshUtils.h"
35 #include "SMESHGUI_IdValidator.h"
36 #include "SMESH_ActorUtils.h"
37 #include "SMESHGUI_SpinBox.h"
38 #include "SMESH_Actor.h"
39 #include "SMESH_TypeFilter.hxx"
40 #include "SMDS_Mesh.hxx"
41
42 #include "VTKViewer_ViewWindow.h"
43
44 #include "SUIT_ResourceMgr.h"
45
46 #include "SalomeApp_Application.h"
47 #include "SalomeApp_Study.h"
48 #include "SUIT_Desktop.h"
49 #include "SUIT_MessageBox.h"
50 #include "SalomeApp_SelectionMgr.h"
51 #include "utilities.h"
52
53 #include "SVTK_ViewWindow.h"
54 #include "SVTK_Selector.h"
55
56 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
57 #include <TColStd_ListOfInteger.hxx>
58 #include <TColStd_ListIteratorOfListOfInteger.hxx>
59
60 #include <vtkCell.h>
61 #include <vtkIdList.h>
62 #include <vtkIntArray.h>
63 #include <vtkCellArray.h>
64 #include <vtkUnsignedCharArray.h>
65 #include <vtkUnstructuredGrid.h>
66 #include <vtkDataSetMapper.h>
67
68 // QT Includes
69 #include <qapplication.h>
70 #include <qbuttongroup.h>
71 #include <qgroupbox.h>
72 #include <qlabel.h>
73 #include <qlineedit.h>
74 #include <qpushbutton.h>
75 #include <qradiobutton.h>
76 #include <qcheckbox.h>
77 #include <qlayout.h>
78 #include <qspinbox.h> 
79 #include <qpixmap.h>
80 #include <qlistbox.h>
81
82 // IDL Headers
83 #include "SALOMEconfig.h"
84 #include CORBA_SERVER_HEADER(SMESH_Group)
85
86 using namespace std;
87
88 namespace SMESH{
89
90 class TPolySimulation{
91     SVTK_ViewWindow* myViewWindow;
92
93     SALOME_Actor *myPreviewActor;
94     vtkDataSetMapper* myMapper;
95     vtkUnstructuredGrid* myGrid;
96
97   public:
98
99     TPolySimulation(SalomeApp_Application* app)
100     {
101       SUIT_ViewManager* mgr = app->activeViewManager();
102       myViewWindow = mgr ? dynamic_cast<SVTK_ViewWindow*>( mgr->getActiveView() ) : NULL;
103
104       myGrid = vtkUnstructuredGrid::New();
105   
106       // Create and display actor
107       myMapper = vtkDataSetMapper::New();
108       myMapper->SetInput( myGrid );
109
110       myPreviewActor = SALOME_Actor::New();
111       myPreviewActor->PickableOff();
112       myPreviewActor->VisibilityOff();
113       myPreviewActor->SetMapper( myMapper );
114
115       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     myViewWindow( SMESH::GetViewWindow( theModule ) ),
196     mySelector( myViewWindow->GetSelector() )
197 {
198   QPixmap image0( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH",tr("ICON_SELECT")));
199
200   if ( !name )
201     setName( "SMESHGUI_CreatePolyhedralVolumeDlg" );
202   resize( 300, 185 ); 
203   setCaption( tr( "SMESH_CREATE_POLYHEDRAL_VOLUME_TITLE" ) );
204   setSizeGripEnabled( TRUE );
205   SMESHGUI_CreatePolyhedralVolumeDlgLayout = new QGridLayout( this ); 
206   SMESHGUI_CreatePolyhedralVolumeDlgLayout->setSpacing( 6 );
207   SMESHGUI_CreatePolyhedralVolumeDlgLayout->setMargin( 11 );
208
209   /***************************************************************/
210   GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
211   GroupConstructors->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)5, (QSizePolicy::SizeType)0, 0, 0, GroupConstructors->sizePolicy().hasHeightForWidth() ) );
212   GroupConstructors->setTitle( tr( "SMESH_ELEMENTS_TYPE" ) );
213   GroupConstructors->setExclusive( TRUE );
214   GroupConstructors->setColumnLayout(0, Qt::Vertical );
215   GroupConstructors->layout()->setSpacing( 0 );
216   GroupConstructors->layout()->setMargin( 0 );
217   GroupConstructorsLayout = new QGridLayout( GroupConstructors->layout() );
218   GroupConstructorsLayout->setAlignment( Qt::AlignTop );
219   GroupConstructorsLayout->setSpacing( 6 );
220   GroupConstructorsLayout->setMargin( 11 );
221   RadioButton1= new QRadioButton( GroupConstructors, "RadioButton1" );
222   RadioButton1->setText( tr( "MESH_NODE"  ) );
223   GroupConstructorsLayout->addWidget( RadioButton1, 0, 0 );
224   RadioButton2= new QRadioButton( GroupConstructors, "RadioButton2" );
225   RadioButton2->setText( tr( "SMESH_FACE"  ) );
226   GroupConstructorsLayout->addWidget( RadioButton2, 0, 2  );
227   SMESHGUI_CreatePolyhedralVolumeDlgLayout->addWidget( GroupConstructors, 0, 0 );
228   
229   /***************************************************************/
230   GroupButtons = new QGroupBox( this, "GroupButtons" );
231   GroupButtons->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)7, (QSizePolicy::SizeType)0, 0, 0, GroupButtons->sizePolicy().hasHeightForWidth() ) );
232   GroupButtons->setGeometry( QRect( 10, 10, 281, 48 ) ); 
233   GroupButtons->setTitle( tr( ""  ) );
234   GroupButtons->setColumnLayout(0, Qt::Vertical );
235   GroupButtons->layout()->setSpacing( 0 );
236   GroupButtons->layout()->setMargin( 0 );
237   GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
238   GroupButtonsLayout->setAlignment( Qt::AlignTop );
239   GroupButtonsLayout->setSpacing( 6 );
240   GroupButtonsLayout->setMargin( 11 );
241   buttonCancel = new QPushButton( GroupButtons, "cancel" );
242   buttonCancel->setText( tr( "SMESH_BUT_CLOSE"  ) );
243   buttonCancel->setAutoDefault( TRUE );
244   GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
245   buttonApply = new QPushButton( GroupButtons, "apply" );
246   buttonApply->setText( tr( "SMESH_BUT_APPLY"  ) );
247   buttonApply->setAutoDefault( TRUE );
248   GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
249   QSpacerItem* spacer_9 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
250   GroupButtonsLayout->addItem( spacer_9, 0, 2 );
251   buttonOk = new QPushButton( GroupButtons, "ok" );
252   buttonOk->setText( tr( "SMESH_BUT_OK"  ) );
253   buttonOk->setAutoDefault( TRUE );
254   buttonOk->setDefault( TRUE );
255   GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
256   SMESHGUI_CreatePolyhedralVolumeDlgLayout->addWidget( GroupButtons, 2, 0 );
257
258   /***************************************************************/
259   GroupContent = new QGroupBox( this, "GroupContent" );
260   GroupContent->setTitle( tr( "SMESH_CONTENT" ) );
261   GroupContent->setColumnLayout(0, Qt::Vertical );
262   GroupContent->layout()->setSpacing( 0 );
263   GroupContent->layout()->setMargin( 0 );
264   GroupContentLayout = new QGridLayout( GroupContent->layout() );
265   GroupContentLayout->setAlignment( Qt::AlignTop );
266   GroupContentLayout->setSpacing( 6 );
267   GroupContentLayout->setMargin( 11 );
268
269   QFrame* anIds = new QFrame(GroupContent, "anIds");
270   QGridLayout* aLayout = new QGridLayout(anIds, 1, 3);
271   aLayout->setSpacing(6);
272   aLayout->setAutoAdd(false);
273   
274   TextLabelIds = new QLabel( anIds, "TextLabelIds" );
275   TextLabelIds->setText( tr( "SMESH_ID_NODES" ) );
276   TextLabelIds->setFixedWidth(74);
277   aLayout->addWidget( TextLabelIds, 0, 0 );
278
279   SelectElementsButton  = new QPushButton( anIds, "SelectElementsButton" );
280   SelectElementsButton->setText( tr( "" ) );
281   SelectElementsButton->setPixmap( image0 );
282   SelectElementsButton->setToggleButton( FALSE );
283   aLayout->addWidget( SelectElementsButton, 0, 1 );
284
285   LineEditElements  = new QLineEdit( anIds, "LineEditElements" );
286   LineEditElements->setValidator( new SMESHGUI_IdValidator( this, "validator" ));
287   aLayout->addWidget( LineEditElements, 0, 2 );
288   
289   GroupContentLayout->addMultiCellWidget(anIds, 0, 0, 0, 1);
290
291   myFacesByNodesLabel = new QLabel(GroupContent, "faces by nodes label");
292   myFacesByNodesLabel->setText(tr("FACES_BY_NODES"));
293   GroupContentLayout->addWidget( myFacesByNodesLabel, 1, 0 );
294
295   myFacesByNodes = new QListBox(GroupContent, "faces by nodes list");
296   myFacesByNodes->setSelectionMode(QListBox::Extended);
297   myFacesByNodes->setMinimumHeight(150);
298   GroupContentLayout->addMultiCellWidget( myFacesByNodes, 2, 4, 0, 0 );
299
300   AddButton = new QPushButton(GroupContent, "add");
301   AddButton->setText(tr("SMESH_BUT_ADD"));
302   AddButton->setMaximumWidth(85);
303   GroupContentLayout->addWidget( AddButton, 2, 1 );
304
305   RemoveButton = new QPushButton(GroupContent, "remove");
306   RemoveButton->setText(tr("SMESH_BUT_REMOVE"));
307   RemoveButton->setMaximumWidth(85);
308   GroupContentLayout->addWidget( RemoveButton, 3, 1 );
309
310   Preview = new QCheckBox( GroupContent, "Preview" );
311   Preview->setText( tr( "SMESH_POLYEDRE_PREVIEW"  ) );
312   GroupContentLayout->addWidget( Preview , 5, 0 );
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 aList;
388   mySelectionMgr->setSelectedObjects( aList );
389   myEditCurrentArgument->clear();
390   myNbOkElements = 0;
391   buttonApply->setEnabled(false);
392   buttonOk->setEnabled(false);
393   mySimulation->SetVisibility(false);
394
395   switch(constructorId)
396     {
397     case 0 :
398       { 
399         if ( myActor ){
400           myActor->SetPointRepresentation(true);
401           myActor->SetEntityMode(SMESH_Actor::eVolumes);
402           myActor->SetRepresentation(SMESH_Actor::eSurface);
403         }
404         else
405           SMESH::SetPointRepresentation(true);
406         myViewWindow->SetSelectionMode(NodeSelection);
407         
408         AddButton->setEnabled(false);
409         RemoveButton->setEnabled(false);
410         TextLabelIds->setText( tr( "SMESH_ID_NODES" ) );
411         myFacesByNodesLabel->show();
412         myFacesByNodes->clear();
413         myFacesByNodes->show();
414         AddButton->show();
415         RemoveButton->show();
416         Preview->show();
417         break;
418       }
419     case 1 :
420       {
421         if( myActor ){
422           myActor->SetPointRepresentation(false);
423           myActor->SetEntityMode(SMESH_Actor::eFaces);
424           myActor->SetEntityMode(SMESH_Actor::eVolumes);
425           myActor->SetRepresentation(SMESH_Actor::eSurface);
426         } else {
427           SMESH::SetPointRepresentation(false);
428         }
429         myViewWindow->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   myViewWindow->SetSelectionMode( ActorSelection );
558   disconnect( mySelectionMgr, 0, this, 0 );
559   mySMESHGUI->ResetState() ;
560   reject() ;
561 }
562
563 //=======================================================================
564 //function : onTextChange
565 //purpose  : 
566 //=======================================================================
567
568 void SMESHGUI_CreatePolyhedralVolumeDlg::onTextChange(const QString& theNewText)
569 {
570   if ( busy ) return;
571   busy = true;
572
573   mySimulation->SetVisibility(false);
574
575   SMDS_Mesh* aMesh = 0;
576   if ( myActor )
577     aMesh = myActor->GetObject()->GetMesh();
578
579   if (GetConstructorId() == 0)
580   {
581     if ( aMesh ) {
582       TColStd_MapOfInteger newIndices;
583       
584       QStringList aListId = QStringList::split( " ", theNewText, false);
585       for ( int i = 0; i < aListId.count(); i++ ) {
586         const SMDS_MeshNode * n = aMesh->FindNode( aListId[ i ].toInt() );
587         if ( n ) {
588           newIndices.Add(n->GetID());
589           myNbOkElements++;
590         }
591       }
592       
593       mySelector->AddOrRemoveIndex( myActor->getIO(), newIndices, false );
594       myViewWindow->highlight( myActor->getIO(), true, true );
595       
596       if ( myNbOkElements>0 && aListId.count()>=3)
597         AddButton->setEnabled(true);
598       else
599         AddButton->setEnabled(false);
600       
601       displaySimulation();
602     }
603   } else if (GetConstructorId() == 1)
604   {
605     myNbOkElements = 0;
606     buttonOk->setEnabled( false );
607     buttonApply->setEnabled( false );
608       
609       // check entered ids of faces and hilight them
610     QStringList aListId;
611     if ( aMesh ) {
612       TColStd_MapOfInteger newIndices;
613       
614       aListId = QStringList::split( " ", theNewText, false);
615
616       for ( int i = 0; i < aListId.count(); i++ ) {
617         const SMDS_MeshElement * e = aMesh->FindElement( aListId[ i ].toInt() );
618         if ( e ) {
619           newIndices.Add(e->GetID());
620           myNbOkElements++;  
621         }
622       }
623
624       mySelector->AddOrRemoveIndex( myActor->getIO(), newIndices, false );
625       myViewWindow->highlight( myActor->getIO(), true, true );
626       
627       if ( myNbOkElements ) {
628         if (aListId.count()>1){ 
629           buttonOk->setEnabled( true );
630           buttonApply->setEnabled( true );
631         }
632         else{
633           buttonOk->setEnabled( false );
634           buttonApply->setEnabled( false );
635         }
636         displaySimulation();
637       }
638     }
639   }
640   busy = false;
641 }
642
643 //=================================================================================
644 // function : SelectionIntoArgument()
645 // purpose  : Called when selection as changed or other case
646 //=================================================================================
647 void SMESHGUI_CreatePolyhedralVolumeDlg::SelectionIntoArgument()
648 {
649   if ( busy ) return;
650   
651   // clear
652   
653   if (GetConstructorId() == 1 || myFacesByNodes->count() <= 1)
654     {
655       myNbOkElements = 0;
656       AddButton->setEnabled(false);
657       buttonOk->setEnabled( false );
658       buttonApply->setEnabled( false );
659     }
660
661   myActor = 0;
662
663   busy = true;
664   myEditCurrentArgument->setText( "" );
665   busy = false;
666   if ( !GroupButtons->isEnabled() ) // inactive
667     return;
668   
669   mySimulation->SetVisibility(false);
670   
671   // get selected mesh
672   
673   SALOME_ListIO selected;
674   mySelectionMgr->selectedObjects( selected );
675   int nbSel = selected.Extent();
676   if(nbSel != 1){
677     return;
678   }
679   
680   myMesh = SMESH::GetMeshByIO( selected.First() );
681   if ( myMesh->_is_nil() )
682     return;
683   
684   myActor = SMESH::FindActorByObject(myMesh);
685   if ( !myActor )
686     return;
687   
688   // get selected nodes/faces
689   QString aString = "";
690   switch(GetConstructorId()){
691   case 0:{
692     int anbNodes = SMESH::GetNameOfSelectedNodes(mySelector, myActor->getIO(), aString);
693     if (anbNodes >= 3)
694       AddButton->setEnabled(true);
695     else if (anbNodes < 3){
696       AddButton->setEnabled(false);
697     }
698     busy = true;
699     myEditCurrentArgument->setText( aString );
700     busy = false;
701     break;
702   }
703   case 1:{
704     // get selected faces
705     int aNbFaces = SMESH::GetNameOfSelectedElements(mySelector, myActor->getIO(), aString);
706     if (aNbFaces<=1){
707       buttonOk->setEnabled( false );
708       buttonApply->setEnabled( false );
709     } else {
710       buttonOk->setEnabled( true );
711       buttonApply->setEnabled( true );
712     }
713     busy = true;
714     myEditCurrentArgument->setText( aString );
715     busy = false;
716     
717     // OK
718     myNbOkElements = 1;
719     break;
720   }
721   default: return;
722   }
723   
724   displaySimulation();
725 }
726
727 //=======================================================================
728 //function : displaySimulation
729 //purpose  : 
730 //=======================================================================
731 void SMESHGUI_CreatePolyhedralVolumeDlg::displaySimulation()
732 {
733   if ( (myNbOkElements || AddButton->isEnabled()) && GroupButtons->isEnabled() && myActor)
734     {
735       SMESH::TPolySimulation::TVTKIds aVTKIds;
736       vtkIdType aType = VTK_CONVEX_POINT_SET ;
737       if (GetConstructorId() == 0){
738         if(!Preview->isChecked()) myActor->SetEntityMode(SMESH_Actor::eFaces);
739         else myActor->SetEntityMode(SMESH_Actor::eVolumes);
740         if (!AddButton->isEnabled()){
741           QListBoxItem* anItem;
742           mySimulation->ResetGrid(true);
743           for (anItem = myFacesByNodes->firstItem(); anItem != 0; anItem = anItem->next()) {
744             QStringList anIds = QStringList::split(" ", anItem->text());
745             SMESH::TPolySimulation::TVTKIds aVTKIds_faces;
746             for (QStringList::iterator it = anIds.begin(); it != anIds.end(); ++it){
747               vtkIdType aId = myActor->GetObject()->GetNodeVTKId( (*it).toInt() ) ;
748               aVTKIds.push_back(aId);
749               aVTKIds_faces.push_back(aId);
750             }
751             if(!Preview->isChecked()){
752               aType = VTK_POLYGON;
753               mySimulation->SetPosition(myActor, aType, aVTKIds_faces,false);
754             }
755           }
756           if(myFacesByNodes->count() == 0){
757             mySimulation->SetVisibility(false);
758           } else {
759             mySimulation->SetVisibility(true);
760           }
761           if(Preview->isChecked()){
762             mySimulation->SetPosition(myActor, aType, aVTKIds);
763           }
764         } else {
765           // add ids from edit line
766           QStringList anEditIds = QStringList::split( " ", myEditCurrentArgument->text(), false);
767           myActor->SetEntityMode(SMESH_Actor::eFaces);
768           for ( int i = 0; i < anEditIds.count(); i++ )
769             aVTKIds.push_back( myActor->GetObject()->GetNodeVTKId( anEditIds[ i ].toInt() ));
770           aType = VTK_POLYGON;
771           mySimulation->SetPosition(myActor, aType, aVTKIds);
772         }
773       }else if(GetConstructorId() == 1){
774         SMDS_Mesh* aMesh = 0;
775         if ( myActor ){
776           aMesh = myActor->GetObject()->GetMesh();
777           if (Preview->isChecked())
778             myActor->SetEntityMode(SMESH_Actor::eVolumes);
779           else
780             myActor->SetEntityMode(SMESH_Actor::eFaces);
781         }
782         if ( aMesh ) {
783           QStringList aListId = QStringList::split( " ", myEditCurrentArgument->text(), false);
784           for ( int i = 0; i < aListId.count(); i++ )
785             {
786               const SMDS_MeshElement * anElem = aMesh->FindElement( aListId[ i ].toInt() );
787               if ( !anElem )
788                 return;
789               
790               SMDS_ElemIteratorPtr anIter = anElem->nodesIterator();
791               SMESH::TPolySimulation::TVTKIds aVTKIds_faces;
792               while( anIter->more() )
793                 if ( const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next() ){
794                   vtkIdType aId = myActor->GetObject()->GetNodeVTKId( aNode->GetID() ) ;
795                   aVTKIds.push_back(aId);
796                   aVTKIds_faces.push_back(aId);
797                 }
798               if(!Preview->isChecked()){
799                 aType = VTK_POLYGON;
800                 mySimulation->SetPosition(myActor, aType, aVTKIds_faces);
801               }
802             }
803           if(Preview->isChecked())
804             mySimulation->SetPosition(myActor, aType, aVTKIds);
805         }
806       }
807       SMESH::UpdateView();
808     }
809 }
810
811 //=================================================================================
812 // function : SetEditCurrentArgument()
813 // purpose  :
814 //=================================================================================
815 void SMESHGUI_CreatePolyhedralVolumeDlg::SetEditCurrentArgument()
816 {
817   QPushButton* send = (QPushButton*)sender();
818   if(send == SelectElementsButton) {
819     LineEditElements->setFocus() ;
820     myEditCurrentArgument = LineEditElements;
821   }
822   SelectionIntoArgument();
823 }
824
825 //=================================================================================
826 // function : DeactivateActiveDialog()
827 // purpose  :
828 //=================================================================================
829 void SMESHGUI_CreatePolyhedralVolumeDlg::DeactivateActiveDialog()
830 {
831   if ( GroupConstructors->isEnabled() ) {
832     GroupConstructors->setEnabled(false) ;
833     GroupContent->setEnabled(false) ;
834     GroupButtons->setEnabled(false) ;
835     mySimulation->SetVisibility(false);
836     mySMESHGUI->ResetState() ;    
837     mySMESHGUI->SetActiveDialogBox(0) ;
838   }
839 }
840
841
842 //=================================================================================
843 // function : ActivateThisDialog()
844 // purpose  :
845 //=================================================================================
846 void SMESHGUI_CreatePolyhedralVolumeDlg::ActivateThisDialog()
847 {
848   /* Emit a signal to deactivate the active dialog */
849   mySMESHGUI->EmitSignalDeactivateDialog() ;   
850   GroupConstructors->setEnabled(true) ;
851   GroupContent->setEnabled(true) ;
852   GroupButtons->setEnabled(true) ;
853   
854   mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
855
856   myViewWindow->SetSelectionMode( FaceSelection );
857   SelectionIntoArgument();
858 }
859
860
861 //=================================================================================
862 // function : enterEvent()
863 // purpose  :
864 //=================================================================================
865 void SMESHGUI_CreatePolyhedralVolumeDlg::enterEvent(QEvent* e)
866 {
867   if ( GroupConstructors->isEnabled() )
868     return ;  
869   ActivateThisDialog() ;
870 }
871
872
873 //=================================================================================
874 // function : closeEvent()
875 // purpose  :
876 //=================================================================================
877 void SMESHGUI_CreatePolyhedralVolumeDlg::closeEvent( QCloseEvent* e )
878 {
879   /* same than click on cancel button */
880   this->ClickOnCancel() ;
881 }
882
883
884 //=======================================================================
885 //function : hideEvent
886 //purpose  : caused by ESC key
887 //=======================================================================
888
889 void SMESHGUI_CreatePolyhedralVolumeDlg::hideEvent ( QHideEvent * e )
890 {
891   if ( !isMinimized() )
892     ClickOnCancel();
893 }
894
895
896 //=================================================================================
897 // function : GetConstructorId()
898 // purpose  : 
899 //=================================================================================
900 int SMESHGUI_CreatePolyhedralVolumeDlg::GetConstructorId()
901
902   if ( GroupConstructors != NULL && GroupConstructors->selected() != NULL )
903     return GroupConstructors->id( GroupConstructors->selected() );
904   return -1;
905 }
906
907 //=================================================================================
908 // function : onAdd()
909 // purpose  :
910 //=================================================================================
911 void SMESHGUI_CreatePolyhedralVolumeDlg::onAdd()
912 {
913   SALOME_ListIO selected;
914   mySelectionMgr->selectedObjects( selected );
915   int aNbSel = selected.Extent();
916   if (aNbSel == 0 || !myActor || myMesh->_is_nil()) return;
917
918   busy = true;
919
920   if ( !(myEditCurrentArgument->text().isEmpty()) )
921     {
922       myFacesByNodes->insertItem(myEditCurrentArgument->text());
923       //myFacesByNodes->setSelected(myFacesByNodes->count() - 1, true);
924       myNbOkElements = 1;
925       myEditCurrentArgument->clear();
926       AddButton->setEnabled(false);
927       buttonOk->setEnabled( true );
928       if(myFacesByNodes->count()>1) buttonApply->setEnabled( true );
929     }
930   busy = false;
931   onListSelectionChanged();
932   displaySimulation();
933 }
934
935 //=================================================================================
936 // function : onRemove()
937 // purpose  :
938 //=================================================================================
939 void SMESHGUI_CreatePolyhedralVolumeDlg::onRemove()
940 {
941   busy = true;
942   for (int i = myFacesByNodes->count(); i > 0; i--) {
943     if (myFacesByNodes->isSelected(i-1)) {
944       myFacesByNodes->removeItem(i-1);
945       myNbOkElements = 1;
946     }
947   }
948   if (myFacesByNodes->count() < 1){
949     RemoveButton->setEnabled(false);
950     buttonOk->setEnabled( false );
951     buttonApply->setEnabled( false );
952   } 
953   else if (myFacesByNodes->count() == 1){
954     buttonOk->setEnabled( false );
955     buttonApply->setEnabled( false );
956   }
957   busy = false;
958   displaySimulation();
959 }
960
961 //=================================================================================
962 // function : onListSelectionChanged()
963 // purpose  : Called when selection in element list is changed
964 //=================================================================================
965 void SMESHGUI_CreatePolyhedralVolumeDlg::onListSelectionChanged()
966 {
967   if (busy || !myActor) return;
968   busy = true;
969   bool isSelected=false;
970   SALOME_ListIO aList;
971   mySelectionMgr->setSelectedObjects( aList );
972   TColStd_MapOfInteger aIndexes;
973   QListBoxItem* anItem;
974   for (anItem = myFacesByNodes->firstItem(); anItem != 0; anItem = anItem->next()) {
975     if (anItem->isSelected()) {
976       isSelected = true;
977       QStringList anIds = QStringList::split(" ", anItem->text());
978       for (QStringList::iterator it = anIds.begin(); it != anIds.end(); ++it)
979         aIndexes.Add((*it).toInt());
980     }
981   }
982   if(isSelected) RemoveButton->setEnabled(true);
983   else RemoveButton->setEnabled(false);
984   mySelector->AddOrRemoveIndex(myActor->getIO(), aIndexes, true );
985   myViewWindow->highlight( myActor->getIO(), true, true );
986   mySelectionMgr->clearFilters(); 
987   aList.Append( myActor->getIO() );
988   mySelectionMgr->setSelectedObjects( aList );
989   
990   busy = false;
991 }