Salome HOME
880406d30554080ca2e369712de7679f556d9877
[modules/gui.git] / src / OCCViewer / OCCViewer_ClippingDlg.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "OCCViewer_ClippingDlg.h"
24
25 #include <QtxDoubleSpinBox.h>
26 #include <QtxAction.h>
27
28 #include "SUIT_Session.h"
29 #include "SUIT_ViewWindow.h"
30 #include "SUIT_ViewManager.h"
31 #include "OCCViewer_ClipPlane.h"
32 #include "OCCViewer_ViewWindow.h"
33 #include "OCCViewer_ViewPort3d.h"
34 #include "OCCViewer_ViewModel.h"
35
36 #include <V3d_View.hxx>
37 #include <Visual3d_View.hxx>
38 #include <Geom_Plane.hxx>
39 #include <Prs3d_Presentation.hxx>
40 #include <AIS_ListIteratorOfListOfInteractive.hxx>
41 #include <AIS_ListOfInteractive.hxx>
42 #include <AIS_InteractiveObject.hxx>
43 #include <AIS_InteractiveContext.hxx>
44 #include <IntAna_IntConicQuad.hxx>
45 #include <gp_Lin.hxx>
46 #include <gp_Pln.hxx>
47 #include <math.h>
48
49 // QT Includes
50 #include <QApplication>
51 #include <QGroupBox>
52 #include <QHBoxLayout>
53 #include <QVBoxLayout>
54 #include <QGridLayout>
55 #include <QLabel>
56 #include <QPushButton>
57 #include <QComboBox>
58 #include <QCheckBox>
59 #include <QStackedLayout>
60 #include <QSlider>
61 #include <QMenu>
62
63 /**********************************************************************************
64  ************************        Internal functions        ************************
65  *********************************************************************************/
66
67 void getMinMaxFromContext( Handle(AIS_InteractiveContext) ic,
68                            double  theDefaultSize,
69                            double& theXMin,
70                            double& theYMin,
71                            double& theZMin,
72                            double& theXMax,
73                            double& theYMax,
74                            double& theZMax) {
75
76   double aXMin, aYMin, aZMin, aXMax, aYMax, aZMax;
77   aXMin = aYMin = aZMin = DBL_MAX;
78   aXMax = aYMax = aZMax = -DBL_MAX;
79   
80   bool isFound = false;
81   AIS_ListOfInteractive aList;
82   ic->DisplayedObjects( aList );
83   for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() ) {
84     Handle(AIS_InteractiveObject) anObj = it.Value();
85     if ( !anObj.IsNull() && anObj->HasPresentation() &&
86          !anObj->IsKind( STANDARD_TYPE(AIS_Plane) ) ) {
87       Handle(Prs3d_Presentation) aPrs = anObj->Presentation();
88       if ( !aPrs->IsEmpty() && !aPrs->IsInfinite() ) {
89         isFound = true;
90         double xmin, ymin, zmin, xmax, ymax, zmax;
91         aPrs->MinMaxValues( xmin, ymin, zmin, xmax, ymax, zmax );
92         aXMin = qMin( aXMin, xmin );  aXMax = qMax( aXMax, xmax );
93         aYMin = qMin( aYMin, ymin );  aYMax = qMax( aYMax, ymax );
94         aZMin = qMin( aZMin, zmin );  aZMax = qMax( aZMax, zmax );
95       }
96     }
97   }
98
99   if(!isFound) {
100     if(theDefaultSize == 0.0)
101       theDefaultSize = 100.;
102     aXMin = aYMin = aZMin = -theDefaultSize;
103     aXMax = aYMax = aZMax = theDefaultSize;
104   }
105   theXMin = aXMin;theYMin = aYMin;theZMin = aZMin;
106   theXMax = aXMax;theYMax = aYMax;theZMax = aZMax;
107 }
108
109 /*!
110   Compute the point of bounding box and current clipping plane
111  */
112 void ComputeBoundsParam( double theBounds[6],
113                          double theDirection[3],
114                          double theMinPnt[3],
115                          double& theMaxBoundPrj,
116                          double& theMinBoundPrj )
117 {
118   //Enlarge bounds in order to avoid conflicts of precision
119   for(int i = 0; i < 6; i += 2) {
120     static double EPS = 1.0E-3;
121     double aDelta = (theBounds[i+1] - theBounds[i])*EPS;
122     theBounds[i] -= aDelta;
123     theBounds[i+1] += aDelta;
124   }
125
126   double aBoundPoints[8][3] = { { theBounds[0], theBounds[2], theBounds[4] },
127                                 { theBounds[1], theBounds[2], theBounds[4] },
128                                 { theBounds[0], theBounds[3], theBounds[4] },
129                                 { theBounds[1], theBounds[3], theBounds[4] },
130                                 { theBounds[0], theBounds[2], theBounds[5] },
131                                 { theBounds[1], theBounds[2], theBounds[5] },
132                                 { theBounds[0], theBounds[3], theBounds[5] },
133                                 { theBounds[1], theBounds[3], theBounds[5] } };
134
135   int aMaxId = 0;
136   theMaxBoundPrj = theDirection[0] * aBoundPoints[aMaxId][0] + theDirection[1] * aBoundPoints[aMaxId][1]
137                    + theDirection[2] * aBoundPoints[aMaxId][2];
138   theMinBoundPrj = theMaxBoundPrj;
139   for(int i = 1; i < 8; i++) {
140     double aTmp = theDirection[0] * aBoundPoints[i][0] + theDirection[1] * aBoundPoints[i][1]
141                   + theDirection[2] * aBoundPoints[i][2];
142     if(theMaxBoundPrj < aTmp) {
143       theMaxBoundPrj = aTmp;
144       aMaxId = i;
145     }
146     if(theMinBoundPrj > aTmp) {
147       theMinBoundPrj = aTmp;
148     }
149   }
150   double *aMinPnt = aBoundPoints[aMaxId];
151   theMinPnt[0] = aMinPnt[0];
152   theMinPnt[1] = aMinPnt[1];
153   theMinPnt[2] = aMinPnt[2];
154 }
155
156 /*!
157   Compute the position of current plane by distance
158  */
159 void DistanceToPosition( double theBounds[6],
160                          double theDirection[3],
161                          double theDist,
162                          double thePos[3] )
163 {
164   double aMaxBoundPrj, aMinBoundPrj, aMinPnt[3];
165   ComputeBoundsParam( theBounds,theDirection,aMinPnt,aMaxBoundPrj,aMinBoundPrj );
166   double aLength = (aMaxBoundPrj - aMinBoundPrj)*theDist;
167   thePos[0] = aMinPnt[0] - theDirection[0]*aLength;
168   thePos[1] = aMinPnt[1] - theDirection[1]*aLength;
169   thePos[2] = aMinPnt[2] - theDirection[2]*aLength;
170 }
171
172 /*!
173   Compute the parameters of clipping plane
174  */
175 bool ComputeClippingPlaneParameters( double theNormal[3],
176                                      double theDist,
177                                      double theOrigin[3],
178                                      Handle(AIS_InteractiveContext) ic,
179                                      double theDefaultSize)
180 {
181   double aBounds[6] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
182   getMinMaxFromContext(ic,theDefaultSize,aBounds[0], aBounds[2], aBounds[4], aBounds[1], aBounds[3], aBounds[5]);
183
184   DistanceToPosition( aBounds, theNormal, theDist, theOrigin );
185   return true;
186 }
187
188 /*!
189   Cross product of two 3-vectors. Result vector in result[3].
190  */
191 void Cross(const double first[3], const double second[3], double result[3])
192 {
193   result[0] = first[1]*second[2] - first[2]*second[1];
194   result[1] = first[2]*second[0] - first[0]*second[2];
195   result[2] = first[0]*second[1] - first[1]*second[0];
196 }
197
198 /*!
199   Compute relative clipping plane in absolute coordinates
200  */
201 void RelativePlaneToAbsolute (OCCViewer_ClipPlane& thePlane, Handle(AIS_InteractiveContext) ic, double theDefaultSize )
202 {
203   double aNormal[3];
204   double aDir[2][3] = { { 0, 0, 0 }, { 0, 0, 0 } };
205   {
206     static double aCoeff = M_PI/180.0;
207
208     double anU[2] = { cos( aCoeff * thePlane.RelativeMode.Rotation1 ), cos( aCoeff * thePlane.RelativeMode.Rotation2 ) };
209     double aV[2] = { sqrt( 1.0 - anU[0]*anU[0] ), sqrt( 1.0 - anU[1] * anU[1] ) };
210     aV[0] = thePlane.RelativeMode.Rotation1 > 0? aV[0]: -aV[0];
211     aV[1] = thePlane.RelativeMode.Rotation2 > 0? aV[1]: -aV[1];
212
213     switch ( thePlane.RelativeMode.Orientation ) {
214     case 0:
215       aDir[0][1] = anU[0];
216       aDir[0][2] = aV[0];
217       aDir[1][0] = anU[1];
218       aDir[1][2] = aV[1];
219       break;
220     case 1:
221       aDir[0][2] = anU[0];
222       aDir[0][0] = aV[0];
223       aDir[1][1] = anU[1];
224       aDir[1][0] = aV[1];
225       break;
226     case 2:
227       aDir[0][0] = anU[0];
228       aDir[0][1] = aV[0];
229       aDir[1][2] = anU[1];
230       aDir[1][1] = aV[1];
231       break;
232     }
233
234     Cross( aDir[1], aDir[0], aNormal );
235     // Normalize
236     double den;
237     den = sqrt( aNormal[0] * aNormal[0] + aNormal[1] * aNormal[1] + aNormal[2] * aNormal[2] );
238     if ( den != 0.0 ) {
239       for (int i=0; i < 3; i++) {
240         aNormal[i] /= den;
241       }
242     }
243     Cross( aNormal, aDir[1], aDir[0] );
244   }
245
246   double anOrigin[3];
247
248   anOrigin[0] = anOrigin[1] = anOrigin[2] = 0;
249   bool anIsOk = true;
250
251   anIsOk = ComputeClippingPlaneParameters( aNormal,
252                                            thePlane.RelativeMode.Distance,
253                                            anOrigin,
254                                            ic,
255                                            theDefaultSize );
256   if( !anIsOk )
257           return;
258   thePlane.X = anOrigin[0];
259   thePlane.Y = anOrigin[1];
260   thePlane.Z = anOrigin[2];
261   thePlane.Dx = aNormal[0];
262   thePlane.Dy = aNormal[1];
263   thePlane.Dz = aNormal[2];
264 }
265
266 /*!
267   Compute clipping plane size base point and normal
268  */
269
270 void clipPlaneParams(OCCViewer_ClipPlane& theClipPlane, Handle(AIS_InteractiveContext) theContext,
271                      double& theSize, gp_Pnt& theBasePnt, gp_Dir& theNormal, double defaultSize) {
272   double aXMin, aYMin, aZMin, aXMax, aYMax, aZMax;
273   aXMin = aYMin = aZMin = DBL_MAX;
274   aXMax = aYMax = aZMax = -DBL_MAX;
275   
276   getMinMaxFromContext(theContext,defaultSize,aXMin, aYMin, aZMin, aXMax, aYMax, aZMax);
277   double aSize = 50;
278
279
280   gp_Pnt aBasePnt(theClipPlane.X ,  theClipPlane.Y ,  theClipPlane.Z);
281   gp_Dir aNormal(theClipPlane.Dx, theClipPlane.Dy, theClipPlane.Dz );
282
283   // compute clipping plane size
284   gp_Pnt aCenter = gp_Pnt( ( aXMin + aXMax ) / 2, ( aYMin + aYMax ) / 2, ( aZMin + aZMax ) / 2 );
285   double aDiag = aCenter.Distance( gp_Pnt( aXMax, aYMax, aZMax ) )*2;
286   aSize = aDiag * 1.1;
287   
288   // compute clipping plane center ( redefine the base point )
289   IntAna_IntConicQuad intersector = IntAna_IntConicQuad();
290   
291   intersector.Perform( gp_Lin( aCenter, aNormal), gp_Pln( aBasePnt, aNormal), Precision::Confusion() );
292
293   if ( intersector.IsDone() && intersector.NbPoints() == 1 )
294     aBasePnt = intersector.Point( 1 );
295   
296   theSize = aSize;
297   theBasePnt = aBasePnt;
298   theNormal = aNormal;
299 }
300
301
302 /*********************************************************************************
303  *********************      class OCCViewer_ClippingDlg      *********************
304  *********************************************************************************/
305 /*!
306   Constructor
307   \param view - view window
308   \param parent - parent widget
309 */
310 OCCViewer_ClippingDlg::OCCViewer_ClippingDlg(OCCViewer_ViewWindow* parent , OCCViewer_Viewer* model)
311   : QDialog( parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint ),
312     myCurrentClipPlaneMode (Absolute)
313 {
314   setObjectName( "OCCViewer_ClippingDlg" );
315   setModal( false );
316
317   setWindowTitle( tr( "Clipping" ) );
318
319   setAttribute (Qt::WA_DeleteOnClose, true);
320   
321   QVBoxLayout* topLayout = new QVBoxLayout( this );
322   topLayout->setMargin( 11 ); topLayout->setSpacing( 6 );
323
324   /***************************************************************/
325   // Controls for selecting, creating, deleting planes
326   QGroupBox* GroupPlanes = new QGroupBox( tr("CLIPPING_PLANES"), this );
327   QHBoxLayout* GroupPlanesLayout = new QHBoxLayout( GroupPlanes );
328   ComboBoxPlanes = new QComboBox( GroupPlanes );
329   isActivePlane = new QCheckBox( tr("IS_ACTIVE_PLANE"), this );
330   buttonNew = new QPushButton( tr("BTN_NEW"), GroupPlanes );
331   buttonDelete = new QPushButton( tr("BTN_DELETE"), GroupPlanes );
332   buttonDisableAll = new QPushButton( tr("BTN_DISABLE_ALL"), GroupPlanes );
333   MenuMode = new QMenu( "MenuMode", buttonNew );
334   MenuMode->addAction( tr( "ABSOLUTE" ), this, SLOT( onModeAbsolute() ) );
335   MenuMode->addAction( tr( "RELATIVE" ), this, SLOT( onModeRelative() ) );
336   buttonNew->setMenu( MenuMode );
337
338   GroupPlanesLayout->addWidget( ComboBoxPlanes );
339   GroupPlanesLayout->addWidget( isActivePlane );
340   GroupPlanesLayout->addWidget( buttonNew );
341   GroupPlanesLayout->addWidget( buttonDelete );
342   GroupPlanesLayout->addWidget( buttonDisableAll );
343
344   ModeStackedLayout = new QStackedLayout();
345
346   /**********************   Mode Absolute   **********************/
347   /* Controls for absolute mode of clipping plane:
348      X, Y, Z - coordinates of the intersection of cutting plane and the three axes
349      Dx, Dy, Dz - components of normal to the cutting plane
350      Orientation - direction of cutting plane
351    */
352   const double min = -1e+7;
353   const double max =  1e+7;
354   const double step = 5;
355   const int precision = -7;
356
357   // Croup Point
358   QGroupBox* GroupAbsolutePoint = new QGroupBox( this );
359   GroupAbsolutePoint->setObjectName( "GroupPoint" );
360   GroupAbsolutePoint->setTitle( tr("BASE_POINT") );
361   QGridLayout* GroupPointLayout = new QGridLayout( GroupAbsolutePoint );
362   GroupPointLayout->setAlignment( Qt::AlignTop );
363   GroupPointLayout->setSpacing( 6 ); GroupPointLayout->setMargin( 11 );
364
365   TextLabelX = new QLabel( GroupAbsolutePoint );
366   TextLabelX->setObjectName( "TextLabelX" );
367   TextLabelX->setText( tr("X:") );
368   GroupPointLayout->addWidget( TextLabelX, 0, 0 );
369   
370   SpinBox_X = new QtxDoubleSpinBox( min, max, step, GroupAbsolutePoint );
371   SpinBox_X->setObjectName("SpinBox_X" );
372   SpinBox_X->setPrecision( precision );
373   GroupPointLayout->addWidget( SpinBox_X, 0, 1 );
374
375   TextLabelY = new QLabel( GroupAbsolutePoint );
376   TextLabelY->setObjectName( "TextLabelY" );
377   TextLabelY->setText( tr("Y:") );
378   GroupPointLayout->addWidget( TextLabelY, 0, 2 );
379
380   SpinBox_Y = new QtxDoubleSpinBox( min, max, step, GroupAbsolutePoint );
381   SpinBox_Y->setObjectName("SpinBox_Y" );
382   SpinBox_Y->setPrecision( precision );
383   GroupPointLayout->addWidget( SpinBox_Y, 0, 3 );
384
385   TextLabelZ = new QLabel( GroupAbsolutePoint );
386   TextLabelZ->setObjectName( "TextLabelZ" );
387   TextLabelZ->setText( tr("Z:") );
388   GroupPointLayout->addWidget( TextLabelZ, 0, 4 );
389
390   SpinBox_Z = new QtxDoubleSpinBox( min, max, step, GroupAbsolutePoint );
391   SpinBox_Z->setObjectName("SpinBox_Z" );
392   SpinBox_Z->setPrecision( precision );
393   GroupPointLayout->addWidget( SpinBox_Z, 0, 5 );
394
395   resetButton  = new QPushButton( GroupAbsolutePoint );
396   resetButton->setObjectName( "resetButton" );
397   resetButton->setText( tr( "RESET"  ) );
398   GroupPointLayout->addWidget( resetButton, 0, 6 );
399
400   // Group Direction
401   GroupAbsoluteDirection = new QGroupBox( this );
402   GroupAbsoluteDirection->setObjectName( "GroupDirection" );
403   GroupAbsoluteDirection->setTitle( tr("DIRECTION") );
404   QGridLayout* GroupDirectionLayout = new QGridLayout( GroupAbsoluteDirection );
405   GroupDirectionLayout->setAlignment( Qt::AlignTop );
406   GroupDirectionLayout->setSpacing( 6 );
407   GroupDirectionLayout->setMargin( 11 );
408   
409   TextLabelDx = new QLabel( GroupAbsoluteDirection );
410   TextLabelDx->setObjectName( "TextLabelDx" );
411   TextLabelDx->setText( tr("Dx:") );
412   GroupDirectionLayout->addWidget( TextLabelDx, 0, 0 );
413   
414   SpinBox_Dx = new QtxDoubleSpinBox( min, max, step, GroupAbsoluteDirection );
415   SpinBox_Dx->setObjectName("SpinBox_Dx" );
416   SpinBox_Dx->setPrecision( precision );
417   GroupDirectionLayout->addWidget( SpinBox_Dx, 0, 1 );
418
419   TextLabelDy = new QLabel( GroupAbsoluteDirection );
420   TextLabelDy->setObjectName( "TextLabelDy" );
421   TextLabelDy->setText( tr("Dy:") );
422   GroupDirectionLayout->addWidget( TextLabelDy, 0, 2 );
423   
424   SpinBox_Dy = new QtxDoubleSpinBox( min, max, step, GroupAbsoluteDirection );
425   SpinBox_Dy->setObjectName("SpinBox_Dy" );
426   SpinBox_Dy->setPrecision( precision );
427   GroupDirectionLayout->addWidget( SpinBox_Dy, 0, 3 );
428
429   TextLabelDz = new QLabel( GroupAbsoluteDirection );
430   TextLabelDz->setObjectName( "TextLabelDz" );
431   TextLabelDz->setText( tr("Dz:") );
432   GroupDirectionLayout->addWidget( TextLabelDz, 0, 4 );
433   
434   SpinBox_Dz = new QtxDoubleSpinBox( min, max, step, GroupAbsoluteDirection );
435   SpinBox_Dz->setObjectName("SpinBox_Dz" );
436   SpinBox_Dz->setPrecision( precision );
437   GroupDirectionLayout->addWidget( SpinBox_Dz, 0, 5 );
438
439   invertButton  = new QPushButton( GroupAbsoluteDirection );
440   invertButton->setObjectName( "invertButton" );
441   invertButton->setText( tr( "INVERT"  ) );
442   GroupDirectionLayout->addWidget( invertButton, 0, 6 );
443  
444   CBAbsoluteOrientation = new QComboBox( GroupAbsoluteDirection );
445   CBAbsoluteOrientation->setObjectName( "AbsoluteOrientation" );
446   CBAbsoluteOrientation->insertItem( CBAbsoluteOrientation->count(), tr( "CUSTOM" ) );
447   CBAbsoluteOrientation->insertItem( CBAbsoluteOrientation->count(), tr( "||X-Y" ) );
448   CBAbsoluteOrientation->insertItem( CBAbsoluteOrientation->count(), tr( "||Y-Z" ) );
449   CBAbsoluteOrientation->insertItem( CBAbsoluteOrientation->count(), tr( "||Z-X" ) );
450   GroupDirectionLayout->addWidget( CBAbsoluteOrientation, 1, 0, 1, 6 );
451
452   QVBoxLayout* ModeActiveLayout = new QVBoxLayout();
453   ModeActiveLayout->setMargin( 11 ); ModeActiveLayout->setSpacing( 6 );
454   ModeActiveLayout->addWidget( GroupAbsolutePoint );
455   ModeActiveLayout->addWidget( GroupAbsoluteDirection );
456
457   QWidget* ModeActiveWidget = new QWidget( this );
458   ModeActiveWidget->setLayout( ModeActiveLayout );
459
460   /**********************   Mode Relative   **********************/
461   /* Controls for relative mode of clipping plane:
462      Distance - Value from 0 to 1.
463      Specifies the distance from the minimum value in a given direction of bounding box to the current position
464      Rotation1, Rotation2 - turn angles of cutting plane in given directions
465      Orientation - direction of cutting plane
466    */
467   QGroupBox* GroupParameters = new QGroupBox( tr("PARAMETERS"), this );
468   QGridLayout* GroupParametersLayout = new QGridLayout( GroupParameters );
469   GroupParametersLayout->setMargin( 11 ); GroupParametersLayout->setSpacing( 6 );
470
471   TextLabelOrientation = new QLabel( tr("ORIENTATION"), GroupParameters);
472   TextLabelOrientation->setObjectName( "TextLabelOrientation" );
473   GroupParametersLayout->addWidget( TextLabelOrientation, 0, 0 );
474
475   CBRelativeOrientation = new QComboBox(GroupParameters);
476   CBRelativeOrientation->setObjectName( "RelativeOrientation" );
477   CBRelativeOrientation->addItem( tr("ALONG_XY") );
478   CBRelativeOrientation->addItem( tr("ALONG_YZ") );
479   CBRelativeOrientation->addItem( tr("ALONG_ZX") );
480   GroupParametersLayout->addWidget( CBRelativeOrientation, 0, 1 );
481
482   TLValueDistance = new QLabel( GroupParameters );
483   TLValueDistance->setObjectName( "TLValueDistance" );
484   TLValueDistance->setAlignment( Qt::AlignCenter );
485   TLValueDistance->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
486   QFont fnt = TLValueDistance->font(); fnt.setBold( true ); TLValueDistance->setFont( fnt );
487   GroupParametersLayout->addWidget( TLValueDistance, 1, 1 );
488
489   TextLabelDistance = new QLabel( tr("DISTANCE"), GroupParameters );
490   TextLabelDistance->setObjectName( "TextLabelDistance" );
491   GroupParametersLayout->addWidget( TextLabelDistance, 2, 0 );
492
493   SliderDistance = new QSlider( Qt::Horizontal, GroupParameters );
494   SliderDistance->setObjectName( "SliderDistance" );
495   SliderDistance->setMinimumSize( 300, 0 );
496   SliderDistance->setMinimum( 0 );
497   SliderDistance->setMaximum( 100 );
498   SliderDistance->setSingleStep( 1 );
499   SliderDistance->setPageStep( 10 );
500   SliderDistance->setTracking( false );
501   GroupParametersLayout->addWidget( SliderDistance, 2, 1 );
502
503   TLValueRotation1 = new QLabel( GroupParameters );
504   TLValueRotation1->setObjectName( "TLValueRotation1" );
505   TLValueRotation1->setAlignment( Qt::AlignCenter );
506   TLValueRotation1->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
507   TLValueRotation1->setFont( fnt );
508   GroupParametersLayout->addWidget( TLValueRotation1, 3, 1 );
509
510   TextLabelRotation1 = new QLabel( tr("ROTATION_AROUND_X_Y2Z"), GroupParameters );
511   TextLabelRotation1->setObjectName( "TextLabelRotation1" );
512   GroupParametersLayout->addWidget( TextLabelRotation1, 4, 0 );
513
514   SliderRotation1 = new QSlider( Qt::Horizontal, GroupParameters );
515   SliderRotation1->setObjectName( "SliderRotation1" );
516   SliderRotation1->setMinimumSize( 300, 0 );
517   SliderRotation1->setMinimum( -180 );
518   SliderRotation1->setMaximum( 180 );
519   SliderRotation1->setSingleStep( 1 );
520   SliderRotation1->setPageStep( 10 );
521   SliderRotation1->setTracking(false);
522   GroupParametersLayout->addWidget( SliderRotation1, 4, 1 );
523
524   TLValueRotation2 = new QLabel( GroupParameters );
525   TLValueRotation2->setObjectName( "TLValueRotation2" );
526   TLValueRotation2->setAlignment( Qt::AlignCenter );
527   TLValueRotation2->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
528   TLValueRotation2->setFont( fnt );
529   GroupParametersLayout->addWidget( TLValueRotation2, 5, 1 );
530
531   TextLabelRotation2 = new QLabel(tr("ROTATION_AROUND_Y_X2Z"), GroupParameters);
532   TextLabelRotation2->setObjectName( "TextLabelRotation2" );
533   TextLabelRotation2->setObjectName( "TextLabelRotation2" );
534   GroupParametersLayout->addWidget( TextLabelRotation2, 6, 0 );
535
536   SliderRotation2 = new QSlider( Qt::Horizontal, GroupParameters );
537   SliderRotation2->setObjectName( "SliderRotation2" );
538   SliderRotation2->setMinimumSize( 300, 0 );
539   SliderRotation2->setMinimum( -180 );
540   SliderRotation2->setMaximum( 180 );
541   SliderRotation2->setSingleStep( 1 );
542   SliderRotation2->setPageStep( 10 );
543   SliderRotation2->setTracking(false);
544   GroupParametersLayout->addWidget( SliderRotation2, 6, 1 );
545
546   /***************************************************************/
547   QGroupBox* CheckBoxWidget = new QGroupBox( this );
548   QHBoxLayout* CheckBoxLayout = new QHBoxLayout( CheckBoxWidget );
549   
550   PreviewCheckBox = new QCheckBox( tr("PREVIEW"), CheckBoxWidget );
551   PreviewCheckBox->setObjectName( "PreviewCheckBox" );
552   PreviewCheckBox->setChecked( true );
553   CheckBoxLayout->addWidget( PreviewCheckBox, 0, Qt::AlignCenter );
554   
555   AutoApplyCheckBox = new QCheckBox( tr("AUTO_APPLY"), CheckBoxWidget );
556   AutoApplyCheckBox->setObjectName( "AutoApplyCheckBox" );
557   CheckBoxLayout->addWidget( AutoApplyCheckBox, 0, Qt::AlignCenter );
558
559   /***************************************************************/
560   QGroupBox* GroupButtons = new QGroupBox( this );
561   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout( GroupButtons );
562   GroupButtonsLayout->setAlignment( Qt::AlignTop );
563   GroupButtonsLayout->setMargin( 11 ); GroupButtonsLayout->setSpacing( 6 );
564   
565   buttonOk = new QPushButton( GroupButtons );
566   buttonOk->setObjectName( "buttonOk" );
567   buttonOk->setText( tr( "BUT_APPLY_AND_CLOSE"  ) );
568   buttonOk->setAutoDefault( TRUE );
569   buttonOk->setDefault( TRUE );
570   GroupButtonsLayout->addWidget( buttonOk );
571
572   buttonApply = new QPushButton( GroupButtons );
573   buttonApply->setObjectName( "buttonApply" );
574   buttonApply->setText( tr( "BUT_APPLY"  ) );
575   buttonApply->setAutoDefault( TRUE );
576   buttonApply->setDefault( TRUE );
577   GroupButtonsLayout->addWidget( buttonApply );
578
579   GroupButtonsLayout->addStretch();
580   
581   buttonClose = new QPushButton( GroupButtons );
582   buttonClose->setObjectName( "buttonClose" );
583   buttonClose->setText( tr( "BUT_CLOSE"  ) );
584   buttonClose->setAutoDefault( TRUE );
585   GroupButtonsLayout->addWidget( buttonClose );
586
587   QPushButton* buttonHelp = new QPushButton( tr( "SMESH_BUT_HELP" ), GroupButtons );
588   buttonHelp->setAutoDefault( TRUE );
589   GroupButtonsLayout->addWidget( buttonHelp );
590
591   /***************************************************************/
592   ModeStackedLayout->addWidget( ModeActiveWidget );
593   ModeStackedLayout->addWidget( GroupParameters );
594
595   topLayout->addWidget( GroupPlanes );
596   topLayout->addLayout( ModeStackedLayout );
597   topLayout->addWidget( CheckBoxWidget );
598   topLayout->addWidget( GroupButtons );
599
600   this->setLayout( topLayout );
601
602   // Initializations
603   initParam();
604
605   // Signals and slots connections
606   connect( ComboBoxPlanes, SIGNAL( activated( int ) ), this, SLOT( onSelectPlane( int ) ) );
607   connect( isActivePlane,  SIGNAL ( toggled ( bool ) ),  this, SLOT( onValueChanged() ) );
608   connect( buttonNew, SIGNAL( clicked() ), buttonNew, SLOT( showMenu() ) );
609   connect( buttonDelete, SIGNAL( clicked() ), this, SLOT( ClickOnDelete() ) );
610   connect( buttonDisableAll, SIGNAL( clicked() ), this, SLOT( ClickOnDisableAll() ) );
611
612   connect( resetButton,  SIGNAL (clicked() ), this, SLOT( onReset() ) );
613   connect( invertButton, SIGNAL (clicked() ), this, SLOT( onInvert() ) ) ;
614   connect( SpinBox_X,  SIGNAL ( valueChanged( double ) ),  this, SLOT( onValueChanged() ) );
615   connect( SpinBox_Y,  SIGNAL ( valueChanged( double ) ),  this, SLOT( onValueChanged() ) );
616   connect( SpinBox_Z,  SIGNAL ( valueChanged( double ) ),  this, SLOT( onValueChanged() ) );
617   connect( SpinBox_Dx, SIGNAL ( valueChanged( double ) ),  this, SLOT( onValueChanged() ) );
618   connect( SpinBox_Dy, SIGNAL ( valueChanged( double ) ),  this, SLOT( onValueChanged() ) );
619   connect( SpinBox_Dz, SIGNAL ( valueChanged( double ) ),  this, SLOT( onValueChanged() ) );
620   connect( CBAbsoluteOrientation, SIGNAL ( activated ( int ) ), this, SLOT( onOrientationAbsoluteChanged( int ) ) ) ;
621
622   connect( CBRelativeOrientation, SIGNAL( activated( int ) ), this, SLOT( onOrientationRelativeChanged( int ) ) );
623   connect( SliderDistance,   SIGNAL( sliderMoved( int ) ),  this, SLOT( SliderDistanceHasMoved( int ) ) );
624   connect( SliderDistance,   SIGNAL( valueChanged( int ) ),  this, SLOT( SliderDistanceHasMoved( int ) ) );
625   connect( SliderRotation1,   SIGNAL( sliderMoved( int ) ),  this, SLOT( SliderRotation1HasMoved( int ) ) );
626   connect( SliderRotation1,   SIGNAL( valueChanged( int ) ),  this, SLOT( SliderRotation1HasMoved( int ) ) );
627   connect( SliderRotation2,   SIGNAL( sliderMoved( int ) ),  this, SLOT( SliderRotation2HasMoved( int ) ) );
628   connect( SliderRotation2,   SIGNAL( valueChanged( int ) ),  this, SLOT( SliderRotation2HasMoved( int ) ) );
629
630   connect( PreviewCheckBox, SIGNAL ( toggled ( bool ) ), this, SLOT( onPreview( bool ) ) ) ;
631   connect( AutoApplyCheckBox, SIGNAL ( toggled( bool ) ), this, SLOT( onAutoApply( bool ) ) );
632   
633   connect( buttonClose, SIGNAL( clicked() ), this, SLOT( ClickOnClose() ) ) ;
634   connect( buttonOk, SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
635   connect( buttonApply, SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
636   connect( buttonHelp, SIGNAL( clicked() ), this, SLOT( ClickOnHelp() ) );
637
638   myBusy = false;
639   myIsSelectPlane = false;
640   myIsPlaneCreation = false;
641   myModel = model;
642
643   myModel->getViewer3d()->InitActiveViews();
644
645   myLocalPlanes = myModel->getClipPlanes();
646   synchronize();
647 }
648
649 /*!
650   Destructor
651   Destroys the object and frees any allocated resources
652 */
653 OCCViewer_ClippingDlg::~OCCViewer_ClippingDlg()
654 {
655   myLocalPlanes.clear();
656 }
657
658 /*!
659   Custom handling of close event: erases preview
660 */
661 void OCCViewer_ClippingDlg::closeEvent( QCloseEvent* e )
662 {
663   erasePreview();
664   QDialog::closeEvent( e );
665   OCCViewer_ViewWindow* v = qobject_cast<OCCViewer_ViewWindow*>(parent());
666   if(v)
667     v->onClipping(false);
668 }
669
670 /*!
671   Custom handling of show event: displays preview
672 */
673 void OCCViewer_ClippingDlg::showEvent( QShowEvent* e )
674 {
675   QDialog::showEvent( e );
676   onPreview( PreviewCheckBox->isChecked() );
677 }
678
679 /*!
680   Custom handling of hide event: erases preview
681 */
682 void OCCViewer_ClippingDlg::hideEvent( QHideEvent* e )
683 {
684   erasePreview();
685   QDialog::hideEvent( e );
686   OCCViewer_ViewWindow* v = qobject_cast<OCCViewer_ViewWindow*>(parent());
687   if(v)
688     v->onClipping(false);
689
690 }
691
692 /*!
693   Initialization of initial values of widgets
694 */
695 void OCCViewer_ClippingDlg::initParam()
696 {
697   SpinBox_X->setValue( 0.0 );
698   SpinBox_Y->setValue( 0.0 );
699   SpinBox_Z->setValue( 0.0 );
700
701   SpinBox_Dx->setValue( 1.0 );
702   SpinBox_Dy->setValue( 1.0 );
703   SpinBox_Dz->setValue( 1.0 );
704
705   CBAbsoluteOrientation->setCurrentIndex(0);
706
707   TLValueDistance->setText( "0.5" );
708   TLValueRotation1->setText( "0\xB0" );
709   TLValueRotation2->setText( "0\xB0" );
710   CBRelativeOrientation->setCurrentIndex( 0 );
711   SliderDistance->setValue( 50 );
712   SliderRotation1->setValue( 0 );
713   SliderRotation2->setValue( 0 );
714 }
715
716 /*!
717   Synchronize dialog's widgets with data
718 */
719 void OCCViewer_ClippingDlg::synchronize()
720 {
721   ComboBoxPlanes->clear();
722   int aNbPlanesAbsolute = myLocalPlanes.size();
723
724   QString aName;
725   for(int i = 1; i<=aNbPlanesAbsolute; i++ ) {
726     aName = QString("Plane %1").arg(i);
727     ComboBoxPlanes->addItem( aName );
728   }
729
730   int aPos = ComboBoxPlanes->count() - 1;
731   ComboBoxPlanes->setCurrentIndex( aPos );
732
733   bool anIsControlsEnable = ( aPos >= 0 );
734   if ( anIsControlsEnable ) {
735     onSelectPlane( aPos );
736   }
737   else {
738     ComboBoxPlanes->addItem( tr( "NO_PLANES" ) );
739     initParam();
740   }
741   if ( myCurrentClipPlaneMode == Absolute ) {
742     SpinBox_X->setEnabled( anIsControlsEnable );
743     SpinBox_Y->setEnabled( anIsControlsEnable );
744     SpinBox_Z->setEnabled( anIsControlsEnable );
745     SpinBox_Dx->setEnabled( anIsControlsEnable );
746     SpinBox_Dy->setEnabled( anIsControlsEnable );
747     SpinBox_Dz->setEnabled( anIsControlsEnable );
748     CBAbsoluteOrientation->setEnabled( anIsControlsEnable );
749     invertButton->setEnabled( anIsControlsEnable );
750     resetButton->setEnabled( anIsControlsEnable );
751   }
752   else if( myCurrentClipPlaneMode == Relative ) {
753     CBRelativeOrientation->setEnabled( anIsControlsEnable );
754     SliderDistance->setEnabled( anIsControlsEnable );
755     SliderRotation1->setEnabled( anIsControlsEnable );
756     SliderRotation2->setEnabled( anIsControlsEnable );
757     isActivePlane->setEnabled( anIsControlsEnable );
758   }
759   isActivePlane->setEnabled( anIsControlsEnable );
760 }
761
762 /*!
763   Displays preview of clipping plane
764 */
765 void OCCViewer_ClippingDlg::displayPreview()
766 {
767   if ( myBusy || !isValid() || !myModel)
768     return;
769
770   Handle(AIS_InteractiveContext) ic = myModel->getAISContext();
771   
772   int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
773
774   for ( int i=0; i < clipPlanesCount(); i++ ) {
775   OCCViewer_ClipPlane& aClipPlane = getClipPlane(i);
776     if ( aClipPlane.IsOn ) {
777       Handle(AIS_Plane) myPreviewPlane;
778       double aSize;
779       gp_Pnt aBasePnt;
780       gp_Dir aNormal;
781       clipPlaneParams(aClipPlane, ic, aSize, aBasePnt, aNormal, myModel->trihedronSize());
782       myPreviewPlane = new AIS_Plane( new Geom_Plane( aBasePnt, aNormal ) );
783       myPreviewPlane->SetSize( aSize, aSize );
784       ic->Display( myPreviewPlane, 1, -1, false );
785       ic->SetWidth( myPreviewPlane, 10, false );
786       ic->SetMaterial( myPreviewPlane, Graphic3d_NOM_PLASTIC, false );
787       ic->SetTransparency( myPreviewPlane, 0.5, false );
788       Quantity_Color c = (aCurPlaneIndex == i) ? Quantity_Color( 255. / 255., 70. / 255., 0. / 255., Quantity_TOC_RGB ) : Quantity_Color( 85 / 255., 85 / 255., 255 / 255., Quantity_TOC_RGB );
789       ic->SetColor( myPreviewPlane, c , false );
790       myPreviewPlaneVector.push_back( myPreviewPlane );
791     }
792   }
793   myModel->update();
794 }
795
796 void OCCViewer_ClippingDlg::updatePreview() {
797   int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
798   int count = clipPlanesCount();
799   if ( myBusy || 
800        !isValid() || 
801        myIsPlaneCreation ||
802        !myModel || 
803        count == 0 || 
804        (aCurPlaneIndex +1 > count) ||
805        !PreviewCheckBox->isChecked())
806     return;
807   
808   Handle(AIS_InteractiveContext) ic = myModel->getAISContext();
809   
810   OCCViewer_ClipPlane& aClipPlane = getClipPlane(aCurPlaneIndex);
811   Handle(AIS_Plane) myPreviewPlane;
812
813   if (aClipPlane.IsOn) {
814     double aSize;
815     gp_Pnt aBasePnt;
816     gp_Dir aNormal;
817     clipPlaneParams(aClipPlane, ic, aSize, aBasePnt, aNormal, myModel->trihedronSize());
818     if(myPreviewPlaneVector.size() < clipPlanesCount()) {
819       myPreviewPlaneVector.resize(clipPlanesCount());
820     }
821     myPreviewPlane = myPreviewPlaneVector[aCurPlaneIndex];
822     if(myPreviewPlane.IsNull()) {
823       //Plane was not created 
824       myPreviewPlane = new AIS_Plane( new Geom_Plane( aBasePnt, aNormal ) );
825       myPreviewPlane->SetSize( aSize, aSize );
826       ic->Display( myPreviewPlane, 1, -1, false );
827       ic->SetWidth( myPreviewPlane, 10, false );
828       ic->SetMaterial( myPreviewPlane, Graphic3d_NOM_PLASTIC, false );
829       ic->SetTransparency( myPreviewPlane, 0.5, false );
830       myPreviewPlaneVector[aCurPlaneIndex] = myPreviewPlane;
831     } else {      
832       myPreviewPlane->SetComponent(new Geom_Plane( aBasePnt, aNormal ));
833       myPreviewPlane->SetSize( aSize, aSize );  
834     }
835
836     ic->SetColor( myPreviewPlane, Quantity_Color( 255. / 255., 70. / 255., 0. / 255., Quantity_TOC_RGB ), false );
837   } else {
838     if(myPreviewPlaneVector.size() > aCurPlaneIndex ) {
839       myPreviewPlane = myPreviewPlaneVector[aCurPlaneIndex];
840       if(ic->IsDisplayed(myPreviewPlane)) {
841         ic->Erase( myPreviewPlane, false );
842         ic->Remove( myPreviewPlane, false );
843       }
844       myPreviewPlaneVector[aCurPlaneIndex].Nullify();
845     }
846   }
847   for(int i = 0; i < myPreviewPlaneVector.size(); i++) {
848     if( i == aCurPlaneIndex ) continue;
849     if(!myPreviewPlaneVector[i].IsNull())
850       ic->SetColor( myPreviewPlaneVector[i], Quantity_Color( 85 / 255., 85 / 255., 255 / 255., Quantity_TOC_RGB ), false );  
851   }
852   myModel->update();
853 }
854
855 /*!
856   Erases preview of clipping plane
857 */
858 void OCCViewer_ClippingDlg::erasePreview()
859 {
860   if ( !myModel )
861     return;
862
863   Handle(AIS_InteractiveContext) ic = myModel->getAISContext();
864
865   for ( int i=0; i < myPreviewPlaneVector.size(); i++ ) {
866   Handle(AIS_Plane) myPreviewPlane = myPreviewPlaneVector[i];
867     if ( !myPreviewPlane.IsNull() && ic->IsDisplayed( myPreviewPlane ) ) {
868       ic->Erase( myPreviewPlane, false );
869       ic->Remove( myPreviewPlane, false );
870       myPreviewPlane.Nullify();
871     }
872   }
873   myPreviewPlaneVector.clear();
874   myModel->update();
875 }
876
877 /*!
878   Return true if plane parameters are valid
879 */
880 bool OCCViewer_ClippingDlg::isValid()
881 {
882   return ( SpinBox_Dx->value() !=0 || SpinBox_Dy->value() !=0 || SpinBox_Dz->value() !=0 );
883 }
884
885 /*!
886   Update view after changes
887 */
888 void OCCViewer_ClippingDlg::updateClipping()
889 {
890   if (PreviewCheckBox->isChecked() || AutoApplyCheckBox->isChecked())
891   {
892     if (AutoApplyCheckBox->isChecked()) {
893       onApply();
894     }
895     
896     if (!PreviewCheckBox->isChecked())
897       myModel->update();
898     else 
899       updatePreview();
900   }
901 }
902
903 /*!
904   SLOT on new button click: create a new clipping plane
905 */
906 void OCCViewer_ClippingDlg::ClickOnNew()
907 {
908
909   OCCViewer_ClipPlane aPlane;
910   aPlane.PlaneMode = (ClipPlaneMode )myCurrentClipPlaneMode;
911   myLocalPlanes.push_back(aPlane);
912   synchronize();
913 }
914
915 /*!
916   SLOT on delete button click: Delete selected clipping plane
917 */
918 void OCCViewer_ClippingDlg::ClickOnDelete()
919 {
920   int aPlaneIndex = ComboBoxPlanes->currentIndex();
921   if ( (clipPlanesCount() == 0) || (aPlaneIndex+1 > clipPlanesCount()))
922     return;
923
924   myLocalPlanes.erase(myLocalPlanes.begin() + aPlaneIndex);
925
926   Handle(AIS_InteractiveContext) ic = myModel->getAISContext();
927
928   if(aPlaneIndex+1 <= myPreviewPlaneVector.size()) {
929     Handle(AIS_Plane) myPreviewPlane = myPreviewPlaneVector[aPlaneIndex];
930     if ( !myPreviewPlane.IsNull() && ic->IsDisplayed( myPreviewPlane ) ) {
931       ic->Erase( myPreviewPlane, false );
932       ic->Remove( myPreviewPlane, false );
933     }
934     myPreviewPlaneVector.erase(myPreviewPlaneVector.begin() + aPlaneIndex);
935   }
936   synchronize();
937   if (AutoApplyCheckBox->isChecked()) {
938     onApply();
939   }
940   myModel->update();
941 }
942
943 /*!
944   SLOT on disable all button click: Restore initial state of viewer,
945   erase all clipping planes
946 */
947 void OCCViewer_ClippingDlg::ClickOnDisableAll()
948 {
949   AutoApplyCheckBox->setChecked (false);
950   int aClipPlanesCount = clipPlanesCount();
951   for ( int anIndex = 0; anIndex < aClipPlanesCount; anIndex++)
952   {
953     OCCViewer_ClipPlane& aPlane = getClipPlane(anIndex);
954     aPlane.IsOn = false;
955   }
956   erasePreview();
957   isActivePlane->setChecked(false);
958   myModel->setClipPlanes(myLocalPlanes);
959   myModel->update();
960 }
961
962 /*!
963   SLOT on ok button click: sets cutting plane and closes dialog
964 */
965 void OCCViewer_ClippingDlg::ClickOnOk()
966 {
967   onApply();
968   ClickOnClose();
969 }
970
971 /*!
972   SLOT on Apply button click: sets cutting plane and update viewer
973 */
974 void OCCViewer_ClippingDlg::ClickOnApply()
975 {
976   onApply();
977   myModel->update();
978 }
979
980 /*!
981   SLOT on close button click: erases preview and rejects dialog
982 */
983 void OCCViewer_ClippingDlg::ClickOnClose()
984 {
985   erasePreview();
986   OCCViewer_ViewWindow* v = qobject_cast<OCCViewer_ViewWindow*>(parent());
987   if(v)
988     v->onClipping(false);
989 }
990
991 /*!
992   SLOT on help button click: opens a help page
993 */
994 void OCCViewer_ClippingDlg::ClickOnHelp()
995 {
996   SUIT_Application* app = SUIT_Session::session()->activeApplication();
997   if ( app )
998     app->onHelpContextModule( "GUI", "occ_3d_viewer_page.html", "clipping_planes" );
999 }
1000
1001 /*!
1002   Set absolute mode of clipping plane
1003 */
1004 void OCCViewer_ClippingDlg::onModeAbsolute()
1005 {
1006   myIsPlaneCreation = true;
1007   ModeStackedLayout->setCurrentIndex(0);
1008   myCurrentClipPlaneMode = Absolute;
1009   ClickOnNew();
1010   myIsPlaneCreation = false;
1011   updateClipping();
1012 }
1013
1014 /*!
1015   Set relative mode of clipping plane
1016 */
1017 void OCCViewer_ClippingDlg::onModeRelative()
1018 {
1019   myIsPlaneCreation = true;
1020   ModeStackedLayout->setCurrentIndex(1);
1021   myCurrentClipPlaneMode = Relative;
1022   ClickOnNew();
1023   myIsPlaneCreation = false;
1024   SetCurrentPlaneParam();
1025   updateClipping();
1026 }
1027
1028 /*!
1029   SLOT: called on value of clipping plane changed
1030 */
1031 void OCCViewer_ClippingDlg::onValueChanged()
1032 {
1033   SetCurrentPlaneParam();
1034   if ( myIsSelectPlane )
1035     return;
1036   updateClipping();
1037 }
1038
1039 /*!
1040   Set current parameters of selected plane
1041 */
1042 void OCCViewer_ClippingDlg::onSelectPlane ( int theIndex )
1043 {
1044   if ( clipPlanesCount() == 0 )
1045     return;
1046
1047   OCCViewer_ClipPlane& aClipPlane = getClipPlane (theIndex);
1048
1049   myIsSelectPlane = true;
1050   if ( aClipPlane.PlaneMode == Absolute ) {
1051     ModeStackedLayout->setCurrentIndex( 0 );
1052     myCurrentClipPlaneMode = Absolute;
1053     int anOrientation = aClipPlane.Orientation;
1054     // Set plane parameters in the dialog
1055     SpinBox_X->setValue( aClipPlane.X );
1056     SpinBox_Y->setValue( aClipPlane.Y );
1057     SpinBox_Z->setValue( aClipPlane.Z );
1058     SpinBox_Dx->setValue( aClipPlane.Dx );
1059     SpinBox_Dy->setValue( aClipPlane.Dy );
1060     SpinBox_Dz->setValue( aClipPlane.Dz );
1061     CBAbsoluteOrientation->setCurrentIndex( anOrientation );
1062     onOrientationAbsoluteChanged( anOrientation );
1063   }
1064   else if( aClipPlane.PlaneMode == Relative ) {
1065     ModeStackedLayout->setCurrentIndex( 1 );
1066     myCurrentClipPlaneMode = Relative;
1067     int anOrientation = aClipPlane.RelativeMode.Orientation;
1068     // Set plane parameters in the dialog
1069     SliderDistance->setValue( aClipPlane.RelativeMode.Distance*100 );
1070     TLValueDistance->setText( QString::number(aClipPlane.RelativeMode.Distance ) );
1071     SliderRotation1->setValue( aClipPlane.RelativeMode.Rotation1 );
1072     TLValueRotation1->setText( QString( "%1\xB0" ).arg( aClipPlane.RelativeMode.Rotation1 ) );
1073     SliderRotation2->setValue( aClipPlane.RelativeMode.Rotation2 );
1074     TLValueRotation2->setText( QString( "%1\xB0" ).arg( aClipPlane.RelativeMode.Rotation2 ) );
1075     CBRelativeOrientation->setCurrentIndex( anOrientation );
1076     onOrientationRelativeChanged( anOrientation );
1077   }
1078   
1079   isActivePlane->setChecked( aClipPlane.IsOn );
1080   ComboBoxPlanes->setCurrentIndex( theIndex );
1081   myIsSelectPlane = false;
1082 }
1083
1084 /*!
1085   Restore parameters of selected plane
1086 */
1087 void OCCViewer_ClippingDlg::SetCurrentPlaneParam()
1088 {
1089   if ( clipPlanesCount() == 0 || myIsSelectPlane )
1090     return;
1091
1092   int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
1093
1094  OCCViewer_ClipPlane& aPlane = getClipPlane (aCurPlaneIndex);
1095
1096   if ( aPlane.PlaneMode == Absolute ) {
1097     aPlane.Orientation = CBAbsoluteOrientation->currentIndex();
1098     aPlane.X = SpinBox_X->value();
1099     aPlane.Y = SpinBox_Y->value();
1100     aPlane.Z = SpinBox_Z->value();
1101     aPlane.Dx = SpinBox_Dx->value();
1102     aPlane.Dy = SpinBox_Dy->value();
1103     aPlane.Dz = SpinBox_Dz->value();
1104   }
1105   else if( aPlane.PlaneMode == Relative ) {
1106     aPlane.RelativeMode.Orientation = CBRelativeOrientation->currentIndex();
1107     aPlane.RelativeMode.Distance = TLValueDistance->text().toDouble();
1108     aPlane.RelativeMode.Rotation1 = TLValueRotation1->text().remove("\xB0").toInt();
1109     aPlane.RelativeMode.Rotation2 = TLValueRotation2->text().remove("\xB0").toInt();
1110     RelativePlaneToAbsolute (aPlane, myModel->getAISContext(),myModel->trihedronSize() );
1111   }
1112   aPlane.IsOn  = isActivePlane->isChecked();
1113 }
1114
1115 /*!
1116   SLOT on reset button click: sets default values
1117 */
1118 void OCCViewer_ClippingDlg::onReset()
1119 {
1120   myBusy = true;
1121   SpinBox_X->setValue(0);
1122   SpinBox_Y->setValue(0);
1123   SpinBox_Z->setValue(0);
1124   myBusy = false;
1125
1126   updateClipping();
1127 }
1128
1129 /*!
1130   SLOT on invert button click: inverts normal of cutting plane
1131 */
1132 void OCCViewer_ClippingDlg::onInvert()
1133 {
1134   double Dx = SpinBox_Dx->value();
1135   double Dy = SpinBox_Dy->value();
1136   double Dz = SpinBox_Dz->value();
1137
1138   myBusy = true;
1139   SpinBox_Dx->setValue( -Dx );
1140   SpinBox_Dy->setValue( -Dy );
1141   SpinBox_Dz->setValue( -Dz );
1142   myBusy = false;
1143
1144   if ( clipPlanesCount() != 0 ) {
1145     int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
1146     OCCViewer_ClipPlane& aPlane = getClipPlane (aCurPlaneIndex);
1147     aPlane.IsInvert = !aPlane.IsInvert;
1148   }
1149   updateClipping();
1150 }
1151
1152 /*!
1153   SLOT: called on orientation of clipping plane in absolute mode changed
1154 */
1155 void OCCViewer_ClippingDlg::onOrientationAbsoluteChanged( int mode )
1156 {
1157   bool isUserMode = (mode==0);
1158
1159   TextLabelX->setEnabled( isUserMode );
1160   TextLabelY->setEnabled( isUserMode );
1161   TextLabelZ->setEnabled( isUserMode );
1162
1163   SpinBox_X->setEnabled( isUserMode );
1164   SpinBox_Y->setEnabled( isUserMode );
1165   SpinBox_Z->setEnabled( isUserMode );
1166
1167   TextLabelDx->setEnabled( isUserMode );
1168   TextLabelDy->setEnabled( isUserMode );
1169   TextLabelDz->setEnabled( isUserMode );
1170
1171   SpinBox_Dx->setEnabled( isUserMode );
1172   SpinBox_Dy->setEnabled( isUserMode );
1173   SpinBox_Dz->setEnabled( isUserMode );
1174
1175   if ( !isUserMode ) {
1176
1177     double aDx = 0, aDy = 0, aDz = 0;
1178
1179     if ( mode == 1 )
1180       {
1181         aDz = 1;
1182         TextLabelZ->setEnabled( true );
1183         SpinBox_Z->setEnabled( true );
1184         SpinBox_Z->setFocus();
1185       }
1186     else if ( mode == 2 )
1187       {
1188         aDx = 1;
1189         TextLabelX->setEnabled( true );
1190         SpinBox_X->setEnabled( true );
1191         SpinBox_X->setFocus();
1192       }
1193     else if ( mode == 3 )
1194       {
1195         aDy = 1;
1196         TextLabelY->setEnabled( true );
1197         SpinBox_Y->setEnabled( true );
1198         SpinBox_Y->setFocus();
1199       }
1200     
1201     int aCurPlaneIndex = ComboBoxPlanes->currentIndex();
1202     OCCViewer_ClipPlane& aPlane = getClipPlane (aCurPlaneIndex);
1203     if ( aPlane.IsInvert == true ) {
1204       aDx = -aDx; aDy = -aDy; aDz = -aDz;
1205     }
1206     
1207     myBusy = true;
1208     SpinBox_Dx->setValue( aDx );
1209     SpinBox_Dy->setValue( aDy );
1210     SpinBox_Dz->setValue( aDz );
1211     myBusy = false;
1212   }
1213   SetCurrentPlaneParam();
1214   updateClipping();
1215 }
1216
1217 /*!
1218   SLOT: called on orientation of clipping plane in relative mode changed
1219 */
1220 void OCCViewer_ClippingDlg::onOrientationRelativeChanged (int theItem)
1221 {
1222   if ( clipPlanesCount() == 0 )
1223     return;
1224   
1225   if ( theItem == 0 ) {
1226     TextLabelRotation1->setText( tr( "ROTATION_AROUND_X_Y2Z" ) );
1227     TextLabelRotation2->setText( tr( "ROTATION_AROUND_Y_X2Z" ) );
1228   }
1229   else if ( theItem == 1 ) {
1230     TextLabelRotation1->setText( tr( "ROTATION_AROUND_Y_Z2X" ) );
1231     TextLabelRotation2->setText( tr( "ROTATION_AROUND_Z_Y2X" ) );
1232   }
1233   else if ( theItem == 2 ) {
1234     TextLabelRotation1->setText( tr( "ROTATION_AROUND_Z_X2Y" ) );
1235     TextLabelRotation2->setText( tr( "ROTATION_AROUND_X_Z2Y" ) );
1236   }
1237
1238   if( (QComboBox*)sender() == CBRelativeOrientation )
1239     SetCurrentPlaneParam();
1240   updateClipping();
1241 }
1242
1243 /*!
1244   SLOT: called on preview check box toggled
1245 */
1246 void OCCViewer_ClippingDlg::onPreview( bool on )
1247 {
1248   erasePreview();
1249   if ( on ) 
1250     displayPreview();
1251 }
1252
1253 /*!
1254   SLOT: called on Auto Apply check box toggled
1255 */
1256 void OCCViewer_ClippingDlg::onAutoApply( bool toggled )
1257 {
1258   if ( toggled ) {
1259     onApply();
1260     myModel->update();
1261   }  
1262 }
1263
1264 /*!
1265   SLOT on Apply button click: sets cutting plane
1266 */
1267 void OCCViewer_ClippingDlg::onApply()
1268 {
1269   if ( myBusy )
1270     return;
1271   myIsSelectPlane = true;
1272
1273   qApp->processEvents();
1274   QApplication::setOverrideCursor( Qt::WaitCursor );
1275   qApp->processEvents();
1276
1277   myModel->setClipPlanes(myLocalPlanes);
1278
1279   QApplication::restoreOverrideCursor();
1280   myIsSelectPlane = false;
1281 }
1282
1283 /*!
1284   SLOT: Called when value of slider distance change
1285 */
1286 void OCCViewer_ClippingDlg::SliderDistanceHasMoved( int value )
1287 {
1288   double new_value = value/100.;
1289   TLValueDistance->setText( QString("%1").arg( new_value ) );
1290   onValueChanged();
1291 }
1292
1293 /*!
1294   SLOT: Called when value of slider rotation1 change
1295 */
1296 void OCCViewer_ClippingDlg::SliderRotation1HasMoved( int value )
1297 {
1298   TLValueRotation1->setText( QString("%1\xB0").arg( value ) );
1299   onValueChanged();
1300 }
1301
1302 /*!
1303   SLOT: Called when value of slider rotation2 change
1304 */
1305 void OCCViewer_ClippingDlg::SliderRotation2HasMoved( int value )
1306 {
1307   TLValueRotation2->setText( QString("%1\xB0").arg( value ) );
1308   onValueChanged();
1309 }
1310
1311 OCCViewer_ClipPlane& OCCViewer_ClippingDlg::getClipPlane (int theIndex) {
1312   return myLocalPlanes[theIndex];
1313 }
1314
1315 int OCCViewer_ClippingDlg::clipPlanesCount() {
1316   return myLocalPlanes.size();
1317 }