Skip to content

Commit ee39814

Browse files
committed
[TD]fix templated autofill for sheet
1 parent ff2bca2 commit ee39814

File tree

4 files changed

+63
-75
lines changed

4 files changed

+63
-75
lines changed

src/Mod/TechDraw/App/DrawSVGTemplate.cpp

+47-69
Original file line numberDiff line numberDiff line change
@@ -96,34 +96,52 @@ void DrawSVGTemplate::onChanged(const App::Property* prop)
9696
TechDraw::DrawTemplate::onChanged(prop);
9797
}
9898

99+
void DrawSVGTemplate::onSettingDocument()
100+
{
101+
attachDocument(DocumentObject::getDocument());
102+
DrawTemplate::onSettingDocument();
103+
}
104+
105+
//? should this check for creation of a template or a page?
106+
void DrawSVGTemplate::slotCreatedObject(const App::DocumentObject& obj)
107+
{
108+
// Base::Console().Message("DSVGT::slotCreatedObject()\n");
109+
if (!obj.isDerivedFrom(TechDraw::DrawPage::getClassTypeId())) {
110+
// we don't care
111+
return;
112+
}
113+
EditableTexts.touch();
114+
}
115+
116+
void DrawSVGTemplate::slotDeletedObject(const App::DocumentObject& obj)
117+
{
118+
// Base::Console().Message("DSVGT::slotDeletedObject()\n");
119+
if (!obj.isDerivedFrom(TechDraw::DrawPage::getClassTypeId())) {
120+
// we don't care
121+
return;
122+
}
123+
EditableTexts.touch();
124+
}
125+
126+
127+
128+
99129
//parse the Svg code, inserting current EditableTexts values, and return the result as a QString.
100130
//While parsing, note the Orientation, Width and Height values in the Svg code.
101131
QString DrawSVGTemplate::processTemplate()
102132
{
103-
// Base::Console().Message("DSVGT::processTemplate() - isRestoring: %d\n", isRestoring());
133+
// Base::Console().Message("DSVGT::processTemplate() - isRestoring: %d\n", isRestoring());
104134
if (isRestoring()) {
105135
//until everything is fully restored, the embedded file is not available, so we
106136
//can't do anything
107137
return QString();
108138
}
139+
109140
QDomDocument templateDocument;
110141
if (!getTemplateDocument(PageResult.getValue(), templateDocument)) {
111142
return QString();
112143
}
113144

114-
// QFile templateFile(Base::Tools::fromStdString(PageResult.getValue()));
115-
// if (!templateFile.open(QIODevice::ReadOnly)) {
116-
// Base::Console().Error("DrawSVGTemplate::processTemplate can't read embedded template %s!\n", PageResult.getValue());
117-
// return QString();
118-
// }
119-
120-
// QDomDocument templateDocument;
121-
// if (!templateDocument.setContent(&templateFile)) {
122-
// Base::Console().Error("DrawSVGTemplate::processTemplate - failed to parse file: %s\n",
123-
// PageResult.getValue());
124-
// return QString();
125-
// }
126-
127145
XMLQuery query(templateDocument);
128146
std::map<std::string, std::string> substitutions = EditableTexts.getValues();
129147

@@ -133,47 +151,36 @@ QString DrawSVGTemplate::processTemplate()
133151
"declare default element namespace \"" SVG_NS_URI "\"; "
134152
"declare namespace freecad=\"" FREECAD_SVG_NS_URI "\"; "
135153
"//text[@" FREECAD_ATTR_EDITABLE "]/tspan"),
136-
[&substitutions, &templateDocument](QDomElement& tspan) -> bool {
154+
[this, &substitutions, &templateDocument](QDomElement& tspan) -> bool {
137155
// Replace the editable text spans with new nodes holding actual values
138156
QString editableName = tspan.parentNode().toElement().attribute(QString::fromUtf8(FREECAD_ATTR_EDITABLE));
139157
std::map<std::string, std::string>::iterator item =
140158
substitutions.find(editableName.toStdString());
141159
if (item != substitutions.end()) {
160+
// we have an editable text, is it autofill? autofill values may have
161+
// changed.
162+
QDomElement parent = tspan.parentNode().toElement();
163+
QString editableValue = QString::fromUtf8(item->second.c_str());
164+
if (parent.hasAttribute(QString::fromUtf8(FREECAD_ATTR_AUTOFILL))) {
165+
QString autofillValue = getAutofillValue(parent.attribute(QString::fromUtf8(FREECAD_ATTR_AUTOFILL)));
166+
if (!autofillValue.isNull()) {
167+
editableValue = autofillValue;
168+
}
169+
}
170+
142171
// Keep all spaces in the text node
143172
tspan.setAttribute(QString::fromUtf8("xml:space"), QString::fromUtf8("preserve"));
144173

145174
// Remove all child nodes and append text node with editable replacement as the only descendant
146175
while (!tspan.lastChild().isNull()) {
147176
tspan.removeChild(tspan.lastChild());
148177
}
149-
tspan.appendChild(templateDocument.createTextNode(QString::fromUtf8(item->second.c_str())));
178+
tspan.appendChild(templateDocument.createTextNode(editableValue));
150179
}
151180
return true;
152181
});
153182

