Skip to content

Commit

Permalink
[TD]fix Symbol update on property change
Browse files Browse the repository at this point in the history
  • Loading branch information
WandererFan committed Jul 28, 2022
1 parent 91cffca commit 0805d28
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 164 deletions.
264 changes: 105 additions & 159 deletions src/Mod/TechDraw/App/DrawViewSymbol.cpp
Expand Up @@ -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"
Expand All @@ -51,7 +45,6 @@
using namespace TechDraw;
using namespace std;


//===========================================================================
// DrawViewSymbol
//===========================================================================
Expand All @@ -75,135 +68,25 @@ 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);
}

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();
}

Expand All @@ -212,52 +95,115 @@ 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;
bool result = true;
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 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());
}
return DrawView::mustExecute();
}

//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)
{
Expand Down
14 changes: 9 additions & 5 deletions src/Mod/TechDraw/App/DrawViewSymbol.h
Expand Up @@ -23,18 +23,21 @@
#ifndef _DrawViewSymbol_h_
#define _DrawViewSymbol_h_

#include <Mod/TechDraw/TechDrawGlobal.h>

#include <QDomDocument>
#include <QXmlResultItems>

#include <App/DocumentObject.h>
#include <App/FeaturePython.h>
#include <Base/BoundBox.h>

#include "DrawView.h"


namespace TechDraw
{
class DrawPage;


class TechDrawExport DrawViewSymbol : public TechDraw::DrawView
{
PROPERTY_HEADER_WITH_OVERRIDE(TechDraw::DrawViewSymbol);
Expand Down Expand Up @@ -63,12 +66,13 @@ class TechDrawExport DrawViewSymbol : public TechDraw::DrawView
//return PyObject as DrawViewSymbolPy
virtual PyObject *getPyObject(void) override;

virtual short mustExecute() const override;


protected:
virtual void onChanged(const App::Property* prop) override;
Base::BoundBox3d bbox;

std::vector<std::string> getEditableFields();
void updateFieldsInSymbol();
bool loadQDomDocument(QDomDocument& symbolDocument);
};

typedef App::FeaturePythonT<DrawViewSymbol> DrawViewSymbolPython;
Expand Down
9 changes: 9 additions & 0 deletions src/Mod/TechDraw/Gui/ViewProviderSymbol.cpp
Expand Up @@ -67,6 +67,15 @@ std::vector<std::string> ViewProviderSymbol::getDisplayModes(void) const

void ViewProviderSymbol::updateData(const App::Property* prop)
{
if (prop == &getViewObject()->Scale) {
onGuiRepaint(getViewObject());
} else if (prop == &getViewObject()->Rotation) {
onGuiRepaint(getViewObject());
} else if (prop == &getViewObject()->Symbol) {
onGuiRepaint(getViewObject());
} else if (prop == &getViewObject()->EditableTexts) {
onGuiRepaint(getViewObject());
}
ViewProviderDrawingView::updateData(prop);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Mod/TechDraw/Gui/ViewProviderSymbol.h
Expand Up @@ -24,6 +24,8 @@
#ifndef DRAWINGGUI_VIEWPROVIDERSYMBOL_H
#define DRAWINGGUI_VIEWPROVIDERSYMBOL_H

#include <Mod/TechDraw/TechDrawGlobal.h>

#include <Mod/TechDraw/App/DrawViewSymbol.h>

#include "ViewProviderDrawingView.h"
Expand Down

0 comments on commit 0805d28

Please sign in to comment.