23
#include "PreCompiled.h"
26
# include <QPixmapCache>
30
#include "BitmapFactory.h"
32
#include "ExpressionBinding.h"
33
#include "QuantitySpinBox_p.h"
35
#include <App/Application.h>
36
#include <App/Document.h>
37
#include <App/DocumentObject.h>
38
#include <App/Expression.h>
39
#include <App/ObjectIdentifier.h>
40
#include <App/PropertyGeo.h>
41
#include <Base/Tools.h>
44
FC_LOG_LEVEL_INIT("Expression",true,true)
48
namespace sp = std::placeholders;
50
ExpressionBinding::ExpressionBinding() = default;
52
ExpressionBinding::~ExpressionBinding() = default;
54
bool ExpressionBinding::isBound() const
56
return path.getDocumentObject() != nullptr;
59
void ExpressionBinding::unbind()
61
expressionchanged.disconnect();
62
objectdeleted.disconnect();
63
path = App::ObjectIdentifier();
66
void Gui::ExpressionBinding::setExpression(std::shared_ptr<Expression> expr)
68
DocumentObject * docObj = path.getDocumentObject();
71
const std::string error = docObj->ExpressionEngine.validateExpression(path, expr);
74
throw Base::RuntimeError(error.c_str());
78
lastExpression = getExpression();
80
bool transaction = !App::GetApplication().getActiveTransaction();
82
std::ostringstream ss;
83
ss << (expr?"Set":"Discard") << " expression " << docObj->Label.getValue();
84
App::GetApplication().setActiveTransaction(ss.str().c_str());
87
docObj->ExpressionEngine.setValue(path, expr);
93
App::GetApplication().closeActiveTransaction();
97
void ExpressionBinding::bind(const App::ObjectIdentifier &_path)
99
const Property * prop = _path.getProperty();
101
Q_ASSERT(prop != nullptr);
103
path = prop->canonicalPath(_path);
106
DocumentObject * docObj = path.getDocumentObject();
109
expressionchanged = docObj->ExpressionEngine.expressionChanged.connect(std::bind(&ExpressionBinding::expressionChange, this, sp::_1));
110
App::Document* doc = docObj->getDocument();
111
objectdeleted = doc->signalDeletedObject.connect(std::bind(&ExpressionBinding::objectDeleted, this, sp::_1));
116
void ExpressionBinding::bind(const Property &prop)
118
bind(App::ObjectIdentifier(prop));
121
bool ExpressionBinding::hasExpression() const
123
return isBound() && getExpression() != nullptr;
126
std::shared_ptr<App::Expression> ExpressionBinding::getExpression() const
128
DocumentObject * docObj = path.getDocumentObject();
130
Q_ASSERT(isBound() && docObj != nullptr);
132
return docObj->getExpression(path).expression;
135
std::string ExpressionBinding::getExpressionString(bool no_throw) const
138
if (!getExpression())
139
throw Base::RuntimeError("No expression found.");
140
return getExpression()->toString();
141
} catch (Base::Exception &e) {
143
FC_ERR("failed to get expression string: " << e.what());
146
} catch (std::exception &e) {
148
FC_ERR("failed to get expression string: " << e.what());
153
FC_ERR("failed to get expression string: unknown exception");
160
std::string ExpressionBinding::getEscapedExpressionString() const
162
std::string escapedstr;
163
escapedstr = Base::Tools::escapedUnicodeFromUtf8(getExpressionString(false).c_str());
164
escapedstr = Base::Tools::escapeQuotesFromString(escapedstr);
168
bool ExpressionBinding::assignToProperty(const std::string & propName, double value)
171
const App::ObjectIdentifier & path = getPath();
172
const Property * prop = path.getProperty();
175
if (prop && prop->isReadOnly())
178
if (prop && prop->isDerivedFrom<App::PropertyPlacement>()) {
179
std::string p = path.getSubPathStr();
180
if (p == ".Rotation.Angle") {
181
value = Base::toRadians(value);
186
Gui::Command::doCommand(Gui::Command::Doc, "%s = %f", propName.c_str(), value);
190
bool ExpressionBinding::apply(const std::string & propName)
193
if (hasExpression()) {
194
DocumentObject * docObj = path.getDocumentObject();
197
throw Base::RuntimeError("Document object not found.");
199
bool transaction = !App::GetApplication().getActiveTransaction();
201
std::ostringstream ss;
202
ss << "Set expression " << docObj->Label.getValue();
203
App::GetApplication().setActiveTransaction(ss.str().c_str());
205
Gui::Command::doCommand(Gui::Command::Doc,"App.getDocument('%s').%s.setExpression('%s', u'%s')",
206
docObj->getDocument()->getName(),
207
docObj->getNameInDocument(),
208
path.toEscapedString().c_str(),
209
getEscapedExpressionString().c_str());
211
App::GetApplication().closeActiveTransaction();
216
DocumentObject * docObj = path.getDocumentObject();
219
throw Base::RuntimeError("Document object not found.");
221
if (lastExpression) {
222
bool transaction = !App::GetApplication().getActiveTransaction();
224
std::ostringstream ss;
225
ss << "Discard expression " << docObj->Label.getValue();
226
App::GetApplication().setActiveTransaction(ss.str().c_str());
228
Gui::Command::doCommand(Gui::Command::Doc,"App.getDocument('%s').%s.setExpression('%s', None)",
229
docObj->getDocument()->getName(),
230
docObj->getNameInDocument(),
231
path.toEscapedString().c_str());
233
App::GetApplication().closeActiveTransaction();
241
bool ExpressionBinding::apply()
243
Property * prop(path.getProperty());
248
DocumentObject * docObj(path.getDocumentObject());
251
throw Base::RuntimeError("Document object not found.");
254
if (prop->isReadOnly())
257
std::string _path = getPath().toEscapedString();
258
const char *path = _path.c_str();
261
return apply(Gui::Command::getObjectCmd(docObj) + "." + path);
264
void ExpressionBinding::expressionChange(const ObjectIdentifier& id) {
270
void ExpressionBinding::objectDeleted(const App::DocumentObject& obj)
272
DocumentObject * docObj = path.getDocumentObject();
273
if (docObj == &obj) {
280
ExpressionWidget::ExpressionWidget() = default;
282
QPixmap ExpressionWidget::getIcon(const char* name, const QSize& size) const
284
QString key = QString::fromLatin1("%1_%2x%3")
285
.arg(QString::fromLatin1(name))
289
if (QPixmapCache::find(key, &icon))
292
icon = BitmapFactory().pixmapFromSvg(name, size);
294
QPixmapCache::insert(key, icon);
298
void ExpressionWidget::makeLabel(QLineEdit* le)
300
defaultPalette = le->palette();
301
defaultPalette.setCurrentColorGroup(QPalette::Active);
304
QFontMetrics fm(le->font());
305
int frameWidth = le->style()->pixelMetric(QStyle::PM_SpinBoxFrameWidth);
306
iconHeight = fm.height() - frameWidth;
307
iconLabel = new ExpressionLabel(le);
308
iconLabel->setCursor(Qt::ArrowCursor);
309
QPixmap pixmap = getIcon(":/icons/bound-expression-unset.svg", QSize(iconHeight, iconHeight));
310
iconLabel->setPixmap(pixmap);
311
iconLabel->setStyleSheet(QString::fromLatin1("QLabel { border: none; padding: 0px; padding-top: %2px; width: %1px; height: %1px }").arg(iconHeight).arg(frameWidth/2));
313
iconLabel->setExpressionText(QString());
314
le->setStyleSheet(QString::fromLatin1("QLineEdit { padding-right: %1px } ").arg(iconHeight+frameWidth));