Salome HOME
Porting SMESH module to Qt 4
[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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File   : SMESHGUI_CreatePolyhedralVolumeDlg.cxx
23 // Author : Michael ZORIN, Open CASCADE S.A.S.
24 //
25
26 // SMESH includes
27 #include "SMESHGUI_CreatePolyhedralVolumeDlg.h"
28
29 #include "SMESHGUI.h"
30 #include "SMESHGUI_Utils.h"
31 #include "SMESHGUI_VTKUtils.h"
32 #include "SMESHGUI_MeshUtils.h"
33 #include "SMESHGUI_IdValidator.h"
34
35 #include <SMESH_Actor.h>
36 #include <SMESH_ActorUtils.h>
37 #include <SMDS_Mesh.hxx>
38
39 // SALOME GUI includes
40 #include <SUIT_Desktop.h>
41 #include <SUIT_ResourceMgr.h>
42 #include <SUIT_Session.h>
43 #include <SUIT_MessageBox.h>
44 #include <SUIT_ViewManager.h>
45
46 #include <SalomeApp_Application.h>
47 #include <LightApp_SelectionMgr.h>
48
49 #include <SVTK_ViewWindow.h>
50
51 // OCCT includes
52 #include <TColStd_ListOfInteger.hxx>
53 #include <TColStd_ListIteratorOfListOfInteger.hxx>
54
55 // VTK includes
56 #include <vtkCell.h>
57 #include <vtkIdList.h>
58 #include <vtkUnstructuredGrid.h>
59 #include <vtkDataSetMapper.h>
60 #include <vtkProperty.h>
61
62 // Qt includes
63 #include <QApplication>
64 #include <QButtonGroup>
65 #include <QGroupBox>
66 #include <QLabel>
67 #include <QLineEdit>
68 #include <QPushButton>
69 #include <QRadioButton>
70 #include <QCheckBox>
71 #include <QHBoxLayout>
72 #include <QVBoxLayout>
73 #include <QGridLayout>
74 #include <QListWidget>
75 #include <QKeyEvent>
76
77 // IDL includes
78 #include <SALOMEconfig.h>
79 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
80
81 #define SPACING 6
82 #define MARGIN  11
83
84 namespace SMESH
85 {
86   class TPolySimulation
87   {
88     SVTK_ViewWindow* myViewWindow;
89
90     SALOME_Actor *myPreviewActor;
91     vtkDataSetMapper* myMapper;
92     vtkUnstructuredGrid* myGrid;
93
94   public:
95
96     TPolySimulation(SalomeApp_Application* app)
97     {
98       SUIT_ViewManager* mgr = app->activeViewManager();
99       myViewWindow = mgr ? dynamic_cast<SVTK_ViewWindow*>( mgr->getActiveView() ) : NULL;
100
101       myGrid = vtkUnstructuredGrid::New();
102   
103       // Create and display actor
104       myMapper = vtkDataSetMapper::New();
105       myMapper->SetInput( myGrid );
106
107       myPreviewActor = SALOME_Actor::New();
108       myPreviewActor->PickableOff();
109       myPreviewActor->VisibilityOff();
110       myPreviewActor->SetMapper( myMapper );
111       myPreviewActor->SetRepresentation( 3 );
112
113       vtkFloatingPointType anRGB[3];
114       vtkProperty* aProp = vtkProperty::New();
115       GetColor( "SMESH", "selection_element_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 170, 255 ) );
116       aProp->SetColor(anRGB[0],anRGB[1],anRGB[2]);
117       myPreviewActor->SetProperty( aProp );
118       vtkFloatingPointType aFactor,aUnits;
119       myPreviewActor->SetResolveCoincidentTopology(true);
120       myPreviewActor->GetPolygonOffsetParameters(aFactor,aUnits);
121       myPreviewActor->SetPolygonOffsetParameters(aFactor,0.2*aUnits);
122       aProp->Delete();
123
124       myViewWindow->AddActor( myPreviewActor );
125       
126     }
127
128
129     typedef std::vector<vtkIdType> TVTKIds;
130     void SetPosition(SMESH_Actor* theActor, 
131                      vtkIdType theType, 
132                      const TVTKIds& theIds,
133                      bool theReset=true)
134     {
135       vtkUnstructuredGrid *aGrid = theActor->GetUnstructuredGrid();
136       myGrid->SetPoints(aGrid->GetPoints());
137
138       ResetGrid(theReset);
139       
140       vtkIdList *anIds = vtkIdList::New();
141
142       for (int i = 0, iEnd = theIds.size(); i < iEnd; i++)
143         anIds->InsertId(i,theIds[i]);
144
145       myGrid->InsertNextCell(theType,anIds);
146       if(theIds.size()!=0){
147         myGrid->InsertNextCell(theType,anIds);
148         myGrid->Modified();
149       }
150         
151       anIds->Delete();
152
153       SetVisibility(true);
154
155     }
156   
157     void ResetGrid(bool theReset=true){
158       if (theReset) myGrid->Reset();
159     }
160
161     void SetVisibility(bool theVisibility){
162       myPreviewActor->SetVisibility(theVisibility);
163       RepaintCurrentView();
164     }
165
166
167     ~TPolySimulation(){
168       if( myViewWindow )
169         myViewWindow->RemoveActor(myPreviewActor);
170
171       myPreviewActor->Delete();
172
173       myMapper->RemoveAllInputs();
174       myMapper->Delete();
175
176       myGrid->Delete();
177     }
178
179   };
180 }
181
182 //=================================================================================
183 // class    : SMESHGUI_CreatePolyhedralVolumeDlgDlg()
184 // purpose  : 
185 //=================================================================================
186 SMESHGUI_CreatePolyhedralVolumeDlg::SMESHGUI_CreatePolyhedralVolumeDlg( SMESHGUI* theModule )
187   : QDialog( SMESH::GetDesktop( theModule ) ),
188     mySMESHGUI( theModule ),
189     mySelectionMgr( SMESH::GetSelectionMgr( theModule ) )
190 {
191   QPixmap image0( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH",tr("ICON_SELECT")));
192
193   setModal(false);
194   setAttribute(Qt::WA_DeleteOnClose, true);
195   setWindowTitle( tr( "SMESH_CREATE_POLYHEDRAL_VOLUME_TITLE" ) );
196   setSizeGripEnabled( true );
197
198   QVBoxLayout* topLayout = new QVBoxLayout( this ); 
199   topLayout->setSpacing( SPACING );
200   topLayout->setMargin( MARGIN );
201
202   /***************************************************************/
203   ConstructorsBox = new QGroupBox(tr( "SMESH_ELEMENTS_TYPE" ), this);
204   GroupConstructors = new QButtonGroup(this);
205   QHBoxLayout* ConstructorsBoxLayout = new QHBoxLayout( ConstructorsBox );
206   ConstructorsBoxLayout->setSpacing( SPACING );
207   ConstructorsBoxLayout->setMargin( MARGIN );
208
209   RadioButton1 = new QRadioButton( tr( "MESH_NODE" ),  ConstructorsBox );
210   RadioButton2 = new QRadioButton( tr( "SMESH_FACE" ), ConstructorsBox );
211
212   ConstructorsBoxLayout->addWidget( RadioButton1 );
213   ConstructorsBoxLayout->addWidget( RadioButton2 );
214   GroupConstructors->addButton(RadioButton1, 0);
215   GroupConstructors->addButton(RadioButton2, 1);
216   
217   /***************************************************************/
218   GroupContent = new QGroupBox( tr( "SMESH_CONTENT" ), this );
219   QGridLayout* GroupContentLayout = new QGridLayout( GroupContent );
220   GroupContentLayout->setSpacing( SPACING );
221   GroupContentLayout->setMargin( MARGIN );
222   
223   TextLabelIds = new QLabel( tr( "SMESH_ID_NODES" ), GroupContent );
224   SelectElementsButton  = new QPushButton( GroupContent );
225   SelectElementsButton->setIcon( image0 );
226   LineEditElements  = new QLineEdit( GroupContent );
227   LineEditElements->setValidator( new SMESHGUI_IdValidator( this ) );
228
229   myFacesByNodesLabel = new QLabel( tr( "FACES_BY_NODES" ), GroupContent );
230   myFacesByNodes = new QListWidget( GroupContent);
231   myFacesByNodes->setSelectionMode( QListWidget::ExtendedSelection );
232   myFacesByNodes->setMinimumHeight( 150);
233
234   AddButton = new QPushButton( tr( "SMESH_BUT_ADD" ), GroupContent );
235   RemoveButton = new QPushButton( tr( "SMESH_BUT_REMOVE" ), GroupContent );
236
237   Preview = new QCheckBox( tr( "SMESH_POLYEDRE_PREVIEW" ), GroupContent );
238
239   GroupContentLayout->addWidget( TextLabelIds,         0, 0 );
240   GroupContentLayout->addWidget( SelectElementsButton, 0, 1 );
241   GroupContentLayout->addWidget( LineEditElements,     0, 2, 1, 2 );
242   GroupContentLayout->addWidget( myFacesByNodesLabel,  1, 0 );
243   GroupContentLayout->addWidget( myFacesByNodes,       2, 0, 3, 3 );
244   GroupContentLayout->addWidget( AddButton,            2, 3 );
245   GroupContentLayout->addWidget( RemoveButton,         3, 3 );
246   GroupContentLayout->addWidget( Preview,              5, 0, 1, 4 );
247
248   /***************************************************************/
249   GroupButtons = new QGroupBox( this );
250   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout( GroupButtons );
251   GroupButtonsLayout->setSpacing( SPACING );
252   GroupButtonsLayout->setMargin( MARGIN );
253
254   buttonOk = new QPushButton( tr( "SMESH_BUT_OK" ), GroupButtons );
255   buttonOk->setAutoDefault( true );
256   buttonOk->setDefault( true );
257   buttonApply = new QPushButton( tr( "SMESH_BUT_APPLY" ), GroupButtons );
258   buttonApply->setAutoDefault( true );
259   buttonCancel = new QPushButton( tr( "SMESH_BUT_CLOSE" ), GroupButtons );
260   buttonCancel->setAutoDefault( true );
261   buttonHelp = new QPushButton( tr("SMESH_BUT_HELP" ), GroupButtons );
262   buttonHelp->setAutoDefault(true);
263
264   GroupButtonsLayout->addWidget( buttonOk );
265   GroupButtonsLayout->addSpacing( 10 );
266   GroupButtonsLayout->addWidget( buttonApply );
267   GroupButtonsLayout->addSpacing( 10 );
268   GroupButtonsLayout->addStretch();
269   GroupButtonsLayout->addWidget( buttonCancel );
270   GroupButtonsLayout->addWidget( buttonHelp);
271
272   /***************************************************************/
273   topLayout->addWidget( ConstructorsBox );
274   topLayout->addWidget( GroupContent );
275   topLayout->addWidget( GroupButtons );
276   
277   mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
278   
279   RadioButton1->setChecked( true );
280  
281   mySMESHGUI->SetActiveDialogBox( (QDialog*)this );
282
283   myHelpFileName = "adding_nodes_and_elements_page.html#adding_polyhedrons_anchor";
284   
285   Init();
286 }
287
288 //=================================================================================
289 // function : ~SMESHGUI_CreatePolyhedralVolumeDlg()
290 // purpose  : Destroys the object and frees any allocated resources
291 //=================================================================================
292 SMESHGUI_CreatePolyhedralVolumeDlg::~SMESHGUI_CreatePolyhedralVolumeDlg()
293 {
294   delete mySimulation;
295 }
296
297 static bool busy = false;
298
299 //=================================================================================
300 // function : Init()
301 // purpose  :
302 //=================================================================================
303 void SMESHGUI_CreatePolyhedralVolumeDlg::Init()
304 {
305   myEditCurrentArgument = LineEditElements;
306   mySMESHGUI->SetActiveDialogBox( (QDialog*)this );
307
308   myNbOkElements = 0;
309   myActor = 0;
310
311   mySimulation = new SMESH::TPolySimulation( dynamic_cast<SalomeApp_Application*>( mySMESHGUI->application() ) );
312
313   /* signals and slots connections */
314   connect(buttonOk,     SIGNAL( clicked() ), SLOT( ClickOnOk() ) );
315   connect(buttonCancel, SIGNAL( clicked() ), SLOT( ClickOnCancel() ) );
316   connect(buttonApply,  SIGNAL( clicked() ), SLOT( ClickOnApply() ) );
317   connect(buttonHelp,   SIGNAL( clicked() ), SLOT( ClickOnHelp() ) );
318
319   connect(GroupConstructors, SIGNAL(buttonClicked(int) ), SLOT( ConstructorsClicked(int) ) );
320   connect(SelectElementsButton, SIGNAL( clicked() ), SLOT( SetEditCurrentArgument() ) );
321   connect(LineEditElements, SIGNAL( textChanged(const QString&) ), SLOT(onTextChange(const QString&)));
322
323   connect(myFacesByNodes, SIGNAL(selectionChanged()), this, SLOT(onListSelectionChanged()));
324   connect(AddButton, SIGNAL(clicked()), this, SLOT(onAdd()));
325   connect(RemoveButton, SIGNAL(clicked()), this, SLOT(onRemove()));
326   
327   connect( mySMESHGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
328   connect( mySelectionMgr, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
329   connect( Preview, SIGNAL(toggled(bool)), this, SLOT(ClickOnPreview(bool)));
330   /* to close dialog if study change */
331   connect( mySMESHGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) );
332   
333   ConstructorsClicked(0);
334   SelectionIntoArgument();
335 }
336
337
338 //=================================================================================
339 // function : ConstructorsClicked()
340 // purpose  : Radio button management
341 //=================================================================================
342 void SMESHGUI_CreatePolyhedralVolumeDlg::ConstructorsClicked(int constructorId)
343 {
344   //disconnect(mySelectionMgr, 0, this, 0);
345
346   SALOME_ListIO io;
347   mySelectionMgr->selectedObjects( io );
348   SALOME_ListIO aList;
349   mySelectionMgr->setSelectedObjects( aList );
350   myEditCurrentArgument->clear();
351   myNbOkElements = 0;
352   buttonApply->setEnabled(false);
353   buttonOk->setEnabled(false);
354   mySimulation->SetVisibility(false);
355
356   switch(constructorId)
357     {
358     case 0 :
359       { 
360         if ( myActor ){
361           myActor->SetPointRepresentation(true);
362         }
363         else
364           SMESH::SetPointRepresentation(true);
365         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
366           aViewWindow->SetSelectionMode(NodeSelection);
367         
368         AddButton->setEnabled(false);
369         RemoveButton->setEnabled(false);
370         TextLabelIds->setText( tr( "SMESH_ID_NODES" ) );
371         myFacesByNodesLabel->show();
372         myFacesByNodes->clear();
373         myFacesByNodes->show();
374         AddButton->show();
375         RemoveButton->show();
376         Preview->show();
377         break;
378       }
379     case 1 :
380       {
381         if( myActor ){
382           myActor->SetPointRepresentation(false);
383         } else {
384           SMESH::SetPointRepresentation(false);
385         }
386         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
387           aViewWindow->SetSelectionMode(FaceSelection);
388         
389         TextLabelIds->setText( tr( "SMESH_ID_FACES" ) );
390         myFacesByNodesLabel->hide();
391         myFacesByNodes->hide();
392         AddButton->hide();
393         RemoveButton->hide();
394         Preview->show();
395         break;
396       }
397     }
398   
399   //connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
400   mySelectionMgr->setSelectedObjects( io );
401
402   QApplication::instance()->processEvents();
403   updateGeometry();
404   resize( minimumSize() );
405 }
406
407 //=================================================================================
408 // function : ClickOnPreview()
409 // purpose  :
410 //=================================================================================
411 void SMESHGUI_CreatePolyhedralVolumeDlg::ClickOnPreview(bool theToggled){
412   Preview->setChecked(theToggled);
413   displaySimulation();
414 }
415
416 //=================================================================================
417 // function : ClickOnApply()
418 // purpose  :
419 //=================================================================================
420 void SMESHGUI_CreatePolyhedralVolumeDlg::ClickOnApply()
421 {
422   if ( myNbOkElements>0 && !mySMESHGUI->isActiveStudyLocked())
423     {
424       if(checkEditLine(false) == -1) {return;}
425       busy = true;
426       if (GetConstructorId() == 0)
427         {
428           SMESH::long_array_var anIdsOfNodes = new SMESH::long_array;
429           SMESH::long_array_var aQuantities  = new SMESH::long_array;
430
431           aQuantities->length( myFacesByNodes->count() );
432
433           TColStd_ListOfInteger aNodesIds;
434
435           int aNbQuantities = 0;
436           for (int i = 0; i < myFacesByNodes->count(); i++ ) {
437             QStringList anIds = myFacesByNodes->item(i)->text().split( " ", QString::SkipEmptyParts );
438             for (QStringList::iterator it = anIds.begin(); it != anIds.end(); ++it)
439               aNodesIds.Append( (*it).toInt() );
440
441             aQuantities[aNbQuantities++] = anIds.count();
442           }
443
444           anIdsOfNodes->length(aNodesIds.Extent());
445
446           int aNbIdsOfNodes = 0;
447           TColStd_ListIteratorOfListOfInteger It;
448           It.Initialize(aNodesIds);
449           for( ;It.More();It.Next())
450             anIdsOfNodes[aNbIdsOfNodes++] = It.Value();
451             
452           try{
453             SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
454             QApplication::setOverrideCursor(Qt::WaitCursor);
455             aMeshEditor->AddPolyhedralVolume(anIdsOfNodes, aQuantities);
456             QApplication::restoreOverrideCursor();
457           }catch(SALOME::SALOME_Exception& exc){
458             INFOS("Follow exception was cought:\n\t"<<exc.details.text);
459           }catch(std::exception& exc){
460             INFOS("Follow exception was cought:\n\t"<<exc.what());
461           }catch(...){
462             INFOS("Unknown exception was cought !!!");
463           }
464         }
465       else if (GetConstructorId() == 1)
466         {
467           SMESH::long_array_var anIdsOfFaces = new SMESH::long_array;
468           
469           QStringList aListId = myEditCurrentArgument->text().split( " ", QString::SkipEmptyParts );
470           anIdsOfFaces->length(aListId.count());
471           for ( int i = 0; i < aListId.count(); i++ )
472             anIdsOfFaces[i] = aListId[i].toInt();
473           
474           try{
475             SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
476             QApplication::setOverrideCursor(Qt::WaitCursor);
477             aMeshEditor->AddPolyhedralVolumeByFaces(anIdsOfFaces);
478             QApplication::restoreOverrideCursor();
479           }catch(SALOME::SALOME_Exception& exc){
480             INFOS("Follow exception was cought:\n\t"<<exc.details.text);
481           }catch(std::exception& exc){
482             INFOS("Follow exception was cought:\n\t"<<exc.what());
483           }catch(...){
484             INFOS("Unknown exception was cought !!!");
485           }
486         }
487       
488       //SALOME_ListIO aList;
489       //mySelectionMgr->setSelectedObjects( aList );
490       SMESH::UpdateView();
491       if( myActor ){
492         unsigned int anEntityMode = myActor->GetEntityMode();
493         myActor->SetEntityMode(SMESH_Actor::eVolumes | anEntityMode);
494       }
495       //ConstructorsClicked( GetConstructorId() );
496       busy = false;
497     }
498 }
499
500 //=================================================================================
501 // function : ClickOnOk()
502 // purpose  :
503 //=================================================================================
504 void SMESHGUI_CreatePolyhedralVolumeDlg::ClickOnOk()
505 {
506   if(checkEditLine(false) == -1) {return;}
507   ClickOnApply();
508   ClickOnCancel();
509 }
510
511         
512 //=================================================================================
513 // function : ClickOnCancel()
514 // purpose  :
515 //=================================================================================
516 void SMESHGUI_CreatePolyhedralVolumeDlg::ClickOnCancel()
517 {
518   mySelectionMgr->clearFilters();
519   //SALOME_ListIO aList;
520   //mySelectionMgr->setSelectedObjects( aList );
521   SMESH::SetPointRepresentation(false);
522   mySimulation->SetVisibility(false);
523   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
524     aViewWindow->SetSelectionMode( ActorSelection );
525   disconnect( mySelectionMgr, 0, this, 0 );
526   mySMESHGUI->ResetState();
527   reject();
528 }
529
530 //=================================================================================
531 // function : ClickOnHelp()
532 // purpose  :
533 //=================================================================================
534 void SMESHGUI_CreatePolyhedralVolumeDlg::ClickOnHelp()
535 {
536   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
537   if (app) 
538     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
539   else {
540     QString platform;
541 #ifdef WIN32
542     platform = "winapplication";
543 #else
544     platform = "application";
545 #endif
546     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
547                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
548                              arg(app->resourceMgr()->stringValue("ExternalBrowser",
549                                                                  platform)).
550                              arg(myHelpFileName));
551   }
552 }
553
554 //=======================================================================
555 //function : onTextChange
556 //purpose  : 
557 //=======================================================================
558
559 void SMESHGUI_CreatePolyhedralVolumeDlg::onTextChange(const QString& theNewText)
560 {
561   if ( busy ) return;
562   if (checkEditLine() == -1) return;
563   busy = true;
564
565   mySimulation->SetVisibility(false);
566
567   SMDS_Mesh* aMesh = 0;
568   if ( myActor )
569     aMesh = myActor->GetObject()->GetMesh();
570
571   if (GetConstructorId() == 0)
572     {
573       if ( aMesh ) {
574         TColStd_MapOfInteger newIndices;
575       
576         QStringList aListId = theNewText.split( " ", QString::SkipEmptyParts );
577         for ( int i = 0; i < aListId.count(); i++ ) {
578           const SMDS_MeshNode * n = aMesh->FindNode( aListId[ i ].toInt() );
579           if ( n ) {
580             newIndices.Add(n->GetID());
581             myNbOkElements++;
582           }
583         }
584       
585         mySelector->AddOrRemoveIndex( myActor->getIO(), newIndices, false );
586       
587         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
588           aViewWindow->highlight( myActor->getIO(), true, true );
589       
590         if ( myNbOkElements>0 && aListId.count()>=3)
591           AddButton->setEnabled(true);
592         else
593           AddButton->setEnabled(false);
594       
595         displaySimulation();
596       }
597     } else if (GetConstructorId() == 1)
598       {
599         myNbOkElements = 0;
600         buttonOk->setEnabled( false );
601         buttonApply->setEnabled( false );
602       
603         // check entered ids of faces and hilight them
604         QStringList aListId;
605         if ( aMesh ) {
606           TColStd_MapOfInteger newIndices;
607       
608           aListId = theNewText.split( " ", QString::SkipEmptyParts );
609
610           for ( int i = 0; i < aListId.count(); i++ ) {
611             const SMDS_MeshElement * e = aMesh->FindElement( aListId[ i ].toInt() );
612             if ( e ) {
613               newIndices.Add(e->GetID());
614               myNbOkElements++;  
615             }
616           }
617
618           mySelector->AddOrRemoveIndex( myActor->getIO(), newIndices, false );
619           if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
620             aViewWindow->highlight( myActor->getIO(), true, true );
621       
622           if ( myNbOkElements ) {
623             if (aListId.count()>1){ 
624               buttonOk->setEnabled( true );
625               buttonApply->setEnabled( true );
626             }
627             else{
628               buttonOk->setEnabled( false );
629               buttonApply->setEnabled( false );
630             }
631             if(aListId.count()>1)
632               displaySimulation();
633           }
634         }
635       }
636   busy = false;
637 }
638
639 //=================================================================================
640 // function : SelectionIntoArgument()
641 // purpose  : Called when selection as changed or other case
642 //=================================================================================
643 void SMESHGUI_CreatePolyhedralVolumeDlg::SelectionIntoArgument()
644 {
645   if ( busy ) return;
646   
647   // clear
648   
649   if (GetConstructorId() == 1 || myFacesByNodes->count() <= 1)
650     {
651       myNbOkElements = 0;
652       AddButton->setEnabled(false);
653       buttonOk->setEnabled( false );
654       buttonApply->setEnabled( false );
655     }
656
657   myActor = 0;
658
659   busy = true;
660   myEditCurrentArgument->setText( "" );
661   busy = false;
662   if ( !GroupButtons->isEnabled() ) // inactive
663     return;
664   
665   mySimulation->SetVisibility(false);
666   
667   // get selected mesh
668   
669   SALOME_ListIO selected;
670   mySelectionMgr->selectedObjects( selected );
671   int nbSel = selected.Extent();
672   if(nbSel != 1){
673     return;
674   }
675   
676   myMesh = SMESH::GetMeshByIO( selected.First() );
677   if ( myMesh->_is_nil() )
678     return;
679   
680   myActor = SMESH::FindActorByObject(myMesh);
681   if ( !myActor )
682     return;
683   
684   // get selected nodes/faces
685   QString aString = "";
686   int anbNodes=0,aNbFaces=0;
687   switch(GetConstructorId()){
688   case 0:{
689     anbNodes = SMESH::GetNameOfSelectedNodes(mySelector, myActor->getIO(), aString);
690     if (anbNodes >= 3)
691       AddButton->setEnabled(true);
692     else if (anbNodes < 3){
693       AddButton->setEnabled(false);
694     }
695     busy = true;
696     myEditCurrentArgument->setText( aString );
697     if (checkEditLine() == -1) {busy = false;return;}
698     busy = false;
699     break;
700   }
701   case 1:{
702     // get selected faces
703     aNbFaces = SMESH::GetNameOfSelectedElements(mySelector, myActor->getIO(), aString);
704     if (aNbFaces<=1){
705       buttonOk->setEnabled( false );
706       buttonApply->setEnabled( false );
707     } else {
708       buttonOk->setEnabled( true );
709       buttonApply->setEnabled( true );
710     }
711     busy = true;
712     myEditCurrentArgument->setText( aString );
713     if (checkEditLine() == -1) {busy = false;return;}
714     busy = false;
715     
716     // OK
717     myNbOkElements = 1;
718     break;
719   }
720   default: return;
721   }
722   if(anbNodes>2 || aNbFaces>1)
723     displaySimulation();
724 }
725
726 /*\brief int SMESHGUI_CreatePolyhedralVolumeDlg::checkEditLine()
727  * Checking of indices in edit line.
728  * If incorecct indices in edit line warning message appear and myEditCurrentArgument remove last index.
729  * \retval 1 - if all ok(or no indices in edit line), -1 - if there are incorrect indices.
730  */
731 int SMESHGUI_CreatePolyhedralVolumeDlg::checkEditLine(bool checkLast)
732 {
733   QString aString = "";
734   SMDS_Mesh* aMesh = 0;
735   
736   if(myMesh->_is_nil()) return 1;
737   if(!myActor){
738     myActor = SMESH::FindActorByObject(myMesh);
739     if(!myActor)
740       return 1;
741   }
742     
743   aMesh = myActor->GetObject()->GetMesh();
744
745   // checking for nodes
746   if (checkLast && myEditCurrentArgument->text().right(1) != QString(" ") ) return 1;
747   QStringList aListId = myEditCurrentArgument->text().split( " ", QString::SkipEmptyParts );
748   for ( int i = 0; i < aListId.count(); i++ ){
749     switch (GetConstructorId()){
750     case 0:{ // nodes
751       const SMDS_MeshNode    * aNode = aMesh->FindNode( aListId[ i ].toInt() );
752       if( !aNode ){
753         SUIT_MessageBox::warning(this,
754                                  tr("SMESH_POLYEDRE_CREATE_ERROR"),
755                                  tr("The incorrect indices of nodes!"));
756         
757         myEditCurrentArgument->clear();
758         myEditCurrentArgument->setText( aString );
759         return -1;
760       }
761
762       break;
763     }
764     case 1:{ // faces
765       bool aElemIsOK = true;
766       const SMDS_MeshElement * aElem = aMesh->FindElement( aListId[ i ].toInt() );
767       if (!aElem)
768         {
769           aElemIsOK = false;
770         }
771       else
772         {
773           SMDSAbs_ElementType aType = aMesh->GetElementType( aElem->GetID(),true );
774           if (aType != SMDSAbs_Face){
775             aElemIsOK = false;
776           }
777         }
778       if (!aElemIsOK){
779         SUIT_MessageBox::warning(this,
780                                  tr("SMESH_POLYEDRE_CREATE_ERROR"),
781                                  tr("The incorrect indices of faces!"));
782         
783         myEditCurrentArgument->clear();
784         myEditCurrentArgument->setText( aString );
785         return -1;
786       }
787       break;
788     }
789     }
790     aString += aListId[ i ] + " "; 
791   }
792
793   return 1;
794 }
795
796 //=======================================================================
797 //function : displaySimulation
798 //purpose  : 
799 //=======================================================================
800 void SMESHGUI_CreatePolyhedralVolumeDlg::displaySimulation()
801 {
802   if ( (myNbOkElements || AddButton->isEnabled()) && GroupButtons->isEnabled() && myActor)
803     {
804       SMESH::TPolySimulation::TVTKIds aVTKIds;
805       vtkIdType aType = VTK_CONVEX_POINT_SET;
806       SMDS_Mesh* aMesh = 0;
807       if ( myActor ){
808         aMesh = myActor->GetObject()->GetMesh();
809       }
810       if (GetConstructorId() == 0 && aMesh){
811         if (!AddButton->isEnabled()){
812           mySimulation->ResetGrid(true);
813           for (int i = 0; i < myFacesByNodes->count(); i++) {
814             QStringList anIds = myFacesByNodes->item(i)->text().split( " ", QString::SkipEmptyParts );
815             SMESH::TPolySimulation::TVTKIds aVTKIds_faces;
816             for (QStringList::iterator it = anIds.begin(); it != anIds.end(); ++it){
817               const SMDS_MeshNode* aNode = aMesh->FindNode( (*it).toInt() );
818               if (!aNode) continue;
819               vtkIdType aId = myActor->GetObject()->GetNodeVTKId( (*it).toInt() );
820               aVTKIds.push_back(aId);
821               aVTKIds_faces.push_back(aId);
822             }
823             if(!Preview->isChecked()){
824               aType = VTK_POLYGON;
825               mySimulation->SetPosition(myActor, aType, aVTKIds_faces,false);
826             }
827           }
828           if(myFacesByNodes->count() == 0){
829             mySimulation->SetVisibility(false);
830           } else {
831             mySimulation->SetVisibility(true);
832           }
833           if(Preview->isChecked()){
834             mySimulation->SetPosition(myActor, aType, aVTKIds);
835           }
836         } else {
837           // add ids from edit line
838           QStringList anEditIds = myEditCurrentArgument->text().split( " ", QString::SkipEmptyParts );
839           for ( int i = 0; i < anEditIds.count(); i++ )
840             aVTKIds.push_back( myActor->GetObject()->GetNodeVTKId( anEditIds[ i ].toInt() ));
841           aType = VTK_POLYGON;
842           mySimulation->SetPosition(myActor, aType, aVTKIds);
843         }
844       }else if(GetConstructorId() == 1 && aMesh){
845         QStringList aListId = myEditCurrentArgument->text().split( " ", QString::SkipEmptyParts );
846         for ( int i = 0; i < aListId.count(); i++ )
847           {
848             const SMDS_MeshElement * anElem = aMesh->FindElement( aListId[ i ].toInt() );
849             if ( !anElem ) continue;
850             SMDSAbs_ElementType aFaceType = aMesh->GetElementType( anElem->GetID(),true );
851             if (aFaceType != SMDSAbs_Face) continue;
852               
853             SMDS_ElemIteratorPtr anIter = anElem->nodesIterator();
854             SMESH::TPolySimulation::TVTKIds aVTKIds_faces;
855             while( anIter->more() )
856               if ( const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next() ){
857                 vtkIdType aId = myActor->GetObject()->GetNodeVTKId( aNode->GetID() );
858                 aVTKIds.push_back(aId);
859                 aVTKIds_faces.push_back(aId);
860               }
861             if(!Preview->isChecked()){
862               aType = VTK_POLYGON;
863               mySimulation->SetPosition(myActor, aType, aVTKIds_faces);
864             }
865           }
866         if(Preview->isChecked())
867           mySimulation->SetPosition(myActor, aType, aVTKIds);
868       }
869       SMESH::UpdateView();
870     }
871 }
872
873 //=================================================================================
874 // function : SetEditCurrentArgument()
875 // purpose  :
876 //=================================================================================
877 void SMESHGUI_CreatePolyhedralVolumeDlg::SetEditCurrentArgument()
878 {
879   QPushButton* send = (QPushButton*)sender();
880   if(send == SelectElementsButton) {
881     LineEditElements->setFocus();
882     myEditCurrentArgument = LineEditElements;
883   }
884   SelectionIntoArgument();
885 }
886
887 //=================================================================================
888 // function : DeactivateActiveDialog()
889 // purpose  :
890 //=================================================================================
891 void SMESHGUI_CreatePolyhedralVolumeDlg::DeactivateActiveDialog()
892 {
893   if ( ConstructorsBox->isEnabled() ) {
894     ConstructorsBox->setEnabled(false);
895     GroupContent->setEnabled(false);
896     GroupButtons->setEnabled(false);
897     mySimulation->SetVisibility(false);
898     mySMESHGUI->ResetState();    
899     mySMESHGUI->SetActiveDialogBox(0);
900   }
901 }
902
903
904 //=================================================================================
905 // function : ActivateThisDialog()
906 // purpose  :
907 //=================================================================================
908 void SMESHGUI_CreatePolyhedralVolumeDlg::ActivateThisDialog()
909 {
910   /* Emit a signal to deactivate the active dialog */
911   mySMESHGUI->EmitSignalDeactivateDialog();   
912   ConstructorsBox->setEnabled(true);
913   GroupContent->setEnabled(true);
914   GroupButtons->setEnabled(true);
915   
916   mySMESHGUI->SetActiveDialogBox( (QDialog*)this );
917
918   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
919     aViewWindow->SetSelectionMode( FaceSelection );
920   SelectionIntoArgument();
921 }
922
923
924 //=================================================================================
925 // function : enterEvent()
926 // purpose  :
927 //=================================================================================
928 void SMESHGUI_CreatePolyhedralVolumeDlg::enterEvent(QEvent* e)
929 {
930   if ( ConstructorsBox->isEnabled() )
931     return;  
932   ActivateThisDialog();
933 }
934
935
936 //=================================================================================
937 // function : closeEvent()
938 // purpose  :
939 //=================================================================================
940 void SMESHGUI_CreatePolyhedralVolumeDlg::closeEvent( QCloseEvent* e )
941 {
942   /* same than click on cancel button */
943   ClickOnCancel();
944 }
945
946
947 //=======================================================================
948 //function : hideEvent
949 //purpose  : caused by ESC key
950 //=======================================================================
951
952 void SMESHGUI_CreatePolyhedralVolumeDlg::hideEvent ( QHideEvent * e )
953 {
954   if ( !isMinimized() )
955     ClickOnCancel();
956 }
957
958
959 //=================================================================================
960 // function : GetConstructorId()
961 // purpose  : 
962 //=================================================================================
963 int SMESHGUI_CreatePolyhedralVolumeDlg::GetConstructorId()
964
965   return GroupConstructors->checkedId();
966 }
967
968 //=================================================================================
969 // function : onAdd()
970 // purpose  :
971 //=================================================================================
972 void SMESHGUI_CreatePolyhedralVolumeDlg::onAdd()
973 {
974   SALOME_ListIO selected;
975   mySelectionMgr->selectedObjects( selected );
976   int aNbSel = selected.Extent();
977   if (aNbSel == 0 || !myActor || myMesh->_is_nil()) return;
978   
979   if (checkEditLine(false) == -1) return;
980
981   busy = true;
982   if ( !(myEditCurrentArgument->text().isEmpty()) )
983     {
984       myFacesByNodes->addItem(myEditCurrentArgument->text());
985       //myFacesByNodes->setSelected(myFacesByNodes->count() - 1, true);
986       myNbOkElements = 1;
987       myEditCurrentArgument->clear();
988       AddButton->setEnabled(false);
989       buttonOk->setEnabled( true );
990       if(myFacesByNodes->count()>1) buttonApply->setEnabled( true );
991     }
992   busy = false;
993   onListSelectionChanged();
994   displaySimulation();
995 }
996
997 //=================================================================================
998 // function : onRemove()
999 // purpose  :
1000 //=================================================================================
1001 void SMESHGUI_CreatePolyhedralVolumeDlg::onRemove()
1002 {
1003   busy = true;
1004   QList<QListWidgetItem*> selItems = myFacesByNodes->selectedItems();
1005   QListWidgetItem* anItem;
1006
1007   if ( selItems.count() > 0 ) myNbOkElements = 1;
1008
1009   foreach( anItem, selItems )
1010     delete anItem;
1011
1012   RemoveButton->setEnabled( myFacesByNodes->count() > 0 );
1013   buttonOk->setEnabled( myFacesByNodes->count() > 1 );
1014   buttonApply->setEnabled( myFacesByNodes->count() > 1 );
1015
1016   busy = false;
1017   displaySimulation();
1018 }
1019
1020 //=================================================================================
1021 // function : onListSelectionChanged()
1022 // purpose  : Called when selection in element list is changed
1023 //=================================================================================
1024 void SMESHGUI_CreatePolyhedralVolumeDlg::onListSelectionChanged()
1025 {
1026   if (busy || !myActor) return;
1027   busy = true;
1028
1029   SALOME_ListIO aList;
1030   mySelectionMgr->setSelectedObjects( aList );
1031   TColStd_MapOfInteger aIndexes;
1032
1033   QList<QListWidgetItem*> selItems = myFacesByNodes->selectedItems();
1034   QListWidgetItem* anItem;
1035   foreach( anItem, selItems ) {
1036     QStringList anIds = anItem->text().split( " ", QString::SkipEmptyParts );
1037     for (QStringList::iterator it = anIds.begin(); it != anIds.end(); ++it)
1038       aIndexes.Add((*it).toInt());
1039   }
1040   RemoveButton->setEnabled(selItems.count() > 0);
1041   mySelector->AddOrRemoveIndex(myActor->getIO(), aIndexes, true );
1042   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
1043     aViewWindow->highlight( myActor->getIO(), true, true );
1044   mySelectionMgr->clearFilters(); 
1045   aList.Append( myActor->getIO() );
1046   mySelectionMgr->setSelectedObjects( aList );
1047   
1048   busy = false;
1049 }
1050
1051 //=================================================================================
1052 // function : keyPressEvent()
1053 // purpose  :
1054 //=================================================================================
1055 void SMESHGUI_CreatePolyhedralVolumeDlg::keyPressEvent( QKeyEvent* e )
1056 {
1057   QDialog::keyPressEvent( e );
1058   if ( e->isAccepted() )
1059     return;
1060
1061   if ( e->key() == Qt::Key_F1 ) {
1062     e->accept();
1063     ClickOnHelp();
1064   }
1065 }