Salome HOME
01e9687ebe7bb24334e690652674e5ab6b98c7fc
[modules/visu.git] / src / VISUGUI / VisuGUI_ClippingPanel.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "VisuGUI_ClippingPanel.h"
21 #include "VisuGUI.h"
22 #include "VisuGUI_Tools.h"
23 #include "VisuGUI_ClippingPlaneDlg.h"
24 #include "VisuGUI_ViewTools.h"
25
26 #include <VISU_ColoredPrs3dCache_i.hh>
27
28 #include <VISU_Gen_i.hh>
29 #include <VISU_ColoredPrs3dHolder_i.hh>
30
31 #include <LightApp_Application.h>
32 #include <SUIT_Desktop.h>
33 #include <SUIT_Session.h>
34 #include <SUIT_ViewManager.h>
35 #include <SUIT_MessageBox.h>
36 #include <SUIT_ResourceMgr.h>
37 #include <SUIT_ViewWindow.h>
38 #include <SVTK_ViewWindow.h>
39 #include <VTKViewer_Utilities.h>
40
41
42
43 #include <QVBoxLayout>
44 #include <QHBoxLayout>
45 #include <QPushButton>
46 #include <QToolButton>
47 #include <QGroupBox>
48 #include <QListWidget>
49 #include <QTableWidget>
50 #include <QStringList>
51 #include <QMessageBox>
52 #include <QCheckBox>
53
54 #include <vtkImageData.h>
55 #include <vtkEDFCutter.h>
56 #include <vtkPolyDataMapper.h>
57
58 #include <SALOMEDSClient_AttributeSequenceOfReal.hxx>
59 #include <SALOMEDSClient_AttributeInteger.hxx>
60
61
62
63 #define SIZEFACTOR 1.1
64
65
66 #define PRINT3(MARK, VAL) printf("#### %s x=%f, y=%f, z=%f\n", MARK, VAL[0], VAL[1], VAL[2]);
67
68 using namespace std;
69
70
71 void AdjustBounds(const double bounds[6], double newBounds[6], double center[3])
72 {
73   center[0] = (bounds[0] + bounds[1])/2.0;
74   center[1] = (bounds[2] + bounds[3])/2.0;
75   center[2] = (bounds[4] + bounds[5])/2.0;
76   
77   newBounds[0] = center[0] + SIZEFACTOR*(bounds[0]-center[0]);
78   newBounds[1] = center[0] + SIZEFACTOR*(bounds[1]-center[0]);
79   newBounds[2] = center[1] + SIZEFACTOR*(bounds[2]-center[1]);
80   newBounds[3] = center[1] + SIZEFACTOR*(bounds[3]-center[1]);
81   newBounds[4] = center[2] + SIZEFACTOR*(bounds[4]-center[2]);
82   newBounds[5] = center[2] + SIZEFACTOR*(bounds[5]-center[2]);
83 }
84
85
86
87 //****************************************************************
88 //PreviewPlane::PreviewPlane(SVTK_ViewWindow* theWindow, const PlaneDef& thePlane, const double* theBounds):
89 PreviewPlane::PreviewPlane(SVTK_ViewWindow* theWindow,
90                            VISU_CutPlaneFunction* thePlane,
91                            const double* theBounds):
92   myWindow(theWindow),
93   myBounds(theBounds)
94
95   double aCenter[3];
96   double aBound[6];
97
98   AdjustBounds(myBounds, aBound, aCenter);
99
100   //myPlane = thePlane.plane;
101   myPlane = thePlane;
102
103   myBox = vtkImageData::New();
104   myBox->SetDimensions(2, 2, 2);
105   myBox->SetOrigin(aBound[0],aBound[2],aBound[4]);
106   myBox->SetSpacing((aBound[1]-aBound[0]),
107                     (aBound[3]-aBound[2]),
108                     (aBound[5]-aBound[4]));
109
110   myCutter = vtkEDFCutter::New();
111   myCutter->SetInput(myBox);
112   myCutter->SetCutFunction(myPlane);
113
114   myMapper = vtkPolyDataMapper::New();
115   myMapper->SetInput(myCutter->GetOutput());
116
117   myActor = vtkActor::New();
118   myActor->VisibilityOff();
119   myActor->PickableOff();
120   myActor->SetMapper(myMapper);
121   vtkProperty* aProp = vtkProperty::New();
122   float anRGB[3];
123   
124   SUIT_ResourceMgr* aResourceMgr = VISU::GetResourceMgr();
125   
126   QColor aFillColor = aResourceMgr->colorValue("SMESH", "fill_color", QColor(0, 170, 255));
127   anRGB[0] = aFillColor.red()/255.;
128   anRGB[1] = aFillColor.green()/255.;
129   anRGB[2] = aFillColor.blue()/255.;
130   aProp->SetColor(anRGB[0],anRGB[1],anRGB[2]);
131   aProp->SetOpacity(0.75);
132   myActor->SetBackfaceProperty(aProp);
133   aProp->Delete();
134   
135   vtkProperty* aBackProp = vtkProperty::New();
136   QColor aBackFaceColor = aResourceMgr->colorValue("SMESH", "backface_color", QColor(0, 0, 255));
137   anRGB[0] = aBackFaceColor.red()/255.;
138   anRGB[1] = aBackFaceColor.green()/255.;
139   anRGB[2] = aBackFaceColor.blue()/255.;
140   aBackProp->SetColor(anRGB[0],anRGB[1],anRGB[2]);
141   aBackProp->SetOpacity(0.75);
142   myActor->SetProperty(aBackProp);
143   aBackProp->Delete();
144
145   myWindow->getRenderer()->AddActor(myActor);
146 }
147
148 //****************************************************************
149 PreviewPlane::~PreviewPlane()
150 {
151   myWindow->getRenderer()->RemoveActor(myActor);
152   myActor->Delete();
153
154   myMapper->RemoveAllInputs();
155   myMapper->Delete();
156   myCutter->Delete();
157   myBox->Delete();
158 }
159
160
161   
162
163
164 //****************************************************************
165 //****************************************************************
166 //****************************************************************
167 /*CutPlaneFunction* CutPlaneFunction::New()
168 {
169   return new CutPlaneFunction();
170 }
171
172 void CutPlaneFunction::setActive(bool theActive) 
173
174   myIsActive = theActive; 
175   Modified();
176 }
177
178 double CutPlaneFunction::EvaluateFunction(double x[3])
179 {
180   if (myIsActive)
181     return vtkPlane::EvaluateFunction(x);
182   else 
183     return -1;
184 }
185
186 double CutPlaneFunction::EvaluateFunction(double x, double y, double z)
187 {
188   if (myIsActive)
189     return vtkPlane::EvaluateFunction(x,y,z);
190   else 
191     return -1;
192 }
193   
194 CutPlaneFunction::CutPlaneFunction():
195   myIsActive(true)
196 {
197
198 }
199
200 CutPlaneFunction::~CutPlaneFunction()
201 {
202 }
203 */
204
205
206 //****************************************************************
207 //****************************************************************
208 //****************************************************************
209 VisuGUI_ClippingPanel::VisuGUI_ClippingPanel( VisuGUI* theModule, QWidget* theParent )
210   : VisuGUI_Panel( tr( "TITLE" ), theModule, theParent, ApplyBtn | HelpBtn ),
211     myPlaneDlg(0),
212     myViewWindow(0),
213     myIsApplied(true)
214 {
215   setWindowTitle( tr( "TITLE" ) );
216   setObjectName( tr( "TITLE" ) );
217
218   QVBoxLayout* aMainLayout = new QVBoxLayout(mainFrame());
219
220   // List of presentations
221   aMainLayout->addWidget(new QLabel(tr("PRESENTATIONS_LBL"), mainFrame()));
222   myPrsList = new QListWidget(mainFrame());
223   myPrsList->setSelectionMode(QAbstractItemView::SingleSelection);
224   connect(myPrsList, SIGNAL(currentRowChanged(int)), this, SLOT(onPrsSelected(int)));
225   aMainLayout->addWidget(myPrsList);
226
227   // List of planes
228   aMainLayout->addWidget(new QLabel(tr("PLANES_LBL"), mainFrame()));
229   myPlanesList = new QTableWidget(0, 2, mainFrame());
230   myPlanesList->setColumnWidth(1, 50);
231   myPlanesList->setSelectionMode(QAbstractItemView::SingleSelection);
232   connect(myPlanesList, SIGNAL(cellChanged(int,int)), this, SLOT(onCellChanged(int,int)));
233   aMainLayout->addWidget(myPlanesList);
234   
235   // Management buttons
236   QWidget* aButtonsWgt = new QWidget(mainFrame());
237   QHBoxLayout* aBtnLayout = new QHBoxLayout(aButtonsWgt);
238   aMainLayout->addWidget(aButtonsWgt);
239
240   QToolButton* aNewBtn = new QToolButton(aButtonsWgt);
241   aNewBtn->setToolButtonStyle(Qt::ToolButtonTextOnly);
242   aNewBtn->setText(tr("NEW_BTN"));
243   connect(aNewBtn, SIGNAL(clicked(bool)), this, SLOT(onNew()));
244   aBtnLayout->addWidget(aNewBtn);
245
246   QToolButton* aEditBtn = new QToolButton(aButtonsWgt);
247   aEditBtn->setToolButtonStyle(Qt::ToolButtonTextOnly);
248   aEditBtn->setText(tr("EDIT_BTN"));
249   connect(aEditBtn, SIGNAL(clicked(bool)), this, SLOT(onEdit()));
250   aBtnLayout->addWidget(aEditBtn);
251
252   QToolButton* aDeleteBtn = new QToolButton(aButtonsWgt);
253   aDeleteBtn->setToolButtonStyle(Qt::ToolButtonTextOnly);
254   aDeleteBtn->setText(tr("DELETE_BTN"));
255   connect(aDeleteBtn, SIGNAL(clicked(bool)), this, SLOT(onPlaneDelete()));
256   aBtnLayout->addWidget(aDeleteBtn);
257
258   myShowPlanes = new QCheckBox(tr("CHK_SHOW_PLANES"), mainFrame());
259   connect(myShowPlanes, SIGNAL( toggled(bool) ), this, SLOT( setPlanesVisible(bool) ));
260   aMainLayout->addWidget(myShowPlanes);
261
262   myNonActivePlanes = new QCheckBox(tr("CHK_ACTIVATE_PLANES"), mainFrame());
263   connect(myNonActivePlanes, SIGNAL(toggled(bool)), this, SLOT(setPlanesNonActive(bool)));
264   aMainLayout->addWidget(myNonActivePlanes);
265
266   myAutoApply = new QCheckBox(tr("CHK_AUTO_APPLY"), mainFrame());
267   connect(myAutoApply, SIGNAL(toggled(bool)), myApply, SLOT(setDisabled(bool)));
268   myAutoApply->setCheckState(Qt::Checked);
269   aMainLayout->addWidget(myAutoApply);
270
271
272   //  fillPrsList();
273   //  fillPlanesList();
274
275   connect(myModule, SIGNAL(presentationCreated(VISU::Prs3d_i*)), 
276           this, SLOT(onPresentationCreate(VISU::Prs3d_i*)));
277   connect(myModule, SIGNAL(beforeObjectDelete(QString)), 
278           this, SLOT(onObjectDelete(QString)));
279
280   SUIT_Desktop* aDesktop = VISU::GetDesktop(myModule);
281   connect(aDesktop, SIGNAL(windowActivated(SUIT_ViewWindow*)), 
282           this, SLOT(onWindowActivated(SUIT_ViewWindow*)));
283 }
284   
285
286 //*************************************************************************
287 VisuGUI_ClippingPanel::~VisuGUI_ClippingPanel()
288 {
289 }
290
291
292 //*************************************************************************
293 void VisuGUI_ClippingPanel::fillPrsList()
294 {
295   myPrsList->clear();
296   _PTR(Study) aStudy = VISU::GetCStudy(VISU::GetAppStudy(myModule));
297   _PTR(SComponent) aVisuSO = aStudy->FindComponent("VISU");
298   if (!aVisuSO) {
299     return;
300   }
301   myPresentations = getPrsList(aStudy, aVisuSO);
302
303   //Process Cache system folder
304   _PTR(SObject) aSObjPtr = aStudy->FindObject(VISU::ColoredPrs3dCache_i::GetFolderName().c_str());
305   if (aSObjPtr) {
306     _PTR(ChildIterator) Iter = aStudy->NewChildIterator( aSObjPtr );
307     for ( ; Iter->More(); Iter->Next() ) {
308       _PTR(SObject) aChildObj = Iter->Value();
309       myPresentations.append(aChildObj->GetID().c_str());
310     }
311   }
312
313   QStringList aNames;
314   for (int i = 0; i < myPresentations.size(); i++) {
315     if (VISU::Prs3d_i* aPrs = getPrs(myPresentations.at(i)))
316       aNames.append(getPrsName(aPrs));
317   }
318   myPrsList->addItems(aNames);
319 }
320
321 //*************************************************************************
322 QString VisuGUI_ClippingPanel::getPrsName(VISU::Prs3d_i* thePrs)
323 {
324   QString aName;
325   QString aObjName;
326   QStringList aPath;
327   SALOMEDS::SObject_var aSObject = thePrs->GetSObject();
328   if (!aSObject->_is_nil()) {
329     SALOMEDS::SObject_var aFather = aSObject->GetFather();
330     while (!aFather->_is_nil()) {
331       QString aFName = aFather->GetName();
332       aPath.append(aFName);
333       if (aFName == "Post-Pro") break;
334       aFather = aFather->GetFather();
335     }
336     aObjName = aSObject->GetName();
337   } else {
338     VISU::ColoredPrs3d_i* aColPrs = dynamic_cast<VISU::ColoredPrs3d_i*>(thePrs);
339     if (aColPrs) {
340       _PTR(Study) aStudy = VISU::GetCStudy(VISU::GetAppStudy(myModule));
341       _PTR(SObject) aSObjPtr = aStudy->FindObjectID(aColPrs->GetHolderEntry().c_str());
342       if (aSObjPtr) {
343         _PTR(SObject) aFather = aSObjPtr->GetFather();
344         while (aFather) {
345           QString aFName(aFather->GetName().c_str());
346           aPath.append(aFName);
347           if (aFName == "Post-Pro")     break;
348           aFather = aFather->GetFather();
349         }
350       }
351       aObjName = aSObjPtr->GetName().c_str();
352     }
353   }
354   for (int j = aPath.size() - 2; j >= 0; j--)
355     aName += aPath.at(j) + "/";
356   aName += aObjName;
357   return aName;
358 }
359
360 //*************************************************************************
361 QStringList VisuGUI_ClippingPanel::getPrsList(_PTR(Study) theStudy, 
362                                               _PTR(SObject) theObject)
363 {
364   //QList<VISU::Prs3d_i*> aList;
365   QStringList aList;
366   _PTR(ChildIterator) aIter = theStudy->NewChildIterator(theObject);
367   for (aIter->InitEx(true); aIter->More(); aIter->Next()) {
368     _PTR(SObject) aSObject = aIter->Value();
369     std::vector<VISU::Prs3d_i*> aSTLList = VISU::GetPrs3dList(myModule, aSObject, true /* enable GaussPoints */);
370     for (int i = 0; i < aSTLList.size(); i++) {
371       VISU::Prs3d_i* aPrs = aSTLList[i];
372       if (!aList.contains(aPrs->GetEntry().c_str()))
373         aList.append(aPrs->GetEntry().c_str());
374     }
375     if (aList.size() == 0) {
376       //QList<VISU::Prs3d_i*> aSubList = getPrsList(theStudy, aSObject);
377       QStringList aSubList = getPrsList(theStudy, aSObject);
378        for (int i = 0; i < aSubList.size(); i++) {
379          //VISU::Prs3d_i* aPrs = aSubList[i];
380         QString aPrsEntry = aSubList[i];
381         if (!aList.contains(aPrsEntry))
382           aList.append(aPrsEntry);
383       }
384     }
385   }
386   return aList;
387 }
388
389 //*************************************************************************
390 VISU_ClippingPlaneMgr& VisuGUI_ClippingPanel::getPlanesMgr() const
391 {
392   return VISU::GetVisuGen(myModule)->GetClippingPlaneMgr();
393 }
394
395 //*************************************************************************
396 void VisuGUI_ClippingPanel::fillPlanesList()
397 {
398   disconnect(myPlanesList, SIGNAL(cellChanged(int,int)), this, SLOT(onCellChanged(int,int)));
399   myPlanesList->clear();
400   _PTR(Study) aStudy = VISU::GetCStudy(VISU::GetAppStudy(myModule));
401   VISU_ClippingPlaneMgr& aMgr = getPlanesMgr();
402   for (int i = 0; i < aMgr.GetClippingPlanesNb(); i++) {
403     VISU_CutPlaneFunction* aPlane = aMgr.GetClippingPlane(i);
404     myPlanesList->insertRow(i);
405     QTableWidgetItem* aItem = new QTableWidgetItem(aPlane->getName().c_str());
406     QTableWidgetItem* aCheckItem = new QTableWidgetItem();
407     aCheckItem->setCheckState((myNonActivePlanes->checkState() == Qt::Checked)? 
408                               Qt::Unchecked : Qt::Checked);    
409     if (aPlane->isAuto()) { // If this is Auto plane
410       aItem->setFlags(0);
411       aItem->setCheckState(Qt::Checked);
412     } else { // if it is not auto
413       aItem->setCheckState(Qt::Unchecked);
414       // Check current presentation
415       int aPrsNum = myPrsList->currentRow();
416       if (aPrsNum >= 0) {
417         if (VISU_ClippingPlaneMgr::ContainsPlane(getPrs(myPresentations.at(aPrsNum)), aPlane))
418           aItem->setCheckState(Qt::Checked);
419       }
420     }
421     myPlanesList->setItem(i, 0, aItem);
422     myPlanesList->setItem(i, 1, aCheckItem);
423   }
424   myPlanesList->setHorizontalHeaderLabels(QString(tr("PLANES_TABLE_TITLES")).split(","));
425   if (myViewWindow)
426     myViewWindow->Repaint();
427   //myPlanes.clear();
428   /*_PTR(Study) aStudy = VISU::GetCStudy(VISU::GetAppStudy(myModule));
429   _PTR(SObject) aFolder;
430   if (VISU::getClippingPlanesFolder(aStudy, aFolder)) {
431     _PTR(ChildIterator) aIter = aStudy->NewChildIterator(aFolder);
432     int i;
433     for (i = 0; aIter->More(); aIter->Next(), i++) { // For each plane
434       _PTR(SObject) aSObject = aIter->Value(); 
435       PlaneDef aNewPlane = createEmptyPlane();
436       updatePlane(aSObject, aNewPlane);
437
438       myPlanesList->insertRow(i);
439       QTableWidgetItem* aItem = new QTableWidgetItem(aNewPlane.name);
440       QTableWidgetItem* aCheckItem = new QTableWidgetItem();
441       aCheckItem->setCheckState((myNonActivePlanes->checkState() == Qt::Checked)? Qt::Unchecked : Qt::Checked);
442
443       if (aNewPlane.isAuto) { // If this is Auto plane
444         aItem->setFlags(0);
445         // apply to all presentations
446         aItem->setCheckState(Qt::Checked);
447         foreach(QString aPrsEntry, myPresentations) {
448           getPrs(aPrsEntry)->AddClippingPlane(aNewPlane.plane);
449         }
450       } else { // if it is not auto
451         foreach(QString aPrsEntry, myPresentations) {
452           _PTR(ChildIterator) aRefIter = aStudy->NewChildIterator(aSObject);      
453           for (; aRefIter->More(); aRefIter->Next()) {
454             _PTR(SObject) aObj = aRefIter->Value();
455             _PTR(SObject) aRefPrsObject;
456             if (aObj->ReferencedObject(aRefPrsObject)) { // If it is referenced on current plane
457               if (QString(aRefPrsObject->GetID().c_str()) == aPrsEntry) {
458                 getPrs(aPrsEntry)->AddClippingPlane(aNewPlane.plane);
459               }
460             }
461           }
462         }
463         aItem->setCheckState(Qt::Unchecked);
464         // Check current presentation
465         int aPrsNum = myPrsList->currentRow();
466         if (aPrsNum >= 0) {
467           if (containsPlane(getPrs(myPresentations.at(aPrsNum)), aNewPlane))
468             aItem->setCheckState(Qt::Checked);
469         }
470       }
471       myPlanesList->setItem(i, 0, aItem);
472       myPlanesList->setItem(i, 1, aCheckItem);
473       myPlanes.append(aNewPlane);
474     }
475   }
476   myPlanesList->setHorizontalHeaderLabels(QString(tr("PLANES_TABLE_TITLES")).split(","));
477   if (myViewWindow)
478   myViewWindow->Repaint();*/
479   connect(myPlanesList, SIGNAL(cellChanged(int,int)), this, SLOT(onCellChanged(int,int)));
480 }
481
482
483 //*************************************************************************
484 void VisuGUI_ClippingPanel::init()
485 {
486   myViewWindow = VISU::GetViewWindow<SVTK_Viewer>(myModule);
487   if (myViewWindow) 
488     connect(myViewWindow, SIGNAL(destroyed(QObject*)), this, SLOT(onWindowDestroyed(QObject*)));
489   fillPrsList();
490   fillPlanesList();
491 }
492
493 void VisuGUI_ClippingPanel::onWindowDestroyed(QObject* theWnd)
494 {
495   if (theWnd == myViewWindow) {
496     myViewWindow = 0;
497   }
498 }
499
500 //*************************************************************************
501 //void VisuGUI_ClippingPanel::showEvent(QShowEvent* event)
502 //{
503 //  VisuGUI_Panel::showEvent(event);
504 //}
505
506 //*************************************************************************
507 // void VisuGUI_ClippingPanel::hideEvent(QHideEvent* event)
508 // {
509 //   disconnect(myModule, SIGNAL(presentationCreated()), this, SLOT(onPresentationCreate()));
510 //   VisuGUI_Panel::hideEvent(event);
511 // }
512
513 //*************************************************************************
514 void VisuGUI_ClippingPanel::onPresentationCreate(VISU::Prs3d_i* thePrs)
515 {
516   myPrsList->addItem(getPrsName(thePrs));
517   string aEntry = thePrs->GetEntry();
518   if (aEntry.length() == 0) {
519     VISU::ColoredPrs3d_i* aColPrs = dynamic_cast<VISU::ColoredPrs3d_i*>(thePrs);
520     if (aColPrs)
521       aEntry = aColPrs->GetHolderEntry();
522   }
523
524   myPresentations.append(aEntry.c_str());
525   VISU_ClippingPlaneMgr& aMgr = getPlanesMgr();
526   for (int i = 0; i < aMgr.GetClippingPlanesNb(); i++) {
527     VISU_CutPlaneFunction* aPlane = aMgr.GetClippingPlane(i);
528     if (aPlane->isAuto())
529       aMgr.ApplyClippingPlane(thePrs, i);
530   }
531   /*  for (int i = 0; i < myPlanes.size(); i++) {
532     const PlaneDef& aPlane = myPlanes.at(i);
533     if (aPlane.isAuto)
534       thePrs->AddClippingPlane(aPlane.plane);
535       }*/
536 }
537
538 //*************************************************************************
539 /*PlaneDef VisuGUI_ClippingPanel::createEmptyPlane()
540 {
541   PlaneDef aPlane;
542   aPlane.name = QString("Plane");
543   aPlane.plane = CutPlaneFunction::New();
544   aPlane.plane->Delete();
545   aPlane.plane->SetOrigin(0.,0.,0.);
546   aPlane.plane->SetNormal(0.,0.,1.);
547   aPlane.isAuto = true;
548   return aPlane;
549 }*/
550
551 //*************************************************************************
552 /*void VisuGUI_ClippingPanel::updatePlane(_PTR(SObject) theObject, PlaneDef& thePlane)
553 {
554   thePlane.name = QString(theObject->GetName().c_str());
555   thePlane.plane->setPlaneObject(theObject);
556
557   _PTR(GenericAttribute) anAttr;
558   if (theObject->FindAttribute(anAttr, "AttributeSequenceOfReal")) {
559     _PTR(AttributeSequenceOfReal) aArray(anAttr);
560     thePlane.plane->SetOrigin(aArray->Value(1), aArray->Value(2), aArray->Value(3));
561     thePlane.plane->SetNormal(aArray->Value(4), aArray->Value(5), aArray->Value(6));
562   }
563   if (theObject->FindAttribute(anAttr, "AttributeInteger")) {
564     _PTR(AttributeInteger) aFlag(anAttr);
565     thePlane.isAuto = (aFlag->Value() == 1);
566   }
567 }*/
568   
569 //*************************************************************************
570 void VisuGUI_ClippingPanel::onNewPlane()
571 {
572   disconnect(myPlaneDlg, SIGNAL(accepted()), this, SLOT(onNewPlane()));
573   disconnect(myPlaneDlg, SIGNAL(rejected()), this, SLOT(onCancelDialog()));
574   disconnect(myPlanesList, SIGNAL(cellChanged(int,int)), this, SLOT(onCellChanged(int,int)));
575   //_PTR(SObject) aPlaneSObj = myPlaneDlg->getPlaneObj();
576   int aId = myPlaneDlg->planeId();
577   VISU_ClippingPlaneMgr& aMgr = getPlanesMgr();
578
579   VISU_CutPlaneFunction* aPlane = aMgr.GetClippingPlane(aId);
580   //PlaneDef aNewPlane = createEmptyPlane();
581   //updatePlane(aPlaneSObj, aNewPlane);
582   int aRow = myPlanesList->rowCount();
583   myPlanesList->insertRow(aRow);
584
585   //QTableWidgetItem* aItem = new QTableWidgetItem(aNewPlane.name);
586   QTableWidgetItem* aItem = new QTableWidgetItem(aPlane->getName().c_str());
587   QTableWidgetItem* aCheckItem = new QTableWidgetItem();
588   aCheckItem->setCheckState((myNonActivePlanes->checkState() == Qt::Checked)? 
589                             Qt::Unchecked : Qt::Checked);
590   //aNewPlane.plane->setActive(myNonActivePlanes->checkState() != Qt::Checked);
591   aPlane->setActive(myNonActivePlanes->checkState() != Qt::Checked);
592   //if (aNewPlane.isAuto) {
593   if (aPlane->isAuto()) {
594     aItem->setFlags(0);
595     aItem->setCheckState(Qt::Checked);
596 //     for (int i = 0; i < myPresentations.size(); i++) {
597 //       getPrs(myPresentations.at(i))->AddClippingPlane(aNewPlane.plane);
598 //     }
599   } else {
600     aItem->setCheckState(Qt::Unchecked);
601   }
602   myPlanesList->setItem(aRow, 0, aItem);
603   myPlanesList->setItem(aRow, 1, aCheckItem);
604   //myPlanes.append(aNewPlane);
605
606   delete myPlaneDlg;
607   myPlaneDlg = 0;
608
609   setPlanesVisible(myShowPlanes->checkState() == Qt::Checked);
610   
611   if (myViewWindow)
612     myViewWindow->Repaint();
613   connect(myPlanesList, SIGNAL(cellChanged(int,int)), this, SLOT(onCellChanged(int,int)));
614 }
615
616 //*************************************************************************
617 void VisuGUI_ClippingPanel::onPlaneEdited() 
618 {
619   disconnect(myPlaneDlg, SIGNAL(accepted()), this, SLOT(onNewPlane()));
620   disconnect(myPlaneDlg, SIGNAL(rejected()), this, SLOT(onCancelDialog()));
621   disconnect(myPlanesList, SIGNAL(cellChanged(int,int)), this, SLOT(onCellChanged(int,int)));
622   //_PTR(SObject) aPlaneSObj = myPlaneDlg->getPlaneObj();
623
624   int aId = myPlaneDlg->planeId();
625   VISU_ClippingPlaneMgr& aMgr = getPlanesMgr();
626   VISU_CutPlaneFunction* aPlane = aMgr.GetClippingPlane(aId);
627   //  PlaneDef aPlane = myPlanes.at(myEditingPlanePos);
628   //updatePlane(aPlaneSObj, aPlane);
629
630   //QTableWidgetItem* aItem = myPlanesList->item(myEditingPlanePos, 0);
631   QTableWidgetItem* aItem = myPlanesList->item(aId, 0);
632   //  if (aPlane.isAuto) {
633   if (aPlane->isAuto()) {
634     aItem->setCheckState(Qt::Checked);    
635     aItem->setFlags(0);
636     QString name = aPlane->getName().c_str();
637     aItem->setText(name);
638 //     _PTR(Study) aStudy = VISU::GetCStudy( VISU::GetAppStudy( myModule ) );
639 //     _PTR(ChildIterator) aIter = aStudy->NewChildIterator(aPlaneSObj);
640 //     for (; aIter->More(); aIter->Next()) {
641 //       _PTR(SObject) aObj = aIter->Value();
642 //       VISU::DeleteSObject(myModule, aStudy, aObj);
643 //     }
644      VISU::UpdateObjBrowser(myModule);
645   } else {
646     aItem->setCheckState(Qt::Unchecked);
647     aItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
648     int aPrsNum = myPrsList->currentRow();
649     if (aPrsNum >= 0) {
650       if (VISU_ClippingPlaneMgr::ContainsPlane(getPrs(myPresentations.at(aPrsNum)), aPlane))
651         aItem->setCheckState(Qt::Checked);
652     }
653   }
654
655   delete myPlaneDlg;
656   myPlaneDlg = 0;
657
658   //myPlanes.replace(myEditingPlanePos, aPlane);
659   setPlanesVisible(myShowPlanes->checkState() == Qt::Checked);
660
661   if (myViewWindow)
662     myViewWindow->Repaint();
663   connect(myPlanesList, SIGNAL(cellChanged(int,int)), this, SLOT(onCellChanged(int,int)));
664 }
665
666 //*************************************************************************
667 void VisuGUI_ClippingPanel::onEdit()
668 {
669   if (!isVISUDataReady()) return;
670   if (myPlaneDlg) return;
671
672   int aRow = myPlanesList->currentRow();
673   if (aRow < 0) return;
674
675   //  const PlaneDef& aPlane = myPlanes.at(aRow);
676
677   myPlaneDlg = new VisuGUI_ClippingPlaneDlg(myModule);
678   //myPlaneDlg->setPlaneObj(aPlane.plane->getPlaneObject());
679   myPlaneDlg->setPlaneId(aRow);
680   connect(myPlaneDlg, SIGNAL(accepted()), this, SLOT(onPlaneEdited()));
681   connect(myPlaneDlg, SIGNAL(rejected()), this, SLOT(onCancelDialog()));
682   //myEditingPlanePos = aRow;
683   myPlaneDlg->show();
684 }
685
686 //*************************************************************************
687 void VisuGUI_ClippingPanel::onNew()
688 {
689   if (!isVISUDataReady()) return;
690
691   if (myPlaneDlg) return;
692
693   myPlaneDlg = new VisuGUI_ClippingPlaneDlg(myModule);
694   connect(myPlaneDlg, SIGNAL(accepted()), this, SLOT(onNewPlane()));
695   connect(myPlaneDlg, SIGNAL(rejected()), this, SLOT(onCancelDialog()));
696   myPlaneDlg->show();
697 }
698
699 //*************************************************************************
700 void VisuGUI_ClippingPanel::onPlaneDelete()
701 {
702   if (!isVISUDataReady()) return;
703   if (myPlaneDlg) return;
704
705   _PTR(Study) aStudy = VISU::GetCStudy( VISU::GetAppStudy( myModule ) );
706   
707   
708   int aRow = myPlanesList->currentRow();
709   if (aRow < 0) return;
710
711   //  const PlaneDef& aPlane = myPlanes.at(aRow);
712   VISU_ClippingPlaneMgr& aMgr = getPlanesMgr();
713   VISU_CutPlaneFunction* aPlane = aMgr.GetClippingPlane(aRow);
714   QMessageBox::StandardButton aRes = 
715     QMessageBox::warning(VISU::GetDesktop(myModule), 
716                          tr("TIT_DELETE_PLANE"), 
717                          tr("MSG_DELETE_PLANE").arg(aPlane->getName().c_str()), 
718                          QMessageBox::Yes | QMessageBox::No, 
719                          QMessageBox::No);
720   if (aRes == QMessageBox::Yes) {
721     aMgr.DeleteClippingPlane(aRow);
722     /*    short aTag1 = aPlane.plane->getPlaneObject()->Tag();
723     for (int i = 0; i < myPresentations.size(); i++) {
724       VISU::Prs3d_i* aPrs = getPrs(myPresentations.at(i));
725       for (int j = aPrs->GetNumberOfClippingPlanes()-1; j > -1; j--) {
726         CutPlaneFunction* aPln = dynamic_cast<CutPlaneFunction*>(aPrs->GetClippingPlane(j));
727         if (aPln) {
728           short aTag2 = aPln->getPlaneObject()->Tag();
729           if (aTag1 == aTag2) {
730             aPrs->RemoveClippingPlane(j);
731           }
732         }
733       }
734     }
735
736     _PTR(SObject) aSObj = aPlane.plane->getPlaneObject();
737     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
738     aBuilder->RemoveObject(aSObj);
739     */
740     myPlanesList->removeRow(aRow);
741     //myPlanes.removeAt(aRow);
742     
743     VISU::UpdateObjBrowser(myModule);
744     
745     if (myViewWindow)
746       myViewWindow->Repaint();
747   }
748 }
749
750 //*************************************************************************
751 bool VisuGUI_ClippingPanel::isVISUDataReady() 
752 {
753   _PTR(Study) aStudy = VISU::GetCStudy( VISU::GetAppStudy( myModule ) );
754
755   _PTR(SComponent) aVisuSO = aStudy->FindComponent("VISU");
756   if (!aVisuSO) return false;
757
758   if(aStudy->GetProperties()->IsLocked()) return false;
759   return true;
760 }
761
762 //*************************************************************************
763 void VisuGUI_ClippingPanel::onApply()
764 {
765   if (myIsApplied) return;
766   
767   //PlaneDef aPlane;
768   int i;
769   VISU_ClippingPlaneMgr& aMgr = getPlanesMgr();
770   for (i = 0; i < aMgr.GetClippingPlanesNb(); i++) {
771     aMgr.GetClippingPlane(i)->setActive((myPlanesList->item(i, 1)->checkState() == Qt::Checked));
772   }
773 //   for(i = 0; i < myPlanes.size(); i++) {
774 //     aPlane = myPlanes.at(i);
775 //     aPlane.plane->setActive((myPlanesList->item(i, 1)->checkState() == Qt::Checked));
776 //   }
777   myIsApplied = true;
778   
779   int aPrsNum = myPrsList->currentRow();
780   if (aPrsNum > -1) {
781     //    for(i = 0; i < myPlanes.size(); i++) {
782     for(i = 0; i < aMgr.GetClippingPlanesNb(); i++) {
783       applyPlaneToPrs(i, aPrsNum, (myPlanesList->item(i, 0)->checkState() == Qt::Checked));
784     }
785   //myCheckedPlanes.clear();
786     VISU::UpdateObjBrowser(myModule);
787   }
788   if (myViewWindow)
789     myViewWindow->Repaint();
790 }
791
792 //*************************************************************************
793 void VisuGUI_ClippingPanel::setPlanesNonActive(bool theState)
794 {
795   //PlaneDef aPlane;
796   disconnect(myPlanesList, SIGNAL(cellChanged(int,int)), this, SLOT(onCellChanged(int,int)));
797   //for (int i = 0; i < myPlanes.size(); i++) {
798   VISU_ClippingPlaneMgr& aMgr = getPlanesMgr();
799   VISU_CutPlaneFunction* aPlane = 0;
800   for (int i = 0; i < aMgr.GetClippingPlanesNb(); i++) {
801     //aPlane = myPlanes.at(i);
802     aPlane = aMgr.GetClippingPlane(i);
803     //aPlane.plane->setActive(!theState);
804     aPlane->setActive(!theState);
805     myPlanesList->item(i, 1)->setCheckState((theState)? Qt::Unchecked : Qt::Checked);
806   }
807   if (myViewWindow)
808     myViewWindow->Repaint();
809   connect(myPlanesList, SIGNAL(cellChanged(int,int)), this, SLOT(onCellChanged(int,int)));
810 }
811
812 //*************************************************************************
813 void VisuGUI_ClippingPanel::setPlanesVisible(bool theVisible)
814 {
815   if (!myViewWindow) return;
816
817   if (theVisible) {
818     // Hide previous
819     setPlanesVisible(false);
820
821     double aBounds[6];
822     ComputeVisiblePropBounds(myViewWindow->getRenderer(), aBounds);
823     VISU_ClippingPlaneMgr& aMgr = getPlanesMgr();
824     //for (int i = 0; i < myPlanes.size(); i++) {
825     for (int i = 0; i < aMgr.GetClippingPlanesNb(); i++) {
826       //PreviewPlane* aPreview = new PreviewPlane(myViewWindow, myPlanes.at(i), aBounds);
827       PreviewPlane* aPreview = new PreviewPlane(myViewWindow, aMgr.GetClippingPlane(i), aBounds);
828       aPreview->setVisible(true);
829       myPreview.append(aPreview);
830     }
831   } else {
832     PreviewPlane* aPreview;
833     while (myPreview.size() > 0) {
834       aPreview = myPreview.last();
835       myPreview.removeLast();
836       delete aPreview;
837     }
838   }
839   myViewWindow->Repaint();
840 }
841
842 //*************************************************************************
843 void VisuGUI_ClippingPanel::onCancelDialog()
844 {
845   disconnect(myPlaneDlg, SIGNAL(accepted()), this, SLOT(onNewPlane()));
846   disconnect(myPlaneDlg, SIGNAL(rejected()), this, SLOT(onCancelDialog()));
847
848   delete myPlaneDlg;
849   myPlaneDlg = 0;
850 }
851
852
853 //*************************************************************************
854 void VisuGUI_ClippingPanel::onWindowActivated(SUIT_ViewWindow* theWindow)
855 {
856   setPlanesVisible(false);
857   myViewWindow = dynamic_cast<SVTK_ViewWindow*>(theWindow);
858   if (myViewWindow) 
859     connect(myViewWindow, SIGNAL(destroyed(QObject*)), this, SLOT(onWindowDestroyed(QObject*)));
860   setPlanesVisible(myShowPlanes->checkState() == Qt::Checked);
861 }
862
863 //*************************************************************************
864 void VisuGUI_ClippingPanel::onCellChanged(int row, int col)
865 {
866   //PlaneDef aPlane = myPlanes.at(row);
867   VISU_ClippingPlaneMgr& aMgr = getPlanesMgr();
868   VISU_CutPlaneFunction* aPlane = aMgr.GetClippingPlane(row);
869   bool isChecked = (myPlanesList->item(row, col)->checkState() == Qt::Checked);
870   if (col == 1) {  // activate column clicked
871     if (isAutoApply()) 
872       //aPlane.plane->setActive(isChecked);
873       aPlane->setActive(isChecked);
874     else {
875       myIsApplied = false;
876       return;
877     }
878   } else { // Plane checked
879     
880     //if (aPlane.isAuto) return;
881     if (aPlane->isAuto()) return;
882
883     if (!isAutoApply()) {
884       //myCheckedPlanes.append(row);
885       myIsApplied = false;
886       return;
887     }
888     int aPrsNum = myPrsList->currentRow();
889     if (aPrsNum < 0) return;
890
891     applyPlaneToPrs(row, aPrsNum, isChecked);
892     VISU::UpdateObjBrowser(myModule);
893   }
894   if (myViewWindow)
895     myViewWindow->Repaint();
896 }
897
898 //*************************************************************************
899 void VisuGUI_ClippingPanel::applyPlaneToPrs(int thePlaneNum, int thePrsNum, bool isChecked)
900 {
901   VISU::Prs3d_i* aPrs = getPrs(myPresentations.at(thePrsNum));
902   VISU_ClippingPlaneMgr& aMgr = getPlanesMgr();
903   if (isChecked) { // Apply
904     aMgr.ApplyClippingPlane(aPrs, thePlaneNum);
905   } else { // Detach
906     aMgr.DetachClippingPlane(aPrs, thePlaneNum);
907   }
908   /*  PlaneDef aPlane = myPlanes.at(thePlaneNum);
909   _PTR(Study) aStudy = VISU::GetCStudy( VISU::GetAppStudy( myModule ) );
910   _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
911   VISU::Prs3d_i* aPrs = getPrs(myPresentations.at(thePrsNum));
912   _PTR(SObject) aSObject = aPlane.plane->getPlaneObject();
913   _PTR(SObject) aPrsSObj = aStudy->FindObjectID(aPrs->GetEntry());
914   if (isChecked) {
915     if (!containsPlane(aPrs, aPlane)) {
916       aPrs->AddClippingPlane(aPlane.plane);
917     
918       if(!aStudy->GetProperties()->IsLocked()) {
919         _PTR(SObject) aNewObj = aBuilder->NewObject(aSObject);
920         aBuilder->Addreference(aNewObj, aPrsSObj);
921       }
922     }
923   } else {
924     for (int i = 0; i < aPrs->GetNumberOfClippingPlanes(); i++) {
925       if (aPrs->GetClippingPlane(i) == aPlane.plane.GetPointer()) {
926         aPrs->RemoveClippingPlane(i);
927         break;
928       }
929     }
930     if(!aStudy->GetProperties()->IsLocked()) {
931       _PTR(ChildIterator) aIter = aStudy->NewChildIterator(aSObject);
932       for (; aIter->More(); aIter->Next()) {
933         _PTR(SObject) aRefObj = aIter->Value();
934         if(aRefObj) {
935           _PTR(SObject) aRefPrsObject;
936           if (aRefObj->ReferencedObject(aRefPrsObject)) {
937             if (QString(aRefPrsObject->GetID().c_str()) == QString(aPrs->GetEntry().c_str())) {
938               VISU::DeleteSObject(myModule, aStudy, aRefObj);
939               break;
940             }
941           }
942         }
943       }
944     }
945     }*/
946 }
947
948 //*************************************************************************
949 /*bool VisuGUI_ClippingPanel::containsPlane(VISU::Prs3d_i* thePrs, const PlaneDef& thePlane)
950 {
951   //bool isContains = false;
952   for (int i = 0; i < thePrs->GetNumberOfClippingPlanes(); i++) {
953     if (thePrs->GetClippingPlane(i) == thePlane.plane.GetPointer()) {
954       return true;
955     }
956   }
957   return false;
958 }*/
959
960
961 //*************************************************************************
962 void VisuGUI_ClippingPanel::onObjectDelete(QString theEntry)
963 {
964   disconnect(myPrsList, SIGNAL(currentRowChanged(int)), this, SLOT(onPrsSelected(int)));
965   int i = 0;
966   foreach (QString aPrsEntry, myPresentations) {
967     VISU::Prs3d_i* aPrs = getPrs(aPrsEntry);
968     if (aPrs) {
969       QString aID(aPrs->GetEntry().c_str());
970       if (aID == theEntry) {
971         myPresentations.removeAt(i);
972         myPrsList->takeItem(i);
973         break;
974       }
975       i++;
976     }
977   }
978   connect(myPrsList, SIGNAL(currentRowChanged(int)), this, SLOT(onPrsSelected(int)));
979   onPrsSelected(myPrsList->currentRow());
980 }
981
982 //*************************************************************************
983 void VisuGUI_ClippingPanel::onPrsSelected(int thePrs)
984 {
985   if (thePrs < 0) return;
986   if (thePrs > myPresentations.size()) return;
987   disconnect(myPlanesList, SIGNAL(cellChanged(int,int)), this, SLOT(onCellChanged(int,int)));
988   VISU::Prs3d_i* aPrs = getPrs(myPresentations.at(thePrs));
989   if (!aPrs) return;
990   //QListOfPlanes::const_iterator aIt;
991   VISU_ClippingPlaneMgr& aMgr = getPlanesMgr();
992   for (int i = 0; i < aMgr.GetClippingPlanesNb(); i++) {
993     QTableWidgetItem* aItem = myPlanesList->item(i, 0);
994     VISU_CutPlaneFunction* aPlane = aMgr.GetClippingPlane(i);
995     if (!aPlane->isAuto())
996       aItem->setCheckState(VISU_ClippingPlaneMgr::ContainsPlane(aPrs, aPlane)? 
997                            Qt::Checked : Qt::Unchecked);
998   }
999   /*  for (aIt = myPlanes.begin(), i = 0; aIt != myPlanes.end(); ++aIt, i++) {
1000     QTableWidgetItem* aItem = myPlanesList->item(i, 0);
1001     const PlaneDef& aPlane = *aIt;
1002     if (!aPlane.isAuto)
1003       aItem->setCheckState(containsPlane(aPrs, aPlane)? Qt::Checked : Qt::Unchecked);    
1004       }*/
1005   connect(myPlanesList, SIGNAL(cellChanged(int,int)), this, SLOT(onCellChanged(int,int)));
1006 }
1007
1008 //*************************************************************************
1009 bool VisuGUI_ClippingPanel::isAutoApply() const
1010 {
1011   return myAutoApply->checkState() == Qt::Checked;
1012 }
1013
1014
1015 //*************************************************************************
1016 VISU::Prs3d_i* VisuGUI_ClippingPanel::getPrs(QString theEntry)
1017 {
1018   VISU::TObjectInfo anObjectInfo = VISU::GetObjectByEntry(VISU::GetAppStudy(myModule), 
1019                                                           qPrintable(theEntry));
1020   return VISU::GetPrs3dFromBase(anObjectInfo.myBase);
1021 }
1022
1023
1024 //*************************************************************************
1025 void VisuGUI_ClippingPanel::onHelp()
1026 {
1027   QString aHelpFileName = "clipping_page.html";
1028   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
1029   if (app)
1030     app->onHelpContextModule(myModule ? app->moduleName(myModule->moduleName()) : QString(""), aHelpFileName);
1031   else {
1032     QString platform;
1033 #ifdef WIN32
1034     platform = "winapplication";
1035 #else
1036     platform = "application";
1037 #endif
1038     SUIT_MessageBox::warning(0, QObject::tr("WRN_WARNING"),
1039                              QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
1040                              arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(aHelpFileName),
1041                              QObject::tr("BUT_OK"));
1042   }
1043 }