Debian Codegraph @

src/Gui/InputVector.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2008 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 <QComboBox>
25
26
27 #include <Base/UnitsApi.h>
28
29 #include "InputVector.h"
30 #include "ui_InputVector.h"
31 #include "QuantitySpinBox.h"
32
33
34 using namespace Gui;
35
36 LocationWidget::LocationWidget(QWidget* parent)
37 : QWidget(parent)
38 {
39 box = new QGridLayout();
40
41 xValue = new QuantitySpinBox(this);
42 xValue->setMinimum(-2.14748e+09);
43 xValue->setMaximum(2.14748e+09);
44 xLabel = new QLabel(this);
45 box->addWidget(xLabel, 0, 0, 1, 1);
46 box->addWidget(xValue, 0, 1, 1, 1);
47
48 yValue = new QuantitySpinBox(this);
49 yValue->setMinimum(-2.14748e+09);
50 yValue->setMaximum(2.14748e+09);
51 yLabel = new QLabel(this);
52 box->addWidget(yLabel, 1, 0, 1, 1);
53 box->addWidget(yValue, 1, 1, 1, 1);
54
55 zValue = new QuantitySpinBox(this);
56 zValue->setMinimum(-2.14748e+09);
57 zValue->setMaximum(2.14748e+09);
58 zLabel = new QLabel(this);
59 box->addWidget(zLabel, 2, 0, 1, 1);
60 box->addWidget(zValue, 2, 1, 1, 1);
61
62 dLabel = new QLabel(this);
63 dValue = new QComboBox(this);
64 dValue->setCurrentIndex(-1);
65 box->addWidget(dLabel, 3, 0, 1, 1);
66 box->addWidget(dValue, 3, 1, 1, 1);
67
68 xValue->setUnit(Base::Unit::Length);
69 yValue->setUnit(Base::Unit::Length);
70 zValue->setUnit(Base::Unit::Length);
71
72 auto gridLayout = new QGridLayout(this);
73 gridLayout->addLayout(box, 0, 0, 1, 2);
74
75 connect(dValue, qOverload<int>(&QComboBox::activated), this, &LocationWidget::onDirectionActivated);
76 retranslateUi();
77 }
78
79 LocationWidget::~LocationWidget() = default;
80
81 QSize LocationWidget::sizeHint() const
82 {
83 return {150, 100};
84 }
85
86 void LocationWidget::changeEvent(QEvent* e)
87 {
88 if (e->type() == QEvent::LanguageChange) {
89 this->retranslateUi();
90 }
91 QWidget::changeEvent(e);
92 }
93
94 void LocationWidget::retranslateUi()
95 {
96 xLabel->setText(QApplication::translate("Gui::LocationWidget", "X:"));
97 yLabel->setText(QApplication::translate("Gui::LocationWidget", "Y:"));
98 zLabel->setText(QApplication::translate("Gui::LocationWidget", "Z:"));
99 dLabel->setText(QApplication::translate("Gui::LocationWidget", "Direction:"));
100
101 if (dValue->count() == 0) {
102 dValue->insertItems(
103 0,
104 QStringList() << QApplication::translate("Gui::LocationDialog", "X")
105 << QApplication::translate("Gui::LocationDialog", "Y")
106 << QApplication::translate("Gui::LocationDialog", "Z")
107 << QApplication::translate("Gui::LocationDialog", "User defined…")
108 );
109
110 dValue->setCurrentIndex(2);
111
112 // Vector3d declared to use with QVariant see Gui/propertyeditor/PropertyItem.h
113 dValue->setItemData(0, QVariant::fromValue<Base::Vector3d>(Base::Vector3d(1, 0, 0)));
114 dValue->setItemData(1, QVariant::fromValue<Base::Vector3d>(Base::Vector3d(0, 1, 0)));
115 dValue->setItemData(2, QVariant::fromValue<Base::Vector3d>(Base::Vector3d(0, 0, 1)));
116 }
117 else {
118 dValue->setItemText(0, QApplication::translate("Gui::LocationDialog", "X"));
119 dValue->setItemText(1, QApplication::translate("Gui::LocationDialog", "Y"));
120 dValue->setItemText(2, QApplication::translate("Gui::LocationDialog", "Z"));
121 dValue->setItemText(
122 dValue->count() - 1,
123 QApplication::translate("Gui::LocationDialog", "User defined…")
124 );
125 }
126 }
127
128 Base::Vector3d LocationWidget::getPosition() const
129 {
130 return Base::Vector3d(
131 this->xValue->value().getValue(),
132 this->yValue->value().getValue(),
133 this->zValue->value().getValue()
134 );
135 }
136
137 void LocationWidget::setPosition(const Base::Vector3d& v)
138 {
139 this->xValue->setValue(v.x);
140 this->yValue->setValue(v.y);
141 this->zValue->setValue(v.z);
142 }
143
144 void LocationWidget::setDirection(const Base::Vector3d& dir)
145 {
146 if (dir.Length() < Base::Vector3d::epsilon()) {
147 return;
148 }
149
150 // check if the user-defined direction is already there
151 for (int i = 0; i < dValue->count() - 1; i++) {
152 QVariant data = dValue->itemData(i);
153 if (data.canConvert<Base::Vector3d>()) {
154 const auto val = data.value<Base::Vector3d>();
155 if (val == dir) {
156 dValue->setCurrentIndex(i);
157 return;
158 }
159 }
160 }
161
162 // add a new item before the very last item
163 QString display = QStringLiteral("(%1,%2,%3)").arg(dir.x).arg(dir.y).arg(dir.z);
164 dValue->insertItem(dValue->count() - 1, display, QVariant::fromValue<Base::Vector3d>(dir));
165 dValue->setCurrentIndex(dValue->count() - 2);
166 }
167
168 Base::Vector3d LocationWidget::getDirection() const
169 {
170 QVariant data = dValue->itemData(this->dValue->currentIndex());
171 if (data.canConvert<Base::Vector3d>()) {
172 return data.value<Base::Vector3d>();
173 }
174 else {
175 return Base::Vector3d(0, 0, 1);
176 }
177 }
178
179 Base::Vector3d LocationWidget::getUserDirection(bool* ok) const
180 {
181 Gui::Dialog::Ui_InputVector iv;
182 QDialog dlg(const_cast<LocationWidget*>(this));
183 iv.setupUi(&dlg);
184 iv.vectorX->setDecimals(Base::UnitsApi::getDecimals());
185 iv.vectorY->setDecimals(Base::UnitsApi::getDecimals());
186 iv.vectorZ->setDecimals(Base::UnitsApi::getDecimals());
187 Base::Vector3d dir;
188 if (dlg.exec()) {
189 dir.x = iv.vectorX->value();
190 dir.y = iv.vectorY->value();
191 dir.z = iv.vectorZ->value();
192 if (ok) {
193 *ok = true;
194 }
195 }
196 else {
197 if (ok) {
198 *ok = false;
199 }
200 }
201
202 return dir;
203 }
204
205 void LocationWidget::onDirectionActivated(int index)
206 {
207 // last item is selected to define direction by user
208 if (index + 1 == dValue->count()) {
209 bool ok;
210 Base::Vector3d dir = this->getUserDirection(&ok);
211 if (ok) {
212 if (dir.Length() < Base::Vector3d::epsilon()) {
213 QMessageBox::critical(
214 this,
215 LocationDialog::tr("Wrong direction"),
216 LocationDialog::tr("Direction must not be the null vector")
217 );
218 return;
219 }
220
221 setDirection(dir);
222 }
223 }
224 }
225
226 // ----------------------------------------------------------------------------
227
228 LocationDialog::LocationDialog(QWidget* parent, Qt::WindowFlags fl)
229 : QDialog(parent, fl)
230 {}
231
232 LocationDialog::~LocationDialog() = default;
233
234 Base::Vector3d LocationDialog::getUserDirection(bool* ok) const
235 {
236 Gui::Dialog::Ui_InputVector iv;
237 QDialog dlg(const_cast<LocationDialog*>(this));
238 iv.setupUi(&dlg);
239 iv.vectorX->setDecimals(Base::UnitsApi::getDecimals());
240 iv.vectorY->setDecimals(Base::UnitsApi::getDecimals());
241 iv.vectorZ->setDecimals(Base::UnitsApi::getDecimals());
242 Base::Vector3d dir;
243 if (dlg.exec()) {
244 dir.x = iv.vectorX->value();
245 dir.y = iv.vectorY->value();
246 dir.z = iv.vectorZ->value();
247 if (ok) {
248 *ok = true;
249 }
250 }
251 else {
252 if (ok) {
253 *ok = false;
254 }
255 }
256
257 return dir;
258 }
259
260 void LocationDialog::onDirectionActivated(int index)
261 {
262 directionActivated(index);
263 }
264
265 // -----------------------------------------------------------
266
267 LocationDialogUiImp::~LocationDialogUiImp() = default;
268
269 Base::Vector3d LocationDialogUiImp::getDirection() const
270 {
271 return ui->getDirection();
272 }
273
274 Base::Vector3d LocationDialogUiImp::getPosition() const
275 {
276 return ui->getPosition();
277 }
278
279 void LocationDialogUiImp::changeEvent(QEvent* e)
280 {
281 if (e->type() == QEvent::LanguageChange) {
282 ui->retranslate(this);
283 }
284 else {
285 QDialog::changeEvent(e);
286 }
287 }
288
289 void LocationDialogUiImp::directionActivated(int index)
290 {
291 ui->directionActivated(this, index);
292 }
293
294 #include "moc_InputVector.cpp"