1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "VisuGUI_ClippingPlaneDlg.h"
22 #include "VisuGUI_Tools.h"
23 #include "VisuGUI_ViewTools.h"
25 #include <VISU_Gen_i.hh>
27 #include <LightApp_Application.h>
28 #include <SUIT_Desktop.h>
29 #include <SUIT_Session.h>
30 #include <SUIT_ViewManager.h>
31 #include <SUIT_MessageBox.h>
32 #include <SUIT_ResourceMgr.h>
33 #include <SVTK_ViewWindow.h>
34 #include <VTKViewer_Utilities.h>
35 #include <SalomeApp_DoubleSpinBox.h>
37 #include <QVBoxLayout>
38 #include <QHBoxLayout>
42 #include <QGridLayout>
44 #include <QPushButton>
46 #include <vtkCallbackCommand.h>
47 #include <vtkImplicitPlaneWidget.h>
50 #define SIZEFACTOR 1.1
55 //****************************************************************
56 //****************************************************************
57 //****************************************************************
58 VisuGUI_ClippingPlaneDlg::VisuGUI_ClippingPlaneDlg(VisuGUI* theModule)
59 : QDialog(VISU::GetDesktop(theModule), Qt::WindowTitleHint | Qt::WindowSystemMenuHint ),
61 myCallback( vtkCallbackCommand::New() ),
66 myViewWindow = VISU::GetActiveViewWindow<SVTK_ViewWindow>(myModule);
67 VISU::ComputeVisiblePropBounds(myViewWindow, myBounds);
69 setWindowTitle(tr("TITLE"));
70 setSizeGripEnabled(true);
73 myCallback->SetClientData(this);
74 myCallback->SetCallback(VisuGUI_ClippingPlaneDlg::ProcessEvents);
76 QVBoxLayout* aMainLayout = new QVBoxLayout(this);
78 QWidget* aPlanesWgt = new QWidget(this);
79 aMainLayout->addWidget(aPlanesWgt);
80 QVBoxLayout* aFrameLayout = new QVBoxLayout(aPlanesWgt);
82 QWidget* aNameBox = new QWidget(aPlanesWgt);
83 aFrameLayout->addWidget(aNameBox);
84 QHBoxLayout* aNameLayout = new QHBoxLayout(aNameBox);
86 aNameLayout->addWidget(new QLabel(tr("LBL_NAME"), aPlanesWgt));
87 myNameEdt = new QLineEdit();
89 _PTR(Study) aStudy = VISU::GetCStudy(VISU::GetAppStudy(myModule));
90 _PTR(SObject) aFolderSO;
91 if (VISU::getClippingPlanesFolder(aStudy, aFolderSO)) {
92 _PTR(ChildIterator) aIter = aStudy->NewChildIterator(aFolderSO);
94 for (; aIter->More(); aIter->Next()) i++;
96 myNameEdt->setText(QString("Plane %1").arg(i));
98 aNameLayout->addWidget(myNameEdt);
100 QGroupBox* aOriginGroup = new QGroupBox( tr( "ORIGIN_TITLE" ), aPlanesWgt );
101 aFrameLayout->addWidget(aOriginGroup);
102 QHBoxLayout* aOriginLayout = new QHBoxLayout(aOriginGroup);
104 aOriginLayout->addWidget( new QLabel("X", aOriginGroup) );
105 myXOrigin = new SalomeApp_DoubleSpinBox( aOriginGroup );
106 VISU::initSpinBox( myXOrigin, -1000.0, 1000.0, 0.1, "length_precision" );
107 myXOrigin->setValue( 0.0 );
108 connect(myXOrigin, SIGNAL(valueChanged(double)), this, SLOT(onValueChanged()));
109 aOriginLayout->addWidget( myXOrigin );
111 aOriginLayout->addWidget( new QLabel("Y", aOriginGroup) );
112 myYOrigin = new SalomeApp_DoubleSpinBox( aOriginGroup );
113 VISU::initSpinBox( myYOrigin, -1000.0, 1000.0, 0.1, "length_precision" );
114 myYOrigin->setValue( 0.0 );
115 connect(myYOrigin, SIGNAL(valueChanged(double)), this, SLOT(onValueChanged()));
116 aOriginLayout->addWidget( myYOrigin );
118 aOriginLayout->addWidget( new QLabel("Z", aOriginGroup) );
119 myZOrigin = new SalomeApp_DoubleSpinBox( aOriginGroup );
120 VISU::initSpinBox( myZOrigin, -1000.0, 1000.0, 0.1, "length_precision" );
121 myZOrigin->setValue( 0.0 );
122 connect(myZOrigin, SIGNAL(valueChanged(double)), this, SLOT(onValueChanged()));
123 aOriginLayout->addWidget( myZOrigin );
125 QGroupBox* aDirGroup = new QGroupBox( tr( "DIRECTION_TITLE" ), aPlanesWgt );
126 aFrameLayout->addWidget(aDirGroup);
127 QHBoxLayout* aDirLayout = new QHBoxLayout(aDirGroup);
129 aDirLayout->addWidget( new QLabel("dX", aDirGroup) );
130 myXDir = new SalomeApp_DoubleSpinBox( aDirGroup );
131 VISU::initSpinBox( myXDir, -1000.0, 1000.0, 0.1, "length_precision" );
132 myXDir->setValue( 0.0 );
133 connect(myXDir, SIGNAL(valueChanged(double)), this, SLOT(onValueChanged()));
134 aDirLayout->addWidget( myXDir );
136 aDirLayout->addWidget( new QLabel("dY", aDirGroup) );
137 myYDir = new SalomeApp_DoubleSpinBox( aDirGroup );
138 VISU::initSpinBox( myYDir, -1000.0, 1000.0, 0.1, "length_precision" );
139 myYDir->setValue( 0.0 );
140 connect(myYDir, SIGNAL(valueChanged(double)), this, SLOT(onValueChanged()));
141 aDirLayout->addWidget( myYDir );
143 aDirLayout->addWidget( new QLabel("dZ", aDirGroup) );
144 myZDir = new SalomeApp_DoubleSpinBox( aDirGroup );
145 VISU::initSpinBox( myZDir, -1000.0, 1000.0, 0.1, "length_precision" );
146 myZDir->setValue( 1.0 );
147 connect(myZDir, SIGNAL(valueChanged(double)), this, SLOT(onValueChanged()));
148 aDirLayout->addWidget( myZDir );
150 myAutoApply = new QCheckBox(tr("CHK_AUTOAPPLY"), aPlanesWgt);
151 myAutoApply->setCheckState(Qt::Checked);
152 aFrameLayout->addWidget(myAutoApply);
155 QGroupBox* aGroupButtons = new QGroupBox (this);
156 aMainLayout->addWidget(aGroupButtons);
158 QSizePolicy aSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed );
159 QHBoxLayout* aButtonsLayout = new QHBoxLayout(aGroupButtons);
161 QPushButton* aBtnOk = new QPushButton(tr("BUT_OK"), aGroupButtons);
162 aButtonsLayout->addWidget(aBtnOk);
164 aButtonsLayout->addStretch();
166 QPushButton* aBtnClose = new QPushButton(tr("BUT_CANCEL"), aGroupButtons);
167 aButtonsLayout->addWidget(aBtnClose);
169 QPushButton* aBtnHelp = new QPushButton(tr("BUT_HELP"), aGroupButtons);
170 aButtonsLayout->addWidget(aBtnHelp);
172 connect(aBtnOk , SIGNAL(clicked()), this, SLOT(accept()));
173 connect(aBtnClose, SIGNAL(clicked()), this, SLOT(reject()));
174 connect(aBtnHelp , SIGNAL(clicked()), this, SLOT(onHelp()));
177 myPreviewWidget = createPreviewWidget();
178 myViewWindow->Repaint();
181 VisuGUI_ClippingPlaneDlg::~VisuGUI_ClippingPlaneDlg()
183 if (myPreviewWidget) {
184 myPreviewWidget->Off();
185 myPreviewWidget->Delete();
188 myCallback->Delete();
191 //****************************************************************
192 void VisuGUI_ClippingPlaneDlg::ProcessEvents(vtkObject* theObject,
193 unsigned long theEvent,
195 void* vtkNotUsed(theCallData))
197 vtkImplicitPlaneWidget* aWidget = vtkImplicitPlaneWidget::SafeDownCast(theObject);
198 if (aWidget == NULL) return;
199 if (theClientData == NULL) return;
201 VisuGUI_ClippingPlaneDlg* aDlg = (VisuGUI_ClippingPlaneDlg*) theClientData;
207 case vtkCommand::InteractionEvent:
208 aWidget->GetOrigin(aOrigin);
209 aWidget->GetNormal(aDir);
211 aDlg->setOrigin(aOrigin);
212 aDlg->setDirection(aDir);
218 //****************************************************************
219 void VisuGUI_ClippingPlaneDlg::setOrigin(double theVal[3])
221 myXOrigin->setValue(theVal[0]);
222 myYOrigin->setValue(theVal[1]);
223 myZOrigin->setValue(theVal[2]);
226 //****************************************************************
227 void VisuGUI_ClippingPlaneDlg::setDirection(double theVal[3])
229 myXDir->setValue(theVal[0]);
230 myYDir->setValue(theVal[1]);
231 myZDir->setValue(theVal[2]);
234 //****************************************************************
235 void VisuGUI_ClippingPlaneDlg::onValueChanged()
237 if (!myPreviewWidget) return;
240 aOrigin[0] = myXOrigin->value();
241 aOrigin[1] = myYOrigin->value();
242 aOrigin[2] = myZOrigin->value();
244 aDir[0] = myXDir->value();
245 aDir[1] = myYDir->value();
246 aDir[2] = myZDir->value();
248 myPreviewWidget->SetOrigin(aOrigin);
249 myPreviewWidget->SetNormal(aDir);
250 myViewWindow->Repaint();
253 //****************************************************************
254 vtkImplicitPlaneWidget* VisuGUI_ClippingPlaneDlg::createPreviewWidget()
256 vtkImplicitPlaneWidget* aPlaneWgt = vtkImplicitPlaneWidget::New();
257 aPlaneWgt->SetInteractor(myViewWindow->getInteractor());
258 aPlaneWgt->SetPlaceFactor(SIZEFACTOR);
259 aPlaneWgt->ScaleEnabledOff();
260 aPlaneWgt->PlaceWidget(myBounds[0],myBounds[1],myBounds[2],myBounds[3],myBounds[4],myBounds[5]);
261 aPlaneWgt->SetOrigin(0,0,0);
262 aPlaneWgt->SetNormal(0,0,1);
265 //aPlaneWgt->OutlineTranslationOff();
266 //aPlaneWgt->ScaleEnabledOn();
267 aPlaneWgt->AddObserver(vtkCommand::InteractionEvent,
268 myCallback.GetPointer(),
274 //****************************************************************
275 void VisuGUI_ClippingPlaneDlg::setPlaneId(int theId)
278 VISU_ClippingPlaneMgr& aMgr = VISU::GetVisuGen(myModule)->GetClippingPlaneMgr();
280 VISU_CutPlaneFunction* aPlane = aMgr.GetClippingPlane(myPlaneId);
282 myNameEdt->setText(QString(aPlane->getName().c_str()));
283 double aOrigin[3], aDir[3];
284 aPlane->GetOrigin(aOrigin);
285 aPlane->GetNormal(aDir);
287 myXOrigin->setValue(aOrigin[0]);
288 myYOrigin->setValue(aOrigin[1]);
289 myZOrigin->setValue(aOrigin[2]);
290 myPreviewWidget->SetOrigin(aOrigin);
292 myXDir->setValue(aDir[0]);
293 myYDir->setValue(aDir[1]);
294 myZDir->setValue(aDir[2]);
296 myPreviewWidget->SetNormal(aDir);
298 myAutoApply->setCheckState((aPlane->isAuto())? Qt::Checked : Qt::Unchecked);
299 myViewWindow->Repaint();
302 /*void VisuGUI_ClippingPlaneDlg::setPlaneObj(_PTR(SObject) thePlaneObj)
304 myPlaneObj = thePlaneObj;
306 myNameEdt->setText(QString(myPlaneObj->GetName().c_str()));
308 _PTR(GenericAttribute) anAttr;
309 if (myPlaneObj->FindAttribute(anAttr, "AttributeSequenceOfReal")) {
310 _PTR(AttributeSequenceOfReal) aArray(anAttr);
311 myXOrigin->setValue(aArray->Value(1));
312 myYOrigin->setValue(aArray->Value(2));
313 myZOrigin->setValue(aArray->Value(3));
315 myPreviewWidget->SetOrigin(aArray->Value(1), aArray->Value(2), aArray->Value(3));
317 myXDir->setValue(aArray->Value(4));
318 myYDir->setValue(aArray->Value(5));
319 myZDir->setValue(aArray->Value(6));
321 myPreviewWidget->SetNormal(aArray->Value(4), aArray->Value(5), aArray->Value(6));
323 if (myPlaneObj->FindAttribute(anAttr, "AttributeInteger")) {
324 _PTR(AttributeInteger) aFlag(anAttr);
325 myAutoApply->setCheckState((aFlag->Value() == 1)? Qt::Checked : Qt::Unchecked);
328 myViewWindow->Repaint();
331 //****************************************************************
332 void VisuGUI_ClippingPlaneDlg::accept()
334 _PTR(Study) aStudy = VISU::GetCStudy( VISU::GetAppStudy( myModule ) );
335 if(!aStudy->GetProperties()->IsLocked()) {
336 VISU_ClippingPlaneMgr& aMgr = VISU::GetVisuGen(myModule)->GetClippingPlaneMgr();
337 if (myPlaneId == -1) { // Create a New plane
338 myPlaneId = aMgr.CreateClippingPlane(myXOrigin->value(), myYOrigin->value(), myZOrigin->value(),
339 myXDir->value(), myYDir->value(), myZDir->value(),
340 myAutoApply->checkState() == Qt::Checked,
341 qPrintable(myNameEdt->text()));
342 } else { // Edit Plane
343 aMgr.EditClippingPlane(myPlaneId,
344 myXOrigin->value(), myYOrigin->value(), myZOrigin->value(),
345 myXDir->value(), myYDir->value(), myZDir->value(),
346 myAutoApply->checkState() == Qt::Checked,
347 qPrintable(myNameEdt->text()));
349 VISU::UpdateObjBrowser(myModule);
351 /* _PTR(Study) aStudy = VISU::GetCStudy( VISU::GetAppStudy( myModule ) );
352 if(!aStudy->GetProperties()->IsLocked()) {
353 _PTR(SObject) aFolder;
354 if (VISU::getClippingPlanesFolder(aStudy, aFolder)) {
355 _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
356 if (myPlaneObj == 0) { // Create a New plane
357 myPlaneObj = aBuilder->NewObject(aFolder);
360 _PTR(GenericAttribute) anAttr;
361 anAttr = aBuilder->FindOrCreateAttribute(myPlaneObj,"AttributeName");
362 _PTR(AttributeName) aName(anAttr);
363 aName->SetValue(myNameEdt->text().toStdString());
367 aParams[0] = myXOrigin->value();
368 aParams[1] = myYOrigin->value();
369 aParams[2] = myZOrigin->value();
370 aParams[3] = myXDir->value();
371 aParams[4] = myYDir->value();
372 aParams[5] = myZDir->value();
374 anAttr = aBuilder->FindOrCreateAttribute(myPlaneObj,"AttributeSequenceOfReal");
375 _PTR(AttributeSequenceOfReal) aArray(anAttr);
376 if (aArray->Length() == 6) {
377 for (int i = 0; i < 6; i++)
378 aArray->ChangeValue(i+1, aParams[i]);
380 for (int i = 0; i < 6; i++)
381 aArray->Add(aParams[i]);
384 anAttr = aBuilder->FindOrCreateAttribute(myPlaneObj,"AttributeInteger");
385 _PTR(AttributeInteger) aFlag(anAttr);
386 aFlag->SetValue((myAutoApply->checkState() == Qt::Checked)? 1 : 0);
388 VISU::UpdateObjBrowser(myModule);
395 //****************************************************************
396 void VisuGUI_ClippingPlaneDlg::onHelp()
398 QString aHelpFileName = "clipping_page.html";
399 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
401 app->onHelpContextModule(myModule ? app->moduleName(myModule->moduleName()) : QString(""), aHelpFileName);
405 platform = "winapplication";
407 platform = "application";
409 SUIT_MessageBox::warning(0, QObject::tr("WRN_WARNING"),
410 QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
411 arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(aHelpFileName),
412 QObject::tr("BUT_OK"));