Skip to content

Commit

Permalink
[TD]fix templated autofill for sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
WandererFan committed Apr 7, 2024
1 parent ff2bca2 commit ee39814
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 75 deletions.
116 changes: 47 additions & 69 deletions src/Mod/TechDraw/App/DrawSVGTemplate.cpp
Expand Up @@ -96,34 +96,52 @@ void DrawSVGTemplate::onChanged(const App::Property* prop)
TechDraw::DrawTemplate::onChanged(prop);
}

void DrawSVGTemplate::onSettingDocument()
{
attachDocument(DocumentObject::getDocument());
DrawTemplate::onSettingDocument();
}

//? should this check for creation of a template or a page?
void DrawSVGTemplate::slotCreatedObject(const App::DocumentObject& obj)
{
// Base::Console().Message("DSVGT::slotCreatedObject()\n");
if (!obj.isDerivedFrom(TechDraw::DrawPage::getClassTypeId())) {
// we don't care
return;
}
EditableTexts.touch();
}

void DrawSVGTemplate::slotDeletedObject(const App::DocumentObject& obj)
{
// Base::Console().Message("DSVGT::slotDeletedObject()\n");
if (!obj.isDerivedFrom(TechDraw::DrawPage::getClassTypeId())) {
// we don't care
return;
}
EditableTexts.touch();
}




//parse the Svg code, inserting current EditableTexts values, and return the result as a QString.
//While parsing, note the Orientation, Width and Height values in the Svg code.
QString DrawSVGTemplate::processTemplate()
{
// Base::Console().Message("DSVGT::processTemplate() - isRestoring: %d\n", isRestoring());
// Base::Console().Message("DSVGT::processTemplate() - isRestoring: %d\n", isRestoring());
if (isRestoring()) {
//until everything is fully restored, the embedded file is not available, so we
//can't do anything
return QString();

Check warning on line 137 in src/Mod/TechDraw/App/DrawSVGTemplate.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

avoid repeating the return type from the declaration; use a braced initializer list instead [modernize-return-braced-init-list]
}

QDomDocument templateDocument;
if (!getTemplateDocument(PageResult.getValue(), templateDocument)) {
return QString();

Check warning on line 142 in src/Mod/TechDraw/App/DrawSVGTemplate.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

avoid repeating the return type from the declaration; use a braced initializer list instead [modernize-return-braced-init-list]
}

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

Expand All @@ -133,47 +151,36 @@ QString DrawSVGTemplate::processTemplate()
"declare default element namespace \"" SVG_NS_URI "\"; "
"declare namespace freecad=\"" FREECAD_SVG_NS_URI "\"; "
"//text[@" FREECAD_ATTR_EDITABLE "]/tspan"),
[&substitutions, &templateDocument](QDomElement& tspan) -> bool {
[this, &substitutions, &templateDocument](QDomElement& tspan) -> bool {
// Replace the editable text spans with new nodes holding actual values
QString editableName = tspan.parentNode().toElement().attribute(QString::fromUtf8(FREECAD_ATTR_EDITABLE));
std::map<std::string, std::string>::iterator item =

Check warning on line 157 in src/Mod/TechDraw/App/DrawSVGTemplate.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

use auto when declaring iterators [modernize-use-auto]
substitutions.find(editableName.toStdString());
if (item != substitutions.end()) {
// we have an editable text, is it autofill? autofill values may have
// changed.
QDomElement parent = tspan.parentNode().toElement();
QString editableValue = QString::fromUtf8(item->second.c_str());
if (parent.hasAttribute(QString::fromUtf8(FREECAD_ATTR_AUTOFILL))) {
QString autofillValue = getAutofillValue(parent.attribute(QString::fromUtf8(FREECAD_ATTR_AUTOFILL)));
if (!autofillValue.isNull()) {
editableValue = autofillValue;
}
}

// Keep all spaces in the text node
tspan.setAttribute(QString::fromUtf8("xml:space"), QString::fromUtf8("preserve"));

// Remove all child nodes and append text node with editable replacement as the only descendant
while (!tspan.lastChild().isNull()) {
tspan.removeChild(tspan.lastChild());
}
tspan.appendChild(templateDocument.createTextNode(QString::fromUtf8(item->second.c_str())));
tspan.appendChild(templateDocument.createTextNode(editableValue));
}
return true;
});

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();
}
Expand Down Expand Up @@ -250,45 +257,16 @@ void DrawSVGTemplate::replaceFileIncluded(std::string newTemplateFileName)

