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