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