Debian Codegraph @

src/Gui/Transform.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2009 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
24 #include <QDialogButtonBox>
25 #include <QKeyEvent>
26 #include <QSignalMapper>
27
28 #include <Base/Tools.h>
29 #include <App/GeoFeature.h>
30 #include <App/PropertyGeo.h>
31
32 #include "Transform.h"
33 #include "Application.h"
34 #include "Document.h"
35 #include "Selection.h"
36 #include "ui_Placement.h"
37 #include "ViewProvider.h"
38 #include "WaitCursor.h"
39
40
41 using namespace Gui::Dialog;
42
43 namespace Gui
44 {
45 namespace Dialog
46 {
47 class find_transform
48 {
49 public:
50 bool operator()(const std::pair<std::string, App::Property*>& elem) const
51 {
52 if (elem.first == "Placement") {
53 return elem.second->isDerivedFrom(Base::Type::fromName("App::PropertyPlacement"));
54 }
55
56 return false;
57 }
58 };
59 } // namespace Dialog
60 } // namespace Gui
61
62 // ----------------------------------------------------------------------------
63
64 TransformStrategy::TransformStrategy() = default;
65
66 TransformStrategy::~TransformStrategy() = default;
67
68 Base::Vector3d TransformStrategy::getRotationCenter() const
69 {
70 // get the global bounding box of all selected objects and use its center as
71 // rotation center
72 std::set<App::DocumentObject*> objects = transformObjects();
73 if (!objects.empty()) {
74 Base::BoundBox3d bbox;
75 bool first = true;
76 for (const auto& object : objects) {
77 if (object->isDerivedFrom<App::GeoFeature>()) {
78 // search for a data property
79 const App::PropertyGeometry* geo
80 = static_cast<App::GeoFeature*>(object)->getPropertyOfGeometry();
81 if (geo) {
82 if (first) {
83 bbox = geo->getBoundingBox();
84 }
85 else {
86 bbox.Add(geo->getBoundingBox());
87 }
88 first = false;
89 }
90 }
91 }
92
93 return Base::Vector3d(
94 (bbox.MinX + bbox.MaxX) / 2,
95 (bbox.MinY + bbox.MaxY) / 2,
96 (bbox.MinZ + bbox.MaxZ) / 2
97 );
98 }
99
100 return Base::Vector3d();
101 }
102
103 void TransformStrategy::commitTransform(const Base::Matrix4D& mat)
104 {
105 std::set<App::DocumentObject*> objects = transformObjects();
106 Gui::Document* doc = Gui::Application::Instance->activeDocument();
107 if (doc) {
108 doc->openCommand(QT_TRANSLATE_NOOP("Command", "Transform"));
109 for (const auto& object : objects) {
110 acceptDataTransform(mat, object);
111 }
112 doc->commitCommand();
113 }
114 }
115
116 void TransformStrategy::acceptDataTransform(const Base::Matrix4D& mat, App::DocumentObject* obj)
117 {
118 Gui::Document* doc = Gui::Application::Instance->getDocument(obj->getDocument());
119 std::map<std::string, App::Property*> props;
120 obj->getPropertyMap(props);
121 // search for the placement property
122 std::map<std::string, App::Property*>::iterator jt;
123 jt = std::find_if(props.begin(), props.end(), find_transform());
124 if (jt != props.end()) {
125 Base::Placement local = static_cast<App::PropertyPlacement*>(jt->second)->getValue();
126 Gui::ViewProvider* vp = doc->getViewProvider(obj);
127 if (vp) {
128 vp->setTransformation(local.toMatrix());
129 }
130 }
131 else {
132 // No placement found
133 Gui::ViewProvider* vp = doc->getViewProvider(obj);
134 if (vp) {
135 vp->setTransformation(Base::Matrix4D());
136 }
137 }
138
139 // Apply the transformation
140 if (obj->isDerivedFrom<App::GeoFeature>()) {
141 // search for a data property
142 const App::PropertyGeometry* geo = static_cast<App::GeoFeature*>(obj)->getPropertyOfGeometry();
143 if (geo) {
144 const_cast<App::PropertyGeometry*>(geo)->transformGeometry(mat);
145 }
146 }
147 }
148
149 void TransformStrategy::applyTransform(const Base::Placement& plm)
150 {
151 std::set<App::DocumentObject*> objects = transformObjects();
152 for (const auto& object : objects) {
153 applyViewTransform(plm, object);
154 }
155 }
156
157 void TransformStrategy::resetTransform()
158 {
159 std::set<App::DocumentObject*> objects = transformObjects();
160 for (const auto& object : objects) {
161 resetViewTransform(object);
162 }
163 }
164
165 void TransformStrategy::applyViewTransform(const Base::Placement& plm, App::DocumentObject* obj)
166 {
167 Gui::Document* doc = Gui::Application::Instance->getDocument(obj->getDocument());
168 std::map<std::string, App::Property*> props;
169 obj->getPropertyMap(props);
170 // search for the placement property
171 std::map<std::string, App::Property*>::iterator jt;
172 jt = std::find_if(props.begin(), props.end(), find_transform());
173 if (jt != props.end()) {
174 Base::Placement local = static_cast<App::PropertyPlacement*>(jt->second)->getValue();
175 local *= plm; // in case a placement is already set
176 Gui::ViewProvider* vp = doc->getViewProvider(obj);
177 if (vp) {
178 vp->setTransformation(local.toMatrix());
179 }
180 }
181 else {
182 // No placement found, so apply the transformation directly
183 Gui::ViewProvider* vp = doc->getViewProvider(obj);
184 if (vp) {
185 vp->setTransformation(plm.toMatrix());
186 }
187 }
188 }
189
190 void TransformStrategy::resetViewTransform(App::DocumentObject* obj)
191 {
192 Gui::Document* doc = Gui::Application::Instance->getDocument(obj->getDocument());
193 std::map<std::string, App::Property*> props;
194 obj->getPropertyMap(props);
195 // search for the placement property
196 std::map<std::string, App::Property*>::iterator jt;
197 jt = std::find_if(props.begin(), props.end(), find_transform());
198 if (jt != props.end()) {
199 Base::Placement local = static_cast<App::PropertyPlacement*>(jt->second)->getValue();
200 Gui::ViewProvider* vp = doc->getViewProvider(obj);
201 if (vp) {
202 vp->setTransformation(local.toMatrix());
203 }
204 }
205 else {
206 // No placement found
207 Gui::ViewProvider* vp = doc->getViewProvider(obj);
208 if (vp) {
209 vp->setTransformation(Base::Matrix4D());
210 }
211 }
212 }
213
214 // ----------------------------------------------------------------------------
215
216 DefaultTransformStrategy::DefaultTransformStrategy(QWidget* w)
217 : widget(w)
218 {
219 Gui::SelectionChanges mod;
220 mod.Type = Gui::SelectionChanges::SetSelection;
221 onSelectionChanged(mod);
222 }
223
224 DefaultTransformStrategy::~DefaultTransformStrategy() = default;
225
226 std::set<App::DocumentObject*> DefaultTransformStrategy::transformObjects() const
227 {
228 return selection;
229 }
230
231 void DefaultTransformStrategy::onSelectionChanged(const Gui::SelectionChanges& msg)
232 {
233 if (msg.Type == SelectionChanges::SetPreselect || msg.Type == SelectionChanges::RmvPreselect) {
234 return; // nothing to do
235 }
236 if (msg.Type == SelectionChanges::ClrSelection) {
237 widget->setDisabled(true);
238 for (const auto& it : selection) {
239 resetViewTransform(it);
240 }
241 selection.clear();
242 return;
243 }
244
245 std::set<App::DocumentObject*> update_selection;
246 std::vector<App::DocumentObject*> sel = Gui::Selection().getObjectsOfType(
247 App::DocumentObject::getClassTypeId()
248 );
249 for (const auto& it : sel) {
250 if (it->isDerivedFrom<App::GeoFeature>()) {
251 // search for a data property
252 const App::PropertyGeometry* geo
253 = static_cast<App::GeoFeature*>(it)->getPropertyOfGeometry();
254 if (geo) {
255 update_selection.insert(it);
256 }
257 }
258 }
259
260 // now we remove all objects which links to another object
261 // of the selected objects because if the source object changes
262 // it is touched and thus a recompute later would overwrite the
263 // changes here anyway
264 std::set<App::DocumentObject*> filter;
265 for (auto it = update_selection.begin(); it != update_selection.end(); ++it) {
266 std::vector<App::DocumentObject*> deps = (*it)->getOutList();
267 std::vector<App::DocumentObject*>::iterator jt;
268 for (jt = deps.begin(); jt != deps.end(); ++jt) {
269 if (update_selection.find(*jt) != update_selection.end()) {
270 filter.insert(*it);
271 break;
272 }
273 }
274 }
275
276 if (!filter.empty()) {
277 std::set<App::DocumentObject*> diff;
278 std::insert_iterator<std::set<App::DocumentObject*>> biit(diff, diff.begin());
279 std::set_difference(
280 update_selection.begin(),
281 update_selection.end(),
282 filter.begin(),
283 filter.end(),
284 biit
285 );
286 update_selection = diff;
287 }
288
289 // reset transform for all deselected objects
290 std::vector<App::DocumentObject*> diff;
291 std::back_insert_iterator<std::vector<App::DocumentObject*>> biit(diff);
292 std::set_difference(
293 selection.begin(),
294 selection.end(),
295 update_selection.begin(),
296 update_selection.end(),
297 biit
298 );
299 for (const auto& it : diff) {
300 resetViewTransform(it);
301 }
302 selection = update_selection;
303
304 widget->setDisabled(selection.empty());
305 }
306
307 // ----------------------------------------------------------------------------
308
309 /* TRANSLATOR Gui::Dialog::Transform */
310
311 Transform::Transform(QWidget* parent, Qt::WindowFlags fl)
312 : QDialog(parent, fl)
313 , strategy(nullptr)
314 {
315 ui = new Ui_Placement();
316 ui->setupUi(this);
317 QPushButton* applyButton = ui->buttonBox->button(QDialogButtonBox::Apply);
318 connect(applyButton, &QPushButton::clicked, this, &Transform::onApplyButtonClicked);
319
320 ui->resetButton->hide();
321 ui->applyIncrementalPlacement->hide();
322 this->setWindowTitle(tr("Transform"));
323
324 // create a signal mapper in order to have one slot to perform the change
325 auto signalMapper = new QSignalMapper(this);
326 signalMapper->setMapping(this, 0);
327
328 int id = 1;
329 QList<Gui::QuantitySpinBox*> sb = this->findChildren<Gui::QuantitySpinBox*>();
330 for (const auto& it : sb) {
331 connect(
332 it,
333 qOverload<double>(&QuantitySpinBox::valueChanged),
334 signalMapper,
335 qOverload<>(&QSignalMapper::map)
336 );
337 signalMapper->setMapping(it, id++);
338 }
339
340 connect(signalMapper, &QSignalMapper::mappedInt, this, &Transform::onTransformChanged);
341
342 setTransformStrategy(new DefaultTransformStrategy(this));
343 }
344
345 Transform::~Transform()
346 {
347 delete ui;
348 delete strategy;
349 }
350
351 void Transform::setTransformStrategy(TransformStrategy* ts)
352 {
353 if (!ts || ts == strategy) {
354 return;
355 }
356 if (strategy) {
357 delete strategy;
358 }
359 strategy = ts;
360 Base::Vector3d cnt = strategy->getRotationCenter();
361 ui->xCnt->setValue(Base::Quantity(cnt.x, Base::Unit::Length));
362 ui->yCnt->setValue(Base::Quantity(cnt.y, Base::Unit::Length));
363 ui->zCnt->setValue(Base::Quantity(cnt.z, Base::Unit::Length));
364 this->setDisabled(strategy->transformObjects().empty());
365 }
366
367 void Transform::showStandardButtons(bool b)
368 {
369 ui->buttonBox->setVisible(b);
370 }
371
372 void Transform::onTransformChanged(int)
373 {
374 Base::Placement plm = this->getPlacementData();
375 strategy->applyTransform(plm);
376 }
377
378 void Transform::accept()
379 {
380 onApplyButtonClicked();
381 QDialog::accept();
382 }
383
384 void Transform::reject()
385 {
386 strategy->resetTransform();
387 QDialog::reject();
388 }
389
390 void Transform::onApplyButtonClicked()
391 {
392 Gui::WaitCursor wc;
393 Base::Placement plm = this->getPlacementData();
394 Base::Matrix4D mat = plm.toMatrix();
395 strategy->commitTransform(mat);
396
397 // nullify the values
398 QList<Gui::QuantitySpinBox*> sb = this->findChildren<Gui::QuantitySpinBox*>();
399 for (auto& it : sb) {
400 it->blockSignals(true);
401 it->setValue(0.0);
402 it->blockSignals(false);
403 }
404
405 Base::Vector3d cnt = strategy->getRotationCenter();
406 ui->xCnt->setValue(Base::Quantity(cnt.x, Base::Unit::Length));
407 ui->yCnt->setValue(Base::Quantity(cnt.y, Base::Unit::Length));
408 ui->zCnt->setValue(Base::Quantity(cnt.z, Base::Unit::Length));
409 }
410
411 Base::Vector3d Transform::getDirection() const
412 {
413 double x = ui->xAxis->value().getValue();
414 double y = ui->yAxis->value().getValue();
415 double z = ui->zAxis->value().getValue();
416 return Base::Vector3d(x, y, z);
417 }
418
419 Base::Placement Transform::getPlacementData() const
420 {
421 int index = ui->rotationInput->currentIndex();
422 Base::Rotation rot;
423 Base::Vector3d pos;
424 Base::Vector3d cnt;
425
426 pos = Base::Vector3d(
427 ui->xPos->value().getValue(),
428 ui->yPos->value().getValue(),
429 ui->zPos->value().getValue()
430 );
431 cnt = Base::Vector3d(
432 ui->xCnt->value().getValue(),
433 ui->yCnt->value().getValue(),
434 ui->zCnt->value().getValue()
435 );
436
437 if (index == 0) {
438 Base::Vector3d dir = getDirection();
439 rot.setValue(
440 Base::Vector3d(dir.x, dir.y, dir.z),
441 Base::toRadians(ui->angle->value().getValue())
442 );
443 }
444 else if (index == 1) {
445 rot.setYawPitchRoll(
446 ui->yawAngle->value().getValue(),
447 ui->pitchAngle->value().getValue(),
448 ui->rollAngle->value().getValue()
449 );
450 }
451
452 Base::Placement p(pos, rot, cnt);
453 return p;
454 }
455
456 void Transform::changeEvent(QEvent* e)
457 {
458 if (e->type() == QEvent::LanguageChange) {
459 ui->retranslateUi(this);
460 this->setWindowTitle(tr("Transform"));
461 }
462 else {
463 QDialog::changeEvent(e);
464 }
465 }
466
467 // ---------------------------------------
468
469 TaskTransform::TaskTransform()
470 {
471 this->setButtonPosition(TaskTransform::South);
472 dialog = new Transform();
473 dialog->showStandardButtons(false);
474 addTaskBox(dialog);
475 }
476
477 TaskTransform::~TaskTransform() = default;
478
479 void TaskTransform::setTransformStrategy(TransformStrategy* ts)
480 {
481 dialog->setTransformStrategy(ts);
482 }
483
484 bool TaskTransform::accept()
485 {
486 dialog->accept();
487 return (dialog->result() == QDialog::Accepted);
488 }
489
490 bool TaskTransform::reject()
491 {
492 dialog->reject();
493 return (dialog->result() == QDialog::Rejected);
494 }
495
496 void TaskTransform::clicked(int id)
497 {
498 if (id == QDialogButtonBox::Apply) {
499 dialog->onApplyButtonClicked();
500 }
501 }
502
503 #include "moc_Transform.cpp"