xlsxcolor.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // xlsxcolor.cpp
  2. #include <QDataStream>
  3. #include <QXmlStreamReader>
  4. #include <QXmlStreamWriter>
  5. #include <QDebug>
  6. #include "xlsxcolor_p.h"
  7. #include "xlsxstyles_p.h"
  8. #include "xlsxutility_p.h"
  9. QT_BEGIN_NAMESPACE_XLSX
  10. XlsxColor::XlsxColor(const QColor &color)
  11. {
  12. if (color.isValid())
  13. val.setValue(color);
  14. }
  15. XlsxColor::XlsxColor(const QString &theme, const QString &tint)
  16. :val(QStringList()<<theme<<tint)
  17. {
  18. }
  19. XlsxColor::XlsxColor(int index)
  20. :val(index)
  21. {
  22. }
  23. bool XlsxColor::isRgbColor() const
  24. {
  25. return val.userType() == qMetaTypeId<QColor>() && val.value<QColor>().isValid();
  26. }
  27. bool XlsxColor::isIndexedColor() const
  28. {
  29. return val.userType() == QMetaType::Int;
  30. }
  31. bool XlsxColor::isThemeColor() const
  32. {
  33. return val.userType() == QMetaType::QStringList;
  34. }
  35. bool XlsxColor::isInvalid() const
  36. {
  37. return !val.isValid();
  38. }
  39. QColor XlsxColor::rgbColor() const
  40. {
  41. return isRgbColor() ? val.value<QColor>() : QColor();
  42. }
  43. int XlsxColor::indexedColor() const
  44. {
  45. return isIndexedColor() ? val.toInt() : -1;
  46. }
  47. QStringList XlsxColor::themeColor() const
  48. {
  49. return isThemeColor() ? val.toStringList() : QStringList();
  50. }
  51. bool XlsxColor::saveToXml(QXmlStreamWriter &writer, const QString &node) const
  52. {
  53. if (!node.isEmpty())
  54. writer.writeEmptyElement(node); //color, bgColor, fgColor
  55. else
  56. writer.writeEmptyElement(QStringLiteral("color"));
  57. if (val.userType() == qMetaTypeId<QColor>()) {
  58. writer.writeAttribute(QStringLiteral("rgb"), XlsxColor::toARGBString(val.value<QColor>()));
  59. } else if (val.userType() == QMetaType::QStringList) {
  60. QStringList themes = val.toStringList();
  61. writer.writeAttribute(QStringLiteral("theme"), themes[0]);
  62. if (!themes[1].isEmpty())
  63. writer.writeAttribute(QStringLiteral("tint"), themes[1]);
  64. } else if (val.userType() == QMetaType::Int) {
  65. writer.writeAttribute(QStringLiteral("indexed"), val.toString());
  66. } else {
  67. writer.writeAttribute(QStringLiteral("auto"), QStringLiteral("1"));
  68. }
  69. return true;
  70. }
  71. bool XlsxColor::loadFromXml(QXmlStreamReader &reader)
  72. {
  73. const auto& attributes = reader.attributes();
  74. if (attributes.hasAttribute(QLatin1String("rgb"))) {
  75. const auto& colorString = attributes.value(QLatin1String("rgb")).toString();
  76. val.setValue(fromARGBString(colorString));
  77. } else if (attributes.hasAttribute(QLatin1String("indexed"))) {
  78. int index = attributes.value(QLatin1String("indexed")).toString().toInt();
  79. val.setValue(index);
  80. } else if (attributes.hasAttribute(QLatin1String("theme"))) {
  81. const auto& theme = attributes.value(QLatin1String("theme")).toString();
  82. const auto& tint = attributes.value(QLatin1String("tint")).toString();
  83. val.setValue(QStringList()<<theme<<tint);
  84. }
  85. return true;
  86. }
  87. XlsxColor::operator QVariant() const
  88. {
  89. const auto& cref
  90. #if QT_VERSION >= 0x060000 // Qt 6.0 or over
  91. = QMetaType::fromType<XlsxColor>();
  92. #else
  93. = qMetaTypeId<XlsxColor>() ;
  94. #endif
  95. return QVariant(cref, this);
  96. }
  97. QColor XlsxColor::fromARGBString(const QString &c)
  98. {
  99. // issue #173 https://github.com/QtExcel/QXlsx/issues/173
  100. QColor color;
  101. QString strColor = "00000000";
  102. if ( c.length() == 8 )
  103. {
  104. strColor = c;
  105. }
  106. if ( c.length() == 6 )
  107. {
  108. strColor = QString("00") + c;
  109. }
  110. #if QT_VERSION >= 0x060000 // Qt 6.0 or over
  111. QString strAlpha = strColor.mid(0, 2);
  112. int alpha = strAlpha.toInt(0, 16);
  113. color.setAlpha( alpha );
  114. QString strRed = strColor.mid(2, 2);
  115. int red = strRed.toInt(0, 16);
  116. color.setRed( red );
  117. QString strGreen = strColor.mid(4, 2);
  118. int green = strGreen.toInt(0, 16);
  119. color.setGreen( green );
  120. QString strBlue = strColor.mid(6, 2);
  121. int blue = strBlue.toInt(0, 16);
  122. color.setBlue( blue );
  123. #else
  124. QStringRef strAlpha = strColor.midRef(0, 2);
  125. int alpha = strAlpha.toInt(0, 16);
  126. color.setAlpha( alpha );
  127. QStringRef strRed = strColor.midRef(2, 2);
  128. int red = strRed.toInt(0, 16);
  129. color.setRed( red );
  130. QStringRef strGreen = strColor.midRef(4, 2);
  131. int green = strGreen.toInt(0, 16);
  132. color.setGreen( green );
  133. QStringRef strBlue = strColor.midRef(6, 2);
  134. int blue = strBlue.toInt(0, 16);
  135. color.setBlue( blue );
  136. #endif
  137. return color;
  138. }
  139. QString XlsxColor::toARGBString(const QColor &c)
  140. {
  141. #if QT_VERSION >= 0x050600 // Qt 5.6 or over
  142. return QString::asprintf("%02X%02X%02X%02X", c.alpha(), c.red(), c.green(), c.blue());
  143. #else
  144. QString color;
  145. color.sprintf("%02X%02X%02X%02X", c.alpha(), c.red(), c.green(), c.blue());
  146. return color;
  147. #endif
  148. }
  149. #if !defined(QT_NO_DATASTREAM)
  150. QDataStream &operator<<(QDataStream &s, const XlsxColor &color)
  151. {
  152. if (color.isInvalid())
  153. s<<0;
  154. else if (color.isRgbColor())
  155. s<<1<<color.rgbColor();
  156. else if (color.isIndexedColor())
  157. s<<2<<color.indexedColor();
  158. else if (color.isThemeColor())
  159. s<<3<<color.themeColor();
  160. else
  161. s<<4;
  162. return s;
  163. }
  164. QDataStream &operator>>(QDataStream &s, XlsxColor &color)
  165. {
  166. int marker(4);
  167. s>>marker;
  168. if (marker == 0) {
  169. color = XlsxColor();
  170. } else if (marker == 1) {
  171. QColor c;
  172. s>>c;
  173. color = XlsxColor(c);
  174. } else if (marker == 2) {
  175. int indexed;
  176. s>>indexed;
  177. color = XlsxColor(indexed);
  178. } else if (marker == 3) {
  179. QStringList list;
  180. s>>list;
  181. color = XlsxColor(list[0], list[1]);
  182. }
  183. return s;
  184. }
  185. #endif
  186. #ifndef QT_NO_DEBUG_STREAM
  187. QDebug operator<<(QDebug dbg, const XlsxColor &c)
  188. {
  189. if (c.isInvalid())
  190. dbg.nospace() << "XlsxColor(invalid)";
  191. else if (c.isRgbColor())
  192. dbg.nospace() << c.rgbColor();
  193. else if (c.isIndexedColor())
  194. dbg.nospace() << "XlsxColor(indexed," << c.indexedColor() << ")";
  195. else if (c.isThemeColor())
  196. dbg.nospace() << "XlsxColor(theme," << c.themeColor().join(QLatin1String(":")) << ")";
  197. return dbg.space();
  198. }
  199. #endif
  200. QT_END_NAMESPACE_XLSX