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