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