[TD]fix 9990 page parms before display

This commit is contained in:
wandererfan
2023-07-31 13:57:10 -04:00
committed by WandererFan
parent 28e0f5e9ff
commit b0b7df2424
2 changed files with 103 additions and 39 deletions

View File

@@ -25,7 +25,6 @@
#ifndef _PreComp_
# include <sstream>
# include <QDomDocument>
# include <QFile>
#endif
@@ -86,6 +85,10 @@ void DrawSVGTemplate::onChanged(const App::Property* prop)
//the old template, but there is no guarantee that the same fields will be present.
replaceFileIncluded(Template.getValue());
EditableTexts.setValues(getEditableTextsFromTemplate());
QDomDocument templateDocument;
if (getTemplateDocument(Template.getValue(), templateDocument)) {
extractTemplateAttributes(templateDocument);
}
} else if (prop == &EditableTexts) {
//handled by ViewProvider
}
@@ -103,20 +106,24 @@ QString DrawSVGTemplate::processTemplate()
//can't do anything
return QString();
}
QFile templateFile(Base::Tools::fromStdString(PageResult.getValue()));
if (!templateFile.open(QIODevice::ReadOnly)) {
Base::Console().Error("DrawSVGTemplate::processTemplate can't read embedded template %s!\n", PageResult.getValue());
return QString();
}
QDomDocument templateDocument;
if (!templateDocument.setContent(&templateFile)) {
Base::Console().Error("DrawSVGTemplate::processTemplate - failed to parse file: %s\n",
PageResult.getValue());
if (!getTemplateDocument(PageResult.getValue(), templateDocument)) {
return QString();
}
// QFile templateFile(Base::Tools::fromStdString(PageResult.getValue()));
// if (!templateFile.open(QIODevice::ReadOnly)) {
// Base::Console().Error("DrawSVGTemplate::processTemplate can't read embedded template %s!\n", PageResult.getValue());
// return QString();
// }
// QDomDocument templateDocument;
// if (!templateDocument.setContent(&templateFile)) {
// Base::Console().Error("DrawSVGTemplate::processTemplate - failed to parse file: %s\n",
// PageResult.getValue());
// return QString();
// }
XMLQuery query(templateDocument);
std::map<std::string, std::string> substitutions = EditableTexts.getValues();
@@ -144,8 +151,36 @@ QString DrawSVGTemplate::processTemplate()
return true;
});
// Calculate the dimensions of the page and store for retrieval
// Obtain the size of the SVG document by reading the document attributes
extractTemplateAttributes(templateDocument);
// // Calculate the dimensions of the page and store for retrieval
// // Obtain the size of the SVG document by reading the document attributes
// QDomElement docElement = templateDocument.documentElement();
// Base::Quantity quantity;
// // Obtain the width
// QString str = docElement.attribute(QString::fromLatin1("width"));
// quantity = Base::Quantity::parse(str);
// quantity.setUnit(Base::Unit::Length);
// Width.setValue(quantity.getValue());
// str = docElement.attribute(QString::fromLatin1("height"));
// quantity = Base::Quantity::parse(str);
// quantity.setUnit(Base::Unit::Length);
// Height.setValue(quantity.getValue());
// bool isLandscape = getWidth() / getHeight() >= 1.;
// Orientation.setValue(isLandscape ? 1 : 0);
//all Qt holds on files should be released on exit #4085
return templateDocument.toString();
}
// find the width, height and orientation of the template and update the properties
void DrawSVGTemplate::extractTemplateAttributes(QDomDocument& templateDocument)
{
QDomElement docElement = templateDocument.documentElement();
Base::Quantity quantity;
@@ -165,9 +200,27 @@ QString DrawSVGTemplate::processTemplate()
bool isLandscape = getWidth() / getHeight() >= 1.;
Orientation.setValue(isLandscape ? 1 : 0);
}
//all Qt holds on files should be released on exit #4085
return templateDocument.toString();
// load the included template file as a QDomDocument
bool DrawSVGTemplate::getTemplateDocument(std::string sourceFile, QDomDocument& templateDocument) const
{
if (sourceFile.empty()) {
return false;
}
QFile templateFile(Base::Tools::fromStdString(sourceFile));
if (!templateFile.open(QIODevice::ReadOnly)) {
Base::Console().Error("DrawSVGTemplate::processTemplate can't read embedded template %s!\n", PageResult.getValue());
return false;
}
if (!templateDocument.setContent(&templateFile)) {
Base::Console().Error("DrawSVGTemplate::processTemplate - failed to parse file: %s\n",
PageResult.getValue());
return false;
}
// no errors templateDocument is loaded
return true;
}
double DrawSVGTemplate::getWidth() const
@@ -200,35 +253,42 @@ std::map<std::string, std::string> DrawSVGTemplate::getEditableTextsFromTemplate
// Base::Console().Message("DSVGT::getEditableTextsFromTemplate()\n");
std::map<std::string, std::string> editables;
std::string templateFilename = Template.getValue();
if (templateFilename.empty()) {
return editables;
}
Base::FileInfo tfi(templateFilename);
if (!tfi.isReadable()) {
// if there is an old absolute template file set use a redirect
tfi.setFile(App::Application::getResourceDir() + "Mod/Drawing/Templates/" + tfi.fileName());
// try the redirect
if (!tfi.isReadable()) {
Base::Console().Error("DrawSVGTemplate::getEditableTextsFromTemplate() not able to open %s!\n", Template.getValue());
return editables;
}
}
QFile templateFile(QString::fromUtf8(tfi.filePath().c_str()));
if (!templateFile.open(QIODevice::ReadOnly)) {
Base::Console().Error("DrawSVGTemplate::getEditableTextsFromTemplate() can't read template %s!\n", Template.getValue());
return editables;
}
// std::string templateFilename = Template.getValue();
// if (templateFilename.empty()) {
// return editables;
// }
// if we pass the filename we can reuse getTemplateDocument here
QDomDocument templateDocument;
if (!templateDocument.setContent(&templateFile)) {
Base::Console().Message("DrawSVGTemplate::getEditableTextsFromTemplate() - failed to parse file: %s\n",
Template.getValue());
if (!getTemplateDocument(Template.getValue(), templateDocument)) {
return editables;
}
// Base::FileInfo tfi(templateFilename);
// if (!tfi.isReadable()) {
// // if there is an old absolute template file set use a redirect
// tfi.setFile(App::Application::getResourceDir() + "Mod/Drawing/Templates/" + tfi.fileName());
// // try the redirect
// if (!tfi.isReadable()) {
// Base::Console().Error("DrawSVGTemplate::getEditableTextsFromTemplate() not able to open %s!\n", Template.getValue());
// return editables;
// }
// }
// QFile templateFile(QString::fromUtf8(tfi.filePath().c_str()));
// if (!templateFile.open(QIODevice::ReadOnly)) {
// Base::Console().Error("DrawSVGTemplate::getEditableTextsFromTemplate() can't read template %s!\n", Template.getValue());
// return editables;
// }
// QDomDocument templateDocument;
// if (!templateDocument.setContent(&templateFile)) {
// Base::Console().Message("DrawSVGTemplate::getEditableTextsFromTemplate() - failed to parse file: %s\n",
// Template.getValue());
// return editables;
// }
XMLQuery query(templateDocument);
// XPath query to select all <tspan> nodes whose <text> parent