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