Salome HOME
371a0582afdf70bef7c17937c271688c85a4503b
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_ClippingDlg.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // SMESH SMESHGUI : GUI for SMESH component
24 // File   : SMESHGUI_ClippingDlg.cxx
25 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
26 // SMESH includes
27 //
28 #include "SMESHGUI_ClippingDlg.h"
29
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_Utils.h"
32 #include "SMESHGUI_VTKUtils.h"
33 #include "SMESHGUI_SpinBox.h"
34
35 #include <QtxDoubleSpinSlider.h>
36 #include <QtxIntSpinSlider.h>
37
38 #include <SMESH_Actor.h>
39 #include <SMESH_ActorUtils.h>
40
41 // SALOME GUI includes
42 #include <SUIT_Desktop.h>
43 #include <SUIT_Session.h>
44 #include <SUIT_OverrideCursor.h>
45 #include <SUIT_MessageBox.h>
46 #include <SUIT_ResourceMgr.h>
47 #include <SUIT_ViewManager.h>
48
49 #include <SALOME_ListIO.hxx>
50
51 #include <SalomeApp_Study.h>
52
53 #include <LightApp_Application.h>
54
55 #include <VTKViewer_Algorithm.h>
56
57 #include <SVTK_ViewWindow.h>
58 #include <SVTK_RenderWindowInteractor.h>
59
60 // Qt includes
61 #include <QLabel>
62 #include <QPushButton>
63 #include <QComboBox>
64 #include <QCheckBox>
65 #include <QVBoxLayout>
66 #include <QHBoxLayout>
67 #include <QGridLayout>
68 #include <QGroupBox>
69 #include <QKeyEvent>
70 #include <QListWidget>
71 #include <QStackedLayout>
72 #include <QSlider>
73 #include <QMenu>
74
75 // VTK includes
76 #include <vtkMath.h>
77 #include <vtkDataSet.h>
78 #include <vtkDataSetMapper.h>
79 #include <vtkPlaneSource.h>
80 #include <vtkProperty.h>
81 #include <vtkRenderer.h>
82 #include <vtkCallbackCommand.h>
83 #include <vtkImplicitPlaneWidget.h>
84
85 #define SPACING 6
86 #define MARGIN  11
87 #define SIZEFACTOR 1.1
88
89 /*!
90   Create new object of class OrientedPlane
91  */
92 SMESH::OrientedPlane* SMESH::OrientedPlane::New()
93 {
94   return new OrientedPlane();
95 }
96
97 /*!
98   Create new object of class OrientedPlane
99  */
100 SMESH::OrientedPlane* SMESH::OrientedPlane::New( SVTK_ViewWindow* theViewWindow )
101 {
102   return new OrientedPlane( theViewWindow );
103 }
104
105 /*!
106   Copy the object of class OrientedPlane
107  */
108 void SMESH::OrientedPlane::ShallowCopy( SMESH::OrientedPlane* theOrientedPlane )
109 {
110   SetNormal( theOrientedPlane->GetNormal() );
111   SetOrigin( theOrientedPlane->GetOrigin() );
112
113   myRelativeOrientation = theOrientedPlane->GetOrientation();
114   myDistance = theOrientedPlane->GetDistance();
115
116   IsOpenGLClipping = theOrientedPlane->IsOpenGLClipping;
117
118   myAngle[0] = theOrientedPlane->myAngle[0];
119   myAngle[1] = theOrientedPlane->myAngle[1];
120
121   myAbsoluteOrientation = theOrientedPlane->myAbsoluteOrientation;
122   X = theOrientedPlane->X;
123   Y = theOrientedPlane->Y;
124   Z = theOrientedPlane->Z;
125   Dx = theOrientedPlane->Dx;
126   Dy = theOrientedPlane->Dy;
127   Dz = theOrientedPlane->Dz;
128
129   PlaneMode = theOrientedPlane->PlaneMode;
130
131   myPlaneSource->SetNormal( theOrientedPlane->myPlaneSource->GetNormal() );
132   myPlaneSource->SetOrigin( theOrientedPlane->myPlaneSource->GetOrigin() );
133   myPlaneSource->SetPoint1( theOrientedPlane->myPlaneSource->GetPoint1() );
134   myPlaneSource->SetPoint2( theOrientedPlane->myPlaneSource->GetPoint2() );
135   myPlaneSource->Update();
136 }
137
138 /*!
139   Invert current clipping plane in contrary direction
140  */
141 SMESH::OrientedPlane* SMESH::OrientedPlane::InvertPlane()
142 {
143   OrientedPlane* aPlane = new OrientedPlane();
144   aPlane->ShallowCopy( this );
145   double* aNormal = aPlane->GetNormal();
146   for( int i=0; i<3; i++ )
147     aNormal[i] = -aNormal[i];
148   aPlane->SetNormal( aNormal );
149   return aPlane;
150 }
151
152 /*!
153   Constructor of class OrientedPlane
154  */
155 SMESH::OrientedPlane::OrientedPlane(SVTK_ViewWindow* theViewWindow):
156   myViewWindow(theViewWindow)
157 {
158   Init();
159   myViewWindow->AddActor(myActor, false, false); // don't adjust actors
160 }
161
162 /*!
163   Constructor of class OrientedPlane
164  */
165 SMESH::OrientedPlane::OrientedPlane():
166   myViewWindow(NULL)
167 {
168   Init();
169 }
170
171 /*!
172   Initialize parameters of class OrientedPlane
173  */
174 void SMESH::OrientedPlane::Init()
175 {
176   myPlaneSource = vtkPlaneSource::New();
177
178   PlaneMode = SMESH::Absolute;
179   X = Y = Z = 0.0;
180   Dx = Dy = Dz = 1.0;
181   myAbsoluteOrientation = 0; // CUSTOM
182   myRelativeOrientation = SMESH::XY;
183   myDistance = 0.5;
184   myAngle[0] = myAngle[1] = 0.0;
185   IsInvert = false;
186   IsOpenGLClipping = false;
187
188   // Create and display actor
189   myMapper = vtkDataSetMapper::New();
190   myMapper->SetInputConnection(myPlaneSource->GetOutputPort());
191
192   myActor = SALOME_Actor::New();
193   myActor->VisibilityOff();
194   myActor->PickableOff();
195   myActor->SetInfinitive(true);
196   myActor->SetMapper(myMapper);
197
198   QColor ffc, bfc;
199   int delta;
200   SMESH::GetColor( "SMESH", "fill_color", ffc, delta, "255, 170, 0|-100" ) ;
201  
202   vtkProperty* aProp = vtkProperty::New();
203   SMESH::GetColor( "SMESH", "fill_color", ffc, delta, "255, 170, 0|-100" ) ;
204   aProp->SetColor(ffc.red() / 255. , ffc.green() / 255. , ffc.blue() / 255.);
205   aProp->SetOpacity(0.75);
206   myActor->SetProperty(aProp);
207   aProp->Delete();
208
209   vtkProperty* aBackProp = vtkProperty::New();
210   bfc = Qtx::mainColorToSecondary(ffc, delta);
211   aBackProp->SetColor( bfc.red() / 255. , bfc.green() / 255. , bfc.blue() / 255.);
212   aBackProp->SetOpacity(0.75);
213   myActor->SetBackfaceProperty(aBackProp);
214   aBackProp->Delete();
215 }
216
217 /*!
218   Destructor of class OrientedPlane
219  */
220 SMESH::OrientedPlane::~OrientedPlane()
221 {
222   if (myViewWindow)
223     myViewWindow->RemoveActor(myActor);
224   myActor->Delete();
225     
226   myMapper->RemoveAllInputs();
227   myMapper->Delete();
228
229   // commented: porting to vtk 5.0
230   // myPlaneSource->UnRegisterAllOutputs();
231   myPlaneSource->Delete();
232 }
233
234 /*!
235   Definition of class ActorItem
236  */
237 class ActorItem : public QListWidgetItem
238 {
239 public:
240   ActorItem( SMESH_Actor* theActor, const QString& theName, QListWidget* theListWidget ) :
241     QListWidgetItem( theName, theListWidget ),
242     myActor( theActor ) {}
243
244   SMESH_Actor* getActor() const { return myActor; }
245
246 private:
247   SMESH_Actor* myActor;
248 };
249
250 /*!
251   Definition of class TSetVisibility
252  */
253 struct TSetVisibility {
254   // Set visibility of cutting plane
255   TSetVisibility(int theIsVisible): myIsVisible(theIsVisible){}
256   void operator()(SMESH::TPlaneData& thePlaneData){
257     bool anIsEmpty = thePlaneData.ActorList.empty();
258     thePlaneData.Plane.GetPointer()->myActor->SetVisibility(myIsVisible && !anIsEmpty);
259   }
260   int myIsVisible;
261 };
262
263 /*********************************************************************************
264  *********************      class SMESHGUI_ClippingDlg      *********************
265  *********************************************************************************/
266
267 /*!
268   Constructor
269 */
270 SMESHGUI_ClippingDlg::SMESHGUI_ClippingDlg( SMESHGUI* theModule, SVTK_ViewWindow* theViewWindow ):
271   QDialog( SMESH::GetDesktop(theModule) ),
272   mySMESHGUI(theModule),
273   myViewWindow(theViewWindow)
274 {
275   setModal( false );
276   setAttribute( Qt::WA_DeleteOnClose, true );
277   setWindowTitle(tr("SMESH_CLIPPING_TITLE"));
278   setSizeGripEnabled(true);
279
280   myPreviewWidget = vtkImplicitPlaneWidget::New();
281   myCallback = vtkCallbackCommand::New();
282   myCallback->SetClientData( this );
283   myCallback->SetCallback( SMESHGUI_ClippingDlg::ProcessEvents );
284   myPreviewWidget = createPreviewWidget();
285
286   myIsPreviewMoved = false;
287
288   QVBoxLayout* SMESHGUI_ClippingDlgLayout = new QVBoxLayout(this);
289   SMESHGUI_ClippingDlgLayout->setSpacing(SPACING);
290   SMESHGUI_ClippingDlgLayout->setMargin(MARGIN);
291
292   // Controls for selecting, creating, deleting planes
293   QGroupBox* GroupPlanes = new QGroupBox(tr("CLIP_PLANES"), this);
294   QGridLayout* GroupPlanesLayout = new QGridLayout(GroupPlanes);
295   GroupPlanesLayout->setSpacing(SPACING);
296   GroupPlanesLayout->setMargin(MARGIN);
297
298   ComboBoxPlanes = new QComboBox(GroupPlanes);
299
300   isOpenGLClipping = new QCheckBox( GroupPlanes );
301   isOpenGLClipping->setText( tr( "IS_OPENGL_CLIPPING" ) );
302
303   buttonNew = new QPushButton(tr("SMESH_BUT_NEW"), GroupPlanes);
304
305   MenuMode = new QMenu( "MenuMode", buttonNew );
306   MenuMode->addAction( tr( "ABSOLUTE" ), this, SLOT( onModeAbsolute() ) );
307   MenuMode->addAction( tr( "RELATIVE" ), this, SLOT( onModeRelative() ) );
308   buttonNew->setMenu( MenuMode );
309   CurrentMode = SMESH::Absolute;
310
311   buttonDelete = new QPushButton(tr("SMESH_BUT_DELETE"), GroupPlanes);
312
313   QLabel* aLabel = new QLabel(tr("MESHES_SUBMESHES_GROUPS"), GroupPlanes);
314
315   ActorList = new QListWidget(GroupPlanes);
316   ActorList->setSelectionMode(QAbstractItemView::SingleSelection);
317   
318   SelectAllCheckBox = new QCheckBox(tr("SELECT_ALL"), GroupPlanes);
319
320   GroupPlanesLayout->addWidget(ComboBoxPlanes,    0, 0);
321   GroupPlanesLayout->addWidget(isOpenGLClipping,  0, 1);
322   GroupPlanesLayout->addWidget(new QWidget(),     0, 2);
323   GroupPlanesLayout->addWidget(buttonNew,         0, 3);
324   GroupPlanesLayout->addWidget(buttonDelete,      0, 4);
325   GroupPlanesLayout->addWidget(aLabel,            1, 0, 1, 5);
326   GroupPlanesLayout->addWidget(ActorList,         2, 0, 1, 5);
327   GroupPlanesLayout->addWidget(SelectAllCheckBox, 3, 0, 1, 5);
328   GroupPlanesLayout->setColumnStretch( 1, 1 );
329
330   ModeStackedLayout = new QStackedLayout();
331
332   // Controls for defining plane parameters
333   /**********************   Mode Absolute   **********************/
334   /* Controls for absolute mode of clipping plane:
335      X, Y, Z - coordinates of the intersection of cutting plane and the three axes
336      Dx, Dy, Dz - components of normal to the cutting plane
337      Orientation - direction of cutting plane
338   */
339   const double min = -1e+7;
340   const double max =  1e+7;
341   const double step = 5;
342   const int precision = -7;
343
344   // Croup Point
345   QGroupBox* GroupAbsolutePoint = new QGroupBox( this );
346   GroupAbsolutePoint->setObjectName( "GroupPoint" );
347   GroupAbsolutePoint->setTitle( tr("BASE_POINT") );
348   QGridLayout* GroupPointLayout = new QGridLayout( GroupAbsolutePoint );
349   GroupPointLayout->setAlignment( Qt::AlignTop );
350   GroupPointLayout->setSpacing( 6 ); GroupPointLayout->setMargin( 11 );
351
352   TextLabelX = new QLabel( GroupAbsolutePoint );
353   TextLabelX->setObjectName( "TextLabelX" );
354   TextLabelX->setText( tr("X:") );
355   GroupPointLayout->addWidget( TextLabelX, 0, 0 );
356
357   SpinBox_X = new QtxDoubleSpinBox( min, max, step, GroupAbsolutePoint );
358   SpinBox_X->setObjectName("SpinBox_X" );
359   SpinBox_X->setPrecision( precision );
360   GroupPointLayout->addWidget( SpinBox_X, 0, 1 );
361
362   TextLabelY = new QLabel( GroupAbsolutePoint );
363   TextLabelY->setObjectName( "TextLabelY" );
364   TextLabelY->setText( tr("Y:") );
365   GroupPointLayout->addWidget( TextLabelY, 0, 2 );
366
367   SpinBox_Y = new QtxDoubleSpinBox( min, max, step, GroupAbsolutePoint );
368   SpinBox_Y->setObjectName("SpinBox_Y" );
369   SpinBox_Y->setPrecision( precision );
370   GroupPointLayout->addWidget( SpinBox_Y, 0, 3 );
371
372   TextLabelZ = new QLabel( GroupAbsolutePoint );
373   TextLabelZ->setObjectName( "TextLabelZ" );
374   TextLabelZ->setText( tr("Z:") );
375   GroupPointLayout->addWidget( TextLabelZ, 0, 4 );
376
377   SpinBox_Z = new QtxDoubleSpinBox( min, max, step, GroupAbsolutePoint );
378   SpinBox_Z->setObjectName("SpinBox_Z" );
379   SpinBox_Z->setPrecision( precision );
380   GroupPointLayout->addWidget( SpinBox_Z, 0, 5 );
381
382   resetButton  = new QPushButton( GroupAbsolutePoint );
383   resetButton->setObjectName( "resetButton" );
384   resetButton->setText( tr( "RESET"  ) );
385   GroupPointLayout->addWidget( resetButton, 0, 6 );
386
387   // Group Direction
388   GroupAbsoluteDirection = new QGroupBox( this );
389   GroupAbsoluteDirection->setObjectName( "GroupDirection" );
390   GroupAbsoluteDirection->setTitle( tr("DIRECTION") );
391   QGridLayout* GroupDirectionLayout = new QGridLayout( GroupAbsoluteDirection );
392   GroupDirectionLayout->setAlignment( Qt::AlignTop );
393   GroupDirectionLayout->setSpacing( 6 );
394   GroupDirectionLayout->setMargin( 11 );
395
396   TextLabelDx = new QLabel( GroupAbsoluteDirection );
397   TextLabelDx->setObjectName( "TextLabelDx" );
398   TextLabelDx->setText( tr("Dx:") );
399   GroupDirectionLayout->addWidget( TextLabelDx, 0, 0 );
400
401   SpinBox_Dx = new QtxDoubleSpinBox( min, max, step, GroupAbsoluteDirection );
402   SpinBox_Dx->setObjectName("SpinBox_Dx" );
403   SpinBox_Dx->setPrecision( precision );
404   GroupDirectionLayout->addWidget( SpinBox_Dx, 0, 1 );
405
406   TextLabelDy = new QLabel( GroupAbsoluteDirection );
407   TextLabelDy->setObjectName( "TextLabelDy" );
408   TextLabelDy->setText( tr("Dy:") );
409   GroupDirectionLayout->addWidget( TextLabelDy, 0, 2 );
410
411   SpinBox_Dy = new QtxDoubleSpinBox( min, max, step, GroupAbsoluteDirection );
412   SpinBox_Dy->setObjectName("SpinBox_Dy" );
413   SpinBox_Dy->setPrecision( precision );
414   GroupDirectionLayout->addWidget( SpinBox_Dy, 0, 3 );
415
416   TextLabelDz = new QLabel( GroupAbsoluteDirection );
417   TextLabelDz->setObjectName( "TextLabelDz" );
418   TextLabelDz->setText( tr("Dz:") );
419   GroupDirectionLayout->addWidget( TextLabelDz, 0, 4 );
420
421   SpinBox_Dz = new QtxDoubleSpinBox( min, max, step, GroupAbsoluteDirection );
422   SpinBox_Dz->setObjectName("SpinBox_Dz" );
423   SpinBox_Dz->setPrecision( precision );
424   GroupDirectionLayout->addWidget( SpinBox_Dz, 0, 5 );
425
426   invertButton  = new QPushButton( GroupAbsoluteDirection );
427   invertButton->setObjectName( "invertButton" );
428   invertButton->setText( tr( "INVERT"  ) );
429   GroupDirectionLayout->addWidget( invertButton, 0, 6 );
430
431   CBAbsoluteOrientation = new QComboBox( GroupAbsoluteDirection );
432   CBAbsoluteOrientation->setObjectName( "AbsoluteOrientation" );
433   CBAbsoluteOrientation->insertItem( CBAbsoluteOrientation->count(), tr( "CUSTOM" ) );
434   CBAbsoluteOrientation->insertItem( CBAbsoluteOrientation->count(), tr( "||X-Y" ) );
435   CBAbsoluteOrientation->insertItem( CBAbsoluteOrientation->count(), tr( "||Y-Z" ) );
436   CBAbsoluteOrientation->insertItem( CBAbsoluteOrientation->count(), tr( "||Z-X" ) );
437   GroupDirectionLayout->addWidget( CBAbsoluteOrientation, 1, 0, 1, 6 );
438
439   QVBoxLayout* ModeActiveLayout = new QVBoxLayout();
440   ModeActiveLayout->setMargin( 11 ); ModeActiveLayout->setSpacing( 6 );
441   ModeActiveLayout->addWidget( GroupAbsolutePoint );
442   ModeActiveLayout->addWidget( GroupAbsoluteDirection );
443
444   QWidget* ModeActiveWidget = new QWidget( this );
445   ModeActiveWidget->setLayout( ModeActiveLayout );
446
447   /**********************   Mode Relative   **********************/
448   /* Controls for relative mode of clipping plane:
449      Distance - Value from 0 to 1.
450      Specifies the distance from the minimum value in a given direction of bounding box to the current position
451      Rotation1, Rotation2 - turn angles of cutting plane in given directions
452      Orientation - direction of cutting plane
453   */
454   QGroupBox* GroupParameters = new QGroupBox( tr("SMESH_PARAMETERS"), this );
455   QGridLayout* GroupParametersLayout = new QGridLayout( GroupParameters );
456   GroupParametersLayout->setMargin( 11 ); GroupParametersLayout->setSpacing( 6 );
457
458   TextLabelOrientation = new QLabel( tr("SMESH_ORIENTATION"), GroupParameters);
459   TextLabelOrientation->setObjectName( "TextLabelOrientation" );
460   GroupParametersLayout->addWidget( TextLabelOrientation, 0, 0 );
461
462   CBRelativeOrientation = new QComboBox(GroupParameters);
463   CBRelativeOrientation->setObjectName( "RelativeOrientation" );
464   CBRelativeOrientation->addItem( tr("ALONG_XY") );
465   CBRelativeOrientation->addItem( tr("ALONG_YZ") );
466   CBRelativeOrientation->addItem( tr("ALONG_ZX") );
467   GroupParametersLayout->addWidget( CBRelativeOrientation, 0, 1 );
468
469   TextLabelDistance = new QLabel( tr("SMESH_DISTANCE"), GroupParameters );
470   TextLabelDistance->setObjectName( "TextLabelDistance" );
471   GroupParametersLayout->addWidget( TextLabelDistance, 1, 0 );
472
473   SpinSliderDistance = new QtxDoubleSpinSlider( 0., 1., 0.01, GroupParameters );
474   SpinSliderDistance->setObjectName( "SpinSliderDistance" );
475   SpinSliderDistance->setPrecision( precision );
476   QFont fnt = SpinSliderDistance->font(); fnt.setBold( true ); SpinSliderDistance->setFont( fnt );
477   GroupParametersLayout->addWidget( SpinSliderDistance, 1, 1 );
478   
479   QString aUnitRot = "\xB0";
480
481   TextLabelRotation1 = new QLabel( tr("ROTATION_AROUND_X_Y2Z"), GroupParameters );
482   TextLabelRotation1->setObjectName( "TextLabelRotation1" );
483   GroupParametersLayout->addWidget( TextLabelRotation1, 2, 0 );
484
485   SpinSliderRotation1 = new QtxIntSpinSlider( -180, 180, 1, GroupParameters );
486   SpinSliderRotation1->setObjectName( "SpinSliderRotation1" );
487   SpinSliderRotation1->setUnit( aUnitRot );
488   SpinSliderRotation1->setFont( fnt );
489   GroupParametersLayout->addWidget( SpinSliderRotation1, 2, 1 );
490
491   TextLabelRotation2 = new QLabel(tr("ROTATION_AROUND_Y_X2Z"), GroupParameters);
492   TextLabelRotation2->setObjectName( "TextLabelRotation2" );
493   TextLabelRotation2->setObjectName( "TextLabelRotation2" );
494   GroupParametersLayout->addWidget( TextLabelRotation2, 3, 0 );
495
496   SpinSliderRotation2 = new QtxIntSpinSlider( -180, 180, 1, GroupParameters );
497   SpinSliderRotation2->setObjectName( "SpinSliderRotation2" );
498   SpinSliderRotation2->setUnit( aUnitRot );
499   SpinSliderRotation2->setFont( fnt );
500   GroupParametersLayout->addWidget( SpinSliderRotation2, 3, 1 );
501
502   /***************************************************************/
503   QWidget* CheckBoxWidget = new QWidget( this );
504   QHBoxLayout* CheckBoxLayout = new QHBoxLayout( CheckBoxWidget );
505
506   PreviewCheckBox = new QCheckBox( tr("SHOW_PREVIEW"), CheckBoxWidget );
507   PreviewCheckBox->setObjectName( "PreviewCheckBox" );
508   PreviewCheckBox->setChecked( true );
509   CheckBoxLayout->addWidget( PreviewCheckBox, 0, Qt::AlignCenter );
510
511   AutoApplyCheckBox = new QCheckBox( tr("AUTO_APPLY"), CheckBoxWidget );
512   AutoApplyCheckBox->setObjectName( "AutoApplyCheckBox" );
513   CheckBoxLayout->addWidget( AutoApplyCheckBox, 0, Qt::AlignCenter );
514
515   /***************************************************************/
516   // Controls for "Ok", "Apply" and "Close" button
517   QGroupBox* GroupButtons = new QGroupBox(this);
518   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
519   GroupButtonsLayout->setSpacing(SPACING);
520   GroupButtonsLayout->setMargin(MARGIN);
521   
522   buttonOk = new QPushButton( tr( "SMESH_BUT_APPLY_AND_CLOSE" ), GroupButtons );
523   buttonOk->setAutoDefault( true );
524   buttonOk->setDefault( true );
525   buttonApply = new QPushButton( tr( "SMESH_BUT_APPLY" ), GroupButtons );
526   buttonApply->setAutoDefault( true );
527   buttonCancel = new QPushButton( tr( "SMESH_BUT_CLOSE" ), GroupButtons );
528   buttonCancel->setAutoDefault( true );
529   buttonHelp = new QPushButton( tr( "SMESH_BUT_HELP" ), GroupButtons );
530   buttonHelp->setAutoDefault( true );
531   GroupButtonsLayout->addWidget( buttonOk );
532   GroupButtonsLayout->addSpacing(10);
533   GroupButtonsLayout->addWidget( buttonApply );
534   GroupButtonsLayout->addSpacing(10);
535   GroupButtonsLayout->addStretch();
536   GroupButtonsLayout->addWidget( buttonCancel );
537   GroupButtonsLayout->addWidget( buttonHelp );
538
539   ModeStackedLayout->addWidget( ModeActiveWidget );
540   ModeStackedLayout->addWidget( GroupParameters );
541
542   SMESHGUI_ClippingDlgLayout->addWidget( GroupPlanes );
543   SMESHGUI_ClippingDlgLayout->addLayout( ModeStackedLayout );
544   SMESHGUI_ClippingDlgLayout->addWidget( CheckBoxWidget );
545   SMESHGUI_ClippingDlgLayout->addWidget( GroupButtons );
546
547   // Initializations
548   initParam();
549
550   myIsSelectPlane = false;
551
552   myHelpFileName = "clipping_page.html";
553
554   // signals and slots connections :
555   connect( ComboBoxPlanes, SIGNAL( activated( int ) ), this, SLOT( onSelectPlane( int ) ) );
556   connect( isOpenGLClipping, SIGNAL( toggled( bool ) ), this, SLOT( onIsOpenGLClipping( bool ) ) );
557   connect( buttonNew, SIGNAL( clicked() ), buttonNew, SLOT( showMenu() ) );
558   connect( buttonDelete, SIGNAL( clicked() ), this, SLOT( ClickOnDelete() ) );
559   connect( ActorList, SIGNAL( itemChanged( QListWidgetItem* ) ), this, SLOT( onActorItemChanged( QListWidgetItem*) ) );
560   connect( SelectAllCheckBox, SIGNAL( stateChanged( int ) ), this, SLOT( onSelectAll( int ) ) );
561
562   connect( invertButton, SIGNAL (clicked() ), this, SLOT( onInvert() ) ) ;
563   connect( resetButton,  SIGNAL (clicked() ), this, SLOT( onReset() ) );
564   connect( SpinBox_X,  SIGNAL ( valueChanged( double ) ),  this, SLOT( SetCurrentPlaneParam() ) );
565   connect( SpinBox_Y,  SIGNAL ( valueChanged( double ) ),  this, SLOT( SetCurrentPlaneParam() ) );
566   connect( SpinBox_Z,  SIGNAL ( valueChanged( double ) ),  this, SLOT( SetCurrentPlaneParam() ) );
567   connect( SpinBox_Dx, SIGNAL ( valueChanged( double ) ),  this, SLOT( SetCurrentPlaneParam() ) );
568   connect( SpinBox_Dy, SIGNAL ( valueChanged( double ) ),  this, SLOT( SetCurrentPlaneParam() ) );
569   connect( SpinBox_Dz, SIGNAL ( valueChanged( double ) ),  this, SLOT( SetCurrentPlaneParam() ) );
570   connect( CBAbsoluteOrientation, SIGNAL ( activated ( int ) ), this, SLOT( onSelectAbsoluteOrientation( int ) ) ) ;
571
572   connect( CBRelativeOrientation, SIGNAL( activated( int ) ), this, SLOT( onSelectRelativeOrientation( int ) ) );
573   connect( SpinSliderDistance, SIGNAL( valueChanged( double ) ),  this, SLOT( SetCurrentPlaneParam() ) );
574   connect( SpinSliderRotation1, SIGNAL( valueChanged( int ) ), this, SLOT( SetCurrentPlaneParam() ) );
575   connect( SpinSliderRotation2, SIGNAL( valueChanged( int ) ), this, SLOT( SetCurrentPlaneParam() ) );
576
577   connect( PreviewCheckBox, SIGNAL( toggled( bool ) ), this, SLOT( OnPreviewToggle( bool ) ) );
578   connect( AutoApplyCheckBox, SIGNAL( toggled( bool ) ), this, SLOT( onAutoApply( bool ) ) );
579   connect( buttonOk, SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
580   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
581   connect( buttonApply, SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
582   connect( buttonHelp, SIGNAL( clicked() ), this, SLOT( ClickOnHelp() ) );
583   connect( mySMESHGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( reject() ) );
584   /* to close dialog if study frame change */
585   connect( mySMESHGUI, SIGNAL ( SignalStudyFrameChanged() ), this, SLOT( reject() ) );
586
587   initializePlaneData();
588   synchronize();
589
590   this->show();
591 }
592
593 /*!
594   Destructor
595   Destroys the object and frees any allocated resources
596 */
597 SMESHGUI_ClippingDlg::~SMESHGUI_ClippingDlg()
598 {
599   // no need to delete child widgets, Qt does it all for us
600   std::for_each(myPlanes.begin(),myPlanes.end(),TSetVisibility(false));
601   if (myViewWindow)
602     SMESH::RenderViewWindow(myViewWindow);
603
604   for ( size_t i = 0; i < myPlanes.size(); i++ ) {
605     SMESH::TPlaneData aPlaneData = myPlanes[i];
606     aPlaneData.Plane->Delete();
607   }
608
609   if ( myPreviewWidget ) {
610     myPreviewWidget->Off();
611     myPreviewWidget->Delete();
612   }
613   myPreviewWidget = 0;
614   myCallback->Delete();
615
616   myViewWindow->Repaint();
617 }
618
619 /*!
620   Get distance for cutting plane in relative mode
621 */
622 double SMESHGUI_ClippingDlg::getDistance() const
623 {
624   return SpinSliderDistance->value();
625 }
626
627 /*!
628   Set distance of cutting plane in relative mode
629 */
630 void SMESHGUI_ClippingDlg::setDistance( const double theDistance )
631 {
632   SpinSliderDistance->setValue( theDistance );
633 }
634
635 /*!
636   Get rotation1 for cutting plane in relative mode
637 */
638 double SMESHGUI_ClippingDlg::getRotation1() const
639 {
640   return SpinSliderRotation1->value();
641 }
642
643 /*!
644   Get rotation2 for cutting plane in relative mode
645 */
646 double SMESHGUI_ClippingDlg::getRotation2() const
647 {
648   return SpinSliderRotation2->value();
649 }
650
651 /*!
652   Set angles of clipping plane in relative mode
653 */
654 void SMESHGUI_ClippingDlg::setRotation (const double theRot1, const double theRot2)
655 {
656   SpinSliderRotation1->setValue( int(floor(theRot1)) );
657   SpinSliderRotation2->setValue( int(floor(theRot2)) );
658 }
659
660 /*!
661   Set coordinates of origin point in dialog box
662 */
663 void SMESHGUI_ClippingDlg::setOrigin( double theVal[3] )
664 {
665   int anOrientation = CBAbsoluteOrientation->currentIndex();
666   if( anOrientation == 0 || anOrientation == 2 )
667     SpinBox_X->setValue( theVal[0] );
668   if( anOrientation == 0 || anOrientation == 3 )
669     SpinBox_Y->setValue( theVal[1] );
670   if( anOrientation == 0 || anOrientation == 1 )
671     SpinBox_Z->setValue( theVal[2] );
672 }
673
674 /*!
675   Set coordinates of normal vector in dialog box
676 */
677 void SMESHGUI_ClippingDlg::setDirection( double theVal[3] )
678 {
679   int anOrientation = CBAbsoluteOrientation->currentIndex();
680   if( anOrientation == 0 ) {
681     SpinBox_Dx->setValue( theVal[0] );
682     SpinBox_Dy->setValue( theVal[1] );
683     SpinBox_Dz->setValue( theVal[2] );
684   }
685 }
686
687 /*!
688   Create a new widget for preview clipping plane
689 */
690 vtkImplicitPlaneWidget* SMESHGUI_ClippingDlg::createPreviewWidget()
691 {
692   vtkImplicitPlaneWidget* aPlaneWgt = vtkImplicitPlaneWidget::New();
693
694   aPlaneWgt->SetInteractor( myViewWindow->getInteractor() );
695   aPlaneWgt->SetPlaceFactor( SIZEFACTOR );
696   aPlaneWgt->ScaleEnabledOff();
697   aPlaneWgt->SetOrigin( 0, 0, 0 );
698   aPlaneWgt->SetNormal( -1, -1, -1 );
699   aPlaneWgt->Off();
700
701   double anRGB[3];
702   SMESH::GetColor( "SMESH", "fill_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 170, 255 ) );
703
704   aPlaneWgt->GetPlaneProperty()->SetColor( anRGB[0],anRGB[1],anRGB[2] );
705   aPlaneWgt->GetPlaneProperty()->SetOpacity( 0.2 );;
706
707   aPlaneWgt->GetSelectedPlaneProperty()->SetColor( anRGB[0],anRGB[1],anRGB[2] );
708   aPlaneWgt->GetSelectedPlaneProperty()->SetOpacity( 0.2 );
709   aPlaneWgt->GetSelectedPlaneProperty()->SetLineWidth( 2.0 );
710
711   aPlaneWgt->AddObserver(vtkCommand::InteractionEvent, myCallback, 0.);
712
713   return aPlaneWgt;
714 }
715
716 /*!
717   Translate two angles of plane to normal
718 */
719 void rotationToNormal ( double theRotation[2],
720                         int theOrientation,
721                         double theNormal[3],
722                         double theDir[2][3] )
723 {
724   static double aCoeff = M_PI/180.0;
725
726   double anU[2] = { cos( aCoeff * theRotation[0] ), cos( aCoeff * theRotation[1] ) };
727   double aV[2] = { sqrt( 1.0 - anU[0]*anU[0] ), sqrt( 1.0 - anU[1] * anU[1] ) };
728   aV[0] = theRotation[0] > 0? aV[0]: -aV[0];
729   aV[1] = theRotation[1] > 0? aV[1]: -aV[1];
730
731   switch ( theOrientation ) {
732   case 0:
733   case 1:
734     theDir[0][1] = anU[0];
735     theDir[0][2] = aV[0];
736     theDir[1][0] = anU[1];
737     theDir[1][2] = aV[1];
738     break;
739   case 2:
740     theDir[0][2] = anU[0];
741     theDir[0][0] = aV[0];
742     theDir[1][1] = anU[1];
743     theDir[1][0] = aV[1];
744     break;
745   case 3:
746     theDir[0][0] = anU[0];
747     theDir[0][1] = aV[0];
748     theDir[1][2] = anU[1];
749     theDir[1][1] = aV[1];
750     break;
751   }
752
753   vtkMath::Cross( theDir[1], theDir[0], theNormal );
754   vtkMath::Normalize( theNormal );
755   vtkMath::Cross( theNormal, theDir[1], theDir[0] );
756 }
757
758 /*!
759   Used in SMESHGUI::restoreVisualParameters() to avoid
760   Declaration of OrientedPlane outside of SMESHGUI_ClippingDlg.cxx
761 */
762 bool SMESHGUI_ClippingDlg::AddPlane ( SMESH::TActorList       theActorList,
763                                       SMESH::OrientedPlane*   thePlane )
764 {
765   double aNormal[3];
766   double aDir[2][3] = {{0, 0, 0}, {0, 0, 0}};
767   static double aCoeff = vtkMath::Pi()/180.0;
768
769   int anOrientation;
770   if ( thePlane->PlaneMode == SMESH::Absolute )
771     anOrientation = thePlane->myAbsoluteOrientation;
772   else if ( thePlane->PlaneMode == SMESH::Relative )
773     anOrientation = thePlane->myRelativeOrientation + 1;
774
775   if ( anOrientation == 0 ) {
776     // compute a direction for plane in absolute mode
777     double znam = sqrt( thePlane->Dx*thePlane->Dx + thePlane->Dy*thePlane->Dy + thePlane->Dz*thePlane->Dz );
778     double aRotation = acos( thePlane->Dy/znam )/aCoeff;
779     if ( thePlane->Dy >= 0.0 && thePlane->Dz >= 0.0 )      thePlane->myAngle[0] = 90.0 + aRotation;
780     else if ( thePlane->Dy >= 0.0 && thePlane->Dz < 0.0 ) thePlane->myAngle[0] = 90.0 - aRotation;
781     else if ( thePlane->Dy < 0.0 && thePlane->Dz >= 0.0 ) thePlane->myAngle[0] = aRotation - 90.0;
782     else if ( thePlane->Dy < 0.0 && thePlane->Dz < 0.0 ) thePlane->myAngle[0] = 270.0 - aRotation;
783
784     aRotation = acos( thePlane->Dx/znam )/aCoeff;
785     if ( thePlane->Dx >= 0.0 && thePlane->Dz >= 0.0 )      thePlane->myAngle[1] = 90.0 + aRotation;
786     else if ( thePlane->Dx >= 0.0 && thePlane->Dz < 0.0 ) thePlane->myAngle[1] = 90.0 - aRotation;
787     else if ( thePlane->Dx < 0.0 && thePlane->Dz >= 0.0 ) thePlane->myAngle[1] = aRotation - 90.0;
788     else if ( thePlane->Dx < 0.0 && thePlane->Dz < 0.0 ) thePlane->myAngle[1] = 270.0 - aRotation;
789   }
790
791   // compute a normal
792   rotationToNormal( thePlane->myAngle, anOrientation, aNormal, aDir );
793
794   double aBounds[6];
795   double anOrigin[3];
796
797   if ( thePlane->PlaneMode == SMESH::Absolute ) {
798     aNormal[0] = thePlane->Dx;
799     aNormal[1] = thePlane->Dy;
800     aNormal[2] = thePlane->Dz;
801   }
802
803   bool anIsOk = false;
804   if( theActorList.empty() ) {
805     // to support planes with empty actor list we should create
806     // a nullified plane that will be initialized later
807     anOrigin[0] = anOrigin[1] = anOrigin[2] = 0;
808     aBounds[0] = aBounds[2] = aBounds[4] = 0;
809     aBounds[1] = aBounds[3] = aBounds[5] = 0;
810     anIsOk = true;
811   }
812   else
813     anIsOk = SMESH::ComputeClippingPlaneParameters( theActorList,
814                                                     aNormal,
815                                                     thePlane->myDistance,
816                                                     aBounds,
817                                                     anOrigin );
818   if( !anIsOk )
819     return false;
820
821   if ( thePlane->PlaneMode == SMESH::Absolute ) {
822     anOrigin[0] = thePlane->X;
823     anOrigin[1] = thePlane->Y;
824     anOrigin[2] = thePlane->Z;
825   }
826   thePlane->SetNormal( aNormal );
827   thePlane->SetOrigin( anOrigin );
828
829
830   double aPnt[3] = { ( aBounds[0] + aBounds[1] ) / 2.,
831                      ( aBounds[2] + aBounds[3] ) / 2.,
832                      ( aBounds[4] + aBounds[5] ) / 2. };
833
834   double aDel = pow( pow( aBounds[1] - aBounds[0], 2 ) +
835                      pow( aBounds[3] - aBounds[2], 2 ) +
836                      pow( aBounds[5] - aBounds[4], 2 ), 0.5 );
837
838   double aDelta[2][3] = { { aDir[0][0]*aDel, aDir[0][1]*aDel, aDir[0][2]*aDel },
839                           { aDir[1][0]*aDel, aDir[1][1]*aDel, aDir[1][2]*aDel } };
840   double aParam, aPnt0[3], aPnt1[3], aPnt2[3];
841
842   double aPnt01[3] = { aPnt[0] - aDelta[0][0] - aDelta[1][0],
843                        aPnt[1] - aDelta[0][1] - aDelta[1][1],
844                        aPnt[2] - aDelta[0][2] - aDelta[1][2] };
845   double aPnt02[3] = { aPnt01[0] + aNormal[0],
846                        aPnt01[1] + aNormal[1],
847                        aPnt01[2] + aNormal[2] };
848   vtkPlane::IntersectWithLine( aPnt01, aPnt02, aNormal, anOrigin, aParam, aPnt0 );
849
850   double aPnt11[3] = { aPnt[0] - aDelta[0][0] + aDelta[1][0],
851                        aPnt[1] - aDelta[0][1] + aDelta[1][1],
852                        aPnt[2] - aDelta[0][2] + aDelta[1][2] };
853   double aPnt12[3] = { aPnt11[0] + aNormal[0],
854                        aPnt11[1] + aNormal[1],
855                        aPnt11[2] + aNormal[2] };
856   vtkPlane::IntersectWithLine( aPnt11, aPnt12, aNormal, anOrigin, aParam, aPnt1);
857
858   double aPnt21[3] = { aPnt[0] + aDelta[0][0] - aDelta[1][0],
859                        aPnt[1] + aDelta[0][1] - aDelta[1][1],
860                        aPnt[2] + aDelta[0][2] - aDelta[1][2] };
861   double aPnt22[3] = { aPnt21[0] + aNormal[0],
862                        aPnt21[1] + aNormal[1],
863                        aPnt21[2] + aNormal[2] };
864   vtkPlane::IntersectWithLine( aPnt21, aPnt22, aNormal, anOrigin, aParam, aPnt2);
865
866   vtkPlaneSource* aPlaneSource = thePlane->myPlaneSource;
867   aPlaneSource->SetNormal( aNormal[0], aNormal[1], aNormal[2] );
868   aPlaneSource->SetOrigin( aPnt0[0], aPnt0[1], aPnt0[2] );
869   aPlaneSource->SetPoint1( aPnt1[0], aPnt1[1], aPnt1[2] );
870   aPlaneSource->SetPoint2( aPnt2[0], aPnt2[1], aPnt2[2] );
871   aPlaneSource->Update();
872
873   SMESH::TActorList::iterator anIter = theActorList.begin();
874   for ( ; anIter != theActorList.end(); anIter++ )
875     if( vtkActor* aVTKActor = *anIter )
876       if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) ) {
877         if( thePlane->IsOpenGLClipping )
878           anActor->AddOpenGLClippingPlane( thePlane->InvertPlane() );
879         else
880           anActor->AddClippingPlane( thePlane );
881       }
882
883   return true;
884 }
885
886 /*!
887   Custom handling of events
888 */
889 void SMESHGUI_ClippingDlg::keyPressEvent( QKeyEvent* e )
890 {
891   QDialog::keyPressEvent( e );
892   if ( e->isAccepted() )
893     return;
894
895   if ( e->key() == Qt::Key_F1 ) {
896     e->accept();
897     ClickOnHelp();
898   }
899 }
900
901 /*!
902   Handles the char preview widget activation event
903 */
904 void SMESHGUI_ClippingDlg::ProcessEvents( vtkObject* theObject,
905                                           unsigned long theEvent,
906                                           void* theClientData,
907                                           void* vtkNotUsed( theCallData ) )
908 {
909   vtkImplicitPlaneWidget* aWidget = vtkImplicitPlaneWidget::SafeDownCast( theObject );
910   if ( aWidget == NULL ) return;
911   if ( theClientData == NULL ) return;
912
913   SMESHGUI_ClippingDlg* aDlg = (SMESHGUI_ClippingDlg*) theClientData;
914
915   double anOrigin[3];
916   double aDir[3];
917
918   switch( theEvent ){
919   case vtkCommand::InteractionEvent:
920     aWidget->GetOrigin( anOrigin );
921     aWidget->GetNormal( aDir );
922
923     aDlg->myIsSelectPlane = true;
924
925     if( aDlg->CurrentMode == SMESH::Absolute ) {
926       aDlg->setOrigin( anOrigin );
927       aDlg->setDirection( aDir );
928       aDlg->myIsPreviewMoved = true;
929     }
930     else if( aDlg->CurrentMode == SMESH::Relative ) {
931       aDlg->absolutePlaneToRelative( anOrigin, aDir );
932     }
933     aDlg->myIsSelectPlane = false;
934
935     aDlg->SetCurrentPlaneParam();
936     aDlg->myIsPreviewMoved = false;
937     break;
938   }
939 }
940
941 /*!
942   Initialize the planes's data when the dialog opened
943 */
944 void SMESHGUI_ClippingDlg::initializePlaneData()
945 {
946   const SMESHGUI_ClippingPlaneInfoMap& aClippingPlaneInfoMap = mySMESHGUI->getClippingPlaneInfoMap();
947   SMESHGUI_ClippingPlaneInfoMap::const_iterator anIter1 = aClippingPlaneInfoMap.find( myViewWindow->getViewManager() );
948   if( anIter1 != aClippingPlaneInfoMap.end() ) {
949     const SMESHGUI_ClippingPlaneInfoList& aClippingPlaneInfoList = anIter1->second;
950     SMESHGUI_ClippingPlaneInfoList::const_iterator anIter2 = aClippingPlaneInfoList.begin();
951     for( ; anIter2 != aClippingPlaneInfoList.end(); anIter2++ ) {
952       const SMESH::ClippingPlaneInfo& aClippingPlaneInfo = *anIter2;
953       SMESH::OrientedPlane* anOrientedPlane = SMESH::OrientedPlane::New(myViewWindow);
954       anOrientedPlane->ShallowCopy(aClippingPlaneInfo.Plane);
955       SMESH::TPlane aTPlane( anOrientedPlane );
956       SMESH::TPlaneData aPlaneData( aTPlane, aClippingPlaneInfo.ActorList );
957       myPlanes.push_back( aPlaneData );
958     }
959   }
960   std::for_each( myPlanes.begin(),myPlanes.end(), TSetVisibility( PreviewCheckBox->isChecked() ) );
961   if( myPlanes.size() )
962     myPreviewWidget->SetEnabled( PreviewCheckBox->isChecked() );
963 }
964
965 /*!
966   Initialization of initial values of widgets
967 */
968 void SMESHGUI_ClippingDlg::initParam()
969 {
970   SpinBox_X->setValue( 0.0 );
971   SpinBox_Y->setValue( 0.0 );
972   SpinBox_Z->setValue( 0.0 );
973
974   SpinBox_Dx->setValue( 1.0 );
975   SpinBox_Dy->setValue( 1.0 );
976   SpinBox_Dz->setValue( 1.0 );
977
978   CBAbsoluteOrientation->setCurrentIndex(0);
979
980   SpinSliderDistance->setValue( 0.5 );
981   SpinSliderRotation1->setValue( 0 );
982   SpinSliderRotation2->setValue( 0 );
983   CBRelativeOrientation->setCurrentIndex( 0 );
984 }
985
986 /*!
987   Synchronize dialog's widgets with data
988 */
989 void SMESHGUI_ClippingDlg::synchronize()
990 {
991   int aNbPlanes = myPlanes.size();
992   ComboBoxPlanes->clear();
993
994   QString aName;
995   for( int i = 1; i<=aNbPlanes; i++ ) {
996     aName = QString( tr( "PLANE_NUM" ) ).arg(i);
997     ComboBoxPlanes->addItem( aName );
998   }
999
1000   int aPos = ComboBoxPlanes->count() - 1;
1001   ComboBoxPlanes->setCurrentIndex( aPos );
1002
1003   bool anIsControlsEnable = ( aPos >= 0 );
1004   if ( anIsControlsEnable ) {
1005     onSelectPlane( aPos );
1006     updateActorList();
1007     if( PreviewCheckBox->isChecked() )
1008       myPreviewWidget->On();
1009   }
1010   else {
1011     ComboBoxPlanes->addItem( tr( "NO_PLANES" ) );
1012     ActorList->clear();
1013     initParam();
1014     if( PreviewCheckBox->isChecked() )
1015       myPreviewWidget->Off();
1016   }
1017
1018   isOpenGLClipping->setEnabled( anIsControlsEnable );
1019   ActorList->setEnabled( anIsControlsEnable );
1020   SelectAllCheckBox->setEnabled( anIsControlsEnable );
1021   buttonDelete->setEnabled( anIsControlsEnable );
1022   if ( CurrentMode == SMESH::Absolute ) {
1023     SpinBox_X->setEnabled( anIsControlsEnable );
1024     SpinBox_Y->setEnabled( anIsControlsEnable );
1025     SpinBox_Z->setEnabled( anIsControlsEnable );
1026     SpinBox_Dx->setEnabled( anIsControlsEnable );
1027     SpinBox_Dy->setEnabled( anIsControlsEnable );
1028     SpinBox_Dz->setEnabled( anIsControlsEnable );
1029     CBAbsoluteOrientation->setEnabled( anIsControlsEnable );
1030     invertButton->setEnabled( anIsControlsEnable );
1031     resetButton->setEnabled( anIsControlsEnable );
1032   }
1033   else if ( CurrentMode == SMESH::Relative ) {
1034     CBRelativeOrientation->setEnabled( anIsControlsEnable );
1035     SpinSliderDistance->setEnabled( anIsControlsEnable );
1036     SpinSliderRotation1->setEnabled( anIsControlsEnable );
1037     SpinSliderRotation2->setEnabled( anIsControlsEnable );
1038   }
1039 }
1040
1041 /*!
1042   Update the list of actors
1043 */
1044 void SMESHGUI_ClippingDlg::updateActorList()
1045 {
1046   ActorList->clear();
1047
1048   SalomeApp_Study* anAppStudy = SMESHGUI::activeStudy();
1049   if( !anAppStudy )
1050     return;
1051
1052   _PTR(Study) aStudy = anAppStudy->studyDS();
1053   if( !aStudy )
1054     return;
1055
1056   if( !myViewWindow )
1057     return;
1058
1059   int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
1060   const SMESH::TPlaneData& aPlaneData = myPlanes[ aCurPlaneIndex ];
1061   const SMESH::TActorList& anActorList = aPlaneData.ActorList;
1062
1063   std::for_each( myPlanes.begin(),myPlanes.end(), TSetVisibility( PreviewCheckBox->isChecked() ) );
1064   aPlaneData.Plane.GetPointer()->myActor->SetVisibility( false );
1065
1066   VTK::ActorCollectionCopy aCopy( myViewWindow->getRenderer()->GetActors() );
1067   vtkActorCollection* anAllActors = aCopy.GetActors();
1068   anAllActors->InitTraversal();
1069   while( vtkActor* aVTKActor = anAllActors->GetNextActor() ) {
1070     if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) ) {
1071       if( anActor->hasIO() ) {
1072         Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
1073         if( _PTR(SObject) aSObj = aStudy->FindObjectID( anIO->getEntry() ) ) {
1074           bool anIsChecked = false;
1075           SMESH::TActorList::const_iterator anIter = anActorList.begin();
1076           for ( ; anIter != anActorList.end(); anIter++ ) {
1077             if( vtkActor* aVTKActorRef = *anIter ) {
1078               if( SMESH_Actor* anActorRef = SMESH_Actor::SafeDownCast( aVTKActorRef ) ) {
1079                 if( anActorRef == anActor ) {
1080                   anIsChecked = true;
1081                   break;
1082                 }
1083               }
1084             }
1085           }
1086           QString aName = QString( aSObj->GetName().c_str() );
1087           QListWidgetItem* anItem = new ActorItem( anActor, aName, ActorList );
1088           anItem->setCheckState( anIsChecked ? Qt::Checked : Qt::Unchecked );
1089           updateActorItem( anItem, true, false );
1090         }
1091       }
1092     }
1093   }
1094 }
1095
1096 /*!
1097   Update an actor in actor's list
1098 */
1099 void SMESHGUI_ClippingDlg::updateActorItem( QListWidgetItem* theItem,
1100                                             bool theUpdateSelectAll,
1101                                             bool theUpdateClippingPlaneMap )
1102 {
1103   // update Select All check box
1104   if( theUpdateSelectAll ) {
1105     int aNbItems = ActorList->count(), aNbChecked = 0;
1106     for( int i = 0; i < aNbItems; i++ )
1107       if( QListWidgetItem* anItem = ActorList->item( i ) )
1108         if( anItem->checkState() == Qt::Checked )
1109           aNbChecked++;
1110
1111     bool anIsBlocked = SelectAllCheckBox->blockSignals( true );
1112     SelectAllCheckBox->setCheckState( aNbChecked == aNbItems ? Qt::Checked : Qt::Unchecked);
1113     SelectAllCheckBox->blockSignals( anIsBlocked );
1114   }
1115
1116   // update clipping plane map
1117   if( theUpdateClippingPlaneMap ) {
1118     int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
1119     if( ActorItem* anItem = dynamic_cast<ActorItem*>( theItem ) ) {
1120       if( SMESH_Actor* anActor = anItem->getActor() ) {
1121         SMESH::TPlaneData& aPlaneData = myPlanes[ aCurPlaneIndex ];
1122         SMESH::TActorList& anActorList = aPlaneData.ActorList;
1123         bool anIsPushed = false;
1124         SMESH::TActorList::iterator anIter = anActorList.begin();
1125         for ( ; anIter != anActorList.end(); anIter++ ) {
1126           if( anActor == *anIter ) {
1127             anIsPushed = true;
1128             break;
1129           }
1130         }
1131         if( theItem->checkState() == Qt::Checked && !anIsPushed )
1132           anActorList.push_back( anActor );
1133         else if( theItem->checkState() == Qt::Unchecked && anIsPushed )
1134           anActorList.remove( anActor );
1135
1136         if( SMESH::ComputeBounds( anActorList, myBounds ) ) {
1137           myPreviewWidget->PlaceWidget( myBounds[0], myBounds[1], myBounds[2],
1138                                         myBounds[3], myBounds[4], myBounds[5] );
1139           if( PreviewCheckBox->isChecked() )
1140             myPreviewWidget->On();
1141         }
1142         else
1143           myPreviewWidget->Off();
1144       }
1145     }
1146   }
1147 }
1148
1149 /*!
1150   Get the list of current actors
1151 */
1152 SMESH::TActorList SMESHGUI_ClippingDlg::getCurrentActors()
1153 {
1154   SMESH::TActorList anActorList;
1155   for( int i = 0, n = ActorList->count(); i < n; i++ )
1156     if( ActorItem* anItem = dynamic_cast<ActorItem*>( ActorList->item( i ) ) )
1157       if( anItem->checkState() == Qt::Checked )
1158         if( SMESH_Actor* anActor = anItem->getActor() )
1159           anActorList.push_back( anActor );
1160   return anActorList;
1161 }
1162
1163 /*!
1164   Dump the parameters of clipping planes
1165 */
1166 void SMESHGUI_ClippingDlg::dumpPlaneData() const
1167 {
1168   printf( "----------- Plane Data -----------\n" );
1169   int anId = 1;
1170   SMESH::TPlaneDataVector::const_iterator anIter1 = myPlanes.begin();
1171   for ( ; anIter1 != myPlanes.end(); anIter1++, anId++ ) {
1172     SMESH::TPlaneData aPlaneData = *anIter1;
1173     SMESH::TPlane aPlane = aPlaneData.Plane;
1174     double* aNormal = aPlane->GetNormal();
1175     double* anOrigin = aPlane->GetOrigin();
1176     printf( "Plane N%d:\n", anId );
1177     printf( "  Normal = ( %f, %f, %f )\n", aNormal[0], aNormal[1], aNormal[2] );
1178     printf( "  Origin = ( %f, %f, %f )\n", anOrigin[0], anOrigin[1], anOrigin[2] );
1179
1180     SMESH::TActorList anActorList = aPlaneData.ActorList;
1181     SMESH::TActorList::const_iterator anIter2 = anActorList.begin();
1182     for ( ; anIter2 != anActorList.end(); anIter2++ ) {
1183       if( vtkActor* aVTKActor = *anIter2 ) {
1184         if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) )
1185           printf( "  - Actor: '%s'\n", anActor->getName() );
1186       }
1187       else
1188         printf( "  - Actor: NULL\n");
1189     }
1190   }
1191   printf( "----------------------------------\n" );
1192 }
1193
1194 /*!
1195   SLOT on close button click: rejects dialog
1196 */
1197 void SMESHGUI_ClippingDlg::reject()
1198 {
1199   //here we can insert actions to do at close.
1200   QDialog::reject();
1201 }
1202
1203 /*!
1204   Set absolute mode of clipping plane
1205 */
1206 void SMESHGUI_ClippingDlg::onModeAbsolute()
1207 {
1208   ModeStackedLayout->setCurrentIndex(0);
1209   CurrentMode = SMESH::Absolute;
1210   ClickOnNew();
1211   SetCurrentPlaneParam();
1212 }
1213
1214 /*!
1215   Set relative mode of clipping plane
1216 */
1217 void SMESHGUI_ClippingDlg::onModeRelative()
1218 {
1219   ModeStackedLayout->setCurrentIndex(1);
1220   CurrentMode = SMESH::Relative;
1221   ClickOnNew();
1222   SetCurrentPlaneParam();
1223 }
1224
1225 /*!
1226   SLOT on new button click: create a new clipping plane
1227 */
1228 void SMESHGUI_ClippingDlg::ClickOnNew()
1229 {
1230   if(myViewWindow) {
1231     SMESH::OrientedPlane* aPlane = SMESH::OrientedPlane::New(myViewWindow);
1232     SMESH::TPlane aTPlane(aPlane);
1233     aPlane->PlaneMode = CurrentMode;
1234     SMESH::TActorList anActorList;
1235     VTK::ActorCollectionCopy aCopy( myViewWindow->getRenderer()->GetActors() );
1236     vtkActorCollection* anAllActors = aCopy.GetActors();
1237     anAllActors->InitTraversal();
1238     while( vtkActor* aVTKActor = anAllActors->GetNextActor() )
1239       if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) )
1240         anActorList.push_back( anActor );
1241
1242     SMESH::TPlaneData aPlaneData(aTPlane, anActorList);
1243
1244     myPlanes.push_back(aPlaneData);
1245
1246
1247     std::for_each( myPlanes.begin(),myPlanes.end(), TSetVisibility( PreviewCheckBox->isChecked() ) );
1248     aPlane->myActor->SetVisibility( false );
1249
1250     bool anIsBlocked = ActorList->blockSignals( true );
1251
1252     if( SMESH::ComputeBounds( anActorList, myBounds ) ) {
1253       myPreviewWidget->PlaceWidget( myBounds[0], myBounds[1], myBounds[2],
1254                                     myBounds[3], myBounds[4], myBounds[5] );
1255       if( PreviewCheckBox->isChecked() )
1256         myPreviewWidget->On();
1257     }
1258     else
1259       myPreviewWidget->Off();
1260
1261     synchronize();
1262     SetCurrentPlaneParam();
1263
1264     ActorList->blockSignals( anIsBlocked );
1265   }
1266 }
1267
1268 /*!
1269   SLOT on delete button click: Delete selected clipping plane
1270 */
1271 void SMESHGUI_ClippingDlg::ClickOnDelete()
1272 {
1273   if (myPlanes.empty())
1274     return;
1275
1276   int aPlaneIndex = ComboBoxPlanes->currentIndex();
1277
1278   SMESH::TPlaneDataVector::iterator anIter = myPlanes.begin() + aPlaneIndex;
1279   SMESH::TPlaneData aPlaneData = *anIter;
1280   aPlaneData.Plane.GetPointer()->myActor->SetVisibility(false);
1281   myPlanes.erase(anIter);
1282
1283   if(AutoApplyCheckBox->isChecked())
1284     ClickOnApply();
1285
1286   synchronize();
1287   SMESH::RenderViewWindow( myViewWindow );
1288 }
1289
1290 /*!
1291   Set current parameters of selected plane
1292 */
1293 void SMESHGUI_ClippingDlg::onSelectPlane ( int theIndex )
1294 {
1295   if ( myPlanes.empty() )
1296     return;
1297
1298   SMESH::TPlaneData aPlaneData = myPlanes[theIndex];
1299   SMESH::OrientedPlane* aPlane = aPlaneData.Plane.GetPointer();
1300
1301   myIsSelectPlane = true;
1302   isOpenGLClipping->setChecked( aPlane->IsOpenGLClipping );
1303
1304   if ( aPlane->PlaneMode == SMESH::Absolute ) {
1305     ModeStackedLayout->setCurrentIndex( 0 );
1306     CurrentMode = SMESH::Absolute;
1307     int anOrientation = aPlane->myAbsoluteOrientation;
1308     // Set plane parameters in the dialog
1309     double anOrigin[3], aDir[3];
1310     anOrigin[0] = aPlane->X;
1311     anOrigin[1] = aPlane->Y;
1312     anOrigin[2] = aPlane->Z;
1313     setOrigin( anOrigin );
1314     aDir[0] = aPlane->Dx;
1315     aDir[1] = aPlane->Dy;
1316     aDir[2] = aPlane->Dz;
1317     setDirection( aDir );
1318     CBAbsoluteOrientation->setCurrentIndex( anOrientation );
1319     onSelectAbsoluteOrientation( anOrientation );
1320   }
1321   else if ( aPlane->PlaneMode == SMESH::Relative ) {
1322     ModeStackedLayout->setCurrentIndex( 1 );
1323     CurrentMode = SMESH::Relative;
1324     SMESH::Orientation anOrientation = aPlane->GetOrientation();
1325     double aRot[2] = { aPlane->myAngle[0], aPlane->myAngle[1] };
1326     // Set plane parameters in the dialog
1327     setDistance( aPlane->GetDistance() );
1328     setRotation( aRot[0], aRot[1] );
1329     switch ( anOrientation ) {
1330     case SMESH::XY:
1331       CBRelativeOrientation->setCurrentIndex(0);
1332       onSelectRelativeOrientation(0);
1333       break;
1334     case SMESH::YZ:
1335       CBRelativeOrientation->setCurrentIndex(1);
1336       onSelectRelativeOrientation(1);
1337       break;
1338     case SMESH::ZX:
1339       CBRelativeOrientation->setCurrentIndex(2);
1340       onSelectRelativeOrientation(2);
1341       break;
1342     }
1343   }
1344   myIsSelectPlane = false;
1345
1346   if( SMESH::ComputeBounds( aPlaneData.ActorList, myBounds ) ) {
1347     myPreviewWidget->PlaceWidget( myBounds[0], myBounds[1], myBounds[2],
1348                                   myBounds[3], myBounds[4], myBounds[5] );
1349     if( PreviewCheckBox->isChecked() )
1350       myPreviewWidget->On();
1351   }
1352   else
1353     myPreviewWidget->Off();
1354
1355   SetCurrentPlaneParam();
1356
1357   // Actors
1358   bool anIsBlocked = ActorList->blockSignals( true );
1359   updateActorList();
1360   ActorList->blockSignals( anIsBlocked );
1361 }
1362
1363 /*!
1364   SLOT: called on OpenGLClipping check box toggled
1365 */
1366 void SMESHGUI_ClippingDlg::onIsOpenGLClipping( bool toggled )
1367 {
1368   if ( myPlanes.empty() || myIsSelectPlane )
1369     return;
1370
1371   int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
1372   SMESH::TPlaneData aPlane = myPlanes[aCurPlaneIndex];
1373   SMESH::OrientedPlane* aPlaneData = aPlane.Plane.GetPointer();
1374   aPlaneData->IsOpenGLClipping = toggled;
1375
1376   if( AutoApplyCheckBox->isChecked() )
1377     ClickOnApply();
1378 }
1379
1380 /*!
1381   SLOT: called on SelectAll check box toggled
1382 */
1383 void SMESHGUI_ClippingDlg::onSelectAll( int theState )
1384 {
1385   if( theState == Qt::PartiallyChecked ) {
1386     SelectAllCheckBox->setCheckState( Qt::Checked );
1387     return;
1388   }
1389
1390   bool anIsBlocked = ActorList->blockSignals( true );
1391   for( int i = 0, n = ActorList->count(); i < n; i++ ) {
1392     if( QListWidgetItem* anItem = ActorList->item( i ) ) {
1393       anItem->setCheckState( theState == Qt::Checked ? Qt::Checked : Qt::Unchecked );
1394       updateActorItem( anItem, false, true );
1395     }
1396   }
1397   SelectAllCheckBox->setTristate( false );
1398   ActorList->blockSignals( anIsBlocked );
1399   SetCurrentPlaneParam();
1400 }
1401
1402 /*!
1403   SLOT: called when actor item was changed
1404 */
1405 void SMESHGUI_ClippingDlg::onActorItemChanged( QListWidgetItem* theItem )
1406 {
1407   updateActorItem( theItem, true, true );
1408   SetCurrentPlaneParam();
1409 }
1410
1411 /*!
1412   Restore parameters of selected plane
1413 */
1414 void SMESHGUI_ClippingDlg::SetCurrentPlaneParam()
1415 {
1416   if ( myPlanes.empty() || myIsSelectPlane )
1417     return;
1418
1419   int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
1420
1421   SMESH::TPlaneData aPlaneData = myPlanes[aCurPlaneIndex];
1422   SMESH::OrientedPlane* aPlane = aPlaneData.Plane.GetPointer();
1423
1424   if ( aPlane->PlaneMode == SMESH::Absolute ) {
1425     aPlane->myAbsoluteOrientation = CBAbsoluteOrientation->currentIndex();
1426     aPlane->X = SpinBox_X->value();
1427     aPlane->Y = SpinBox_Y->value();
1428     aPlane->Z = SpinBox_Z->value();
1429     aPlane->Dx = SpinBox_Dx->value();
1430     aPlane->Dy = SpinBox_Dy->value();
1431     aPlane->Dz = SpinBox_Dz->value();
1432   }
1433
1434   double aNormal[3];
1435   double aDir[2][3] = { {0, 0, 0}, {0, 0, 0} };
1436   static double aCoeff = vtkMath::Pi()/180.0;
1437
1438   double aRot[2] = { getRotation1(), getRotation2() };
1439   int anOrient;
1440   if ( aPlane->PlaneMode == SMESH::Absolute )
1441     anOrient = CBAbsoluteOrientation->currentIndex();
1442   else if ( aPlane->PlaneMode == SMESH::Relative )
1443     anOrient = CBRelativeOrientation->currentIndex() + 1;
1444
1445   if ( aPlane->PlaneMode == SMESH::Relative ) {
1446     aPlane->myAngle[0] = aRot[0];
1447     aPlane->myAngle[1] = aRot[1];
1448     aPlane->SetOrientation( SMESH::Orientation( CBRelativeOrientation->currentIndex() ) );
1449     aPlane->SetDistance( getDistance() );
1450   }
1451
1452   if ( anOrient == 0 ) {
1453     // compute a direction for plane in absolute mode
1454     double znam = sqrt( aPlane->Dx*aPlane->Dx + aPlane->Dy*aPlane->Dy + aPlane->Dz*aPlane->Dz );
1455     double aRotation = acos( aPlane->Dy/znam )/aCoeff;
1456     if ( aPlane->Dy >= 0.0 && aPlane->Dz >= 0.0 )     aRot[0] = 90.0 + aRotation;
1457     else if ( aPlane->Dy >= 0.0 && aPlane->Dz < 0.0 ) aRot[0] = 90.0 - aRotation;
1458     else if ( aPlane->Dy < 0.0 && aPlane->Dz >= 0.0 ) aRot[0] = aRotation - 90.0;
1459     else if ( aPlane->Dy < 0.0 && aPlane->Dz < 0.0 )  aRot[0] = 270.0 - aRotation;
1460
1461     aRotation = acos( aPlane->Dx/znam )/aCoeff;
1462     if ( aPlane->Dx >= 0.0 && aPlane->Dz >= 0.0 )     aRot[1] = 90.0 + aRotation;
1463     else if ( aPlane->Dx >= 0.0 && aPlane->Dz < 0.0 ) aRot[1] = 90.0 - aRotation;
1464     else if ( aPlane->Dx < 0.0 && aPlane->Dz >= 0.0 ) aRot[1] = aRotation - 90.0;
1465     else if ( aPlane->Dx < 0.0 && aPlane->Dz < 0.0 )  aRot[1] = 270.0 - aRotation;
1466   }
1467
1468   // compute a normal
1469   rotationToNormal( aRot, anOrient, aNormal, aDir );
1470
1471
1472   SMESH::TActorList anActorList = aPlaneData.ActorList;
1473
1474   double aBounds[6];
1475   double anOrigin[3];
1476   double aDistance;
1477   aDistance = getDistance();
1478   if ( aPlane->PlaneMode == SMESH::Absolute ) {
1479     aNormal[0] = aPlane->Dx;
1480     aNormal[1] = aPlane->Dy;
1481     aNormal[2] = aPlane->Dz;
1482   }
1483
1484   bool anIsOk = SMESH::ComputeClippingPlaneParameters( anActorList,
1485                                                        aNormal,
1486                                                        aDistance,
1487                                                        aBounds,
1488                                                        anOrigin );
1489
1490   if ( aPlane->PlaneMode == SMESH::Absolute ) {
1491     anOrigin[0] = aPlane->X;
1492     anOrigin[1] = aPlane->Y;
1493     anOrigin[2] = aPlane->Z;
1494   }
1495
1496   if( anIsOk ) {
1497     aPlane->SetNormal( aNormal );
1498     aPlane->SetOrigin( anOrigin );
1499
1500     double aPnt[3] = { ( aBounds[0] + aBounds[1] ) / 2.,
1501                        ( aBounds[2] + aBounds[3] ) / 2.,
1502                        ( aBounds[4] + aBounds[5] ) / 2. };
1503
1504     double aDel = pow( pow( aBounds[1] - aBounds[0], 2 ) +
1505                        pow( aBounds[3] - aBounds[2], 2 ) +
1506                        pow( aBounds[5] - aBounds[4], 2 ), 0.5 );
1507
1508     double aDelta[2][3] = { { aDir[0][0]*aDel, aDir[0][1]*aDel, aDir[0][2]*aDel },
1509                             { aDir[1][0]*aDel, aDir[1][1]*aDel, aDir[1][2]*aDel } };
1510     double aParam, aPnt0[3], aPnt1[3], aPnt2[3];
1511
1512     double aPnt01[3] = { aPnt[0] - aDelta[0][0] - aDelta[1][0],
1513                          aPnt[1] - aDelta[0][1] - aDelta[1][1],
1514                          aPnt[2] - aDelta[0][2] - aDelta[1][2] };
1515     double aPnt02[3] = { aPnt01[0] + aNormal[0],
1516                          aPnt01[1] + aNormal[1],
1517                          aPnt01[2] + aNormal[2] };
1518     vtkPlane::IntersectWithLine(aPnt01,aPnt02,aNormal,anOrigin,aParam,aPnt0);
1519
1520     double aPnt11[3] = { aPnt[0] - aDelta[0][0] + aDelta[1][0],
1521                          aPnt[1] - aDelta[0][1] + aDelta[1][1],
1522                          aPnt[2] - aDelta[0][2] + aDelta[1][2] };
1523     double aPnt12[3] = { aPnt11[0] + aNormal[0],
1524                          aPnt11[1] + aNormal[1],
1525                          aPnt11[2] + aNormal[2] };
1526     vtkPlane::IntersectWithLine(aPnt11,aPnt12,aNormal,anOrigin,aParam,aPnt1);
1527
1528     double aPnt21[3] = { aPnt[0] + aDelta[0][0] - aDelta[1][0],
1529                          aPnt[1] + aDelta[0][1] - aDelta[1][1],
1530                          aPnt[2] + aDelta[0][2] - aDelta[1][2] };
1531     double aPnt22[3] = { aPnt21[0] + aNormal[0],
1532                          aPnt21[1] + aNormal[1],
1533                          aPnt21[2] + aNormal[2] };
1534     vtkPlane::IntersectWithLine(aPnt21,aPnt22,aNormal,anOrigin,aParam,aPnt2);
1535
1536     vtkPlaneSource* aPlaneSource = aPlane->myPlaneSource;
1537
1538     aPlaneSource->SetNormal( aNormal[0], aNormal[1], aNormal[2] );
1539     aPlaneSource->SetOrigin( aPnt0[0], aPnt0[1], aPnt0[2] );
1540     aPlaneSource->SetPoint1( aPnt1[0], aPnt1[1], aPnt1[2] );
1541     aPlaneSource->SetPoint2( aPnt2[0], aPnt2[1], aPnt2[2] );
1542     aPlaneSource->Update();
1543   }
1544
1545   setBoundsForPreviewWidget();
1546
1547   myPreviewWidget->SetOrigin( anOrigin );
1548   myPreviewWidget->SetNormal( aNormal );
1549
1550   if(AutoApplyCheckBox->isChecked())
1551     ClickOnApply();
1552
1553   SMESH::RenderViewWindow( myViewWindow );
1554 }
1555
1556 /*!
1557   Set current bounds for preview widget
1558 */
1559 void SMESHGUI_ClippingDlg::setBoundsForPreviewWidget()
1560 {
1561   int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
1562   SMESH::TPlaneData aPlaneData = myPlanes[aCurPlaneIndex];
1563   SMESH::OrientedPlane* aPlane = aPlaneData.Plane.GetPointer();
1564   SMESH::TActorList anActorList = aPlaneData.ActorList;
1565
1566   double* anOrigin = aPlane->GetOrigin();
1567
1568   double aBounds[6];
1569   SMESH::ComputeBounds( anActorList, aBounds );
1570
1571   bool isBoundsChanged = false;
1572
1573   if( myIsPreviewMoved ) {
1574     // if widget has moved by hand the bounds can to minimize
1575     if( anOrigin[0] > myBounds[0] && anOrigin[0] < aBounds[0] ) {
1576       myBounds[0] = anOrigin[0]; isBoundsChanged = true; }
1577     if( anOrigin[0] < myBounds[1] && anOrigin[0] > aBounds[1] ) {
1578       myBounds[1] = anOrigin[0]; isBoundsChanged = true; }
1579     if( anOrigin[1] > myBounds[2] && anOrigin[1] < aBounds[2] ) {
1580       myBounds[2] = anOrigin[1]; isBoundsChanged = true; }
1581     if( anOrigin[1] < myBounds[3] && anOrigin[1] > aBounds[3] ) {
1582       myBounds[3] = anOrigin[1]; isBoundsChanged = true; }
1583     if( anOrigin[2] > myBounds[4] && anOrigin[2] < aBounds[4] ) {
1584       myBounds[4] = anOrigin[2]; isBoundsChanged = true; }
1585     if( anOrigin[2] < myBounds[5] && anOrigin[2] > aBounds[5] ) {
1586       myBounds[5] = anOrigin[2]; isBoundsChanged = true; }
1587   }
1588   else {
1589     // if widget has moved by dialog data the bounds can to take necessary size
1590     if( anOrigin[0] < aBounds[0] ) {
1591       myBounds[0] = anOrigin[0]; isBoundsChanged = true; }
1592     if( anOrigin[0] > aBounds[1] ) {
1593       myBounds[1] = anOrigin[0]; isBoundsChanged = true; }
1594     if( anOrigin[1] < aBounds[2] ) {
1595       myBounds[2] = anOrigin[1]; isBoundsChanged = true; }
1596     if( anOrigin[1] > aBounds[3] ) {
1597       myBounds[3] = anOrigin[1]; isBoundsChanged = true; }
1598     if( anOrigin[2] < aBounds[4] ) {
1599       myBounds[4] = anOrigin[2]; isBoundsChanged = true; }
1600     if( anOrigin[2] > aBounds[5] ) {
1601         myBounds[5] = anOrigin[2]; isBoundsChanged = true; }
1602   }
1603
1604   if( isBoundsChanged )
1605     myPreviewWidget->PlaceWidget( myBounds[0],myBounds[1],myBounds[2],
1606                                   myBounds[3],myBounds[4],myBounds[5] );
1607   else if( !myIsPreviewMoved )
1608     myPreviewWidget->PlaceWidget( aBounds[0],aBounds[1],aBounds[2],
1609                                   aBounds[3],aBounds[4],aBounds[5] );
1610
1611 }
1612
1613 /*!
1614   Convert absolute coordinates of plane to relative mode
1615 */
1616 void SMESHGUI_ClippingDlg::absolutePlaneToRelative ( double theOrigin[3], double theDir[3] )
1617 {
1618   double aRot[2];
1619
1620   aRot[0] = getRotation1();
1621   aRot[1] = getRotation2();
1622
1623   double eps = 0.0001;
1624
1625   int anOrientation = CBRelativeOrientation->currentIndex();
1626   double aDirection[3];
1627   double aRotation1, aRotation2;
1628   switch( anOrientation ) {
1629   case 0:
1630     aDirection[0] = theDir[0] + eps;
1631     aDirection[1] = theDir[1] + eps;
1632     aDirection[2] = theDir[2] + eps;
1633     aRotation1 = atan2( theDir[2], theDir[1] )*180.0/M_PI;
1634     aRotation2 = atan2( theDir[2], theDir[0] )*180.0/M_PI;
1635     break;
1636   case 1:
1637     aDirection[0] = theDir[1] + eps;
1638     aDirection[1] = theDir[2] + eps;
1639     aDirection[2] = theDir[0] + eps;
1640     aRotation1 = atan2( theDir[0], theDir[2] )*180.0/M_PI;
1641     aRotation2 = atan2( theDir[0], theDir[1] )*180.0/M_PI;
1642     break;
1643   case 2:
1644     aDirection[0] = theDir[2] + eps;
1645     aDirection[1] = theDir[0] + eps;
1646     aDirection[2] = theDir[1] + eps;
1647     aRotation1 = atan2( theDir[1], theDir[0] )*180.0/M_PI;
1648     aRotation2 = atan2( theDir[1], theDir[2] )*180.0/M_PI;
1649     break;
1650   }
1651
1652   if( aDirection[0] > 0 && aDirection[1] > 0 && aDirection[2] > 0 && aRot[0] <= 0 ) {
1653     aRot[0] = aRotation1 - 90.0;  aRot[1] = aRotation2 - 90.0; }
1654   else if( aDirection[0] > 0 && aDirection[1] > 0 && aDirection[2] > 0 && aRot[0] > 0 ) {
1655     aRot[0] = aRotation1 + 90.0;  aRot[1] = aRotation2 + 90.0; }
1656   else if( aDirection[0] > 0 && aDirection[1] > 0 && aDirection[2] < 0 && aRot[0] <= 0 ) {
1657     aRot[0] = aRotation1 - 90.0;  aRot[1] = aRotation2 + 90.0; }
1658   else if( aDirection[0] > 0 && aDirection[1] > 0 && aDirection[2] < 0 && aRot[0] > 0 ) {
1659     aRot[0] = aRotation1 + 90.0;  aRot[1] = aRotation2 - 90.0; }
1660   else if( aDirection[0] > 0 && aDirection[1] < 0 && aDirection[2] > 0 && aRot[0] <= 0 ) {
1661     aRot[0] = aRotation1 - 270.0; aRot[1] = aRotation2 + 90.0; }
1662   else if( aDirection[0] > 0 && aDirection[1] < 0 && aDirection[2] > 0 && aRot[0] > 0 ) {
1663     aRot[0] = aRotation1 - 90.0;  aRot[1] = aRotation2 - 90.0; }
1664   else if( aDirection[0] > 0 && aDirection[1] < 0 && aDirection[2] < 0 && aRot[0] <= 0 ) {
1665     aRot[0] = aRotation1 + 90.0;  aRot[1] = aRotation2 - 90.0; }
1666   else if( aDirection[0] > 0 && aDirection[1] < 0 && aDirection[2] < 0 && aRot[0] > 0 ) {
1667     aRot[0] = aRotation1 + 270.0; aRot[1] = aRotation2 + 90.0; }
1668   else if( aDirection[0] < 0 && aDirection[1] > 0 && aDirection[2] > 0 && aRot[0] <= 0 ) {
1669     aRot[0] = aRotation1 - 90.0;  aRot[1] = aRotation2 - 90.0; }
1670   else if( aDirection[0] < 0 && aDirection[1] > 0 && aDirection[2] > 0 && aRot[0] > 0 ) {
1671     aRot[0] = aRotation1 + 90.0;  aRot[1] = aRotation2 - 270.0; }
1672   else if( aDirection[0] < 0 && aDirection[1] > 0 && aDirection[2] < 0 && aRot[0] <= 0 ) {
1673     aRot[0] = aRotation1 - 90.0;  aRot[1] = aRotation2 + 90.0; }
1674   else if( aDirection[0] < 0 && aDirection[1] > 0 && aDirection[2] < 0 && aRot[0] > 0 ) {
1675     aRot[0] = aRotation1 + 90.0;  aRot[1] = aRotation2 + 270.0; }
1676   else if( aDirection[0] < 0 && aDirection[1] < 0 && aDirection[2] > 0 && aRot[0] <= 0  ) {
1677     aRot[0] = aRotation1 - 270.0; aRot[1] = aRotation2 - 270.0; }
1678   else if( aDirection[0] < 0 && aDirection[1] < 0 && aDirection[2] > 0 && aRot[0] > 0 ) {
1679     aRot[0] = aRotation1 - 90.0;  aRot[1] = aRotation2 - 90.0; }
1680   else if( aDirection[0] < 0 && aDirection[1] < 0 && aDirection[2] < 0 && aRot[0] <= 0  ) {
1681     aRot[0] = aRotation1 + 90.0;  aRot[1] = aRotation2 + 270.0; }
1682   else if( aDirection[0] < 0 && aDirection[1] < 0 && aDirection[2] < 0 && aRot[0] > 0 ) {
1683     aRot[0] = aRotation1 + 270.0; aRot[1] = aRotation2 + 90.0; }
1684
1685   SpinSliderRotation1->setValue( qRound( aRot[0] ) );
1686   SpinSliderRotation2->setValue( qRound( aRot[1] ) );
1687
1688   int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
1689   const SMESH::TPlaneData& aPlaneData = myPlanes[ aCurPlaneIndex ];
1690   const SMESH::TActorList& anActorList = aPlaneData.ActorList;
1691
1692   double aBounds[6];
1693   double aDist;
1694   SMESH::ComputeBounds( anActorList, aBounds );
1695   SMESH::PositionToDistance( aBounds, theDir, theOrigin, aDist );
1696   aDist = 1.0 - aDist;
1697   if( aDist>1.0 )
1698     aDist = 1.0;
1699   else if( aDist < 0.0 )
1700     aDist = 0.0;
1701
1702   SpinSliderDistance->setValue( qRound( aDist*100 ) );
1703   return;
1704 }
1705
1706 /*!
1707   SLOT: called on preview check box toggled
1708 */
1709 void SMESHGUI_ClippingDlg::OnPreviewToggle (bool theIsToggled)
1710 {
1711   std::for_each( myPlanes.begin(), myPlanes.end(), TSetVisibility( theIsToggled ) );
1712   int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
1713   SMESH::TPlaneData aPlane = myPlanes[aCurPlaneIndex];
1714   SMESH::OrientedPlane* aPlaneData = aPlane.Plane.GetPointer();
1715   aPlaneData->myActor->SetVisibility( false );
1716   if( theIsToggled )
1717     myPreviewWidget->On();
1718   else
1719     myPreviewWidget->Off();
1720   SMESH::RenderViewWindow( myViewWindow );
1721 }
1722
1723 /*!
1724   SLOT: called on Auto Apply check box toggled
1725 */
1726 void SMESHGUI_ClippingDlg::onAutoApply(bool toggled)
1727 {
1728   if ( toggled ) ClickOnApply();
1729 }
1730
1731 /*!
1732   SLOT on ok button click: sets cutting plane and closes dialog
1733 */
1734 void SMESHGUI_ClippingDlg::ClickOnOk()
1735 {
1736   ClickOnApply();
1737   accept();
1738 }
1739
1740
1741 /*!
1742   SLOT on Apply button click: sets cutting plane and update viewer
1743 */
1744 void SMESHGUI_ClippingDlg::ClickOnApply()
1745 {
1746   if (myViewWindow) {
1747     SUIT_OverrideCursor wc;
1748
1749     QWidget *aCurrWid = this->focusWidget();
1750     aCurrWid->clearFocus();
1751     aCurrWid->setFocus();
1752
1753     SMESHGUI_ClippingPlaneInfoMap& aClippingPlaneInfoMap = mySMESHGUI->getClippingPlaneInfoMap();
1754     SMESHGUI_ClippingPlaneInfoList& aClippingPlaneInfoList = aClippingPlaneInfoMap[ myViewWindow->getViewManager() ];
1755
1756     // clean memory allocated for planes
1757     SMESHGUI_ClippingPlaneInfoList::iterator anIter1 = aClippingPlaneInfoList.begin();
1758     for( ; anIter1 != aClippingPlaneInfoList.end(); anIter1++ )
1759       if( SMESH::OrientedPlane* aPlane = (*anIter1).Plane )
1760         aPlane->Delete();
1761
1762     aClippingPlaneInfoList.clear();
1763
1764     VTK::ActorCollectionCopy aCopy( myViewWindow->getRenderer()->GetActors() );
1765     vtkActorCollection* anAllActors = aCopy.GetActors();
1766     anAllActors->InitTraversal();
1767     while( vtkActor* aVTKActor = anAllActors->GetNextActor() )
1768       if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) )
1769         anActor->RemoveAllClippingPlanes();
1770
1771     SMESH::TPlaneDataVector::iterator anIter2 = myPlanes.begin();
1772     for( ; anIter2 != myPlanes.end(); anIter2++ ) {
1773       SMESH::TPlaneData aPlaneData = *anIter2;
1774       SMESH::TPlane aPlane = aPlaneData.Plane;
1775       SMESH::TActorList anActorList = aPlaneData.ActorList;
1776
1777       // the check is disabled to support planes with empty actor list
1778       //if( anActorList.empty() )
1779       //  continue;
1780
1781       SMESH::OrientedPlane* anOrientedPlane = SMESH::OrientedPlane::New(myViewWindow);
1782       anOrientedPlane->ShallowCopy(aPlane.GetPointer());
1783       SMESH::TActorList::iterator anIter3 = anActorList.begin();
1784       for( ; anIter3 != anActorList.end(); anIter3++ )
1785         if( vtkActor* aVTKActor = *anIter3 )
1786           if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) ) {
1787             if( anOrientedPlane->IsOpenGLClipping )
1788               anActor->AddOpenGLClippingPlane( anOrientedPlane->InvertPlane() );
1789             else
1790               anActor->AddClippingPlane( anOrientedPlane );
1791           }
1792
1793       SMESH::ClippingPlaneInfo aClippingPlaneInfo;
1794       aClippingPlaneInfo.Plane = anOrientedPlane;
1795       aClippingPlaneInfo.ActorList = anActorList;
1796
1797       aClippingPlaneInfoList.push_back( aClippingPlaneInfo );
1798     }
1799
1800     anAllActors->InitTraversal();
1801     while( vtkActor* aVTKActor = anAllActors->GetNextActor() )
1802       if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) ) {
1803         anActor->SetOpenGLClippingPlane();
1804       }
1805
1806     SMESH::RenderViewWindow( myViewWindow );
1807   }
1808 }
1809
1810 /*!
1811   SLOT on help button click: opens a help page
1812 */
1813 void SMESHGUI_ClippingDlg::ClickOnHelp()
1814 {
1815   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
1816   if (app)
1817     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
1818   else {
1819                 QString platform;
1820 #ifdef WIN32
1821                 platform = "winapplication";
1822 #else
1823                 platform = "application";
1824 #endif
1825     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
1826                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
1827                              arg(app->resourceMgr()->stringValue("ExternalBrowser",
1828                                                                  platform)).
1829                              arg(myHelpFileName));
1830   }
1831 }
1832
1833 void SMESHGUI_ClippingDlg::onSelectAbsoluteOrientation( int mode )
1834 {
1835   bool isUserMode = (mode==0);
1836
1837   TextLabelX->setEnabled( isUserMode );
1838   TextLabelY->setEnabled( isUserMode );
1839   TextLabelZ->setEnabled( isUserMode );
1840
1841   SpinBox_X->setEnabled( isUserMode );
1842   SpinBox_Y->setEnabled( isUserMode );
1843   SpinBox_Z->setEnabled( isUserMode );
1844
1845   TextLabelDx->setEnabled( isUserMode );
1846   TextLabelDy->setEnabled( isUserMode );
1847   TextLabelDz->setEnabled( isUserMode );
1848
1849   SpinBox_Dx->setEnabled( isUserMode );
1850   SpinBox_Dy->setEnabled( isUserMode );
1851   SpinBox_Dz->setEnabled( isUserMode );
1852
1853   if ( isUserMode )
1854     return;
1855
1856   double aDx = 0, aDy = 0, aDz = 0;
1857
1858   if ( mode == 1 )
1859   {
1860     aDz = 1;
1861     TextLabelZ->setEnabled( true );
1862     SpinBox_Z->setEnabled( true );
1863     SpinBox_Z->setFocus();
1864   }
1865   else if ( mode == 2 )
1866   {
1867     aDx = 1;
1868     TextLabelX->setEnabled( true );
1869     SpinBox_X->setEnabled( true );
1870     SpinBox_X->setFocus();
1871   }
1872   else if ( mode == 3 )
1873   {
1874     aDy = 1;
1875     TextLabelY->setEnabled( true );
1876     SpinBox_Y->setEnabled( true );
1877     SpinBox_Y->setFocus();
1878   }
1879
1880   int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
1881   SMESH::TPlaneData aPlane = myPlanes[aCurPlaneIndex];
1882   SMESH::OrientedPlane* aPlaneData = aPlane.Plane.GetPointer();
1883
1884   if ( aPlaneData->IsInvert == true ) {
1885     aDx = -aDx; aDy = -aDy; aDz = -aDz;
1886   }
1887
1888   myIsSelectPlane = true;
1889   SpinBox_Dx->setValue( aDx );
1890   SpinBox_Dy->setValue( aDy );
1891   SpinBox_Dz->setValue( aDz );
1892   myIsSelectPlane = false;
1893
1894   SetCurrentPlaneParam();
1895 }
1896
1897 /*!
1898   SLOT: called on orientation of clipping plane in relative mode changed
1899 */
1900 void SMESHGUI_ClippingDlg::onSelectRelativeOrientation ( int theItem )
1901 {
1902   if ( myPlanes.empty() )
1903     return;
1904
1905   if ( theItem == 0 ) {
1906     TextLabelRotation1->setText( tr( "ROTATION_AROUND_X_Y2Z" ) );
1907     TextLabelRotation2->setText( tr( "ROTATION_AROUND_Y_X2Z" ) );
1908   }
1909   else if ( theItem == 1 ) {
1910     TextLabelRotation1->setText( tr( "ROTATION_AROUND_Y_Z2X" ) );
1911     TextLabelRotation2->setText( tr( "ROTATION_AROUND_Z_Y2X" ) );
1912   }
1913   else if ( theItem == 2 ) {
1914     TextLabelRotation1->setText( tr( "ROTATION_AROUND_Z_X2Y" ) );
1915     TextLabelRotation2->setText( tr( "ROTATION_AROUND_X_Z2Y" ) );
1916   }
1917
1918   if( (QComboBox*)sender() == CBRelativeOrientation )
1919     SetCurrentPlaneParam();
1920 }
1921
1922 /*!
1923   SLOT on reset button click: sets default values
1924 */
1925 void SMESHGUI_ClippingDlg::onReset()
1926 {
1927   myIsSelectPlane = true;
1928   SpinBox_X->setValue(0);
1929   SpinBox_Y->setValue(0);
1930   SpinBox_Z->setValue(0);
1931   myIsSelectPlane = false;
1932
1933   SetCurrentPlaneParam();
1934 }
1935
1936 /*!
1937   SLOT on invert button click: inverts normal of cutting plane
1938 */
1939 void SMESHGUI_ClippingDlg::onInvert()
1940 {
1941   double Dx = SpinBox_Dx->value();
1942   double Dy = SpinBox_Dy->value();
1943   double Dz = SpinBox_Dz->value();
1944
1945   myIsSelectPlane = true;
1946   SpinBox_Dx->setValue( -Dx );
1947   SpinBox_Dy->setValue( -Dy );
1948   SpinBox_Dz->setValue( -Dz );
1949   myIsSelectPlane = false;
1950
1951   if ( !myPlanes.empty() ) {
1952     int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
1953     SMESH::TPlaneData aPlane = myPlanes[aCurPlaneIndex];
1954     SMESH::OrientedPlane* aPlaneData = aPlane.Plane.GetPointer();
1955     aPlaneData->IsInvert = !aPlaneData->IsInvert;
1956   }
1957   SetCurrentPlaneParam();
1958 }
1959