-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqxmlputget.cpp
More file actions
2440 lines (2099 loc) · 70.3 KB
/
qxmlputget.cpp
File metadata and controls
2440 lines (2099 loc) · 70.3 KB
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
** **
** QXmlPutGet, classes for conveniently handling XML with Qt **
** Copyright (C) 2012 Emanuel Eichhammer **
** **
** 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 3 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, see http://www.gnu.org/licenses/. **
** **
****************************************************************************
** Author: Emanuel Eichhammer **
** Website/Contact: http://www.WorksLikeClockwork.com/ **
** Date: 05.03.12 **
****************************************************************************/
#include "qxmlputget.h"
/*! \mainpage
\section introduction Introduction
%QXmlPutGet is a library for convenient and intuitive writing and reading of XML. This is a quick
and more technical introduction to %QXmlPutGet. For a practically oriented tutorial, visit
http://www.workslikeclockwork.com/index.php/components/xml-classes-for-qt/
\section overview Overview
The library consists of two classes: writing is done by QXmlPut and reading by QXmlGet.
Both classes work on their <i>current element</i>. In QXmlPut this is the element that was
previously created (e.g. with QXmlPut::putString), in QXmlGet this is the element which was
previously navigated to (e.g. with \ref QXmlGet::find). Due to this concept, writing and
especially reading may not always happen in one single call, but multiple calls, that base on the
internal state (the <i>current element</i>) of the QXmlPut/QXmlGet instance.
In QXmlPut the navigation is tied to the creation of tags (\ref QXmlPut::putSingleTag, \ref
QXmlPut::putString, \ref QXmlPut::descend,...).
In QXmlGet the navigation is done by finding tags by name (QXmlGet::find, QXmlGet::findNext), and
possibly descending into them (\ref QXmlGet::descend, \ref QXmlGet::descended).
Both classes have functions that allow direct jumps inside the underlying QDomDocument (\ref
QXmlGet::goTo, \ref QXmlPut::goTo, \ref QXmlGet::element, \ref QXmlPut::element). Use these
functions only when necessary, as it's not the way normal linear navigation should be carried
out.
In QXmlGet, once there is a current element available, it can be accessed e.g. with \ref
QXmlGet::getString, \ref QXmlGet::getInt, \ref QXmlGet::getAttributeString etc.
In QXmlPut, the current element (which was created with \ref QXmlPut::putString("tagName",
"string content"), for example), can further be modified by setting attributes, e.g. with \ref
QXmlPut::setAttributeString.
*/
// ================================================================================
// =================== QXMLPut
// ================================================================================
/* start documentation of inline functions */
/*! \fn QDomElement QXmlPut::element() const
Returns the current element as a QDomElement. This can be used as a jump target for \ref goTo, or
some low-level manipulation of the node with the Qt-DOM-interface.
*/
/*! \fn QDomDocument QXmlPut::document() const
Returns the QDomDocument this QXmlPut instance is using.
*/
/* end documentation of inline functions */
/*!
Creates a new XML document with a root tag named \a rootTag. XML version is set to 1.0, encoding
to UTF-8 and standalone to false.
*/
QXmlPut::QXmlPut(const QString &rootTag) :
mXmlDeclaration("xml"),
mXmlVersion("1.0"),
mEncoding("UTF-8"),
mStandalone(false)
{
if (rootTag.isEmpty())
qDebug() << FUNCNAME << "root tag can't be empty";
mDocument.appendChild(mDocument.createProcessingInstruction(mXmlDeclaration, QString("version=\"%1\" encoding=\"%2\"").arg(mXmlVersion).arg(mEncoding)));
mDocument.appendChild(mDocument.createElement(rootTag));
mCurrentParent = mDocument.documentElement();
mCurrentElement = mCurrentParent;
mBarrierNode = mCurrentParent;
}
/*!
Creates a new XML document with a root tag named \a rootTag. And the specified XML parameters.
\b example:
The call
\code
QXmlPut xmlPut("myRoot", "1.0", "UTF-8", true, "myDocType", "myPublicId", "mySystemId");
\endcode
creates the following XML document:
\code
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE myDocType PUBLIC 'myPublicId' 'mySystemId'>
<myRoot>
(...)
</myRoot>
\endcode
*/
QXmlPut::QXmlPut(const QString &rootTag, const QString &xmlVersion, const QString &encoding, bool standalone, const QString &docType, const QString &publicId, const QString &systemId):
mDocument(),
mXmlDeclaration("xml"),
mXmlVersion(xmlVersion),
mEncoding(encoding),
mStandalone(standalone)
{
if (rootTag.isEmpty())
qDebug() << FUNCNAME << "rootTag can't be empty";
if (mXmlVersion.isEmpty())
qDebug() << FUNCNAME << "xmlVersion can't be empty";
if (mEncoding.isEmpty())
qDebug() << FUNCNAME << "encoding can't be empty";
if (mStandalone)
mDocument.appendChild(mDocument.createProcessingInstruction(mXmlDeclaration, QString("version=\"%1\" encoding=\"%2\" standalone=\"yes\"").arg(mXmlVersion).arg(mEncoding)));
else
mDocument.appendChild(mDocument.createProcessingInstruction(mXmlDeclaration, QString("version=\"%1\" encoding=\"%2\"").arg(mXmlVersion).arg(mEncoding)));
if (!docType.isEmpty())
mDocument.appendChild(QDomImplementation().createDocumentType(docType, publicId, systemId));
mDocument.appendChild(mDocument.createElement(rootTag));
mCurrentParent = mDocument.documentElement();
mCurrentElement = mCurrentParent;
mBarrierNode = mCurrentParent;
}
/*!
Returns an QXmlPut instance located at the current position. This can be used to read and write
to the XML document at the same time.
*/
QXmlPut::QXmlPut(const QXmlGet &xmlGet) :
mDocument(xmlGet.mDocument),
mBarrierNode(xmlGet.mBarrierNode),
mCurrentParent(xmlGet.mCurrentParent),
mCurrentElement(xmlGet.mCurrentElement),
mXmlDeclaration(xmlGet.mXmlDeclaration),
mXmlVersion(xmlGet.mXmlVersion),
mEncoding(xmlGet.mEncoding),
mStandalone(xmlGet.mStandalone)
{
}
QXmlPut::~QXmlPut()
{
}
/*!
Inserts a comment. Comments can't be read by QXmlGet and are meant for other humans reading the
XML document in a text editor.
<b>example output:</b>
\code
<!-- example comment -->
\endcode
*/
void QXmlPut::putComment(const QString &comment)
{
mCurrentParent.appendChild(mDocument.createComment(comment));
}
/*!
Inserts a single tag with name \a tagName. As with every tag, you can attach attributes to it by
subsequent calls to setAttribute(...) functions.
<b>example output:</b>
\code
<tagName>
\endcode
*/
void QXmlPut::putSingleTag(const QString &tagName)
{
QDomElement el = mDocument.createElement(tagName);
mCurrentParent.appendChild(el);
mCurrentElement = el;
}
/*!
Inserts a tag with name \a tagName which contains the string \a value. If the string contains
many special characters like "<", ">" and linebreaks, you should consider setting \a
asCDATA to true, so the string will be embedded in a CDATA-section which avoids escaping most of
the special characters and thus makes it easier for users to read/modify the resulting XML
document in a text editor.
Note that due to escaping mechanisms any content is allowed for \a value. Even if \a asCDATA is
set to true and \a value itself contains a string like "<![CDATA[trick]]>", it will be saved in
two disjoint CDATA sections, splitting the \a value string into "<![CDATA[trick]]" and ">",
preserving the resulting XML validity aswell as the saved \a value.
<b>example output:</b>
\code
<tagName>example text</tagName>
\endcode
\code
<tagName><![CDATA[example text]]></tagName>
\endcode
\see putStringList
*/
void QXmlPut::putString(const QString &tagName, const QString &value, bool asCDATA)
{
QDomElement el = mDocument.createElement(tagName);
mCurrentParent.appendChild(el);
mCurrentElement = el;
if (asCDATA)
{
int p = value.indexOf("]]>");
if (p > -1) // split into multiple CDATA sections, which separate the "]]>" occurences
{
el.appendChild(mDocument.createCDATASection(value.mid(0, p+2))); // +2 so we split between "]" and ">"
while (p > -1)
{
int nextp = value.indexOf("]]>", p+3);
if (nextp > -1)
el.appendChild(mDocument.createCDATASection(value.mid(p+2, nextp+2-(p+2))));
else
el.appendChild(mDocument.createCDATASection(value.mid(p+2)));
p = nextp;
}
} else
el.appendChild(mDocument.createCDATASection(value));
}
else
el.appendChild(mDocument.createTextNode(value));
}
/*!
Inserts a tag with name \a tagName which contains the string list \a values. If the strings
contain many special characters like "<", ">" and linebreaks, you should consider setting \a
asCDATA to true, so the strings will be embedded in a CDATA-section which avoids escaping most of
the special characters and thus makes it easier for users to read/modify the resulting XML
document in a text editor.
<b>example output:</b>
\code
<tagName>
<li>first line</li>
<li>second line</li>
<li>third line</li>
</tagName>
\endcode
\see putString
*/
void QXmlPut::putStringList(const QString &tagName, const QStringList &values, bool asCDATA)
{
descend(tagName);
for (int i=0; i<values.size(); ++i)
putString("li", values.at(i), asCDATA);
rise();
}
/*!
Inserts a tag with name \a tagName which contains the integer \a value.
<b>example output:</b>
\code
<tagName>42</tagName>
\endcode
\see putDouble, putIntVector
*/
void QXmlPut::putInt(const QString &tagName, int value)
{
QDomElement el = mDocument.createElement(tagName);
mCurrentParent.appendChild(el);
mCurrentElement = el;
el.appendChild(mDocument.createTextNode(QString::number(value)));
}
/*!
Inserts a tag with name \a tagName which contains the integer vector \a value.
<b>example output:</b>
\code
<tagName>0;1;1;2;3;5;8;13</tagName>
\endcode
\see putInt
*/
void QXmlPut::putIntVector(const QString &tagName, const QVector<int> &values)
{
QDomElement el = mDocument.createElement(tagName);
mCurrentParent.appendChild(el);
mCurrentElement = el;
el.appendChild(mDocument.createTextNode(intVectorToStr(values)));
}
/*!
Inserts a tag with name \a tagName which contains the double \a value.
<b>example output:</b>
\code
<tagName>3.1415</tagName>
\endcode
\see putInt, putDoubleVector
*/
void QXmlPut::putDouble(const QString &tagName, double value)
{
QDomElement el = mDocument.createElement(tagName);
mCurrentParent.appendChild(el);
mCurrentElement = el;
el.appendChild(mDocument.createTextNode(QString::number(value)));
}
/*!
Inserts a tag with name \a tagName which contains the double vector \a value.
<b>example output:</b>
\code
<tagName>0.1;0.24;0.52;-0.22</tagName>
\endcode
\see putDouble
*/
void QXmlPut::putDoubleVector(const QString &tagName, const QVector<double> &values)
{
QDomElement el = mDocument.createElement(tagName);
mCurrentParent.appendChild(el);
mCurrentElement = el;
el.appendChild(mDocument.createTextNode(doubleVectorToStr(values)));
}
/*!
Inserts a tag with name \a tagName which contains the bool \a value.
Depending on the specified \a format (see \ref QXmlPutGet::BoolFormat), the boolean value is
represented with different strings.
<b>example output:</b>
\code
<tagName>yes</tagName>
\endcode
\see putBoolVector
*/
void QXmlPut::putBool(const QString &tagName, bool value, QXmlPutGet::BoolFormat format)
{
QDomElement el = mDocument.createElement(tagName);
mCurrentParent.appendChild(el);
mCurrentElement = el;
el.appendChild(mDocument.createTextNode(boolToStr(value, format)));
}
/*!
Inserts a tag with name \a tagName which contains the bool vector \a value.
Depending on the specified \a format (see \ref QXmlPutGet::BoolFormat), the boolean values are
represented with different strings.
<b>example output:</b>
\code
<tagName>yes;no;no;yes;no;yes</tagName>
\endcode
\see putBool
*/
void QXmlPut::putBoolVector(const QString &tagName, const QVector<bool> &values, QXmlPutGet::BoolFormat format)
{
QDomElement el = mDocument.createElement(tagName);
mCurrentParent.appendChild(el);
mCurrentElement = el;
QString value;
el.appendChild(mDocument.createTextNode(boolVectorToStr(values, format)));
}
/*!
Inserts a tag with name \a tagName which contains the QColor \a value.
If the color contains no transparency component (i.e. alpha is 255), the color is saved in the
format "#rrggbb". If it contains alpha, it is saved as "#rrggbbaa".
<b>example output:</b>
\code
<tagName>#2280ff</tagName>
\endcode
*/
void QXmlPut::putColor(const QString &tagName, const QColor &value)
{
QDomElement el = mDocument.createElement(tagName);
mCurrentParent.appendChild(el);
mCurrentElement = el;
el.appendChild(mDocument.createTextNode(colorToStr(value)));
}
/*!
Inserts a tag with name \a tagName which contains the QSize \a value.
<b>example output:</b>
\code
<tagName width="640" height="480">
\endcode
\see putSizeF
*/
void QXmlPut::putSize(const QString &tagName, const QSize &value)
{
putSingleTag(tagName);
setAttributeInt("width", value.width());
setAttributeInt("height", value.height());
}
/*!
Inserts a tag with name \a tagName which contains the QSizeF \a value.
<b>example output:</b>
\code
<tagName width="122.4" height="10.95">
\endcode
\see putSize
*/
void QXmlPut::putSizeF(const QString &tagName, const QSizeF &value)
{
putSingleTag(tagName);
setAttributeDouble("width", value.width());
setAttributeDouble("height", value.height());
}
/*!
Inserts a tag with name \a tagName which contains the QPoint \a value.
<b>example output:</b>
\code
<tagName x="640" y="480">
\endcode
\see putPointF
*/
void QXmlPut::putPoint(const QString &tagName, const QPoint &value)
{
putSingleTag(tagName);
setAttributeInt("x", value.x());
setAttributeInt("y", value.y());
}
/*!
Inserts a tag with name \a tagName which contains the QPointF \a value.
<b>example output:</b>
\code
<tagName x="122.4" y="10.95">
\endcode
\see putPoint
*/
void QXmlPut::putPointF(const QString &tagName, const QPointF &value)
{
putSingleTag(tagName);
setAttributeDouble("x", value.x());
setAttributeDouble("y", value.y());
}
/*!
Inserts a tag with name \a tagName which contains the QRect \a value.
<b>example output:</b>
\code
<tagName left="10" top="15" width="640" height="480">
\endcode
\see putRectF
*/
void QXmlPut::putRect(const QString &tagName, const QRect &value)
{
putSingleTag(tagName);
setAttributeInt("left", value.left());
setAttributeInt("top", value.top());
setAttributeInt("width", value.width());
setAttributeInt("height", value.height());
}
/*!
Inserts a tag with name \a tagName which contains the QRectF \a value.
<b>example output:</b>
\code
<tagName left="10.5" top="14.99" width="122.4" height="10.95">
\endcode
\see putRect
*/
void QXmlPut::putRectF(const QString &tagName, const QRectF &value)
{
putSingleTag(tagName);
setAttributeDouble("left", value.left());
setAttributeDouble("top", value.top());
setAttributeDouble("width", value.width());
setAttributeDouble("height", value.height());
}
/*!
Inserts a tag with name \a tagName which contains the QDate \a value.
<b>example output:</b>
\code
<tagName>2012-04-03</tagName>
\endcode
\see putTime, putDateTime
*/
void QXmlPut::putDate(const QString &tagName, const QDate &value)
{
QDomElement el = mDocument.createElement(tagName);
mCurrentParent.appendChild(el);
mCurrentElement = el;
el.appendChild(mDocument.createTextNode(value.toString(Qt::ISODate)));
}
/*!
Inserts a tag with name \a tagName which contains the QTime \a value.
<b>example output:</b>
\code
<tagName>09:50:27</tagName>
\endcode
\see putDate, putDateTime
*/
void QXmlPut::putTime(const QString &tagName, const QTime &value)
{
QDomElement el = mDocument.createElement(tagName);
mCurrentParent.appendChild(el);
mCurrentElement = el;
el.appendChild(mDocument.createTextNode(value.toString(Qt::ISODate)));
}
/*!
Inserts a tag with name \a tagName which contains the QDateTime \a value.
<b>example output:</b>
\code
<tagName>2012-04-03T09:50:27</tagName>
\endcode
\see putDate, putTime
*/
void QXmlPut::putDateTime(const QString &tagName, const QDateTime &value)
{
QDomElement el = mDocument.createElement(tagName);
mCurrentParent.appendChild(el);
mCurrentElement = el;
el.appendChild(mDocument.createTextNode(value.toString(Qt::ISODate)));
}
/*!
Inserts a tag with name \a tagName which contains the QByteArray \a value.
The data of the QByteArray is saved in base-64. By setting \a compression to a number between 0
and 9, the data may be saved uncompressed to highly compressed.
To improve handling with text editors, every \a blockWidth characters, a linebreak is inserted
into the XML output.
<b>example output:</b>
\code
<tagName compression="9"><![CDATA[AAAPmnjatVdrUFNnGoa6u7h1KZ261k4i4
oxbnN2t0B0I4SLEdqhgUXBXuVTCZU2rLi
cBjykBBUldRi2yR7brKlVAVFQkgYOEnnB
/tk/O+bHmeR87/fN9z==
]]></tagName>
\endcode
\see putImage
*/
void QXmlPut::putByteArray(const QString &tagName, const QByteArray &value, int blockWidth, int compression)
{
QDomElement el = mDocument.createElement(tagName);
mCurrentParent.appendChild(el);
mCurrentElement = el;
compression = qBound(0, compression, 9);
QByteArray data;
if (compression > 0)
{
el.setAttribute("compression", QString::number(compression));
data = qCompress(value, compression).toBase64();
} else
data = value.toBase64();
QString newLine = "\n";
QByteArray sepData;
sepData.reserve(data.size() + (int)(data.size()/(double)blockWidth+0.5)*newLine.size());
int p = 0;
while (p < data.size())
{
int nextBlock = qMin(blockWidth, data.size()-p);
sepData.append(data.data()+p, nextBlock);
sepData.append(newLine);
p += nextBlock;
}
el.appendChild(mDocument.createCDATASection(QString(sepData)));
}
/*!
Inserts a tag with name \a tagName which contains the QImage \a value.
The data of the QImage is saved in base-64. Compression is determined depending on the \a format,
"JPEG" and "JPG" are saved uncompressed (because they are compressed already), all others are
compressed. To improve handling with text editors, every \a blockWidth characters, a linebreak is
inserted into the XML output.
Available formats typically are: BMP, GIF, JPG, JPEG, PNG, PBM, PGM, PPM, TIFF, XBM, XPM.
(see QImageReader::supportedImageFormats())
<b>example output:</b>
\code
<tagName format="PNG" compression="9"><![CDATA[AAAPmnjatVdrUFNnGoa6u7h1KZ261k4i4
oxbnN2t0B0I4SLEdqhgUXBXuVTCZU2rLi
cBjykBBUldRi2yR7brKlVAVFQkgYOEnnB
/tk/O+bHmeR87/fN9z==
]]></tagName>
\endcode
\see putImage
*/
void QXmlPut::putImage(const QString &tagName, const QImage &value, QString format, int blockWidth)
{
QByteArray data;
QBuffer buff(&data);
buff.open(QBuffer::ReadWrite);
if (!value.save(&buff, format.toUtf8().constData()))
{
qDebug() << FUNCNAME << "Couldn't write image to buffer with format" << format;
return;
}
buff.close();
QStringList noCompressFormats;
noCompressFormats << "JPG" << "JPEG";
bool compress = !noCompressFormats.contains(format.toUpper());
putByteArray(tagName, data, blockWidth, compress ? 9 : 0);
setAttributeString("format", format);
}
/*!
Inserts a tag with name \a tagName which contains the QPen \a value.
<b>example output:</b>
\code
<tagName width="2" joinstyle="0" capstyle="32" miterlimit="2" color="#5018ff" penstyle="3"/>
\endcode
\see putBrush, putFont
*/
void QXmlPut::putPen(const QString &tagName, const QPen &value)
{
putSingleTag(tagName);
setAttributeColor("color", value.color());
setAttributeInt("penstyle", value.style());
setAttributeDouble("width", value.widthF());
if (value.style() == Qt::CustomDashLine)
{
QVector<qreal> rv = value.dashPattern(); // just in case we're on ARM
QVector<double> dv(rv.size());
qCopy(rv.constBegin(), rv.constEnd(), dv.begin());
setAttributeDoubleVector("dashpattern", dv);
setAttributeDouble("dashoffset", value.dashOffset());
}
if (value.capStyle() != Qt::SquareCap)
setAttributeInt("capstyle", value.capStyle());
if (value.joinStyle() != Qt::BevelJoin)
setAttributeInt("joinstyle", value.joinStyle());
if (value.joinStyle() == Qt::MiterJoin)
setAttributeDouble("miterlimit", value.miterLimit());
}
/*!
Inserts a tag with name \a tagName which contains the QBrush \a value.
<b>example output:</b>
\code
<tagName brushstyle="14" color="#5018ff"/>
\endcode
\see putPen, putFont
*/
void QXmlPut::putBrush(const QString &tagName, const QBrush &value)
{
putSingleTag(tagName);
setAttributeColor("color", value.color());
setAttributeInt("brushstyle", value.style());
}
/*!
Inserts a tag with name \a tagName which contains the QFont \a value.
<b>example output:</b>
\code
<tagName fontdescription="Monospace,24,-1,5,75,1,0,0,0,0"/>
\endcode
\see putPen, putBrush
*/
void QXmlPut::putFont(const QString &tagName, const QFont &value)
{
putSingleTag(tagName);
setAttributeString("fontdescription", value.toString());
}
/*!
Returns a QXmlPut instance on the same Document and at the same position as this instance, but
which is restricted to this hierarchy level (and the levels below). This means, the returned
QXmlPut instance isn't allowed to \ref rise above the current hierarchy level.
This is useful if you wish that subroutines can handle their own XML work without possibly
interfering with the rest. By passing a restricted instance, it's guaranteed the subroutines
don't accidentally write/read outside their designated XML element.
If only the subroutine needs to write to/read from a specific element, consider using \ref
descended.
\b example:
\code
xmlPut.descend("toptag");
xmlPut.putString("exampleTag", "test");
writeOtherContent(xmlPut.restricted()); // A subroutine that writes to the <toptag> level
xmlPut.rise();
}
\endcode
\see descended
*/
QXmlPut QXmlPut::restricted()
{
QXmlPut result(*this);
result.mBarrierNode = result.mCurrentParent;
return result;
}
/*!
Creates a tag with the name \a tagName and descends into it. Child elements can then be created.
Once the work in the lower hierarchy level is done, you can return to the previous position in
the parent hierarchy level by calling \ref rise.
If a subroutine needs to write to/read from a specific element, consider using \ref descended
instead of a descend-rise-pair.
\b example:
\code
xmlPut.descend("toptag");
xmlPut.putString("exampleTag", "test");
xmlPut.descend("subtag");
xmlPut.putInt("answer", 42);
xmlPut.rise();
xmlPut.rise();
\endcode
\see rise, descended
*/
void QXmlPut::descend(const QString &tagName)
{
QDomElement el = mDocument.createElement(tagName);
mCurrentParent.appendChild(el);
mCurrentElement = el;
mCurrentParent = el;
}
/*!
Returns a QXmlPut instance that is descended into and restricted to the current element. Child
elements can then be created with the returned instance normally via \ref putString etc.
Due to the restriction, the returned instance can't rise above its initial hierarchy level, i.e.
into or above the hierarchy level of the instance this function is called on.
When descending into elements like this, there is no need to call \ref rise (and thus no
possibility to forget a \ref rise), because the current instance isn't influenced. Whatever
descending/rising the subroutine does with the returned instance can't break the callers XML
handling code.
\b example:
\code
writeHeaderSubroutine(xmlPut.descended("header"));
writeBodySubroutine(xmlPut.descended("body"));
writeFooterSubroutine(xmlPut.descended("footer"));
\endcode
\see restricted, descend
*/
QXmlPut QXmlPut::descended(const QString &tagName)
{
QXmlPut result(*this);
result.descend(tagName);
result.mBarrierNode = result.mCurrentParent;
mCurrentElement = result.mCurrentParent;
return result;
}
/*!
Rises to the previous position in the parent hierarchy level. This finishes the work in a lower
hierarchy level that was started with \ref descend earlier.
If a subroutine needs to write to/read from a specific element, consider using \ref descended
instead of a descend-rise-pair.
If this instance is restricted (see \ref descended and \ref restricted) and is already inside its
highest allowed hierarchy level, a further attempt to \ref rise will return false without
changing the current hierarchy level, and cause a corresponding qDebug output.
\b example:
\code
xmlPut.descend("toptag");
xmlPut.putString("exampleTag", "test");
xmlPut.descend("subtag");
xmlPut.putInt("answer", 42);
xmlPut.rise();
xmlPut.rise();
\endcode
\see descend
*/
bool QXmlPut::rise()
{
if (mCurrentParent == mBarrierNode)
{
qDebug() << FUNCNAME << "attept to rise beyond allowed node";
return false;
}
QDomNode node = mCurrentParent.parentNode();
if (!node.isNull())
{
if (node.isElement())
{
QDomElement newParent = node.toElement();
mCurrentElement = mCurrentParent;
mCurrentParent = newParent;
if (mCurrentElement.isNull())
mCurrentElement = newParent;
return true;
} else
qDebug() << FUNCNAME << "Attempt to rise into non-element node";
} else
qDebug() << FUNCNAME << "Attempt to rise above document node";
return false;
}
/*!
Changes the current parent element to \a parentElement. It must be in the current QDomDocument
already.
If this instance is restricted (see \ref descended and \ref restricted) and \a parentElement is
not inside the allowed hierarchy, this function will return false without changing the current
position, and cause a corresponding qDebug output.
You probably won't use this function very often, since normal, linear QXmlPutGet navigation
should be done with \ref descend, \ref rise, \ref descended, etc. However, it's useful
for nonlinear XML navigation, where frequent jumps between different locations in the XML
hierarchy need to be done.
\see element
*/
bool QXmlPut::goTo(QDomElement parentElement)
{
// check whether this instance is allowed to jump to the element:
QDomElement el = parentElement;
while (!el.isNull() && el != mBarrierNode)
el = el.parentNode().toElement();
if (el.isNull())
{
qDebug() << FUNCNAME << "attempt to jump outside of allowed tree";
return false;
}
// perform jump:
if (parentElement.ownerDocument() == mDocument)
{
mCurrentParent = parentElement;
mCurrentElement = parentElement.lastChildElement();
if (mCurrentElement.isNull())
mCurrentElement = parentElement;
return true;
} else
qDebug() << FUNCNAME << "Attempt to go to element not in document";
return false;
}
/*!
Adds an attribute with \a name to the current element. The attribute will carry the string \a
value.
<b>example output:</b>
\code
<tagName name="example string"/>
\endcode
*/
void QXmlPut::setAttributeString(const QString &name, const QString &value)
{
mCurrentElement.setAttribute(name, value);
}
/*!
Adds an attribute with \a name to the current element. The attribute will carry the int \a value.
<b>example output:</b>
\code
<tagName name="2"/>
\endcode
*/
void QXmlPut::setAttributeInt(const QString &name, int value)
{
mCurrentElement.setAttribute(name, QString::number(value));
}
/*!
Adds an attribute with \a name to the current element. The attribute will carry the int vector \a
value.
<b>example output:</b>
\code
<tagName name="2;4;6;8;10"/>
\endcode
*/
void QXmlPut::setAttributeIntVector(const QString &name, const QVector<int> &value)
{
mCurrentElement.setAttribute(name, intVectorToStr(value));
}
/*!
Adds an attribute with \a name to the current element. The attribute will carry the double \a
value.
<b>example output:</b>
\code
<tagName name="3.623"/>
\endcode
*/
void QXmlPut::setAttributeDouble(const QString &name, double value)
{
mCurrentElement.setAttribute(name, QString::number(value));
}
/*!
Adds an attribute with \a name to the current element. The attribute will carry the double vector
\a value.
<b>example output:</b>
\code
<tagName name="3.6;4.2;9.9"/>
\endcode
*/
void QXmlPut::setAttributeDoubleVector(const QString &name, const QVector<double> &value)
{
mCurrentElement.setAttribute(name, doubleVectorToStr(value));
}
/*!
Adds an attribute with \a name to the current element. The attribute will carry the bool \a value.
Depending on the specified \a format (see \ref QXmlPutGet::BoolFormat), the boolean value is
represented with different strings.
<b>example output:</b>
\code
<tagName name="no"/>
\endcode
*/
void QXmlPut::setAttributeBool(const QString &name, bool value, QXmlPutGet::BoolFormat format)
{
mCurrentElement.setAttribute(name, boolToStr(value, format));
}
/*!
Adds an attribute with \a name to the current element. The attribute will carry the bool vector \a value.
Depending on the specified \a format (see \ref QXmlPutGet::BoolFormat), the boolean value is
represented with different strings.
<b>example output:</b>
\code
<tagName name="no;yes;yes;no;yes"/>
\endcode
*/
void QXmlPut::setAttributeBoolVector(const QString &name, const QVector<bool> &value, QXmlPutGet::BoolFormat format)
{
mCurrentElement.setAttribute(name, boolVectorToStr(value, format));
}
/*!
Adds an attribute with \a name to the current element. The attribute will carry the QColor \a value.
If the color contains no transparency component (i.e. alpha is 255), the color is saved in the