Debian Codegraph @

src/Gui/Clipping.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2013 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
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. *
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 *
14 * GNU Library General Public License for more details. *
15 * *
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 *
20 * *
21 ***************************************************************************/
22
23 #include <limits>
24
25 #include <Inventor/actions/SoGetBoundingBoxAction.h>
26 #include <Inventor/nodes/SoClipPlane.h>
27 #include <Inventor/nodes/SoGroup.h>
28 #include <Inventor/sensors/SoTimerSensor.h>
29 #include <QDockWidget>
30 #include <QPointer>
31
32 #include "Clipping.h"
33 #include "ui_Clipping.h"
34 #include "DockWindowManager.h"
35 #include "View3DInventor.h"
36 #include "View3DInventorViewer.h"
37
38 using namespace Gui::Dialog;
39
40 class Clipping::Private
41 {
42 public:
43 Ui_Clipping ui;
44 QPointer<Gui::View3DInventor> view;
45 SoGroup* node;
46 SoClipPlane* clipX;
47 SoClipPlane* clipY;
48 SoClipPlane* clipZ;
49 SoClipPlane* clipView;
50 bool flipX {false};
51 bool flipY {false};
52 bool flipZ {false};
53 SoTimerSensor* sensor;
54 Private()
55 {
56 clipX = new SoClipPlane();
57 clipX->on.setValue(false);
58 clipX->plane.setValue(SbPlane(SbVec3f(1, 0, 0), 0));
59 clipX->ref();
60
61 clipY = new SoClipPlane();
62 clipY->on.setValue(false);
63 clipY->plane.setValue(SbPlane(SbVec3f(0, 1, 0), 0));
64 clipY->ref();
65
66 clipZ = new SoClipPlane();
67 clipZ->on.setValue(false);
68 clipZ->plane.setValue(SbPlane(SbVec3f(0, 0, 1), 0));
69 clipZ->ref();
70
71 clipView = new SoClipPlane();
72 clipView->on.setValue(false);
73 clipView->plane.setValue(SbPlane(SbVec3f(0, 0, 1), 0));
74 clipView->ref();
75
76 node = nullptr;
77 sensor = new SoTimerSensor(moveCallback, this);
78 }
79 ~Private()
80 {
81 clipX->unref();
82 clipY->unref();
83 clipZ->unref();
84 clipView->unref();
85 delete sensor;
86 }
87 static void moveCallback(void* data, SoSensor* sensor)
88 {
89 Q_UNUSED(sensor);
90 auto self = static_cast<Private*>(data);
91 if (self->view) {
92 Gui::View3DInventorViewer* view = self->view->getViewer();
93 SoClipPlane* clip = self->clipView;
94 SbPlane pln = clip->plane.getValue();
95 clip->plane.setValue(SbPlane(view->getViewDirection(), pln.getDistanceFromOrigin()));
96 }
97 }
98 };
99
100 /* TRANSLATOR Gui::Dialog::Clipping */
101
102 Clipping::Clipping(Gui::View3DInventor* view, QWidget* parent)
103 : QDialog(parent)
104 , d(new Private)
105 {
106 // create widgets
107 d->ui.setupUi(this);
108 setupConnections();
109
110 constexpr int max = std::numeric_limits<int>::max();
111 d->ui.clipView->setRange(-max, max);
112 d->ui.clipView->setSingleStep(0.1f);
113 d->ui.clipX->setRange(-max, max);
114 d->ui.clipX->setSingleStep(0.1f);
115 d->ui.clipY->setRange(-max, max);
116 d->ui.clipY->setSingleStep(0.1f);
117 d->ui.clipZ->setRange(-max, max);
118 d->ui.clipZ->setSingleStep(0.1f);
119
120 d->ui.dirX->setRange(-max, max);
121 d->ui.dirX->setSingleStep(0.1f);
122 d->ui.dirY->setRange(-max, max);
123 d->ui.dirY->setSingleStep(0.1f);
124 d->ui.dirZ->setRange(-max, max);
125 d->ui.dirZ->setSingleStep(0.1f);
126 d->ui.dirZ->setValue(1.0f);
127
128 d->view = view;
129 View3DInventorViewer* viewer = view->getViewer();
130 d->node = static_cast<SoGroup*>(viewer->getSceneGraph());
131 d->node->ref();
132 int index = -1;
133 if (auto editingRoot = viewer->getEditingRoot()) {
134 index = d->node->findChild(editingRoot);
135 }
136 d->node->insertChild(d->clipX, index + 1);
137 d->node->insertChild(d->clipY, index + 1);
138 d->node->insertChild(d->clipZ, index + 1);
139 d->node->insertChild(d->clipView, index + 1);
140
141 SoGetBoundingBoxAction action(viewer->getSoRenderManager()->getViewportRegion());
142 action.apply(viewer->getSceneGraph());
143 SbBox3f box = action.getBoundingBox();
144
145 if (!box.isEmpty()) {
146 SbVec3f cnt = box.getCenter();
147 d->ui.clipView->setValue(cnt[2]);
148 d->ui.clipX->setValue(cnt[0]);
149 d->ui.clipY->setValue(cnt[1]);
150 d->ui.clipZ->setValue(cnt[2]);
151
152 int minDecimals = 2;
153 float lenx, leny, lenz;
154 box.getSize(lenx, leny, lenz);
155 int steps = 100;
156 float minlen = std::min<float>(lenx, std::min<float>(leny, lenz));
157
158 // determine the single step values
159 {
160 minlen = minlen / steps;
161 int dim = static_cast<int>(log10(minlen));
162 double singleStep = pow(10.0, dim);
163 d->ui.clipView->setSingleStep(singleStep);
164 minDecimals = std::max(minDecimals, -dim);
165 }
166 {
167 lenx = lenx / steps;
168 int dim = static_cast<int>(log10(lenx));
169 double singleStep = pow(10.0, dim);
170 d->ui.clipX->setSingleStep(singleStep);
171 }
172 {
173 leny = leny / steps;
174 int dim = static_cast<int>(log10(leny));
175 double singleStep = pow(10.0, dim);
176 d->ui.clipY->setSingleStep(singleStep);
177 }
178 {
179 lenz = lenz / steps;
180 int dim = static_cast<int>(log10(lenz));
181 double singleStep = pow(10.0, dim);
182 d->ui.clipZ->setSingleStep(singleStep);
183 }
184
185 // set decimals
186 d->ui.clipView->setDecimals(minDecimals);
187 d->ui.clipX->setDecimals(minDecimals);
188 d->ui.clipY->setDecimals(minDecimals);
189 d->ui.clipZ->setDecimals(minDecimals);
190 }
191 }
192
193 Clipping* Clipping::makeDockWidget(Gui::View3DInventor* view)
194 {
195 // embed this dialog into a QDockWidget
196 auto clipping = new Clipping(view);
197 Gui::DockWindowManager* pDockMgr = Gui::DockWindowManager::instance();
198 QDockWidget* dw = pDockMgr->addDockWindow("Clipping", clipping, Qt::LeftDockWidgetArea);
199 dw->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
200 dw->show();
201
202 return clipping;
203 }
204
205 /** Destroys the object and frees any allocated resources */
206 Clipping::~Clipping()
207 {
208 d->node->removeChild(d->clipX);
209 d->node->removeChild(d->clipY);
210 d->node->removeChild(d->clipZ);
211 d->node->removeChild(d->clipView);
212 d->node->unref();
213 delete d;
214 }
215
216 void Clipping::setupConnections()
217 {
218 // clang-format off
219 connect(d->ui.groupBoxX, &QGroupBox::toggled,
220 this, &Clipping::onGroupBoxXToggled);
221 connect(d->ui.groupBoxY, &QGroupBox::toggled,
222 this, &Clipping::onGroupBoxYToggled);
223 connect(d->ui.groupBoxZ, &QGroupBox::toggled,
224 this, &Clipping::onGroupBoxZToggled);
225 connect(d->ui.clipX, qOverload<double>(&QDoubleSpinBox::valueChanged),
226 this, &Clipping::onClipXValueChanged);
227 connect(d->ui.clipY, qOverload<double>(&QDoubleSpinBox::valueChanged),
228 this, &Clipping::onClipYValueChanged);
229 connect(d->ui.clipZ, qOverload<double>(&QDoubleSpinBox::valueChanged),
230 this, &Clipping::onClipZValueChanged);
231 connect(d->ui.flipClipX, &QPushButton::clicked,
232 this, &Clipping::onFlipClipXClicked);
233 connect(d->ui.flipClipY, &QPushButton::clicked,
234 this, &Clipping::onFlipClipYClicked);
235 connect(d->ui.flipClipZ, &QPushButton::clicked,
236 this, &Clipping::onFlipClipZClicked);
237 connect(d->ui.groupBoxView, &QGroupBox::toggled,
238 this, &Clipping::onGroupBoxViewToggled);
239 connect(d->ui.clipView, qOverload<double>(&QDoubleSpinBox::valueChanged),
240 this, &Clipping::onClipViewValueChanged);
241 connect(d->ui.fromView, &QPushButton::clicked,
242 this, &Clipping::onFromViewClicked);
243 connect(d->ui.adjustViewdirection, &QCheckBox::toggled,
244 this, &Clipping::onAdjustViewdirectionToggled);
245 connect(d->ui.dirX, qOverload<double>(&QDoubleSpinBox::valueChanged),
246 this, &Clipping::onDirXValueChanged);
247 connect(d->ui.dirY, qOverload<double>(&QDoubleSpinBox::valueChanged),
248 this, &Clipping::onDirYValueChanged);
249 connect(d->ui.dirZ, qOverload<double>(&QDoubleSpinBox::valueChanged),
250 this, &Clipping::onDirZValueChanged);
251 // clang-format on
252 }
253
254 void Clipping::reject()
255 {
256 QDialog::reject();
257 auto dw = qobject_cast<QDockWidget*>(parent());
258 if (dw) {
259 dw->deleteLater();
260 }
261 }
262
263 void Clipping::onGroupBoxXToggled(bool on)
264 {
265 if (on) {
266 d->ui.groupBoxView->setChecked(false);
267 }
268
269 d->clipX->on.setValue(on);
270 }
271
272 void Clipping::onGroupBoxYToggled(bool on)
273 {
274 if (on) {
275 d->ui.groupBoxView->setChecked(false);
276 }
277
278 d->clipY->on.setValue(on);
279 }
280
281 void Clipping::onGroupBoxZToggled(bool on)
282 {
283 if (on) {
284 d->ui.groupBoxView->setChecked(false);
285 }
286
287 d->clipZ->on.setValue(on);
288 }
289
290 void Clipping::onClipXValueChanged(double val)
291 {
292 SbPlane pln = d->clipX->plane.getValue();
293 d->clipX->plane.setValue(SbPlane(pln.getNormal(), d->flipX ? -val : val));
294 }
295
296 void Clipping::onClipYValueChanged(double val)
297 {
298 SbPlane pln = d->clipY->plane.getValue();
299 d->clipY->plane.setValue(SbPlane(pln.getNormal(), d->flipY ? -val : val));
300 }
301
302 void Clipping::onClipZValueChanged(double val)
303 {
304 SbPlane pln = d->clipZ->plane.getValue();
305 d->clipZ->plane.setValue(SbPlane(pln.getNormal(), d->flipZ ? -val : val));
306 }
307
308 void Clipping::onFlipClipXClicked()
309 {
310 d->flipX = !d->flipX;
311 SbPlane pln = d->clipX->plane.getValue();
312 d->clipX->plane.setValue(SbPlane(-pln.getNormal(), -pln.getDistanceFromOrigin()));
313 }
314
315 void Clipping::onFlipClipYClicked()
316 {
317 d->flipY = !d->flipY;
318 SbPlane pln = d->clipY->plane.getValue();
319 d->clipY->plane.setValue(SbPlane(-pln.getNormal(), -pln.getDistanceFromOrigin()));
320 }
321
322 void Clipping::onFlipClipZClicked()
323 {
324 d->flipZ = !d->flipZ;
325 SbPlane pln = d->clipZ->plane.getValue();
326 d->clipZ->plane.setValue(SbPlane(-pln.getNormal(), -pln.getDistanceFromOrigin()));
327 }
328
329 void Clipping::onGroupBoxViewToggled(bool on)
330 {
331 if (on) {
332 d->ui.groupBoxX->setChecked(false);
333 d->ui.groupBoxY->setChecked(false);
334 d->ui.groupBoxZ->setChecked(false);
335 }
336
337 d->clipView->on.setValue(on);
338 }
339
340 void Clipping::onClipViewValueChanged(double val)
341 {
342 SbPlane pln = d->clipView->plane.getValue();
343 d->clipView->plane.setValue(SbPlane(pln.getNormal(), val));
344 }
345
346 void Clipping::onFromViewClicked()
347 {
348 if (d->view) {
349 Gui::View3DInventorViewer* view = d->view->getViewer();
350 SbVec3f dir = view->getViewDirection();
351 SbPlane pln = d->clipView->plane.getValue();
352 d->clipView->plane.setValue(SbPlane(dir, pln.getDistanceFromOrigin()));
353 }
354 }
355
356 void Clipping::onAdjustViewdirectionToggled(bool on)
357 {
358 d->ui.dirX->setDisabled(on);
359 d->ui.dirY->setDisabled(on);
360 d->ui.dirZ->setDisabled(on);
361 d->ui.fromView->setDisabled(on);
362
363 if (on) {
364 d->sensor->schedule();
365 }
366 else {
367 d->sensor->unschedule();
368 }
369 }
370
371 void Clipping::onDirXValueChanged(double)
372 {
373 double x = d->ui.dirX->value();
374 double y = d->ui.dirY->value();
375 double z = d->ui.dirZ->value();
376
377 SbPlane pln = d->clipView->plane.getValue();
378 SbVec3f normal(x, y, z);
379 if (normal.sqrLength() > 0.0f) {
380 d->clipView->plane.setValue(SbPlane(normal, pln.getDistanceFromOrigin()));
381 }
382 }
383
384 void Clipping::onDirYValueChanged(double)
385 {
386 double x = d->ui.dirX->value();
387 double y = d->ui.dirY->value();
388 double z = d->ui.dirZ->value();
389
390 SbPlane pln = d->clipView->plane.getValue();
391 SbVec3f normal(x, y, z);
392 if (normal.sqrLength() > 0.0f) {
393 d->clipView->plane.setValue(SbPlane(normal, pln.getDistanceFromOrigin()));
394 }
395 }
396
397 void Clipping::onDirZValueChanged(double)
398 {
399 double x = d->ui.dirX->value();
400 double y = d->ui.dirY->value();
401 double z = d->ui.dirZ->value();
402
403 SbPlane pln = d->clipView->plane.getValue();
404 SbVec3f normal(x, y, z);
405 if (normal.sqrLength() > 0.0f) {
406 d->clipView->plane.setValue(SbPlane(normal, pln.getDistanceFromOrigin()));
407 }
408 }
409
410 #include "moc_Clipping.cpp"