Salome HOME
6bc626d3acd604e830daee4ee0f81e44fa766974
[modules/visu.git] / src / VISUGUI / VisuGUI_ClippingDlg.cxx
1 // Copyright (C) 2007-2011  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 #include "VisuGUI_ClippingDlg.h"
24
25 #include "VisuGUI.h"
26 #include "VisuGUI_Tools.h"
27 #include "VisuGUI_ViewTools.h"
28
29 #include "VISU_Prs3d_i.hh"
30 #include "VISU_Result_i.hh"
31 #include "VISU_ColoredPrs3dHolder_i.hh"
32
33 #include "VISU_PipeLine.hxx"
34 #include "VISU_PipeLineUtils.hxx"
35 #include "VISU_DataSetActor.h"
36
37 #include <SalomeApp_IntSpinBox.h>
38 #include <SalomeApp_DoubleSpinBox.h>
39
40 #include "LightApp_SelectionMgr.h"
41 #include "LightApp_Application.h"
42
43 #include "SVTK_ViewWindow.h"
44 #include <VTKViewer_Utilities.h>
45
46 #include "SUIT_Session.h"
47 #include "SUIT_Desktop.h"
48 #include "SUIT_MessageBox.h"
49 #include "SUIT_ResourceMgr.h"
50 #include "SUIT_OverrideCursor.h"
51
52 #include "SALOME_Actor.h"
53 #include "VISU_ViewManager_i.hh"
54
55 // QT Includes
56 #include <QLabel>
57 #include <QComboBox>
58 #include <QCheckBox>
59 #include <QLayout>
60 #include <QGroupBox>
61 #include <QButtonGroup>
62 #include <QValidator>
63 #include <QTabWidget>
64 #include <QRadioButton>
65 #include <QKeyEvent>
66 #include <QPushButton>
67 #include <QListWidget>
68 #include <QVBoxLayout>
69 #include <QHBoxLayout>
70 #include <QStackedWidget>
71
72 // VTK Includes
73 #include <vtkMath.h>
74 #include <vtkCamera.h>
75 #include <vtkRenderer.h>
76 #include <vtkDataSet.h>
77 #include <vtkDataSetMapper.h>
78 #include <vtkImplicitFunction.h>
79 #include <vtkPlaneSource.h>
80 #include <vtkPolyData.h>
81 #include <vtkUnstructuredGrid.h>
82 #include <vtkProperty.h>
83 #include <vtkImplicitFunctionCollection.h>
84
85 // OCCT Includes
86 #include <gp_Dir.hxx>
87
88 using namespace std;
89
90 namespace VISU {
91   float GetFloat (const QString& theValue, float theDefault)
92   {
93     if (theValue.isEmpty()) return theDefault;
94     SUIT_ResourceMgr* aResourceMgr = VISU::GetResourceMgr();
95     QString aValue = aResourceMgr->stringValue("VISU",theValue);
96     if (aValue.isEmpty()) return theDefault;
97     return aValue.toFloat();
98   }
99
100   void RenderViewWindow (SVTK_ViewWindow* vw)
101   {
102     if (vw) {
103       vw->getRenderer()->ResetCameraClippingRange();
104       vw->Repaint();
105     }
106   }
107 };
108
109 //=================================================================================
110 //class    : OrientedPlane
111 //purpose  :
112 //=================================================================================
113 OrientedPlane* OrientedPlane::New() 
114 {
115   return new OrientedPlane();
116 }
117
118 OrientedPlane* OrientedPlane::New (SVTK_ViewWindow* vw) 
119 {
120   return new OrientedPlane(vw);
121 }
122
123 void OrientedPlane::SetOrientation(VISU::Orientation theOrientation) 
124 {
125   myOrientation = theOrientation;
126 }
127
128 VISU::Orientation OrientedPlane::GetOrientation() 
129 {
130   return myOrientation;
131 }
132
133 void OrientedPlane::SetDistance(float theDistance) 
134 {
135   myDistance = theDistance;
136 }
137
138 float OrientedPlane::GetDistance() 
139 {
140   return myDistance;
141 }
142
143 void OrientedPlane::ShallowCopy(OrientedPlane* theOrientedPlane)
144 {
145   SetNormal(theOrientedPlane->GetNormal());
146   SetOrigin(theOrientedPlane->GetOrigin());
147   
148   myOrientation = theOrientedPlane->GetOrientation();
149   myDistance = theOrientedPlane->GetDistance();
150   
151   myAngle[0] = theOrientedPlane->myAngle[0];
152   myAngle[1] = theOrientedPlane->myAngle[1];
153   
154   myPlaneSource->SetNormal(theOrientedPlane->myPlaneSource->GetNormal());
155   myPlaneSource->SetOrigin(theOrientedPlane->myPlaneSource->GetOrigin());
156   myPlaneSource->SetPoint1(theOrientedPlane->myPlaneSource->GetPoint1());
157   myPlaneSource->SetPoint2(theOrientedPlane->myPlaneSource->GetPoint2());
158 }
159
160 OrientedPlane::OrientedPlane(SVTK_ViewWindow* vw):
161   myOrientation(VISU::XY),
162   myDistance(0.5),
163   myViewWindow(vw)
164 {
165   Init();
166   myViewWindow->AddActor(myActor, false, false); // don't adjust actors
167 }
168
169 OrientedPlane::OrientedPlane():
170   myOrientation(VISU::XY),
171   myDistance(0.5),
172   myViewWindow(NULL)
173 {
174   Init();
175 }
176
177 void OrientedPlane::Init()
178 {
179   myPlaneSource = vtkPlaneSource::New();
180   
181   myAngle[0] = myAngle[1] = 0.0;
182
183   // Create and display actor
184   myMapper = vtkDataSetMapper::New();
185   myMapper->SetInput(myPlaneSource->GetOutput());
186
187   myActor = SALOME_Actor::New();
188   myActor->VisibilityOff();
189   myActor->PickableOff();
190   myActor->SetInfinitive(true);
191   myActor->SetMapper(myMapper);
192   
193   vtkProperty* aProp = vtkProperty::New();
194   float anRGB[3];
195   
196   SUIT_ResourceMgr* aResourceMgr = VISU::GetResourceMgr();
197   
198   QColor aFillColor = aResourceMgr->colorValue("SMESH", "fill_color", QColor(0, 170, 255));
199   anRGB[0] = aFillColor.red()/255.;
200   anRGB[1] = aFillColor.green()/255.;
201   anRGB[2] = aFillColor.blue()/255.;
202   aProp->SetColor(anRGB[0],anRGB[1],anRGB[2]);
203   aProp->SetOpacity(0.75);
204   myActor->SetProperty(aProp);
205   aProp->Delete();
206   
207   vtkProperty* aBackProp = vtkProperty::New();
208   QColor aBackFaceColor = aResourceMgr->colorValue("SMESH", "backface_color", QColor(0, 0, 255));//@
209   anRGB[0] = aBackFaceColor.red()/255.;
210   anRGB[1] = aBackFaceColor.green()/255.;
211   anRGB[2] = aBackFaceColor.blue()/255.;
212   aBackProp->SetColor(anRGB[0],anRGB[1],anRGB[2]);
213   aBackProp->SetOpacity(0.75);
214   myActor->SetBackfaceProperty(aBackProp);
215   aBackProp->Delete();
216 }
217
218 OrientedPlane::~OrientedPlane()
219 {
220   if ( !myViewWindow.isNull() )
221     myViewWindow->RemoveActor(myActor);
222
223   myActor->Delete();
224
225   myMapper->RemoveAllInputs();
226   myMapper->Delete();
227   
228   // commented: porting to vtk 5.0
229   //myPlaneSource->UnRegisterAllOutputs();
230   myPlaneSource->Delete();
231 }
232
233 struct TSetVisiblity {
234   TSetVisiblity(int theIsVisible): myIsVisible(theIsVisible){}
235   void operator()(VISU::TVTKPlane& theOrientedPlane){
236     theOrientedPlane->myActor->SetVisibility(myIsVisible);
237   }
238   int myIsVisible;
239 };
240
241 //=================================================================================
242 // class    : VisuGUI_ClippingDlg()
243 // purpose  :
244 //
245 //=================================================================================
246 VisuGUI_ClippingDlg::VisuGUI_ClippingDlg (VisuGUI* theModule,
247                                           bool modal )
248   : QDialog(VISU::GetDesktop(theModule), Qt::WindowTitleHint | Qt::WindowSystemMenuHint ),
249     mySelectionMgr(VISU::GetSelectionMgr(theModule)),
250     myVisuGUI(theModule),
251     myPrs3d(0),
252     myIsSelectPlane(false),
253     myDSActor(0)
254 {
255   setWindowTitle(tr("TITLE"));
256   setSizeGripEnabled(TRUE);
257   setAttribute( Qt::WA_DeleteOnClose, true );
258
259   QVBoxLayout* VisuGUI_ClippingDlgLayout = new QVBoxLayout(this);
260   VisuGUI_ClippingDlgLayout->setSpacing(6);
261   VisuGUI_ClippingDlgLayout->setMargin(11);
262   
263   QStackedWidget* aStackWidget = new QStackedWidget(this);
264   VisuGUI_ClippingDlgLayout->addWidget(aStackWidget);
265   // Local planes
266   QWidget* aLocalPlanes = new QWidget(aStackWidget);
267   QVBoxLayout* aLocalLayout = new QVBoxLayout(aLocalPlanes);
268   aStackWidget->addWidget(aLocalPlanes);
269
270   // Controls for selecting, creating, deleting planes
271   QGroupBox* GroupPlanes = new QGroupBox (tr("GRP_PLANES"),  aLocalPlanes);
272   QGridLayout* GroupPlanesLayout = new QGridLayout (GroupPlanes);
273   GroupPlanesLayout->setAlignment(Qt::AlignTop);
274   GroupPlanesLayout->setSpacing(6);
275   GroupPlanesLayout->setMargin(11);
276   aLocalLayout->addWidget(GroupPlanes);
277
278   ComboBoxPlanes = new QComboBox (GroupPlanes);
279   GroupPlanesLayout->addWidget(ComboBoxPlanes, 0, 0);
280
281   QSpacerItem* spacerGP = new QSpacerItem (20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
282   GroupPlanesLayout->addItem(spacerGP, 0, 1);
283
284   buttonNew = new QPushButton (GroupPlanes );
285   buttonNew->setText(tr("BUT_NEW"));
286   GroupPlanesLayout->addWidget(buttonNew, 0, 2);
287
288   buttonDelete = new QPushButton(GroupPlanes);
289   buttonDelete->setText(tr("BUT_DELETE"));
290   GroupPlanesLayout->addWidget(buttonDelete, 0, 3);
291
292   // Controls for defining plane parameters
293
294   // Tab pane
295   QGroupBox* GroupParameters = new QGroupBox(tr("GRP_PARAMETERS"), aLocalPlanes);
296   QGridLayout* GroupParametersLayout = new QGridLayout (GroupParameters);
297   GroupParametersLayout->setAlignment(Qt::AlignTop);
298   GroupParametersLayout->setSpacing(6);
299   GroupParametersLayout->setMargin(11);
300   aLocalLayout->addWidget(GroupParameters);
301
302   TabPane = new QTabWidget (GroupParameters);
303   TabPane->addTab(createParamsTab()   , tr("TAB_NON_STRUCTURED"));
304   TabPane->addTab(createIJKParamsTab(), tr("TAB_IJK_STRUCTURED"));
305   GroupParametersLayout->addWidget(TabPane, 0, 0);
306
307
308
309
310
311   // "Show preview" and "Auto Apply" check boxes
312   QHBoxLayout* aCheckBoxLayout = new QHBoxLayout(this);
313   VisuGUI_ClippingDlgLayout->addLayout(aCheckBoxLayout);
314
315   PreviewCheckBox = new QCheckBox (tr("SHOW_PREVIEW_CHK"), this);
316   PreviewCheckBox->setChecked(true);
317   aCheckBoxLayout->addWidget(PreviewCheckBox);
318   aCheckBoxLayout->addStretch();
319
320   AutoApplyCheckBox = new QCheckBox (tr("AUTO_APPLY_CHK"), this);
321   AutoApplyCheckBox->setChecked(false);
322   aCheckBoxLayout->addWidget(AutoApplyCheckBox);
323
324   // Controls for "Ok", "Apply" and "Close" button
325   QGroupBox* GroupButtons = new QGroupBox (this);
326   VisuGUI_ClippingDlgLayout->addWidget(GroupButtons);
327   QSizePolicy aSizePolicy(QSizePolicy::Expanding,
328                           QSizePolicy::Fixed );
329   aSizePolicy.setHeightForWidth( GroupButtons->sizePolicy().hasHeightForWidth() );
330   aSizePolicy.setHorizontalStretch( 0 );
331   aSizePolicy.setVerticalStretch( 0 );
332   GroupButtons->setSizePolicy( aSizePolicy );
333   GroupButtons->setGeometry(QRect(10, 10, 281, 48));
334   QGridLayout* GroupButtonsLayout = new QGridLayout (GroupButtons);
335   GroupButtons->setLayout(GroupButtonsLayout);
336   GroupButtonsLayout->setAlignment(Qt::AlignTop);
337   GroupButtonsLayout->setSpacing(6);
338   GroupButtonsLayout->setMargin(11);
339   buttonHelp = new QPushButton (GroupButtons);
340   buttonHelp->setText(tr("BUT_HELP"));
341   buttonHelp->setAutoDefault(TRUE);
342   GroupButtonsLayout->addWidget(buttonHelp, 0, 4);
343   buttonCancel = new QPushButton (GroupButtons);
344   buttonCancel->setText(tr("BUT_CLOSE"));
345   buttonCancel->setAutoDefault(TRUE);
346   GroupButtonsLayout->addWidget(buttonCancel, 0, 3);
347   buttonApply = new QPushButton (GroupButtons);
348   buttonApply->setText(tr("BUT_APPLY"));
349   buttonApply->setAutoDefault(TRUE);
350   GroupButtonsLayout->addWidget(buttonApply, 0, 1);
351   QSpacerItem* spacer_9 = new QSpacerItem (20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
352   GroupButtonsLayout->addItem(spacer_9, 0, 2);
353   buttonOk = new QPushButton (GroupButtons);
354   buttonOk->setText(tr("BUT_OK"));
355   buttonOk->setAutoDefault(TRUE);
356   buttonOk->setDefault(TRUE);
357   GroupButtonsLayout->addWidget(buttonOk, 0, 0);
358
359   // Initial state
360   VISU::initSpinBox( SpinBoxDistance, 0., 1., .01, "length_precision" );
361   VISU::initSpinBox( SpinBoxRot1, -180., 180., 1., "angle_precision" );
362   VISU::initSpinBox( SpinBoxRot2, -180., 180., 1., "angle_precision" );  
363
364   ComboBoxOrientation->addItem(tr("PARALLEL_XOY_COMBO_ITEM"));
365   ComboBoxOrientation->addItem(tr("PARALLEL_YOZ_COMBO_ITEM"));
366   ComboBoxOrientation->addItem(tr("PARALLEL_ZOX_COMBO_ITEM"));
367
368   SpinBoxDistance->setValue(0.5);
369
370   onSelectionChanged();
371
372   // signals and slots connections :
373   connect(ComboBoxPlanes         , SIGNAL(activated(int))           , this, SLOT(onSelectPlane(int)));
374   connect(buttonNew              , SIGNAL(clicked())                , this, SLOT(ClickOnNew()));
375   connect(buttonDelete           , SIGNAL(clicked())                , this, SLOT(ClickOnDelete()));
376   connect(ComboBoxOrientation    , SIGNAL(activated(int))           , this, SLOT(onSelectOrientation(int)));
377   connect(SpinBoxDistance        , SIGNAL(valueChanged(double))     , this, SLOT(SetCurrentPlaneParam()));
378   connect(SpinBoxRot1            , SIGNAL(valueChanged(double))     , this, SLOT(SetCurrentPlaneParam()));
379   connect(SpinBoxRot2            , SIGNAL(valueChanged(double))     , this, SLOT(SetCurrentPlaneParam()));
380   connect(ButtonGroupIJKAxis     , SIGNAL(buttonClicked(int))             , this, SLOT(onIJKAxisChanged(int)));
381   connect(SpinBoxIJKIndex        , SIGNAL(valueChanged(int))        , this, SLOT(SetCurrentPlaneIJKParam()));
382   connect(CheckBoxIJKPlaneReverse, SIGNAL(toggled(bool))            , this, SLOT(SetCurrentPlaneIJKParam()));
383   connect(TabPane                , SIGNAL(currentChanged (QWidget*)), this, SLOT(onTabChanged(QWidget*)));
384
385   connect(PreviewCheckBox  , SIGNAL(toggled(bool)), this, SLOT(OnPreviewToggle(bool)));
386   connect(AutoApplyCheckBox, SIGNAL(toggled(bool)), this, SLOT(ClickOnApply()));
387
388   connect(buttonOk    , SIGNAL(clicked()), this, SLOT(ClickOnOk()));
389   connect(buttonApply , SIGNAL(clicked()), this, SLOT(ClickOnApply()));
390   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
391   connect(buttonHelp  , SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
392
393   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(onSelectionChanged()));
394
395   this->show();
396 }
397
398 //=================================================================================
399 // function : ~VisuGUI_ClippingDlg()
400 // purpose  :
401 //=================================================================================
402 VisuGUI_ClippingDlg::~VisuGUI_ClippingDlg()
403 {
404   // no need to delete child widgets, Qt does it all for us
405   SetPrs3d(NULL);
406   std::for_each(myPlanes.begin(),myPlanes.end(),TSetVisiblity(false));
407   VISU::RenderViewWindow(VISU::GetActiveViewWindow<SVTK_ViewWindow>(myVisuGUI));
408 }
409
410 //=================================================================================
411 // function : createParamsTab
412 // purpose  :
413 //=================================================================================
414 QWidget* VisuGUI_ClippingDlg::createParamsTab()
415 {
416   QFrame* GroupParameters = new QFrame(this);
417   QGridLayout* GroupParametersLayout = new QGridLayout(GroupParameters);
418   GroupParametersLayout->setAlignment(Qt::AlignTop);
419   GroupParametersLayout->setSpacing(6);
420   GroupParametersLayout->setMargin(11);
421
422   TextLabelOrientation = new QLabel(GroupParameters);
423   TextLabelOrientation->setText(tr("LBL_ORIENTATION"));
424   GroupParametersLayout->addWidget(TextLabelOrientation, 0, 0);
425
426   ComboBoxOrientation = new QComboBox(GroupParameters);
427   GroupParametersLayout->addWidget(ComboBoxOrientation, 0, 1);
428
429   TextLabelDistance = new QLabel(GroupParameters);
430   TextLabelDistance->setText(tr("LBL_DISTANCE"));
431   GroupParametersLayout->addWidget(TextLabelDistance, 1, 0);
432
433   SpinBoxDistance = new SalomeApp_DoubleSpinBox(GroupParameters);
434   GroupParametersLayout->addWidget(SpinBoxDistance, 1, 1);
435
436   TextLabelRot1 = new QLabel(GroupParameters);
437   TextLabelRot1->setText(tr("LBL_ROTATION_YZ"));
438   GroupParametersLayout->addWidget(TextLabelRot1, 2, 0);
439
440   SpinBoxRot1 = new SalomeApp_DoubleSpinBox(GroupParameters);
441   GroupParametersLayout->addWidget(SpinBoxRot1, 2, 1);
442
443   TextLabelRot2 = new QLabel(GroupParameters);
444   TextLabelRot2->setText(tr("LBL_ROTATION_XZ"));
445   GroupParametersLayout->addWidget(TextLabelRot2, 3, 0);
446
447   SpinBoxRot2 = new SalomeApp_DoubleSpinBox(GroupParameters);
448   GroupParametersLayout->addWidget(SpinBoxRot2, 3, 1);
449
450   return GroupParameters;
451 }
452
453 //=================================================================================
454 // function : createIJKParamsTab
455 // purpose  :
456 //=================================================================================
457 QWidget* VisuGUI_ClippingDlg::createIJKParamsTab()
458 {
459   // tab layout
460   WidgetIJKTab = new QFrame(this);
461   QGridLayout* IJKParametersLayout = new QGridLayout(WidgetIJKTab);
462   IJKParametersLayout->setAlignment(Qt::AlignTop);
463   IJKParametersLayout->setSpacing(6);
464   IJKParametersLayout->setMargin(11);
465
466   // Axis group
467   ButtonGroupIJKAxis = new QButtonGroup ( WidgetIJKTab);
468   //QGroupBox* aGBGroupBoxIJKAxis= new QGroupBox(tr("GRP_IJK_AXIS"), WidgetIJKTab );
469   GroupBoxIJKAxis= new QGroupBox(tr("GRP_IJK_AXIS"), WidgetIJKTab );
470   QHBoxLayout* aHBLay = new QHBoxLayout( GroupBoxIJKAxis  );
471   ButtonGroupIJKAxis->addButton( new QRadioButton (tr("I_RADIO_BTN"), GroupBoxIJKAxis), 0 );  // 0
472   ButtonGroupIJKAxis->addButton( new QRadioButton (tr("J_RADIO_BTN"), GroupBoxIJKAxis), 1 );  // 1
473   ButtonGroupIJKAxis->addButton( new QRadioButton (tr("K_RADIO_BTN"), GroupBoxIJKAxis), 2 );  // 2
474   ButtonGroupIJKAxis->button(0)->setChecked(true);
475   aHBLay->addWidget( ButtonGroupIJKAxis->button(0) );
476   aHBLay->addWidget( ButtonGroupIJKAxis->button(1) );
477   aHBLay->addWidget( ButtonGroupIJKAxis->button(2) );  
478
479   // Index
480   TextLabelIJKIndex = new QLabel(WidgetIJKTab);
481   TextLabelIJKIndex->setText(tr("LBL_IJK_INDEX"));
482   SpinBoxIJKIndex = new SalomeApp_IntSpinBox(WidgetIJKTab);
483   SpinBoxIJKIndex->setAcceptNames( false );
484
485   // Orientation
486   CheckBoxIJKPlaneReverse = new QCheckBox (tr("REVERSE_NORMAL_CHK"), WidgetIJKTab);
487   CheckBoxIJKPlaneReverse->setChecked(false);
488
489   IJKParametersLayout->addWidget(GroupBoxIJKAxis, 0, 0, 1, 2);
490   IJKParametersLayout->addWidget(TextLabelIJKIndex,          1, 0);
491   IJKParametersLayout->addWidget(SpinBoxIJKIndex,            1, 1);
492   IJKParametersLayout->addWidget(CheckBoxIJKPlaneReverse, 2, 0);
493
494   return WidgetIJKTab;
495 }
496
497 //=================================================================================
498 // function : ClickOnApply()
499 // purpose  :
500 //=================================================================================
501 void VisuGUI_ClippingDlg::ClickOnApply()
502 {
503   applyLocalPlanes();
504 }
505
506
507 //=================================================================================
508 // function : applyLocalPlanes()
509 // purpose  :
510 //=================================================================================
511 void VisuGUI_ClippingDlg::applyLocalPlanes()
512 {
513   if (!myPrs3d)
514     return;
515
516   if (SVTK_ViewWindow* aViewWindow = VISU::GetActiveViewWindow<SVTK_ViewWindow>(myVisuGUI)) {
517     SUIT_OverrideCursor wc;
518
519     QWidget *aCurrWid = this->focusWidget();
520     aCurrWid->clearFocus();
521     aCurrWid->setFocus();
522
523     // Save clipping planes, currently applied to the presentation
524     // to enable restoring this state in case of failure.
525     // Refer to bugs IPAL8849, IPAL8850 for more information.
526     typedef vtkSmartPointer<vtkPlane> TPln;
527     typedef std::vector<TPln> TPlns;
528     bool isFailed = false;
529     TPlns anOldPlanes;
530     int iopl = 0, nbOldPlanes = myPrs3d->GetNumberOfClippingPlanes();
531     for (; iopl < nbOldPlanes; iopl++) {
532       anOldPlanes.push_back(myPrs3d->GetClippingPlane(iopl));
533     }
534
535     // Try to apply new clipping
536     //myPrs3d->RemoveAllClippingPlanes();
537     removeAllClippingPlanes(myPrs3d);
538
539     VISU::TPlanes::iterator anIter = myPlanes.begin();
540     for (; anIter != myPlanes.end(); anIter++) {
541       OrientedPlane* anOrientedPlane = OrientedPlane::New(aViewWindow);
542       anOrientedPlane->ShallowCopy(anIter->GetPointer());
543       if (!myPrs3d->AddClippingPlane(anOrientedPlane)) {
544         isFailed = true;
545       }
546       anOrientedPlane->Delete();
547     }
548
549     // Check contents of the resulting (clipped) presentation data
550     if (!isFailed) {
551       VISU_PipeLine* aPL = myPrs3d->GetPipeLine();
552       vtkMapper* aMapper = aPL->GetMapper();
553       vtkDataSet* aPrsData = aMapper->GetInput();
554       aPrsData->Update();
555       if (aPrsData->GetNumberOfCells() < 1) {
556         isFailed = true;
557       }
558     }
559
560     if (isFailed) {
561       // Restore previous clipping state because of failure.
562       //myPrs3d->RemoveAllClippingPlanes();
563       removeAllClippingPlanes(myPrs3d);
564
565       TPlns::iterator anOldIter = anOldPlanes.begin();
566       for (; anOldIter != anOldPlanes.end(); anOldIter++) {
567         myPrs3d->AddClippingPlane(anOldIter->GetPointer());
568       }
569
570       SUIT_MessageBox::warning(VISU::GetDesktop(myVisuGUI),
571                                tr("WRN_VISU"),
572                                tr("WRN_EMPTY_RESULTING_PRS"),
573                                tr("BUT_OK") );
574     }
575
576     //VISU::RenderViewWindow(aViewWindow);
577     VISU::RepaintViewWindows(myVisuGUI, myIO);
578   }
579 }
580
581
582
583
584 //=================================================================================
585 // function : ClickOnOk()
586 // purpose  :
587 //=================================================================================
588 void VisuGUI_ClippingDlg::ClickOnOk()
589 {
590   ClickOnApply();
591   ClickOnCancel();
592 }
593
594 //=================================================================================
595 // function : ClickOnCancel()
596 // purpose  :
597 //=================================================================================
598 void VisuGUI_ClippingDlg::ClickOnCancel()
599 {
600   close();
601 }
602
603 //=================================================================================
604 // function : ClickOnHelp()
605 // purpose  :
606 //=================================================================================
607 void VisuGUI_ClippingDlg::ClickOnHelp()
608 {
609   QString aHelpFileName = "clipping_page.html";
610   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
611   if (app)
612     app->onHelpContextModule(myVisuGUI ? app->moduleName(myVisuGUI->moduleName()) : QString(""), aHelpFileName);
613   else {
614     QString platform;
615 #ifdef WIN32
616     platform = "winapplication";
617 #else
618     platform = "application";
619 #endif
620     SUIT_MessageBox::warning(0, QObject::tr("WRN_WARNING"),
621                              QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
622                              arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(aHelpFileName),
623                              QObject::tr("BUT_OK"));
624   }
625 }
626
627 //=================================================================================
628 // function : onSelectionChanged()
629 // purpose  : Called when selection is changed
630 //=================================================================================
631 void VisuGUI_ClippingDlg::onSelectionChanged()
632 {
633   if(SVTK_ViewWindow* aViewWindow = VISU::GetActiveViewWindow<SVTK_ViewWindow>(myVisuGUI)){
634
635     VISU::TSelectionInfo aSelectionInfo = VISU::GetSelectedObjects(myVisuGUI);
636     if(aSelectionInfo.empty())
637       return;
638
639     VISU::TSelectionItem aSelectionItem = aSelectionInfo.front();
640     VISU::Base_i* aBase = aSelectionItem.myObjectInfo.myBase;
641     if(!aBase) 
642       return;
643   
644     Handle(SALOME_InteractiveObject) anIO = aSelectionItem.myIO;
645     if (!anIO.IsNull()) 
646       myIO = anIO;
647
648     //----
649     // rnv: fix for issue 0020114 (EDF VISU 918 : Impossible to 
650     // create a new clipping plane on field presentation)
651     // set last visited presentation from holder as myPrs3d
652     VISU::ColoredPrs3dHolder_i* aHolder = dynamic_cast<VISU::ColoredPrs3dHolder_i*>(aBase);
653     VISU::Prs3d_i* aPrs3d = NULL;
654     if(aHolder) 
655       aPrs3d = aHolder->GetPrs3dDevice();
656     else
657       aPrs3d = dynamic_cast<VISU::Prs3d_i*>(aBase);
658     //----
659     
660     SetPrs3d(aPrs3d);
661     if (myPrs3d) {
662       std::for_each(myPlanes.begin(),myPlanes.end(),TSetVisiblity(false));
663       myPlanes.clear();
664
665       CORBA::Float anOffset[3];
666       myPrs3d->GetOffset(anOffset[0],anOffset[1],anOffset[2]);
667
668       vtkIdType anId = 0, anEnd = myPrs3d->GetNumberOfClippingPlanes();
669       for (; anId < anEnd; anId++) {
670         if (vtkImplicitFunction* aFunction = myPrs3d->GetClippingPlane(anId)) {
671           if (OrientedPlane* aPlane = OrientedPlane::SafeDownCast(aFunction)) {
672             OrientedPlane* anOrientedPlane = OrientedPlane::New(aViewWindow);
673             VISU::TVTKPlane aTVTKPlane(anOrientedPlane);
674             anOrientedPlane->Delete();
675             aTVTKPlane->ShallowCopy(aPlane);
676             aTVTKPlane->myActor->SetPosition(anOffset[0],anOffset[1],anOffset[2]);
677             myPlanes.push_back(aTVTKPlane);
678           }
679         }
680       }
681
682       std::for_each(myPlanes.begin(),myPlanes.end(),
683                     TSetVisiblity(PreviewCheckBox->isChecked()));
684     }
685
686     // enable/disable IJK tab
687     TabPane->setTabEnabled(TabPane->indexOf(WidgetIJKTab), isStructured());
688     Sinchronize();
689     VISU::RenderViewWindow(aViewWindow);
690   }
691 }
692
693 //=================================================================================
694 // function : onSelectPlane()
695 // purpose  :
696 //=================================================================================
697 void VisuGUI_ClippingDlg::onSelectPlane(int theIndex)
698 {
699   if (!myPrs3d || myPlanes.empty())
700     return;
701
702   OrientedPlane* aPlane = myPlanes[theIndex].GetPointer();
703
704   // Orientation
705   VISU::Orientation anOrientation = aPlane->GetOrientation();
706
707   // Rotations
708   double aRot[2] = {aPlane->myAngle[0], aPlane->myAngle[1]};
709
710   // Set plane parameters in the dialog
711   myIsSelectPlane = true;
712   setDistance(aPlane->GetDistance());
713   setRotation(aRot[0], aRot[1]);
714   int item = 0;
715   switch (anOrientation) {
716   case VISU::XY: item = 0; break;
717   case VISU::YZ: item = 1; break;
718   case VISU::ZX: item = 2; break;
719   }
720   ComboBoxOrientation->setCurrentIndex(item);
721
722   bool isIJK = (TabPane->currentWidget() == WidgetIJKTab);
723   if (isIJK)
724     setIJKByNonStructured();
725   else
726     onSelectOrientation(item);
727
728   myIsSelectPlane = false;
729 }
730
731 //=================================================================================
732 // function : ClickOnNew()
733 // purpose  :
734 //=================================================================================
735 void VisuGUI_ClippingDlg::ClickOnNew()
736 {
737   VISU::TSelectionInfo aSelectionInfo = VISU::GetSelectedObjects(myVisuGUI);
738   if(aSelectionInfo.empty())
739     return;
740
741   const VISU::TSelectionItem& aSelectionItem = aSelectionInfo[0];
742   if(!aSelectionItem.myObjectInfo.myBase) 
743     return;
744
745   SetCurrentPlaneParam();
746   
747   if (!myPrs3d)
748     return;
749
750   if (SVTK_ViewWindow* aViewWindow = VISU::GetActiveViewWindow<SVTK_ViewWindow>(myVisuGUI)) {
751     OrientedPlane* aPlane = OrientedPlane::New(aViewWindow);
752     VISU::TVTKPlane aTVTKPlane(aPlane);
753     myPlanes.push_back(aTVTKPlane);
754
755     CORBA::Float anOffset[3];
756     myPrs3d->GetOffset(anOffset[0],anOffset[1],anOffset[2]);
757     aTVTKPlane->myActor->SetPosition(anOffset[0],anOffset[1],anOffset[2]);
758
759     if (PreviewCheckBox->isChecked())
760       aTVTKPlane->myActor->VisibilityOn();
761
762     Sinchronize();
763     SetCurrentPlaneParam();
764   }
765 }
766
767 //=================================================================================
768 // function : ClickOnDelete()
769 // purpose  :
770 //=================================================================================
771 void VisuGUI_ClippingDlg::ClickOnDelete()
772 {
773   if (!myPrs3d || myPlanes.empty())
774     return;
775
776   int aPlaneIndex = ComboBoxPlanes->currentIndex();
777
778   VISU::TPlanes::iterator anIter = myPlanes.begin() + aPlaneIndex;
779   anIter->GetPointer()->myActor->SetVisibility(false);
780   myPlanes.erase(anIter);
781
782   if(AutoApplyCheckBox->isChecked())
783     ClickOnApply();
784
785   Sinchronize();
786   if (SVTK_ViewWindow* aViewWindow = VISU::GetActiveViewWindow<SVTK_ViewWindow>(myVisuGUI))
787     VISU::RenderViewWindow(aViewWindow);
788 }
789
790 //=================================================================================
791 // function : onSelectOrientation()
792 // purpose  :
793 //=================================================================================
794 void VisuGUI_ClippingDlg::onSelectOrientation(int theItem)
795 {
796   if (myPlanes.empty())
797     return;
798
799   if      (theItem == 0) {
800     TextLabelRot1->setText(tr("LBL_ROTATION_YZ"));
801     TextLabelRot2->setText(tr("LBL_ROTATION_XZ"));
802   }
803   else if (theItem == 1) {
804     TextLabelRot1->setText(tr("LBL_ROTATION_ZX"));
805     TextLabelRot2->setText(tr("LBL_ROTATION_YX"));
806   }
807   else if (theItem == 2) {
808     TextLabelRot1->setText(tr("LBL_ROTATION_XY"));
809     TextLabelRot2->setText(tr("LBL_ROTATION_ZY"));
810   }
811
812   if((QComboBox*)sender() == ComboBoxOrientation)
813     SetCurrentPlaneParam();
814 }
815
816 //=================================================================================
817 // function : Sinchronize()
818 // purpose  : update control values according to plane selection
819 //=================================================================================
820 void VisuGUI_ClippingDlg::Sinchronize()
821 {
822   int aNbPlanes = myPlanes.size();
823   ComboBoxPlanes->clear();
824
825   QString aName;
826   for (int i = 1; i<=aNbPlanes; i++) {
827     aName = QString(tr("PLANES_COMBO_ITEM_i")).arg(i);
828     ComboBoxPlanes->addItem(aName);
829   }
830
831   int aPos = ComboBoxPlanes->count() - 1;
832   ComboBoxPlanes->setCurrentIndex(aPos);
833
834   bool anIsControlsEnable = (aPos >= 0);
835   if (anIsControlsEnable) {
836     onSelectPlane(aPos);
837   } else {
838     ComboBoxPlanes->addItem(tr("PLANES_COMBO_ITEM_no"));
839     SpinBoxRot1->setValue(0.0);
840     SpinBoxRot2->setValue(0.0);
841     SpinBoxDistance->setValue(0.5);
842   }
843
844   buttonDelete           ->setEnabled(anIsControlsEnable);
845   //buttonApply            ->setEnabled(anIsControlsEnable);
846   //  PreviewCheckBox        ->setEnabled(anIsControlsEnable);
847   //  AutoApplyCheckBox      ->setEnabled(anIsControlsEnable);
848
849   ComboBoxOrientation    ->setEnabled(anIsControlsEnable);
850   SpinBoxDistance        ->setEnabled(anIsControlsEnable);
851   SpinBoxRot1            ->setEnabled(anIsControlsEnable);
852   SpinBoxRot2            ->setEnabled(anIsControlsEnable);
853
854   GroupBoxIJKAxis        ->setEnabled(anIsControlsEnable);
855   SpinBoxIJKIndex        ->setEnabled(anIsControlsEnable);
856   CheckBoxIJKPlaneReverse->setEnabled(anIsControlsEnable);
857   //ENK: 23.11.2006 - PAL13176 - EDF228 VISU : Enhancement of structured datas processing
858   if ( myPrs3d ) {
859     VISU_PipeLine* aPipeLine = myPrs3d->GetPipeLine();
860     VISU::PIDMapper anIDMapper = aPipeLine->GetIDMapper();
861     if ( anIDMapper->IsStructured() ) {
862       VISU::TStructuredId aStructuredId = anIDMapper->GetStructure();
863       ButtonGroupIJKAxis->button(0)->setEnabled( aStructuredId[0] >= 0 );
864       ButtonGroupIJKAxis->button(1)->setEnabled( aStructuredId[1] >= 0 );
865       ButtonGroupIJKAxis->button(2)->setEnabled( aStructuredId[2] >= 0 );
866     }
867   }
868   //ENK: 23.11.2006
869 }
870
871 //=================================================================================
872 // function : setRotation()
873 // purpose  :
874 //=================================================================================
875 void VisuGUI_ClippingDlg::setRotation(const double theRot1, const double theRot2)
876 {
877   SpinBoxRot1->setValue(theRot1);
878   SpinBoxRot2->setValue(theRot2);
879 }
880
881 //=================================================================================
882 // function : SetCurrentPlaneParam()
883 // purpose  :
884 //=================================================================================
885 void VisuGUI_ClippingDlg::SetCurrentPlaneParam()
886 {
887   if (myPlanes.empty() || myIsSelectPlane)
888     return;
889
890   int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
891
892   OrientedPlane* aPlane = myPlanes[aCurPlaneIndex].GetPointer();
893
894   vtkFloatingPointType aNormal[3];
895   VISU::Orientation anOrientation;
896   vtkFloatingPointType aDir[3][3] = {{0, 0, 0}, {0, 0, 0}};
897   {
898     static double aCoeff = vtkMath::Pi()/180.0;
899
900     vtkFloatingPointType aRot[2] = {getRotation1(), getRotation2()};
901     aPlane->myAngle[0] = aRot[0];
902     aPlane->myAngle[1] = aRot[1];
903
904     vtkFloatingPointType anU[2] = {cos(aCoeff*aRot[0]), cos(aCoeff*aRot[1])};
905     vtkFloatingPointType aV[2] = {sqrt(1.0-anU[0]*anU[0]), sqrt(1.0-anU[1]*anU[1])};
906     aV[0] = aRot[0] > 0? aV[0]: -aV[0];
907     aV[1] = aRot[1] > 0? aV[1]: -aV[1];
908
909     switch (ComboBoxOrientation->currentIndex()) {
910     case 0:
911       anOrientation = VISU::XY;
912
913       aDir[0][1] = anU[0];
914       aDir[0][2] = aV[0];
915
916       aDir[1][0] = anU[1];
917       aDir[1][2] = aV[1];
918
919       break;
920     case 1:
921       anOrientation = VISU::YZ;
922
923       aDir[0][2] = anU[0];
924       aDir[0][0] = aV[0];
925
926       aDir[1][1] = anU[1];
927       aDir[1][0] = aV[1];
928
929       break;
930     case 2:
931       anOrientation = VISU::ZX;
932
933       aDir[0][0] = anU[0];
934       aDir[0][1] = aV[0];
935
936       aDir[1][2] = anU[1];
937       aDir[1][1] = aV[1];
938
939       break;
940     }
941
942     vtkMath::Cross(aDir[1],aDir[0],aNormal);
943     vtkMath::Normalize(aNormal);
944     vtkMath::Cross(aNormal,aDir[1],aDir[0]);
945   }
946
947   aPlane->SetOrientation(anOrientation);
948   aPlane->SetDistance(getDistance());
949
950   myPrs3d->SetPlaneParam(aNormal, 1. - getDistance(), aPlane);
951
952   //Get bounds of the visible part of the dataset
953   vtkFloatingPointType aBounds[6];
954   myPrs3d->GetPipeLine()->GetVisibleBounds(aBounds);
955
956   //Get center 
957   vtkFloatingPointType aPnt[3];
958   VISU::ComputeBoxCenter(aBounds,aPnt);
959
960   vtkFloatingPointType* anOrigin = aPlane->GetOrigin();
961   
962   //Get Length of the diagonal
963   vtkFloatingPointType aDel = VISU::ComputeBoxDiagonal(aBounds)/2.0;
964   
965   vtkFloatingPointType aDelta[2][3] = {{aDir[0][0]*aDel, aDir[0][1]*aDel, aDir[0][2]*aDel},
966                                        {aDir[1][0]*aDel, aDir[1][1]*aDel, aDir[1][2]*aDel}};
967   vtkFloatingPointType aParam, aPnt0[3], aPnt1[3], aPnt2[3];
968   
969   vtkFloatingPointType aPnt01[3] = {aPnt[0] - aDelta[0][0] - aDelta[1][0],
970                                     aPnt[1] - aDelta[0][1] - aDelta[1][1],
971                                     aPnt[2] - aDelta[0][2] - aDelta[1][2]};
972   vtkFloatingPointType aPnt02[3] = {aPnt01[0] + aNormal[0],
973                                     aPnt01[1] + aNormal[1],
974                                     aPnt01[2] + aNormal[2]};
975   vtkPlane::IntersectWithLine(aPnt01,aPnt02,aNormal,anOrigin,aParam,aPnt0);
976
977   vtkFloatingPointType aPnt11[3] = {aPnt[0] - aDelta[0][0] + aDelta[1][0],
978                                     aPnt[1] - aDelta[0][1] + aDelta[1][1],
979                                     aPnt[2] - aDelta[0][2] + aDelta[1][2]};
980   vtkFloatingPointType aPnt12[3] = {aPnt11[0] + aNormal[0],
981                                     aPnt11[1] + aNormal[1],
982                                     aPnt11[2] + aNormal[2]};
983   vtkPlane::IntersectWithLine(aPnt11,aPnt12,aNormal,anOrigin,aParam,aPnt1);
984
985   vtkFloatingPointType aPnt21[3] = {aPnt[0] + aDelta[0][0] - aDelta[1][0],
986                                     aPnt[1] + aDelta[0][1] - aDelta[1][1],
987                                     aPnt[2] + aDelta[0][2] - aDelta[1][2]};
988   vtkFloatingPointType aPnt22[3] = {aPnt21[0] + aNormal[0],
989                                     aPnt21[1] + aNormal[1],
990                                     aPnt21[2] + aNormal[2]};
991   vtkPlane::IntersectWithLine(aPnt21,aPnt22,aNormal,anOrigin,aParam,aPnt2);
992
993   vtkPlaneSource* aPlaneSource = aPlane->myPlaneSource;
994   aPlaneSource->SetNormal(aNormal[0],aNormal[1],aNormal[2]);
995   aPlaneSource->SetOrigin(aPnt0[0],aPnt0[1],aPnt0[2]);
996   aPlaneSource->SetPoint1(aPnt1[0],aPnt1[1],aPnt1[2]);
997   aPlaneSource->SetPoint2(aPnt2[0],aPnt2[1],aPnt2[2]);
998
999   if (AutoApplyCheckBox->isChecked())
1000     ClickOnApply();
1001
1002   if (SVTK_ViewWindow* vw = VISU::GetActiveViewWindow<SVTK_ViewWindow>(myVisuGUI))
1003     VISU::RenderViewWindow(vw);
1004 }
1005
1006 //=================================================================================
1007 // function : onTabChanged
1008 // purpose  :
1009 //=================================================================================
1010 void VisuGUI_ClippingDlg::onTabChanged(QWidget* newTab)
1011 {
1012   if (newTab == WidgetIJKTab) // IJK
1013     setIJKByNonStructured();
1014   else {
1015     // set correct labels of rotation spin boxes
1016     onSelectOrientation(ComboBoxOrientation->currentIndex());
1017   }
1018 }
1019
1020 //=================================================================================
1021 // function : SetCurrentPlaneIJKParam
1022 // purpose  : set non structured parameters by IJK parameters
1023 //=================================================================================
1024 void VisuGUI_ClippingDlg::SetCurrentPlaneIJKParam()
1025 {  
1026   if (myPlanes.empty() || myIsSelectPlane || !WidgetIJKTab->isEnabled())
1027     return;
1028
1029   VISU::Result_i* result = myPrs3d ? myPrs3d->GetCResult() : 0;
1030   if (!result)
1031     return;
1032
1033   // get axis data
1034   int i, axId = ButtonGroupIJKAxis->id (ButtonGroupIJKAxis->checkedButton());
1035   VISU::Result_i::TAxis axis = (VISU::Result_i::TAxis) axId;
1036   gp_Dir dir;
1037   CORBA::String_var aMeshName = myPrs3d->GetMeshName();
1038   const vector<vtkFloatingPointType> * values =
1039     result->GetAxisInfo(aMeshName.in(), axis, dir);
1040   if (!values)
1041     return;
1042
1043   // find distance;
1044   int index = SpinBoxIJKIndex->value();
1045   vtkFloatingPointType distance = 0;
1046   if (index < values->size())
1047     distance = (*values)[ index ];
1048
1049   // find id of axis closest to dir
1050   // 0  || X-Y - axis Z
1051   // 1  || Y-Z - azis X
1052   // 2  || Z-X - axiz Y
1053   double cos[3] = { gp::DZ() * dir, gp::DX() * dir, gp::DY() * dir };
1054   double maxCos = 0;
1055   for (i = 0; i < 3; ++i) {
1056     if (Abs(cos[ i ]) > Abs (maxCos)) {
1057       maxCos = cos[ i ];
1058       axId = i;
1059     }
1060   }
1061   // find rotation angles
1062   vtkFloatingPointType angle[2];
1063   int rotId[2] = {
1064     (axId == 0) ? 2 : axId - 1,
1065     (axId == 2) ? 0 : axId + 1
1066     };
1067   static double aCoeff = 180.0/vtkMath::Pi();
1068   for (i = 0; i < 2; ++i) {
1069     vtkFloatingPointType cosin = cos[ rotId[ i ]];
1070 //     if (maxCos < 0)
1071 //       cosin = -cosin;
1072     angle[ i ] = asin(cosin) * aCoeff;
1073 //     if (maxCos < 0)
1074 //       angle[ i ] += 180. * (angle[ i ] < 0 ? 1. : -1.);
1075   }
1076   if (CheckBoxIJKPlaneReverse->isChecked()) {
1077     angle[ 0 ] += 180. * (angle[ 0 ] < 0 ? 1. : -1.);
1078     distance = 1. - distance;
1079   }
1080 //   if (maxCos < 0)
1081 //     distance = 1. - distance;
1082
1083   // set paramerets
1084   myIsSelectPlane = true;
1085   ComboBoxOrientation->setCurrentIndex(axId);
1086   setRotation(-angle[0], -angle[1]);
1087   setDistance(distance);
1088   myIsSelectPlane = false;
1089
1090   SetCurrentPlaneParam();
1091 }
1092
1093 //=================================================================================
1094 // function : setIJKByNonStructured
1095 // purpose  : convert current non structured parameters to structured ones
1096 //=================================================================================
1097 void VisuGUI_ClippingDlg::setIJKByNonStructured()
1098 {
1099   if (!myPrs3d || myPlanes.empty() || !myPrs3d->GetCResult())
1100     return;
1101
1102   // get plane normal
1103   int planeIndex = ComboBoxPlanes->currentIndex();
1104   OrientedPlane* plane = myPlanes[ planeIndex ].GetPointer();
1105   vtkPlaneSource* planeSource = plane->myPlaneSource;
1106   vtkFloatingPointType * planeNormal = planeSource->GetNormal();
1107   gp_Dir normal(planeNormal[0], planeNormal[1], planeNormal[2]);
1108
1109   // find a grid axis most co-directed with plane normal
1110   // and cartesian axis most co-directed with plane normal
1111   int i, maxAx = 0, gridAxId = 0;
1112   gp_Dir dir, gridDir;
1113   double maxDot = 0;
1114   const vector<vtkFloatingPointType> *curValues, *values = 0;
1115   VISU::Result_i* result = myPrs3d->GetCResult();
1116   int aNbAxes = 3;
1117   VISU_PipeLine* aPipeLine = myPrs3d->GetPipeLine();
1118   VISU::PIDMapper anIDMapper = aPipeLine->GetIDMapper();
1119   if ( anIDMapper->IsStructured() && !anIDMapper->myIsPolarType )
1120     aNbAxes = anIDMapper->GetStructureDim();
1121   for (i = 0; i < aNbAxes; ++i) {
1122     VISU::Result_i::TAxis axis = (VISU::Result_i::TAxis) i;
1123     CORBA::String_var aMeshName = myPrs3d->GetMeshName();
1124     curValues = result->GetAxisInfo(aMeshName.in(), axis, dir);
1125     if (curValues) {
1126       double dot = normal * dir;
1127       //ENK: 23.11.2006 - PAL13176
1128       if(i==0){
1129         maxDot = dot;
1130         gridDir = dir;
1131         values = curValues;
1132         gridAxId = i;
1133       } else if (Abs(dot) >= Abs(maxDot)) {
1134         maxDot = dot;
1135         gridDir = dir;
1136         values = curValues;
1137         gridAxId = i;
1138       }
1139       //ENK: 23.11.2006
1140     }
1141     if (Abs (planeNormal[ maxAx ]) < Abs (planeNormal[ i ]))
1142       maxAx = i;
1143   }
1144   gp_XYZ axDir(0,0,0);
1145   axDir.SetCoord(maxAx + 1, 1.);
1146
1147   // find index value
1148   double v = SpinBoxDistance->value();
1149   // reverse value?
1150    bool reverse = (normal * axDir < 0); // normal and axis are opposite
1151    if (gridDir * axDir < 0) // grid dir and axis are opposite
1152      reverse = !reverse;
1153    if (reverse)
1154      v = 1. - v;
1155   for (i = 0; i < values->size(); ++i)
1156     if ((*values)[ i ] > v)
1157       break;
1158   if (i == values->size())
1159     --i;
1160   if (i != 0 && (*values)[ i ] - v > v - (*values)[ i - 1])
1161     --i;
1162
1163   // set control values
1164   onIJKAxisChanged(gridAxId); // first of all update label and range of index
1165   myIsSelectPlane = true;
1166   CheckBoxIJKPlaneReverse->setChecked(normal * axDir < 0);
1167   SpinBoxIJKIndex->setValue(i);
1168   ButtonGroupIJKAxis->button(gridAxId)->setChecked( true );
1169   myIsSelectPlane = false;
1170
1171   SetCurrentPlaneIJKParam();
1172 }
1173
1174 //=================================================================================
1175 // function : isStructured
1176 // purpose  : return true if mesh is structured
1177 //=================================================================================
1178 bool VisuGUI_ClippingDlg::isStructured() const
1179 {
1180   VISU::Result_i* result = myPrs3d ? myPrs3d->GetCResult() : 0;
1181   if (result) {
1182     gp_Dir dir;
1183     return result->GetAxisInfo(myPrs3d->GetCMeshName(),
1184                                VISU::Result_i::AXIS_X,
1185                                dir);
1186   }
1187   return false;
1188 }
1189
1190 //=================================================================================
1191 // function : onIJKAxisChanged
1192 // purpose  : update Index range and call SetCurrentPlaneParam()
1193 //=================================================================================
1194 void VisuGUI_ClippingDlg::onIJKAxisChanged(int axisId)
1195 {
1196   // set index range
1197   int maxIndex = 0;
1198   VISU::Result_i* result = myPrs3d ? myPrs3d->GetCResult() : 0;
1199   if (result) {
1200     VISU::Result_i::TAxis axis = (VISU::Result_i::TAxis) axisId;
1201     gp_Dir dir;
1202     CORBA::String_var aMeshName = myPrs3d->GetMeshName();
1203     const vector<vtkFloatingPointType> * indices = result->GetAxisInfo(aMeshName.in(),
1204                                                                        axis, dir);
1205     if (indices)
1206       maxIndex = indices->size() - 1;
1207   }
1208   QString text = tr("LBL_IJK_INDEX_TO_arg").arg(maxIndex);
1209   TextLabelIJKIndex->setText(text);
1210   SpinBoxIJKIndex->setRange(0, maxIndex);
1211
1212   if (SpinBoxIJKIndex->value() > maxIndex)
1213     SpinBoxIJKIndex->setValue(0);
1214
1215   SetCurrentPlaneIJKParam();
1216 }
1217
1218 //=================================================================================
1219 // function : OnPreviewToggle()
1220 // purpose  :
1221 //=================================================================================
1222 void VisuGUI_ClippingDlg::OnPreviewToggle (bool theIsToggled)
1223 {
1224   std::for_each(myPlanes.begin(),myPlanes.end(),TSetVisiblity(theIsToggled));
1225   if (SVTK_ViewWindow* vw = VISU::GetActiveViewWindow<SVTK_ViewWindow>(myVisuGUI))
1226     VISU::RenderViewWindow(vw);
1227 }
1228
1229
1230 //=================================================================================
1231 // function : keyPressEvent()
1232 // purpose  :
1233 //=================================================================================
1234 void VisuGUI_ClippingDlg::keyPressEvent( QKeyEvent* e )
1235 {
1236   QDialog::keyPressEvent( e );
1237   if ( e->isAccepted() )
1238     return;
1239
1240   if ( e->key() == Qt::Key_F1 )
1241     {
1242       e->accept();
1243       ClickOnHelp();
1244     }
1245 }
1246
1247 void VisuGUI_ClippingDlg::SetPrs3d(VISU::Prs3d_i* thePrs)
1248 {
1249   if(thePrs != myPrs3d){
1250     if(myPrs3d)
1251       myPrs3d->UnRegister();
1252     if(thePrs)
1253       thePrs->Register();
1254     myPrs3d = thePrs;
1255   } else 
1256     return;
1257 }
1258
1259
1260 void VisuGUI_ClippingDlg::removeAllClippingPlanes(VISU::Prs3d_i* thePrs)
1261 {
1262   for (int i = thePrs->GetNumberOfClippingPlanes() - 1; i >= 0 ; i--) {
1263     OrientedPlane* aPlane = dynamic_cast<OrientedPlane*>(thePrs->GetClippingPlane(i));
1264     if (aPlane) 
1265       thePrs->RemoveClippingPlane(i);
1266   }
1267 }
1268
1269