1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
|
/*
* Kivio - Visual Modelling and Flowcharting
* Copyright (C) 2001 Nikolas Zimmermann <wildfox@kde.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <tqdom.h>
#include <tqfile.h>
#include <tqregexp.h>
#include <tqstringlist.h>
#include <kdebug.h>
#include <math.h>
#include "kivio_stencil.h"
#include "kivio_dia_stencil_spawner.h"
#include "diapathparser.h"
KivioDiaStencilSpawner::KivioDiaStencilSpawner(KivioStencilSpawnerSet *p) : KivioStencilSpawner(p)
{
m_smlStencilSpawner = new KivioSMLStencilSpawner(p);
}
KivioDiaStencilSpawner::~KivioDiaStencilSpawner()
{
}
TQDomElement KivioDiaStencilSpawner::saveXML(TQDomDocument &d)
{
return m_smlStencilSpawner->saveXML(d);
}
void KivioDiaStencilSpawner::calculateDimensions(float x, float y)
{
m_xlist.append(x);
m_ylist.append(y);
}
float KivioDiaStencilSpawner::diaPointToKivio(float point, bool xpoint)
{
float returnPoint = 0.0;
if(xpoint)
{
//if(m_lowestx < 0)
returnPoint = point - m_lowestx;
//else
// returnPoint = (fabs(m_highestx) - fabs(m_lowestx)) - (fabs(m_highestx) + fabs(point));
}
else
{
//if(m_lowesty < 0)
returnPoint =point - m_lowesty;
//else
// returnPoint = (fabs(m_highesty) + fabs(m_lowesty)) - (fabs(m_highesty) + fabs(point));
}
//kdDebug () << "Point " << point << " Return point " << returnPoint << endl;
return returnPoint;
}
bool KivioDiaStencilSpawner::load(const TQString &file)
{
TQDomDocument dia("test");
TQDomDocument kivio("XML");
m_filename = file;
TQFile f(file);
if(f.open(IO_ReadOnly) == false)
{
kdDebug(43000) << "KivioDiaStencilSpawner::load() - Error opening stencil: " << file << endl;
return false;
}
dia.setContent(&f);
TQDomNode diaMain = dia.namedItem("shape");
// Set "creator" attribute
TQDomElement firstElement = kivio.createElement("KivioShapeStencil");
firstElement.setAttribute("creator", "kiviodiafilter");
kivio.appendChild(firstElement);
// Add KivioSMLStencilSpawnerInfo
TQDomElement spawnerInfoElement = kivio.createElement("KivioSMLStencilSpawnerInfo");
TQDomElement authorInfoElement = kivio.createElement("Author");
authorInfoElement.setAttribute("data", "n/a");
TQDomElement titleInfoElement = kivio.createElement("Title");
titleInfoElement.setAttribute("data", diaMain.namedItem("name").toElement().text());
TQDomElement idInfoElement = kivio.createElement("Id");
idInfoElement.setAttribute("data", diaMain.namedItem("name").toElement().text());
TQDomElement descriptionInfoElement = kivio.createElement("Description");
descriptionInfoElement.setAttribute("data", diaMain.namedItem("description").toElement().text());
TQDomElement versionInfoElement = kivio.createElement("Version");
versionInfoElement.setAttribute("data", "1.0");
TQDomElement webInfoElement = kivio.createElement("Web");
webInfoElement.setAttribute("data", "http://");
TQDomElement emailInfoElement = kivio.createElement("Email");
emailInfoElement.setAttribute("data", "n/a");
TQDomElement copyrightInfoElement = kivio.createElement("Copyright");
copyrightInfoElement.setAttribute("data", "n/a");
TQDomElement autoUpdateInfoElement = kivio.createElement("AutoUpdate");
autoUpdateInfoElement.setAttribute("data", "off");
spawnerInfoElement.appendChild(authorInfoElement);
spawnerInfoElement.appendChild(titleInfoElement);
spawnerInfoElement.appendChild(idInfoElement);
spawnerInfoElement.appendChild(descriptionInfoElement);
spawnerInfoElement.appendChild(versionInfoElement);
spawnerInfoElement.appendChild(webInfoElement);
spawnerInfoElement.appendChild(emailInfoElement);
spawnerInfoElement.appendChild(copyrightInfoElement);
spawnerInfoElement.appendChild(autoUpdateInfoElement);
kivio.documentElement().appendChild(spawnerInfoElement);
m_xscale = m_yscale = 20.0f;
// Add Dimensions
TQDomElement dimensionsElement = kivio.createElement("Dimensions");
kivio.documentElement().appendChild(dimensionsElement);
// Calculate Dimensions
TQDomElement svgElement = diaMain.namedItem("svg:svg").toElement();
TQDomNode svgNode = svgElement.firstChild();
while(!svgNode.isNull())
{
TQDomElement svgChild = svgNode.toElement();
if(!svgChild.isNull())
{
if(svgChild.tagName() == "svg:rect")
{
// TODO: rx and ry -> rounded rects
if(svgChild.hasAttribute("x") && svgChild.hasAttribute("y") && svgChild.hasAttribute("width") && svgChild.hasAttribute("height"))
{
calculateDimensions(svgChild.attribute("x").toFloat(), svgChild.attribute("y").toFloat());
calculateDimensions(svgChild.attribute("x").toFloat() + svgChild.attribute("width").toFloat(), svgChild.attribute("y").toFloat() + svgChild.attribute("height").toFloat());
}
}
else if(svgChild.tagName() == "svg:circle")
{
if(svgChild.hasAttribute("cx") && svgChild.hasAttribute("cy") && svgChild.hasAttribute("r"))
{
calculateDimensions((svgChild.attribute("cx").toFloat()) - (svgChild.attribute("r").toFloat()), (svgChild.attribute("cy").toFloat()) - (svgChild.attribute("r").toFloat()));
calculateDimensions((svgChild.attribute("cx").toFloat()) + (svgChild.attribute("r").toFloat()), (svgChild.attribute("cy").toFloat()) + (svgChild.attribute("r").toFloat()));
}
}
else if(svgChild.tagName() == "svg:ellipse")
{
if(svgChild.hasAttribute("cx") && svgChild.hasAttribute("cy") && svgChild.hasAttribute("rx") && svgChild.hasAttribute("ry"))
{
calculateDimensions((svgChild.attribute("cx").toFloat()) - (svgChild.attribute("rx").toFloat()), (svgChild.attribute("cy").toFloat()) - (svgChild.attribute("ry").toFloat()));
calculateDimensions((svgChild.attribute("cx").toFloat()) + (svgChild.attribute("rx").toFloat()), (svgChild.attribute("cy").toFloat()) + (svgChild.attribute("ry").toFloat()));
}
}
else if(svgChild.tagName() == "svg:line")
{
if(svgChild.hasAttribute("x1") && svgChild.hasAttribute("y1") && svgChild.hasAttribute("x2") && svgChild.hasAttribute("y2"))
{
calculateDimensions(svgChild.attribute("x1").toFloat(), svgChild.attribute("y1").toFloat());
calculateDimensions(svgChild.attribute("x2").toFloat(), svgChild.attribute("y2").toFloat());
}
}
else if(svgChild.tagName() == "svg:polyline")
{
if(svgChild.hasAttribute("points"))
{
TQStringList points = TQStringList::split(" ", svgChild.attribute("points"));
for(TQStringList::Iterator it = points.begin(); it != points.end(); ++it)
{
TQString x, y;
TQStringList parsed = TQStringList::split(",", (*it));
TQStringList::Iterator itp = parsed.begin();
x = (*itp);
++itp;
y = (*itp);
calculateDimensions(x.toFloat(), y.toFloat());
}
}
}
else if(svgChild.tagName() == "svg:polygon")
{
if(svgChild.hasAttribute("points"))
{
TQStringList points = TQStringList::split(" ", svgChild.attribute("points"));
for(TQStringList::Iterator it = points.begin(); it != points.end(); ++it)
{
TQString x, y;
TQStringList parsed = TQStringList::split(",", (*it));
TQStringList::Iterator itp = parsed.begin();
x = (*itp);
++itp;
y = (*itp);
calculateDimensions(x.toFloat(), y.toFloat());
}
}
}
else if(svgChild.tagName() == "svg:path")
{
if(svgChild.hasAttribute("d"))
{
DiaPointFinder *dpf = new DiaPointFinder(&m_xlist, &m_ylist);
dpf->parseSVG(svgChild.attribute("d"), true);
delete dpf;
}
}
}
svgNode = svgNode.nextSibling();
}
TQValueList<float>::Iterator itx = m_xlist.begin();
TQValueList<float>::Iterator ity = m_ylist.begin();
m_highestx = *itx;
m_lowestx = *itx;
m_highesty = *ity;
m_lowesty = *ity;
++itx;
++ity;
for( ; itx != m_xlist.end(); ++itx)
{
m_highestx = TQMAX(m_highestx, *itx);
m_lowestx = TQMIN(m_lowestx, *itx);
}
for( ; ity != m_ylist.end(); ++ity)
{
m_highesty = TQMAX(m_highesty, *ity);
m_lowesty = TQMIN(m_lowesty, *ity);
}
//if( svgElement.hasAttribute("width") && svgElement.hasAttribute("height"))
//{
// m_yscale = svgElement.attribute("height").toFloat()/(m_highesty - m_lowesty);
// m_xscale = svgElement.attribute("width").toFloat()/(m_highestx - m_lowestx);
//}
//else
{
// scale the shape to be close to 30 by 30
m_yscale = 30.0/(m_highesty - m_lowesty);
m_xscale = 30.0/(m_highestx - m_lowestx);
}
// Add KivioConnectorTarget's
TQDomElement connectionsElement = diaMain.namedItem("connections").toElement();
TQDomNode connectionsNode = connectionsElement.firstChild();
while(!connectionsNode.isNull())
{
TQDomElement connectionChild = connectionsNode.toElement();
if(!connectionChild.isNull())
{
if(connectionChild.tagName() == "point")
{
if(connectionChild.hasAttribute("x") && connectionChild.hasAttribute("y"))
{
TQDomElement kivioConnectorTarget = kivio.createElement("KivioConnectorTarget");
kivioConnectorTarget.setAttribute("x", TQString::number(diaPointToKivio(connectionChild.attribute("x").toFloat(),true) * m_xscale));
kivioConnectorTarget.setAttribute("y", TQString::number(diaPointToKivio(connectionChild.attribute("y").toFloat(), false) * m_yscale));
kivio.documentElement().appendChild(kivioConnectorTarget);
}
}
}
connectionsNode = connectionsNode.nextSibling();
}
// Add KivioShape's and convert to Kivio's Coordinate System
svgNode = svgElement.firstChild();
int runs = 0;
while(!svgNode.isNull())
{
TQDomElement svgChild = svgNode.toElement();
if(!svgChild.isNull())
{
if(svgChild.tagName() == "svg:rect")
{
runs++;
// TODO: rx and ry -> rounded rects
if(svgChild.hasAttribute("x") && svgChild.hasAttribute("y") && svgChild.hasAttribute("width") && svgChild.hasAttribute("height"))
{
TQDomElement kivioShape = kivio.createElement("KivioShape");
kivioShape.setAttribute("type", "Rectangle");
kivioShape.setAttribute("name", TQString::fromLatin1("Element") + TQString::number(runs));
kivioShape.setAttribute("x", TQString::number(diaPointToKivio(svgChild.attribute("x").toFloat(),true) * m_xscale));
kivioShape.setAttribute("y", TQString::number(diaPointToKivio(svgChild.attribute("y").toFloat(), false) * m_yscale));
kivioShape.setAttribute("w", TQString::number(svgChild.attribute("width").toFloat() * m_xscale));
kivioShape.setAttribute("h", TQString::number(svgChild.attribute("height").toFloat() * m_yscale));
kivio.documentElement().appendChild(kivioShape);
}
}
else if(svgChild.tagName() == "svg:circle")
{
runs++;
if(svgChild.hasAttribute("cx") && svgChild.hasAttribute("cy") && svgChild.hasAttribute("r"))
{
TQDomElement kivioShape = kivio.createElement("KivioShape");
kivioShape.setAttribute("type", "Ellipse");
kivioShape.setAttribute("name", TQString::fromLatin1("Element") + TQString::number(runs));
kivioShape.setAttribute("x", TQString::number((diaPointToKivio(svgChild.attribute("cx").toFloat() - svgChild.attribute("r").toFloat(),true) * m_xscale)));
kivioShape.setAttribute("y", TQString::number((diaPointToKivio(svgChild.attribute("cy").toFloat() - svgChild.attribute("r").toFloat(), false) * m_yscale)));
kivioShape.setAttribute("w", TQString::number(svgChild.attribute("r").toFloat() * m_xscale * 2));
kivioShape.setAttribute("h", TQString::number(svgChild.attribute("r").toFloat() * m_yscale * 2));
kivio.documentElement().appendChild(kivioShape);
}
}
else if(svgChild.tagName() == "svg:ellipse")
{
runs++;
if(svgChild.hasAttribute("cx") && svgChild.hasAttribute("cy") && svgChild.hasAttribute("rx") && svgChild.hasAttribute("ry"))
{
TQDomElement kivioShape = kivio.createElement("KivioShape");
kivioShape.setAttribute("type", "Ellipse");
kivioShape.setAttribute("name", TQString::fromLatin1("Element") + TQString::number(runs));
kivioShape.setAttribute("x", TQString::number((diaPointToKivio(svgChild.attribute("cx").toFloat() - svgChild.attribute("rx").toFloat(),true) * m_xscale)));
kivioShape.setAttribute("y", TQString::number((diaPointToKivio(svgChild.attribute("cy").toFloat() - svgChild.attribute("ry").toFloat(), false) * m_yscale)));
kivioShape.setAttribute("w", TQString::number(svgChild.attribute("rx").toFloat() * m_xscale * 2));
kivioShape.setAttribute("h", TQString::number(svgChild.attribute("ry").toFloat() * m_yscale * 2));
kivio.documentElement().appendChild(kivioShape);
}
}
else if(svgChild.tagName() == "svg:line")
{
runs++;
if(svgChild.hasAttribute("x1") && svgChild.hasAttribute("y1") && svgChild.hasAttribute("x2") && svgChild.hasAttribute("y2"))
{
TQDomElement kivioShape = kivio.createElement("KivioShape");
kivioShape.setAttribute("type", "LineArray");
kivioShape.setAttribute("name", TQString::fromLatin1("Element") + TQString::number(runs));
TQDomElement lineArrayElement = kivio.createElement("Line");
lineArrayElement.setAttribute("x1", TQString::number(diaPointToKivio(svgChild.attribute("x1").toFloat(),true) * m_xscale));
lineArrayElement.setAttribute("y1", TQString::number(diaPointToKivio(svgChild.attribute("y1").toFloat(), false) * m_yscale));
lineArrayElement.setAttribute("x2", TQString::number(diaPointToKivio(svgChild.attribute("x2").toFloat(),true) * m_xscale));
lineArrayElement.setAttribute("y2", TQString::number(diaPointToKivio(svgChild.attribute("y2").toFloat(), false) * m_yscale));
kivioShape.appendChild(lineArrayElement);
kivio.documentElement().appendChild(kivioShape);
}
}
else if(svgChild.tagName() == "svg:polyline")
{
runs++;
if(svgChild.hasAttribute("points"))
{
TQDomElement kivioShape = kivio.createElement("KivioShape");
kivioShape.setAttribute("type", "Polyline");
kivioShape.setAttribute("name", TQString::fromLatin1("Element") + TQString::number(runs));
TQStringList points = TQStringList::split(" ", svgChild.attribute("points"));
for(TQStringList::Iterator it = points.begin(); it != points.end(); ++it)
{
TQString x, y;
TQStringList parsed = TQStringList::split(",", (*it));
TQStringList::Iterator itp = parsed.begin();
x = (*itp);
++itp;
y = (*itp);
TQDomElement kivioPointElement = kivio.createElement("KivioPoint");
kivioPointElement.setAttribute("x", TQString::number(diaPointToKivio(x.toFloat(),true) * m_xscale));
kivioPointElement.setAttribute("y", TQString::number(diaPointToKivio(y.toFloat(), false) * m_yscale));
kivioShape.appendChild(kivioPointElement);
}
kivio.documentElement().appendChild(kivioShape);
}
}
else if(svgChild.tagName() == "svg:polygon")
{
runs++;
if(svgChild.hasAttribute("points"))
{
TQDomElement kivioShape = kivio.createElement("KivioShape");
kivioShape.setAttribute("type", "Polygon");
kivioShape.setAttribute("name", TQString::fromLatin1("Element") + TQString::number(runs));
TQStringList points = TQStringList::split(" ", svgChild.attribute("points"));
for(TQStringList::Iterator it = points.begin(); it != points.end(); ++it)
{
TQString x, y;
TQStringList parsed = TQStringList::split(",", (*it));
TQStringList::Iterator itp = parsed.begin();
x = (*itp);
++itp;
y = (*itp);
TQDomElement kivioPointElement = kivio.createElement("KivioPoint");
kivioPointElement.setAttribute("x", TQString::number(diaPointToKivio(x.toFloat(),true) * m_xscale));
kivioPointElement.setAttribute("y", TQString::number(diaPointToKivio(y.toFloat(), false) * m_yscale));
kivioShape.appendChild(kivioPointElement);
}
kivio.documentElement().appendChild(kivioShape);
}
}
else if(svgChild.tagName() == "svg:path")
{
runs++;
bool isClosed;
TQDomElement kivioShape = kivio.createElement("KivioShape");
if(svgChild.hasAttribute("d"))
{
if(svgChild.attribute("d").contains('z') || svgChild.attribute("d").contains('Z'))
{
isClosed = true;
kivioShape.setAttribute("type", "ClosedPath");
}
else
{
isClosed = false;
kivioShape.setAttribute("type", "OpenPath");
}
kivioShape.setAttribute("name", TQString::fromLatin1("Element") + TQString::number(runs));
DiaPathParser *dpp = new DiaPathParser(&kivio,
&kivioShape, m_xscale, m_yscale,
m_lowestx, m_lowesty);
dpp->parseSVG(svgChild.attribute("d"), true);
delete dpp;
}
if( svgChild.hasAttribute("style"))
{
// style="stroke: background; stroke-width: 0.8; stroke-miterlimit: 1; stroke-linecap: round; stroke-linejoin: round"
// Supported:
// stroke-width:
// stroke-linejoin: milter, bevel, round
// stroke-linecap: round, square, flat
// fill: ?
TQStringList styles = TQStringList::split(";", svgChild.attribute("style"));
for( uint idx = 0; idx < styles.count(); idx++)
{
//kdDebug(43000) << "Style: " << styles[idx] << endl;
if( isClosed && styles[idx].contains("fill:"))
{
TQDomElement fillStyle = kivio.createElement("KivioFillStyle");
if( styles[idx].contains("forground"))
fillStyle.setAttribute("color", "#0000");
else if (styles[idx].contains("background"))
fillStyle.setAttribute("color", "#ffff");
fillStyle.setAttribute("colorStyle", "1");
kivioShape.appendChild(fillStyle);
}
}
}
kivio.documentElement().appendChild(kivioShape);
}
}
svgNode = svgNode.nextSibling();
}
// Apply width and height
dimensionsElement.setAttribute("w", TQString::number((fabs(m_highestx - m_lowestx)) * m_xscale));
dimensionsElement.setAttribute("h", TQString::number((fabs(m_highesty - m_lowesty)) * m_yscale));
m_xlist.clear();
m_ylist.clear();
return loadXML(file, kivio);
}
bool KivioDiaStencilSpawner::loadXML(const TQString &file, TQDomDocument &d)
{
bool ret = m_smlStencilSpawner->loadXML(file, d);
m_icon = *m_smlStencilSpawner->icon();
m_pSet = m_smlStencilSpawner->set();
m_pInfo = m_smlStencilSpawner->info();
m_defWidth = m_smlStencilSpawner->defWidth();
m_defHeight = m_smlStencilSpawner->defHeight();
return ret;
}
KivioStencil *KivioDiaStencilSpawner::newStencil()
{
KivioStencil *newStencil = m_smlStencilSpawner->newStencil();
newStencil->setSpawner(this);
return newStencil;
}
|