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