Salome HOME
SMH: Preparation version 3.0.0 - merge (HEAD+POLYWORK)
[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       vtkProperty* aProp = vtkProperty::New();
116       float anRGB[3];
117       anRGB[0] = GetFloat("SMESH:SettingsFillColorRed", 0)/255.;
118       anRGB[1] = GetFloat("SMESH:SettingsFillColorGreen", 170)/255.;
119       anRGB[2] = GetFloat("SMESH:SettingsFillColorBlue", 255)/255.;
120       aProp->SetColor(anRGB[0],anRGB[1],anRGB[2]);
121       myPreviewActor->SetProperty( aProp );
122       aProp->Delete();
123
124       vtkProperty* aBackProp = vtkProperty::New();
125       anRGB[0] = GetFloat("SMESH:SettingsBackFaceColorRed", 0)/255.;
126       anRGB[1] = GetFloat("SMESH:SettingsBackFaceColorGreen", 0)/255.;
127       anRGB[2] = GetFloat("SMESH:SettingsBackFaceColorBlue", 255)/255.;
128       aBackProp->SetColor(anRGB[0],anRGB[1],anRGB[2]);
129       myPreviewActor->SetBackfaceProperty( aBackProp );
130       aBackProp->Delete();
131
132       myViewWindow->AddActor( myPreviewActor );
133
134     }
135
136
137     typedef std::vector<vtkIdType> TVTKIds;
138     void SetPosition(SMESH_Actor* theActor, 
139                      vtkIdType theType, 
140                      const TVTKIds& theIds,
141                      bool theReset=true)
142     {
143       vtkUnstructuredGrid *aGrid = theActor->GetUnstructuredGrid();
144       myGrid->SetPoints(aGrid->GetPoints());
145
146       if (theReset) ResetGrid(theReset);
147       
148       vtkIdList *anIds = vtkIdList::New();
149
150       for (int i = 0, iEnd = theIds.size(); i < iEnd; i++)
151         anIds->InsertId(i,theIds[i]);
152
153       myGrid->InsertNextCell(theType,anIds);
154       anIds->Delete();
155
156       myGrid->Modified();
157
158       SetVisibility(true);
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( SMESHGUI::resourceMgr()->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         }
402         else
403           SMESH::SetPointRepresentation(true);
404         myViewWindow->SetSelectionMode(NodeSelection);
405         
406         AddButton->setEnabled(false);
407         RemoveButton->setEnabled(false);
408         TextLabelIds->setText( tr( "SMESH_ID_NODES" ) );
409         myFacesByNodesLabel->show();
410         myFacesByNodes->clear();
411         myFacesByNodes->show();
412         AddButton->show();
413         RemoveButton->show();
414         Preview->show();
415         break;
416       }
417     case 1 :
418       {
419         SMESH::SetPointRepresentation(false);
420         myViewWindow->SetSelectionMode(FaceSelection);
421         
422         TextLabelIds->setText( tr( "SMESH_ID_FACES" ) );
423         myFacesByNodesLabel->hide();
424         myFacesByNodes->hide();
425         AddButton->hide();
426         RemoveButton->hide();
427         Preview->show();
428         break;
429       }
430     }
431   
432   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
433 }
434
435 //=================================================================================
436 // function : ClickOnPreview()
437 // purpose  :
438 //=================================================================================
439 void SMESHGUI_CreatePolyhedralVolumeDlg::ClickOnPreview(bool theToggled){
440   Preview->setChecked(theToggled);
441   displaySimulation();
442 }
443
444 //=================================================================================
445 // function : ClickOnApply()
446 // purpose  :
447 //=================================================================================
448 void SMESHGUI_CreatePolyhedralVolumeDlg::ClickOnApply()
449 {
450   if ( myNbOkElements>0 && !mySMESHGUI->isActiveStudyLocked())
451     {
452       busy = true;
453       if (GetConstructorId() == 0)
454         {
455           SMESH::long_array_var anIdsOfNodes = new SMESH::long_array;
456           SMESH::long_array_var aQuantities  = new SMESH::long_array;
457
458           aQuantities->length( myFacesByNodes->count() );
459
460           TColStd_ListOfInteger aNodesIds;
461
462           QListBoxItem* anItem;
463           int aNbQuantities = 0;
464           for (anItem = myFacesByNodes->firstItem(); anItem != 0; anItem = anItem->next()) {
465             QStringList anIds = QStringList::split(" ", anItem->text());
466             int aNbNodesInFace = 0;
467             for (QStringList::iterator it = anIds.begin(); it != anIds.end(); ++it, ++aNbNodesInFace)
468               aNodesIds.Append( (*it).toInt() );
469
470             aQuantities[aNbQuantities++] = aNbNodesInFace;
471           }
472
473           anIdsOfNodes->length(aNodesIds.Extent());
474
475           int aNbIdsOfNodes = 0;
476           TColStd_ListIteratorOfListOfInteger It;
477           It.Initialize(aNodesIds);
478           for(;It.More();It.Next())
479             anIdsOfNodes[aNbIdsOfNodes++] = It.Value();
480             
481           try{
482             SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
483             QApplication::setOverrideCursor(Qt::waitCursor);
484             aMeshEditor->AddPolyhedralVolume(anIdsOfNodes, aQuantities);
485             QApplication::restoreOverrideCursor();
486           }catch(SALOME::SALOME_Exception& exc){
487             INFOS("Follow exception was cought:\n\t"<<exc.details.text);
488           }catch(std::exception& exc){
489             INFOS("Follow exception was cought:\n\t"<<exc.what());
490           }catch(...){
491             INFOS("Unknown exception was cought !!!");
492           }
493         }
494       else if (GetConstructorId() == 1)
495         {
496           SMESH::long_array_var anIdsOfFaces = new SMESH::long_array;
497           
498           QStringList aListId = QStringList::split( " ", myEditCurrentArgument->text() );
499           anIdsOfFaces->length(aListId.count());
500           for ( int i = 0; i < aListId.count(); i++ )
501             anIdsOfFaces[i] = aListId[i].toInt();
502           
503           try{
504             SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
505             QApplication::setOverrideCursor(Qt::waitCursor);
506             aMeshEditor->AddPolyhedralVolumeByFaces(anIdsOfFaces);
507             QApplication::restoreOverrideCursor();
508           }catch(SALOME::SALOME_Exception& exc){
509             INFOS("Follow exception was cought:\n\t"<<exc.details.text);
510           }catch(std::exception& exc){
511             INFOS("Follow exception was cought:\n\t"<<exc.what());
512           }catch(...){
513             INFOS("Unknown exception was cought !!!");
514           }
515         }
516       
517       SALOME_ListIO aList;
518       mySelectionMgr->setSelectedObjects( aList );
519       mySimulation->SetVisibility(false);
520       SMESH::UpdateView();
521       ConstructorsClicked( GetConstructorId() );
522       busy = false;
523     }
524 }
525
526 //=================================================================================
527 // function : ClickOnOk()
528 // purpose  :
529 //=================================================================================
530 void SMESHGUI_CreatePolyhedralVolumeDlg::ClickOnOk()
531 {
532   ClickOnApply() ;
533   ClickOnCancel() ;
534 }
535
536         
537 //=================================================================================
538 // function : ClickOnCancel()
539 // purpose  :
540 //=================================================================================
541 void SMESHGUI_CreatePolyhedralVolumeDlg::ClickOnCancel()
542 {
543   mySelectionMgr->clearFilters();
544   SALOME_ListIO aList;
545   mySelectionMgr->setSelectedObjects( aList );
546   SMESH::SetPointRepresentation(false);
547   mySimulation->SetVisibility(false);
548   myViewWindow->SetSelectionMode( ActorSelection );
549   disconnect( mySelectionMgr, 0, this, 0 );
550   mySMESHGUI->ResetState() ;
551   reject() ;
552 }
553
554 //=======================================================================
555 //function : onTextChange
556 //purpose  : 
557 //=======================================================================
558
559 void SMESHGUI_CreatePolyhedralVolumeDlg::onTextChange(const QString& theNewText)
560 {
561   if ( busy ) return;
562   busy = true;
563
564   mySimulation->SetVisibility(false);
565
566   SMDS_Mesh* aMesh = 0;
567   if ( myActor )
568     aMesh = myActor->GetObject()->GetMesh();
569
570   if (GetConstructorId() == 0)
571   {
572     if ( aMesh ) {
573       SALOME_ListIO aList; aList.Append( myActor->getIO() );
574       mySelectionMgr->setSelectedObjects( aList );
575
576       TColStd_IndexedMapOfInteger selectedIndices;
577       TColStd_MapOfInteger newIndices;
578       mySelector->GetIndex( myActor->getIO(), selectedIndices);
579
580       QStringList aListId = QStringList::split( " ", theNewText, false);
581       for ( int i = 0; i < aListId.count(); i++ ) {
582         const SMDS_MeshNode * n = aMesh->FindNode( aListId[ i ].toInt() );
583         if ( n ) {
584             /*if ( mySelectionMgr->IsIndexSelected( myActor->getIO(), n->GetID() ))
585               allOk = false;
586             else
587               mySelectionMgr->AddOrRemoveIndex (myActor->getIO(), n->GetID(), true);*/
588           if (selectedIndices.Add(n->GetID()))
589             newIndices.Add(n->GetID());
590           myNbOkElements++;
591         }
592       }
593
594       if (newIndices.Extent() > 0){
595         mySelector->AddOrRemoveIndex( myActor->getIO(), newIndices, true );
596         myViewWindow->highlight( myActor->getIO(), true, true );
597       }
598
599 /*      bool aNodesOK = false;
600         if (aListId.count() >= 3)
601           aNodesOK = true;
602
603         allOk = (allOk && aNodesOK);*/
604         
605       if ( myNbOkElements>0 && aListId.count()>=3)
606         AddButton->setEnabled(true);
607         else
608           AddButton->setEnabled(false);
609
610 //      if (allOk && !myOkElements)
611         displaySimulation();
612       }
613     }
614   else if (GetConstructorId() == 1)
615   {
616     myNbOkElements = 0;
617     buttonOk->setEnabled( false );
618     buttonApply->setEnabled( false );
619       
620       // check entered ids of faces and hilight them
621     QStringList aListId;
622     if ( aMesh ) {
623       SALOME_ListIO aList; aList.Append( myActor->getIO() );
624       mySelectionMgr->setSelectedObjects( aList );
625
626       TColStd_IndexedMapOfInteger selectedIndices;
627       TColStd_MapOfInteger newIndices;
628       mySelector->GetIndex( myActor->getIO(), selectedIndices);
629
630       aListId = QStringList::split( " ", theNewText, false);
631
632       for ( int i = 0; i < aListId.count(); i++ ) {
633         const SMDS_MeshElement * e = aMesh->FindElement( aListId[ i ].toInt() );
634         if ( e ) {
635           /*if ( mySelectionMgr->IsIndexSelected( myActor->getIO(), e->GetID() ))
636             myOkElements = false;
637           else
638             mySelectionMgr->AddOrRemoveIndex (myActor->getIO(), e->GetID(), true);*/
639           if (selectedIndices.Add(e->GetID()))
640               newIndices.Add(e->GetID());
641           myNbOkElements++;  
642         }
643       }
644
645       if (newIndices.Extent() > 0){
646         mySelector->AddOrRemoveIndex( myActor->getIO(), newIndices, true );
647         myViewWindow->highlight( myActor->getIO(), true, true );
648       }
649
650       if ( myNbOkElements ) {
651         if (aListId.count()>1){ 
652           buttonOk->setEnabled( true );
653           buttonApply->setEnabled( true );
654         }
655         else{
656           buttonOk->setEnabled( false );
657           buttonApply->setEnabled( false );
658         }
659         displaySimulation();
660       }
661     }
662   }
663   busy = false;
664 }
665
666 //=================================================================================
667 // function : SelectionIntoArgument()
668 // purpose  : Called when selection as changed or other case
669 //=================================================================================
670 void SMESHGUI_CreatePolyhedralVolumeDlg::SelectionIntoArgument()
671 {
672   if ( busy ) return;
673   
674   // clear
675   
676   if (GetConstructorId() == 1 || myFacesByNodes->count() <= 1)
677     {
678       myNbOkElements = 0;
679       AddButton->setEnabled(false);
680       buttonOk->setEnabled( false );
681       buttonApply->setEnabled( false );
682     }
683
684   myActor = 0;
685
686   busy = true;
687   myEditCurrentArgument->setText( "" );
688   busy = false;
689   if ( !GroupButtons->isEnabled() ) // inactive
690     return;
691   
692   mySimulation->SetVisibility(false);
693   
694   // get selected mesh
695   
696   SALOME_ListIO selected;
697   mySelectionMgr->selectedObjects( selected );
698   int nbSel = selected.Extent();
699   if(nbSel != 1){
700     return;
701   }
702   
703   myMesh = SMESH::GetMeshByIO( selected.First() );
704   if ( myMesh->_is_nil() )
705     return;
706   
707   myActor = SMESH::FindActorByObject(myMesh);
708   if ( !myActor )
709     return;
710   
711   // get selected nodes/faces
712   QString aString = "";
713   switch(GetConstructorId()){
714   case 0:{
715     int anbNodes = SMESH::GetNameOfSelectedNodes(mySelector, myActor->getIO(), aString);
716     if (anbNodes >= 3)
717       AddButton->setEnabled(true);
718     else 
719       break;
720     busy = true;
721     myEditCurrentArgument->setText( aString );
722     busy = false;
723     break;
724   }
725   case 1:{
726     // get selected faces
727     int aNbFaces = SMESH::GetNameOfSelectedElements(mySelector, myActor->getIO(), aString);
728     if (aNbFaces<=1)
729       return;
730     busy = true;
731     myEditCurrentArgument->setText( aString );
732     busy = false;
733     
734     // OK
735     myNbOkElements = 1;
736     buttonOk->setEnabled( true );
737     buttonApply->setEnabled( true );
738     break;
739   }
740   default: return;
741   }
742   
743   displaySimulation();
744 }
745
746 //=======================================================================
747 //function : displaySimulation
748 //purpose  : 
749 //=======================================================================
750 void SMESHGUI_CreatePolyhedralVolumeDlg::displaySimulation()
751 {
752   if ( (myNbOkElements || AddButton->isEnabled()) && GroupButtons->isEnabled() && myActor)
753     {
754       SMESH::TPolySimulation::TVTKIds aVTKIds;
755       vtkIdType aType = VTK_CONVEX_POINT_SET ;
756       if (GetConstructorId() == 0){
757         if(!Preview->isChecked()) myActor->SetEntityMode(SMESH_Actor::eFaces);
758         else myActor->SetEntityMode(SMESH_Actor::eVolumes);
759         if (!AddButton->isEnabled()){
760           QListBoxItem* anItem;
761           mySimulation->ResetGrid(true);
762           for (anItem = myFacesByNodes->firstItem(); anItem != 0; anItem = anItem->next()) {
763             QStringList anIds = QStringList::split(" ", anItem->text());
764             SMESH::TPolySimulation::TVTKIds aVTKIds_faces;
765             for (QStringList::iterator it = anIds.begin(); it != anIds.end(); ++it){
766               vtkIdType aId = myActor->GetObject()->GetNodeVTKId( (*it).toInt() ) ;
767               aVTKIds.push_back(aId);
768               aVTKIds_faces.push_back(aId);
769             }
770             if(!Preview->isChecked()){
771               aType = VTK_POLYGON;
772               mySimulation->SetPosition(myActor, aType, aVTKIds_faces,false);
773             }
774           }
775           if(Preview->isChecked()){
776             mySimulation->SetPosition(myActor, aType, aVTKIds);
777           }
778         } else {
779           // add ids from edit line
780           QStringList anEditIds = QStringList::split( " ", myEditCurrentArgument->text(), false);
781           myActor->SetEntityMode(SMESH_Actor::eFaces);
782           for ( int i = 0; i < anEditIds.count(); i++ )
783             aVTKIds.push_back( myActor->GetObject()->GetNodeVTKId( anEditIds[ i ].toInt() ));
784           aType = VTK_POLYGON;
785           mySimulation->SetPosition(myActor, aType, aVTKIds);
786         }
787       }else if(GetConstructorId() == 1){
788         SMDS_Mesh* aMesh = 0;
789         if ( myActor ){
790           aMesh = myActor->GetObject()->GetMesh();
791           if (Preview->isChecked())
792             myActor->SetEntityMode(SMESH_Actor::eVolumes);
793           else
794             myActor->SetEntityMode(SMESH_Actor::eFaces);
795         }
796         if ( aMesh ) {
797           QStringList aListId = QStringList::split( " ", myEditCurrentArgument->text(), false);
798           for ( int i = 0; i < aListId.count(); i++ )
799             {
800               const SMDS_MeshElement * anElem = aMesh->FindElement( aListId[ i ].toInt() );
801               if ( !anElem )
802                 return;
803               
804               SMDS_ElemIteratorPtr anIter = anElem->nodesIterator();
805               SMESH::TPolySimulation::TVTKIds aVTKIds_faces;
806               while( anIter->more() )
807                 if ( const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next() ){
808                   vtkIdType aId = myActor->GetObject()->GetNodeVTKId( aNode->GetID() ) ;
809                   aVTKIds.push_back(aId);
810                   aVTKIds_faces.push_back(aId);
811                 }
812               if(!Preview->isChecked()){
813                 aType = VTK_POLYGON;
814                 mySimulation->SetPosition(myActor, aType, aVTKIds_faces);
815               }
816             }
817           if(Preview->isChecked())
818             mySimulation->SetPosition(myActor, aType, aVTKIds);
819         }
820       }
821     }  
822 }
823
824 //=================================================================================
825 // function : SetEditCurrentArgument()
826 // purpose  :
827 //=================================================================================
828 void SMESHGUI_CreatePolyhedralVolumeDlg::SetEditCurrentArgument()
829 {
830   QPushButton* send = (QPushButton*)sender();
831   if(send == SelectElementsButton) {
832     LineEditElements->setFocus() ;
833     myEditCurrentArgument = LineEditElements;
834   }
835   SelectionIntoArgument();
836 }
837
838 //=================================================================================
839 // function : DeactivateActiveDialog()
840 // purpose  :
841 //=================================================================================
842 void SMESHGUI_CreatePolyhedralVolumeDlg::DeactivateActiveDialog()
843 {
844   if ( GroupConstructors->isEnabled() ) {
845     GroupConstructors->setEnabled(false) ;
846     GroupContent->setEnabled(false) ;
847     GroupButtons->setEnabled(false) ;
848     mySimulation->SetVisibility(false);
849     mySMESHGUI->ResetState() ;    
850     mySMESHGUI->SetActiveDialogBox(0) ;
851   }
852 }
853
854
855 //=================================================================================
856 // function : ActivateThisDialog()
857 // purpose  :
858 //=================================================================================
859 void SMESHGUI_CreatePolyhedralVolumeDlg::ActivateThisDialog()
860 {
861   /* Emit a signal to deactivate the active dialog */
862   mySMESHGUI->EmitSignalDeactivateDialog() ;   
863   GroupConstructors->setEnabled(true) ;
864   GroupContent->setEnabled(true) ;
865   GroupButtons->setEnabled(true) ;
866   
867   mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
868
869   myViewWindow->SetSelectionMode( FaceSelection );
870   SelectionIntoArgument();
871 }
872
873
874 //=================================================================================
875 // function : enterEvent()
876 // purpose  :
877 //=================================================================================
878 void SMESHGUI_CreatePolyhedralVolumeDlg::enterEvent(QEvent* e)
879 {
880   if ( GroupConstructors->isEnabled() )
881     return ;  
882   ActivateThisDialog() ;
883 }
884
885
886 //=================================================================================
887 // function : closeEvent()
888 // purpose  :
889 //=================================================================================
890 void SMESHGUI_CreatePolyhedralVolumeDlg::closeEvent( QCloseEvent* e )
891 {
892   /* same than click on cancel button */
893   this->ClickOnCancel() ;
894 }
895
896
897 //=======================================================================
898 //function : hideEvent
899 //purpose  : caused by ESC key
900 //=======================================================================
901
902 void SMESHGUI_CreatePolyhedralVolumeDlg::hideEvent ( QHideEvent * e )
903 {
904   if ( !isMinimized() )
905     ClickOnCancel();
906 }
907
908
909 //=================================================================================
910 // function : GetConstructorId()
911 // purpose  : 
912 //=================================================================================
913 int SMESHGUI_CreatePolyhedralVolumeDlg::GetConstructorId()
914
915   if ( GroupConstructors != NULL && GroupConstructors->selected() != NULL )
916     return GroupConstructors->id( GroupConstructors->selected() );
917   return -1;
918 }
919
920 //=================================================================================
921 // function : onAdd()
922 // purpose  :
923 //=================================================================================
924 void SMESHGUI_CreatePolyhedralVolumeDlg::onAdd()
925 {
926   SALOME_ListIO selected;
927   mySelectionMgr->selectedObjects( selected );
928   int aNbSel = selected.Extent();
929   if (aNbSel == 0 || !myActor || myMesh->_is_nil()) return;
930
931   busy = true;
932
933   if ( !(myEditCurrentArgument->text().isEmpty()) )
934     {
935       myFacesByNodes->insertItem(myEditCurrentArgument->text());
936       //myFacesByNodes->setSelected(myFacesByNodes->count() - 1, true);
937       myNbOkElements = 1;
938       myEditCurrentArgument->clear();
939       AddButton->setEnabled(false);
940       RemoveButton->setEnabled(true);
941       buttonOk->setEnabled( true );
942       if(myFacesByNodes->count()>1) buttonApply->setEnabled( true );
943     }
944   busy = false;
945   onListSelectionChanged();
946   displaySimulation();
947 }
948
949 //=================================================================================
950 // function : onRemove()
951 // purpose  :
952 //=================================================================================
953 void SMESHGUI_CreatePolyhedralVolumeDlg::onRemove()
954 {
955   busy = true;
956   for (int i = myFacesByNodes->count(); i > 0; i--) {
957     if (myFacesByNodes->isSelected(i-1)) {
958       myFacesByNodes->removeItem(i-1);
959     }
960   }
961   if (myFacesByNodes->count() < 1){
962     myNbOkElements = 0;
963     RemoveButton->setEnabled(false);
964     buttonOk->setEnabled( false );
965     buttonApply->setEnabled( false );
966   } 
967   else if (myFacesByNodes->count() == 1){
968     myNbOkElements = 0;
969     buttonOk->setEnabled( false );
970     buttonApply->setEnabled( false );
971   }
972   displaySimulation();
973   busy = false;
974   onListSelectionChanged();
975 }
976
977 //=================================================================================
978 // function : onListSelectionChanged()
979 // purpose  : Called when selection in element list is changed
980 //=================================================================================
981 void SMESHGUI_CreatePolyhedralVolumeDlg::onListSelectionChanged()
982 {
983   if (busy || !myActor) return;
984   busy = true;
985
986   SALOME_ListIO list;
987   mySelectionMgr->setSelectedObjects( list );
988   TColStd_MapOfInteger aIndexes;
989   QListBoxItem* anItem;
990   for (anItem = myFacesByNodes->firstItem(); anItem != 0; anItem = anItem->next()) {
991     if (anItem->isSelected()) {
992       QStringList anIds = QStringList::split(" ", anItem->text());
993       for (QStringList::iterator it = anIds.begin(); it != anIds.end(); ++it)
994         aIndexes.Add((*it).toInt());
995     }
996   }
997   mySelector->AddOrRemoveIndex(myActor->getIO(), aIndexes, true );
998   myViewWindow->highlight( myActor->getIO(), true, true );
999   list.Append( myActor->getIO() );
1000   mySelectionMgr->setSelectedObjects( list );
1001   
1002   busy = false;
1003 }