FreeCAD

Форк
0
/
InputVector.cpp 
286 строк · 9.2 Кб
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 "PreCompiled.h"
25
#ifndef _PreComp_
26
# include <QComboBox>
27
#endif
28

29
#include <Base/UnitsApi.h>
30

31
#include "InputVector.h"
32
#include "ui_InputVector.h"
33
#include "QuantitySpinBox.h"
34

35

36
using namespace Gui;
37

38
LocationWidget::LocationWidget (QWidget * parent)
39
  : QWidget(parent)
40
{
41
    box = new QGridLayout();
42

43
    xValue = new QuantitySpinBox(this);
44
    xValue->setMinimum(-2.14748e+09);
45
    xValue->setMaximum(2.14748e+09);
46
    xLabel = new QLabel(this);
47
    box->addWidget(xLabel, 0, 0, 1, 1);
48
    box->addWidget(xValue, 0, 1, 1, 1);
49

50
    yValue = new QuantitySpinBox(this);
51
    yValue->setMinimum(-2.14748e+09);
52
    yValue->setMaximum(2.14748e+09);
53
    yLabel = new QLabel(this);
54
    box->addWidget(yLabel, 1, 0, 1, 1);
55
    box->addWidget(yValue, 1, 1, 1, 1);
56

57
    zValue = new QuantitySpinBox(this);
58
    zValue->setMinimum(-2.14748e+09);
59
    zValue->setMaximum(2.14748e+09);
60
    zLabel = new QLabel(this);
61
    box->addWidget(zLabel, 2, 0, 1, 1);
62
    box->addWidget(zValue, 2, 1, 1, 1);
63

64
    dLabel = new QLabel(this);
65
    dValue = new QComboBox(this);
66
    dValue->setCurrentIndex(-1);
67
    box->addWidget(dLabel, 3, 0, 1, 1);
68
    box->addWidget(dValue, 3, 1, 1, 1);
69

70
    xValue->setUnit(Base::Unit::Length);
71
    yValue->setUnit(Base::Unit::Length);
72
    zValue->setUnit(Base::Unit::Length);
73

74
    auto gridLayout = new QGridLayout(this);
75
    gridLayout->addLayout(box, 0, 0, 1, 2);
76

77
    connect(dValue, qOverload<int>(&QComboBox::activated),
78
            this, &LocationWidget::onDirectionActivated);
79
    retranslateUi();
80
}
81

82
LocationWidget::~LocationWidget() = default;
83

84
QSize LocationWidget::sizeHint() const
85
{
86
    return {150,100};
87
}
88

89
void LocationWidget::changeEvent(QEvent* e)
90
{
91
    if (e->type() == QEvent::LanguageChange) {
92
        this->retranslateUi();
93
    }
94
    QWidget::changeEvent(e);
95
}
96

97
void LocationWidget::retranslateUi()
98
{
99
    xLabel->setText(QApplication::translate("Gui::LocationWidget", "X:"));
100
    yLabel->setText(QApplication::translate("Gui::LocationWidget", "Y:"));
101
    zLabel->setText(QApplication::translate("Gui::LocationWidget", "Z:"));
102
    dLabel->setText(QApplication::translate("Gui::LocationWidget", "Direction:"));
103

104
    if (dValue->count() == 0) {
105
        dValue->insertItems(0, QStringList()
106
         << QApplication::translate("Gui::LocationDialog", "X")
107
         << QApplication::translate("Gui::LocationDialog", "Y")
108
         << QApplication::translate("Gui::LocationDialog", "Z")
109
         << QApplication::translate("Gui::LocationDialog", "User defined...")
110
        );
111

112
        dValue->setCurrentIndex(2);
113

114
        // Vector3d declared to use with QVariant see Gui/propertyeditor/PropertyItem.h
115
        dValue->setItemData(0, QVariant::fromValue<Base::Vector3d>(Base::Vector3d(1,0,0)));
116
        dValue->setItemData(1, QVariant::fromValue<Base::Vector3d>(Base::Vector3d(0,1,0)));
117
        dValue->setItemData(2, QVariant::fromValue<Base::Vector3d>(Base::Vector3d(0,0,1)));
118
    }
119
    else {
120
        dValue->setItemText(0, QApplication::translate("Gui::LocationDialog", "X"));
121
        dValue->setItemText(1, QApplication::translate("Gui::LocationDialog", "Y"));
122
        dValue->setItemText(2, QApplication::translate("Gui::LocationDialog", "Z"));
123
        dValue->setItemText(dValue->count()-1,
124
            QApplication::translate("Gui::LocationDialog", "User defined..."));
125
    }
126
}
127

128
Base::Vector3d LocationWidget::getPosition() const
129
{
130
    return Base::Vector3d(this->xValue->value().getValue(),
131
                          this->yValue->value().getValue(),
132
                          this->zValue->value().getValue());
133
}
134

135
void LocationWidget::setPosition(const Base::Vector3d& v)
136
{
137
    this->xValue->setValue(v.x);
138
    this->yValue->setValue(v.y);
139
    this->zValue->setValue(v.z);
140
}
141

