Make image scaling command aware of user units (#7222)

* Image: image scaling command is user unit aware ; fixes #6118

- if unitless value entered, InputField uses user choosen base unit
 Before change, always used FC core base unit
 Only applies to GUI user entered values, value changed programmatically still ...
 ... considered in core base unit if applied unitless
This commit is contained in:
0penBrain
2022-07-25 21:17:02 +02:00
committed by GitHub
parent 73f91e3623
commit b3cbedcc4e
2 changed files with 20 additions and 15 deletions

View File

@@ -642,6 +642,19 @@ void InputField::focusInEvent(QFocusEvent *event)
void InputField::focusOutEvent(QFocusEvent *event)
{
try {
if (Quantity::parse(this->text()).getUnit().isEmpty()) {
// if user didn't enter a unit, we virtually compensate
// the multiplication factor induced by user unit system
double factor;
QString unitStr;
actQuantity.getUserString(factor, unitStr);
actQuantity = actQuantity * factor;
}
}
catch (const Base::ParserError& e) {
// do nothing, let apply the last known good value
}
this->setText(actQuantity.getUserString());
QLineEdit::focusOutEvent(event);
}

View File

@@ -108,9 +108,11 @@ def cmdCreateImageScaling(name, trackers):
self.label.setObjectName(_fromUtf8("label"))
self.horizontalLayout.addWidget(self.label)
self.lineEdit = QtGui.QLineEdit(Dialog)
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.horizontalLayout.addWidget(self.lineEdit)
self.quantity = FreeCADGui.UiLoader().createWidget("Gui::InputField")
self.quantity.setParent(Dialog)
self.quantity.setProperty('unit', 'mm')
self.quantity.setObjectName(_fromUtf8("QuantityField"))
self.horizontalLayout.addWidget(self.quantity)
self.label1 = QtGui.QLabel(Dialog)
self.label1.setObjectName(_fromUtf8("label1"))
@@ -136,23 +138,13 @@ def cmdCreateImageScaling(name, trackers):
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(translate("Dialog", "Scale image plane", None))
self.label.setText(translate("Dialog", "Distance [mm]", None))
self.label.setText(translate("Dialog", "Distance", None))
self.label1.setText(translate("Dialog", "Select first point", None))
def accept(self):
sel = FreeCADGui.Selection.getSelection()
try:
try:
q = FreeCAD.Units.parseQuantity(self.lineEdit.text())
d = q.Value
if q.Unit == FreeCAD.Units.Unit(): # plain number
ok = True
elif q.Unit == FreeCAD.Units.Length:
ok = True
except Exception:
ok = False
if not ok:
raise ValueError
d = self.quantity.property('rawValue')
s=d/self.distance
sel[0].XSize.Value=sel[0].XSize.Value*s
sel[0].YSize.Value=sel[0].YSize.Value*s