1
/***************************************************************************
2
* Copyright (c) 2013 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
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 *
14
* GNU Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
24
#include "PreCompiled.h"
26
# include <Inventor/actions/SoGetBoundingBoxAction.h>
27
# include <Inventor/nodes/SoClipPlane.h>
28
# include <Inventor/nodes/SoGroup.h>
29
# include <Inventor/sensors/SoTimerSensor.h>
30
# include <QDockWidget>
35
#include "ui_Clipping.h"
36
#include "DockWindowManager.h"
37
#include "View3DInventor.h"
38
#include "View3DInventorViewer.h"
40
using namespace Gui::Dialog;
42
class Clipping::Private {
45
QPointer<Gui::View3DInventor> view;
50
SoClipPlane* clipView;
54
SoTimerSensor* sensor;
57
clipX = new SoClipPlane();
58
clipX->on.setValue(false);
59
clipX->plane.setValue(SbPlane(SbVec3f(1,0,0),0));
62
clipY = new SoClipPlane();
63
clipY->on.setValue(false);
64
clipY->plane.setValue(SbPlane(SbVec3f(0,1,0),0));
67
clipZ = new SoClipPlane();
68
clipZ->on.setValue(false);
69
clipZ->plane.setValue(SbPlane(SbVec3f(0,0,1),0));
72
clipView = new SoClipPlane();
73
clipView->on.setValue(false);
74
clipView->plane.setValue(SbPlane(SbVec3f(0,0,1),0));
78
sensor = new SoTimerSensor(moveCallback, this);
88
static void moveCallback(void * data, SoSensor * sensor)
91
auto self = static_cast<Private*>(data);
93
Gui::View3DInventorViewer* view = self->view->getViewer();
94
SoClipPlane* clip = self->clipView;
95
SbPlane pln = clip->plane.getValue();
96
clip->plane.setValue(SbPlane(view->getViewDirection(),pln.getDistanceFromOrigin()));
101
/* TRANSLATOR Gui::Dialog::Clipping */
103
Clipping::Clipping(Gui::View3DInventor* view, QWidget* parent)
111
d->ui.clipView->setRange(-INT_MAX,INT_MAX);
112
d->ui.clipView->setSingleStep(0.1f);
113
d->ui.clipX->setRange(-INT_MAX,INT_MAX);
114
d->ui.clipX->setSingleStep(0.1f);
115
d->ui.clipY->setRange(-INT_MAX,INT_MAX);
116
d->ui.clipY->setSingleStep(0.1f);
117
d->ui.clipZ->setRange(-INT_MAX,INT_MAX);
118
d->ui.clipZ->setSingleStep(0.1f);
120
d->ui.dirX->setRange(-INT_MAX,INT_MAX);
121
d->ui.dirX->setSingleStep(0.1f);
122
d->ui.dirY->setRange(-INT_MAX,INT_MAX);
123
d->ui.dirY->setSingleStep(0.1f);
124
d->ui.dirZ->setRange(-INT_MAX,INT_MAX);
125
d->ui.dirZ->setSingleStep(0.1f);
126
d->ui.dirZ->setValue(1.0f);
129
View3DInventorViewer* viewer = view->getViewer();
130
d->node = static_cast<SoGroup*>(viewer->getSceneGraph());
132
d->node->insertChild(d->clipX, 0);
133
d->node->insertChild(d->clipY, 0);
134
d->node->insertChild(d->clipZ, 0);
135
d->node->insertChild(d->clipView, 0);
137
SoGetBoundingBoxAction action(viewer->getSoRenderManager()->getViewportRegion());
138
action.apply(viewer->getSceneGraph());
139
SbBox3f box = action.getBoundingBox();
141
if (!box.isEmpty()) {
142
SbVec3f cnt = box.getCenter();
143
d->ui.clipView->setValue(cnt[2]);
144
d->ui.clipX->setValue(cnt[0]);
145
d->ui.clipY->setValue(cnt[1]);
146
d->ui.clipZ->setValue(cnt[2]);
149
float lenx, leny,lenz;
150
box.getSize(lenx, leny, lenz);
152
float minlen = std::min<float>(lenx, std::min<float>(leny, lenz));
154
// determine the single step values
156
minlen = minlen / steps;
157
int dim = static_cast<int>(log10(minlen));
158
double singleStep = pow(10.0, dim);
159
d->ui.clipView->setSingleStep(singleStep);
160
minDecimals = std::max(minDecimals, -dim);
164
int dim = static_cast<int>(log10(lenx));
165
double singleStep = pow(10.0, dim);
166
d->ui.clipX->setSingleStep(singleStep);
170
int dim = static_cast<int>(log10(leny));
171
double singleStep = pow(10.0, dim);
172
d->ui.clipY->setSingleStep(singleStep);
176
int dim = static_cast<int>(log10(lenz));
177
double singleStep = pow(10.0, dim);
178
d->ui.clipZ->setSingleStep(singleStep);
182
d->ui.clipView->setDecimals(minDecimals);
183
d->ui.clipX->setDecimals(minDecimals);
184
d->ui.clipY->setDecimals(minDecimals);
185
d->ui.clipZ->setDecimals(minDecimals);
189
Clipping* Clipping::makeDockWidget(Gui::View3DInventor* view)
191
// embed this dialog into a QDockWidget
192
auto clipping = new Clipping(view);
193
Gui::DockWindowManager* pDockMgr = Gui::DockWindowManager::instance();
194
QDockWidget* dw = pDockMgr->addDockWindow("Clipping", clipping, Qt::LeftDockWidgetArea);
195
dw->setFeatures(QDockWidget::DockWidgetMovable|QDockWidget::DockWidgetFloatable);
201
/** Destroys the object and frees any allocated resources */
204
d->node->removeChild(d->clipX);
205
d->node->removeChild(d->clipY);
206
d->node->removeChild(d->clipZ);
207
d->node->removeChild(d->clipView);
212
void Clipping::setupConnections()
214
connect(d->ui.groupBoxX, &QGroupBox::toggled,
215
this, &Clipping::onGroupBoxXToggled);
216
connect(d->ui.groupBoxY, &QGroupBox::toggled,
217
this, &Clipping::onGroupBoxYToggled);
218
connect(d->ui.groupBoxZ, &QGroupBox::toggled,
219
this, &Clipping::onGroupBoxZToggled);
220
connect(d->ui.clipX, qOverload<double>(&QDoubleSpinBox::valueChanged),
221
this, &Clipping::onClipXValueChanged);
222
connect(d->ui.clipY, qOverload<double>(&QDoubleSpinBox::valueChanged),
223
this, &Clipping::onClipYValueChanged);
224
connect(d->ui.clipZ, qOverload<double>(&QDoubleSpinBox::valueChanged),
225
this, &Clipping::onClipZValueChanged);
226
connect(d->ui.flipClipX, &QPushButton::clicked,
227
this, &Clipping::onFlipClipXClicked);
228
connect(d->ui.flipClipY, &QPushButton::clicked,
229
this, &Clipping::onFlipClipYClicked);
230
connect(d->ui.flipClipZ, &QPushButton::clicked,
231
this, &Clipping::onFlipClipZClicked);
232
connect(d->ui.groupBoxView, &QGroupBox::toggled,
233
this, &Clipping::onGroupBoxViewToggled);
234
connect(d->ui.clipView, qOverload<double>(&QDoubleSpinBox::valueChanged),
235
this, &Clipping::onClipViewValueChanged);
236
connect(d->ui.fromView, &QPushButton::clicked,
237
this, &Clipping::onFromViewClicked);
238
connect(d->ui.adjustViewdirection, &QCheckBox::toggled,
239
this, &Clipping::onAdjustViewdirectionToggled);
240
connect(d->ui.dirX, qOverload<double>(&QDoubleSpinBox::valueChanged),
241
this, &Clipping::onDirXValueChanged);
242
connect(d->ui.dirY, qOverload<double>(&QDoubleSpinBox::valueChanged),
243
this, &Clipping::onDirYValueChanged);
244
connect(d->ui.dirZ, qOverload<double>(&QDoubleSpinBox::valueChanged),
245
this, &Clipping::onDirZValueChanged);
248
void Clipping::reject()
251
auto dw = qobject_cast<QDockWidget*>(parent());
257
void Clipping::onGroupBoxXToggled(bool on)
260
d->ui.groupBoxView->setChecked(false);
263
d->clipX->on.setValue(on);
266
void Clipping::onGroupBoxYToggled(bool on)
269
d->ui.groupBoxView->setChecked(false);
272
d->clipY->on.setValue(on);
275
void Clipping::onGroupBoxZToggled(bool on)
278
d->ui.groupBoxView->setChecked(false);
281
d->clipZ->on.setValue(on);
284
void Clipping::onClipXValueChanged(double val)
286
SbPlane pln = d->clipX->plane.getValue();
287
d->clipX->plane.setValue(SbPlane(pln.getNormal(),d->flipX ? -val : val));
290
void Clipping::onClipYValueChanged(double val)
292
SbPlane pln = d->clipY->plane.getValue();
293
d->clipY->plane.setValue(SbPlane(pln.getNormal(),d->flipY ? -val : val));
296
void Clipping::onClipZValueChanged(double val)
298
SbPlane pln = d->clipZ->plane.getValue();
299
d->clipZ->plane.setValue(SbPlane(pln.getNormal(),d->flipZ ? -val : val));
302
void Clipping::onFlipClipXClicked()
304
d->flipX = !d->flipX;
305
SbPlane pln = d->clipX->plane.getValue();
306
d->clipX->plane.setValue(SbPlane(-pln.getNormal(),-pln.getDistanceFromOrigin()));
309
void Clipping::onFlipClipYClicked()
311
d->flipY = !d->flipY;
312
SbPlane pln = d->clipY->plane.getValue();
313
d->clipY->plane.setValue(SbPlane(-pln.getNormal(),-pln.getDistanceFromOrigin()));
316
void Clipping::onFlipClipZClicked()
318
d->flipZ = !d->flipZ;
319
SbPlane pln = d->clipZ->plane.getValue();
320
d->clipZ->plane.setValue(SbPlane(-pln.getNormal(),-pln.getDistanceFromOrigin()));
323
void Clipping::onGroupBoxViewToggled(bool on)
326
d->ui.groupBoxX->setChecked(false);
327
d->ui.groupBoxY->setChecked(false);
328
d->ui.groupBoxZ->setChecked(false);
331
d->clipView->on.setValue(on);
334
void Clipping::onClipViewValueChanged(double val)
336
SbPlane pln = d->clipView->plane.getValue();
337
d->clipView->plane.setValue(SbPlane(pln.getNormal(),val));
340
void Clipping::onFromViewClicked()
343
Gui::View3DInventorViewer* view = d->view->getViewer();
344
SbVec3f dir = view->getViewDirection();
345
SbPlane pln = d->clipView->plane.getValue();
346
d->clipView->plane.setValue(SbPlane(dir,pln.getDistanceFromOrigin()));
350
void Clipping::onAdjustViewdirectionToggled(bool on)
352
d->ui.dirX->setDisabled(on);
353
d->ui.dirY->setDisabled(on);
354
d->ui.dirZ->setDisabled(on);
355
d->ui.fromView->setDisabled(on);
358
d->sensor->schedule();
360
d->sensor->unschedule();
363
void Clipping::onDirXValueChanged(double)
365
double x = d->ui.dirX->value();
366
double y = d->ui.dirY->value();
367
double z = d->ui.dirZ->value();
369
SbPlane pln = d->clipView->plane.getValue();
370
SbVec3f normal(x,y,z);
371
if (normal.sqrLength() > 0.0f)
372
d->clipView->plane.setValue(SbPlane(normal,pln.getDistanceFromOrigin()));
375
void Clipping::onDirYValueChanged(double)
377
double x = d->ui.dirX->value();
378
double y = d->ui.dirY->value();
379
double z = d->ui.dirZ->value();
381
SbPlane pln = d->clipView->plane.getValue();
382
SbVec3f normal(x,y,z);
383
if (normal.sqrLength() > 0.0f)
384
d->clipView->plane.setValue(SbPlane(normal,pln.getDistanceFromOrigin()));
387
void Clipping::onDirZValueChanged(double)
389
double x = d->ui.dirX->value();
390
double y = d->ui.dirY->value();
391
double z = d->ui.dirZ->value();
393
SbPlane pln = d->clipView->plane.getValue();
394
SbVec3f normal(x,y,z);
395
if (normal.sqrLength() > 0.0f)
396
d->clipView->plane.setValue(SbPlane(normal,pln.getDistanceFromOrigin()));
399
#include "moc_Clipping.cpp"