]> SALOME platform Git repositories - modules/visu.git/blob - src/VISUGUI/VisuGUI_ClippingPlaneDlg.cxx
Salome HOME
IPAL20954 problem loading help pages
[modules/visu.git] / src / VISUGUI / VisuGUI_ClippingPlaneDlg.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_ClippingPlaneDlg.h"
23 #include "VisuGUI.h"
24 #include "VisuGUI_Tools.h"
25 #include "VisuGUI_ViewTools.h"
26
27 #include <VISU_Gen_i.hh>
28
29 #include <LightApp_Application.h>
30 #include <SUIT_Desktop.h>
31 #include <SUIT_Session.h>
32 #include <SUIT_ViewManager.h>
33 #include <SUIT_MessageBox.h>
34 #include <SUIT_ResourceMgr.h>
35 #include <QtxDoubleSpinBox.h>
36 #include <SVTK_ViewWindow.h>
37 #include <VTKViewer_Utilities.h>
38
39 #include <QVBoxLayout>
40 #include <QHBoxLayout>
41 #include <QLineEdit>
42 #include <QWidget>
43 #include <QGroupBox>
44 #include <QGridLayout>
45 #include <QCheckBox>
46 #include <QPushButton>
47
48 #include <vtkCallbackCommand.h>
49 #include <vtkImplicitPlaneWidget.h>
50
51
52 #define SIZEFACTOR 1.1
53
54
55
56
57 //****************************************************************
58 //****************************************************************
59 //****************************************************************
60 VisuGUI_ClippingPlaneDlg::VisuGUI_ClippingPlaneDlg(VisuGUI* theModule)
61   : QDialog(VISU::GetDesktop(theModule), Qt::WindowTitleHint | Qt::WindowSystemMenuHint ),
62     myModule(theModule),
63     myCallback( vtkCallbackCommand::New() ),
64     myPreviewWidget(0),
65     myViewWindow(0),
66     myPlaneId(-1)
67 {
68   myViewWindow = VISU::GetActiveViewWindow<SVTK_ViewWindow>(myModule);
69   VISU::ComputeVisiblePropBounds(myViewWindow, myBounds);
70
71   setWindowTitle(tr("TITLE"));
72   setSizeGripEnabled(true);
73   setModal(false);
74
75   myCallback->SetClientData(this); 
76   myCallback->SetCallback(VisuGUI_ClippingPlaneDlg::ProcessEvents);
77
78   QVBoxLayout* aMainLayout = new QVBoxLayout(this);
79
80   QWidget* aPlanesWgt = new QWidget(this);
81   aMainLayout->addWidget(aPlanesWgt);
82   QVBoxLayout* aFrameLayout = new QVBoxLayout(aPlanesWgt);
83
84   QWidget* aNameBox = new QWidget(aPlanesWgt);
85   aFrameLayout->addWidget(aNameBox);
86   QHBoxLayout* aNameLayout = new QHBoxLayout(aNameBox);
87
88   aNameLayout->addWidget(new QLabel(tr("LBL_NAME"), aPlanesWgt));
89   myNameEdt = new QLineEdit();
90   
91   _PTR(Study) aStudy = VISU::GetCStudy(VISU::GetAppStudy(myModule));
92   _PTR(SObject) aFolderSO;
93   if (VISU::getClippingPlanesFolder(aStudy, aFolderSO)) {
94     _PTR(ChildIterator) aIter = aStudy->NewChildIterator(aFolderSO);
95     int i = 1;
96     for (; aIter->More(); aIter->Next()) i++;
97
98     myNameEdt->setText(QString("Plane %1").arg(i));
99   }
100   aNameLayout->addWidget(myNameEdt);
101
102   QGroupBox* aOriginGroup = new QGroupBox( tr( "ORIGIN_TITLE" ), aPlanesWgt );
103   aFrameLayout->addWidget(aOriginGroup);
104   QHBoxLayout* aOriginLayout = new QHBoxLayout(aOriginGroup);
105
106   aOriginLayout->addWidget( new QLabel("X", aOriginGroup) );
107   myXOrigin = new QtxDoubleSpinBox( -1000.0, 1000.0, 0.1, aOriginGroup );
108   myXOrigin->setValue( 0.0 );
109   connect(myXOrigin, SIGNAL(valueChanged(double)), this, SLOT(onValueChanged()));
110   aOriginLayout->addWidget( myXOrigin );
111
112   aOriginLayout->addWidget( new QLabel("Y", aOriginGroup) );
113   myYOrigin = new QtxDoubleSpinBox( -1000.0, 1000, 0.1, aOriginGroup );
114   myYOrigin->setValue( 0.0 );
115   connect(myYOrigin, SIGNAL(valueChanged(double)), this, SLOT(onValueChanged()));
116   aOriginLayout->addWidget( myYOrigin );
117
118   aOriginLayout->addWidget( new QLabel("Z", aOriginGroup) );
119   myZOrigin = new QtxDoubleSpinBox( -1000.0, 1000.0, 0.1, aOriginGroup );
120   myZOrigin->setValue( 0.0 );
121   connect(myZOrigin, SIGNAL(valueChanged(double)), this, SLOT(onValueChanged()));
122   aOriginLayout->addWidget( myZOrigin );
123   
124   QGroupBox* aDirGroup = new QGroupBox( tr( "DIRECTION_TITLE" ), aPlanesWgt );
125   aFrameLayout->addWidget(aDirGroup);
126   QHBoxLayout* aDirLayout = new QHBoxLayout(aDirGroup);
127
128   aDirLayout->addWidget( new QLabel("dX", aDirGroup) );
129   myXDir = new QtxDoubleSpinBox( -1000.0, 1000.0, 0.1, aDirGroup );
130   myXDir->setValue( 0.0 );
131   connect(myXDir, SIGNAL(valueChanged(double)), this, SLOT(onValueChanged()));
132   aDirLayout->addWidget( myXDir );
133
134   aDirLayout->addWidget( new QLabel("dY", aDirGroup) );
135   myYDir = new QtxDoubleSpinBox( -1000.0, 1000.0, 0.1, aDirGroup );
136   myYDir->setValue( 0.0 );
137   connect(myYDir, SIGNAL(valueChanged(double)), this, SLOT(onValueChanged()));
138   aDirLayout->addWidget( myYDir );
139
140   aDirLayout->addWidget( new QLabel("dZ", aDirGroup) );
141   myZDir = new QtxDoubleSpinBox( -1000.0, 1000.0, 0.1, aDirGroup );
142   myZDir->setValue( 1.0 );
143   connect(myZDir, SIGNAL(valueChanged(double)), this, SLOT(onValueChanged()));
144   aDirLayout->addWidget( myZDir );
145
146   myAutoApply = new QCheckBox(tr("CHK_AUTOAPPLY"), aPlanesWgt);
147   myAutoApply->setCheckState(Qt::Checked);
148   aFrameLayout->addWidget(myAutoApply);
149
150   // Dialog buttons
151   QGroupBox* aGroupButtons = new QGroupBox (this);
152   aMainLayout->addWidget(aGroupButtons);
153
154   QSizePolicy aSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed );
155   QHBoxLayout* aButtonsLayout = new QHBoxLayout(aGroupButtons);
156
157   QPushButton* aBtnOk = new QPushButton(tr("BUT_OK"), aGroupButtons);
158   aButtonsLayout->addWidget(aBtnOk);
159
160   aButtonsLayout->addStretch();
161
162   QPushButton* aBtnClose = new QPushButton(tr("BUT_CANCEL"), aGroupButtons);
163   aButtonsLayout->addWidget(aBtnClose);
164
165   QPushButton* aBtnHelp = new QPushButton(tr("BUT_HELP"), aGroupButtons);
166   aButtonsLayout->addWidget(aBtnHelp);
167
168   connect(aBtnOk   , SIGNAL(clicked()), this, SLOT(accept()));
169   connect(aBtnClose, SIGNAL(clicked()), this, SLOT(reject()));
170   connect(aBtnHelp , SIGNAL(clicked()), this, SLOT(onHelp()));
171
172
173   myPreviewWidget = createPreviewWidget();
174   myViewWindow->Repaint();
175 }
176
177 VisuGUI_ClippingPlaneDlg::~VisuGUI_ClippingPlaneDlg()
178 {
179   if (myPreviewWidget) {
180     myPreviewWidget->Off();
181     myPreviewWidget->Delete();
182   }
183   myPreviewWidget = 0;
184   myCallback->Delete();
185 }
186
187 //****************************************************************
188 void VisuGUI_ClippingPlaneDlg::ProcessEvents(vtkObject* theObject, 
189                                              unsigned long theEvent,
190                                              void* theClientData, 
191                                              void* vtkNotUsed(theCallData))
192 {
193   vtkImplicitPlaneWidget* aWidget = vtkImplicitPlaneWidget::SafeDownCast(theObject);
194   if (aWidget == NULL) return;
195   if (theClientData == NULL) return;
196
197   VisuGUI_ClippingPlaneDlg* aDlg = (VisuGUI_ClippingPlaneDlg*) theClientData;
198
199   double aOrigin[3];
200   double aDir[3];
201
202   switch(theEvent){
203   case vtkCommand::InteractionEvent:
204     aWidget->GetOrigin(aOrigin);
205     aWidget->GetNormal(aDir);
206
207     aDlg->setOrigin(aOrigin);
208     aDlg->setDirection(aDir);
209
210     break;
211   }
212 }
213
214 //****************************************************************
215 void VisuGUI_ClippingPlaneDlg::setOrigin(double theVal[3])
216 {
217   myXOrigin->setValue(theVal[0]);
218   myYOrigin->setValue(theVal[1]);
219   myZOrigin->setValue(theVal[2]);
220 }
221
222 //****************************************************************
223 void VisuGUI_ClippingPlaneDlg::setDirection(double theVal[3])
224 {
225   myXDir->setValue(theVal[0]);
226   myYDir->setValue(theVal[1]);
227   myZDir->setValue(theVal[2]);
228 }
229
230 //****************************************************************
231 void VisuGUI_ClippingPlaneDlg::onValueChanged()
232 {
233   if (!myPreviewWidget) return;
234   double aOrigin[3];
235   double aDir[3];
236   aOrigin[0] = myXOrigin->value();
237   aOrigin[1] = myYOrigin->value();
238   aOrigin[2] = myZOrigin->value();
239
240   aDir[0] = myXDir->value();
241   aDir[1] = myYDir->value();
242   aDir[2] = myZDir->value();
243
244   myPreviewWidget->SetOrigin(aOrigin);
245   myPreviewWidget->SetNormal(aDir);
246   myViewWindow->Repaint();
247 }
248
249 //****************************************************************
250 vtkImplicitPlaneWidget* VisuGUI_ClippingPlaneDlg::createPreviewWidget()
251 {
252   vtkImplicitPlaneWidget* aPlaneWgt = vtkImplicitPlaneWidget::New();
253   aPlaneWgt->SetInteractor(myViewWindow->getInteractor());
254   aPlaneWgt->SetPlaceFactor(SIZEFACTOR);
255   aPlaneWgt->ScaleEnabledOff();
256   aPlaneWgt->PlaceWidget(myBounds[0],myBounds[1],myBounds[2],myBounds[3],myBounds[4],myBounds[5]);
257   aPlaneWgt->SetOrigin(0,0,0);
258   aPlaneWgt->SetNormal(0,0,1);
259   aPlaneWgt->On();
260
261   //aPlaneWgt->OutlineTranslationOff();
262   //aPlaneWgt->ScaleEnabledOn();
263   aPlaneWgt->AddObserver(vtkCommand::InteractionEvent, 
264                          myCallback.GetPointer(), 
265                          0.);
266   return aPlaneWgt;
267 }
268
269
270 //****************************************************************
271 void VisuGUI_ClippingPlaneDlg::setPlaneId(int theId)
272 {
273   myPlaneId = theId;
274   VISU_ClippingPlaneMgr& aMgr = VISU::GetVisuGen(myModule)->GetClippingPlaneMgr();
275
276   VISU_CutPlaneFunction* aPlane =  aMgr.GetClippingPlane(myPlaneId);
277   if (aPlane) {
278     myNameEdt->setText(QString(aPlane->getName().c_str()));
279     double aOrigin[3], aDir[3];
280     aPlane->GetOrigin(aOrigin);
281     aPlane->GetNormal(aDir);
282
283     myXOrigin->setValue(aOrigin[0]);
284     myYOrigin->setValue(aOrigin[1]);
285     myZOrigin->setValue(aOrigin[2]);
286     myPreviewWidget->SetOrigin(aOrigin);
287
288     myXDir->setValue(aDir[0]);
289     myYDir->setValue(aDir[1]);
290     myZDir->setValue(aDir[2]);
291
292     myPreviewWidget->SetNormal(aDir);
293
294     myAutoApply->setCheckState((aPlane->isAuto())? Qt::Checked : Qt::Unchecked);
295     myViewWindow->Repaint();
296   }
297 }
298 /*void VisuGUI_ClippingPlaneDlg::setPlaneObj(_PTR(SObject) thePlaneObj)
299 {
300   myPlaneObj = thePlaneObj;
301
302   myNameEdt->setText(QString(myPlaneObj->GetName().c_str()));
303
304   _PTR(GenericAttribute) anAttr;
305   if (myPlaneObj->FindAttribute(anAttr, "AttributeSequenceOfReal")) {
306     _PTR(AttributeSequenceOfReal) aArray(anAttr);
307     myXOrigin->setValue(aArray->Value(1));
308     myYOrigin->setValue(aArray->Value(2));
309     myZOrigin->setValue(aArray->Value(3));
310
311     myPreviewWidget->SetOrigin(aArray->Value(1), aArray->Value(2), aArray->Value(3));
312
313     myXDir->setValue(aArray->Value(4));
314     myYDir->setValue(aArray->Value(5));
315     myZDir->setValue(aArray->Value(6));
316
317     myPreviewWidget->SetNormal(aArray->Value(4), aArray->Value(5), aArray->Value(6));
318   }
319   if (myPlaneObj->FindAttribute(anAttr, "AttributeInteger")) {
320     _PTR(AttributeInteger) aFlag(anAttr);
321     myAutoApply->setCheckState((aFlag->Value() == 1)? Qt::Checked : Qt::Unchecked);
322   }
323
324   myViewWindow->Repaint();
325 }*/
326
327 //****************************************************************
328 void VisuGUI_ClippingPlaneDlg::accept()
329 {
330   _PTR(Study) aStudy = VISU::GetCStudy( VISU::GetAppStudy( myModule ) );
331   if(!aStudy->GetProperties()->IsLocked()) {
332     VISU_ClippingPlaneMgr& aMgr = VISU::GetVisuGen(myModule)->GetClippingPlaneMgr();
333     if (myPlaneId == -1) { // Create a New plane
334       myPlaneId = aMgr.CreateClippingPlane(myXOrigin->value(), myYOrigin->value(), myZOrigin->value(),
335                                            myXDir->value(), myYDir->value(), myZDir->value(),
336                                            myAutoApply->checkState() == Qt::Checked,
337                                            qPrintable(myNameEdt->text()));
338     } else { // Edit Plane
339       aMgr.EditClippingPlane(myPlaneId,
340                              myXOrigin->value(), myYOrigin->value(), myZOrigin->value(),
341                              myXDir->value(), myYDir->value(), myZDir->value(),
342                              myAutoApply->checkState() == Qt::Checked,
343                              qPrintable(myNameEdt->text()));
344     }
345     VISU::UpdateObjBrowser(myModule);
346   }
347   /*  _PTR(Study) aStudy = VISU::GetCStudy( VISU::GetAppStudy( myModule ) );
348   if(!aStudy->GetProperties()->IsLocked()) {
349     _PTR(SObject) aFolder;
350     if (VISU::getClippingPlanesFolder(aStudy, aFolder)) {
351       _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
352       if (myPlaneObj == 0) { // Create a New plane
353         myPlaneObj = aBuilder->NewObject(aFolder);
354       } 
355       // Save Name
356       _PTR(GenericAttribute) anAttr;
357       anAttr = aBuilder->FindOrCreateAttribute(myPlaneObj,"AttributeName");
358       _PTR(AttributeName) aName(anAttr);
359       aName->SetValue(myNameEdt->text().toStdString());
360
361       //Save Parameters
362       double aParams[6];
363       aParams[0] = myXOrigin->value();
364       aParams[1] = myYOrigin->value();
365       aParams[2] = myZOrigin->value();
366       aParams[3] = myXDir->value();
367       aParams[4] = myYDir->value();
368       aParams[5] = myZDir->value();
369
370       anAttr = aBuilder->FindOrCreateAttribute(myPlaneObj,"AttributeSequenceOfReal");
371       _PTR(AttributeSequenceOfReal) aArray(anAttr);
372       if (aArray->Length() == 6) {
373         for (int i = 0; i < 6; i++)
374           aArray->ChangeValue(i+1, aParams[i]);
375       } else {
376         for (int i = 0; i < 6; i++)
377           aArray->Add(aParams[i]);
378       }
379       // Save Bool Flag
380       anAttr = aBuilder->FindOrCreateAttribute(myPlaneObj,"AttributeInteger");
381       _PTR(AttributeInteger) aFlag(anAttr);
382       aFlag->SetValue((myAutoApply->checkState() == Qt::Checked)? 1 : 0);
383
384       VISU::UpdateObjBrowser(myModule);
385     }
386     }*/
387   QDialog::accept();
388 }
389
390
391 //****************************************************************
392 void VisuGUI_ClippingPlaneDlg::onHelp()
393 {
394   QString aHelpFileName = "clipping_page.html";
395   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
396   if (app)
397     app->onHelpContextModule(myModule ? app->moduleName(myModule->moduleName()) : QString(""), aHelpFileName);
398   else {
399     QString platform;
400 #ifdef WIN32
401     platform = "winapplication";
402 #else
403     platform = "application";
404 #endif
405     SUIT_MessageBox::warning(0, QObject::tr("WRN_WARNING"),
406                              QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
407                              arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(aHelpFileName),
408                              QObject::tr("BUT_OK"));
409   }
410 }
411