154183
extractTemplateAttributes(templateDocument);
155-
// // Calculate the dimensions of the page and store for retrieval
156-
// // Obtain the size of the SVG document by reading the document attributes
157-
// QDomElement docElement = templateDocument.documentElement();
158-
// Base::Quantity quantity;
159-
160-
// // Obtain the width
161-
// QString str = docElement.attribute(QString::fromLatin1("width"));
162-
// quantity = Base::Quantity::parse(str);
163-
// quantity.setUnit(Base::Unit::Length);
164-
165-
// Width.setValue(quantity.getValue());
166-
167-
// str = docElement.attribute(QString::fromLatin1("height"));
168-
// quantity = Base::Quantity::parse(str);
169-
// quantity.setUnit(Base::Unit::Length);
170-
171-
// Height.setValue(quantity.getValue());
172-
173-
// bool isLandscape = getWidth() / getHeight() >= 1.;
174-
175-
// Orientation.setValue(isLandscape ? 1 : 0);
176-
177184
//all Qt holds on files should be released on exit #4085
178185
return templateDocument.toString();
179186
}
@@ -250,45 +257,16 @@ void DrawSVGTemplate::replaceFileIncluded(std::string newTemplateFileName)
250257

251258
std::map<std::string, std::string> DrawSVGTemplate::getEditableTextsFromTemplate()
252259
{
253-
// Base::Console().Message("DSVGT::getEditableTextsFromTemplate()\n");
260+
// Base::Console().Message("DSVGT::getEditableTextsFromTemplate()\n");
254261
std::map<std::string, std::string> editables;
255262

256-
// std::string templateFilename = Template.getValue();
257-
// if (templateFilename.empty()) {
258-
// return editables;
259-
// }
260-
261263
// if we pass the filename we can reuse getTemplateDocument here
262264
QDomDocument templateDocument;
263265
if (!getTemplateDocument(Template.getValue(), templateDocument)) {
264266
return editables;
265267
}
266268

267269

268-
// Base::FileInfo tfi(templateFilename);
269-
// if (!tfi.isReadable()) {
270-
// // if there is an old absolute template file set use a redirect
271-
// tfi.setFile(App::Application::getResourceDir() + "Mod/Drawing/Templates/" + tfi.fileName());
272-
// // try the redirect
273-
// if (!tfi.isReadable()) {
274-
// Base::Console().Error("DrawSVGTemplate::getEditableTextsFromTemplate() not able to open %s!\n", Template.getValue());
275-
// return editables;
276-
// }
277-
// }
278-
279-
// QFile templateFile(QString::fromUtf8(tfi.filePath().c_str()));
280-
// if (!templateFile.open(QIODevice::ReadOnly)) {
281-
// Base::Console().Error("DrawSVGTemplate::getEditableTextsFromTemplate() can't read template %s!\n", Template.getValue());
282-
// return editables;
283-
// }
284-
285-
// QDomDocument templateDocument;
286-
// if (!templateDocument.setContent(&templateFile)) {
287-
// Base::Console().Message("DrawSVGTemplate::getEditableTextsFromTemplate() - failed to parse file: %s\n",
288-
// Template.getValue());
289-
// return editables;
290-
// }
291-
292270
XMLQuery query(templateDocument);
293271

294272
// XPath query to select all <tspan> nodes whose <text> parent
@@ -311,7 +289,7 @@ std::map<std::string, std::string> DrawSVGTemplate::getEditableTextsFromTemplate
311289

312290
// If the autofill value is not specified or unsupported, use the default text value
313291
if (editableValue.isNull()) {
314-
editableValue = tspan.firstChild().nodeValue();
292+
editableValue = tspan.firstChild().nodeValue();
315293
}
316294

317295
editables[std::string(editableName.toUtf8().constData())] =

src/Mod/TechDraw/App/DrawSVGTemplate.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
# include <QDomDocument>
2727

28+
#include <App/DocumentObserver.h>
2829
#include <App/FeaturePython.h>
2930
#include <App/PropertyFile.h>
3031
#include <Mod/TechDraw/TechDrawGlobal.h>
@@ -35,7 +36,8 @@
3536
namespace TechDraw
3637
{
3738

38-
class TechDrawExport DrawSVGTemplate: public TechDraw::DrawTemplate
39+
class TechDrawExport DrawSVGTemplate: public TechDraw::DrawTemplate,
40+
public App::DocumentObserver
3941
{
4042
PROPERTY_HEADER_WITH_OVERRIDE(TechDraw::DrawSVGTemplate);
4143

@@ -65,9 +67,15 @@ class TechDrawExport DrawSVGTemplate: public TechDraw::DrawTemplate
6567

6668

6769
protected:
70+
void onSettingDocument() override;
71+
6872
void replaceFileIncluded(std::string newTemplateFileName);
6973
std::map<std::string, std::string> getEditableTextsFromTemplate();
7074

75+
private:
76+
void slotCreatedObject(const App::DocumentObject& obj) override;
77+
void slotDeletedObject(const App::DocumentObject& obj) override;
78+
7179
};
7280

7381
using DrawSVGTemplatePython = App::FeaturePythonT<DrawSVGTemplate>;

src/Mod/TechDraw/App/DrawTemplate.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,11 @@ QString DrawTemplate::getAutofillValue(const QString &id) const
136136
std::vector<DocumentObject *> pages = getDocument()->getObjectsOfType(TechDraw::DrawPage::getClassTypeId());
137137
std::vector<QString> pageNames;
138138
for (auto page : pages) {
139-
pageNames.push_back(QString::fromUtf8(page->Label.getValue()));
139+
if (page->isAttachedToDocument() &&
140+
!page->testStatus(App::ObjectStatus::Remove)) {
141+
pageNames.push_back(QString::fromUtf8(page->Label.getValue()));
142+
}
140143
}
141-
142144
QCollator collator;
143145
std::sort(pageNames.begin(), pageNames.end(), collator);
144146

src/Mod/TechDraw/Gui/ViewProviderTemplate.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ ViewProviderTemplate::ViewProviderTemplate() : m_myName(std::string())
6262

6363
sPixmap = "TechDraw_TreePageTemplate";
6464

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

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

0 commit comments

Comments
 (0)