[TD]fix Symbol update on property change

This commit is contained in:
Wanderer Fan
2022-07-27 18:31:27 -04:00
committed by WandererFan
parent f9bdc3aca7
commit eca5ff6db7
4 changed files with 126 additions and 165 deletions

View File

@@ -25,23 +25,17 @@
#ifndef _PreComp_
# include <sstream>
#include <QDomDocument>
#endif
#include <iomanip>
#include <iterator>
#include <boost/regex.hpp>
#include <QDomDocument>
#include <QXmlQuery>
#include <QXmlResultItems>
#include "QDomNodeModel.h"
#include <Base/Exception.h>
#include <Base/FileInfo.h>
#include <Base/Console.h>
#include <Base/Tools.h>
#include "QDomNodeModel.h"
#include "DrawUtil.h"
#include "DrawPage.h"
#include "DrawViewSymbol.h"
@@ -51,7 +45,6 @@
using namespace TechDraw;
using namespace std;
//===========================================================================
// DrawViewSymbol
//===========================================================================
@@ -75,58 +68,16 @@ DrawViewSymbol::~DrawViewSymbol()
void DrawViewSymbol::onChanged(const App::Property* prop)
{
// Base::Console().Message("DVS::onChanged(%s) \n",prop->getName());
if (prop == &Symbol) {
if (!isRestoring() && Symbol.getValue()[0]) {
//this pulls the initial values from svg into editabletexts
// should only happen first time?? extra loop onChanged->execute->onChanged
std::vector<string> editables;
QDomDocument symbolDocument;
const char* symbol = Symbol.getValue();
QByteArray qba(symbol);
QString errorMsg;
int errorLine;
int errorCol;
bool nsProcess = false;
bool rc = symbolDocument.setContent(qba, nsProcess, &errorMsg, &errorLine, &errorCol);
if (rc) {
QDomElement symbolDocElem = symbolDocument.documentElement();
QXmlQuery query(QXmlQuery::XQuery10);
QDomNodeModel model(query.namePool(), symbolDocument);
query.setFocus(QXmlItem(model.fromDomNode(symbolDocElem)));
// 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"));
QXmlResultItems queryResult;
query.evaluateTo(&queryResult);
while (!queryResult.next().isNull())
{
QDomElement tspanElement = model.toDomNode(queryResult.current().toNodeModelIndex()).toElement();
editables.push_back(tspanElement.text().toStdString());
}
}
else {
Base::Console().Warning("DVS::onChanged - %s - SVG for Symbol is not valid. See log.\n");
Base::Console().Log(
"Warning: DVS::onChanged(Symbol) for %s - len: %d rc: %d error: %s line: %d col: %d\n",
getNameInDocument(), strlen(symbol), rc,
qPrintable(errorMsg), errorLine, errorCol);
}
if (!isRestoring() && !Symbol.isEmpty()) {
std::vector<std::string> editables = getEditableFields();
EditableTexts.setValues(editables);
// requestPaint();
}
} else if (prop == &EditableTexts) {
//this will change Symbol, which will call onChanged(Symbol),
//which will change EditableTexts, but the loop stops after
//1 cycle
updateFieldsInSymbol();
}
TechDraw::DrawView::onChanged(prop);
@@ -134,76 +85,8 @@ void DrawViewSymbol::onChanged(const App::Property* prop)
App::DocumentObjectExecReturn *DrawViewSymbol::execute(void)
{
// Base::Console().Message("DVS::execute() \n");
// //dvs::execute is pretty fast. doesn't need to be blocked?
// if (!keepUpdated()) {
// return App::DocumentObject::StdReturn;
// }
std::string svg = Symbol.getValue();
if (svg.empty()) {
return App::DocumentObject::StdReturn;
}
const std::vector<std::string>& editText = EditableTexts.getValues();
if (!editText.empty()) {
QDomDocument symbolDocument;
const char* symbol = Symbol.getValue();
QByteArray qba(symbol);
QString errorMsg;
int errorLine;
int errorCol;
bool nsProcess = false;
bool rc = symbolDocument.setContent(qba, nsProcess, &errorMsg, &errorLine, &errorCol);
if (rc) {
QDomElement symbolDocElem = symbolDocument.documentElement();
QXmlQuery query(QXmlQuery::XQuery10);
QDomNodeModel model(query.namePool(), symbolDocument);
query.setFocus(QXmlItem(model.fromDomNode(symbolDocElem)));
// 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"));
QXmlResultItems queryResult;
query.evaluateTo(&queryResult);
unsigned int count = 0;
while (!queryResult.next().isNull())
{
QDomElement tspanElement = model.toDomNode(queryResult.current().toNodeModelIndex()).toElement();
// Keep all spaces in the text node
tspanElement.setAttribute(QString::fromUtf8("xml:space"), QString::fromUtf8("preserve"));
// Remove all child nodes (if any)
while (!tspanElement.lastChild().isNull()) {
tspanElement.removeChild(tspanElement.lastChild());
}
// Finally append text node with editable replacement as the only <tspan> descendant
tspanElement.appendChild(symbolDocument.createTextNode(
QString::fromUtf8(editText[count].c_str())));
++count;
}
Symbol.setValue(symbolDocument.toString(1).toStdString());
}
else {
Base::Console().Warning("DVS::execute - %s - SVG for Symbol is not valid. See log.\n");
Base::Console().Log(
"Warning: DVS::execute() - %s - len: %d rc: %d error: %s line: %d col: %d\n",
getNameInDocument(), strlen(symbol), rc,
qPrintable(errorMsg), errorLine, errorCol);
}
}
// requestPaint();
//nothing to do. DVS is just a container for properties.
//the action takes place on the Gui side.
return DrawView::execute();
}
@@ -212,32 +95,9 @@ QRectF DrawViewSymbol::getRect() const
double w = 64.0; //must default to something
double h = 64.0;
return (QRectF(0,0,w,h));
// std::string svg = Symbol.getValue();
// string::const_iterator begin, end;
// begin = svg.begin();
// end = svg.end();
// boost::match_results<std::string::const_iterator> what;
// boost::regex e1 ("width=\"([0-9.]*?)[a-zA-Z]*?\"");
// if (boost::regex_search(begin, end, what, e1)) {
// //std::string wText = what[0].str(); //this is the whole match 'width="100"'
// std::string wNum = what[1].str(); //this is just the number 100
// w = std::stod(wNum);
// }
//
// boost::regex e2 ("height=\"([0-9.]*?)[a-zA-Z]*?\"");
// if (boost::regex_search(begin, end, what, e2)) {
// //std::string hText = what[0].str();
// std::string hNum = what[1].str();
// h = std::stod(hNum);
// }
// return (QRectF(0,0,getScale() * w,getScale() * h));
//we now have a w x h, but we don't really know what it means - px,mm,in,...
}
//!Assume all svg files fit the page and/or the user will scale manually
//see getRect() above
bool DrawViewSymbol::checkFit(TechDraw::DrawPage* p) const
{
(void)p;
@@ -245,19 +105,105 @@ bool DrawViewSymbol::checkFit(TechDraw::DrawPage* p) const
return result;
}
short DrawViewSymbol::mustExecute() const
//get editable fields from symbol
std::vector<std::string> DrawViewSymbol::getEditableFields()
{
short result = 0;
if (!isRestoring()) {
result = (Scale.isTouched() ||
EditableTexts.isTouched());
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())));
// 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();
QString editableValue = tspan.firstChild().nodeValue();
editables.push_back(std::string(editableValue.toUtf8().constData()));
}
}
if ((bool) result) {
return result;
}
return DrawView::mustExecute();
return editables;
}
//replace editable field in symbol with values from property
void DrawViewSymbol::updateFieldsInSymbol()
{
const std::vector<std::string>& editText = EditableTexts.getValues();
if (editText.empty()) {
return;
}
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)));
// 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();
// Keep all spaces in the text node
tspanElement.setAttribute(QString::fromUtf8("xml:space"), QString::fromUtf8("preserve"));
// Remove all child nodes (if any)
while (!tspanElement.lastChild().isNull()) {
tspanElement.removeChild(tspanElement.lastChild());
}
// Finally append text node with editable replacement as the only <tspan> descendant
tspanElement.appendChild(symbolDocument.createTextNode(
QString::fromUtf8(editText[count].c_str())));
++count;
}
Symbol.setValue(symbolDocument.toString(1).toStdString());
}
}
//load QDomDocument
bool DrawViewSymbol::loadQDomDocument(QDomDocument& symbolDocument)
{
const char* symbol = Symbol.getValue();
QByteArray qba(symbol);
QString errorMsg;
int errorLine;
int errorCol;
bool nsProcess = false;
bool rc = symbolDocument.setContent(qba, nsProcess, &errorMsg, &errorLine, &errorCol);
if (!rc) {
//invalid SVG message
Base::Console().Warning("DrawViewSymbol - %s - SVG for Symbol is not valid. See log.\n");
Base::Console().Log("DrawViewSymbol - %s - len: %d rc: %d error: %s line: %d col: %d\n",
getNameInDocument(), strlen(symbol), rc,
qPrintable(errorMsg), errorLine, errorCol);
}
return rc;
}
PyObject *DrawViewSymbol::getPyObject(void)
{