Salome HOME
23395: EDF 13855 - Crash SALOME when creating a mesh
[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   std::map< std::string, QListWidgetItem* > itemMap; // used to sort items by entry
1090
1091   VTK::ActorCollectionCopy aCopy( myViewWindow->getRenderer()->GetActors() );
1092   vtkActorCollection* anAllActors = aCopy.GetActors();
1093   anAllActors->InitTraversal();
1094   while( vtkActor* aVTKActor = anAllActors->GetNextActor() ) {
1095     if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) ) {
1096       if( anActor->hasIO() ) {
1097         Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
1098         if( _PTR(SObject) aSObj = aStudy->FindObjectID( anIO->getEntry() ) ) {
1099           bool anIsChecked = false;
1100           SMESH::TActorList::const_iterator anIter = anActorList.begin();
1101           for ( ; anIter != anActorList.end(); anIter++ ) {
1102             if( vtkActor* aVTKActorRef = *anIter ) {
1103               if( SMESH_Actor* anActorRef = SMESH_Actor::SafeDownCast( aVTKActorRef ) ) {
1104                 if( anActorRef == anActor ) {
1105                   anIsChecked = true;
1106                   break;
1107                 }
1108               }
1109             }
1110           }
1111           QString aName = QString( aSObj->GetName().c_str() );
1112           QString aFatherName = getFatherName( aSObj );
1113           if ( !aFatherName.isEmpty() )
1114             aName = aFatherName + " / " + aName;
1115           aName += QString(" (%1)").arg( aSObj->GetID().c_str() );
1116           QListWidgetItem* anItem = new ActorItem( anActor, aName, 0 );
1117           anItem->setCheckState( anIsChecked ? Qt::Checked : Qt::Unchecked );
1118           itemMap.insert( std::make_pair( aSObj->GetID(), anItem ));
1119         }
1120       }
1121     }
1122   }
1123   std::map< std::string, QListWidgetItem* >::iterator s2i = itemMap.begin();
1124   for ( ; s2i != itemMap.end(); ++s2i )
1125   {
1126     QListWidgetItem* anItem = s2i->second;
1127     ActorList->addItem( anItem );
1128   }
1129   updateActorItem( 0, true, false );
1130 }
1131
1132 /*!
1133   Update an actor in actor's list
1134 */
1135 void SMESHGUI_ClippingDlg::updateActorItem( QListWidgetItem* theItem,
1136                                             bool theUpdateSelectAll,
1137                                             bool theUpdateClippingPlaneMap )
1138 {
1139   // update Select All check box
1140   if( theUpdateSelectAll ) {
1141     int aNbItems = ActorList->count(), aNbChecked = 0;
1142     for( int i = 0; i < aNbItems; i++ )
1143       if( QListWidgetItem* anItem = ActorList->item( i ) )
1144         if( anItem->checkState() == Qt::Checked )
1145           aNbChecked++;
1146
1147     bool anIsBlocked = SelectAllCheckBox->blockSignals( true );
1148     SelectAllCheckBox->setCheckState( aNbChecked == aNbItems ? Qt::Checked : Qt::Unchecked);
1149     SelectAllCheckBox->blockSignals( anIsBlocked );
1150   }
1151
1152   // update clipping plane map
1153   if( theUpdateClippingPlaneMap ) {
1154     int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
1155     if( ActorItem* anItem = dynamic_cast<ActorItem*>( theItem ) ) {
1156       if( SMESH_Actor* anActor = anItem->getActor() ) {
1157         SMESH::TPlaneData& aPlaneData = myPlanes[ aCurPlaneIndex ];
1158         SMESH::TActorList& anActorList = aPlaneData.ActorList;
1159         bool anIsPushed = false;
1160         SMESH::TActorList::iterator anIter = anActorList.begin();
1161         for ( ; anIter != anActorList.end(); anIter++ ) {
1162           if( anActor == *anIter ) {
1163             anIsPushed = true;
1164             break;
1165           }
1166         }
1167         if( theItem->checkState() == Qt::Checked && !anIsPushed )
1168           anActorList.push_back( anActor );
1169         else if( theItem->checkState() == Qt::Unchecked && anIsPushed )
1170           anActorList.remove( anActor );
1171
1172         if( SMESH::ComputeBounds( anActorList, myBounds ) ) {
1173           myPreviewWidget->PlaceWidget( myBounds[0], myBounds[1], myBounds[2],
1174                                         myBounds[3], myBounds[4], myBounds[5] );
1175           if( PreviewCheckBox->isChecked() )
1176             myPreviewWidget->On();
1177         }
1178         else
1179           myPreviewWidget->Off();
1180       }
1181     }
1182   }
1183 }
1184
1185 /*!
1186   Get the list of current actors
1187 */
1188 SMESH::TActorList SMESHGUI_ClippingDlg::getCurrentActors()
1189 {
1190   SMESH::TActorList anActorList;
1191   for( int i = 0, n = ActorList->count(); i < n; i++ )
1192     if( ActorItem* anItem = dynamic_cast<ActorItem*>( ActorList->item( i ) ) )
1193       if( anItem->checkState() == Qt::Checked )
1194         if( SMESH_Actor* anActor = anItem->getActor() )
1195           anActorList.push_back( anActor );
1196   return anActorList;
1197 }
1198
1199 /*!
1200   Dump the parameters of clipping planes
1201 */
1202 void SMESHGUI_ClippingDlg::dumpPlaneData() const
1203 {
1204   printf( "----------- Plane Data -----------\n" );
1205   int anId = 1;
1206   SMESH::TPlaneDataVector::const_iterator anIter1 = myPlanes.begin();
1207   for ( ; anIter1 != myPlanes.end(); anIter1++, anId++ ) {
1208     SMESH::TPlaneData aPlaneData = *anIter1;
1209     SMESH::TPlane aPlane = aPlaneData.Plane;
1210     double* aNormal = aPlane->GetNormal();
1211     double* anOrigin = aPlane->GetOrigin();
1212     printf( "Plane N%d:\n", anId );
1213     printf( "  Normal = ( %f, %f, %f )\n", aNormal[0], aNormal[1], aNormal[2] );
1214     printf( "  Origin = ( %f, %f, %f )\n", anOrigin[0], anOrigin[1], anOrigin[2] );
1215
1216     SMESH::TActorList anActorList = aPlaneData.ActorList;
1217     SMESH::TActorList::const_iterator anIter2 = anActorList.begin();
1218     for ( ; anIter2 != anActorList.end(); anIter2++ ) {
1219       if( vtkActor* aVTKActor = *anIter2 ) {
1220         if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) )
1221           printf( "  - Actor: '%s'\n", anActor->getName() );
1222       }
1223       else
1224         printf( "  - Actor: NULL\n");
1225     }
1226   }
1227   printf( "----------------------------------\n" );
1228 }
1229
1230 /*!
1231   SLOT on close button click: rejects dialog
1232 */
1233 void SMESHGUI_ClippingDlg::reject()
1234 {
1235   //here we can insert actions to do at close.
1236   QDialog::reject();
1237 }
1238
1239 /*!
1240   Set absolute mode of clipping plane
1241 */
1242 void SMESHGUI_ClippingDlg::onModeAbsolute()
1243 {
1244   ModeStackedLayout->setCurrentIndex(0);
1245   CurrentMode = SMESH::Absolute;
1246   ClickOnNew();
1247   SetCurrentPlaneParam();
1248 }
1249
1250 /*!
1251   Set relative mode of clipping plane
1252 */
1253 void SMESHGUI_ClippingDlg::onModeRelative()
1254 {
1255   ModeStackedLayout->setCurrentIndex(1);
1256   CurrentMode = SMESH::Relative;
1257   ClickOnNew();
1258   SetCurrentPlaneParam();
1259 }
1260
1261 /*!
1262   SLOT on new button click: create a new clipping plane
1263 */
1264 void SMESHGUI_ClippingDlg::ClickOnNew()
1265 {
1266   if(myViewWindow) {
1267     SMESH::OrientedPlane* aPlane = SMESH::OrientedPlane::New(myViewWindow);
1268     SMESH::TPlane aTPlane(aPlane);
1269     aPlane->PlaneMode = CurrentMode;
1270     SMESH::TActorList anActorList, aVisibleActorList;
1271     VTK::ActorCollectionCopy aCopy( myViewWindow->getRenderer()->GetActors() );
1272     vtkActorCollection* anAllActors = aCopy.GetActors();
1273     anAllActors->InitTraversal();
1274     while( vtkActor* aVTKActor = anAllActors->GetNextActor() )
1275       if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) )
1276       {
1277         anActorList.push_back( anActor );
1278         if ( anActor->GetVisibility() )
1279           aVisibleActorList.push_back( anActor );
1280       }
1281     SMESH::TPlaneData aPlaneData(aTPlane,
1282                                  aVisibleActorList.empty() ? anActorList : aVisibleActorList);
1283     myPlanes.push_back(aPlaneData);
1284
1285
1286     std::for_each( myPlanes.begin(),myPlanes.end(), TSetVisibility( PreviewCheckBox->isChecked() ) );
1287     aPlane->myActor->SetVisibility( false );
1288
1289     bool anIsBlocked = ActorList->blockSignals( true );
1290
1291     if( SMESH::ComputeBounds( anActorList, myBounds ) ) {
1292       myPreviewWidget->PlaceWidget( myBounds[0], myBounds[1], myBounds[2],
1293                                     myBounds[3], myBounds[4], myBounds[5] );
1294       if( PreviewCheckBox->isChecked() )
1295         myPreviewWidget->On();
1296     }
1297     else
1298       myPreviewWidget->Off();
1299
1300     synchronize();
1301     SetCurrentPlaneParam();
1302
1303     ActorList->blockSignals( anIsBlocked );
1304   }
1305 }
1306
1307 /*!
1308   SLOT on delete button click: Delete selected clipping plane
1309 */
1310 void SMESHGUI_ClippingDlg::ClickOnDelete()
1311 {
1312   if (myPlanes.empty())
1313     return;
1314
1315   int aPlaneIndex = ComboBoxPlanes->currentIndex();
1316
1317   SMESH::TPlaneDataVector::iterator anIter = myPlanes.begin() + aPlaneIndex;
1318   SMESH::TPlaneData aPlaneData = *anIter;
1319   aPlaneData.Plane.GetPointer()->myActor->SetVisibility(false);
1320   myPlanes.erase(anIter);
1321
1322   if(AutoApplyCheckBox->isChecked())
1323     ClickOnApply();
1324
1325   synchronize();
1326   SMESH::RenderViewWindow( myViewWindow );
1327 }
1328
1329 /*!
1330   Set current parameters of selected plane
1331 */
1332 void SMESHGUI_ClippingDlg::onSelectPlane ( int theIndex )
1333 {
1334   if ( myPlanes.empty() )
1335     return;
1336
1337   SMESH::TPlaneData aPlaneData = myPlanes[theIndex];
1338   SMESH::OrientedPlane* aPlane = aPlaneData.Plane.GetPointer();
1339
1340   myIsSelectPlane = true;
1341   isOpenGLClipping->setChecked( aPlane->IsOpenGLClipping );
1342
1343   if ( aPlane->PlaneMode == SMESH::Absolute ) {
1344     ModeStackedLayout->setCurrentIndex( 0 );
1345     CurrentMode = SMESH::Absolute;
1346     int anOrientation = aPlane->myAbsoluteOrientation;
1347     // Set plane parameters in the dialog
1348     double anOrigin[3], aDir[3];
1349     anOrigin[0] = aPlane->X;
1350     anOrigin[1] = aPlane->Y;
1351     anOrigin[2] = aPlane->Z;
1352     setOrigin( anOrigin );
1353     aDir[0] = aPlane->Dx;
1354     aDir[1] = aPlane->Dy;
1355     aDir[2] = aPlane->Dz;
1356     setDirection( aDir );
1357     CBAbsoluteOrientation->setCurrentIndex( anOrientation );
1358     onSelectAbsoluteOrientation( anOrientation );
1359   }
1360   else if ( aPlane->PlaneMode == SMESH::Relative ) {
1361     ModeStackedLayout->setCurrentIndex( 1 );
1362     CurrentMode = SMESH::Relative;
1363     SMESH::Orientation anOrientation = aPlane->GetOrientation();
1364     double aRot[2] = { aPlane->myAngle[0], aPlane->myAngle[1] };
1365     // Set plane parameters in the dialog
1366     setDistance( aPlane->GetDistance() );
1367     setRotation( aRot[0], aRot[1] );
1368     switch ( anOrientation ) {
1369     case SMESH::XY:
1370       CBRelativeOrientation->setCurrentIndex(0);
1371       onSelectRelativeOrientation(0);
1372       break;
1373     case SMESH::YZ:
1374       CBRelativeOrientation->setCurrentIndex(1);
1375       onSelectRelativeOrientation(1);
1376       break;
1377     case SMESH::ZX:
1378       CBRelativeOrientation->setCurrentIndex(2);
1379       onSelectRelativeOrientation(2);
1380       break;
1381     }
1382   }
1383   myIsSelectPlane = false;
1384
1385   if( SMESH::ComputeBounds( aPlaneData.ActorList, myBounds ) ) {
1386     myPreviewWidget->PlaceWidget( myBounds[0], myBounds[1], myBounds[2],
1387                                   myBounds[3], myBounds[4], myBounds[5] );
1388     if( PreviewCheckBox->isChecked() )
1389       myPreviewWidget->On();
1390   }
1391   else
1392     myPreviewWidget->Off();
1393
1394   SetCurrentPlaneParam();
1395
1396   // Actors
1397   bool anIsBlocked = ActorList->blockSignals( true );
1398   updateActorList();
1399   ActorList->blockSignals( anIsBlocked );
1400 }
1401
1402 /*!
1403   SLOT: called on OpenGLClipping check box toggled
1404 */
1405 void SMESHGUI_ClippingDlg::onIsOpenGLClipping( bool toggled )
1406 {
1407   if ( myPlanes.empty() || myIsSelectPlane )
1408     return;
1409
1410   int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
1411   SMESH::TPlaneData aPlane = myPlanes[aCurPlaneIndex];
1412   SMESH::OrientedPlane* aPlaneData = aPlane.Plane.GetPointer();
1413   aPlaneData->IsOpenGLClipping = toggled;
1414
1415   if( AutoApplyCheckBox->isChecked() )
1416     ClickOnApply();
1417 }
1418
1419 /*!
1420   SLOT: called on SelectAll check box toggled
1421 */
1422 void SMESHGUI_ClippingDlg::onSelectAll( int theState )
1423 {
1424   if( theState == Qt::PartiallyChecked ) {
1425     SelectAllCheckBox->setCheckState( Qt::Checked );
1426     return;
1427   }
1428
1429   bool anIsBlocked = ActorList->blockSignals( true );
1430   for( int i = 0, n = ActorList->count(); i < n; i++ ) {
1431     if( QListWidgetItem* anItem = ActorList->item( i ) ) {
1432       anItem->setCheckState( theState == Qt::Checked ? Qt::Checked : Qt::Unchecked );
1433       updateActorItem( anItem, false, true );
1434     }
1435   }
1436   SelectAllCheckBox->setTristate( false );
1437   ActorList->blockSignals( anIsBlocked );
1438   SetCurrentPlaneParam();
1439 }
1440
1441 /*!
1442   SLOT: called when actor item was changed
1443 */
1444 void SMESHGUI_ClippingDlg::onActorItemChanged( QListWidgetItem* theItem )
1445 {
1446   updateActorItem( theItem, true, true );
1447   SetCurrentPlaneParam();
1448 }
1449
1450 /*!
1451   Restore parameters of selected plane
1452 */
1453 void SMESHGUI_ClippingDlg::SetCurrentPlaneParam()
1454 {
1455   if ( myPlanes.empty() || myIsSelectPlane )
1456     return;
1457
1458   int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
1459
1460   SMESH::TPlaneData aPlaneData = myPlanes[aCurPlaneIndex];
1461   SMESH::OrientedPlane* aPlane = aPlaneData.Plane.GetPointer();
1462
1463   if ( aPlane->PlaneMode == SMESH::Absolute ) {
1464     aPlane->myAbsoluteOrientation = CBAbsoluteOrientation->currentIndex();
1465     aPlane->X = SpinBox_X->value();
1466     aPlane->Y = SpinBox_Y->value();
1467     aPlane->Z = SpinBox_Z->value();
1468     aPlane->Dx = SpinBox_Dx->value();
1469     aPlane->Dy = SpinBox_Dy->value();
1470     aPlane->Dz = SpinBox_Dz->value();
1471   }
1472
1473   double aNormal[3];
1474   double aDir[2][3] = { {0, 0, 0}, {0, 0, 0} };
1475   static double aCoeff = vtkMath::Pi()/180.0;
1476
1477   double aRot[2] = { getRotation1(), getRotation2() };
1478   int anOrient = 0;
1479   if ( aPlane->PlaneMode == SMESH::Absolute )
1480     anOrient = CBAbsoluteOrientation->currentIndex();
1481   else if ( aPlane->PlaneMode == SMESH::Relative )
1482     anOrient = CBRelativeOrientation->currentIndex() + 1;
1483
1484   if ( aPlane->PlaneMode == SMESH::Relative ) {
1485     aPlane->myAngle[0] = aRot[0];
1486     aPlane->myAngle[1] = aRot[1];
1487     aPlane->SetOrientation( SMESH::Orientation( CBRelativeOrientation->currentIndex() ) );
1488     aPlane->SetDistance( getDistance() );
1489   }
1490
1491   if ( anOrient == 0 ) {
1492     // compute a direction for plane in absolute mode
1493     double znam = sqrt( aPlane->Dx*aPlane->Dx + aPlane->Dy*aPlane->Dy + aPlane->Dz*aPlane->Dz );
1494     double aRotation = acos( aPlane->Dy/znam )/aCoeff;
1495     if ( aPlane->Dy >= 0.0 && aPlane->Dz >= 0.0 )     aRot[0] = 90.0 + aRotation;
1496     else if ( aPlane->Dy >= 0.0 && aPlane->Dz < 0.0 ) aRot[0] = 90.0 - aRotation;
1497     else if ( aPlane->Dy < 0.0 && aPlane->Dz >= 0.0 ) aRot[0] = aRotation - 90.0;
1498     else if ( aPlane->Dy < 0.0 && aPlane->Dz < 0.0 )  aRot[0] = 270.0 - aRotation;
1499
1500     aRotation = acos( aPlane->Dx/znam )/aCoeff;
1501     if ( aPlane->Dx >= 0.0 && aPlane->Dz >= 0.0 )     aRot[1] = 90.0 + aRotation;
1502     else if ( aPlane->Dx >= 0.0 && aPlane->Dz < 0.0 ) aRot[1] = 90.0 - aRotation;
1503     else if ( aPlane->Dx < 0.0 && aPlane->Dz >= 0.0 ) aRot[1] = aRotation - 90.0;
1504     else if ( aPlane->Dx < 0.0 && aPlane->Dz < 0.0 )  aRot[1] = 270.0 - aRotation;
1505   }
1506
1507   // compute a normal
1508   rotationToNormal( aRot, anOrient, aNormal, aDir );
1509
1510
1511   SMESH::TActorList anActorList = aPlaneData.ActorList;
1512
1513   double aBounds[6];
1514   double anOrigin[3];
1515   double aDistance;
1516   aDistance = getDistance();
1517   if ( aPlane->PlaneMode == SMESH::Absolute ) {
1518     aNormal[0] = aPlane->Dx;
1519     aNormal[1] = aPlane->Dy;
1520     aNormal[2] = aPlane->Dz;
1521   }
1522
1523   bool anIsOk = SMESH::ComputeClippingPlaneParameters( anActorList,
1524                                                        aNormal,
1525                                                        aDistance,
1526                                                        aBounds,
1527                                                        anOrigin );
1528
1529   if ( aPlane->PlaneMode == SMESH::Absolute ) {
1530     anOrigin[0] = aPlane->X;
1531     anOrigin[1] = aPlane->Y;
1532     anOrigin[2] = aPlane->Z;
1533   }
1534
1535   if( anIsOk ) {
1536     aPlane->SetNormal( aNormal );
1537     aPlane->SetOrigin( anOrigin );
1538
1539     double aPnt[3] = { ( aBounds[0] + aBounds[1] ) / 2.,
1540                        ( aBounds[2] + aBounds[3] ) / 2.,
1541                        ( aBounds[4] + aBounds[5] ) / 2. };
1542
1543     double aDel = pow( pow( aBounds[1] - aBounds[0], 2 ) +
1544                        pow( aBounds[3] - aBounds[2], 2 ) +
1545                        pow( aBounds[5] - aBounds[4], 2 ), 0.5 );
1546
1547     double aDelta[2][3] = { { aDir[0][0]*aDel, aDir[0][1]*aDel, aDir[0][2]*aDel },
1548                             { aDir[1][0]*aDel, aDir[1][1]*aDel, aDir[1][2]*aDel } };
1549     double aParam, aPnt0[3], aPnt1[3], aPnt2[3];
1550
1551     double aPnt01[3] = { aPnt[0] - aDelta[0][0] - aDelta[1][0],
1552                          aPnt[1] - aDelta[0][1] - aDelta[1][1],
1553                          aPnt[2] - aDelta[0][2] - aDelta[1][2] };
1554     double aPnt02[3] = { aPnt01[0] + aNormal[0],
1555                          aPnt01[1] + aNormal[1],
1556                          aPnt01[2] + aNormal[2] };
1557     vtkPlane::IntersectWithLine(aPnt01,aPnt02,aNormal,anOrigin,aParam,aPnt0);
1558
1559     double aPnt11[3] = { aPnt[0] - aDelta[0][0] + aDelta[1][0],
1560                          aPnt[1] - aDelta[0][1] + aDelta[1][1],
1561                          aPnt[2] - aDelta[0][2] + aDelta[1][2] };
1562     double aPnt12[3] = { aPnt11[0] + aNormal[0],
1563                          aPnt11[1] + aNormal[1],
1564                          aPnt11[2] + aNormal[2] };
1565     vtkPlane::IntersectWithLine(aPnt11,aPnt12,aNormal,anOrigin,aParam,aPnt1);
1566
1567     double aPnt21[3] = { aPnt[0] + aDelta[0][0] - aDelta[1][0],
1568                          aPnt[1] + aDelta[0][1] - aDelta[1][1],
1569                          aPnt[2] + aDelta[0][2] - aDelta[1][2] };
1570     double aPnt22[3] = { aPnt21[0] + aNormal[0],
1571                          aPnt21[1] + aNormal[1],
1572                          aPnt21[2] + aNormal[2] };
1573     vtkPlane::IntersectWithLine(aPnt21,aPnt22,aNormal,anOrigin,aParam,aPnt2);
1574
1575     vtkPlaneSource* aPlaneSource = aPlane->myPlaneSource;
1576
1577     aPlaneSource->SetNormal( aNormal[0], aNormal[1], aNormal[2] );
1578     aPlaneSource->SetOrigin( aPnt0[0], aPnt0[1], aPnt0[2] );
1579     aPlaneSource->SetPoint1( aPnt1[0], aPnt1[1], aPnt1[2] );
1580     aPlaneSource->SetPoint2( aPnt2[0], aPnt2[1], aPnt2[2] );
1581     aPlaneSource->Update();
1582   }
1583
1584   setBoundsForPreviewWidget();
1585
1586   myPreviewWidget->SetOrigin( anOrigin );
1587   myPreviewWidget->SetNormal( aNormal );
1588
1589   if(AutoApplyCheckBox->isChecked())
1590     ClickOnApply();
1591
1592   SMESH::RenderViewWindow( myViewWindow );
1593 }
1594
1595 /*!
1596   Set current bounds for preview widget
1597 */
1598 void SMESHGUI_ClippingDlg::setBoundsForPreviewWidget()
1599 {
1600   int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
1601   SMESH::TPlaneData aPlaneData = myPlanes[aCurPlaneIndex];
1602   SMESH::OrientedPlane* aPlane = aPlaneData.Plane.GetPointer();
1603   SMESH::TActorList anActorList = aPlaneData.ActorList;
1604
1605   double* anOrigin = aPlane->GetOrigin();
1606
1607   double aBounds[6];
1608   SMESH::ComputeBounds( anActorList, aBounds );
1609
1610   bool isBoundsChanged = false;
1611
1612   if( myIsPreviewMoved ) {
1613     // if widget has moved by hand the bounds can to minimize
1614     if( anOrigin[0] > myBounds[0] && anOrigin[0] < aBounds[0] ) {
1615       myBounds[0] = anOrigin[0]; isBoundsChanged = true; }
1616     if( anOrigin[0] < myBounds[1] && anOrigin[0] > aBounds[1] ) {
1617       myBounds[1] = anOrigin[0]; isBoundsChanged = true; }
1618     if( anOrigin[1] > myBounds[2] && anOrigin[1] < aBounds[2] ) {
1619       myBounds[2] = anOrigin[1]; isBoundsChanged = true; }
1620     if( anOrigin[1] < myBounds[3] && anOrigin[1] > aBounds[3] ) {
1621       myBounds[3] = anOrigin[1]; isBoundsChanged = true; }
1622     if( anOrigin[2] > myBounds[4] && anOrigin[2] < aBounds[4] ) {
1623       myBounds[4] = anOrigin[2]; isBoundsChanged = true; }
1624     if( anOrigin[2] < myBounds[5] && anOrigin[2] > aBounds[5] ) {
1625       myBounds[5] = anOrigin[2]; isBoundsChanged = true; }
1626   }
1627   else {
1628     // if widget has moved by dialog data the bounds can to take necessary size
1629     if( anOrigin[0] < aBounds[0] ) {
1630       myBounds[0] = anOrigin[0]; isBoundsChanged = true; }
1631     if( anOrigin[0] > aBounds[1] ) {
1632       myBounds[1] = anOrigin[0]; isBoundsChanged = true; }
1633     if( anOrigin[1] < aBounds[2] ) {
1634       myBounds[2] = anOrigin[1]; isBoundsChanged = true; }
1635     if( anOrigin[1] > aBounds[3] ) {
1636       myBounds[3] = anOrigin[1]; isBoundsChanged = true; }
1637     if( anOrigin[2] < aBounds[4] ) {
1638       myBounds[4] = anOrigin[2]; isBoundsChanged = true; }
1639     if( anOrigin[2] > aBounds[5] ) {
1640         myBounds[5] = anOrigin[2]; isBoundsChanged = true; }
1641   }
1642
1643   if( isBoundsChanged )
1644     myPreviewWidget->PlaceWidget( myBounds[0],myBounds[1],myBounds[2],
1645                                   myBounds[3],myBounds[4],myBounds[5] );
1646   else if( !myIsPreviewMoved )
1647     myPreviewWidget->PlaceWidget( aBounds[0],aBounds[1],aBounds[2],
1648                                   aBounds[3],aBounds[4],aBounds[5] );
1649
1650 }
1651
1652 /*!
1653   Convert absolute coordinates of plane to relative mode
1654 */
1655 void SMESHGUI_ClippingDlg::absolutePlaneToRelative ( double theOrigin[3], double theDir[3] )
1656 {
1657   double aRot[2];
1658
1659   aRot[0] = getRotation1();
1660   aRot[1] = getRotation2();
1661
1662   double eps = 0.0001;
1663
1664   int anOrientation = CBRelativeOrientation->currentIndex();
1665   double aDirection[3] = { 0.,0.,0. };
1666   double aRotation1 = 0, aRotation2 = 0;
1667   switch( anOrientation ) {
1668   case 0:
1669     aDirection[0] = theDir[0] + eps;
1670     aDirection[1] = theDir[1] + eps;
1671     aDirection[2] = theDir[2] + eps;
1672     aRotation1 = atan2( theDir[2], theDir[1] )*180.0/M_PI;
1673     aRotation2 = atan2( theDir[2], theDir[0] )*180.0/M_PI;
1674     break;
1675   case 1:
1676     aDirection[0] = theDir[1] + eps;
1677     aDirection[1] = theDir[2] + eps;
1678     aDirection[2] = theDir[0] + eps;
1679     aRotation1 = atan2( theDir[0], theDir[2] )*180.0/M_PI;
1680     aRotation2 = atan2( theDir[0], theDir[1] )*180.0/M_PI;
1681     break;
1682   case 2:
1683     aDirection[0] = theDir[2] + eps;
1684     aDirection[1] = theDir[0] + eps;
1685     aDirection[2] = theDir[1] + eps;
1686     aRotation1 = atan2( theDir[1], theDir[0] )*180.0/M_PI;
1687     aRotation2 = atan2( theDir[1], theDir[2] )*180.0/M_PI;
1688     break;
1689   }
1690
1691   if( aDirection[0] > 0 && aDirection[1] > 0 && aDirection[2] > 0 && aRot[0] <= 0 ) {
1692     aRot[0] = aRotation1 - 90.0;  aRot[1] = aRotation2 - 90.0; }
1693   else if( aDirection[0] > 0 && aDirection[1] > 0 && aDirection[2] > 0 && aRot[0] > 0 ) {
1694     aRot[0] = aRotation1 + 90.0;  aRot[1] = aRotation2 + 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 - 90.0; }
1699   else if( aDirection[0] > 0 && aDirection[1] < 0 && aDirection[2] > 0 && aRot[0] <= 0 ) {
1700     aRot[0] = aRotation1 - 270.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 - 90.0; }
1703   else if( aDirection[0] > 0 && aDirection[1] < 0 && aDirection[2] < 0 && aRot[0] <= 0 ) {
1704     aRot[0] = aRotation1 + 90.0;  aRot[1] = aRotation2 - 90.0; }
1705   else if( aDirection[0] > 0 && aDirection[1] < 0 && aDirection[2] < 0 && aRot[0] > 0 ) {
1706     aRot[0] = aRotation1 + 270.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 - 90.0; }
1709   else if( aDirection[0] < 0 && aDirection[1] > 0 && aDirection[2] > 0 && aRot[0] > 0 ) {
1710     aRot[0] = aRotation1 + 90.0;  aRot[1] = aRotation2 - 270.0; }
1711   else if( aDirection[0] < 0 && aDirection[1] > 0 && aDirection[2] < 0 && aRot[0] <= 0 ) {
1712     aRot[0] = aRotation1 - 90.0;  aRot[1] = aRotation2 + 90.0; }
1713   else if( aDirection[0] < 0 && aDirection[1] > 0 && aDirection[2] < 0 && aRot[0] > 0 ) {
1714     aRot[0] = aRotation1 + 90.0;  aRot[1] = aRotation2 + 270.0; }
1715   else if( aDirection[0] < 0 && aDirection[1] < 0 && aDirection[2] > 0 && aRot[0] <= 0  ) {
1716     aRot[0] = aRotation1 - 270.0; aRot[1] = aRotation2 - 270.0; }
1717   else if( aDirection[0] < 0 && aDirection[1] < 0 && aDirection[2] > 0 && aRot[0] > 0 ) {
1718     aRot[0] = aRotation1 - 90.0;  aRot[1] = aRotation2 - 90.0; }
1719   else if( aDirection[0] < 0 && aDirection[1] < 0 && aDirection[2] < 0 && aRot[0] <= 0  ) {
1720     aRot[0] = aRotation1 + 90.0;  aRot[1] = aRotation2 + 270.0; }
1721   else if( aDirection[0] < 0 && aDirection[1] < 0 && aDirection[2] < 0 && aRot[0] > 0 ) {
1722     aRot[0] = aRotation1 + 270.0; aRot[1] = aRotation2 + 90.0; }
1723
1724   SpinSliderRotation1->setValue( qRound( aRot[0] ) );
1725   SpinSliderRotation2->setValue( qRound( aRot[1] ) );
1726
1727   int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
1728   const SMESH::TPlaneData& aPlaneData = myPlanes[ aCurPlaneIndex ];
1729   const SMESH::TActorList& anActorList = aPlaneData.ActorList;
1730
1731   double aBounds[6];
1732   double aDist;
1733   SMESH::ComputeBounds( anActorList, aBounds );
1734   SMESH::PositionToDistance( aBounds, theDir, theOrigin, aDist );
1735   aDist = 1.0 - aDist;
1736   if( aDist>1.0 )
1737     aDist = 1.0;
1738   else if( aDist < 0.0 )
1739     aDist = 0.0;
1740
1741   SpinSliderDistance->setValue( qRound( aDist*100 ) );
1742   return;
1743 }
1744
1745 /*!
1746   SLOT: called on preview check box toggled
1747 */
1748 void SMESHGUI_ClippingDlg::OnPreviewToggle (bool theIsToggled)
1749 {
1750   std::for_each( myPlanes.begin(), myPlanes.end(), TSetVisibility( theIsToggled ) );
1751   int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
1752   SMESH::TPlaneData aPlane = myPlanes[aCurPlaneIndex];
1753   SMESH::OrientedPlane* aPlaneData = aPlane.Plane.GetPointer();
1754   aPlaneData->myActor->SetVisibility( false );
1755   if( theIsToggled )
1756     myPreviewWidget->On();
1757   else
1758     myPreviewWidget->Off();
1759   SMESH::RenderViewWindow( myViewWindow );
1760 }
1761
1762 /*!
1763   SLOT: called on Auto Apply check box toggled
1764 */
1765 void SMESHGUI_ClippingDlg::onAutoApply(bool toggled)
1766 {
1767   if ( toggled ) ClickOnApply();
1768 }
1769
1770 /*!
1771   SLOT on ok button click: sets cutting plane and closes dialog
1772 */
1773 void SMESHGUI_ClippingDlg::ClickOnOk()
1774 {
1775   ClickOnApply();
1776   accept();
1777 }
1778
1779
1780 /*!
1781   SLOT on Apply button click: sets cutting plane and update viewer
1782 */
1783 void SMESHGUI_ClippingDlg::ClickOnApply()
1784 {
1785   if (myViewWindow) {
1786     SUIT_OverrideCursor wc;
1787
1788     QWidget *aCurrWid = this->focusWidget();
1789     aCurrWid->clearFocus();
1790     aCurrWid->setFocus();
1791
1792     SMESHGUI_ClippingPlaneInfoMap& aClippingPlaneInfoMap = mySMESHGUI->getClippingPlaneInfoMap();
1793     SMESHGUI_ClippingPlaneInfoList& aClippingPlaneInfoList = aClippingPlaneInfoMap[ myViewWindow->getViewManager() ];
1794
1795     // clean memory allocated for planes
1796     SMESHGUI_ClippingPlaneInfoList::iterator anIter1 = aClippingPlaneInfoList.begin();
1797     for( ; anIter1 != aClippingPlaneInfoList.end(); anIter1++ )
1798       if( SMESH::OrientedPlane* aPlane = (*anIter1).Plane )
1799         aPlane->Delete();
1800
1801     aClippingPlaneInfoList.clear();
1802
1803     VTK::ActorCollectionCopy aCopy( myViewWindow->getRenderer()->GetActors() );
1804     vtkActorCollection* anAllActors = aCopy.GetActors();
1805     anAllActors->InitTraversal();
1806     while( vtkActor* aVTKActor = anAllActors->GetNextActor() )
1807       if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) )
1808         anActor->RemoveAllClippingPlanes();
1809
1810     SMESH::TPlaneDataVector::iterator anIter2 = myPlanes.begin();
1811     for( ; anIter2 != myPlanes.end(); anIter2++ ) {
1812       SMESH::TPlaneData aPlaneData = *anIter2;
1813       SMESH::TPlane aPlane = aPlaneData.Plane;
1814       SMESH::TActorList anActorList = aPlaneData.ActorList;
1815
1816       // the check is disabled to support planes with empty actor list
1817       //if( anActorList.empty() )
1818       //  continue;
1819
1820       SMESH::OrientedPlane* anOrientedPlane = SMESH::OrientedPlane::New(myViewWindow);
1821       anOrientedPlane->ShallowCopy(aPlane.GetPointer());
1822       SMESH::TActorList::iterator anIter3 = anActorList.begin();
1823       for( ; anIter3 != anActorList.end(); anIter3++ )
1824         if( vtkActor* aVTKActor = *anIter3 )
1825           if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) ) {
1826             if( anOrientedPlane->IsOpenGLClipping )
1827               anActor->AddOpenGLClippingPlane( anOrientedPlane->InvertPlane() );
1828             else
1829               anActor->AddClippingPlane( anOrientedPlane );
1830           }
1831
1832       SMESH::ClippingPlaneInfo aClippingPlaneInfo;
1833       aClippingPlaneInfo.Plane = anOrientedPlane;
1834       aClippingPlaneInfo.ActorList = anActorList;
1835
1836       aClippingPlaneInfoList.push_back( aClippingPlaneInfo );
1837     }
1838
1839     anAllActors->InitTraversal();
1840     while( vtkActor* aVTKActor = anAllActors->GetNextActor() )
1841       if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) ) {
1842         anActor->SetOpenGLClippingPlane();
1843       }
1844
1845     SMESH::RenderViewWindow( myViewWindow );
1846   }
1847 }
1848
1849 /*!
1850   SLOT on help button click: opens a help page
1851 */
1852 void SMESHGUI_ClippingDlg::ClickOnHelp()
1853 {
1854   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
1855   if (app)
1856     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
1857   else {
1858                 QString platform;
1859 #ifdef WIN32
1860                 platform = "winapplication";
1861 #else
1862                 platform = "application";
1863 #endif
1864     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
1865                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
1866                              arg(app->resourceMgr()->stringValue("ExternalBrowser",
1867                                                                  platform)).
1868                              arg(myHelpFileName));
1869   }
1870 }
1871
1872 void SMESHGUI_ClippingDlg::onSelectAbsoluteOrientation( int mode )
1873 {
1874   bool isUserMode = (mode==0);
1875
1876   TextLabelX->setEnabled( isUserMode );
1877   TextLabelY->setEnabled( isUserMode );
1878   TextLabelZ->setEnabled( isUserMode );
1879
1880   SpinBox_X->setEnabled( isUserMode );
1881   SpinBox_Y->setEnabled( isUserMode );
1882   SpinBox_Z->setEnabled( isUserMode );
1883
1884   TextLabelDx->setEnabled( isUserMode );
1885   TextLabelDy->setEnabled( isUserMode );
1886   TextLabelDz->setEnabled( isUserMode );
1887
1888   SpinBox_Dx->setEnabled( isUserMode );
1889   SpinBox_Dy->setEnabled( isUserMode );
1890   SpinBox_Dz->setEnabled( isUserMode );
1891
1892   if ( isUserMode )
1893     return;
1894
1895   double aDx = 0, aDy = 0, aDz = 0;
1896
1897   if ( mode == 1 )
1898   {
1899     aDz = 1;
1900     TextLabelZ->setEnabled( true );
1901     SpinBox_Z->setEnabled( true );
1902     SpinBox_Z->setFocus();
1903   }
1904   else if ( mode == 2 )
1905   {
1906     aDx = 1;
1907     TextLabelX->setEnabled( true );
1908     SpinBox_X->setEnabled( true );
1909     SpinBox_X->setFocus();
1910   }
1911   else if ( mode == 3 )
1912   {
1913     aDy = 1;
1914     TextLabelY->setEnabled( true );
1915     SpinBox_Y->setEnabled( true );
1916     SpinBox_Y->setFocus();
1917   }
1918
1919   int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
1920   SMESH::TPlaneData aPlane = myPlanes[aCurPlaneIndex];
1921   SMESH::OrientedPlane* aPlaneData = aPlane.Plane.GetPointer();
1922
1923   if ( aPlaneData->IsInvert == true ) {
1924     aDx = -aDx; aDy = -aDy; aDz = -aDz;
1925   }
1926
1927   myIsSelectPlane = true;
1928   SpinBox_Dx->setValue( aDx );
1929   SpinBox_Dy->setValue( aDy );
1930   SpinBox_Dz->setValue( aDz );
1931   myIsSelectPlane = false;
1932
1933   SetCurrentPlaneParam();
1934 }
1935
1936 /*!
1937   SLOT: called on orientation of clipping plane in relative mode changed
1938 */
1939 void SMESHGUI_ClippingDlg::onSelectRelativeOrientation ( int theItem )
1940 {
1941   if ( myPlanes.empty() )
1942     return;
1943
1944   if ( theItem == 0 ) {
1945     TextLabelRotation1->setText( tr( "ROTATION_AROUND_X_Y2Z" ) );
1946     TextLabelRotation2->setText( tr( "ROTATION_AROUND_Y_X2Z" ) );
1947   }
1948   else if ( theItem == 1 ) {
1949     TextLabelRotation1->setText( tr( "ROTATION_AROUND_Y_Z2X" ) );
1950     TextLabelRotation2->setText( tr( "ROTATION_AROUND_Z_Y2X" ) );
1951   }
1952   else if ( theItem == 2 ) {
1953     TextLabelRotation1->setText( tr( "ROTATION_AROUND_Z_X2Y" ) );
1954     TextLabelRotation2->setText( tr( "ROTATION_AROUND_X_Z2Y" ) );
1955   }
1956
1957   if( (QComboBox*)sender() == CBRelativeOrientation )
1958     SetCurrentPlaneParam();
1959 }
1960
1961 /*!
1962   SLOT on reset button click: sets default values
1963 */
1964 void SMESHGUI_ClippingDlg::onReset()
1965 {
1966   myIsSelectPlane = true;
1967   SpinBox_X->setValue(0);
1968   SpinBox_Y->setValue(0);
1969   SpinBox_Z->setValue(0);
1970   myIsSelectPlane = false;
1971
1972   SetCurrentPlaneParam();
1973 }
1974
1975 /*!
1976   SLOT on invert button click: inverts normal of cutting plane
1977 */
1978 void SMESHGUI_ClippingDlg::onInvert()
1979 {
1980   double Dx = SpinBox_Dx->value();
1981   double Dy = SpinBox_Dy->value();
1982   double Dz = SpinBox_Dz->value();
1983
1984   myIsSelectPlane = true;
1985   SpinBox_Dx->setValue( -Dx );
1986   SpinBox_Dy->setValue( -Dy );
1987   SpinBox_Dz->setValue( -Dz );
1988   myIsSelectPlane = false;
1989
1990   if ( !myPlanes.empty() ) {
1991     int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
1992     SMESH::TPlaneData aPlane = myPlanes[aCurPlaneIndex];
1993     SMESH::OrientedPlane* aPlaneData = aPlane.Plane.GetPointer();
1994     aPlaneData->IsInvert = !aPlaneData->IsInvert;
1995   }
1996   SetCurrentPlaneParam();
1997 }
1998