1
/***************************************************************************
2
* Copyright (c) 2022 WandererFan <wandererfan@gmail.com> *
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
***************************************************************************/
23
// A widget for editing Vector3d without taking up too much space in the UI.
25
#include "PreCompiled.h"
27
#include <QApplication>
41
#include <Mod/TechDraw/TechDrawGlobal.h>
45
#include <Base/Console.h>
46
#include <Base/Tools.h>
47
#include <Base/UnitsApi.h>
49
#include <Gui/SpinBox.h>
51
#include <Mod/TechDraw/App/DrawUtil.h>
53
#include "VectorEditWidget.h"
55
using namespace TechDrawGui;
56
using namespace TechDraw;
58
VectorEditWidget::VectorEditWidget(QWidget* parent) : QWidget(parent),
61
m_expandedHeight(155),
64
m_size = QSize(m_minimumWidth, m_minimumHeight);
65
setObjectName(QString::fromUtf8("VectorEdit"));
68
connect(tbExpand, &QToolButton::toggled, this, &VectorEditWidget::slotExpandButtonToggled);
69
connect(dsbX, qOverload<double>(&Gui::DoubleSpinBox::valueChanged), this, &VectorEditWidget::slotXValueChanged);
70
connect(dsbY, qOverload<double>(&Gui::DoubleSpinBox::valueChanged), this, &VectorEditWidget::slotYValueChanged);
71
connect(dsbZ, qOverload<double>(&Gui::DoubleSpinBox::valueChanged), this, &VectorEditWidget::slotZValueChanged);
73
dsbX->installEventFilter(this);
74
dsbY->installEventFilter(this);
75
dsbZ->installEventFilter(this);
78
//trap Enter press in dsb? so as not to invoke task accept processing
79
bool VectorEditWidget::eventFilter(QObject *target, QEvent *event)
84
if (event->type() == QEvent::KeyPress) {
85
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
86
if (keyEvent->key() == Qt::Key_Return ||
87
keyEvent->key() == Qt::Key_Enter) {
88
QDoubleSpinBox* dsb = static_cast<QDoubleSpinBox*>(target);
90
Q_EMIT dsb->valueChanged(dsb->value());
95
return QWidget::eventFilter(target, event);
97
void VectorEditWidget::setLabel(std::string newLabel)
99
QString qNewLabelString = Base::Tools::fromStdString(newLabel);
100
lvectorName->setText(qNewLabelString);
103
void VectorEditWidget::setLabel(QString newLabel)
105
lvectorName->setText(newLabel);
108
void VectorEditWidget::setValue(Base::Vector3d newValue)
110
// Base::Console().Message("VEW::setValue(%.6f, %.6f, %.6f)\n", newValue.x, newValue.y, newValue.z);
112
dsbX->setValue(m_value.x);
113
dsbY->setValue(m_value.y);
114
dsbZ->setValue(m_value.z);
118
void VectorEditWidget::setValueNoNotify(Base::Vector3d newValue)
120
// Base::Console().Message("VEW::setValueNoNotify(%.6f, %.6f, %.6f)\n", newValue.x, newValue.y, newValue.z);
122
m_blockNotify = true;
123
dsbX->setValue(m_value.x);
124
dsbY->setValue(m_value.y);
125
dsbZ->setValue(m_value.z);
126
m_blockNotify = false;
130
void VectorEditWidget::slotExpandButtonToggled(bool checked)
132
// Base::Console().Message("VEW::slotExpand - checked: %d\n", checked);
134
vectorEditLayout->addLayout(VectorEditItemLayout);
135
vectorEditLayout->addItem(verticalSpacer);
136
m_size = QSize(m_minimumWidth, m_expandedHeight);
139
vectorEditLayout->removeItem(VectorEditItemLayout);
140
vectorEditLayout->removeItem(verticalSpacer);
141
m_size = QSize(m_minimumWidth, m_minimumHeight);
145
//slotXValueChanged can be triggered by the Ui or programmatically. We only want
146
//to tell the world about the change if it comes from the Ui.
147
void VectorEditWidget::slotXValueChanged(double newValue)
149
// Base::Console().Message("VEW::xValueChanged(%.6f) - m_value.x: %.6f\n", newValue, m_value.x);
150
if (!m_blockNotify) {
151
//this is a change from the dsb
152
m_value.x = newValue;
154
Q_EMIT valueChanged(m_value);
157
void VectorEditWidget::slotYValueChanged(double newValue)
159
// Base::Console().Message("VEW::yValueChanged(%.6f) - m_value.y: %.6f\n", newValue, m_value.y);
160
if (!m_blockNotify) {
161
//this is a change from the dsb
162
m_value.y = newValue;
164
Q_EMIT valueChanged(m_value);
167
void VectorEditWidget::slotZValueChanged(double newValue)
169
// Base::Console().Message("VEW::zValueChanged(%.6f)\n", newValue);
170
if (!m_blockNotify) {
171
//this is a change from the dsb
172
m_value.z = newValue;
174
Q_EMIT valueChanged(m_value);
178
void VectorEditWidget::updateDisplay()
180
// Base::Console().Message("VEW::updateDisplay() - m_value: %s\n", DrawUtil::formatVector(m_value).c_str());
181
QString qNewDisplayString = Base::Tools::fromStdString(DrawUtil::formatVector(m_value));
182
leVectorDisplay->setText(qNewDisplayString);
185
QSize VectorEditWidget::minimumSizeHint() const
190
void VectorEditWidget::buildWidget()
192
if (objectName().isEmpty())
193
setObjectName(QString::fromUtf8("VectorEdit"));
194
QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
195
setSizePolicy(sizePolicy);
197
vectorEditLayout = new QVBoxLayout(this);
198
vectorEditLayout->setObjectName(QString::fromUtf8("vectorEditLayout"));
199
vectorEditLayout->setContentsMargins(0, 0, 0, 0);
200
VectorEditButtonLayout = new QHBoxLayout();
201
VectorEditButtonLayout->setSpacing(0);
202
VectorEditButtonLayout->setObjectName(QString::fromUtf8("VectorEditButtonLayout"));
204
lvectorName = new QLabel(this);
205
lvectorName->setObjectName(QString::fromUtf8("lvectorName"));
206
VectorEditButtonLayout->addWidget(lvectorName);
208
leVectorDisplay = new QLineEdit(this);
209
leVectorDisplay->setObjectName(QString::fromUtf8("leVectorDisplay"));
210
VectorEditButtonLayout->addWidget(leVectorDisplay);
212
tbExpand = new QToolButton(this);
213
tbExpand->setObjectName(QString::fromUtf8("tbExpand"));
214
tbExpand->setText(QString::fromUtf8("..."));
215
tbExpand->setCheckable(true);
216
VectorEditButtonLayout->addWidget(tbExpand);
218
VectorEditButtonLayout->setStretch(0, 1);
219
VectorEditButtonLayout->setStretch(1, 1);
220
vectorEditLayout->addLayout(VectorEditButtonLayout);
222
VectorEditItemLayout = new QGridLayout();
223
VectorEditItemLayout->setObjectName(QString::fromUtf8("VectorEditItemLayout"));
226
lX->setObjectName(QString::fromUtf8("lX"));
227
lX->setText(QString::fromUtf8("X:"));
228
VectorEditItemLayout->addWidget(lX, 0, 0, 1, 1);
230
dsbX = new Gui::DoubleSpinBox();
231
dsbX->setObjectName(QString::fromUtf8("dsbX"));
232
dsbX->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
233
dsbX->setKeyboardTracking(false);
234
dsbX->setMaximum(std::numeric_limits<double>::max());
235
dsbX->setMinimum(std::numeric_limits<double>::lowest());
236
dsbX->setDecimals(Base::UnitsApi::getDecimals());
237
VectorEditItemLayout->addWidget(dsbX, 0, 1, 1, 1);
240
lY->setObjectName(QString::fromUtf8("lY"));
241
lY->setText(QString::fromUtf8("Y:"));
242
VectorEditItemLayout->addWidget(lY, 1, 0, 1, 1);
244
dsbY = new Gui::DoubleSpinBox();
245
dsbY->setObjectName(QString::fromUtf8("dsbY"));
246
dsbY->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
247
dsbY->setKeyboardTracking(false);
248
dsbY->setMaximum(std::numeric_limits<double>::max());
249
dsbY->setMinimum(std::numeric_limits<double>::lowest());
250
dsbY->setDecimals(Base::UnitsApi::getDecimals());
251
VectorEditItemLayout->addWidget(dsbY, 1, 1, 1, 1);
254
lZ->setObjectName(QString::fromUtf8("lZ"));
255
lZ->setText(QString::fromUtf8("Z:"));
256
VectorEditItemLayout->addWidget(lZ, 2, 0, 1, 1);
258
dsbZ = new Gui::DoubleSpinBox();
259
dsbZ->setObjectName(QString::fromUtf8("dsbZ"));
260
dsbZ->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
261
dsbZ->setKeyboardTracking(false);
262
dsbZ->setMaximum(std::numeric_limits<double>::max());
263
dsbZ->setMinimum(std::numeric_limits<double>::lowest());
264
dsbZ->setDecimals(Base::UnitsApi::getDecimals());
265
VectorEditItemLayout->addWidget(dsbZ, 2, 1, 1, 1);
267
verticalSpacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);