FreeCAD

Форк
0
/
VectorEditWidget.cpp 
268 строк · 9.9 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2022 WandererFan <wandererfan@gmail.com>                *
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
// A widget for editing Vector3d without taking up too much space in the UI.
24

25
#include "PreCompiled.h"
26
#ifndef _PreComp_
27
#include <QApplication>
28
#include <QLabel>
29
#include <QObject>
30
#include <QPushButton>
31
#include <QtGui>
32
#include <QVBoxLayout>
33
#include <QGridLayout>
34
#include <QHBoxLayout>
35
#include <QLabel>
36
#include <QLineEdit>
37
#include <QSpacerItem>
38
#include <QToolButton>
39
#endif
40

41
#include <Mod/TechDraw/TechDrawGlobal.h>
42

43
#include <limits>
44

45
#include <Base/Console.h>
46
#include <Base/Tools.h>
47
#include <Base/UnitsApi.h>
48

49
#include <Gui/SpinBox.h>
50

51
#include <Mod/TechDraw/App/DrawUtil.h>
52

53
#include "VectorEditWidget.h"
54

55
using namespace TechDrawGui;
56
using namespace TechDraw;
57

58
VectorEditWidget::VectorEditWidget(QWidget* parent) : QWidget(parent),
59
    m_minimumWidth(200),
60
    m_minimumHeight(30),
61
    m_expandedHeight(155),
62
    m_blockNotify(false)
63
{
64
    m_size = QSize(m_minimumWidth, m_minimumHeight);
65
    setObjectName(QString::fromUtf8("VectorEdit"));
66
    buildWidget();
67

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);
72

73
    dsbX->installEventFilter(this);
74
    dsbY->installEventFilter(this);
75
    dsbZ->installEventFilter(this);
76
}
77

78
//trap Enter press in dsb? so as not to invoke task accept processing
79
bool VectorEditWidget::eventFilter(QObject *target, QEvent *event)
80
{
81
    if (target == dsbX ||
82
            target == dsbY ||
83
            target == dsbZ) {
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);
89
                dsb->interpretText();
90
                Q_EMIT dsb->valueChanged(dsb->value());
91
                return true;
92
            }
93
        }
94
    }
95
    return QWidget::eventFilter(target, event);
96
}
97
void VectorEditWidget::setLabel(std::string newLabel)
98
{
99
    QString qNewLabelString = Base::Tools::fromStdString(newLabel);
100
    lvectorName->setText(qNewLabelString);
101
}
102

103
void VectorEditWidget::setLabel(QString newLabel)
104
{
105
    lvectorName->setText(newLabel);
106
}
107

108
void VectorEditWidget::setValue(Base::Vector3d newValue)
109
{
110
//    Base::Console().Message("VEW::setValue(%.6f, %.6f, %.6f)\n", newValue.x, newValue.y, newValue.z);
111
    m_value = newValue;
112
    dsbX->setValue(m_value.x);
113
    dsbY->setValue(m_value.y);
114
    dsbZ->setValue(m_value.z);
115
    updateDisplay();
116
}
117

118
void VectorEditWidget::setValueNoNotify(Base::Vector3d newValue)
119
{
120
//    Base::Console().Message("VEW::setValueNoNotify(%.6f, %.6f, %.6f)\n", newValue.x, newValue.y, newValue.z);
121
    m_value = newValue;
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;
127
    updateDisplay();
128
}
129

130
void VectorEditWidget::slotExpandButtonToggled(bool checked)
131
{
132
//    Base::Console().Message("VEW::slotExpand - checked: %d\n", checked);
133
    if (checked) {
134
        vectorEditLayout->addLayout(VectorEditItemLayout);
135
        vectorEditLayout->addItem(verticalSpacer);
136
        m_size = QSize(m_minimumWidth, m_expandedHeight);
137

138
    } else {
139
        vectorEditLayout->removeItem(VectorEditItemLayout);
140
        vectorEditLayout->removeItem(verticalSpacer);
141
        m_size = QSize(m_minimumWidth, m_minimumHeight);
142
    }
143
}
144

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)
148
{
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;
153
        updateDisplay();
154
        Q_EMIT valueChanged(m_value);
155
    }
156
}
157
void VectorEditWidget::slotYValueChanged(double newValue)
158
{
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;
163
        updateDisplay();
164
        Q_EMIT valueChanged(m_value);
165
    }
166
}
167
void VectorEditWidget::slotZValueChanged(double newValue)
168
{
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;
173
        updateDisplay();
174
        Q_EMIT valueChanged(m_value);
175
    }
176
}
177

178
void VectorEditWidget::updateDisplay()
179
{
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);
183
}
184

185
QSize VectorEditWidget::minimumSizeHint() const
186
{
187
    return m_size;
188
}
189

190
void VectorEditWidget::buildWidget()
191
{
192
    if (objectName().isEmpty())
193
        setObjectName(QString::fromUtf8("VectorEdit"));
194
    QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
195
    setSizePolicy(sizePolicy);
196

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"));
203

204
    lvectorName = new QLabel(this);
205
    lvectorName->setObjectName(QString::fromUtf8("lvectorName"));
206
    VectorEditButtonLayout->addWidget(lvectorName);
207

208
    leVectorDisplay = new QLineEdit(this);
209
    leVectorDisplay->setObjectName(QString::fromUtf8("leVectorDisplay"));
210
    VectorEditButtonLayout->addWidget(leVectorDisplay);
211

212
    tbExpand = new QToolButton(this);
213
    tbExpand->setObjectName(QString::fromUtf8("tbExpand"));
214
    tbExpand->setText(QString::fromUtf8("..."));
215
    tbExpand->setCheckable(true);
216
    VectorEditButtonLayout->addWidget(tbExpand);
217

218
    VectorEditButtonLayout->setStretch(0, 1);
219
    VectorEditButtonLayout->setStretch(1, 1);
220
    vectorEditLayout->addLayout(VectorEditButtonLayout);
221

222
    VectorEditItemLayout = new QGridLayout();
223
    VectorEditItemLayout->setObjectName(QString::fromUtf8("VectorEditItemLayout"));
224

225
    lX = new QLabel();
226
    lX->setObjectName(QString::fromUtf8("lX"));
227
    lX->setText(QString::fromUtf8("X:"));
228
    VectorEditItemLayout->addWidget(lX, 0, 0, 1, 1);
229

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);
238

239
    lY = new QLabel();
240
    lY->setObjectName(QString::fromUtf8("lY"));
241
    lY->setText(QString::fromUtf8("Y:"));
242
    VectorEditItemLayout->addWidget(lY, 1, 0, 1, 1);
243

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);
252

253
    lZ = new QLabel();
254
    lZ->setObjectName(QString::fromUtf8("lZ"));
255
    lZ->setText(QString::fromUtf8("Z:"));
256
    VectorEditItemLayout->addWidget(lZ, 2, 0, 1, 1);
257

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);
266

267
    verticalSpacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
268
}
269

270

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.