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