diff --git a/src/Mod/TechDraw/App/DrawSVGTemplate.cpp b/src/Mod/TechDraw/App/DrawSVGTemplate.cpp index 3773b3632f..fe36964dcc 100644 --- a/src/Mod/TechDraw/App/DrawSVGTemplate.cpp +++ b/src/Mod/TechDraw/App/DrawSVGTemplate.cpp @@ -25,7 +25,6 @@ #ifndef _PreComp_ # include -# include # include #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 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 DrawSVGTemplate::getEditableTextsFromTemplate // Base::Console().Message("DSVGT::getEditableTextsFromTemplate()\n"); std::map 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 nodes whose parent diff --git a/src/Mod/TechDraw/App/DrawSVGTemplate.h b/src/Mod/TechDraw/App/DrawSVGTemplate.h index 030ebc33f1..4454967e11 100644 --- a/src/Mod/TechDraw/App/DrawSVGTemplate.h +++ b/src/Mod/TechDraw/App/DrawSVGTemplate.h @@ -23,6 +23,8 @@ #ifndef TECHDRAW_DrawSVGTemplate_h_ #define TECHDRAW_DrawSVGTemplate_h_ +# include + #include #include #include @@ -56,6 +58,8 @@ public: double getHeight() const override; QString processTemplate(); + void extractTemplateAttributes(QDomDocument& templateDocument); + bool getTemplateDocument(std::string sourceFile, QDomDocument& templateDocument) const; void translateLabel(std::string context, std::string baseName, std::string uniqueName);