Salome HOME
0626e4575c3b4e4e3adf984e833b58f4f9ac0303
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_ExtrusionDlg.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_ExtrusionDlg.cxx
24 // Author : Michael ZORIN, Open CASCADE S.A.S.
25 // SMESH includes
26 //
27 #include "SMESHGUI_ExtrusionDlg.h"
28
29 #include "SMESHGUI.h"
30 #include "SMESHGUI_Utils.h"
31 #include "SMESHGUI_VTKUtils.h"
32 #include "SMESHGUI_MeshUtils.h"
33 #include "SMESHGUI_SpinBox.h"
34 #include "SMESHGUI_IdValidator.h"
35 #include "SMESHGUI_FilterDlg.h"
36
37 #include <SMESH_Actor.h>
38 #include <SMESH_TypeFilter.hxx>
39 #include <SMESH_LogicalFilter.hxx>
40
41 #include <SMDS_Mesh.hxx>
42
43 // SALOME GUI includes
44 #include <SUIT_ResourceMgr.h>
45 #include <SUIT_Desktop.h>
46 #include <SUIT_MessageBox.h>
47 #include <SUIT_Session.h>
48 #include <SUIT_OverrideCursor.h>
49
50 #include <LightApp_Application.h>
51 #include <LightApp_SelectionMgr.h>
52
53 #include <SVTK_ViewModel.h>
54 #include <SVTK_ViewWindow.h>
55
56 #include <SalomeApp_IntSpinBox.h>
57
58 // OCCT includes
59 #include <TColStd_MapOfInteger.hxx>
60 #include <TColStd_IndexedMapOfInteger.hxx>
61 #include <gp_XYZ.hxx>
62
63 // Qt includes
64 #include <QApplication>
65 #include <QButtonGroup>
66 #include <QGroupBox>
67 #include <QLabel>
68 #include <QLineEdit>
69 #include <QPushButton>
70 #include <QRadioButton>
71 #include <QCheckBox>
72 #include <QHBoxLayout>
73 #include <QVBoxLayout>
74 #include <QGridLayout>
75 #include <QKeyEvent>
76
77 // IDL includes
78 #include <SALOMEconfig.h>
79 #include CORBA_SERVER_HEADER(SMESH_Group)
80 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
81
82 #define SPACING 6
83 #define MARGIN  11
84
85 //=================================================================================
86 // function : SMESHGUI_ExtrusionDlg()
87 // purpose  : constructor
88 //=================================================================================
89 SMESHGUI_ExtrusionDlg::SMESHGUI_ExtrusionDlg (SMESHGUI* theModule)
90   : QDialog( SMESH::GetDesktop( theModule ) ),
91     mySMESHGUI( theModule ),
92     mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
93     myFilterDlg( 0 ),
94     mySelectedObject(SMESH::SMESH_IDSource::_nil())
95 {
96   QPixmap image0 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_DLG_EDGE")));
97   QPixmap image1 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_DLG_TRIANGLE")));
98   QPixmap image2 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
99
100   setModal( false );
101   setAttribute( Qt::WA_DeleteOnClose, true );
102   setWindowTitle(tr("EXTRUSION_ALONG_LINE"));
103   setSizeGripEnabled(true);
104
105   QVBoxLayout* SMESHGUI_ExtrusionDlgLayout = new QVBoxLayout(this);
106   SMESHGUI_ExtrusionDlgLayout->setSpacing(SPACING);
107   SMESHGUI_ExtrusionDlgLayout->setMargin(MARGIN);
108
109   /***************************************************************/
110   ConstructorsBox = new QGroupBox(tr("SMESH_EXTRUSION"), this);
111   GroupConstructors = new QButtonGroup(this);
112   QHBoxLayout* ConstructorsBoxLayout = new QHBoxLayout(ConstructorsBox);
113   ConstructorsBoxLayout->setSpacing(SPACING);
114   ConstructorsBoxLayout->setMargin(MARGIN);
115
116   RadioButton1= new QRadioButton(ConstructorsBox);
117   RadioButton1->setIcon(image0);
118   RadioButton2= new QRadioButton(ConstructorsBox);
119   RadioButton2->setIcon(image1);
120
121   ConstructorsBoxLayout->addWidget(RadioButton1);
122   ConstructorsBoxLayout->addWidget(RadioButton2);
123
124   GroupConstructors->addButton(RadioButton1, 0);
125   GroupConstructors->addButton(RadioButton2, 1);
126
127   /***************************************************************/
128   GroupButtons = new QGroupBox(this);
129   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
130   GroupButtonsLayout->setSpacing(SPACING);
131   GroupButtonsLayout->setMargin(MARGIN);
132
133   buttonOk = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), GroupButtons);
134   buttonOk->setAutoDefault(true);
135   buttonOk->setDefault(true);
136   buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
137   buttonApply->setAutoDefault(true);
138   buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
139   buttonCancel->setAutoDefault(true);
140   buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
141   buttonHelp->setAutoDefault(true);
142
143   GroupButtonsLayout->addWidget(buttonOk);
144   GroupButtonsLayout->addSpacing(10);
145   GroupButtonsLayout->addWidget(buttonApply);
146   GroupButtonsLayout->addSpacing(10);
147   GroupButtonsLayout->addStretch();
148   GroupButtonsLayout->addWidget(buttonCancel);
149   GroupButtonsLayout->addWidget(buttonHelp);
150
151   /***************************************************************/
152   GroupArguments = new QGroupBox(tr("EXTRUSION_1D"), this);
153   QGridLayout* GroupArgumentsLayout = new QGridLayout(GroupArguments);
154   GroupArgumentsLayout->setSpacing(SPACING);
155   GroupArgumentsLayout->setMargin(MARGIN);
156
157   myIdValidator = new SMESHGUI_IdValidator(this);
158
159   // Controls for elements selection
160   TextLabelElements = new QLabel(tr("SMESH_ID_ELEMENTS"), GroupArguments);
161
162   SelectElementsButton = new QPushButton(GroupArguments);
163   SelectElementsButton->setIcon(image2);
164
165   LineEditElements = new QLineEdit(GroupArguments);
166   LineEditElements->setValidator(myIdValidator);
167   QPushButton* filterBtn = new QPushButton( tr( "SMESH_BUT_FILTER" ), GroupArguments );
168   connect(filterBtn,   SIGNAL(clicked()), this, SLOT(setFilters()));
169
170   // Control for the whole mesh selection
171   CheckBoxMesh = new QCheckBox(tr("SMESH_SELECT_WHOLE_MESH"), GroupArguments);
172
173   //Control for the Distance selection
174   TextLabelDistance = new QLabel(tr("SMESH_DISTANCE"), GroupArguments);
175   
176   TextLabelVector = new QLabel(tr("SMESH_VECTOR"), GroupArguments);
177   TextLabelDx = new QLabel(tr("SMESH_X"), GroupArguments);
178   SpinBox_Dx = new SMESHGUI_SpinBox(GroupArguments);
179   
180   TextLabelDy = new QLabel(tr("SMESH_Y"), GroupArguments);
181   SpinBox_Dy = new SMESHGUI_SpinBox(GroupArguments);
182
183   TextLabelDz = new QLabel(tr("SMESH_Z"), GroupArguments);
184   SpinBox_Dz = new SMESHGUI_SpinBox(GroupArguments);
185
186   // Controls for vector selection
187
188   SelectVectorButton = new QPushButton(GroupArguments);
189   SelectVectorButton->setIcon(image2);
190
191   TextLabelVx = new QLabel(tr("SMESH_DX"), GroupArguments);
192   SpinBox_Vx = new SMESHGUI_SpinBox(GroupArguments);
193
194   TextLabelVy = new QLabel(tr("SMESH_DY"), GroupArguments);
195   SpinBox_Vy = new SMESHGUI_SpinBox(GroupArguments);
196
197   TextLabelVz = new QLabel(tr("SMESH_DZ"), GroupArguments);
198   SpinBox_Vz = new SMESHGUI_SpinBox(GroupArguments);
199
200   // Controls for nb. steps defining
201   TextLabelNbSteps = new QLabel(tr("SMESH_NUMBEROFSTEPS"), GroupArguments);
202   SpinBox_NbSteps = new SalomeApp_IntSpinBox(GroupArguments);
203
204   // CheckBox for groups generation
205   MakeGroupsCheck = new QCheckBox(tr("SMESH_MAKE_GROUPS"), GroupArguments);
206
207   GroupArgumentsLayout->addWidget(TextLabelElements,    0, 0);
208   GroupArgumentsLayout->addWidget(SelectElementsButton, 0, 1);
209   GroupArgumentsLayout->addWidget(LineEditElements,     0, 2, 1, 5);
210   GroupArgumentsLayout->addWidget(filterBtn,            0, 7);
211   GroupArgumentsLayout->addWidget(CheckBoxMesh,         1, 0, 1, 8);
212   GroupArgumentsLayout->addWidget(TextLabelDistance,    2, 0);
213   GroupArgumentsLayout->addWidget(TextLabelDx,          2, 2);
214   GroupArgumentsLayout->addWidget(SpinBox_Dx,           2, 3);
215   GroupArgumentsLayout->addWidget(TextLabelDy,          2, 4);
216   GroupArgumentsLayout->addWidget(SpinBox_Dy,           2, 5);
217   GroupArgumentsLayout->addWidget(TextLabelDz,          2, 6);
218   GroupArgumentsLayout->addWidget(SpinBox_Dz,           2, 7);
219   GroupArgumentsLayout->addWidget(TextLabelVector,      3, 0);
220   GroupArgumentsLayout->addWidget(SelectVectorButton,   3, 1);
221   GroupArgumentsLayout->addWidget(TextLabelVx,          3, 2);
222   GroupArgumentsLayout->addWidget(SpinBox_Vx,           3, 3);
223   GroupArgumentsLayout->addWidget(TextLabelVy,          3, 4);
224   GroupArgumentsLayout->addWidget(SpinBox_Vy,           3, 5);
225   GroupArgumentsLayout->addWidget(TextLabelVz,          3, 6);
226   GroupArgumentsLayout->addWidget(SpinBox_Vz,           3, 7);
227   GroupArgumentsLayout->addWidget(TextLabelNbSteps,     4, 0);
228   GroupArgumentsLayout->addWidget(SpinBox_NbSteps,      4, 2, 1, 6);
229   GroupArgumentsLayout->addWidget(MakeGroupsCheck,      5, 0, 1, 8);
230
231   /***************************************************************/
232   SMESHGUI_ExtrusionDlgLayout->addWidget(ConstructorsBox);
233   SMESHGUI_ExtrusionDlgLayout->addWidget(GroupArguments);
234   SMESHGUI_ExtrusionDlgLayout->addWidget(GroupButtons);
235
236   /* Initialisations */
237   SpinBox_Vx->RangeStepAndValidator(-1, 1, 0.01, 3);
238   SpinBox_Vy->RangeStepAndValidator(-1, 1, 0.01, 3);
239   SpinBox_Vz->RangeStepAndValidator(-1, 1, 0.01, 3);
240
241   SpinBox_Dx->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, 3);
242   SpinBox_Dy->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, 3);
243   SpinBox_Dz->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, 3);
244   
245   SpinBox_NbSteps->setRange(1, 999999);
246
247   RadioButton1->setChecked(true);
248   MakeGroupsCheck->setChecked(true);
249
250   mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
251
252   mySMESHGUI->SetActiveDialogBox(this);
253
254   // Costruction of the logical filter for the elements: mesh/sub-mesh/group
255   SMESH_TypeFilter* aMeshOrSubMeshFilter = new SMESH_TypeFilter (MESHorSUBMESH);
256   SMESH_TypeFilter* aSmeshGroupFilter    = new SMESH_TypeFilter (GROUP);
257
258   QList<SUIT_SelectionFilter*> aListOfFilters;
259   if (aMeshOrSubMeshFilter) aListOfFilters.append(aMeshOrSubMeshFilter);
260   if (aSmeshGroupFilter)    aListOfFilters.append(aSmeshGroupFilter);
261
262   myMeshOrSubMeshOrGroupFilter =
263     new SMESH_LogicalFilter (aListOfFilters, SMESH_LogicalFilter::LO_OR);
264
265   myHelpFileName = "extrusion_page.html";
266
267   Init();
268
269   /***************************************************************/
270   // signals and slots connections
271   connect(buttonOk,     SIGNAL(clicked()), this, SLOT(ClickOnOk()));
272   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
273   connect(buttonApply,  SIGNAL(clicked()), this, SLOT(ClickOnApply()));
274   connect(buttonHelp,   SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
275
276   // to update state of the Ok & Apply buttons
277   connect(SpinBox_Vx, SIGNAL(valueChanged(double)), SLOT(CheckIsEnable()));
278   connect(SpinBox_Vy, SIGNAL(valueChanged(double)), SLOT(CheckIsEnable()));
279   connect(SpinBox_Vz, SIGNAL(valueChanged(double)), SLOT(CheckIsEnable()));
280   connect(SpinBox_Dx, SIGNAL(valueChanged(double)), SLOT(CheckIsEnable()));
281   connect(SpinBox_Dy, SIGNAL(valueChanged(double)), SLOT(CheckIsEnable()));
282   connect(SpinBox_Dz, SIGNAL(valueChanged(double)), SLOT(CheckIsEnable()));
283
284   connect(GroupConstructors,    SIGNAL(buttonClicked(int)), SLOT(ConstructorsClicked(int)));
285   connect(SelectElementsButton, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
286   connect(SelectVectorButton,   SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
287   connect(mySMESHGUI,           SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
288   connect(mySelectionMgr,       SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
289   /* to close dialog if study change */
290   connect(mySMESHGUI,           SIGNAL(SignalCloseAllDialogs()),   this, SLOT(ClickOnCancel()));
291   connect(LineEditElements,     SIGNAL(textChanged(const QString&)), SLOT(onTextChange(const QString&)));
292   connect(CheckBoxMesh,         SIGNAL(toggled(bool)),               SLOT(onSelectMesh(bool)));
293
294   /***************************************************************/
295   
296   ConstructorsClicked(0);
297   SelectionIntoArgument();
298 }
299
300 //=================================================================================
301 // function : ~SMESHGUI_ExtrusionDlg()
302 // purpose  : destructor
303 //=================================================================================
304 SMESHGUI_ExtrusionDlg::~SMESHGUI_ExtrusionDlg()
305 {
306   if ( myFilterDlg != 0 ) {
307     myFilterDlg->setParent( 0 );
308     delete myFilterDlg;
309   }
310 }
311
312 //=================================================================================
313 // function : Init()
314 // purpose  : initialization
315 //=================================================================================
316 void SMESHGUI_ExtrusionDlg::Init (bool ResetControls)
317 {
318   myBusy = false;
319
320   LineEditElements->clear();
321   myNbOkElements = 0;
322
323   myActor = 0;
324   myMesh = SMESH::SMESH_Mesh::_nil();
325
326   if (ResetControls) {
327     SpinBox_NbSteps->setValue(1);
328     SpinBox_Dx->SetValue(0);
329     SpinBox_Dy->SetValue(0);
330     SpinBox_Dz->SetValue(0);
331     SpinBox_Vx->SetValue(0);
332     SpinBox_Vy->SetValue(0);
333     SpinBox_Vz->SetValue(0);
334
335     CheckBoxMesh->setChecked(false);
336     onSelectMesh(false);
337   }
338
339   CheckIsEnable();
340 }
341
342 //=================================================================================
343 // function : CheckIsEnable()
344 // purpose  : Check whether the Ok and Apply buttons should be enabled or not
345 //=================================================================================
346 void SMESHGUI_ExtrusionDlg::CheckIsEnable()
347 {
348   
349   double aX = SpinBox_Vx->GetValue()*SpinBox_Dx->GetValue();
350   double aY = SpinBox_Vy->GetValue()*SpinBox_Dy->GetValue();
351   double aZ = SpinBox_Vz->GetValue()*SpinBox_Dz->GetValue();
352   double aModule = sqrt(aX*aX + aY*aY + aZ*aZ);
353   
354   bool anIsEnable = myNbOkElements > 0 && aModule > 1.0E-38;
355
356   buttonOk->setEnabled(anIsEnable);
357   buttonApply->setEnabled(anIsEnable);
358 }
359
360 //=================================================================================
361 // function : ConstructorsClicked()
362 // purpose  : Radio button management
363 //=================================================================================
364 void SMESHGUI_ExtrusionDlg::ConstructorsClicked (int constructorId)
365 {
366   disconnect(mySelectionMgr, 0, this, 0);
367
368   switch (constructorId) {
369   case 0:
370     {
371       GroupArguments->setTitle(tr("EXTRUSION_1D"));
372       if (!CheckBoxMesh->isChecked())
373         {
374           if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
375             aViewWindow->SetSelectionMode(EdgeSelection);
376         }
377       break;
378     }
379   case 1:
380     {
381       GroupArguments->setTitle(tr("EXTRUSION_2D"));
382       if (!CheckBoxMesh->isChecked())
383         {
384           if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
385             aViewWindow->SetSelectionMode(FaceSelection);
386         }
387       break;
388     }
389   }
390
391   myEditCurrentArgument = (QWidget*)LineEditElements;
392   LineEditElements->setFocus();
393
394   if (CheckBoxMesh->isChecked())
395     onSelectMesh(true);
396
397   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
398 }
399
400 //=================================================================================
401 // function : ClickOnApply()
402 // purpose  : Called when user presses <Apply> button
403 //=================================================================================
404 bool SMESHGUI_ExtrusionDlg::ClickOnApply()
405 {
406   if (mySMESHGUI->isActiveStudyLocked())
407     return false;
408
409   if (!isValid())
410     return false;
411
412   if (myNbOkElements) {
413     
414     gp_XYZ aNormale(SpinBox_Vx->GetValue(),
415                     SpinBox_Vy->GetValue(),
416                     SpinBox_Vz->GetValue());
417     
418     aNormale /= aNormale.Modulus();
419     
420     SMESH::DirStruct aVector;
421     aVector.PS.x = SpinBox_Dx->GetValue()*aNormale.X();
422     aVector.PS.y = SpinBox_Dy->GetValue()*aNormale.Y();
423     aVector.PS.z = SpinBox_Dz->GetValue()*aNormale.Z();
424
425     long aNbSteps = (long)SpinBox_NbSteps->value();
426
427     QStringList aParameters;
428     aParameters << SpinBox_Dx->text();
429     aParameters << SpinBox_Dy->text();
430     aParameters << SpinBox_Dz->text();
431     aParameters << SpinBox_NbSteps->text();
432
433     try {
434       SUIT_OverrideCursor aWaitCursor;
435       SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
436
437       if ( MakeGroupsCheck->isEnabled() && MakeGroupsCheck->isChecked() ) {
438         if( CheckBoxMesh->isChecked() ) {
439           if( GetConstructorId() == 0 )
440             SMESH::ListOfGroups_var groups = 
441               aMeshEditor->ExtrusionSweepObject1DMakeGroups(mySelectedObject, aVector, aNbSteps);
442           else
443             SMESH::ListOfGroups_var groups = 
444               aMeshEditor->ExtrusionSweepObject2DMakeGroups(mySelectedObject, aVector, aNbSteps);
445         }
446         else
447           SMESH::ListOfGroups_var groups = 
448             aMeshEditor->ExtrusionSweepMakeGroups(myElementsId.inout(), aVector, aNbSteps);
449       }
450       else {
451         if( CheckBoxMesh->isChecked() ) {
452           if( GetConstructorId() == 0 )
453             aMeshEditor->ExtrusionSweepObject1D(mySelectedObject, aVector, aNbSteps);
454           else
455             aMeshEditor->ExtrusionSweepObject2D(mySelectedObject, aVector, aNbSteps);
456         }
457         else
458           aMeshEditor->ExtrusionSweep(myElementsId.inout(), aVector, aNbSteps);
459       }
460
461       myMesh->SetParameters( SMESHGUI::JoinObjectParameters(aParameters) );
462
463     } catch (...) {
464     }
465
466     SMESH::UpdateView();
467     if ( MakeGroupsCheck->isEnabled() && MakeGroupsCheck->isChecked() )
468       mySMESHGUI->updateObjBrowser(true); // new groups may appear
469     Init(false);
470     ConstructorsClicked(GetConstructorId());
471     mySelectedObject = SMESH::SMESH_IDSource::_nil();
472     SelectionIntoArgument();
473   }
474   return true;
475 }
476
477 //=================================================================================
478 // function : ClickOnOk()
479 // purpose  : Called when user presses <OK> button
480 //=================================================================================
481 void SMESHGUI_ExtrusionDlg::ClickOnOk()
482 {
483   if (ClickOnApply())
484     ClickOnCancel();
485 }
486
487 //=================================================================================
488 // function : ClickOnCancel()
489 // purpose  : Called when dialog box is closed
490 //=================================================================================
491 void SMESHGUI_ExtrusionDlg::ClickOnCancel()
492 {
493   disconnect(mySelectionMgr, 0, this, 0);
494   mySelectionMgr->clearFilters();
495   //mySelectionMgr->clearSelected();
496   if (SMESH::GetCurrentVtkView()) {
497     SMESH::RemoveFilters(); // PAL6938 -- clean all mesh entity filters
498     SMESH::SetPointRepresentation(false);
499     SMESH::SetPickable();
500   }
501   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
502     aViewWindow->SetSelectionMode(ActorSelection);
503   mySMESHGUI->ResetState();
504   reject();
505 }
506
507 //=================================================================================
508 // function : ClickOnHelp()
509 // purpose  :
510 //=================================================================================
511 void SMESHGUI_ExtrusionDlg::ClickOnHelp()
512 {
513   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
514   if (app) 
515     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
516   else {
517     QString platform;
518 #ifdef WIN32
519     platform = "winapplication";
520 #else
521     platform = "application";
522 #endif
523     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
524                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
525                              arg(app->resourceMgr()->stringValue("ExternalBrowser", 
526                                                                  platform)).
527                              arg(myHelpFileName));
528   }
529 }
530
531 //=================================================================================
532 // function : onTextChange()
533 // purpose  :
534 //=================================================================================
535 void SMESHGUI_ExtrusionDlg::onTextChange (const QString& theNewText)
536 {
537   QLineEdit* send = (QLineEdit*)sender();
538
539   // return if busy
540   if (myBusy) return;
541
542   // set busy flag
543   myBusy = true;
544
545   if (send == LineEditElements)
546     myNbOkElements = 0;
547
548   // hilight entered elements/nodes
549
550   if (!myIO.IsNull()) {
551     QStringList aListId = theNewText.split(" ", QString::SkipEmptyParts);
552
553     if (send == LineEditElements)
554     {
555       SMDS_Mesh* aMesh = myActor ? myActor->GetObject()->GetMesh() : 0;
556       SMESH::ElementType SMESHType = GetConstructorId() ? SMESH::FACE : SMESH::EDGE;
557       SMDSAbs_ElementType SMDSType = GetConstructorId() ? SMDSAbs_Face: SMDSAbs_Edge;
558
559       myElementsId = new SMESH::long_array;
560       myElementsId->length( aListId.count() );
561       TColStd_MapOfInteger newIndices;
562       for (int i = 0; i < aListId.count(); i++) {
563         int id = aListId[ i ].toInt();
564         bool validId = false;
565         if ( id > 0 ) {
566           if ( aMesh ) {
567             const SMDS_MeshElement * e = aMesh->FindElement( id );
568             validId = ( e && e->GetType() == SMDSType );
569           } else {
570             validId = ( myMesh->GetElementType( id, true ) == SMESHType );
571           }
572         }
573         if ( validId && newIndices.Add( id ))
574           myElementsId[ newIndices.Extent()-1 ] = id;
575       }
576       myElementsId->length( myNbOkElements = newIndices.Extent() );
577       mySelector->AddOrRemoveIndex(myIO, newIndices, false);
578       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
579         aViewWindow->highlight( myIO, true, true );
580     }
581   }
582
583   CheckIsEnable();
584
585   myBusy = false;
586 }
587
588 //=================================================================================
589 // function : SelectionIntoArgument()
590 // purpose  : Called when selection as changed or other case
591 //=================================================================================
592 void SMESHGUI_ExtrusionDlg::SelectionIntoArgument()
593 {
594   if (myBusy) return;
595
596   // return if dialog box is inactive
597   if (!GroupButtons->isEnabled())
598     return;
599
600   // clear
601   myActor = 0;
602   myIO.Nullify();
603
604   QString aString = "";
605   // set busy flag
606   if(myEditCurrentArgument == (QWidget*)LineEditElements) {
607     myBusy = true;
608     LineEditElements->setText(aString);
609     myNbOkElements = 0;
610     myBusy = false;
611   }
612   // get selected mesh
613   SALOME_ListIO aList;
614   mySelectionMgr->selectedObjects(aList, SVTK_Viewer::Type());
615   int nbSel = SMESH::GetNameOfSelectedIObjects(mySelectionMgr, aString);
616   if (nbSel != 1)
617     return;
618
619   Handle(SALOME_InteractiveObject) IO = aList.First();
620   myMesh = SMESH::GetMeshByIO(IO);
621   if (myMesh->_is_nil())
622     return;
623   myIO = IO;
624   myActor = SMESH::FindActorByObject(myMesh);
625
626   if (myEditCurrentArgument == (QWidget*)LineEditElements) {    
627     int aNbElements = 0;
628
629     // MakeGroups is available if there are groups
630     if ( myMesh->NbGroups() == 0 ) {
631       MakeGroupsCheck->setChecked(false);
632       MakeGroupsCheck->setEnabled(false);
633     } else {
634       MakeGroupsCheck->setEnabled(true);
635     }
636
637     if (CheckBoxMesh->isChecked()) {
638       SMESH::GetNameOfSelectedIObjects(mySelectionMgr, aString);
639
640       if (!SMESH::IObjectToInterface<SMESH::SMESH_IDSource>(IO)->_is_nil())
641         mySelectedObject = SMESH::IObjectToInterface<SMESH::SMESH_IDSource>(IO);
642       else
643         return;
644     } else {
645       // get indices of selcted elements
646       TColStd_IndexedMapOfInteger aMapIndex;
647       mySelector->GetIndex(IO,aMapIndex);
648       aNbElements = aMapIndex.Extent();
649
650       if (aNbElements < 1)
651         return;
652
653       myElementsId = new SMESH::long_array;
654       myElementsId->length( aNbElements );
655       aString = "";
656       for ( int i = 0; i < aNbElements; ++i )
657         aString += QString(" %1").arg( myElementsId[ i ] = aMapIndex( i+1 ) );
658     }
659
660     myNbOkElements = true;
661
662     myBusy = true;
663     ((QLineEdit*)myEditCurrentArgument)->setText(aString);
664     myBusy = false;
665   }
666   else if(myEditCurrentArgument == (QWidget*)SpinBox_Vx){
667     TColStd_IndexedMapOfInteger aMapIndex;
668     mySelector->GetIndex(IO,aMapIndex);
669     int aNbElements = aMapIndex.Extent();
670     SMDS_Mesh* aMesh =  myActor ? myActor->GetObject()->GetMesh() : 0;
671
672     if(aNbElements != 1 || !aMesh)
673       return;
674     
675     const SMDS_MeshFace* face = dynamic_cast<const SMDS_MeshFace*>(aMesh->FindElement(aMapIndex(aNbElements)));
676
677     if (!face)
678       return;
679
680     gp_XYZ aNormale = SMESH::getNormale(face);
681     SpinBox_Vx->SetValue(aNormale.X());
682     SpinBox_Vy->SetValue(aNormale.Y());
683     SpinBox_Vz->SetValue(aNormale.Z());
684     
685   }
686   
687   // OK
688   CheckIsEnable();
689 }
690
691 //=================================================================================
692 // function : SetEditCurrentArgument()
693 // purpose  :
694 //=================================================================================
695 void SMESHGUI_ExtrusionDlg::SetEditCurrentArgument()
696 {
697   QPushButton* send = (QPushButton*)sender();
698
699   disconnect(mySelectionMgr, 0, this, 0);
700   mySelectionMgr->clearSelected();
701   mySelectionMgr->clearFilters();
702
703   if (send == SelectElementsButton) {
704     myEditCurrentArgument = (QWidget*)LineEditElements;
705     if (CheckBoxMesh->isChecked()) {
706       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
707         aViewWindow->SetSelectionMode(ActorSelection);
708       mySelectionMgr->installFilter(myMeshOrSubMeshOrGroupFilter);
709     } else {
710       int aConstructorId = GetConstructorId();
711       if (aConstructorId == 0)
712         {
713           if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
714             aViewWindow->SetSelectionMode(EdgeSelection);
715         }
716       else if (aConstructorId == 1)
717         {
718           if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
719             aViewWindow->SetSelectionMode(FaceSelection);
720         }
721     }
722   }
723   else if (send == SelectVectorButton){
724     myEditCurrentArgument = (QWidget*)SpinBox_Vx;
725     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
726       aViewWindow->SetSelectionMode(FaceSelection);
727   }
728   
729   myEditCurrentArgument->setFocus();
730   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
731   SelectionIntoArgument();
732 }
733
734 //=================================================================================
735 // function : DeactivateActiveDialog()
736 // purpose  : Deactivates this dialog
737 //=================================================================================
738 void SMESHGUI_ExtrusionDlg::DeactivateActiveDialog()
739 {
740   if (ConstructorsBox->isEnabled()) {
741     ConstructorsBox->setEnabled(false);
742     GroupArguments->setEnabled(false);
743     GroupButtons->setEnabled(false);
744     mySMESHGUI->ResetState();
745     mySMESHGUI->SetActiveDialogBox(0);
746   }
747 }
748
749 //=================================================================================
750 // function : ActivateThisDialog()
751 // purpose  : Activates this dialog
752 //=================================================================================
753 void SMESHGUI_ExtrusionDlg::ActivateThisDialog()
754 {
755   // Emit a signal to deactivate the active dialog
756   mySMESHGUI->EmitSignalDeactivateDialog();
757   ConstructorsBox->setEnabled(true);
758   GroupArguments->setEnabled(true);
759   GroupButtons->setEnabled(true);
760
761   mySMESHGUI->SetActiveDialogBox(this);
762
763   ConstructorsClicked(GetConstructorId());
764   SelectionIntoArgument();
765 }
766
767 //=================================================================================
768 // function : enterEvent()
769 // purpose  : Mouse enter event
770 //=================================================================================
771 void SMESHGUI_ExtrusionDlg::enterEvent (QEvent*)
772 {
773   if (!ConstructorsBox->isEnabled())
774     ActivateThisDialog();
775 }
776
777 //=================================================================================
778 // function : onSelectMesh()
779 // purpose  :
780 //=================================================================================
781 void SMESHGUI_ExtrusionDlg::onSelectMesh (bool toSelectMesh)
782 {
783   if (toSelectMesh)
784     TextLabelElements->setText(tr("SMESH_NAME"));
785   else
786     TextLabelElements->setText(tr("SMESH_ID_ELEMENTS"));
787
788   if (myEditCurrentArgument != LineEditElements) {
789     LineEditElements->clear();
790     return;
791   }
792
793   mySelectionMgr->clearFilters();
794
795   if (toSelectMesh) {
796     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
797       aViewWindow->SetSelectionMode(ActorSelection);
798     mySelectionMgr->installFilter(myMeshOrSubMeshOrGroupFilter);
799     LineEditElements->setReadOnly(true);
800     LineEditElements->setValidator(0);
801   } else {
802     int aConstructorId = GetConstructorId();
803     if (aConstructorId == 0)
804       {
805         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
806           aViewWindow->SetSelectionMode(EdgeSelection);
807       }
808     else if (aConstructorId == 0)
809       {
810         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
811           aViewWindow->SetSelectionMode(FaceSelection);
812       }
813
814     LineEditElements->setReadOnly(false);
815     LineEditElements->setValidator(myIdValidator);
816     onTextChange(LineEditElements->text());
817   }
818
819   SelectionIntoArgument();
820 }
821
822 //=================================================================================
823 // function : GetConstructorId()
824 // purpose  :
825 //=================================================================================
826 int SMESHGUI_ExtrusionDlg::GetConstructorId()
827 {
828   return GroupConstructors->checkedId();
829 }
830
831 //=================================================================================
832 // function : keyPressEvent()
833 // purpose  :
834 //=================================================================================
835 void SMESHGUI_ExtrusionDlg::keyPressEvent( QKeyEvent* e )
836 {
837   QDialog::keyPressEvent( e );
838   if ( e->isAccepted() )
839     return;
840
841   if ( e->key() == Qt::Key_F1 ) {
842     e->accept();
843     ClickOnHelp();
844   }
845 }
846
847 //=================================================================================
848 // function : setFilters()
849 // purpose  : SLOT. Called when "Filter" button pressed.
850 //=================================================================================
851 void SMESHGUI_ExtrusionDlg::setFilters()
852 {
853   if ( !myFilterDlg )
854   {
855     QList<int> types;  
856     types.append( SMESH::EDGE );
857     types.append( SMESH::FACE );
858     myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, types );
859   }
860   myFilterDlg->Init( GetConstructorId() ? SMESH::FACE : SMESH::EDGE );
861
862   myFilterDlg->SetSelection();
863   myFilterDlg->SetMesh( myMesh );
864   myFilterDlg->SetSourceWg( LineEditElements );
865
866   myFilterDlg->show();
867 }
868
869 //=================================================================================
870 // function : isValid
871 // purpose  :
872 //=================================================================================
873 bool SMESHGUI_ExtrusionDlg::isValid()
874 {
875   QString msg;
876   bool ok = true;
877   ok = SpinBox_Dx->isValid( msg, true ) && ok;
878   ok = SpinBox_Dy->isValid( msg, true ) && ok;
879   ok = SpinBox_Dz->isValid( msg, true ) && ok;
880   ok = SpinBox_NbSteps->isValid( msg, true ) && ok;
881
882   if( !ok ) {
883     QString str( tr( "SMESH_INCORRECT_INPUT" ) );
884     if ( !msg.isEmpty() )
885       str += "\n" + msg;
886     SUIT_MessageBox::critical( this, tr( "SMESH_ERROR" ), str );
887     return false;
888   }
889   return true;
890 }