Gui: [skip ci] refactor QuantitySpinBox::apply

This commit is contained in:
wmayer
2022-10-28 00:44:55 +02:00
parent 8c6e02ac90
commit 9d32c892f0
3 changed files with 25 additions and 19 deletions

View File

@@ -37,6 +37,7 @@
#include <App/DocumentObject.h>
#include <App/Expression.h>
#include <App/ObjectIdentifier.h>
#include <App/PropertyGeo.h>
#include <Base/Tools.h>
@@ -167,6 +168,28 @@ std::string ExpressionBinding::getEscapedExpressionString() const
return Base::Tools::escapedUnicodeFromUtf8(getExpressionString(false).c_str());
}
bool ExpressionBinding::assignToProperty(const std::string & propName, double value)
{
if (isBound()) {
const App::ObjectIdentifier & path = getPath();
const Property * prop = path.getProperty();
/* Skip update if property is bound and we know it is read-only */
if (prop && prop->isReadOnly())
return true;
if (prop && prop->getTypeId().isDerivedFrom(App::PropertyPlacement::getClassTypeId())) {
std::string p = path.getSubPathStr();
if (p == ".Rotation.Angle") {
value = Base::toRadians(value);
}
}
}
Gui::Command::doCommand(Gui::Command::Doc, "%s = %f", propName.c_str(), value);
return true;
}
QPixmap ExpressionBinding::getIcon(const char* name, const QSize& size) const
{
QString key = QString::fromLatin1("%1_%2x%3")

View File

@@ -65,6 +65,7 @@ protected:
std::shared_ptr<App::Expression> getExpression() const;
std::string getExpressionString(bool no_throw=true) const;
std::string getEscapedExpressionString() const;
bool assignToProperty(const std::string & propName, double);
virtual void setExpression(std::shared_ptr<App::Expression> expr);
//gets called when the bound expression is changed, either by this binding or any external action

View File

@@ -39,7 +39,6 @@
#include <App/Document.h>
#include <App/DocumentObject.h>
#include <App/ExpressionParser.h>
#include <App/PropertyGeo.h>
#include <Base/Exception.h>
#include <Base/UnitsApi.h>
#include <Base/Tools.h>
@@ -370,24 +369,7 @@ bool QuantitySpinBox::apply(const std::string & propName)
{
if (!ExpressionBinding::apply(propName)) {
double dValue = value().getValue();
if (isBound()) {
const App::ObjectIdentifier & path = getPath();
const Property * prop = path.getProperty();
/* Skip update if property is bound and we know it is read-only */
if (prop && prop->isReadOnly())
return true;
if (prop && prop->getTypeId().isDerivedFrom(App::PropertyPlacement::getClassTypeId())) {
std::string p = path.getSubPathStr();
if (p == ".Rotation.Angle") {
dValue = Base::toRadians(dValue);
}
}
}
Gui::Command::doCommand(Gui::Command::Doc, "%s = %f", propName.c_str(), dValue);
return true;
return assignToProperty(propName, dValue);
}
return false;