[TD]allow template fields to re-apply autofill

This commit is contained in:
wandererfan
2024-06-22 18:53:13 -04:00
committed by WandererFan
parent 4d6fbac1a5
commit 6fd73487c7
6 changed files with 67 additions and 5 deletions

View File

@@ -288,6 +288,41 @@ std::map<std::string, std::string> 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 <tspan> nodes whose <text> 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)

View File

@@ -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);

View File

@@ -67,6 +67,11 @@ QString DlgTemplateField::getFieldContent()
return ui->leInput->text();
}
bool DlgTemplateField::getAutofillState()
{
return ui->cbAutofill->isChecked();
}
void DlgTemplateField::accept()
{
QDialog::accept();

View File

@@ -45,6 +45,7 @@ public:
void setFieldLength(int length);
void setFieldContent(std::string content);
QString getFieldContent();
bool getAutofillState();
public Q_SLOTS:
void accept() override;

View File

@@ -10,7 +10,7 @@
<x>0</x>
<y>0</y>
<width>340</width>
<height>90</height>
<height>127</height>
</rect>
</property>
<property name="windowTitle">
@@ -46,6 +46,16 @@
<item row="1" column="1">
<widget class="QLineEdit" name="leInput"/>
</item>
<item row="2" column="0">
<widget class="QCheckBox" name="cbAutofill">
<property name="toolTip">
<string>Check this box to reapply autofill to this field. </string>
</property>
<property name="text">
<string>Autofill</string>
</property>
</widget>
</item>
</layout>
</item>
<item>

View File

@@ -30,12 +30,16 @@
#endif // #ifndef _PreCmp_
#include <Base/Console.h>
#include <Base/Tools.h>
#include <Mod/TechDraw/App/DrawTemplate.h>
#include <Mod/TechDraw/App/DrawSVGTemplate.h>
#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 "&amp;" etc
// QString qsClean = ui.getFieldContent().toHtmlEscaped();
QString qsClean = ui.getFieldContent();
std::string utf8Content = qsClean.toUtf8().constData();
if (ui.getAutofillState()) {
auto svgTemplate = dynamic_cast<DrawSVGTemplate*>(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);
}