Debian Codegraph @

src/Gui/TaskView/TaskOrientation.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2023 Werner Mayer <wmayer[at]users.sourceforge.net> *
5 * *
6 * This file is part of FreeCAD. *
7 * *
8 * FreeCAD is free software: you can redistribute it and/or modify it *
9 * under the terms of the GNU Lesser General Public License as *
10 * published by the Free Software Foundation, either version 2.1 of the *
11 * License, or (at your option) any later version. *
12 * *
13 * FreeCAD is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with FreeCAD. If not, see *
20 * <https://www.gnu.org/licenses/>. *
21 * *
22 **************************************************************************/
23
24 #include <QDialog>
25 #include <map>
26
27 #include <Base/Tools.h>
28 #include <App/Document.h>
29 #include <Gui/BitmapFactory.h>
30 #include <Gui/Camera.h>
31 #include <Gui/TaskView/TaskView.h>
32
33 #include "TaskOrientation.h"
34 #include "ui_TaskOrientation.h"
35
36
37 using namespace Gui;
38
39 TaskOrientation::TaskOrientation(App::GeoFeature* obj, QWidget* parent)
40 : QWidget(parent)
41 , ui(new Ui_TaskOrientation)
42 , feature(obj)
43 {
44 ui->setupUi(this);
45
46 // clang-format off
47 connect(ui->Reverse_checkBox, &QCheckBox::clicked,
48 this, &TaskOrientation::onPreview);
49 connect(ui->XY_radioButton , &QRadioButton::clicked,
50 this, &TaskOrientation::onPreview);
51 connect(ui->XZ_radioButton , &QRadioButton::clicked,
52 this, &TaskOrientation::onPreview);
53 connect(ui->YZ_radioButton , &QRadioButton::clicked,
54 this, &TaskOrientation::onPreview);
55 connect(ui->Offset_doubleSpinBox, qOverload<double>(&QuantitySpinBox::valueChanged),
56 this, &TaskOrientation::onPreview);
57 // clang-format on
58 }
59
60 TaskOrientation::~TaskOrientation() = default;
61
62 void TaskOrientation::open()
63 {
64 if (!feature.expired()) {
65 App::Document* doc = feature->getDocument();
66 doc->openTransaction(QT_TRANSLATE_NOOP("Command", "Edit image"));
67 restore(feature->Placement.getValue());
68 }
69 }
70
71 void TaskOrientation::accept()
72 {
73 if (!feature.expired()) {
74 App::Document* doc = feature->getDocument();
75 doc->commitTransaction();
76 doc->recompute();
77 }
78 }
79
80 void TaskOrientation::reject()
81 {
82 if (!feature.expired()) {
83 App::Document* doc = feature->getDocument();
84 doc->abortTransaction();
85 feature->purgeTouched();
86 }
87 }
88
89 void TaskOrientation::onPreview()
90 {
91 updateIcon();
92 updatePlacement();
93 }
94
95 void TaskOrientation::restore(const Base::Placement& plm)
96 {
97 auto isReversed = [](Camera::Orientation type) {
98 return (type == Camera::Bottom) || (type == Camera::Rear) || (type == Camera::Left);
99 };
100 std::map<Camera::Orientation, Base::Rotation> rotations {
101 {Camera::Top, Camera::convert(Camera::Top)},
102 {Camera::Bottom, Camera::convert(Camera::Bottom)},
103 {Camera::Front, Camera::convert(Camera::Front)},
104 {Camera::Rear, Camera::convert(Camera::Rear)},
105 {Camera::Right, Camera::convert(Camera::Right)},
106 {Camera::Left, Camera::convert(Camera::Left)}
107 };
108
109 Base::Rotation rot = plm.getRotation();
110 Base::Vector3d pos = plm.getPosition();
111
112 double prec = 1.0e-5;
113 for (const auto& it : rotations) {
114 if (rot.isSame(it.second, prec)) {
115 if (it.first == Camera::Top || it.first == Camera::Bottom) {
116 ui->XY_radioButton->setChecked(true);
117 ui->Offset_doubleSpinBox->setValue(pos.z);
118 }
119 else if (it.first == Camera::Front || it.first == Camera::Rear) {
120 ui->XZ_radioButton->setChecked(true);
121 ui->Offset_doubleSpinBox->setValue(pos.y);
122 }
123 else if (it.first == Camera::Right || it.first == Camera::Left) {
124 ui->YZ_radioButton->setChecked(true);
125 ui->Offset_doubleSpinBox->setValue(pos.x);
126 }
127
128 if (isReversed(it.first)) {
129 ui->Reverse_checkBox->setChecked(true);
130 }
131 }
132 }
133
134 onPreview();
135 }
136
137 void TaskOrientation::updatePlacement()
138 {
139 Base::Placement Pos;
140 double offset = ui->Offset_doubleSpinBox->value().getValue();
141 bool reverse = ui->Reverse_checkBox->isChecked();
142 if (ui->XY_radioButton->isChecked()) {
143 if (!reverse) {
144 Pos = Base::Placement(Base::Vector3d(0, 0, offset), Camera::convert(Camera::Top));
145 }
146 else {
147 Pos = Base::Placement(Base::Vector3d(0, 0, offset), Camera::convert(Camera::Bottom));
148 }
149 }
150 else if (ui->XZ_radioButton->isChecked()) {
151 if (!reverse) {
152 Pos = Base::Placement(Base::Vector3d(0, offset, 0), Camera::convert(Camera::Front));
153 }
154 else {
155 Pos = Base::Placement(Base::Vector3d(0, offset, 0), Camera::convert(Camera::Rear));
156 }
157 }
158 else if (ui->YZ_radioButton->isChecked()) {
159 if (!reverse) {
160 Pos = Base::Placement(Base::Vector3d(offset, 0, 0), Camera::convert(Camera::Right));
161 }
162 else {
163 Pos = Base::Placement(Base::Vector3d(offset, 0, 0), Camera::convert(Camera::Left));
164 }
165 }
166
167 if (!feature.expired()) {
168 feature->Placement.setValue(Pos);
169 }
170 }
171
172 void TaskOrientation::updateIcon()
173 {
174 std::string icon;
175 bool reverse = ui->Reverse_checkBox->isChecked();
176 if (ui->XY_radioButton->isChecked()) {
177 icon = reverse ? "view-bottom" : "view-top";
178 }
179 else if (ui->XZ_radioButton->isChecked()) {
180 icon = reverse ? "view-rear" : "view-front";
181 }
182 else if (ui->YZ_radioButton->isChecked()) {
183 icon = reverse ? "view-left" : "view-right";
184 }
185
186 ui->previewLabel->setPixmap(
187 Gui::BitmapFactory().pixmapFromSvg(icon.c_str(), ui->previewLabel->size())
188 );
189 }
190
191 // ----------------------------------------------------------------------------
192
193 TaskOrientationDialog::TaskOrientationDialog(App::GeoFeature* obj)
194 {
195 widget = new TaskOrientation(obj);
196 addTaskBox(widget);
197 }
198
199 void TaskOrientationDialog::open()
200 {
201 widget->open();
202 }
203
204 bool TaskOrientationDialog::accept()
205 {
206 widget->accept();
207 return true;
208 }
209
210 bool TaskOrientationDialog::reject()
211 {
212 widget->reject();
213 return true;
214 }
215
216 #include "moc_TaskOrientation.cpp"