Debian Codegraph @

src/Gui/ExpressionBinding.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2015 Eivind Kvedalen <[email protected]> *
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., 51 Franklin Street, *
19 * Fifth Floor, Boston, MA 02110-1301, USA *
20 * *
21 ***************************************************************************/
22
23 #include <QLineEdit>
24 #include <QPixmapCache>
25 #include <QStyle>
26
27 #include "BitmapFactory.h"
28 #include "Command.h"
29 #include "ExpressionBinding.h"
30 #include "QuantitySpinBox_p.h"
31
32 #include <App/Application.h>
33 #include <App/Document.h>
34 #include <App/DocumentObject.h>
35 #include <App/Expression.h>
36 #include <App/ObjectIdentifier.h>
37 #include <App/PropertyGeo.h>
38 #include <Base/Tools.h>
39
40
41 FC_LOG_LEVEL_INIT("Expression", true, true)
42
43 using namespace Gui;
44 using namespace App;
45 namespace sp = std::placeholders;
46
47 ExpressionBinding::ExpressionBinding() = default;
48
49 ExpressionBinding::~ExpressionBinding() = default;
50
51 bool ExpressionBinding::isBound() const
52 {
53 return path.getDocumentObject() != nullptr;
54 }
55
56 void ExpressionBinding::unbind()
57 {
58 expressionchanged.disconnect();
59 objectdeleted.disconnect();
60 documentdeleted.disconnect();
61 path = App::ObjectIdentifier();
62 }
63
64 void Gui::ExpressionBinding::setExpression(std::shared_ptr<Expression> expr)
65 {
66 DocumentObject* docObj = path.getDocumentObject();
67
68 if (expr) {
69 const std::string error = docObj->ExpressionEngine.validateExpression(path, expr);
70
71 if (!error.empty()) {
72 throw Base::RuntimeError(error.c_str());
73 }
74 }
75
76 lastExpression = getExpression();
77
78 bool transaction = !App::GetApplication().getActiveTransaction();
79 if (transaction) {
80 std::ostringstream ss;
81 ss << (expr ? "Set" : "Discard") << " expression " << docObj->Label.getValue();
82 App::GetApplication().setActiveTransaction(ss.str().c_str());
83 }
84
85 docObj->ExpressionEngine.setValue(path, expr);
86
87 if (m_autoApply) {
88 apply();
89 }
90
91 if (transaction) {
92 App::GetApplication().closeActiveTransaction();
93 }
94 }
95
96 void ExpressionBinding::bind(const App::ObjectIdentifier& _path)
97 {
98 const Property* prop = _path.getProperty();
99
100 Q_ASSERT(prop != nullptr);
101
102 path = prop->canonicalPath(_path);
103
104 // connect to be informed about changes
105 DocumentObject* docObj = path.getDocumentObject();
106 if (docObj) {
107 // NOLINTBEGIN
108 expressionchanged = docObj->ExpressionEngine.expressionChanged.connect(
109 std::bind(&ExpressionBinding::expressionChange, this, sp::_1)
110 );
111 App::Document* doc = docObj->getDocument();
112 objectdeleted = doc->signalDeletedObject.connect(
113 std::bind(&ExpressionBinding::objectDeleted, this, sp::_1)
114 );
115 documentdeleted = App::GetApplication().signalDeleteDocument.connect(
116 std::bind(&ExpressionBinding::onDocumentDeleted, this, sp::_1)
117 );
118 // NOLINTEND
119 }
120 }
121
122 void ExpressionBinding::bind(const Property& prop)
123 {
124 bind(App::ObjectIdentifier(prop));
125 }
126
127 bool ExpressionBinding::hasExpression() const
128 {
129 return isBound() && getExpression() != nullptr;
130 }
131
132 std::shared_ptr<App::Expression> ExpressionBinding::getExpression() const
133 {
134 DocumentObject* docObj = path.getDocumentObject();
135
136 Q_ASSERT(isBound() && docObj != nullptr);
137
138 return docObj->getExpression(path).expression;
139 }
140
141 std::string ExpressionBinding::getExpressionString(bool no_throw) const
142 {
143 try {
144 if (!getExpression()) {
145 throw Base::RuntimeError("No expression found.");
146 }
147 return getExpression()->toString();
148 }
149 catch (Base::Exception& e) {
150 if (no_throw) {
151 FC_ERR("failed to get expression string: " << e.what());
152 }
153 else {
154 throw;
155 }
156 }
157 catch (std::exception& e) {
158 if (no_throw) {
159 FC_ERR("failed to get expression string: " << e.what());
160 }
161 else {
162 throw;
163 }
164 }
165 catch (...) {
166 if (no_throw) {
167 FC_ERR("failed to get expression string: unknown exception");
168 }
169 else {
170 throw;
171 }
172 }
173 return {};
174 }
175
176 std::string ExpressionBinding::getEscapedExpressionString() const
177 {
178 std::string escapedstr;
179 escapedstr = Base::Tools::escapedUnicodeFromUtf8(getExpressionString(false).c_str());
180 escapedstr = Base::Tools::escapeQuotesFromString(escapedstr);
181 return escapedstr;
182 }
183
184 bool ExpressionBinding::assignToProperty(const std::string& propName, double value)
185 {
186 if (isBound()) {
187 const App::ObjectIdentifier& path = getPath();
188 const Property* prop = path.getProperty();
189
190 /* Skip update if property is bound and we know it is read-only */
191 if (prop && prop->isReadOnly()) {
192 return true;
193 }
194
195 if (prop && prop->isDerivedFrom<App::PropertyPlacement>()) {
196 std::string p = path.getSubPathStr();
197 if (p == ".Rotation.Angle") {
198 value = Base::toRadians(value);
199 }
200 }
201 }
202
203 Gui::Command::doCommand(Gui::Command::Doc, "%s = %f", propName.c_str(), value);
204 return true;
205 }
206
207 bool ExpressionBinding::apply(const std::string& propName)
208 {
209 Q_UNUSED(propName);
210 if (hasExpression()) {
211 DocumentObject* docObj = path.getDocumentObject();
212
213 if (!docObj) {
214 throw Base::RuntimeError("Document object not found.");
215 }
216
217 bool transaction = !App::GetApplication().getActiveTransaction();
218 if (transaction) {
219 std::ostringstream ss;
220 ss << "Set expression " << docObj->Label.getValue();
221 App::GetApplication().setActiveTransaction(ss.str().c_str());
222 }
223 Gui::Command::doCommand(
224 Gui::Command::Doc,
225 "App.getDocument('%s').%s.setExpression('%s', u'%s')",
226 docObj->getDocument()->getName(),
227 docObj->getNameInDocument(),
228 path.toEscapedString().c_str(),
229 getEscapedExpressionString().c_str()
230 );
231 if (transaction) {
232 App::GetApplication().closeActiveTransaction();
233 }
234 return true;
235 }
236 else {
237 if (isBound()) {
238 DocumentObject* docObj = path.getDocumentObject();
239
240 if (!docObj) {
241 throw Base::RuntimeError("Document object not found.");
242 }
243
244 if (lastExpression) {
245 bool transaction = !App::GetApplication().getActiveTransaction();
246 if (transaction) {
247 std::ostringstream ss;
248 ss << "Discard expression " << docObj->Label.getValue();
249 App::GetApplication().setActiveTransaction(ss.str().c_str());
250 }
251 Gui::Command::doCommand(
252 Gui::Command::Doc,
253 "App.getDocument('%s').%s.setExpression('%s', None)",
254 docObj->getDocument()->getName(),
255 docObj->getNameInDocument(),
256 path.toEscapedString().c_str()
257 );
258 if (transaction) {
259 App::GetApplication().closeActiveTransaction();
260 }
261 }
262 }
263
264 return false;
265 }
266 }
267
268 bool ExpressionBinding::apply()
269 {
270 Property* prop(path.getProperty());
271
272 assert(prop);
273 Q_UNUSED(prop);
274
275 DocumentObject* docObj(path.getDocumentObject());
276
277 if (!docObj) {
278 throw Base::RuntimeError("Document object not found.");
279 }
280
281 /* Skip updating read-only properties */
282 if (prop->isReadOnly()) {
283 return true;
284 }
285
286 std::string _path = getPath().toEscapedString();
287 const char* path = _path.c_str();
288 if (path[0] == '.') {
289 ++path;
290 }
291 return apply(Gui::Command::getObjectCmd(docObj) + "." + path);
292 }
293
294 void ExpressionBinding::expressionChange(const ObjectIdentifier& id)
295 {
296
297 if (id == path) {
298 onChange();
299 }
300 }
301
302 void ExpressionBinding::objectDeleted(const App::DocumentObject& obj)
303 {
304 DocumentObject* docObj = path.getDocumentObject();
305 if (docObj == &obj) {
306 unbind();
307 }
308 }
309
310 void ExpressionBinding::onDocumentDeleted(const App::Document& doc)
311 {
312 if (path.getOwner() && path.getOwner()->getDocument() == &doc) {
313 unbind();
314 }
315 }
316
317 // ----------------------------------------------------------------------------
318
319 ExpressionWidget::ExpressionWidget() = default;
320
321 QPixmap ExpressionWidget::getIcon(const char* name, const QSize& size) const
322 {
323 QString key
324 = QStringLiteral("%1_%2x%3").arg(QString::fromLatin1(name)).arg(size.width()).arg(size.height());
325 QPixmap icon;
326 if (QPixmapCache::find(key, &icon)) {
327 return icon;
328 }
329
330 icon = BitmapFactory().pixmapFromSvg(name, size);
331 if (!icon.isNull()) {
332 QPixmapCache::insert(key, icon);
333 }
334 return icon;
335 }
336
337 void ExpressionWidget::unbind()
338 {
339 iconLabel->hide();
340 ExpressionBinding::unbind();
341 }
342
343 void ExpressionWidget::makeLabel(QLineEdit* le)
344 {
345 defaultPalette = le->palette();
346 defaultPalette.setCurrentColorGroup(QPalette::Active);
347
348 /* Icon for f(x) */
349 QFontMetrics fm(le->font());
350 iconHeight = fm.height();
351 iconLabel = new ExpressionLabel(le);
352 iconLabel->setCursor(Qt::ArrowCursor);
353 QPixmap pixmap = getIcon(":/icons/bound-expression-unset.svg", QSize(iconHeight, iconHeight));
354 iconLabel->setPixmap(pixmap);
355 iconLabel->hide();
356 iconLabel->setExpressionText(QString());
357 }