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