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