TD: move all XML query handling to a single function

This commit is contained in:
wmayer
2023-03-18 14:48:03 +01:00
committed by WandererFan
parent c7d637d677
commit 6fb167582a
7 changed files with 213 additions and 128 deletions

View File

@@ -23,11 +23,7 @@
#include "PreCompiled.h"
#ifndef _PreComp_
# include <sstream>
# include "QDomNodeModel.h"
# include <QDomDocument>
# include <QXmlResultItems>
# include <QXmlQuery>
#endif
#include <Base/Console.h>
@@ -36,6 +32,7 @@
#include "DrawViewSymbolPy.h" // generated from DrawViewSymbolPy.xml
#include "DrawPage.h"
#include "DrawUtil.h"
#include "XMLQuery.h"
using namespace TechDraw;
@@ -105,30 +102,22 @@ bool DrawViewSymbol::checkFit(TechDraw::DrawPage* p) const
std::vector<std::string> DrawViewSymbol::getEditableFields()
{
QDomDocument symbolDocument;
QXmlResultItems queryResult;
std::vector<std::string> editables;
bool rc = loadQDomDocument(symbolDocument);
if (rc) {
QDomElement symbolDocElem = symbolDocument.documentElement();
QXmlQuery query(QXmlQuery::XQuery10);
QDomNodeModel model(query.namePool(), symbolDocument);
query.setFocus(QXmlItem(model.fromDomNode(symbolDocument.documentElement())));
XMLQuery query(symbolDocument);
// XPath query to select all <tspan> nodes whose <text> parent
// has "freecad:editable" attribute
query.setQuery(QString::fromUtf8("declare default element namespace \"" SVG_NS_URI "\"; "
"declare namespace freecad=\"" FREECAD_SVG_NS_URI "\"; "
"//text[@freecad:editable]/tspan"));
query.evaluateTo(&queryResult);
while (!queryResult.next().isNull()) {
QDomElement tspan =
model.toDomNode(queryResult.current().toNodeModelIndex()).toElement();
query.processItems(QString::fromUtf8("declare default element namespace \"" SVG_NS_URI "\"; "
"declare namespace freecad=\"" FREECAD_SVG_NS_URI "\"; "
"//text[@freecad:editable]/tspan"),
[&editables](QDomElement& tspan) -> bool {
QString editableValue = tspan.firstChild().nodeValue();
editables.emplace_back(editableValue.toUtf8().constData());
}
editables.emplace_back(editableValue.toStdString());
return true;
});
}
return editables;
}
@@ -142,27 +131,22 @@ void DrawViewSymbol::updateFieldsInSymbol()
}
QDomDocument symbolDocument;
QXmlResultItems queryResult;
bool rc = loadQDomDocument(symbolDocument);
if (rc) {
QDomElement symbolDocElem = symbolDocument.documentElement();
QXmlQuery query(QXmlQuery::XQuery10);
QDomNodeModel model(query.namePool(), symbolDocument);
query.setFocus(QXmlItem(model.fromDomNode(symbolDocElem)));
XMLQuery query(symbolDocument);
std::size_t count = 0;
// XPath query to select all <tspan> nodes whose <text> parent
// has "freecad:editable" attribute
query.setQuery(QString::fromUtf8("declare default element namespace \"" SVG_NS_URI "\"; "
"declare namespace freecad=\"" FREECAD_SVG_NS_URI "\"; "
"//text[@freecad:editable]/tspan"));
query.evaluateTo(&queryResult);
unsigned int count = 0;
while (!queryResult.next().isNull()) {
QDomElement tspanElement =
model.toDomNode(queryResult.current().toNodeModelIndex()).toElement();
query.processItems(QString::fromUtf8("declare default element namespace \"" SVG_NS_URI "\"; "
"declare namespace freecad=\"" FREECAD_SVG_NS_URI "\"; "
"//text[@freecad:editable]/tspan"),
[&symbolDocument, &editText, &count](QDomElement& tspanElement) -> bool {
if (count >= editText.size()) {
return false;
}
// Keep all spaces in the text node
tspanElement.setAttribute(QString::fromUtf8("xml:space"),
QString::fromUtf8("preserve"));
@@ -174,9 +158,11 @@ void DrawViewSymbol::updateFieldsInSymbol()
// Finally append text node with editable replacement as the only <tspan> descendant
tspanElement.appendChild(
symbolDocument.createTextNode(QString::fromUtf8(editText[count].c_str())));
symbolDocument.createTextNode(QString::fromStdString(editText[count])));
++count;
}
return true;
});
Symbol.setValue(symbolDocument.toString(1).toStdString());
}
}