xlsxcolor.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #include "xlsxcolor_p.h"
  2. #include "xlsxstyles_p.h"
  3. #include "xlsxutility_p.h"
  4. #include <QDataStream>
  5. #include <QXmlStreamReader>
  6. #include <QXmlStreamWriter>
  7. #include <QDebug>
  8. namespace QXlsx {
  9. XlsxColor::XlsxColor(const QColor &color)
  10. {
  11. if (color.isValid())
  12. val.setValue(color);
  13. }
  14. XlsxColor::XlsxColor(const QString &theme, const QString &tint)
  15. :val(QStringList()<<theme<<tint)
  16. {
  17. }
  18. XlsxColor::XlsxColor(int index)
  19. :val(index)
  20. {
  21. }
  22. bool XlsxColor::isRgbColor() const
  23. {
  24. if (val.userType() == qMetaTypeId<QColor>() && val.value<QColor>().isValid())
  25. return true;
  26. return false;
  27. }
  28. bool XlsxColor::isIndexedColor() const
  29. {
  30. return val.userType() == QMetaType::Int;
  31. }
  32. bool XlsxColor::isThemeColor() const
  33. {
  34. return val.userType() == QMetaType::QStringList;
  35. }
  36. bool XlsxColor::isInvalid() const
  37. {
  38. return !val.isValid();
  39. }
  40. QColor XlsxColor::rgbColor() const
  41. {
  42. if (isRgbColor())
  43. return val.value<QColor>();
  44. return QColor();
  45. }
  46. int XlsxColor::indexedColor() const
  47. {
  48. if (isIndexedColor())
  49. return val.toInt();
  50. return -1;
  51. }
  52. QStringList XlsxColor::themeColor() const
  53. {
  54. if (isThemeColor())
  55. return val.toStringList();
  56. return QStringList();
  57. }
  58. bool XlsxColor::saveToXml(QXmlStreamWriter &writer, const QString &node) const
  59. {
  60. if (!node.isEmpty())
  61. writer.writeEmptyElement(node); //color, bgColor, fgColor
  62. else
  63. writer.writeEmptyElement(QStringLiteral("color"));
  64. if (val.userType() == qMetaTypeId<QColor>()) {
  65. writer.writeAttribute(QStringLiteral("rgb"), XlsxColor::toARGBString(val.value<QColor>()));
  66. } else if (val.userType() == QMetaType::QStringList) {
  67. QStringList themes = val.toStringList();
  68. writer.writeAttribute(QStringLiteral("theme"), themes[0]);
  69. if (!themes[1].isEmpty())
  70. writer.writeAttribute(QStringLiteral("tint"), themes[1]);
  71. } else if (val.userType() == QMetaType::Int) {
  72. writer.writeAttribute(QStringLiteral("indexed"), val.toString());
  73. } else {
  74. writer.writeAttribute(QStringLiteral("auto"), QStringLiteral("1"));
  75. }
  76. return true;
  77. }
  78. bool XlsxColor::loadFromXml(QXmlStreamReader &reader)
  79. {
  80. QXmlStreamAttributes attributes = reader.attributes();
  81. if (attributes.hasAttribute(QLatin1String("rgb"))) {
  82. QString colorString = attributes.value(QLatin1String("rgb")).toString();
  83. val.setValue(fromARGBString(colorString));
  84. } else if (attributes.hasAttribute(QLatin1String("indexed"))) {
  85. int index = attributes.value(QLatin1String("indexed")).toString().toInt();
  86. val.setValue(index);
  87. } else if (attributes.hasAttribute(QLatin1String("theme"))) {
  88. QString theme = attributes.value(QLatin1String("theme")).toString();
  89. QString tint = attributes.value(QLatin1String("tint")).toString();
  90. val.setValue(QStringList()<<theme<<tint);
  91. }
  92. return true;
  93. }
  94. XlsxColor::operator QVariant() const
  95. {
  96. return QVariant(qMetaTypeId<XlsxColor>(), this);
  97. }
  98. QColor XlsxColor::fromARGBString(const QString &c)
  99. {
  100. Q_ASSERT(c.length() == 8);
  101. QColor color;
  102. color.setAlpha(c.mid(0, 2).toInt(0, 16));
  103. color.setRed(c.mid(2, 2).toInt(0, 16));
  104. color.setGreen(c.mid(4, 2).toInt(0, 16));
  105. color.setBlue(c.mid(6, 2).toInt(0, 16));
  106. return color;
  107. }
  108. QString XlsxColor::toARGBString(const QColor &c)
  109. {
  110. QString color;
  111. color.asprintf("%02X%02X%02X%02X", c.alpha(), c.red(), c.green(), c.blue());
  112. return color;
  113. }
  114. #if !defined(QT_NO_DATASTREAM)
  115. QDataStream &operator<<(QDataStream &s, const XlsxColor &color)
  116. {
  117. if (color.isInvalid())
  118. s<<0;
  119. else if (color.isRgbColor())
  120. s<<1<<color.rgbColor();
  121. else if (color.isIndexedColor())
  122. s<<2<<color.indexedColor();
  123. else if (color.isThemeColor())
  124. s<<3<<color.themeColor();
  125. else
  126. s<<4;
  127. return s;
  128. }
  129. QDataStream &operator>>(QDataStream &s, XlsxColor &color)
  130. {
  131. int marker(4);
  132. s>>marker;
  133. if (marker == 0) {
  134. color = XlsxColor();
  135. } else if (marker == 1) {
  136. QColor c;
  137. s>>c;
  138. color = XlsxColor(c);
  139. } else if (marker == 2) {
  140. int indexed;
  141. s>>indexed;
  142. color = XlsxColor(indexed);
  143. } else if (marker == 3) {
  144. QStringList list;
  145. s>>list;
  146. color = XlsxColor(list[0], list[1]);
  147. }
  148. return s;
  149. }
  150. #endif
  151. #ifndef QT_NO_DEBUG_STREAM
  152. QDebug operator<<(QDebug dbg, const XlsxColor &c)
  153. {
  154. if (c.isInvalid())
  155. dbg.nospace() << "XlsxColor(invalid)";
  156. else if (c.isRgbColor())
  157. dbg.nospace() << c.rgbColor();
  158. else if (c.isIndexedColor())
  159. dbg.nospace() << "XlsxColor(indexed," << c.indexedColor() << ")";
  160. else if (c.isThemeColor())
  161. dbg.nospace() << "XlsxColor(theme," << c.themeColor().join(QLatin1Char(':')) << ")";
  162. return dbg.space();
  163. }
  164. #endif
  165. } // namespace QXlsx