Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_CreatePolyhedralVolumeDlg.cxx
1 //  SMESH SMESHGUI : GUI for SMESH component
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SMESHGUI_CreatePolyhedralVolumeDlg.cxx
25 //  Author : Michael ZORIN
26 //  Module : SMESH
27 //  $Header: 
28
29 #include "SMESHGUI_CreatePolyhedralVolumeDlg.h"
30
31 #include "SMESHGUI.h"
32 #include "SMESHGUI_Utils.h"
33 #include "SMESHGUI_VTKUtils.h"
34 #include "SMESHGUI_MeshUtils.h"
35 #include "SMESHGUI_IdValidator.h"
36 #include "SMESH_ActorUtils.h"
37 #include "SMESHGUI_SpinBox.h"
38 #include "SMESH_Actor.h"
39 #include "SMESH_TypeFilter.hxx"
40 #include "SMDS_Mesh.hxx"
41
42 #include "SUIT_ResourceMgr.h"
43
44 #include "SalomeApp_Application.h"
45 #include "SalomeApp_Study.h"
46 #include "SUIT_Desktop.h"
47 #include "SUIT_MessageBox.h"
48 #include "LightApp_SelectionMgr.h"
49 #include "utilities.h"
50
51 #include "SVTK_ViewWindow.h"
52 #include "SVTK_Selector.h"
53
54 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
55 #include <TColStd_ListOfInteger.hxx>
56 #include <TColStd_ListIteratorOfListOfInteger.hxx>
57
58 #include <vtkCell.h>
59 #include <vtkIdList.h>
60 #include <vtkIntArray.h>
61 #include <vtkCellArray.h>
62 #include <vtkUnsignedCharArray.h>
63 #include <vtkUnstructuredGrid.h>
64 #include <vtkDataSetMapper.h>
65 #include <vtkProperty.h>
66
67 // QT Includes
68 #include <qapplication.h>
69 #include <qbuttongroup.h>
70 #include <qgroupbox.h>
71 #include <qlabel.h>
72 #include <qlineedit.h>
73 #include <qpushbutton.h>
74 #include <qradiobutton.h>
75 #include <qcheckbox.h>
76 #include <qlayout.h>
77 #include <qspinbox.h> 
78 #include <qpixmap.h>
79 #include <qlistbox.h>
80
81 // IDL Headers
82 #include "SALOMEconfig.h"
83 #include CORBA_SERVER_HEADER(SMESH_Group)
84
85 using namespace std;
86
87 namespace SMESH{
88
89 class TPolySimulation{
90     SVTK_ViewWindow* myViewWindow;
91
92     SALOME_Actor *myPreviewActor;
93     vtkDataSetMapper* myMapper;
94     vtkUnstructuredGrid* myGrid;
95
96   public:
97
98     TPolySimulation(SalomeApp_Application* app)
99     {
100       SUIT_ViewManager* mgr = app->activeViewManager();
101       myViewWindow = mgr ? dynamic_cast<SVTK_ViewWindow*>( mgr->getActiveView() ) : NULL;
102
103       myGrid = vtkUnstructuredGrid::New();
104   
105       // Create and display actor
106       myMapper = vtkDataSetMapper::New();
107       myMapper->SetInput( myGrid );
108
109       myPreviewActor = SALOME_Actor::New();
110       myPreviewActor->PickableOff();
111       myPreviewActor->VisibilityOff();
112       myPreviewActor->SetMapper( myMapper );
113       myPreviewActor->SetRepresentation( 3 );
114
115       float anRGB[3];
116       vtkProperty* aProp = vtkProperty::New();
117       GetColor( "SMESH", "selection_element_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       float aFactor,aUnits;
121       myPreviewActor->SetResolveCoincidentTopology(true);
122       myPreviewActor->GetPolygonOffsetParameters(aFactor,aUnits);
123       myPreviewActor->SetPolygonOffsetParameters(aFactor,0.2*aUnits);
124       aProp->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       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   this->show() ; /* displays Dialog */
367
368   ConstructorsClicked(0);
369   SelectionIntoArgument();
370 }
371
372
373 //=================================================================================
374 // function : ConstructorsClicked()
375 // purpose  : Radio button management
376 //=================================================================================
377 void SMESHGUI_CreatePolyhedralVolumeDlg::ConstructorsClicked(int constructorId)
378 {
379   //disconnect(mySelectionMgr, 0, this, 0);
380
381   SALOME_ListIO io;
382   mySelectionMgr->selectedObjects( io );
383   SALOME_ListIO aList;
384   mySelectionMgr->setSelectedObjects( aList );
385   myEditCurrentArgument->clear();
386   myNbOkElements = 0;
387   buttonApply->setEnabled(false);
388   buttonOk->setEnabled(false);
389   mySimulation->SetVisibility(false);
390
391   switch(constructorId)
392     {
393     case 0 :
394       { 
395         if ( myActor ){
396           myActor->SetPointRepresentation(true);
397         }
398         else
399           SMESH::SetPointRepresentation(true);
400         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
401           aViewWindow->SetSelectionMode(NodeSelection);
402         
403         AddButton->setEnabled(false);
404         RemoveButton->setEnabled(false);
405         TextLabelIds->setText( tr( "SMESH_ID_NODES" ) );
406         myFacesByNodesLabel->show();
407         myFacesByNodes->clear();
408         myFacesByNodes->show();
409         AddButton->show();
410         RemoveButton->show();
411         Preview->show();
412         break;
413       }
414     case 1 :
415       {
416         if( myActor ){
417           myActor->SetPointRepresentation(false);
418         } else {
419           SMESH::SetPointRepresentation(false);
420         }
421         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
422           aViewWindow->SetSelectionMode(FaceSelection);
423         
424         TextLabelIds->setText( tr( "SMESH_ID_FACES" ) );
425         myFacesByNodesLabel->hide();
426         myFacesByNodes->hide();
427         AddButton->hide();
428         RemoveButton->hide();
429         Preview->show();
430         break;
431       }
432     }
433   
434   //connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
435   mySelectionMgr->setSelectedObjects( io );
436 }
437
438 //=================================================================================
439 // function : ClickOnPreview()
440 // purpose  :
441 //=================================================================================
442 void SMESHGUI_CreatePolyhedralVolumeDlg::ClickOnPreview(bool theToggled){
443   Preview->setChecked(theToggled);
444   displaySimulation();
445 }
446
447 //=================================================================================
448 // function : ClickOnApply()
449 // purpose  :
450 //=================================================================================
451 void SMESHGUI_CreatePolyhedralVolumeDlg::ClickOnApply()
452 {
453   if ( myNbOkElements>0 && !mySMESHGUI->isActiveStudyLocked())
454     {
455       if(checkEditLine(false) == -1) {return;}
456       busy = true;
457       if (GetConstructorId() == 0)
458         {
459           SMESH::long_array_var anIdsOfNodes = new SMESH::long_array;
460           SMESH::long_array_var aQuantities  = new SMESH::long_array;
461
462           aQuantities->length( myFacesByNodes->count() );
463
464           TColStd_ListOfInteger aNodesIds;
465
466           QListBoxItem* anItem;
467           int aNbQuantities = 0;
468           for (anItem = myFacesByNodes->firstItem(); anItem != 0; anItem = anItem->next()) {
469             QStringList anIds = QStringList::split(" ", anItem->text());
470             int aNbNodesInFace = 0;
471             for (QStringList::iterator it = anIds.begin(); it != anIds.end(); ++it, ++aNbNodesInFace)
472               aNodesIds.Append( (*it).toInt() );
473
474             aQuantities[aNbQuantities++] = aNbNodesInFace;
475           }
476
477           anIdsOfNodes->length(aNodesIds.Extent());
478
479           int aNbIdsOfNodes = 0;
480           TColStd_ListIteratorOfListOfInteger It;
481           It.Initialize(aNodesIds);
482           for(;It.More();It.Next())
483             anIdsOfNodes[aNbIdsOfNodes++] = It.Value();
484             
485           try{
486             SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
487             QApplication::setOverrideCursor(Qt::waitCursor);
488             aMeshEditor->AddPolyhedralVolume(anIdsOfNodes, aQuantities);
489             QApplication::restoreOverrideCursor();
490           }catch(SALOME::SALOME_Exception& exc){
491             INFOS("Follow exception was cought:\n\t"<<exc.details.text);
492           }catch(std::exception& exc){
493             INFOS("Follow exception was cought:\n\t"<<exc.what());
494           }catch(...){
495             INFOS("Unknown exception was cought !!!");
496           }
497         }
498       else if (GetConstructorId() == 1)
499         {
500           SMESH::long_array_var anIdsOfFaces = new SMESH::long_array;
501           
502           QStringList aListId = QStringList::split( " ", myEditCurrentArgument->text() );
503           anIdsOfFaces->length(aListId.count());
504           for ( int i = 0; i < aListId.count(); i++ )
505             anIdsOfFaces[i] = aListId[i].toInt();
506           
507           try{
508             SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
509             QApplication::setOverrideCursor(Qt::waitCursor);
510             aMeshEditor->AddPolyhedralVolumeByFaces(anIdsOfFaces);
511             QApplication::restoreOverrideCursor();
512           }catch(SALOME::SALOME_Exception& exc){
513             INFOS("Follow exception was cought:\n\t"<<exc.details.text);
514           }catch(std::exception& exc){
515             INFOS("Follow exception was cought:\n\t"<<exc.what());
516           }catch(...){
517             INFOS("Unknown exception was cought !!!");
518           }
519         }
520       
521       //SALOME_ListIO aList;
522       //mySelectionMgr->setSelectedObjects( aList );
523       SMESH::UpdateView();
524       if( myActor ){
525         unsigned int anEntityMode = myActor->GetEntityMode();
526         myActor->SetEntityMode(SMESH_Actor::eVolumes | anEntityMode);
527       }
528       //ConstructorsClicked( GetConstructorId() );
529       busy = false;
530     }
531 }
532
533 //=================================================================================
534 // function : ClickOnOk()
535 // purpose  :
536 //=================================================================================
537 void SMESHGUI_CreatePolyhedralVolumeDlg::ClickOnOk()
538 {
539   if(checkEditLine(false) == -1) {return;}
540   ClickOnApply() ;
541   ClickOnCancel() ;
542 }
543
544         
545 //=================================================================================
546 // function : ClickOnCancel()
547 // purpose  :
548 //=================================================================================
549 void SMESHGUI_CreatePolyhedralVolumeDlg::ClickOnCancel()
550 {
551   mySelectionMgr->clearFilters();
552   //SALOME_ListIO aList;
553   //mySelectionMgr->setSelectedObjects( aList );
554   SMESH::SetPointRepresentation(false);
555   mySimulation->SetVisibility(false);
556   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
557     aViewWindow->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   if (checkEditLine() == -1) 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         if(aListId.count()>1)
641           displaySimulation();
642       }
643     }
644   }
645   busy = false;
646 }
647
648 //=================================================================================
649 // function : SelectionIntoArgument()
650 // purpose  : Called when selection as changed or other case
651 //=================================================================================
652 void SMESHGUI_CreatePolyhedralVolumeDlg::SelectionIntoArgument()
653 {
654   if ( busy ) return;
655   
656   // clear
657   
658   if (GetConstructorId() == 1 || myFacesByNodes->count() <= 1)
659     {
660       myNbOkElements = 0;
661       AddButton->setEnabled(false);
662       buttonOk->setEnabled( false );
663       buttonApply->setEnabled( false );
664     }
665
666   myActor = 0;
667
668   busy = true;
669   myEditCurrentArgument->setText( "" );
670   busy = false;
671   if ( !GroupButtons->isEnabled() ) // inactive
672     return;
673   
674   mySimulation->SetVisibility(false);
675   
676   // get selected mesh
677   
678   SALOME_ListIO selected;
679   mySelectionMgr->selectedObjects( selected );
680   int nbSel = selected.Extent();
681   if(nbSel != 1){
682     return;
683   }
684   
685   myMesh = SMESH::GetMeshByIO( selected.First() );
686   if ( myMesh->_is_nil() )
687     return;
688   
689   myActor = SMESH::FindActorByObject(myMesh);
690   if ( !myActor )
691     return;
692   
693   // get selected nodes/faces
694   QString aString = "";
695   int anbNodes=0,aNbFaces=0;
696   switch(GetConstructorId()){
697   case 0:{
698     anbNodes = SMESH::GetNameOfSelectedNodes(mySelector, myActor->getIO(), aString);
699     if (anbNodes >= 3)
700       AddButton->setEnabled(true);
701     else if (anbNodes < 3){
702       AddButton->setEnabled(false);
703     }
704     busy = true;
705     myEditCurrentArgument->setText( aString );
706     if (checkEditLine() == -1) {busy = false;return;}
707     busy = false;
708     break;
709   }
710   case 1:{
711     // get selected faces
712     aNbFaces = SMESH::GetNameOfSelectedElements(mySelector, myActor->getIO(), aString);
713     if (aNbFaces<=1){
714       buttonOk->setEnabled( false );
715       buttonApply->setEnabled( false );
716     } else {
717       buttonOk->setEnabled( true );
718       buttonApply->setEnabled( true );
719     }
720     busy = true;
721     myEditCurrentArgument->setText( aString );
722     if (checkEditLine() == -1) {busy = false;return;}
723     busy = false;
724     
725     // OK
726     myNbOkElements = 1;
727     break;
728   }
729   default: return;
730   }
731   if(anbNodes>2 || aNbFaces>1)
732     displaySimulation();
733 }
734
735 /*\brief int SMESHGUI_CreatePolyhedralVolumeDlg::checkEditLine()
736  * Checking of indices in edit line.
737  * If incorecct indices in edit line warning message appear and myEditCurrentArgument remove last index.
738  * \retval 1 - if all ok(or no indices in edit line), -1 - if there are incorrect indices.
739  */
740 int SMESHGUI_CreatePolyhedralVolumeDlg::checkEditLine(bool checkLast)
741 {
742   QString aString = "";
743   SMDS_Mesh* aMesh = 0;
744   
745   if(myMesh->_is_nil()) return 1;
746   if(!myActor){
747     myActor = SMESH::FindActorByObject(myMesh);
748     if(!myActor)
749       return 1;
750   }
751     
752   aMesh = myActor->GetObject()->GetMesh();
753
754   // checking for nodes
755   if (checkLast && myEditCurrentArgument->text().right(1) != QString(" ") ) return 1;
756   QStringList aListId = QStringList::split( " ", myEditCurrentArgument->text() );
757   for ( int i = 0; i < aListId.count(); i++ ){
758     switch (GetConstructorId()){
759     case 0:{ // nodes
760       const SMDS_MeshNode    * aNode = aMesh->FindNode( aListId[ i ].toInt() );
761       if( !aNode ){
762         std::string aWarning;
763         aWarning = "The incorrect indices of nodes!";
764         SUIT_MessageBox::warn1(SMESHGUI::desktop(),
765                                QObject::tr("SMESH_POLYEDRE_CREATE_ERROR"),
766                                QObject::tr(aWarning.c_str()),
767                                QObject::tr("SMESH_BUT_OK"));
768         
769         myEditCurrentArgument->clear();
770         myEditCurrentArgument->setText( aString );
771         return -1;
772       }
773
774       break;
775     }
776     case 1:{ // faces
777       bool aElemIsOK = true;
778       const SMDS_MeshElement * aElem = aMesh->FindElement( aListId[ i ].toInt() );
779       if (!aElem)
780         {
781           aElemIsOK = false;
782         }
783       else
784         {
785           SMDSAbs_ElementType aType = aMesh->GetElementType( aElem->GetID(),true );
786           if (aType != SMDSAbs_Face){
787             aElemIsOK = false;
788           }
789         }
790       if (!aElemIsOK){
791         std::string aWarning;
792         aWarning = "The incorrect indices of faces!";
793         SUIT_MessageBox::warn1(SMESHGUI::desktop(),
794                                QObject::tr("SMESH_POLYEDRE_CREATE_ERROR"),
795                                QObject::tr(aWarning.c_str()),
796                                QObject::tr("SMESH_BUT_OK"));
797         
798         myEditCurrentArgument->clear();
799         myEditCurrentArgument->setText( aString );
800         return -1;
801       }
802       break;
803     }
804     }
805     aString += aListId[ i ] + " "; 
806   }
807
808   return 1;
809 }
810
811 //=======================================================================
812 //function : displaySimulation
813 //purpose  : 
814 //=======================================================================
815 void SMESHGUI_CreatePolyhedralVolumeDlg::displaySimulation()
816 {
817   if ( (myNbOkElements || AddButton->isEnabled()) && GroupButtons->isEnabled() && myActor)
818     {
819       SMESH::TPolySimulation::TVTKIds aVTKIds;
820       vtkIdType aType = VTK_CONVEX_POINT_SET ;
821       SMDS_Mesh* aMesh = 0;
822       if ( myActor ){
823         aMesh = myActor->GetObject()->GetMesh();
824       }
825       if (GetConstructorId() == 0 && aMesh){
826         if (!AddButton->isEnabled()){
827           QListBoxItem* anItem;
828           mySimulation->ResetGrid(true);
829           for (anItem = myFacesByNodes->firstItem(); anItem != 0; anItem = anItem->next()) {
830             QStringList anIds = QStringList::split(" ", anItem->text());
831             SMESH::TPolySimulation::TVTKIds aVTKIds_faces;
832             for (QStringList::iterator it = anIds.begin(); it != anIds.end(); ++it){
833               const SMDS_MeshNode    * aNode = aMesh->FindNode( (*it).toInt() );
834               if (!aNode) continue;
835               vtkIdType aId = myActor->GetObject()->GetNodeVTKId( (*it).toInt() ) ;
836               aVTKIds.push_back(aId);
837               aVTKIds_faces.push_back(aId);
838             }
839             if(!Preview->isChecked()){
840               aType = VTK_POLYGON;
841               mySimulation->SetPosition(myActor, aType, aVTKIds_faces,false);
842             }
843           }
844           if(myFacesByNodes->count() == 0){
845             mySimulation->SetVisibility(false);
846           } else {
847             mySimulation->SetVisibility(true);
848           }
849           if(Preview->isChecked()){
850             mySimulation->SetPosition(myActor, aType, aVTKIds);
851           }
852         } else {
853           // add ids from edit line
854           QStringList anEditIds = QStringList::split( " ", myEditCurrentArgument->text(), false);
855           for ( int i = 0; i < anEditIds.count(); i++ )
856             aVTKIds.push_back( myActor->GetObject()->GetNodeVTKId( anEditIds[ i ].toInt() ));
857           aType = VTK_POLYGON;
858           mySimulation->SetPosition(myActor, aType, aVTKIds);
859         }
860       }else if(GetConstructorId() == 1 && aMesh){
861         QStringList aListId = QStringList::split( " ", myEditCurrentArgument->text(), false);
862         for ( int i = 0; i < aListId.count(); i++ )
863           {
864             const SMDS_MeshElement * anElem = aMesh->FindElement( aListId[ i ].toInt() );
865             if ( !anElem ) continue;
866             SMDSAbs_ElementType aFaceType = aMesh->GetElementType( anElem->GetID(),true );
867             if (aFaceType != SMDSAbs_Face) continue;
868               
869             SMDS_ElemIteratorPtr anIter = anElem->nodesIterator();
870             SMESH::TPolySimulation::TVTKIds aVTKIds_faces;
871             while( anIter->more() )
872                 if ( const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next() ){
873                   vtkIdType aId = myActor->GetObject()->GetNodeVTKId( aNode->GetID() ) ;
874                   aVTKIds.push_back(aId);
875                   aVTKIds_faces.push_back(aId);
876                 }
877             if(!Preview->isChecked()){
878               aType = VTK_POLYGON;
879               mySimulation->SetPosition(myActor, aType, aVTKIds_faces);
880             }
881           }
882         if(Preview->isChecked())
883           mySimulation->SetPosition(myActor, aType, aVTKIds);
884       }
885       SMESH::UpdateView();
886     }
887 }
888
889 //=================================================================================
890 // function : SetEditCurrentArgument()
891 // purpose  :
892 //=================================================================================
893 void SMESHGUI_CreatePolyhedralVolumeDlg::SetEditCurrentArgument()
894 {
895   QPushButton* send = (QPushButton*)sender();
896   if(send == SelectElementsButton) {
897     LineEditElements->setFocus() ;
898     myEditCurrentArgument = LineEditElements;
899   }
900   SelectionIntoArgument();
901 }
902
903 //=================================================================================
904 // function : DeactivateActiveDialog()
905 // purpose  :
906 //=================================================================================
907 void SMESHGUI_CreatePolyhedralVolumeDlg::DeactivateActiveDialog()
908 {
909   if ( GroupConstructors->isEnabled() ) {
910     GroupConstructors->setEnabled(false) ;
911     GroupContent->setEnabled(false) ;
912     GroupButtons->setEnabled(false) ;
913     mySimulation->SetVisibility(false);
914     mySMESHGUI->ResetState() ;    
915     mySMESHGUI->SetActiveDialogBox(0) ;
916   }
917 }
918
919
920 //=================================================================================
921 // function : ActivateThisDialog()
922 // purpose  :
923 //=================================================================================
924 void SMESHGUI_CreatePolyhedralVolumeDlg::ActivateThisDialog()
925 {
926   /* Emit a signal to deactivate the active dialog */
927   mySMESHGUI->EmitSignalDeactivateDialog() ;   
928   GroupConstructors->setEnabled(true) ;
929   GroupContent->setEnabled(true) ;
930   GroupButtons->setEnabled(true) ;
931   
932   mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
933
934   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
935     aViewWindow->SetSelectionMode( FaceSelection );
936   SelectionIntoArgument();
937 }
938
939
940 //=================================================================================
941 // function : enterEvent()
942 // purpose  :
943 //=================================================================================
944 void SMESHGUI_CreatePolyhedralVolumeDlg::enterEvent(QEvent* e)
945 {
946   if ( GroupConstructors->isEnabled() )
947     return ;  
948   ActivateThisDialog() ;
949 }
950
951
952 //=================================================================================
953 // function : closeEvent()
954 // purpose  :
955 //=================================================================================
956 void SMESHGUI_CreatePolyhedralVolumeDlg::closeEvent( QCloseEvent* e )
957 {
958   /* same than click on cancel button */
959   this->ClickOnCancel() ;
960 }
961
962
963 //=======================================================================
964 //function : hideEvent
965 //purpose  : caused by ESC key
966 //=======================================================================
967
968 void SMESHGUI_CreatePolyhedralVolumeDlg::hideEvent ( QHideEvent * e )
969 {
970   if ( !isMinimized() )
971     ClickOnCancel();
972 }
973
974
975 //=================================================================================
976 // function : GetConstructorId()
977 // purpose  : 
978 //=================================================================================
979 int SMESHGUI_CreatePolyhedralVolumeDlg::GetConstructorId()
980
981   if ( GroupConstructors != NULL && GroupConstructors->selected() != NULL )
982     return GroupConstructors->id( GroupConstructors->selected() );
983   return -1;
984 }
985
986 //=================================================================================
987 // function : onAdd()
988 // purpose  :
989 //=================================================================================
990 void SMESHGUI_CreatePolyhedralVolumeDlg::onAdd()
991 {
992   SALOME_ListIO selected;
993   mySelectionMgr->selectedObjects( selected );
994   int aNbSel = selected.Extent();
995   if (aNbSel == 0 || !myActor || myMesh->_is_nil()) return;
996   
997   if (this->checkEditLine(false) == -1) return;
998
999   busy = true;
1000   if ( !(myEditCurrentArgument->text().isEmpty()) )
1001     {
1002       myFacesByNodes->insertItem(myEditCurrentArgument->text());
1003       //myFacesByNodes->setSelected(myFacesByNodes->count() - 1, true);
1004       myNbOkElements = 1;
1005       myEditCurrentArgument->clear();
1006       AddButton->setEnabled(false);
1007       buttonOk->setEnabled( true );
1008       if(myFacesByNodes->count()>1) buttonApply->setEnabled( true );
1009     }
1010   busy = false;
1011   onListSelectionChanged();
1012   displaySimulation();
1013 }
1014
1015 //=================================================================================
1016 // function : onRemove()
1017 // purpose  :
1018 //=================================================================================
1019 void SMESHGUI_CreatePolyhedralVolumeDlg::onRemove()
1020 {
1021   busy = true;
1022   for (int i = myFacesByNodes->count(); i > 0; i--) {
1023     if (myFacesByNodes->isSelected(i-1)) {
1024       myFacesByNodes->removeItem(i-1);
1025       myNbOkElements = 1;
1026     }
1027   }
1028   if (myFacesByNodes->count() < 1){
1029     RemoveButton->setEnabled(false);
1030     buttonOk->setEnabled( false );
1031     buttonApply->setEnabled( false );
1032   } 
1033   else if (myFacesByNodes->count() == 1){
1034     buttonOk->setEnabled( false );
1035     buttonApply->setEnabled( false );
1036   }
1037   busy = false;
1038   displaySimulation();
1039 }
1040
1041 //=================================================================================
1042 // function : onListSelectionChanged()
1043 // purpose  : Called when selection in element list is changed
1044 //=================================================================================
1045 void SMESHGUI_CreatePolyhedralVolumeDlg::onListSelectionChanged()
1046 {
1047   if (busy || !myActor) return;
1048   busy = true;
1049   bool isSelected=false;
1050   SALOME_ListIO aList;
1051   mySelectionMgr->setSelectedObjects( aList );
1052   TColStd_MapOfInteger aIndexes;
1053   QListBoxItem* anItem;
1054   for (anItem = myFacesByNodes->firstItem(); anItem != 0; anItem = anItem->next()) {
1055     if (anItem->isSelected()) {
1056       isSelected = true;
1057       QStringList anIds = QStringList::split(" ", anItem->text());
1058       for (QStringList::iterator it = anIds.begin(); it != anIds.end(); ++it)
1059         aIndexes.Add((*it).toInt());
1060     }
1061   }
1062   if(isSelected) RemoveButton->setEnabled(true);
1063   else RemoveButton->setEnabled(false);
1064   mySelector->AddOrRemoveIndex(myActor->getIO(), aIndexes, true );
1065   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
1066     aViewWindow->highlight( myActor->getIO(), true, true );
1067   mySelectionMgr->clearFilters(); 
1068   aList.Append( myActor->getIO() );
1069   mySelectionMgr->setSelectedObjects( aList );
1070   
1071   busy = false;
1072 }