Salome HOME
Merge from V7_3_BR branch 18/12/2013
[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->setMinimumSize( 300, 0 );
480   SliderDistance->setMinimum( 0 );
481   SliderDistance->setMaximum( 100 );
482   SliderDistance->setSingleStep( 1 );
483   SliderDistance->setPageStep( 10 );
484   SliderDistance->setTracking( false );
485   GroupParametersLayout->addWidget( SliderDistance, 2, 1 );
486
487   TLValueRotation1 = new QLabel( GroupParameters );
488   TLValueRotation1->setObjectName( "TLValueRotation1" );
489   TLValueRotation1->setAlignment( Qt::AlignCenter );
490   TLValueRotation1->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
491   TLValueRotation1->setFont( fnt );
492   GroupParametersLayout->addWidget( TLValueRotation1, 3, 1 );
493
494   TextLabelRotation1 = new QLabel( tr("ROTATION_AROUND_X_Y2Z"), GroupParameters );
495   TextLabelRotation1->setObjectName( "TextLabelRotation1" );
496   GroupParametersLayout->addWidget( TextLabelRotation1, 4, 0 );
497
498   SliderRotation1 = new QSlider( Qt::Horizontal, GroupParameters );
499   SliderRotation1->setObjectName( "SliderRotation1" );
500   SliderRotation1->setMinimumSize( 300, 0 );
501   SliderRotation1->setMinimum( -180 );
502   SliderRotation1->setMaximum( 180 );
503   SliderRotation1->setSingleStep( 1 );
504   SliderRotation1->setPageStep( 10 );
505   SliderRotation1->setTracking(false);
506   GroupParametersLayout->addWidget( SliderRotation1, 4, 1 );
507
508   TLValueRotation2 = new QLabel( GroupParameters );
509   TLValueRotation2->setObjectName( "TLValueRotation2" );
510   TLValueRotation2->setAlignment( Qt::AlignCenter );
511   TLValueRotation2->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
512   TLValueRotation2->setFont( fnt );
513   GroupParametersLayout->addWidget( TLValueRotation2, 5, 1 );
514
515   TextLabelRotation2 = new QLabel(tr("ROTATION_AROUND_Y_X2Z"), GroupParameters);
516   TextLabelRotation2->setObjectName( "TextLabelRotation2" );
517   TextLabelRotation2->setObjectName( "TextLabelRotation2" );
518   GroupParametersLayout->addWidget( TextLabelRotation2, 6, 0 );
519
520   SliderRotation2 = new QSlider( Qt::Horizontal, GroupParameters );
521   SliderRotation2->setObjectName( "SliderRotation2" );
522   SliderRotation2->setMinimumSize( 300, 0 );
523   SliderRotation2->setMinimum( -180 );
524   SliderRotation2->setMaximum( 180 );
525   SliderRotation2->setSingleStep( 1 );
526   SliderRotation2->setPageStep( 10 );
527   SliderRotation2->setTracking(false);
528   GroupParametersLayout->addWidget( SliderRotation2, 6, 1 );
529
530   /***************************************************************/
531   QWidget* CheckBoxWidget = new QWidget( this );
532   QHBoxLayout* CheckBoxLayout = new QHBoxLayout( CheckBoxWidget );
533
534   PreviewCheckBox = new QCheckBox( tr("SHOW_PREVIEW"), CheckBoxWidget );
535   PreviewCheckBox->setObjectName( "PreviewCheckBox" );
536   PreviewCheckBox->setChecked( true );
537   CheckBoxLayout->addWidget( PreviewCheckBox, 0, Qt::AlignCenter );
538
539   AutoApplyCheckBox = new QCheckBox( tr("AUTO_APPLY"), CheckBoxWidget );
540   AutoApplyCheckBox->setObjectName( "AutoApplyCheckBox" );
541   CheckBoxLayout->addWidget( AutoApplyCheckBox, 0, Qt::AlignCenter );
542
543   /***************************************************************/
544   // Controls for "Ok", "Apply" and "Close" button
545   QGroupBox* GroupButtons = new QGroupBox(this);
546   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
547   GroupButtonsLayout->setSpacing(SPACING);
548   GroupButtonsLayout->setMargin(MARGIN);
549   
550   buttonOk = new QPushButton( tr( "SMESH_BUT_APPLY_AND_CLOSE" ), GroupButtons );
551   buttonOk->setAutoDefault( true );
552   buttonOk->setDefault( true );
553   buttonApply = new QPushButton( tr( "SMESH_BUT_APPLY" ), GroupButtons );
554   buttonApply->setAutoDefault( true );
555   buttonCancel = new QPushButton( tr( "SMESH_BUT_CLOSE" ), GroupButtons );
556   buttonCancel->setAutoDefault( true );
557   buttonHelp = new QPushButton( tr( "SMESH_BUT_HELP" ), GroupButtons );
558   buttonHelp->setAutoDefault( true );
559   GroupButtonsLayout->addWidget( buttonOk );
560   GroupButtonsLayout->addSpacing(10);
561   GroupButtonsLayout->addWidget( buttonApply );
562   GroupButtonsLayout->addSpacing(10);
563   GroupButtonsLayout->addStretch();
564   GroupButtonsLayout->addWidget( buttonCancel );
565   GroupButtonsLayout->addWidget( buttonHelp );
566
567   ModeStackedLayout->addWidget( ModeActiveWidget );
568   ModeStackedLayout->addWidget( GroupParameters );
569
570   SMESHGUI_ClippingDlgLayout->addWidget( GroupPlanes );
571   SMESHGUI_ClippingDlgLayout->addLayout( ModeStackedLayout );
572   SMESHGUI_ClippingDlgLayout->addWidget( CheckBoxWidget );
573   SMESHGUI_ClippingDlgLayout->addWidget( GroupButtons );
574
575   // Initializations
576   initParam();
577
578   myIsSelectPlane = false;
579
580   myHelpFileName = "clipping_page.html";
581
582   // signals and slots connections :
583   connect( ComboBoxPlanes, SIGNAL( activated( int ) ), this, SLOT( onSelectPlane( int ) ) );
584   connect( isOpenGLClipping, SIGNAL( toggled( bool ) ), this, SLOT( onIsOpenGLClipping( bool ) ) );
585   connect( buttonNew, SIGNAL( clicked() ), buttonNew, SLOT( showMenu() ) );
586   connect( buttonDelete, SIGNAL( clicked() ), this, SLOT( ClickOnDelete() ) );
587   connect( ActorList, SIGNAL( itemChanged( QListWidgetItem* ) ), this, SLOT( onActorItemChanged( QListWidgetItem*) ) );
588   connect( SelectAllCheckBox, SIGNAL( stateChanged( int ) ), this, SLOT( onSelectAll( int ) ) );
589
590   connect( invertButton, SIGNAL (clicked() ), this, SLOT( onInvert() ) ) ;
591   connect( resetButton,  SIGNAL (clicked() ), this, SLOT( onReset() ) );
592   connect( SpinBox_X,  SIGNAL ( valueChanged( double ) ),  this, SLOT( SetCurrentPlaneParam() ) );
593   connect( SpinBox_Y,  SIGNAL ( valueChanged( double ) ),  this, SLOT( SetCurrentPlaneParam() ) );
594   connect( SpinBox_Z,  SIGNAL ( valueChanged( double ) ),  this, SLOT( SetCurrentPlaneParam() ) );
595   connect( SpinBox_Dx, SIGNAL ( valueChanged( double ) ),  this, SLOT( SetCurrentPlaneParam() ) );
596   connect( SpinBox_Dy, SIGNAL ( valueChanged( double ) ),  this, SLOT( SetCurrentPlaneParam() ) );
597   connect( SpinBox_Dz, SIGNAL ( valueChanged( double ) ),  this, SLOT( SetCurrentPlaneParam() ) );
598   connect( CBAbsoluteOrientation, SIGNAL ( activated ( int ) ), this, SLOT( onSelectAbsoluteOrientation( int ) ) ) ;
599
600   connect( CBRelativeOrientation, SIGNAL( activated( int ) ), this, SLOT( onSelectRelativeOrientation( int ) ) );
601   connect( SliderDistance,   SIGNAL( sliderMoved( int ) ),  this, SLOT( SliderDistanceHasMoved( int ) ) );
602   connect( SliderDistance,   SIGNAL( valueChanged( int ) ),  this, SLOT( SliderDistanceHasMoved( int ) ) );
603   connect( SliderRotation1,   SIGNAL( sliderMoved( int ) ),  this, SLOT( SliderRotation1HasMoved( int ) ) );
604   connect( SliderRotation1,   SIGNAL( valueChanged( int ) ),  this, SLOT( SliderRotation1HasMoved( int ) ) );
605   connect( SliderRotation2,   SIGNAL( sliderMoved( int ) ),  this, SLOT( SliderRotation2HasMoved( int ) ) );
606   connect( SliderRotation2,   SIGNAL( valueChanged( int ) ),  this, SLOT( SliderRotation2HasMoved( int ) ) );
607
608   connect( PreviewCheckBox, SIGNAL( toggled( bool ) ), this, SLOT( OnPreviewToggle( bool ) ) );
609   connect( AutoApplyCheckBox, SIGNAL( toggled( bool ) ), this, SLOT( onAutoApply( bool ) ) );
610   connect( buttonOk, SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
611   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
612   connect( buttonApply, SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
613   connect( buttonHelp, SIGNAL( clicked() ), this, SLOT( ClickOnHelp() ) );
614   connect( mySMESHGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( reject() ) );
615   /* to close dialog if study frame change */
616   connect( mySMESHGUI, SIGNAL ( SignalStudyFrameChanged() ), this, SLOT( reject() ) );
617
618   initializePlaneData();
619   synchronize();
620
621   this->show();
622 }
623
624 /*!
625   Destructor
626   Destroys the object and frees any allocated resources
627 */
628 SMESHGUI_ClippingDlg::~SMESHGUI_ClippingDlg()
629 {
630   // no need to delete child widgets, Qt does it all for us
631   std::for_each(myPlanes.begin(),myPlanes.end(),TSetVisibility(false));
632   if (myViewWindow)
633     SMESH::RenderViewWindow(myViewWindow);
634
635   for( int i=0; i< myPlanes.size(); i++ ) {
636     SMESH::TPlaneData aPlaneData = myPlanes[i];
637     aPlaneData.Plane->Delete();
638   }
639
640   if (myPreviewWidget) {
641     myPreviewWidget->Off();
642     myPreviewWidget->Delete();
643   }
644   myPreviewWidget = 0;
645   myCallback->Delete();
646
647   myViewWindow->Repaint();
648 }
649
650 /*!
651   Get distance for cutting plane in relative mode
652 */
653 double SMESHGUI_ClippingDlg::getDistance() const
654 {
655   return TLValueDistance->text().toDouble();
656 }
657
658 /*!
659   Set distance of cutting plane in relative mode
660 */
661 void SMESHGUI_ClippingDlg::setDistance( const double theDistance )
662 {
663   SliderDistance->setValue( theDistance*100 );
664 }
665
666 /*!
667   Get rotation1 for cutting plane in relative mode
668 */
669 double SMESHGUI_ClippingDlg::getRotation1() const
670 {
671   return TLValueRotation1->text().remove("\xB0").toInt();
672 }
673
674 /*!
675   Get rotation2 for cutting plane in relative mode
676 */
677 double SMESHGUI_ClippingDlg::getRotation2() const
678 {
679   return TLValueRotation2->text().remove("\xB0").toInt();
680 }
681
682 /*!
683   Set angles of clipping plane in relative mode
684 */
685 void SMESHGUI_ClippingDlg::setRotation (const double theRot1, const double theRot2)
686 {
687   SliderRotation1->setValue( theRot1 );
688   SliderRotation2->setValue( theRot2 );
689 }
690
691 /*!
692   Set coordinates of origin point in dialog box
693 */
694 void SMESHGUI_ClippingDlg::setOrigin( double theVal[3] )
695 {
696   int anOrientation = CBAbsoluteOrientation->currentIndex();
697   if( anOrientation == 0 || anOrientation == 2 )
698     SpinBox_X->setValue( theVal[0] );
699   if( anOrientation == 0 || anOrientation == 3 )
700     SpinBox_Y->setValue( theVal[1] );
701   if( anOrientation == 0 || anOrientation == 1 )
702     SpinBox_Z->setValue( theVal[2] );
703 }
704
705 /*!
706   Set coordinates of normal vector in dialog box
707 */
708 void SMESHGUI_ClippingDlg::setDirection( double theVal[3] )
709 {
710   int anOrientation = CBAbsoluteOrientation->currentIndex();
711   if( anOrientation == 0 ) {
712     SpinBox_Dx->setValue( theVal[0] );
713     SpinBox_Dy->setValue( theVal[1] );
714     SpinBox_Dz->setValue( theVal[2] );
715   }
716 }
717
718 /*!
719   Create a new widget for preview clipping plane
720 */
721 vtkImplicitPlaneWidget* SMESHGUI_ClippingDlg::createPreviewWidget()
722 {
723   vtkImplicitPlaneWidget* aPlaneWgt = vtkImplicitPlaneWidget::New();
724
725   aPlaneWgt->SetInteractor( myViewWindow->getInteractor() );
726   aPlaneWgt->SetPlaceFactor( SIZEFACTOR );
727   aPlaneWgt->ScaleEnabledOff();
728   aPlaneWgt->SetOrigin( 0, 0, 0 );
729   aPlaneWgt->SetNormal( -1, -1, -1 );
730   aPlaneWgt->Off();
731
732   double anRGB[3];
733   SMESH::GetColor( "SMESH", "fill_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 170, 255 ) );
734
735   aPlaneWgt->GetPlaneProperty()->SetColor( anRGB[0],anRGB[1],anRGB[2] );
736   aPlaneWgt->GetPlaneProperty()->SetOpacity( 0.2 );;
737
738   aPlaneWgt->GetSelectedPlaneProperty()->SetColor( anRGB[0],anRGB[1],anRGB[2] );
739   aPlaneWgt->GetSelectedPlaneProperty()->SetOpacity( 0.2 );
740   aPlaneWgt->GetSelectedPlaneProperty()->SetLineWidth( 2.0 );
741
742   aPlaneWgt->AddObserver(vtkCommand::InteractionEvent, myCallback, 0.);
743
744   return aPlaneWgt;
745 }
746
747 /*!
748   Translate two angles of plane to normal
749 */
750 void rotationToNormal ( double theRotation[2],
751                         int theOrientation,
752                         double theNormal[3],
753                         double theDir[2][3] )
754 {
755   static double aCoeff = M_PI/180.0;
756
757   double anU[2] = { cos( aCoeff * theRotation[0] ), cos( aCoeff * theRotation[1] ) };
758   double aV[2] = { sqrt( 1.0 - anU[0]*anU[0] ), sqrt( 1.0 - anU[1] * anU[1] ) };
759   aV[0] = theRotation[0] > 0? aV[0]: -aV[0];
760   aV[1] = theRotation[1] > 0? aV[1]: -aV[1];
761
762   switch ( theOrientation ) {
763   case 0:
764   case 1:
765         theDir[0][1] = anU[0];
766         theDir[0][2] = aV[0];
767         theDir[1][0] = anU[1];
768         theDir[1][2] = aV[1];
769     break;
770   case 2:
771         theDir[0][2] = anU[0];
772         theDir[0][0] = aV[0];
773         theDir[1][1] = anU[1];
774         theDir[1][0] = aV[1];
775     break;
776   case 3:
777         theDir[0][0] = anU[0];
778         theDir[0][1] = aV[0];
779         theDir[1][2] = anU[1];
780         theDir[1][1] = aV[1];
781     break;
782   }
783
784   vtkMath::Cross( theDir[1], theDir[0], theNormal );
785   vtkMath::Normalize( theNormal );
786   vtkMath::Cross( theNormal, theDir[1], theDir[0] );
787 }
788
789 /*!
790   Used in SMESHGUI::restoreVisualParameters() to avoid
791   Declaration of OrientedPlane outside of SMESHGUI_ClippingDlg.cxx
792 */
793 bool SMESHGUI_ClippingDlg::AddPlane ( SMESH::TActorList       theActorList,
794                                       SMESH::OrientedPlane*   thePlane )
795 {
796   double aNormal[3];
797   double aDir[2][3] = {{0, 0, 0}, {0, 0, 0}};
798   static double aCoeff = vtkMath::Pi()/180.0;
799
800   int anOrientation;
801   if ( thePlane->PlaneMode == SMESH::Absolute )
802    anOrientation = thePlane->myAbsoluteOrientation;
803   else if ( thePlane->PlaneMode == SMESH::Relative )
804    anOrientation = thePlane->myRelativeOrientation + 1;
805
806   if ( anOrientation == 0 ) {
807         // compute a direction for plane in absolute mode
808     double znam = sqrt( thePlane->Dx*thePlane->Dx + thePlane->Dy*thePlane->Dy + thePlane->Dz*thePlane->Dz );
809     double aRotation = acos( thePlane->Dy/znam )/aCoeff;
810     if ( thePlane->Dy >= 0.0 && thePlane->Dz >= 0.0 )      thePlane->myAngle[0] = 90.0 + aRotation;
811     else if ( thePlane->Dy >= 0.0 && thePlane->Dz < 0.0 ) thePlane->myAngle[0] = 90.0 - aRotation;
812     else if ( thePlane->Dy < 0.0 && thePlane->Dz >= 0.0 ) thePlane->myAngle[0] = aRotation - 90.0;
813     else if ( thePlane->Dy < 0.0 && thePlane->Dz < 0.0 ) thePlane->myAngle[0] = 270.0 - aRotation;
814
815     aRotation = acos( thePlane->Dx/znam )/aCoeff;
816     if ( thePlane->Dx >= 0.0 && thePlane->Dz >= 0.0 )      thePlane->myAngle[1] = 90.0 + aRotation;
817     else if ( thePlane->Dx >= 0.0 && thePlane->Dz < 0.0 ) thePlane->myAngle[1] = 90.0 - aRotation;
818     else if ( thePlane->Dx < 0.0 && thePlane->Dz >= 0.0 ) thePlane->myAngle[1] = aRotation - 90.0;
819     else if ( thePlane->Dx < 0.0 && thePlane->Dz < 0.0 ) thePlane->myAngle[1] = 270.0 - aRotation;
820   }
821
822   // compute a normal
823   rotationToNormal( thePlane->myAngle, anOrientation, aNormal, aDir );
824
825   double aBounds[6];
826   double anOrigin[3];
827
828   if ( thePlane->PlaneMode == SMESH::Absolute ) {
829     aNormal[0] = thePlane->Dx;
830     aNormal[1] = thePlane->Dy;
831     aNormal[2] = thePlane->Dz;
832   }
833
834   bool anIsOk = false;
835   if( theActorList.empty() ) {
836     // to support planes with empty actor list we should create
837     // a nullified plane that will be initialized later
838     anOrigin[0] = anOrigin[1] = anOrigin[2] = 0;
839     aBounds[0] = aBounds[2] = aBounds[4] = 0;
840     aBounds[1] = aBounds[3] = aBounds[5] = 0;
841     anIsOk = true;
842   }
843   else
844     anIsOk = SMESH::ComputeClippingPlaneParameters( theActorList,
845                                                     aNormal,
846                                                     thePlane->myDistance,
847                                                     aBounds,
848                                                     anOrigin );
849   if( !anIsOk )
850     return false;
851
852   if ( thePlane->PlaneMode == SMESH::Absolute ) {
853     anOrigin[0] = thePlane->X;
854     anOrigin[1] = thePlane->Y;
855     anOrigin[2] = thePlane->Z;
856   }
857   thePlane->SetNormal( aNormal );
858   thePlane->SetOrigin( anOrigin );
859
860
861   double aPnt[3] = { ( aBounds[0] + aBounds[1] ) / 2.,
862                      ( aBounds[2] + aBounds[3] ) / 2.,
863                      ( aBounds[4] + aBounds[5] ) / 2. };
864
865   double aDel = pow( pow( aBounds[1] - aBounds[0], 2 ) +
866                      pow( aBounds[3] - aBounds[2], 2 ) +
867                      pow( aBounds[5] - aBounds[4], 2 ), 0.5 );
868
869   double aDelta[2][3] = { { aDir[0][0]*aDel, aDir[0][1]*aDel, aDir[0][2]*aDel },
870                           { aDir[1][0]*aDel, aDir[1][1]*aDel, aDir[1][2]*aDel } };
871   double aParam, aPnt0[3], aPnt1[3], aPnt2[3];
872
873   double aPnt01[3] = { aPnt[0] - aDelta[0][0] - aDelta[1][0],
874                        aPnt[1] - aDelta[0][1] - aDelta[1][1],
875                        aPnt[2] - aDelta[0][2] - aDelta[1][2] };
876   double aPnt02[3] = { aPnt01[0] + aNormal[0],
877                        aPnt01[1] + aNormal[1],
878                        aPnt01[2] + aNormal[2] };
879   vtkPlane::IntersectWithLine( aPnt01, aPnt02, aNormal, anOrigin, aParam, aPnt0 );
880
881   double aPnt11[3] = { aPnt[0] - aDelta[0][0] + aDelta[1][0],
882                        aPnt[1] - aDelta[0][1] + aDelta[1][1],
883                        aPnt[2] - aDelta[0][2] + aDelta[1][2] };
884   double aPnt12[3] = { aPnt11[0] + aNormal[0],
885                        aPnt11[1] + aNormal[1],
886                        aPnt11[2] + aNormal[2] };
887   vtkPlane::IntersectWithLine( aPnt11, aPnt12, aNormal, anOrigin, aParam, aPnt1);
888
889   double aPnt21[3] = { aPnt[0] + aDelta[0][0] - aDelta[1][0],
890                        aPnt[1] + aDelta[0][1] - aDelta[1][1],
891                        aPnt[2] + aDelta[0][2] - aDelta[1][2] };
892   double aPnt22[3] = { aPnt21[0] + aNormal[0],
893                        aPnt21[1] + aNormal[1],
894                        aPnt21[2] + aNormal[2] };
895   vtkPlane::IntersectWithLine( aPnt21, aPnt22, aNormal, anOrigin, aParam, aPnt2);
896
897   vtkPlaneSource* aPlaneSource = thePlane->myPlaneSource;
898   aPlaneSource->SetNormal( aNormal[0], aNormal[1], aNormal[2] );
899   aPlaneSource->SetOrigin( aPnt0[0], aPnt0[1], aPnt0[2] );
900   aPlaneSource->SetPoint1( aPnt1[0], aPnt1[1], aPnt1[2] );
901   aPlaneSource->SetPoint2( aPnt2[0], aPnt2[1], aPnt2[2] );
902   aPlaneSource->Update();
903
904   SMESH::TActorList::iterator anIter = theActorList.begin();
905   for ( ; anIter != theActorList.end(); anIter++ )
906     if( vtkActor* aVTKActor = *anIter )
907       if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) ) {
908         if( thePlane->IsOpenGLClipping )
909           anActor->AddOpenGLClippingPlane( thePlane->InvertPlane() );
910         else
911                 anActor->AddClippingPlane( thePlane );
912       }
913
914   return true;
915 }
916
917 /*!
918   Custom handling of events
919 */
920 void SMESHGUI_ClippingDlg::keyPressEvent( QKeyEvent* e )
921 {
922   QDialog::keyPressEvent( e );
923   if ( e->isAccepted() )
924     return;
925
926   if ( e->key() == Qt::Key_F1 ) {
927     e->accept();
928     ClickOnHelp();
929   }
930 }
931
932 /*!
933   Handles the char preview widget activation event
934 */
935 void SMESHGUI_ClippingDlg::ProcessEvents( vtkObject* theObject,
936                                           unsigned long theEvent,
937                                           void* theClientData,
938                                           void* vtkNotUsed( theCallData ) )
939 {
940   vtkImplicitPlaneWidget* aWidget = vtkImplicitPlaneWidget::SafeDownCast( theObject );
941   if ( aWidget == NULL ) return;
942   if ( theClientData == NULL ) return;
943
944   SMESHGUI_ClippingDlg* aDlg = (SMESHGUI_ClippingDlg*) theClientData;
945
946   double anOrigin[3];
947   double aDir[3];
948
949   switch( theEvent ){
950   case vtkCommand::InteractionEvent:
951     aWidget->GetOrigin( anOrigin );
952     aWidget->GetNormal( aDir );
953
954     aDlg->myIsSelectPlane = true;
955
956     if( aDlg->CurrentMode == SMESH::Absolute ) {
957       aDlg->setOrigin( anOrigin );
958       aDlg->setDirection( aDir );
959       aDlg->myIsPreviewMoved = true;
960     }
961     else if( aDlg->CurrentMode == SMESH::Relative ) {
962       aDlg->absolutePlaneToRelative( anOrigin, aDir );
963     }
964     aDlg->myIsSelectPlane = false;
965
966     aDlg->SetCurrentPlaneParam();
967     aDlg->myIsPreviewMoved = false;
968     break;
969   }
970 }
971
972 /*!
973   Initialize the planes's data when the dialog opened
974 */
975 void SMESHGUI_ClippingDlg::initializePlaneData()
976 {
977   const SMESHGUI_ClippingPlaneInfoMap& aClippingPlaneInfoMap = mySMESHGUI->getClippingPlaneInfoMap();
978   SMESHGUI_ClippingPlaneInfoMap::const_iterator anIter1 = aClippingPlaneInfoMap.find( myViewWindow->getViewManager() );
979   if( anIter1 != aClippingPlaneInfoMap.end() ) {
980     const SMESHGUI_ClippingPlaneInfoList& aClippingPlaneInfoList = anIter1->second;
981     SMESHGUI_ClippingPlaneInfoList::const_iterator anIter2 = aClippingPlaneInfoList.begin();
982     for( ; anIter2 != aClippingPlaneInfoList.end(); anIter2++ ) {
983       const SMESH::ClippingPlaneInfo& aClippingPlaneInfo = *anIter2;
984       SMESH::OrientedPlane* anOrientedPlane = SMESH::OrientedPlane::New(myViewWindow);
985       anOrientedPlane->ShallowCopy(aClippingPlaneInfo.Plane);
986       SMESH::TPlane aTPlane( anOrientedPlane );
987       SMESH::TPlaneData aPlaneData( aTPlane, aClippingPlaneInfo.ActorList );
988       myPlanes.push_back( aPlaneData );
989     }
990   }
991   std::for_each( myPlanes.begin(),myPlanes.end(), TSetVisibility( PreviewCheckBox->isChecked() ) );
992   if( myPlanes.size() )
993     myPreviewWidget->SetEnabled( PreviewCheckBox->isChecked() );
994 }
995
996 /*!
997   Initialization of initial values of widgets
998 */
999 void SMESHGUI_ClippingDlg::initParam()
1000 {
1001   SpinBox_X->setValue( 0.0 );
1002   SpinBox_Y->setValue( 0.0 );
1003   SpinBox_Z->setValue( 0.0 );
1004
1005   SpinBox_Dx->setValue( 1.0 );
1006   SpinBox_Dy->setValue( 1.0 );
1007   SpinBox_Dz->setValue( 1.0 );
1008
1009   CBAbsoluteOrientation->setCurrentIndex(0);
1010
1011   TLValueDistance->setText( "0.5" );
1012   TLValueRotation1->setText( "0\xB0" );
1013   TLValueRotation2->setText( "0\xB0" );
1014   CBRelativeOrientation->setCurrentIndex( 0 );
1015   SliderDistance->setValue( 50 );
1016   SliderRotation1->setValue( 0 );
1017   SliderRotation2->setValue( 0 );
1018 }
1019
1020 /*!
1021   Synchronize dialog's widgets with data
1022 */
1023 void SMESHGUI_ClippingDlg::synchronize()
1024 {
1025   int aNbPlanes = myPlanes.size();
1026   ComboBoxPlanes->clear();
1027
1028   QString aName;
1029   for( int i = 1; i<=aNbPlanes; i++ ) {
1030     aName = QString( tr( "PLANE_NUM" ) ).arg(i);
1031     ComboBoxPlanes->addItem( aName );
1032   }
1033
1034   int aPos = ComboBoxPlanes->count() - 1;
1035   ComboBoxPlanes->setCurrentIndex( aPos );
1036
1037   bool anIsControlsEnable = ( aPos >= 0 );
1038   if ( anIsControlsEnable ) {
1039     onSelectPlane( aPos );
1040     updateActorList();
1041     if( PreviewCheckBox->isChecked() )
1042       myPreviewWidget->On();
1043   }
1044   else {
1045     ComboBoxPlanes->addItem( tr( "NO_PLANES" ) );
1046     ActorList->clear();
1047     initParam();
1048     if( PreviewCheckBox->isChecked() )
1049       myPreviewWidget->Off();
1050   }
1051
1052   isOpenGLClipping->setEnabled( anIsControlsEnable );
1053   ActorList->setEnabled( anIsControlsEnable );
1054   SelectAllCheckBox->setEnabled( anIsControlsEnable );
1055   buttonDelete->setEnabled( anIsControlsEnable );
1056   if ( CurrentMode == SMESH::Absolute ) {
1057     SpinBox_X->setEnabled( anIsControlsEnable );
1058     SpinBox_Y->setEnabled( anIsControlsEnable );
1059     SpinBox_Z->setEnabled( anIsControlsEnable );
1060     SpinBox_Dx->setEnabled( anIsControlsEnable );
1061     SpinBox_Dy->setEnabled( anIsControlsEnable );
1062     SpinBox_Dz->setEnabled( anIsControlsEnable );
1063     CBAbsoluteOrientation->setEnabled( anIsControlsEnable );
1064     invertButton->setEnabled( anIsControlsEnable );
1065     resetButton->setEnabled( anIsControlsEnable );
1066   }
1067   else if ( CurrentMode == SMESH::Relative ) {
1068     CBRelativeOrientation->setEnabled( anIsControlsEnable );
1069     SliderDistance->setEnabled( anIsControlsEnable );
1070     SliderRotation1->setEnabled( anIsControlsEnable );
1071     SliderRotation2->setEnabled( anIsControlsEnable );
1072   }
1073 }
1074
1075 /*!
1076   Update the list of actors
1077 */
1078 void SMESHGUI_ClippingDlg::updateActorList()
1079 {
1080   ActorList->clear();
1081
1082   SalomeApp_Study* anAppStudy = SMESHGUI::activeStudy();
1083   if( !anAppStudy )
1084     return;
1085
1086   _PTR(Study) aStudy = anAppStudy->studyDS();
1087   if( !aStudy )
1088     return;
1089
1090   if( !myViewWindow )
1091     return;
1092
1093   int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
1094   const SMESH::TPlaneData& aPlaneData = myPlanes[ aCurPlaneIndex ];
1095   const SMESH::TActorList& anActorList = aPlaneData.ActorList;
1096
1097   std::for_each( myPlanes.begin(),myPlanes.end(), TSetVisibility( PreviewCheckBox->isChecked() ) );
1098   aPlaneData.Plane.GetPointer()->myActor->SetVisibility( false );
1099
1100   VTK::ActorCollectionCopy aCopy( myViewWindow->getRenderer()->GetActors() );
1101   vtkActorCollection* anAllActors = aCopy.GetActors();
1102   anAllActors->InitTraversal();
1103   while( vtkActor* aVTKActor = anAllActors->GetNextActor() ) {
1104     if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) ) {
1105       if( anActor->hasIO() ) {
1106         Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
1107         if( _PTR(SObject) aSObj = aStudy->FindObjectID( anIO->getEntry() ) ) {
1108           bool anIsChecked = false;
1109           SMESH::TActorList::const_iterator anIter = anActorList.begin();
1110           for ( ; anIter != anActorList.end(); anIter++ ) {
1111             if( vtkActor* aVTKActorRef = *anIter ) {
1112               if( SMESH_Actor* anActorRef = SMESH_Actor::SafeDownCast( aVTKActorRef ) ) {
1113                 if( anActorRef == anActor ) {
1114                   anIsChecked = true;
1115                   break;
1116                 }
1117               }
1118             }
1119           }
1120           QString aName = QString( aSObj->GetName().c_str() );
1121           QListWidgetItem* anItem = new ActorItem( anActor, aName, ActorList );
1122           anItem->setCheckState( anIsChecked ? Qt::Checked : Qt::Unchecked );
1123           updateActorItem( anItem, true, false );
1124         }
1125       }
1126     }
1127   }
1128 }
1129
1130 /*!
1131   Update an actor in actor's list
1132 */
1133 void SMESHGUI_ClippingDlg::updateActorItem( QListWidgetItem* theItem,
1134                                             bool theUpdateSelectAll,
1135                                             bool theUpdateClippingPlaneMap )
1136 {
1137   // update Select All check box
1138   if( theUpdateSelectAll ) {
1139     int aNbItems = ActorList->count(), aNbChecked = 0;
1140     for( int i = 0; i < aNbItems; i++ )
1141       if( QListWidgetItem* anItem = ActorList->item( i ) )
1142         if( anItem->checkState() == Qt::Checked )
1143           aNbChecked++;
1144
1145     bool anIsBlocked = SelectAllCheckBox->blockSignals( true );
1146     SelectAllCheckBox->setCheckState( aNbChecked == aNbItems ? Qt::Checked : Qt::Unchecked);
1147     SelectAllCheckBox->blockSignals( anIsBlocked );
1148   }
1149
1150   // update clipping plane map
1151   if( theUpdateClippingPlaneMap ) {
1152     int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
1153     if( ActorItem* anItem = dynamic_cast<ActorItem*>( theItem ) ) {
1154       if( SMESH_Actor* anActor = anItem->getActor() ) {
1155         SMESH::TPlaneData& aPlaneData = myPlanes[ aCurPlaneIndex ];
1156         SMESH::TActorList& anActorList = aPlaneData.ActorList;
1157         bool anIsPushed = false;
1158         SMESH::TActorList::iterator anIter = anActorList.begin();
1159         for ( ; anIter != anActorList.end(); anIter++ ) {
1160           if( anActor == *anIter ) {
1161             anIsPushed = true;
1162             break;
1163           }
1164         }
1165         if( theItem->checkState() == Qt::Checked && !anIsPushed )
1166           anActorList.push_back( anActor );
1167         else if( theItem->checkState() == Qt::Unchecked && anIsPushed )
1168           anActorList.remove( anActor );
1169
1170         SMESH::ComputeBounds( anActorList, myBounds );
1171         myPreviewWidget->PlaceWidget( myBounds[0], myBounds[1], myBounds[2],
1172                                                   myBounds[3], myBounds[4], myBounds[5] );
1173       }
1174     }
1175   }
1176 }
1177
1178 /*!
1179   Get the list of current actors
1180 */
1181 SMESH::TActorList SMESHGUI_ClippingDlg::getCurrentActors()
1182 {
1183   SMESH::TActorList anActorList;
1184   for( int i = 0, n = ActorList->count(); i < n; i++ )
1185     if( ActorItem* anItem = dynamic_cast<ActorItem*>( ActorList->item( i ) ) )
1186       if( anItem->checkState() == Qt::Checked )
1187         if( SMESH_Actor* anActor = anItem->getActor() )
1188           anActorList.push_back( anActor );
1189   return anActorList;
1190 }
1191
1192 /*!
1193   Dump the parameters of clipping planes
1194 */
1195 void SMESHGUI_ClippingDlg::dumpPlaneData() const
1196 {
1197   printf( "----------- Plane Data -----------\n" );
1198   int anId = 1;
1199   SMESH::TPlaneDataVector::const_iterator anIter1 = myPlanes.begin();
1200   for ( ; anIter1 != myPlanes.end(); anIter1++, anId++ ) {
1201     SMESH::TPlaneData aPlaneData = *anIter1;
1202     SMESH::TPlane aPlane = aPlaneData.Plane;
1203     double* aNormal = aPlane->GetNormal();
1204     double* anOrigin = aPlane->GetOrigin();
1205     printf( "Plane N%d:\n", anId );
1206     printf( "  Normal = ( %f, %f, %f )\n", aNormal[0], aNormal[1], aNormal[2] );
1207     printf( "  Origin = ( %f, %f, %f )\n", anOrigin[0], anOrigin[1], anOrigin[2] );
1208
1209     SMESH::TActorList anActorList = aPlaneData.ActorList;
1210     SMESH::TActorList::const_iterator anIter2 = anActorList.begin();
1211     for ( ; anIter2 != anActorList.end(); anIter2++ ) {
1212       if( vtkActor* aVTKActor = *anIter2 ) {
1213         if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) )
1214           printf( "  - Actor: '%s'\n", anActor->getName() );
1215       }
1216       else
1217         printf( "  - Actor: NULL\n");
1218     }
1219   }
1220   printf( "----------------------------------\n" );
1221 }
1222
1223 /*!
1224   SLOT on close button click: rejects dialog
1225 */
1226 void SMESHGUI_ClippingDlg::reject()
1227 {
1228   //here we can insert actions to do at close.
1229   QDialog::reject();
1230 }
1231
1232 /*!
1233   Set absolute mode of clipping plane
1234 */
1235 void SMESHGUI_ClippingDlg::onModeAbsolute()
1236 {
1237   ModeStackedLayout->setCurrentIndex(0);
1238   CurrentMode = SMESH::Absolute;
1239   ClickOnNew();
1240   SetCurrentPlaneParam();
1241 }
1242
1243 /*!
1244   Set relative mode of clipping plane
1245 */
1246 void SMESHGUI_ClippingDlg::onModeRelative()
1247 {
1248   ModeStackedLayout->setCurrentIndex(1);
1249   CurrentMode = SMESH::Relative;
1250   ClickOnNew();
1251   SetCurrentPlaneParam();
1252 }
1253
1254 /*!
1255   SLOT on new button click: create a new clipping plane
1256 */
1257 void SMESHGUI_ClippingDlg::ClickOnNew()
1258 {
1259   if(myViewWindow) {
1260     SMESH::OrientedPlane* aPlane = SMESH::OrientedPlane::New(myViewWindow);
1261     SMESH::TPlane aTPlane(aPlane);
1262     aPlane->PlaneMode = CurrentMode;
1263     SMESH::TActorList anActorList;
1264     VTK::ActorCollectionCopy aCopy( myViewWindow->getRenderer()->GetActors() );
1265     vtkActorCollection* anAllActors = aCopy.GetActors();
1266     anAllActors->InitTraversal();
1267     while( vtkActor* aVTKActor = anAllActors->GetNextActor() )
1268       if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) )
1269         anActorList.push_back( anActor );
1270
1271     SMESH::TPlaneData aPlaneData(aTPlane, anActorList);
1272
1273     myPlanes.push_back(aPlaneData);
1274
1275
1276     std::for_each( myPlanes.begin(),myPlanes.end(), TSetVisibility( PreviewCheckBox->isChecked() ) );
1277     aPlane->myActor->SetVisibility( false );
1278
1279     bool anIsBlocked = ActorList->blockSignals( true );
1280
1281     SMESH::ComputeBounds( anActorList, myBounds );
1282     myPreviewWidget->PlaceWidget( myBounds[0],myBounds[1],myBounds[2],
1283                                   myBounds[3],myBounds[4],myBounds[5] );
1284     synchronize();
1285     SetCurrentPlaneParam();
1286
1287     ActorList->blockSignals( anIsBlocked );
1288   }
1289 }
1290
1291 /*!
1292   SLOT on delete button click: Delete selected clipping plane
1293 */
1294 void SMESHGUI_ClippingDlg::ClickOnDelete()
1295 {
1296   if (myPlanes.empty())
1297     return;
1298
1299   int aPlaneIndex = ComboBoxPlanes->currentIndex();
1300
1301   SMESH::TPlaneDataVector::iterator anIter = myPlanes.begin() + aPlaneIndex;
1302   SMESH::TPlaneData aPlaneData = *anIter;
1303   aPlaneData.Plane.GetPointer()->myActor->SetVisibility(false);
1304   myPlanes.erase(anIter);
1305
1306   if(AutoApplyCheckBox->isChecked())
1307     ClickOnApply();
1308
1309   synchronize();
1310   SMESH::RenderViewWindow( myViewWindow );
1311 }
1312
1313 /*!
1314   Set current parameters of selected plane
1315 */
1316 void SMESHGUI_ClippingDlg::onSelectPlane ( int theIndex )
1317 {
1318   if ( myPlanes.empty() )
1319     return;
1320
1321   SMESH::TPlaneData aPlaneData = myPlanes[theIndex];
1322   SMESH::OrientedPlane* aPlane = aPlaneData.Plane.GetPointer();
1323
1324   myIsSelectPlane = true;
1325   isOpenGLClipping->setChecked( aPlane->IsOpenGLClipping );
1326
1327   if ( aPlane->PlaneMode == SMESH::Absolute ) {
1328     ModeStackedLayout->setCurrentIndex( 0 );
1329     CurrentMode = SMESH::Absolute;
1330     int anOrientation = aPlane->myAbsoluteOrientation;
1331     // Set plane parameters in the dialog
1332     double anOrigin[3], aDir[3];
1333     anOrigin[0] = aPlane->X;
1334     anOrigin[1] = aPlane->Y;
1335     anOrigin[2] = aPlane->Z;
1336     setOrigin( anOrigin );
1337     aDir[0] = aPlane->Dx;
1338     aDir[1] = aPlane->Dy;
1339     aDir[2] = aPlane->Dz;
1340     setDirection( aDir );
1341     CBAbsoluteOrientation->setCurrentIndex( anOrientation );
1342     onSelectAbsoluteOrientation( anOrientation );
1343   }
1344   else if ( aPlane->PlaneMode == SMESH::Relative ) {
1345     ModeStackedLayout->setCurrentIndex( 1 );
1346     CurrentMode = SMESH::Relative;
1347     SMESH::Orientation anOrientation = aPlane->GetOrientation();
1348     double aRot[2] = { aPlane->myAngle[0], aPlane->myAngle[1] };
1349     // Set plane parameters in the dialog
1350     setDistance( aPlane->GetDistance() );
1351     setRotation( aRot[0], aRot[1] );
1352     switch ( anOrientation ) {
1353     case SMESH::XY:
1354       CBRelativeOrientation->setCurrentIndex(0);
1355       onSelectRelativeOrientation(0);
1356       break;
1357     case SMESH::YZ:
1358       CBRelativeOrientation->setCurrentIndex(1);
1359       onSelectRelativeOrientation(1);
1360       break;
1361     case SMESH::ZX:
1362       CBRelativeOrientation->setCurrentIndex(2);
1363       onSelectRelativeOrientation(2);
1364       break;
1365     }
1366   }
1367   myIsSelectPlane = false;
1368   SMESH::ComputeBounds( aPlaneData.ActorList, myBounds );
1369   myPreviewWidget->PlaceWidget( myBounds[0], myBounds[1], myBounds[2],
1370                                           myBounds[3], myBounds[4], myBounds[5] );
1371   SetCurrentPlaneParam();
1372
1373   // Actors
1374   bool anIsBlocked = ActorList->blockSignals( true );
1375   updateActorList();
1376   ActorList->blockSignals( anIsBlocked );
1377 }
1378
1379 /*!
1380   SLOT: called on OpenGLClipping check box toggled
1381 */
1382 void SMESHGUI_ClippingDlg::onIsOpenGLClipping( bool toggled )
1383 {
1384   if ( myPlanes.empty() || myIsSelectPlane )
1385     return;
1386
1387   int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
1388   SMESH::TPlaneData aPlane = myPlanes[aCurPlaneIndex];
1389   SMESH::OrientedPlane* aPlaneData = aPlane.Plane.GetPointer();
1390   aPlaneData->IsOpenGLClipping = toggled;
1391
1392   if( AutoApplyCheckBox->isChecked() )
1393     ClickOnApply();
1394 }
1395
1396 /*!
1397   SLOT: called on SelectAll check box toggled
1398 */
1399 void SMESHGUI_ClippingDlg::onSelectAll( int theState )
1400 {
1401   if( theState == Qt::PartiallyChecked ) {
1402     SelectAllCheckBox->setCheckState( Qt::Checked );
1403     return;
1404   }
1405
1406   bool anIsBlocked = ActorList->blockSignals( true );
1407   for( int i = 0, n = ActorList->count(); i < n; i++ ) {
1408     if( QListWidgetItem* anItem = ActorList->item( i ) ) {
1409       anItem->setCheckState( theState == Qt::Checked ? Qt::Checked : Qt::Unchecked );
1410       updateActorItem( anItem, false, true );
1411     }
1412   }
1413   SelectAllCheckBox->setTristate( false );
1414   ActorList->blockSignals( anIsBlocked );
1415   SetCurrentPlaneParam();
1416 }
1417
1418 /*!
1419   SLOT: called when actor item was changed
1420 */
1421 void SMESHGUI_ClippingDlg::onActorItemChanged( QListWidgetItem* theItem )
1422 {
1423   updateActorItem( theItem, true, true );
1424   SetCurrentPlaneParam();
1425 }
1426
1427 /*!
1428   Restore parameters of selected plane
1429 */
1430 void SMESHGUI_ClippingDlg::SetCurrentPlaneParam()
1431 {
1432   if ( myPlanes.empty() || myIsSelectPlane )
1433     return;
1434
1435   int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
1436
1437   SMESH::TPlaneData aPlaneData = myPlanes[aCurPlaneIndex];
1438   SMESH::OrientedPlane* aPlane = aPlaneData.Plane.GetPointer();
1439
1440   if ( aPlane->PlaneMode == SMESH::Absolute ) {
1441     aPlane->myAbsoluteOrientation = CBAbsoluteOrientation->currentIndex();
1442     aPlane->X = SpinBox_X->value();
1443     aPlane->Y = SpinBox_Y->value();
1444     aPlane->Z = SpinBox_Z->value();
1445     aPlane->Dx = SpinBox_Dx->value();
1446     aPlane->Dy = SpinBox_Dy->value();
1447     aPlane->Dz = SpinBox_Dz->value();
1448   }
1449
1450   double aNormal[3];
1451   double aDir[2][3] = { {0, 0, 0}, {0, 0, 0} };
1452   static double aCoeff = vtkMath::Pi()/180.0;
1453
1454   double aRot[2] = { getRotation1(), getRotation2() };
1455   int anOrient;
1456   if ( aPlane->PlaneMode == SMESH::Absolute )
1457         anOrient = CBAbsoluteOrientation->currentIndex();
1458   else if ( aPlane->PlaneMode == SMESH::Relative )
1459         anOrient = CBRelativeOrientation->currentIndex() + 1;
1460
1461   if ( aPlane->PlaneMode == SMESH::Relative ) {
1462     aPlane->myAngle[0] = aRot[0];
1463     aPlane->myAngle[1] = aRot[1];
1464     aPlane->SetOrientation( SMESH::Orientation( CBRelativeOrientation->currentIndex() ) );
1465     aPlane->SetDistance( getDistance() );
1466   }
1467
1468   if ( anOrient == 0 ) {
1469         // compute a direction for plane in absolute mode
1470     double znam = sqrt( aPlane->Dx*aPlane->Dx + aPlane->Dy*aPlane->Dy + aPlane->Dz*aPlane->Dz );
1471     double aRotation = acos( aPlane->Dy/znam )/aCoeff;
1472     if ( aPlane->Dy >= 0.0 && aPlane->Dz >= 0.0 )     aRot[0] = 90.0 + aRotation;
1473     else if ( aPlane->Dy >= 0.0 && aPlane->Dz < 0.0 ) aRot[0] = 90.0 - aRotation;
1474     else if ( aPlane->Dy < 0.0 && aPlane->Dz >= 0.0 ) aRot[0] = aRotation - 90.0;
1475     else if ( aPlane->Dy < 0.0 && aPlane->Dz < 0.0 )  aRot[0] = 270.0 - aRotation;
1476
1477     aRotation = acos( aPlane->Dx/znam )/aCoeff;
1478     if ( aPlane->Dx >= 0.0 && aPlane->Dz >= 0.0 )     aRot[1] = 90.0 + aRotation;
1479     else if ( aPlane->Dx >= 0.0 && aPlane->Dz < 0.0 ) aRot[1] = 90.0 - aRotation;
1480     else if ( aPlane->Dx < 0.0 && aPlane->Dz >= 0.0 ) aRot[1] = aRotation - 90.0;
1481     else if ( aPlane->Dx < 0.0 && aPlane->Dz < 0.0 )  aRot[1] = 270.0 - aRotation;
1482   }
1483
1484   // compute a normal
1485   rotationToNormal( aRot, anOrient, aNormal, aDir );
1486
1487
1488   SMESH::TActorList anActorList = aPlaneData.ActorList;
1489
1490   double aBounds[6];
1491   double anOrigin[3];
1492   double aDistance;
1493   aDistance = getDistance();
1494   if ( aPlane->PlaneMode == SMESH::Absolute ) {
1495     aNormal[0] = aPlane->Dx;
1496     aNormal[1] = aPlane->Dy;
1497     aNormal[2] = aPlane->Dz;
1498   }
1499
1500   bool anIsOk = SMESH::ComputeClippingPlaneParameters( anActorList,
1501                                                        aNormal,
1502                                                        aDistance,
1503                                                        aBounds,
1504                                                        anOrigin );
1505
1506   if ( aPlane->PlaneMode == SMESH::Absolute ) {
1507     anOrigin[0] = aPlane->X;
1508     anOrigin[1] = aPlane->Y;
1509     anOrigin[2] = aPlane->Z;
1510   }
1511   
1512   if( anIsOk ) {
1513     aPlane->SetNormal( aNormal );
1514     aPlane->SetOrigin( anOrigin );
1515
1516     double aPnt[3] = { ( aBounds[0] + aBounds[1] ) / 2.,
1517                        ( aBounds[2] + aBounds[3] ) / 2.,
1518                        ( aBounds[4] + aBounds[5] ) / 2. };
1519
1520     double aDel = pow( pow( aBounds[1] - aBounds[0], 2 ) +
1521                        pow( aBounds[3] - aBounds[2], 2 ) +
1522                        pow( aBounds[5] - aBounds[4], 2 ), 0.5 );
1523
1524     double aDelta[2][3] = { { aDir[0][0]*aDel, aDir[0][1]*aDel, aDir[0][2]*aDel },
1525                             { aDir[1][0]*aDel, aDir[1][1]*aDel, aDir[1][2]*aDel } };
1526     double aParam, aPnt0[3], aPnt1[3], aPnt2[3];
1527
1528     double aPnt01[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 aPnt02[3] = { aPnt01[0] + aNormal[0],
1532                          aPnt01[1] + aNormal[1],
1533                          aPnt01[2] + aNormal[2] };
1534     vtkPlane::IntersectWithLine(aPnt01,aPnt02,aNormal,anOrigin,aParam,aPnt0);
1535
1536     double aPnt11[3] = { aPnt[0] - aDelta[0][0] + aDelta[1][0],
1537                          aPnt[1] - aDelta[0][1] + aDelta[1][1],
1538                          aPnt[2] - aDelta[0][2] + aDelta[1][2] };
1539     double aPnt12[3] = { aPnt11[0] + aNormal[0],
1540                          aPnt11[1] + aNormal[1],
1541                          aPnt11[2] + aNormal[2] };
1542     vtkPlane::IntersectWithLine(aPnt11,aPnt12,aNormal,anOrigin,aParam,aPnt1);
1543
1544     double aPnt21[3] = { aPnt[0] + aDelta[0][0] - aDelta[1][0],
1545                          aPnt[1] + aDelta[0][1] - aDelta[1][1],
1546                          aPnt[2] + aDelta[0][2] - aDelta[1][2] };
1547     double aPnt22[3] = { aPnt21[0] + aNormal[0],
1548                          aPnt21[1] + aNormal[1],
1549                          aPnt21[2] + aNormal[2] };
1550     vtkPlane::IntersectWithLine(aPnt21,aPnt22,aNormal,anOrigin,aParam,aPnt2);
1551
1552     vtkPlaneSource* aPlaneSource = aPlane->myPlaneSource;
1553
1554     aPlaneSource->SetNormal( aNormal[0], aNormal[1], aNormal[2] );
1555     aPlaneSource->SetOrigin( aPnt0[0], aPnt0[1], aPnt0[2] );
1556     aPlaneSource->SetPoint1( aPnt1[0], aPnt1[1], aPnt1[2] );
1557     aPlaneSource->SetPoint2( aPnt2[0], aPnt2[1], aPnt2[2] );
1558     aPlaneSource->Update();
1559   }
1560
1561   setBoundsForPreviewWidget();
1562
1563   myPreviewWidget->SetOrigin( anOrigin );
1564   myPreviewWidget->SetNormal( aNormal );
1565
1566   if(AutoApplyCheckBox->isChecked())
1567     ClickOnApply();
1568
1569   SMESH::RenderViewWindow( myViewWindow );
1570 }
1571
1572 /*!
1573   Set current bounds for preview widget
1574 */
1575 void SMESHGUI_ClippingDlg::setBoundsForPreviewWidget()
1576 {
1577   int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
1578   SMESH::TPlaneData aPlaneData = myPlanes[aCurPlaneIndex];
1579   SMESH::OrientedPlane* aPlane = aPlaneData.Plane.GetPointer();
1580   SMESH::TActorList anActorList = aPlaneData.ActorList;
1581
1582   double* anOrigin = aPlane->GetOrigin();
1583
1584   double aBounds[6];
1585   SMESH::ComputeBounds( anActorList, aBounds );
1586
1587   bool isBoundsChanged = false;
1588
1589   if( myIsPreviewMoved ) {
1590     // if widget has moved by hand the bounds can to minimize
1591     if( anOrigin[0] > myBounds[0] && anOrigin[0] < aBounds[0] ) {
1592       myBounds[0] = anOrigin[0]; isBoundsChanged = true; }
1593     if( anOrigin[0] < myBounds[1] && anOrigin[0] > aBounds[1] ) {
1594       myBounds[1] = anOrigin[0]; isBoundsChanged = true; }
1595     if( anOrigin[1] > myBounds[2] && anOrigin[1] < aBounds[2] ) {
1596       myBounds[2] = anOrigin[1]; isBoundsChanged = true; }
1597     if( anOrigin[1] < myBounds[3] && anOrigin[1] > aBounds[3] ) {
1598       myBounds[3] = anOrigin[1]; isBoundsChanged = true; }
1599     if( anOrigin[2] > myBounds[4] && anOrigin[2] < aBounds[4] ) {
1600       myBounds[4] = anOrigin[2]; isBoundsChanged = true; }
1601     if( anOrigin[2] < myBounds[5] && anOrigin[2] > aBounds[5] ) {
1602       myBounds[5] = anOrigin[2]; isBoundsChanged = true; }
1603   }
1604   else {
1605     // if widget has moved by dialog data the bounds can to take necessary size
1606     if( anOrigin[0] < aBounds[0] ) {
1607       myBounds[0] = anOrigin[0]; isBoundsChanged = true; }
1608     if( anOrigin[0] > aBounds[1] ) {
1609       myBounds[1] = anOrigin[0]; isBoundsChanged = true; }
1610     if( anOrigin[1] < aBounds[2] ) {
1611       myBounds[2] = anOrigin[1]; isBoundsChanged = true; }
1612     if( anOrigin[1] > aBounds[3] ) {
1613       myBounds[3] = anOrigin[1]; isBoundsChanged = true; }
1614     if( anOrigin[2] < aBounds[4] ) {
1615       myBounds[4] = anOrigin[2]; isBoundsChanged = true; }
1616     if( anOrigin[2] > aBounds[5] ) {
1617         myBounds[5] = anOrigin[2]; isBoundsChanged = true; }
1618   }
1619
1620   if( isBoundsChanged )
1621     myPreviewWidget->PlaceWidget( myBounds[0],myBounds[1],myBounds[2],
1622                                   myBounds[3],myBounds[4],myBounds[5] );
1623   else if( !myIsPreviewMoved )
1624     myPreviewWidget->PlaceWidget( aBounds[0],aBounds[1],aBounds[2],
1625                                   aBounds[3],aBounds[4],aBounds[5] );
1626
1627 }
1628
1629 /*!
1630   Convert absolute coordinates of plane to relative mode
1631 */
1632 void SMESHGUI_ClippingDlg::absolutePlaneToRelative ( double theOrigin[3], double theDir[3] )
1633 {
1634   double aRot[2];
1635
1636   aRot[0] = getRotation1();
1637   aRot[1] = getRotation2();
1638
1639   double eps = 0.0001;
1640
1641   int anOrientation = CBRelativeOrientation->currentIndex();
1642   double aDirection[3];
1643   double aRotation1, aRotation2;
1644   switch( anOrientation ) {
1645   case 0:
1646     aDirection[0] = theDir[0] + eps;
1647     aDirection[1] = theDir[1] + eps;
1648     aDirection[2] = theDir[2] + eps;
1649     aRotation1 = atan2( theDir[2], theDir[1] )*180.0/M_PI;
1650     aRotation2 = atan2( theDir[2], theDir[0] )*180.0/M_PI;
1651     break;
1652   case 1:
1653     aDirection[0] = theDir[1] + eps;
1654     aDirection[1] = theDir[2] + eps;
1655     aDirection[2] = theDir[0] + eps;
1656     aRotation1 = atan2( theDir[0], theDir[2] )*180.0/M_PI;
1657     aRotation2 = atan2( theDir[0], theDir[1] )*180.0/M_PI;
1658     break;
1659   case 2:
1660     aDirection[0] = theDir[2] + eps;
1661     aDirection[1] = theDir[0] + eps;
1662     aDirection[2] = theDir[1] + eps;
1663     aRotation1 = atan2( theDir[1], theDir[0] )*180.0/M_PI;
1664     aRotation2 = atan2( theDir[1], theDir[2] )*180.0/M_PI;
1665     break;
1666   }
1667
1668   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 + 90.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 - 90.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 + 90.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 - 90.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   else if( aDirection[0] < 0 && aDirection[1] > 0 && aDirection[2] > 0 && aRot[0] <= 0 ) {
1685     aRot[0] = aRotation1 - 90.0;  aRot[1] = aRotation2 - 90.0; }
1686   else if( aDirection[0] < 0 && aDirection[1] > 0 && aDirection[2] > 0 && aRot[0] > 0 ) {
1687     aRot[0] = aRotation1 + 90.0;  aRot[1] = aRotation2 - 270.0; }
1688   else if( aDirection[0] < 0 && aDirection[1] > 0 && aDirection[2] < 0 && aRot[0] <= 0 ) {
1689     aRot[0] = aRotation1 - 90.0;  aRot[1] = aRotation2 + 90.0; }
1690   else if( aDirection[0] < 0 && aDirection[1] > 0 && aDirection[2] < 0 && aRot[0] > 0 ) {
1691     aRot[0] = aRotation1 + 90.0;  aRot[1] = aRotation2 + 270.0; }
1692   else if( aDirection[0] < 0 && aDirection[1] < 0 && aDirection[2] > 0 && aRot[0] <= 0  ) {
1693     aRot[0] = aRotation1 - 270.0; aRot[1] = aRotation2 - 270.0; }
1694   else if( aDirection[0] < 0 && aDirection[1] < 0 && aDirection[2] > 0 && aRot[0] > 0 ) {
1695     aRot[0] = aRotation1 - 90.0;  aRot[1] = aRotation2 - 90.0; }
1696   else if( aDirection[0] < 0 && aDirection[1] < 0 && aDirection[2] < 0 && aRot[0] <= 0  ) {
1697     aRot[0] = aRotation1 + 90.0;  aRot[1] = aRotation2 + 270.0; }
1698   else if( aDirection[0] < 0 && aDirection[1] < 0 && aDirection[2] < 0 && aRot[0] > 0 ) {
1699     aRot[0] = aRotation1 + 270.0; aRot[1] = aRotation2 + 90.0; }
1700
1701   SliderRotation1HasMoved( qRound( aRot[0] ) );
1702   SliderRotation1->setValue( qRound( aRot[0] ) );
1703   SliderRotation2HasMoved( qRound( aRot[1] ) );
1704   SliderRotation2->setValue( qRound( aRot[1] ) );
1705
1706   int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
1707   const SMESH::TPlaneData& aPlaneData = myPlanes[ aCurPlaneIndex ];
1708   const SMESH::TActorList& anActorList = aPlaneData.ActorList;
1709
1710   double aBounds[6];
1711   double aDist;
1712   SMESH::ComputeBounds( anActorList, aBounds );
1713   SMESH::PositionToDistance( aBounds, theDir, theOrigin, aDist );
1714   aDist = 1.0 - aDist;
1715   if( aDist>1.0 )
1716     aDist = 1.0;
1717   else if( aDist < 0.0 )
1718     aDist = 0.0;
1719
1720   SliderDistanceHasMoved( qRound( aDist*100 ) );
1721   SliderDistance->setValue( qRound( aDist*100 ) );
1722   return;
1723 }
1724
1725 /*!
1726   SLOT: called on preview check box toggled
1727 */
1728 void SMESHGUI_ClippingDlg::OnPreviewToggle (bool theIsToggled)
1729 {
1730   std::for_each( myPlanes.begin(), myPlanes.end(), TSetVisibility( theIsToggled ) );
1731   int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
1732   SMESH::TPlaneData aPlane = myPlanes[aCurPlaneIndex];
1733   SMESH::OrientedPlane* aPlaneData = aPlane.Plane.GetPointer();
1734   aPlaneData->myActor->SetVisibility( false );
1735   if( theIsToggled )
1736     myPreviewWidget->On();
1737   else
1738     myPreviewWidget->Off();
1739   SMESH::RenderViewWindow( myViewWindow );
1740 }
1741
1742 /*!
1743   SLOT: called on Auto Apply check box toggled
1744 */
1745 void SMESHGUI_ClippingDlg::onAutoApply(bool toggled)
1746 {
1747   if ( toggled ) ClickOnApply();
1748 }
1749
1750 /*!
1751   SLOT on ok button click: sets cutting plane and closes dialog
1752 */
1753 void SMESHGUI_ClippingDlg::ClickOnOk()
1754 {
1755   ClickOnApply();
1756   accept();
1757 }
1758
1759
1760 /*!
1761   SLOT on Apply button click: sets cutting plane and update viewer
1762 */
1763 void SMESHGUI_ClippingDlg::ClickOnApply()
1764 {
1765   if (myViewWindow) {
1766     SUIT_OverrideCursor wc;
1767
1768     QWidget *aCurrWid = this->focusWidget();
1769     aCurrWid->clearFocus();
1770     aCurrWid->setFocus();
1771
1772     SMESHGUI_ClippingPlaneInfoMap& aClippingPlaneInfoMap = mySMESHGUI->getClippingPlaneInfoMap();
1773     SMESHGUI_ClippingPlaneInfoList& aClippingPlaneInfoList = aClippingPlaneInfoMap[ myViewWindow->getViewManager() ];
1774
1775     // clean memory allocated for planes
1776     SMESHGUI_ClippingPlaneInfoList::iterator anIter1 = aClippingPlaneInfoList.begin();
1777     for( ; anIter1 != aClippingPlaneInfoList.end(); anIter1++ )
1778       if( SMESH::OrientedPlane* aPlane = (*anIter1).Plane )
1779         aPlane->Delete();
1780
1781     aClippingPlaneInfoList.clear();
1782
1783     VTK::ActorCollectionCopy aCopy( myViewWindow->getRenderer()->GetActors() );
1784     vtkActorCollection* anAllActors = aCopy.GetActors();
1785     anAllActors->InitTraversal();
1786     while( vtkActor* aVTKActor = anAllActors->GetNextActor() )
1787       if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) )
1788         anActor->RemoveAllClippingPlanes();
1789
1790     SMESH::TPlaneDataVector::iterator anIter2 = myPlanes.begin();
1791     for( ; anIter2 != myPlanes.end(); anIter2++ ) {
1792       SMESH::TPlaneData aPlaneData = *anIter2;
1793       SMESH::TPlane aPlane = aPlaneData.Plane;
1794       SMESH::TActorList anActorList = aPlaneData.ActorList;
1795
1796       // the check is disabled to support planes with empty actor list
1797       //if( anActorList.empty() )
1798       //  continue;
1799
1800       SMESH::OrientedPlane* anOrientedPlane = SMESH::OrientedPlane::New(myViewWindow);
1801       anOrientedPlane->ShallowCopy(aPlane.GetPointer());
1802       SMESH::TActorList::iterator anIter3 = anActorList.begin();
1803       for( ; anIter3 != anActorList.end(); anIter3++ )
1804         if( vtkActor* aVTKActor = *anIter3 )
1805           if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) ) {
1806                 if( anOrientedPlane->IsOpenGLClipping )
1807               anActor->AddOpenGLClippingPlane( anOrientedPlane->InvertPlane() );
1808                 else
1809                         anActor->AddClippingPlane( anOrientedPlane );
1810           }
1811
1812       SMESH::ClippingPlaneInfo aClippingPlaneInfo;
1813       aClippingPlaneInfo.Plane = anOrientedPlane;
1814       aClippingPlaneInfo.ActorList = anActorList;
1815
1816       aClippingPlaneInfoList.push_back( aClippingPlaneInfo );
1817     }
1818
1819     SMESH_Actor* anSMESHActor;
1820     anAllActors->InitTraversal();
1821     while( vtkActor* aVTKActor = anAllActors->GetNextActor() )
1822       if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) ) {
1823         anSMESHActor = anActor;
1824         anActor->SetOpenGLClippingPlane();
1825       }
1826
1827     SMESH::RenderViewWindow( myViewWindow );
1828   }
1829 }
1830
1831 /*!
1832   SLOT on help button click: opens a help page
1833 */
1834 void SMESHGUI_ClippingDlg::ClickOnHelp()
1835 {
1836   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
1837   if (app)
1838     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
1839   else {
1840                 QString platform;
1841 #ifdef WIN32
1842                 platform = "winapplication";
1843 #else
1844                 platform = "application";
1845 #endif
1846     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
1847                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
1848                              arg(app->resourceMgr()->stringValue("ExternalBrowser",
1849                                                                  platform)).
1850                              arg(myHelpFileName));
1851   }
1852 }
1853
1854 /*!
1855   SLOT: Called when value of slider distance change
1856 */
1857 void SMESHGUI_ClippingDlg::SliderDistanceHasMoved( int value )
1858 {
1859   double new_value = value/100.;
1860   TLValueDistance->setText( QString("%1").arg( new_value ) );
1861   SetCurrentPlaneParam();
1862 }
1863
1864 /*!
1865   SLOT: Called when value of slider rotation1 change
1866 */
1867 void SMESHGUI_ClippingDlg::SliderRotation1HasMoved( int value )
1868 {
1869   TLValueRotation1->setText( QString("%1\xB0").arg( value ) );
1870   SetCurrentPlaneParam();
1871 }
1872
1873 /*!
1874   SLOT: Called when value of slider rotation2 change
1875 */
1876 void SMESHGUI_ClippingDlg::SliderRotation2HasMoved( int value )
1877 {
1878   TLValueRotation2->setText( QString("%1\xB0").arg( value ) );
1879   SetCurrentPlaneParam();
1880 }
1881
1882 void SMESHGUI_ClippingDlg::onSelectAbsoluteOrientation( int mode )
1883 {
1884   bool isUserMode = (mode==0);
1885
1886   TextLabelX->setEnabled( isUserMode );
1887   TextLabelY->setEnabled( isUserMode );
1888   TextLabelZ->setEnabled( isUserMode );
1889
1890   SpinBox_X->setEnabled( isUserMode );
1891   SpinBox_Y->setEnabled( isUserMode );
1892   SpinBox_Z->setEnabled( isUserMode );
1893
1894   TextLabelDx->setEnabled( isUserMode );
1895   TextLabelDy->setEnabled( isUserMode );
1896   TextLabelDz->setEnabled( isUserMode );
1897
1898   SpinBox_Dx->setEnabled( isUserMode );
1899   SpinBox_Dy->setEnabled( isUserMode );
1900   SpinBox_Dz->setEnabled( isUserMode );
1901
1902   if ( isUserMode )
1903     return;
1904
1905   double aDx = 0, aDy = 0, aDz = 0;
1906
1907   if ( mode == 1 )
1908   {
1909     aDz = 1;
1910     TextLabelZ->setEnabled( true );
1911     SpinBox_Z->setEnabled( true );
1912     SpinBox_Z->setFocus();
1913   }
1914   else if ( mode == 2 )
1915   {
1916     aDx = 1;
1917     TextLabelX->setEnabled( true );
1918     SpinBox_X->setEnabled( true );
1919     SpinBox_X->setFocus();
1920   }
1921   else if ( mode == 3 )
1922   {
1923     aDy = 1;
1924     TextLabelY->setEnabled( true );
1925     SpinBox_Y->setEnabled( true );
1926     SpinBox_Y->setFocus();
1927   }
1928
1929   int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
1930   SMESH::TPlaneData aPlane = myPlanes[aCurPlaneIndex];
1931   SMESH::OrientedPlane* aPlaneData = aPlane.Plane.GetPointer();
1932
1933   if ( aPlaneData->IsInvert == true ) {
1934     aDx = -aDx; aDy = -aDy; aDz = -aDz;
1935   }
1936
1937   myIsSelectPlane = true;
1938   SpinBox_Dx->setValue( aDx );
1939   SpinBox_Dy->setValue( aDy );
1940   SpinBox_Dz->setValue( aDz );
1941   myIsSelectPlane = false;
1942
1943   SetCurrentPlaneParam();
1944 }
1945
1946 /*!
1947   SLOT: called on orientation of clipping plane in relative mode changed
1948 */
1949 void SMESHGUI_ClippingDlg::onSelectRelativeOrientation ( int theItem )
1950 {
1951   if ( myPlanes.empty() )
1952     return;
1953
1954   if ( theItem == 0 ) {
1955     TextLabelRotation1->setText( tr( "ROTATION_AROUND_X_Y2Z" ) );
1956     TextLabelRotation2->setText( tr( "ROTATION_AROUND_Y_X2Z" ) );
1957   }
1958   else if ( theItem == 1 ) {
1959     TextLabelRotation1->setText( tr( "ROTATION_AROUND_Y_Z2X" ) );
1960     TextLabelRotation2->setText( tr( "ROTATION_AROUND_Z_Y2X" ) );
1961   }
1962   else if ( theItem == 2 ) {
1963     TextLabelRotation1->setText( tr( "ROTATION_AROUND_Z_X2Y" ) );
1964     TextLabelRotation2->setText( tr( "ROTATION_AROUND_X_Z2Y" ) );
1965   }
1966
1967   if( (QComboBox*)sender() == CBRelativeOrientation )
1968     SetCurrentPlaneParam();
1969 }
1970
1971 /*!
1972   SLOT on reset button click: sets default values
1973 */
1974 void SMESHGUI_ClippingDlg::onReset()
1975 {
1976   myIsSelectPlane = true;
1977   SpinBox_X->setValue(0);
1978   SpinBox_Y->setValue(0);
1979   SpinBox_Z->setValue(0);
1980   myIsSelectPlane = false;
1981
1982   SetCurrentPlaneParam();
1983 }
1984
1985 /*!
1986   SLOT on invert button click: inverts normal of cutting plane
1987 */
1988 void SMESHGUI_ClippingDlg::onInvert()
1989 {
1990   double Dx = SpinBox_Dx->value();
1991   double Dy = SpinBox_Dy->value();
1992   double Dz = SpinBox_Dz->value();
1993
1994   myIsSelectPlane = true;
1995   SpinBox_Dx->setValue( -Dx );
1996   SpinBox_Dy->setValue( -Dy );
1997   SpinBox_Dz->setValue( -Dz );
1998   myIsSelectPlane = false;
1999
2000   if ( !myPlanes.empty() ) {
2001     int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
2002     SMESH::TPlaneData aPlane = myPlanes[aCurPlaneIndex];
2003     SMESH::OrientedPlane* aPlaneData = aPlane.Plane.GetPointer();
2004     aPlaneData->IsInvert = !aPlaneData->IsInvert;
2005   }
2006   SetCurrentPlaneParam();
2007 }
2008