Salome HOME
PAL9391
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_ClippingDlg.cxx
1 //  SMESH SMESHGUI : GUI for SMESH component
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //
24 //  File   : SMESHGUI_ClippingDlg.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SMESH
27 //  $Header$
28
29 #include "SMESHGUI_ClippingDlg.h"
30
31 #include "SMESHGUI.h"
32 #include "SMESHGUI_Utils.h"
33 #include "SMESHGUI_VTKUtils.h"
34
35 #include "SMESH_Actor.h"
36 #include "SMESH_ActorUtils.h"
37
38 #include "SUIT_Session.h"
39 #include "SUIT_OverrideCursor.h"
40
41 #include "SALOME_ListIO.hxx"
42 #include "SALOME_InteractiveObject.hxx"
43 #include "SALOME_ListIteratorOfListIO.hxx"
44
45 #include "SalomeApp_Application.h"
46 #include "SalomeApp_SelectionMgr.h"
47
48 #include "SVTK_Selector.h"
49 #include "SVTK_ViewWindow.h"
50
51 // QT Includes
52 #include <qlabel.h>
53 #include <qpushbutton.h>
54 #include <qcombobox.h>
55 #include <qcheckbox.h>
56 #include <qlayout.h>
57 #include <qgroupbox.h>
58
59 // VTK Includes
60 #include <vtkMath.h>
61 #include <vtkCamera.h>
62 #include <vtkRenderer.h>
63 #include <vtkImplicitBoolean.h>
64 #include <vtkImplicitFunctionCollection.h>
65 #include <vtkObjectFactory.h>
66
67 #include <vtkDataSet.h>
68 #include <vtkDataSetMapper.h>
69 #include <vtkPlaneSource.h>
70 #include <vtkPolyData.h>
71 #include <vtkRenderer.h>
72
73 // STL includes
74 #include <algorithm>
75
76 using namespace std;
77
78
79 class OrientedPlane: public vtkPlane
80 {
81   SVTK_ViewWindow* myViewWindow;
82
83   vtkDataSetMapper* myMapper;
84
85 public:
86   static OrientedPlane *New()
87   {
88     return new OrientedPlane();
89   }
90   static OrientedPlane *New(SVTK_ViewWindow* theViewWindow)
91   {
92     return new OrientedPlane(theViewWindow);
93   }
94   vtkTypeMacro (OrientedPlane, vtkPlane);
95
96   SMESH::Orientation myOrientation;
97   float myDistance;
98   double myAngle[2];
99
100   vtkPlaneSource* myPlaneSource;
101   SALOME_Actor *myActor;
102
103   void SetOrientation (SMESH::Orientation theOrientation) { myOrientation = theOrientation; }
104   SMESH::Orientation GetOrientation() { return myOrientation; }
105
106   void SetDistance (float theDistance) { myDistance = theDistance; }
107   float GetDistance() { return myDistance; }
108
109   void ShallowCopy (OrientedPlane* theOrientedPlane)
110   {
111     SetNormal(theOrientedPlane->GetNormal());
112     SetOrigin(theOrientedPlane->GetOrigin());
113
114     myOrientation = theOrientedPlane->GetOrientation();
115     myDistance = theOrientedPlane->GetDistance();
116
117     myAngle[0] = theOrientedPlane->myAngle[0];
118     myAngle[1] = theOrientedPlane->myAngle[1];
119
120     myPlaneSource->SetNormal(theOrientedPlane->myPlaneSource->GetNormal());
121     myPlaneSource->SetOrigin(theOrientedPlane->myPlaneSource->GetOrigin());
122     myPlaneSource->SetPoint1(theOrientedPlane->myPlaneSource->GetPoint1());
123     myPlaneSource->SetPoint2(theOrientedPlane->myPlaneSource->GetPoint2());
124   }
125
126 protected:
127   OrientedPlane(SVTK_ViewWindow* theViewWindow):
128     myViewWindow(theViewWindow),
129     myOrientation(SMESH::XY),
130     myDistance(0.5)
131   {
132     Init();
133     myViewWindow->AddActor(myActor);
134   }
135
136   OrientedPlane():
137     myOrientation(SMESH::XY),
138     myViewWindow(NULL),
139     myDistance(0.5)
140   {
141     Init();
142   }
143
144   void Init()
145   {
146     myPlaneSource = vtkPlaneSource::New();
147
148     myAngle[0] = myAngle[1] = 0.0;
149
150     // Create and display actor
151     myMapper = vtkDataSetMapper::New();
152     myMapper->SetInput(myPlaneSource->GetOutput());
153
154     myActor = SALOME_Actor::New();
155     myActor->VisibilityOff();
156     myActor->PickableOff();
157     myActor->SetInfinitive(true);
158     myActor->SetMapper(myMapper);
159
160     float anRGB[3];
161     vtkProperty* aProp = vtkProperty::New();
162     SMESH::GetColor( "SMESH", "fill_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 170, 255 ) );
163     aProp->SetColor(anRGB[0],anRGB[1],anRGB[2]);
164     aProp->SetOpacity(0.75);
165     myActor->SetProperty(aProp);
166     aProp->Delete();
167
168     vtkProperty* aBackProp = vtkProperty::New();
169     SMESH::GetColor( "SMESH", "backface_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 0, 255 ) );
170     aBackProp->SetColor(anRGB[0],anRGB[1],anRGB[2]);
171     aBackProp->SetOpacity(0.75);
172     myActor->SetBackfaceProperty(aBackProp);
173     aBackProp->Delete();
174   }
175
176   ~OrientedPlane(){
177     myViewWindow->RemoveActor(myActor);
178     myActor->Delete();
179     
180     myMapper->RemoveAllInputs();
181     myMapper->Delete();
182
183     myPlaneSource->UnRegisterAllOutputs();
184     myPlaneSource->Delete();
185   };
186
187 private:
188   // Not implemented.
189   OrientedPlane (const OrientedPlane&);
190   void operator= (const OrientedPlane&);
191
192 };
193
194 struct TSetVisiblity {
195   TSetVisiblity(int theIsVisible): myIsVisible(theIsVisible){}
196   void operator()(SMESH::TVTKPlane& theOrientedPlane){
197     theOrientedPlane->myActor->SetVisibility(myIsVisible);
198   }
199   int myIsVisible;
200 };
201
202 //=================================================================================
203 // class    : SMESHGUI_ClippingDlg()
204 // purpose  :
205 //
206 //=================================================================================
207 SMESHGUI_ClippingDlg::SMESHGUI_ClippingDlg (SMESHGUI* theModule,
208                                             const char* name,
209                                             bool modal,
210                                             WFlags fl):
211   QDialog(SMESH::GetDesktop(theModule),
212           name, 
213           modal, 
214           WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose),
215   mySelector(SMESH::GetViewWindow(theModule)->GetSelector()),
216   mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
217   mySMESHGUI(theModule)
218 {
219   if (!name)
220     setName("SMESHGUI_ClippingDlg");
221   setCaption(tr("SMESH_CLIPPING_TITLE"));
222   setSizeGripEnabled(TRUE);
223   QGridLayout* SMESHGUI_ClippingDlgLayout = new QGridLayout(this);
224   SMESHGUI_ClippingDlgLayout->setSpacing(6);
225   SMESHGUI_ClippingDlgLayout->setMargin(11);
226
227   // Controls for selecting, creating, deleting planes
228   QGroupBox* GroupPlanes = new QGroupBox (this, "GroupPlanes");
229   GroupPlanes->setTitle(tr("Clipping planes"));
230   GroupPlanes->setColumnLayout(0, Qt::Vertical);
231   GroupPlanes->layout()->setSpacing(0);
232   GroupPlanes->layout()->setMargin(0);
233   QGridLayout* GroupPlanesLayout = new QGridLayout (GroupPlanes->layout());
234   GroupPlanesLayout->setAlignment(Qt::AlignTop);
235   GroupPlanesLayout->setSpacing(6);
236   GroupPlanesLayout->setMargin(11);
237
238   ComboBoxPlanes = new QComboBox(GroupPlanes, "ComboBoxPlanes");
239   GroupPlanesLayout->addWidget(ComboBoxPlanes, 0, 0);
240
241   QSpacerItem* spacerGP = new QSpacerItem (20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
242   GroupPlanesLayout->addItem(spacerGP, 0, 1);
243
244   buttonNew = new QPushButton (GroupPlanes, "buttonNew");
245   buttonNew->setText(tr("SMESH_BUT_NEW"));
246   GroupPlanesLayout->addWidget(buttonNew, 0, 2);
247
248   buttonDelete = new QPushButton (GroupPlanes, "buttonDelete");
249   buttonDelete->setText(tr("SMESH_BUT_DELETE"));
250   GroupPlanesLayout->addWidget(buttonDelete, 0, 3);
251
252   // Controls for defining plane parameters
253   QGroupBox* GroupParameters = new QGroupBox (this, "GroupParameters");
254   GroupParameters->setTitle(tr("SMESH_PARAMETERS"));
255   GroupParameters->setColumnLayout(0, Qt::Vertical);
256   GroupParameters->layout()->setSpacing(0);
257   GroupParameters->layout()->setMargin(0);
258   QGridLayout* GroupParametersLayout = new QGridLayout (GroupParameters->layout());
259   GroupParametersLayout->setAlignment(Qt::AlignTop);
260   GroupParametersLayout->setSpacing(6);
261   GroupParametersLayout->setMargin(11);
262
263   TextLabelOrientation = new QLabel(GroupParameters, "TextLabelOrientation");
264   TextLabelOrientation->setText(tr("SMESH_ORIENTATION"));
265   GroupParametersLayout->addWidget(TextLabelOrientation, 0, 0);
266
267   ComboBoxOrientation = new QComboBox(GroupParameters, "ComboBoxOrientation");
268   GroupParametersLayout->addWidget(ComboBoxOrientation, 0, 1);
269
270   TextLabelDistance = new QLabel(GroupParameters, "TextLabelDistance");
271   TextLabelDistance->setText(tr("SMESH_DISTANCE"));
272   GroupParametersLayout->addWidget(TextLabelDistance, 1, 0);
273
274   SpinBoxDistance = new SMESHGUI_SpinBox(GroupParameters, "SpinBoxDistance");
275   GroupParametersLayout->addWidget(SpinBoxDistance, 1, 1);
276
277   TextLabelRot1 = new QLabel(GroupParameters, "TextLabelRot1");
278   TextLabelRot1->setText(tr("Rotation around X (Y to Z):"));
279   GroupParametersLayout->addWidget(TextLabelRot1, 2, 0);
280
281   SpinBoxRot1 = new SMESHGUI_SpinBox(GroupParameters, "SpinBoxRot1");
282   GroupParametersLayout->addWidget(SpinBoxRot1, 2, 1);
283
284   TextLabelRot2 = new QLabel(GroupParameters, "TextLabelRot2");
285   TextLabelRot2->setText(tr("Rotation around Y (X to Z):"));
286   GroupParametersLayout->addWidget(TextLabelRot2, 3, 0);
287
288   SpinBoxRot2 = new SMESHGUI_SpinBox(GroupParameters, "SpinBoxRot2");
289   GroupParametersLayout->addWidget(SpinBoxRot2, 3, 1);
290
291   PreviewCheckBox = new QCheckBox(tr("Show preview"), GroupParameters);
292   PreviewCheckBox->setChecked(true);
293   GroupParametersLayout->addWidget(PreviewCheckBox, 4, 0);
294
295   AutoApplyCheckBox = new QCheckBox(tr("Auto Apply"), GroupParameters);
296   AutoApplyCheckBox->setChecked(false);
297   GroupParametersLayout->addWidget(AutoApplyCheckBox, 4, 1);
298
299   // Controls for "Ok", "Apply" and "Close" button
300   QGroupBox* GroupButtons = new QGroupBox(this, "GroupButtons");
301   GroupButtons->setSizePolicy(QSizePolicy((QSizePolicy::SizeType)7, (QSizePolicy::SizeType)0, 0, 0, GroupButtons->sizePolicy().hasHeightForWidth()));
302   GroupButtons->setGeometry(QRect(10, 10, 281, 48));
303   GroupButtons->setTitle(tr("" ));
304   GroupButtons->setColumnLayout(0, Qt::Vertical);
305   GroupButtons->layout()->setSpacing(0);
306   GroupButtons->layout()->setMargin(0);
307   QGridLayout* GroupButtonsLayout = new QGridLayout(GroupButtons->layout());
308   GroupButtonsLayout->setAlignment(Qt::AlignTop);
309   GroupButtonsLayout->setSpacing(6);
310   GroupButtonsLayout->setMargin(11);
311   buttonCancel = new QPushButton(GroupButtons, "buttonCancel");
312   buttonCancel->setText(tr("SMESH_BUT_CLOSE" ));
313   buttonCancel->setAutoDefault(TRUE);
314   GroupButtonsLayout->addWidget(buttonCancel, 0, 3);
315   buttonApply = new QPushButton(GroupButtons, "buttonApply");
316   buttonApply->setText(tr("SMESH_BUT_APPLY" ));
317   buttonApply->setAutoDefault(TRUE);
318   GroupButtonsLayout->addWidget(buttonApply, 0, 1);
319   QSpacerItem* spacer_9 = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
320   GroupButtonsLayout->addItem(spacer_9, 0, 2);
321   buttonOk = new QPushButton(GroupButtons, "buttonOk");
322   buttonOk->setText(tr("SMESH_BUT_OK" ));
323   buttonOk->setAutoDefault(TRUE);
324   buttonOk->setDefault(TRUE);
325   GroupButtonsLayout->addWidget(buttonOk, 0, 0);
326
327   SMESHGUI_ClippingDlgLayout->addWidget(GroupPlanes,      0, 0);
328   SMESHGUI_ClippingDlgLayout->addWidget(GroupParameters,  1, 0);
329   SMESHGUI_ClippingDlgLayout->addWidget(GroupButtons,     2, 0);
330
331   // Initial state
332   SpinBoxDistance->RangeStepAndValidator(0.0, 1.0, 0.01, 3);
333   SpinBoxRot1->RangeStepAndValidator(-180.0, 180.0, 1, 3);
334   SpinBoxRot2->RangeStepAndValidator(-180.0, 180.0, 1, 3);
335
336   ComboBoxOrientation->insertItem(tr("|| X-Y"));
337   ComboBoxOrientation->insertItem(tr("|| Y-Z"));
338   ComboBoxOrientation->insertItem(tr("|| Z-X"));
339
340   SpinBoxDistance->SetValue(0.5);
341
342   myActor = 0;
343   myIsSelectPlane = false;
344   onSelectionChanged();
345
346   // signals and slots connections :
347   connect(ComboBoxPlanes, SIGNAL(activated(int)), this, SLOT(onSelectPlane(int)));
348   connect(buttonNew, SIGNAL(clicked()), this, SLOT(ClickOnNew()));
349   connect(buttonDelete, SIGNAL(clicked()), this, SLOT(ClickOnDelete()));
350   connect(ComboBoxOrientation, SIGNAL(activated(int)), this, SLOT(onSelectOrientation(int)));
351   connect(SpinBoxDistance, SIGNAL(valueChanged(double)), this, SLOT(SetCurrentPlaneParam()));
352   connect(SpinBoxRot1, SIGNAL(valueChanged(double)), this, SLOT(SetCurrentPlaneParam()));
353   connect(SpinBoxRot2, SIGNAL(valueChanged(double)), this, SLOT(SetCurrentPlaneParam()));
354   connect(PreviewCheckBox, SIGNAL(toggled(bool)), this, SLOT(OnPreviewToggle(bool)));
355   connect(AutoApplyCheckBox, SIGNAL(toggled(bool)), this, SLOT(ClickOnApply()));
356   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
357   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
358   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
359   connect(mySMESHGUI, SIGNAL (SignalCloseAllDialogs()), this, SLOT(ClickOnOk()));
360   connect(mySelectionMgr,  SIGNAL(currentSelectionChanged()), this, SLOT(onSelectionChanged()));
361   /* to close dialog if study frame change */
362   connect(mySMESHGUI, SIGNAL (SignalStudyFrameChanged()), this, SLOT(ClickOnCancel()));
363
364   /* Move widget on the botton right corner of main widget */
365   int x, y;
366   mySMESHGUI->DefineDlgPosition(this, x, y);
367   this->move(x, y);
368   this->show();
369 }
370
371 //=================================================================================
372 // function : ~SMESHGUI_ClippingDlg()
373 // purpose  :
374 //=================================================================================
375 SMESHGUI_ClippingDlg::~SMESHGUI_ClippingDlg()
376 {
377   // no need to delete child widgets, Qt does it all for us
378   std::for_each(myPlanes.begin(),myPlanes.end(),TSetVisiblity(false));
379   SMESH::RenderViewWindow(SMESH::GetViewWindow(mySMESHGUI));
380 }
381
382 //=======================================================================
383 // function : ClickOnApply()
384 // purpose  :
385 //=======================================================================
386 void SMESHGUI_ClippingDlg::ClickOnApply()
387 {
388   if (!myActor)
389     return;
390
391   if (SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow(mySMESHGUI)) {
392     SUIT_OverrideCursor wc;
393
394     myActor->RemoveAllClippingPlanes();
395
396     SMESH::TPlanes::iterator anIter = myPlanes.begin();
397     for (; anIter != myPlanes.end(); anIter++) {
398       OrientedPlane* anOrientedPlane = OrientedPlane::New(aViewWindow);
399       anOrientedPlane->ShallowCopy(anIter->GetPointer());
400       myActor->AddClippingPlane(anOrientedPlane);
401       anOrientedPlane->Delete();
402     }
403
404     SMESH::RenderViewWindow(aViewWindow);
405   }
406 }
407
408 //=======================================================================
409 // function : ClickOnOk()
410 // purpose  :
411 //=======================================================================
412 void SMESHGUI_ClippingDlg::ClickOnOk()
413 {
414   ClickOnApply();
415   ClickOnCancel();
416 }
417
418 //=======================================================================
419 // function : ClickOnCancel()
420 // purpose  :
421 //=======================================================================
422 void SMESHGUI_ClippingDlg::ClickOnCancel()
423 {
424   close();
425 }
426
427 //=================================================================================
428 // function : onSelectionChanged()
429 // purpose  : Called when selection is changed
430 //=================================================================================
431 void SMESHGUI_ClippingDlg::onSelectionChanged()
432 {
433   if (SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow(mySMESHGUI)) {
434     const SALOME_ListIO& aList = mySelector->StoredIObjects();
435     if (aList.Extent() > 0) {
436       Handle(SALOME_InteractiveObject) IOS = aList.First();
437       myActor = SMESH::FindActorByEntry(IOS->getEntry());
438       if (myActor) {
439         std::for_each(myPlanes.begin(),myPlanes.end(),TSetVisiblity(false));
440         myPlanes.clear();
441
442         vtkIdType anId = 0, anEnd = myActor->GetNumberOfClippingPlanes();
443         for (; anId < anEnd; anId++) {
444           if (vtkImplicitFunction* aFunction = myActor->GetClippingPlane(anId)) {
445             if(OrientedPlane* aPlane = OrientedPlane::SafeDownCast(aFunction)){
446               OrientedPlane* anOrientedPlane = OrientedPlane::New(aViewWindow);
447               SMESH::TVTKPlane aTVTKPlane(anOrientedPlane);
448               anOrientedPlane->Delete();
449               aTVTKPlane->ShallowCopy(aPlane);
450               myPlanes.push_back(aTVTKPlane);
451             }
452           }
453         }
454
455         std::for_each(myPlanes.begin(),myPlanes.end(),
456                       TSetVisiblity(PreviewCheckBox->isChecked()));
457       }
458     }
459     SMESH::RenderViewWindow(aViewWindow);
460   }
461   Sinchronize();
462 }
463
464 //=======================================================================
465 // function : onSelectPlane()
466 // purpose  :
467 //=======================================================================
468 void SMESHGUI_ClippingDlg::onSelectPlane (int theIndex)
469 {
470   if (!myActor || myPlanes.empty())
471     return;
472
473   OrientedPlane* aPlane = myPlanes[theIndex].GetPointer();
474
475   // Orientation
476   SMESH::Orientation anOrientation = aPlane->GetOrientation();
477
478   // Rotations
479   double aRot[2] = {aPlane->myAngle[0], aPlane->myAngle[1]};
480
481   // Set plane parameters in the dialog
482   myIsSelectPlane = true;
483   setDistance(aPlane->GetDistance());
484   setRotation(aRot[0], aRot[1]);
485   switch (anOrientation) {
486   case SMESH::XY:
487     ComboBoxOrientation->setCurrentItem(0);
488     onSelectOrientation(0);
489     break;
490   case SMESH::YZ:
491     ComboBoxOrientation->setCurrentItem(1);
492     onSelectOrientation(1);
493     break;
494   case SMESH::ZX:
495     ComboBoxOrientation->setCurrentItem(2);
496     onSelectOrientation(2);
497     break;
498   }
499   myIsSelectPlane = false;
500 }
501
502 //=======================================================================
503 // function : ClickOnNew()
504 // purpose  :
505 //=======================================================================
506 void SMESHGUI_ClippingDlg::ClickOnNew()
507 {
508   if (!myActor)
509     return;
510
511   if(SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow(mySMESHGUI)){
512     OrientedPlane* aPlane = OrientedPlane::New(aViewWindow);
513     SMESH::TVTKPlane aTVTKPlane(aPlane);
514     myPlanes.push_back(aTVTKPlane);
515
516     if (PreviewCheckBox->isChecked())
517       aTVTKPlane->myActor->VisibilityOn();
518     
519     Sinchronize();
520     SetCurrentPlaneParam();
521   }
522 }
523
524 //=======================================================================
525 // function : ClickOnDelete()
526 // purpose  :
527 //=======================================================================
528 void SMESHGUI_ClippingDlg::ClickOnDelete()
529 {
530   if (!myActor || myPlanes.empty())
531     return;
532
533   int aPlaneIndex = ComboBoxPlanes->currentItem();
534
535   SMESH::TPlanes::iterator anIter = myPlanes.begin() + aPlaneIndex;
536   anIter->GetPointer()->myActor->SetVisibility(false);
537   myPlanes.erase(anIter);
538
539   if(AutoApplyCheckBox->isChecked())
540     ClickOnApply();
541
542   Sinchronize();
543   SMESH::RenderViewWindow(SMESH::GetCurrentVtkView());
544 }
545
546 //=======================================================================
547 // function : onSelectOrientation()
548 // purpose  :
549 //=======================================================================
550 void SMESHGUI_ClippingDlg::onSelectOrientation (int theItem)
551 {
552   if (myPlanes.empty())
553     return;
554
555   if      (theItem == 0) {
556     TextLabelRot1->setText(tr("Rotation around X (Y to Z):"));
557     TextLabelRot2->setText(tr("Rotation around Y (X to Z):"));
558   }
559   else if (theItem == 1) {
560     TextLabelRot1->setText(tr("Rotation around Y (Z to X):"));
561     TextLabelRot2->setText(tr("Rotation around Z (Y to X):"));
562   }
563   else if (theItem == 2) {
564     TextLabelRot1->setText(tr("Rotation around Z (X to Y):"));
565     TextLabelRot2->setText(tr("Rotation around X (Z to Y):"));
566   }
567
568   if((QComboBox*)sender() == ComboBoxOrientation)
569     SetCurrentPlaneParam();
570 }
571
572 //=======================================================================
573 // function : Sinchronize()
574 // purpose  :
575 //=======================================================================
576 void SMESHGUI_ClippingDlg::Sinchronize()
577 {
578   int aNbPlanes = myPlanes.size();
579   ComboBoxPlanes->clear();
580
581   QString aName;
582   for(int i = 1; i<=aNbPlanes; i++) {
583     aName = QString(tr("Plane# %1")).arg(i);
584     ComboBoxPlanes->insertItem(aName);
585   }
586
587   int aPos = ComboBoxPlanes->count() - 1;
588   ComboBoxPlanes->setCurrentItem(aPos);
589
590   bool anIsControlsEnable = (aPos >= 0);
591   if (anIsControlsEnable) {
592     onSelectPlane(aPos);
593   } else {
594     ComboBoxPlanes->insertItem(tr("No planes"));
595     SpinBoxRot1->SetValue(0.0);
596     SpinBoxRot2->SetValue(0.0);
597     SpinBoxDistance->SetValue(0.5);
598   }
599
600   buttonDelete->setEnabled(anIsControlsEnable);
601   buttonApply->setEnabled(anIsControlsEnable);
602   PreviewCheckBox->setEnabled(anIsControlsEnable);
603   AutoApplyCheckBox->setEnabled(anIsControlsEnable);
604   ComboBoxOrientation->setEnabled(anIsControlsEnable);
605   SpinBoxDistance->setEnabled(anIsControlsEnable);
606   SpinBoxRot1->setEnabled(anIsControlsEnable);
607   SpinBoxRot2->setEnabled(anIsControlsEnable);
608 }
609
610 //=======================================================================
611 // function : setRotation()
612 // purpose  :
613 //=======================================================================
614 void SMESHGUI_ClippingDlg::setRotation (const double theRot1, const double theRot2)
615 {
616   SpinBoxRot1->SetValue(theRot1);
617   SpinBoxRot2->SetValue(theRot2);
618 }
619
620 //=======================================================================
621 // function : SetCurrentPlaneParam()
622 // purpose  :
623 //=======================================================================
624 void SMESHGUI_ClippingDlg::SetCurrentPlaneParam()
625 {
626   if (myPlanes.empty() || myIsSelectPlane)
627     return;
628
629   int aCurPlaneIndex = ComboBoxPlanes->currentItem();
630
631   OrientedPlane* aPlane = myPlanes[aCurPlaneIndex].GetPointer();
632
633   float aNormal[3];
634   SMESH::Orientation anOrientation;
635   float aDir[3][3] = {{0, 0, 0}, {0, 0, 0}};
636   {
637     static double aCoeff = vtkMath::Pi()/180.0;
638
639     float aRot[2] = {getRotation1(), getRotation2()};
640     aPlane->myAngle[0] = aRot[0];
641     aPlane->myAngle[1] = aRot[1];
642
643     float anU[2] = {cos(aCoeff*aRot[0]), cos(aCoeff*aRot[1])};
644     float aV[2] = {sqrt(1.0-anU[0]*anU[0]), sqrt(1.0-anU[1]*anU[1])};
645     aV[0] = aRot[0] > 0? aV[0]: -aV[0];
646     aV[1] = aRot[1] > 0? aV[1]: -aV[1];
647
648     switch (ComboBoxOrientation->currentItem()) {
649     case 0:
650       anOrientation = SMESH::XY;
651
652       aDir[0][1] = anU[0];
653       aDir[0][2] = aV[0];
654
655       aDir[1][0] = anU[1];
656       aDir[1][2] = aV[1];
657
658       break;
659     case 1:
660       anOrientation = SMESH::YZ;
661
662       aDir[0][2] = anU[0];
663       aDir[0][0] = aV[0];
664
665       aDir[1][1] = anU[1];
666       aDir[1][0] = aV[1];
667
668       break;
669     case 2:
670       anOrientation = SMESH::ZX;
671
672       aDir[0][0] = anU[0];
673       aDir[0][1] = aV[0];
674
675       aDir[1][2] = anU[1];
676       aDir[1][1] = aV[1];
677
678       break;
679     }
680
681     vtkMath::Cross(aDir[1],aDir[0],aNormal);
682     vtkMath::Normalize(aNormal);
683     vtkMath::Cross(aNormal,aDir[1],aDir[0]);
684   }
685
686   aPlane->SetOrientation(anOrientation);
687   aPlane->SetDistance(getDistance());
688
689   myActor->SetPlaneParam(aNormal, getDistance(), aPlane);
690
691   vtkDataSet* aDataSet = myActor->GetInput();
692   float *aPnt = aDataSet->GetCenter();
693
694   float* anOrigin = aPlane->GetOrigin();
695   float aDel = aDataSet->GetLength()/2.0;
696
697   float aDelta[2][3] = {{aDir[0][0]*aDel, aDir[0][1]*aDel, aDir[0][2]*aDel},
698                         {aDir[1][0]*aDel, aDir[1][1]*aDel, aDir[1][2]*aDel}};
699   float aParam, aPnt0[3], aPnt1[3], aPnt2[3];
700
701   float aPnt01[3] = {aPnt[0] - aDelta[0][0] - aDelta[1][0],
702                      aPnt[1] - aDelta[0][1] - aDelta[1][1],
703                      aPnt[2] - aDelta[0][2] - aDelta[1][2]};
704   float aPnt02[3] = {aPnt01[0] + aNormal[0],
705                       aPnt01[1] + aNormal[1],
706                       aPnt01[2] + aNormal[2]};
707   vtkPlane::IntersectWithLine(aPnt01,aPnt02,aNormal,anOrigin,aParam,aPnt0);
708
709   float aPnt11[3] = {aPnt[0] - aDelta[0][0] + aDelta[1][0],
710                      aPnt[1] - aDelta[0][1] + aDelta[1][1],
711                      aPnt[2] - aDelta[0][2] + aDelta[1][2]};
712   float aPnt12[3] = {aPnt11[0] + aNormal[0],
713                      aPnt11[1] + aNormal[1],
714                      aPnt11[2] + aNormal[2]};
715   vtkPlane::IntersectWithLine(aPnt11,aPnt12,aNormal,anOrigin,aParam,aPnt1);
716
717   float aPnt21[3] = {aPnt[0] + aDelta[0][0] - aDelta[1][0],
718                      aPnt[1] + aDelta[0][1] - aDelta[1][1],
719                      aPnt[2] + aDelta[0][2] - aDelta[1][2]};
720   float aPnt22[3] = {aPnt21[0] + aNormal[0],
721                      aPnt21[1] + aNormal[1],
722                      aPnt21[2] + aNormal[2]};
723   vtkPlane::IntersectWithLine(aPnt21,aPnt22,aNormal,anOrigin,aParam,aPnt2);
724
725   vtkPlaneSource* aPlaneSource = aPlane->myPlaneSource;
726   aPlaneSource->SetNormal(aNormal[0],aNormal[1],aNormal[2]);
727   aPlaneSource->SetOrigin(aPnt0[0],aPnt0[1],aPnt0[2]);
728   aPlaneSource->SetPoint1(aPnt1[0],aPnt1[1],aPnt1[2]);
729   aPlaneSource->SetPoint2(aPnt2[0],aPnt2[1],aPnt2[2]);
730
731   if(AutoApplyCheckBox->isChecked())
732     ClickOnApply();
733
734   SMESH::RenderViewWindow(SMESH::GetCurrentVtkView());
735 }
736
737 //=======================================================================
738 // function : OnPreviewToggle()
739 // purpose  :
740 //=======================================================================
741 void SMESHGUI_ClippingDlg::OnPreviewToggle (bool theIsToggled)
742 {
743   std::for_each(myPlanes.begin(),myPlanes.end(),TSetVisiblity(theIsToggled));
744   SMESH::RenderViewWindow(SMESH::GetCurrentVtkView());
745 }