From 55515b1e7f4e4e0a2a054b16b89ba2fb997bf7ee Mon Sep 17 00:00:00 2001 From: wandererfan Date: Sat, 22 Jun 2024 18:53:13 -0400 Subject: [PATCH] [TD]allow template fields to re-apply autofill --- src/Mod/TechDraw/App/DrawSVGTemplate.cpp | 35 ++++++++++++++++++++++ src/Mod/TechDraw/App/DrawSVGTemplate.h | 1 + src/Mod/TechDraw/Gui/DlgTemplateField.cpp | 5 ++++ src/Mod/TechDraw/Gui/DlgTemplateField.h | 1 + src/Mod/TechDraw/Gui/DlgTemplateField.ui | 12 +++++++- src/Mod/TechDraw/Gui/TemplateTextField.cpp | 18 ++++++++--- 6 files changed, 67 insertions(+), 5 deletions(-) diff --git a/src/Mod/TechDraw/App/DrawSVGTemplate.cpp b/src/Mod/TechDraw/App/DrawSVGTemplate.cpp index 5b22fc43c2..7caf3d3df7 100644 --- a/src/Mod/TechDraw/App/DrawSVGTemplate.cpp +++ b/src/Mod/TechDraw/App/DrawSVGTemplate.cpp @@ -288,6 +288,41 @@ std::map DrawSVGTemplate::getEditableTextsFromTemplate return editables; } +QString DrawSVGTemplate::getAutofillByEditableName(QString nameToMatch) +{ + QString result; + QString nameCapture{nameToMatch}; + + QDomDocument templateDocument; + if (!getTemplateDocument(Template.getValue(), templateDocument)) { + return {}; + } + + XMLQuery query(templateDocument); + + // XPath query to select all nodes whose parent + // has "freecad:editable" attribute + query.processItems(QString::fromUtf8( + "declare default element namespace \"" SVG_NS_URI "\"; " + "declare namespace freecad=\"" FREECAD_SVG_NS_URI "\"; " + "//text[@" FREECAD_ATTR_EDITABLE "]/tspan"), + [this, &nameCapture, &result](QDomElement& tspan) -> bool { + QDomElement parent = tspan.parentNode().toElement(); + QString editableName = parent.attribute(QString::fromUtf8(FREECAD_ATTR_EDITABLE)); + if (editableName == nameCapture && + parent.hasAttribute(QString::fromUtf8(FREECAD_ATTR_AUTOFILL))) { + QString autofillName = parent.attribute(QString::fromUtf8(FREECAD_ATTR_AUTOFILL)); + QString autofillValue = getAutofillValue(autofillName); + if (!autofillValue.isEmpty()) { + result = autofillValue; + } + } + return true; + }); + return result; +} + + //! get a translated label string from the context (ex TaskActiveView), the base name (ex ActiveView) and //! the unique name within the document (ex ActiveView001), and use it to update the Label property. void DrawSVGTemplate::translateLabel(std::string context, std::string baseName, std::string uniqueName) diff --git a/src/Mod/TechDraw/App/DrawSVGTemplate.h b/src/Mod/TechDraw/App/DrawSVGTemplate.h index b8b662c49f..dc120eb361 100644 --- a/src/Mod/TechDraw/App/DrawSVGTemplate.h +++ b/src/Mod/TechDraw/App/DrawSVGTemplate.h @@ -62,6 +62,7 @@ public: QString processTemplate(); void extractTemplateAttributes(QDomDocument& templateDocument); bool getTemplateDocument(std::string sourceFile, QDomDocument& templateDocument) const; + QString getAutofillByEditableName(QString nameToMatch); void translateLabel(std::string context, std::string baseName, std::string uniqueName); diff --git a/src/Mod/TechDraw/Gui/DlgTemplateField.cpp b/src/Mod/TechDraw/Gui/DlgTemplateField.cpp index 2acb8a2a4a..b0f804148d 100644 --- a/src/Mod/TechDraw/Gui/DlgTemplateField.cpp +++ b/src/Mod/TechDraw/Gui/DlgTemplateField.cpp @@ -67,6 +67,11 @@ QString DlgTemplateField::getFieldContent() return ui->leInput->text(); } +bool DlgTemplateField::getAutofillState() +{ + return ui->cbAutofill->isChecked(); +} + void DlgTemplateField::accept() { QDialog::accept(); diff --git a/src/Mod/TechDraw/Gui/DlgTemplateField.h b/src/Mod/TechDraw/Gui/DlgTemplateField.h index 129d375822..ebd6a2f6c7 100644 --- a/src/Mod/TechDraw/Gui/DlgTemplateField.h +++ b/src/Mod/TechDraw/Gui/DlgTemplateField.h @@ -45,6 +45,7 @@ public: void setFieldLength(int length); void setFieldContent(std::string content); QString getFieldContent(); + bool getAutofillState(); public Q_SLOTS: void accept() override; diff --git a/src/Mod/TechDraw/Gui/DlgTemplateField.ui b/src/Mod/TechDraw/Gui/DlgTemplateField.ui index 6565a9d86b..c6f9492b97 100644 --- a/src/Mod/TechDraw/Gui/DlgTemplateField.ui +++ b/src/Mod/TechDraw/Gui/DlgTemplateField.ui @@ -10,7 +10,7 @@ 0 0 340 - 90 + 127 @@ -46,6 +46,16 @@ + + + + Check this box to reapply autofill to this field. + + + Autofill + + + diff --git a/src/Mod/TechDraw/Gui/TemplateTextField.cpp b/src/Mod/TechDraw/Gui/TemplateTextField.cpp index e21b5eb820..af6e138d41 100644 --- a/src/Mod/TechDraw/Gui/TemplateTextField.cpp +++ b/src/Mod/TechDraw/Gui/TemplateTextField.cpp @@ -30,12 +30,16 @@ #endif // #ifndef _PreCmp_ #include +#include + #include +#include #include "DlgTemplateField.h" #include "TemplateTextField.h" using namespace TechDrawGui; +using namespace TechDraw; TemplateTextField::TemplateTextField(QGraphicsItem *parent, TechDraw::DrawTemplate *myTmplte, @@ -76,12 +80,18 @@ void TemplateTextField::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) ui.setFieldContent(tmplte->EditableTexts[fieldNameStr]); if (ui.exec() == QDialog::Accepted) { - //WF: why is this escaped? - // "<" is converted elsewhere and no other characters cause problems. - // escaping causes "&" to appear as "&" etc -// QString qsClean = ui.getFieldContent().toHtmlEscaped(); QString qsClean = ui.getFieldContent(); std::string utf8Content = qsClean.toUtf8().constData(); + if (ui.getAutofillState()) { + auto svgTemplate = dynamic_cast(tmplte); + if (svgTemplate) { + QString fieldName = Base::Tools::fromStdString(fieldNameStr); + QString autofillValue = svgTemplate->getAutofillByEditableName(fieldName); + if (!autofillValue.isEmpty()) { + utf8Content = autofillValue.toUtf8().constData(); + } + } + } tmplte->EditableTexts.setValue(fieldNameStr, utf8Content); }