142
void LocationWidget::setDirection(const Base::Vector3d& dir)
143
{
144
    if (dir.Length() < Base::Vector3d::epsilon()) {
145
        return;
146
    }
147

148
    // check if the user-defined direction is already there
149
    for (int i=0; i<dValue->count()-1; i++) {
150
        QVariant data = dValue->itemData (i);
151
        if (data.canConvert<Base::Vector3d>()) {
152
            const auto val = data.value<Base::Vector3d>();
153
            if (val == dir) {
154
                dValue->setCurrentIndex(i);
155
                return;
156
            }
157
        }
158
    }
159

160
    // add a new item before the very last item
161
    QString display = QString::fromLatin1("(%1,%2,%3)")
162
        .arg(dir.x)
163
        .arg(dir.y)
164
        .arg(dir.z);
165
    dValue->insertItem(dValue->count()-1, display,
166
        QVariant::fromValue<Base::Vector3d>(dir));
167
    dValue->setCurrentIndex(dValue->count()-2);
168
}
169

170
Base::Vector3d LocationWidget::getDirection() const
171
{
172
    QVariant data = dValue->itemData (this->dValue->currentIndex());
173
    if (data.canConvert<Base::Vector3d>()) {
174
        return data.value<Base::Vector3d>();
175
    }
176
    else {
177
        return Base::Vector3d(0,0,1);
178
    }
179
}
180

181
Base::Vector3d LocationWidget::getUserDirection(bool* ok) const
182
{
183
    Gui::Dialog::Ui_InputVector iv;
184
    QDialog dlg(const_cast<LocationWidget*>(this));
185
    iv.setupUi(&dlg);
186
    iv.vectorX->setDecimals(Base::UnitsApi::getDecimals());
187
    iv.vectorY->setDecimals(Base::UnitsApi::getDecimals());
188
    iv.vectorZ->setDecimals(Base::UnitsApi::getDecimals());
189
    Base::Vector3d dir;
190
    if (dlg.exec()) {
191
        dir.x = iv.vectorX->value();
192
        dir.y = iv.vectorY->value();
193
        dir.z = iv.vectorZ->value();
194
        if (ok) *ok = true;
195
    }
196
    else {
197
        if (ok) *ok = false;
198
    }
199

200
    return dir;
201
}
202

203
void LocationWidget::onDirectionActivated(int index)
204
{
205
    // last item is selected to define direction by user
206
    if (index+1 == dValue->count()) {
207
        bool ok;
208
        Base::Vector3d dir = this->getUserDirection(&ok);
209
        if (ok) {
210
            if (dir.Length() < Base::Vector3d::epsilon()) {
211
                QMessageBox::critical(this, LocationDialog::tr("Wrong direction"),
212
                    LocationDialog::tr("Direction must not be the null vector"));
213
                return;
214
            }
215

216
            setDirection(dir);
217
        }
218
    }
219
}
220

221
// ----------------------------------------------------------------------------
222

223
LocationDialog::LocationDialog(QWidget* parent, Qt::WindowFlags fl)
224
  : QDialog(parent, fl)
225
{
226
}
227

228
LocationDialog::~LocationDialog() = default;
229

230
Base::Vector3d LocationDialog::getUserDirection(bool* ok) const
231
{
232
    Gui::Dialog::Ui_InputVector iv;
233
    QDialog dlg(const_cast<LocationDialog*>(this));
234
    iv.setupUi(&dlg);
235
    iv.vectorX->setDecimals(Base::UnitsApi::getDecimals());
236
    iv.vectorY->setDecimals(Base::UnitsApi::getDecimals());
237
    iv.vectorZ->setDecimals(Base::UnitsApi::getDecimals());
238
    Base::Vector3d dir;
239
    if (dlg.exec()) {
240
        dir.x = iv.vectorX->value();
241
        dir.y = iv.vectorY->value();
242
        dir.z = iv.vectorZ->value();
243
        if (ok) *ok = true;
244
    }
245
    else {
246
        if (ok) *ok = false;
247
    }
248

249
    return dir;
250
}
251

252
void LocationDialog::onDirectionActivated(int index)
253
{
254
    directionActivated(index);
255
}
256

257
// -----------------------------------------------------------
258

259
LocationDialogUiImp::~LocationDialogUiImp() = default;
260

261
Base::Vector3d LocationDialogUiImp::getDirection() const
262
{
263
    return ui->getDirection();
264
}
265

266
Base::Vector3d LocationDialogUiImp::getPosition() const
267
{
268
    return ui->getPosition();
269
}
270

271
void LocationDialogUiImp::changeEvent(QEvent *e)
272
{
273
    if (e->type() == QEvent::LanguageChange) {
274
        ui->retranslate(this);
275
    }
276
    else {
277
        QDialog::changeEvent(e);
278
    }
279
}
280

281
void LocationDialogUiImp::directionActivated(int index)
282
{
283
    ui->directionActivated(this,index);
284
}
285

286
#include "moc_InputVector.cpp"
287

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

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

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

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