std::map<std::string, std::string> DrawSVGTemplate::getEditableTextsFromTemplate()
{
// Base::Console().Message("DSVGT::getEditableTextsFromTemplate()\n");
// Base::Console().Message("DSVGT::getEditableTextsFromTemplate()\n");
std::map<std::string, std::string> editables;

// std::string templateFilename = Template.getValue();
// if (templateFilename.empty()) {
// return editables;
// }

// if we pass the filename we can reuse getTemplateDocument here
QDomDocument templateDocument;
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
Expand All @@ -311,7 +289,7 @@ std::map<std::string, std::string> DrawSVGTemplate::getEditableTextsFromTemplate

// If the autofill value is not specified or unsupported, use the default text value
if (editableValue.isNull()) {
editableValue = tspan.firstChild().nodeValue();
editableValue = tspan.firstChild().nodeValue();
}

editables[std::string(editableName.toUtf8().constData())] =
Expand Down
10 changes: 9 additions & 1 deletion src/Mod/TechDraw/App/DrawSVGTemplate.h
Expand Up @@ -25,6 +25,7 @@

# include <QDomDocument>

#include <App/DocumentObserver.h>
#include <App/FeaturePython.h>
#include <App/PropertyFile.h>
#include <Mod/TechDraw/TechDrawGlobal.h>
Expand All @@ -35,7 +36,8 @@
namespace TechDraw
{

class TechDrawExport DrawSVGTemplate: public TechDraw::DrawTemplate
class TechDrawExport DrawSVGTemplate: public TechDraw::DrawTemplate,
public App::DocumentObserver
{
PROPERTY_HEADER_WITH_OVERRIDE(TechDraw::DrawSVGTemplate);

Expand Down Expand Up @@ -65,9 +67,15 @@ class TechDrawExport DrawSVGTemplate: public TechDraw::DrawTemplate


protected:
void onSettingDocument() override;

void replaceFileIncluded(std::string newTemplateFileName);
std::map<std::string, std::string> getEditableTextsFromTemplate();

private:
void slotCreatedObject(const App::DocumentObject& obj) override;
void slotDeletedObject(const App::DocumentObject& obj) override;

};

using DrawSVGTemplatePython = App::FeaturePythonT<DrawSVGTemplate>;
Expand Down
6 changes: 4 additions & 2 deletions src/Mod/TechDraw/App/DrawTemplate.cpp
Expand Up @@ -136,9 +136,11 @@ QString DrawTemplate::getAutofillValue(const QString &id) const
std::vector<DocumentObject *> pages = getDocument()->getObjectsOfType(TechDraw::DrawPage::getClassTypeId());
std::vector<QString> pageNames;
for (auto page : pages) {
pageNames.push_back(QString::fromUtf8(page->Label.getValue()));
if (page->isAttachedToDocument() &&
!page->testStatus(App::ObjectStatus::Remove)) {
pageNames.push_back(QString::fromUtf8(page->Label.getValue()));
}
}

QCollator collator;
std::sort(pageNames.begin(), pageNames.end(), collator);

Expand Down
6 changes: 3 additions & 3 deletions src/Mod/TechDraw/Gui/ViewProviderTemplate.cpp
Expand Up @@ -62,8 +62,8 @@ ViewProviderTemplate::ViewProviderTemplate() : m_myName(std::string())

sPixmap = "TechDraw_TreePageTemplate";

// Do not show in property editor why? wf WF: because DisplayMode applies only to coin and we
// don't use coin.
// Do not show in property editor why? wf WF: because DisplayMode applies only to coin and we
// don't use coin.
DisplayMode.setStatus(App::Property::Hidden, true);
}

Expand All @@ -86,7 +86,7 @@ void ViewProviderTemplate::updateData(const App::Property* prop)
if (prop == &(t->Template)) {
auto page = t->getParentPage();
Gui::ViewProvider* vp =
Gui::Application::Instance->getDocument(t->getDocument())->getViewProvider(page);
Gui::Application::Instance->getDocument(t->DocumentObject::getDocument())->getViewProvider(page);
TechDrawGui::ViewProviderPage* vpp = dynamic_cast<TechDrawGui::ViewProviderPage*>(vp);
if (vpp) {
vpp->getQGSPage()->attachTemplate(t);
Expand Down

0 comments on commit ee39814

Please sign in to comment.