23#include <tqstringlist.h>
27#include "kivio_stencil.h"
28#include "kivio_dia_stencil_spawner.h"
29#include "diapathparser.h"
31KivioDiaStencilSpawner::KivioDiaStencilSpawner(KivioStencilSpawnerSet *p) : KivioStencilSpawner(p)
33 m_smlStencilSpawner =
new KivioSMLStencilSpawner(p);
36KivioDiaStencilSpawner::~KivioDiaStencilSpawner()
40TQDomElement KivioDiaStencilSpawner::saveXML(TQDomDocument &d)
42 return m_smlStencilSpawner->saveXML(d);
45void KivioDiaStencilSpawner::calculateDimensions(
float x,
float y)
51float KivioDiaStencilSpawner::diaPointToKivio(
float point,
bool xpoint)
53 float returnPoint = 0.0;
57 returnPoint = point - m_lowestx;
64 returnPoint =point - m_lowesty;
72bool KivioDiaStencilSpawner::load(
const TQString &file)
74 TQDomDocument dia(
"test");
75 TQDomDocument kivio(
"XML");
80 if(f.open(IO_ReadOnly) ==
false)
82 kdDebug(43000) <<
"KivioDiaStencilSpawner::load() - Error opening stencil: " << file << endl;
86 TQDomNode diaMain = dia.namedItem(
"shape");
89 TQDomElement firstElement = kivio.createElement(
"KivioShapeStencil");
90 firstElement.setAttribute(
"creator",
"kiviodiafilter");
92 kivio.appendChild(firstElement);
95 TQDomElement spawnerInfoElement = kivio.createElement(
"KivioSMLStencilSpawnerInfo");
96 TQDomElement authorInfoElement = kivio.createElement(
"Author");
97 authorInfoElement.setAttribute(
"data",
"n/a");
98 TQDomElement titleInfoElement = kivio.createElement(
"Title");
99 titleInfoElement.setAttribute(
"data", diaMain.namedItem(
"name").toElement().text());
100 TQDomElement idInfoElement = kivio.createElement(
"Id");
101 idInfoElement.setAttribute(
"data", diaMain.namedItem(
"name").toElement().text());
102 TQDomElement descriptionInfoElement = kivio.createElement(
"Description");
103 descriptionInfoElement.setAttribute(
"data", diaMain.namedItem(
"description").toElement().text());
104 TQDomElement versionInfoElement = kivio.createElement(
"Version");
105 versionInfoElement.setAttribute(
"data",
"1.0");
106 TQDomElement webInfoElement = kivio.createElement(
"Web");
107 webInfoElement.setAttribute(
"data",
"http://");
108 TQDomElement emailInfoElement = kivio.createElement(
"Email");
109 emailInfoElement.setAttribute(
"data",
"n/a");
110 TQDomElement copyrightInfoElement = kivio.createElement(
"Copyright");
111 copyrightInfoElement.setAttribute(
"data",
"n/a");
112 TQDomElement autoUpdateInfoElement = kivio.createElement(
"AutoUpdate");
113 autoUpdateInfoElement.setAttribute(
"data",
"off");
115 spawnerInfoElement.appendChild(authorInfoElement);
116 spawnerInfoElement.appendChild(titleInfoElement);
117 spawnerInfoElement.appendChild(idInfoElement);
118 spawnerInfoElement.appendChild(descriptionInfoElement);
119 spawnerInfoElement.appendChild(versionInfoElement);
120 spawnerInfoElement.appendChild(webInfoElement);
121 spawnerInfoElement.appendChild(emailInfoElement);
122 spawnerInfoElement.appendChild(copyrightInfoElement);
123 spawnerInfoElement.appendChild(autoUpdateInfoElement);
125 kivio.documentElement().appendChild(spawnerInfoElement);
127 m_xscale = m_yscale = 20.0f;
130 TQDomElement dimensionsElement = kivio.createElement(
"Dimensions");
131 kivio.documentElement().appendChild(dimensionsElement);
134 TQDomElement svgElement = diaMain.namedItem(
"svg:svg").toElement();
135 TQDomNode svgNode = svgElement.firstChild();
136 while(!svgNode.isNull())
138 TQDomElement svgChild = svgNode.toElement();
139 if(!svgChild.isNull())
141 if(svgChild.tagName() ==
"svg:rect")
144 if(svgChild.hasAttribute(
"x") && svgChild.hasAttribute(
"y") && svgChild.hasAttribute(
"width") && svgChild.hasAttribute(
"height"))
146 calculateDimensions(svgChild.attribute(
"x").toFloat(), svgChild.attribute(
"y").toFloat());
147 calculateDimensions(svgChild.attribute(
"x").toFloat() + svgChild.attribute(
"width").toFloat(), svgChild.attribute(
"y").toFloat() + svgChild.attribute(
"height").toFloat());
150 else if(svgChild.tagName() ==
"svg:circle")
152 if(svgChild.hasAttribute(
"cx") && svgChild.hasAttribute(
"cy") && svgChild.hasAttribute(
"r"))
154 calculateDimensions((svgChild.attribute(
"cx").toFloat()) - (svgChild.attribute(
"r").toFloat()), (svgChild.attribute(
"cy").toFloat()) - (svgChild.attribute(
"r").toFloat()));
155 calculateDimensions((svgChild.attribute(
"cx").toFloat()) + (svgChild.attribute(
"r").toFloat()), (svgChild.attribute(
"cy").toFloat()) + (svgChild.attribute(
"r").toFloat()));
158 else if(svgChild.tagName() ==
"svg:ellipse")
160 if(svgChild.hasAttribute(
"cx") && svgChild.hasAttribute(
"cy") && svgChild.hasAttribute(
"rx") && svgChild.hasAttribute(
"ry"))
162 calculateDimensions((svgChild.attribute(
"cx").toFloat()) - (svgChild.attribute(
"rx").toFloat()), (svgChild.attribute(
"cy").toFloat()) - (svgChild.attribute(
"ry").toFloat()));
163 calculateDimensions((svgChild.attribute(
"cx").toFloat()) + (svgChild.attribute(
"rx").toFloat()), (svgChild.attribute(
"cy").toFloat()) + (svgChild.attribute(
"ry").toFloat()));
166 else if(svgChild.tagName() ==
"svg:line")
168 if(svgChild.hasAttribute(
"x1") && svgChild.hasAttribute(
"y1") && svgChild.hasAttribute(
"x2") && svgChild.hasAttribute(
"y2"))
170 calculateDimensions(svgChild.attribute(
"x1").toFloat(), svgChild.attribute(
"y1").toFloat());
171 calculateDimensions(svgChild.attribute(
"x2").toFloat(), svgChild.attribute(
"y2").toFloat());
174 else if(svgChild.tagName() ==
"svg:polyline")
176 if(svgChild.hasAttribute(
"points"))
178 TQStringList points = TQStringList::split(
" ", svgChild.attribute(
"points"));
179 for(TQStringList::Iterator it = points.begin(); it != points.end(); ++it)
183 TQStringList parsed = TQStringList::split(
",", (*it));
184 TQStringList::Iterator itp = parsed.begin();
189 calculateDimensions(x.toFloat(), y.toFloat());
193 else if(svgChild.tagName() ==
"svg:polygon")
195 if(svgChild.hasAttribute(
"points"))
197 TQStringList points = TQStringList::split(
" ", svgChild.attribute(
"points"));
198 for(TQStringList::Iterator it = points.begin(); it != points.end(); ++it)
202 TQStringList parsed = TQStringList::split(
",", (*it));
203 TQStringList::Iterator itp = parsed.begin();
208 calculateDimensions(x.toFloat(), y.toFloat());
212 else if(svgChild.tagName() ==
"svg:path")
214 if(svgChild.hasAttribute(
"d"))
216 DiaPointFinder *dpf =
new DiaPointFinder(&m_xlist, &m_ylist);
217 dpf->parseSVG(svgChild.attribute(
"d"),
true);
222 svgNode = svgNode.nextSibling();
225 TQValueList<float>::Iterator itx = m_xlist.begin();
226 TQValueList<float>::Iterator ity = m_ylist.begin();
234 for( ; itx != m_xlist.end(); ++itx)
236 m_highestx = TQMAX(m_highestx, *itx);
237 m_lowestx = TQMIN(m_lowestx, *itx);
240 for( ; ity != m_ylist.end(); ++ity)
242 m_highesty = TQMAX(m_highesty, *ity);
243 m_lowesty = TQMIN(m_lowesty, *ity);
254 m_yscale = 30.0/(m_highesty - m_lowesty);
255 m_xscale = 30.0/(m_highestx - m_lowestx);
259 TQDomElement connectionsElement = diaMain.namedItem(
"connections").toElement();
260 TQDomNode connectionsNode = connectionsElement.firstChild();
261 while(!connectionsNode.isNull())
263 TQDomElement connectionChild = connectionsNode.toElement();
264 if(!connectionChild.isNull())
266 if(connectionChild.tagName() ==
"point")
268 if(connectionChild.hasAttribute(
"x") && connectionChild.hasAttribute(
"y"))
270 TQDomElement kivioConnectorTarget = kivio.createElement(
"KivioConnectorTarget");
271 kivioConnectorTarget.setAttribute(
"x", TQString::number(diaPointToKivio(connectionChild.attribute(
"x").toFloat(),
true) * m_xscale));
272 kivioConnectorTarget.setAttribute(
"y", TQString::number(diaPointToKivio(connectionChild.attribute(
"y").toFloat(),
false) * m_yscale));
274 kivio.documentElement().appendChild(kivioConnectorTarget);
278 connectionsNode = connectionsNode.nextSibling();
282 svgNode = svgElement.firstChild();
284 while(!svgNode.isNull())
286 TQDomElement svgChild = svgNode.toElement();
287 if(!svgChild.isNull())
289 if(svgChild.tagName() ==
"svg:rect")
293 if(svgChild.hasAttribute(
"x") && svgChild.hasAttribute(
"y") && svgChild.hasAttribute(
"width") && svgChild.hasAttribute(
"height"))
295 TQDomElement kivioShape = kivio.createElement(
"KivioShape");
296 kivioShape.setAttribute(
"type",
"Rectangle");
297 kivioShape.setAttribute(
"name", TQString::fromLatin1(
"Element") + TQString::number(runs));
298 kivioShape.setAttribute(
"x", TQString::number(diaPointToKivio(svgChild.attribute(
"x").toFloat(),
true) * m_xscale));
299 kivioShape.setAttribute(
"y", TQString::number(diaPointToKivio(svgChild.attribute(
"y").toFloat(),
false) * m_yscale));
300 kivioShape.setAttribute(
"w", TQString::number(svgChild.attribute(
"width").toFloat() * m_xscale));
301 kivioShape.setAttribute(
"h", TQString::number(svgChild.attribute(
"height").toFloat() * m_yscale));
302 kivio.documentElement().appendChild(kivioShape);
305 else if(svgChild.tagName() ==
"svg:circle")
308 if(svgChild.hasAttribute(
"cx") && svgChild.hasAttribute(
"cy") && svgChild.hasAttribute(
"r"))
310 TQDomElement kivioShape = kivio.createElement(
"KivioShape");
311 kivioShape.setAttribute(
"type",
"Ellipse");
312 kivioShape.setAttribute(
"name", TQString::fromLatin1(
"Element") + TQString::number(runs));
313 kivioShape.setAttribute(
"x", TQString::number((diaPointToKivio(svgChild.attribute(
"cx").toFloat() - svgChild.attribute(
"r").toFloat(),
true) * m_xscale)));
314 kivioShape.setAttribute(
"y", TQString::number((diaPointToKivio(svgChild.attribute(
"cy").toFloat() - svgChild.attribute(
"r").toFloat(),
false) * m_yscale)));
315 kivioShape.setAttribute(
"w", TQString::number(svgChild.attribute(
"r").toFloat() * m_xscale * 2));
316 kivioShape.setAttribute(
"h", TQString::number(svgChild.attribute(
"r").toFloat() * m_yscale * 2));
317 kivio.documentElement().appendChild(kivioShape);
320 else if(svgChild.tagName() ==
"svg:ellipse")
323 if(svgChild.hasAttribute(
"cx") && svgChild.hasAttribute(
"cy") && svgChild.hasAttribute(
"rx") && svgChild.hasAttribute(
"ry"))
325 TQDomElement kivioShape = kivio.createElement(
"KivioShape");
326 kivioShape.setAttribute(
"type",
"Ellipse");
327 kivioShape.setAttribute(
"name", TQString::fromLatin1(
"Element") + TQString::number(runs));
328 kivioShape.setAttribute(
"x", TQString::number((diaPointToKivio(svgChild.attribute(
"cx").toFloat() - svgChild.attribute(
"rx").toFloat(),
true) * m_xscale)));
329 kivioShape.setAttribute(
"y", TQString::number((diaPointToKivio(svgChild.attribute(
"cy").toFloat() - svgChild.attribute(
"ry").toFloat(),
false) * m_yscale)));
330 kivioShape.setAttribute(
"w", TQString::number(svgChild.attribute(
"rx").toFloat() * m_xscale * 2));
331 kivioShape.setAttribute(
"h", TQString::number(svgChild.attribute(
"ry").toFloat() * m_yscale * 2));
332 kivio.documentElement().appendChild(kivioShape);
335 else if(svgChild.tagName() ==
"svg:line")
338 if(svgChild.hasAttribute(
"x1") && svgChild.hasAttribute(
"y1") && svgChild.hasAttribute(
"x2") && svgChild.hasAttribute(
"y2"))
340 TQDomElement kivioShape = kivio.createElement(
"KivioShape");
341 kivioShape.setAttribute(
"type",
"LineArray");
342 kivioShape.setAttribute(
"name", TQString::fromLatin1(
"Element") + TQString::number(runs));
344 TQDomElement lineArrayElement = kivio.createElement(
"Line");
345 lineArrayElement.setAttribute(
"x1", TQString::number(diaPointToKivio(svgChild.attribute(
"x1").toFloat(),
true) * m_xscale));
346 lineArrayElement.setAttribute(
"y1", TQString::number(diaPointToKivio(svgChild.attribute(
"y1").toFloat(),
false) * m_yscale));
347 lineArrayElement.setAttribute(
"x2", TQString::number(diaPointToKivio(svgChild.attribute(
"x2").toFloat(),
true) * m_xscale));
348 lineArrayElement.setAttribute(
"y2", TQString::number(diaPointToKivio(svgChild.attribute(
"y2").toFloat(),
false) * m_yscale));
350 kivioShape.appendChild(lineArrayElement);
351 kivio.documentElement().appendChild(kivioShape);
354 else if(svgChild.tagName() ==
"svg:polyline")
357 if(svgChild.hasAttribute(
"points"))
359 TQDomElement kivioShape = kivio.createElement(
"KivioShape");
360 kivioShape.setAttribute(
"type",
"Polyline");
361 kivioShape.setAttribute(
"name", TQString::fromLatin1(
"Element") + TQString::number(runs));
363 TQStringList points = TQStringList::split(
" ", svgChild.attribute(
"points"));
364 for(TQStringList::Iterator it = points.begin(); it != points.end(); ++it)
368 TQStringList parsed = TQStringList::split(
",", (*it));
369 TQStringList::Iterator itp = parsed.begin();
374 TQDomElement kivioPointElement = kivio.createElement(
"KivioPoint");
375 kivioPointElement.setAttribute(
"x", TQString::number(diaPointToKivio(x.toFloat(),
true) * m_xscale));
376 kivioPointElement.setAttribute(
"y", TQString::number(diaPointToKivio(y.toFloat(),
false) * m_yscale));
378 kivioShape.appendChild(kivioPointElement);
380 kivio.documentElement().appendChild(kivioShape);
383 else if(svgChild.tagName() ==
"svg:polygon")
386 if(svgChild.hasAttribute(
"points"))
388 TQDomElement kivioShape = kivio.createElement(
"KivioShape");
389 kivioShape.setAttribute(
"type",
"Polygon");
390 kivioShape.setAttribute(
"name", TQString::fromLatin1(
"Element") + TQString::number(runs));
392 TQStringList points = TQStringList::split(
" ", svgChild.attribute(
"points"));
393 for(TQStringList::Iterator it = points.begin(); it != points.end(); ++it)
397 TQStringList parsed = TQStringList::split(
",", (*it));
398 TQStringList::Iterator itp = parsed.begin();
403 TQDomElement kivioPointElement = kivio.createElement(
"KivioPoint");
404 kivioPointElement.setAttribute(
"x", TQString::number(diaPointToKivio(x.toFloat(),
true) * m_xscale));
405 kivioPointElement.setAttribute(
"y", TQString::number(diaPointToKivio(y.toFloat(),
false) * m_yscale));
407 kivioShape.appendChild(kivioPointElement);
409 kivio.documentElement().appendChild(kivioShape);
412 else if(svgChild.tagName() ==
"svg:path")
416 TQDomElement kivioShape = kivio.createElement(
"KivioShape");
417 if(svgChild.hasAttribute(
"d"))
420 if(svgChild.attribute(
"d").contains(
'z') || svgChild.attribute(
"d").contains(
'Z'))
423 kivioShape.setAttribute(
"type",
"ClosedPath");
428 kivioShape.setAttribute(
"type",
"OpenPath");
431 kivioShape.setAttribute(
"name", TQString::fromLatin1(
"Element") + TQString::number(runs));
433 DiaPathParser *dpp =
new DiaPathParser(&kivio,
434 &kivioShape, m_xscale, m_yscale,
435 m_lowestx, m_lowesty);
436 dpp->parseSVG(svgChild.attribute(
"d"),
true);
442 if( svgChild.hasAttribute(
"style"))
450 TQStringList styles = TQStringList::split(
";", svgChild.attribute(
"style"));
451 for( uint idx = 0; idx < styles.count(); idx++)
454 if( isClosed && styles[idx].contains(
"fill:"))
456 TQDomElement fillStyle = kivio.createElement(
"KivioFillStyle");
457 if( styles[idx].contains(
"forground"))
458 fillStyle.setAttribute(
"color",
"#0000");
459 else if (styles[idx].contains(
"background"))
460 fillStyle.setAttribute(
"color",
"#ffff");
462 fillStyle.setAttribute(
"colorStyle",
"1");
463 kivioShape.appendChild(fillStyle);
467 kivio.documentElement().appendChild(kivioShape);
470 svgNode = svgNode.nextSibling();
475 dimensionsElement.setAttribute(
"w", TQString::number((fabs(m_highestx - m_lowestx)) * m_xscale));
476 dimensionsElement.setAttribute(
"h", TQString::number((fabs(m_highesty - m_lowesty)) * m_yscale));
481 return loadXML(file, kivio);
484bool KivioDiaStencilSpawner::loadXML(
const TQString &file, TQDomDocument &d)
486 bool ret = m_smlStencilSpawner->loadXML(file, d);
488 m_icon = *m_smlStencilSpawner->icon();
489 m_pSet = m_smlStencilSpawner->set();
490 m_pInfo = m_smlStencilSpawner->info();
491 m_defWidth = m_smlStencilSpawner->defWidth();
492 m_defHeight = m_smlStencilSpawner->defHeight();
497KivioStencil *KivioDiaStencilSpawner::newStencil()
499 KivioStencil *newStencil = m_smlStencilSpawner->newStencil();
500 newStencil->setSpawner(
this);