[TD]allow manual update of autofill fields

This commit is contained in:
wandererfan
2024-06-22 11:36:11 -04:00
committed by WandererFan
parent 668a6ea010
commit cb2573844f

View File

@@ -78,7 +78,7 @@ PyObject *DrawSVGTemplate::getPyObject()
void DrawSVGTemplate::onChanged(const App::Property* prop)
{
if (prop == &Template && !isRestoring()) {
//if we are restoring an existing file we just want the properties set as they were save,
//if we are restoring an existing file we just want the properties set as they were saved,
//but if we are not restoring, we need to replace the embedded file and extract the new
//EditableTexts.
//We could try to find matching field names are preserve the values from
@@ -89,8 +89,6 @@ void DrawSVGTemplate::onChanged(const App::Property* prop)
if (getTemplateDocument(Template.getValue(), templateDocument)) {
extractTemplateAttributes(templateDocument);
}
} else if (prop == &EditableTexts) {
//handled by ViewProvider
}
TechDraw::DrawTemplate::onChanged(prop);
@@ -123,14 +121,10 @@ void DrawSVGTemplate::slotDeletedObject(const App::DocumentObject& obj)
EditableTexts.touch();
}
//parse the Svg code, inserting current EditableTexts values, and return the result as a QString.
//While parsing, note the Orientation, Width and Height values in the Svg code.
QString DrawSVGTemplate::processTemplate()
{
Base::Console().Message("DSVGT::processTemplate() - isRestoring: %d\n", isRestoring());
if (isRestoring()) {
//until everything is fully restored, the embedded file is not available, so we
//can't do anything
@@ -144,6 +138,7 @@ QString DrawSVGTemplate::processTemplate()
XMLQuery query(templateDocument);
std::map<std::string, std::string> substitutions = EditableTexts.getValues();
// auto captureTextValues = m_initialTextValues;
// XPath query to select all <tspan> nodes whose <text> parent
// has "freecad:editable" attribute
@@ -151,22 +146,17 @@ QString DrawSVGTemplate::processTemplate()
"declare default element namespace \"" SVG_NS_URI "\"; "
"declare namespace freecad=\"" FREECAD_SVG_NS_URI "\"; "
"//text[@" FREECAD_ATTR_EDITABLE "]/tspan"),
[this, &substitutions, &templateDocument](QDomElement& tspan) -> bool {
// [this, &substitutions, &templateDocument, &captureTextValues](QDomElement& tspan) -> bool {
[&substitutions, &templateDocument](QDomElement& tspan) -> bool {
// Replace the editable text spans with new nodes holding actual values
QString editableName = tspan.parentNode().toElement().attribute(QString::fromUtf8(FREECAD_ATTR_EDITABLE));
std::map<std::string, std::string>::iterator item =
substitutions.find(editableName.toStdString());
if (item != substitutions.end()) {
// we have an editable text, is it autofill? autofill values may have
// changed.
// we have an editable text
QDomElement parent = tspan.parentNode().toElement();
QString editableValue = QString::fromUtf8(item->second.c_str());
if (parent.hasAttribute(QString::fromUtf8(FREECAD_ATTR_AUTOFILL))) {
QString autofillValue = getAutofillValue(parent.attribute(QString::fromUtf8(FREECAD_ATTR_AUTOFILL)));
if (!autofillValue.isNull()) {
editableValue = autofillValue;
}
}
// Keep all spaces in the text node
tspan.setAttribute(QString::fromUtf8("xml:space"), QString::fromUtf8("preserve"));
@@ -242,7 +232,6 @@ double DrawSVGTemplate::getHeight() const
void DrawSVGTemplate::replaceFileIncluded(std::string newTemplateFileName)
{
// Base::Console().Message("DSVGT::replaceFileIncluded(%s)\n", newTemplateFileName.c_str());
if (newTemplateFileName.empty()) {
return;
}
@@ -255,18 +244,17 @@ void DrawSVGTemplate::replaceFileIncluded(std::string newTemplateFileName)
}
}
//! find the special fields in the template (freecad:editable or freecad:autofill)
std::map<std::string, std::string> DrawSVGTemplate::getEditableTextsFromTemplate()
{
Base::Console().Message("DSVGT::getEditableTextsFromTemplate()\n");
std::map<std::string, std::string> editables;
// if we pass the filename we can reuse getTemplateDocument here
QDomDocument templateDocument;
if (!getTemplateDocument(Template.getValue(), templateDocument)) {
return editables;
}
XMLQuery query(templateDocument);
// XPath query to select all <tspan> nodes whose <text> parent
@@ -278,17 +266,17 @@ std::map<std::string, std::string> DrawSVGTemplate::getEditableTextsFromTemplate
[this, &editables](QDomElement& tspan) -> bool {
QDomElement parent = tspan.parentNode().toElement();
QString editableName = parent.attribute(QString::fromUtf8(FREECAD_ATTR_EDITABLE));
QString editableValue;
if (parent.hasAttribute(QString::fromUtf8(FREECAD_ATTR_AUTOFILL))) {
QString autofillValue = getAutofillValue(parent.attribute(QString::fromUtf8(FREECAD_ATTR_AUTOFILL)));
if (!autofillValue.isNull()) {
QString autofillName = parent.attribute(QString::fromUtf8(FREECAD_ATTR_AUTOFILL));
QString autofillValue = getAutofillValue(autofillName);
if (!autofillValue.isEmpty()) {
editableValue = autofillValue;
}
}
// If the autofill value is not specified or unsupported, use the default text value
if (editableValue.isNull()) {
if (editableValue.isEmpty()) {
editableValue = tspan.firstChild().nodeValue();
}