comparison src/xlsx/xlsxcolor.cpp @ 1:93d3106bb9a4

Add qt xlsx library
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 22 Mar 2016 10:38:08 +0100
parents
children
comparison
equal deleted inserted replaced
0:49cd5cc0b072 1:93d3106bb9a4
1 #include "xlsxcolor_p.h"
2 #include "xlsxstyles_p.h"
3 #include "xlsxutility_p.h"
4
5 #include <QDataStream>
6 #include <QXmlStreamReader>
7 #include <QXmlStreamWriter>
8 #include <QDebug>
9
10 namespace QXlsx {
11
12
13 XlsxColor::XlsxColor(const QColor &color)
14 {
15 if (color.isValid())
16 val.setValue(color);
17 }
18
19 XlsxColor::XlsxColor(const QString &theme, const QString &tint)
20 :val(QStringList()<<theme<<tint)
21 {
22
23 }
24
25 XlsxColor::XlsxColor(int index)
26 :val(index)
27 {
28
29 }
30
31 bool XlsxColor::isRgbColor() const
32 {
33 if (val.userType() == qMetaTypeId<QColor>() && val.value<QColor>().isValid())
34 return true;
35 return false;
36 }
37
38 bool XlsxColor::isIndexedColor() const
39 {
40 return val.userType() == QMetaType::Int;
41 }
42
43 bool XlsxColor::isThemeColor() const
44 {
45 return val.userType() == QMetaType::QStringList;
46 }
47
48 bool XlsxColor::isInvalid() const
49 {
50 return !val.isValid();
51 }
52
53 QColor XlsxColor::rgbColor() const
54 {
55 if (isRgbColor())
56 return val.value<QColor>();
57 return QColor();
58 }
59
60 int XlsxColor::indexedColor() const
61 {
62 if (isIndexedColor())
63 return val.toInt();
64 return -1;
65 }
66
67 QStringList XlsxColor::themeColor() const
68 {
69 if (isThemeColor())
70 return val.toStringList();
71 return QStringList();
72 }
73
74 bool XlsxColor::saveToXml(QXmlStreamWriter &writer, const QString &node) const
75 {
76 if (!node.isEmpty())
77 writer.writeEmptyElement(node); //color, bgColor, fgColor
78 else
79 writer.writeEmptyElement(QStringLiteral("color"));
80
81 if (val.userType() == qMetaTypeId<QColor>()) {
82 writer.writeAttribute(QStringLiteral("rgb"), XlsxColor::toARGBString(val.value<QColor>()));
83 } else if (val.userType() == QMetaType::QStringList) {
84 QStringList themes = val.toStringList();
85 writer.writeAttribute(QStringLiteral("theme"), themes[0]);
86 if (!themes[1].isEmpty())
87 writer.writeAttribute(QStringLiteral("tint"), themes[1]);
88 } else if (val.userType() == QMetaType::Int) {
89 writer.writeAttribute(QStringLiteral("indexed"), val.toString());
90 } else {
91 writer.writeAttribute(QStringLiteral("auto"), QStringLiteral("1"));
92 }
93
94 return true;
95 }
96
97 bool XlsxColor::loadFromXml(QXmlStreamReader &reader)
98 {
99 QXmlStreamAttributes attributes = reader.attributes();
100
101 if (attributes.hasAttribute(QLatin1String("rgb"))) {
102 QString colorString = attributes.value(QLatin1String("rgb")).toString();
103 val.setValue(fromARGBString(colorString));
104 } else if (attributes.hasAttribute(QLatin1String("indexed"))) {
105 int index = attributes.value(QLatin1String("indexed")).toString().toInt();
106 val.setValue(index);
107 } else if (attributes.hasAttribute(QLatin1String("theme"))) {
108 QString theme = attributes.value(QLatin1String("theme")).toString();
109 QString tint = attributes.value(QLatin1String("tint")).toString();
110 val.setValue(QStringList()<<theme<<tint);
111 }
112 return true;
113 }
114
115 XlsxColor::operator QVariant() const
116 {
117 return QVariant(qMetaTypeId<XlsxColor>(), this);
118 }
119
120
121 QColor XlsxColor::fromARGBString(const QString &c)
122 {
123 Q_ASSERT(c.length() == 8);
124 QColor color;
125 color.setAlpha(c.mid(0, 2).toInt(0, 16));
126 color.setRed(c.mid(2, 2).toInt(0, 16));
127 color.setGreen(c.mid(4, 2).toInt(0, 16));
128 color.setBlue(c.mid(6, 2).toInt(0, 16));
129 return color;
130 }
131
132 QString XlsxColor::toARGBString(const QColor &c)
133 {
134 QString color;
135 color.sprintf("%02X%02X%02X%02X", c.alpha(), c.red(), c.green(), c.blue());
136 return color;
137 }
138
139 #if !defined(QT_NO_DATASTREAM)
140 QDataStream &operator<<(QDataStream &s, const XlsxColor &color)
141 {
142 if (color.isInvalid())
143 s<<0;
144 else if (color.isRgbColor())
145 s<<1<<color.rgbColor();
146 else if (color.isIndexedColor())
147 s<<2<<color.indexedColor();
148 else if (color.isThemeColor())
149 s<<3<<color.themeColor();
150 else
151 s<<4;
152
153 return s;
154 }
155
156 QDataStream &operator>>(QDataStream &s, XlsxColor &color)
157 {
158 int marker(4);
159 s>>marker;
160 if (marker == 0) {
161 color = XlsxColor();
162 } else if (marker == 1) {
163 QColor c;
164 s>>c;
165 color = XlsxColor(c);
166 } else if (marker == 2) {
167 int indexed;
168 s>>indexed;
169 color = XlsxColor(indexed);
170 } else if (marker == 3) {
171 QStringList list;
172 s>>list;
173 color = XlsxColor(list[0], list[1]);
174 }
175
176 return s;
177 }
178
179 #endif
180
181 #ifndef QT_NO_DEBUG_STREAM
182 QDebug operator<<(QDebug dbg, const XlsxColor &c)
183 {
184 if (c.isInvalid())
185 dbg.nospace() << "XlsxColor(invalid)";
186 else if (c.isRgbColor())
187 dbg.nospace() << c.rgbColor();
188 else if (c.isIndexedColor())
189 dbg.nospace() << "XlsxColor(indexed," << c.indexedColor() << ")";
190 else if (c.isThemeColor())
191 dbg.nospace() << "XlsxColor(theme," << c.themeColor().join(QLatin1Char(':')) << ")";
192
193 return dbg.space();
194 }
195
196 #endif
197
198 } // namespace QXlsx
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)