@@ -96,34 +96,52 @@ void DrawSVGTemplate::onChanged(const App::Property* prop)
96
96
TechDraw::DrawTemplate::onChanged (prop);
97
97
}
98
98
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
+
99
129
// parse the Svg code, inserting current EditableTexts values, and return the result as a QString.
100
130
// While parsing, note the Orientation, Width and Height values in the Svg code.
101
131
QString DrawSVGTemplate::processTemplate ()
102
132
{
103
- // Base::Console().Message("DSVGT::processTemplate() - isRestoring: %d\n", isRestoring());
133
+ // Base::Console().Message("DSVGT::processTemplate() - isRestoring: %d\n", isRestoring());
104
134
if (isRestoring ()) {
105
135
// until everything is fully restored, the embedded file is not available, so we
106
136
// can't do anything
107
137
return QString ();
108
138
}
139
+
109
140
QDomDocument templateDocument;
110
141
if (!getTemplateDocument (PageResult.getValue (), templateDocument)) {
111
142
return QString ();
112
143
}
113
144
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
-
127
145
XMLQuery query (templateDocument);
128
146
std::map<std::string, std::string> substitutions = EditableTexts.getValues ();
129
147
@@ -133,47 +151,36 @@ QString DrawSVGTemplate::processTemplate()
133
151
" declare default element namespace \" " SVG_NS_URI " \" ; "
134
152
" declare namespace freecad=\" " FREECAD_SVG_NS_URI " \" ; "
135
153
" //text[@" FREECAD_ATTR_EDITABLE " ]/tspan" ),
136
- [&substitutions, &templateDocument](QDomElement& tspan) -> bool {
154
+ [this , &substitutions, &templateDocument](QDomElement& tspan) -> bool {
137
155
// Replace the editable text spans with new nodes holding actual values
138
156
QString editableName = tspan.parentNode ().toElement ().attribute (QString::fromUtf8 (FREECAD_ATTR_EDITABLE));
139
157
std::map<std::string, std::string>::iterator item =
140
158
substitutions.find (editableName.toStdString ());
141
159
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
+
142
171
// Keep all spaces in the text node
143
172
tspan.setAttribute (QString::fromUtf8 (" xml:space" ), QString::fromUtf8 (" preserve" ));
144
173
145
174
// Remove all child nodes and append text node with editable replacement as the only descendant
146
175
while (!tspan.lastChild ().isNull ()) {
147
176
tspan.removeChild (tspan.lastChild ());
148
177
}
149
- tspan.appendChild (templateDocument.createTextNode (QString::fromUtf8 (item-> second . c_str ()) ));
178
+ tspan.appendChild (templateDocument.createTextNode (editableValue ));
150
179
}
151
180
return true ;
152
181
});
153
182
154
183
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
-
177
184
// all Qt holds on files should be released on exit #4085
178
185
return templateDocument.toString ();
179
186
}
@@ -250,45 +257,16 @@ void DrawSVGTemplate::replaceFileIncluded(std::string newTemplateFileName)
250
257
251
258
std::map<std::string, std::string> DrawSVGTemplate::getEditableTextsFromTemplate ()
252
259
{
253
- // Base::Console().Message("DSVGT::getEditableTextsFromTemplate()\n");
260
+ // Base::Console().Message("DSVGT::getEditableTextsFromTemplate()\n");
254
261
std::map<std::string, std::string> editables;
255
262
256
- // std::string templateFilename = Template.getValue();
257
- // if (templateFilename.empty()) {
258
- // return editables;
259
- // }
260
-
261
263
// if we pass the filename we can reuse getTemplateDocument here
262
264
QDomDocument templateDocument;
263
265
if (!getTemplateDocument (Template.getValue (), templateDocument)) {
264
266
return editables;
265
267
}
266
268
267
269
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
-
292
270
XMLQuery query (templateDocument);
293
271
294
272
// XPath query to select all <tspan> nodes whose <text> parent
@@ -311,7 +289,7 @@ std::map<std::string, std::string> DrawSVGTemplate::getEditableTextsFromTemplate
311
289
312
290
// If the autofill value is not specified or unsupported, use the default text value
313
291
if (editableValue.isNull ()) {
314
- editableValue = tspan.firstChild ().nodeValue ();
292
+ editableValue = tspan.firstChild ().nodeValue ();
315
293
}
316
294
317
295
editables[std::string (editableName.toUtf8 ().constData ())] =
0 commit comments