File indexing completed on 2025-03-16 03:35:10
0001 // xlsxcolor.cpp 0002 0003 #include <QDataStream> 0004 #include <QXmlStreamReader> 0005 #include <QXmlStreamWriter> 0006 #include <QDebug> 0007 0008 #include "xlsxcolor_p.h" 0009 #include "xlsxstyles_p.h" 0010 #include "xlsxutility_p.h" 0011 0012 QT_BEGIN_NAMESPACE_XLSX 0013 0014 XlsxColor::XlsxColor(const QColor &color) 0015 { 0016 if (color.isValid()) 0017 val.setValue(color); 0018 } 0019 0020 XlsxColor::XlsxColor(const QString &theme, const QString &tint) 0021 :val(QStringList()<<theme<<tint) 0022 { 0023 0024 } 0025 0026 XlsxColor::XlsxColor(int index) 0027 :val(index) 0028 { 0029 0030 } 0031 0032 bool XlsxColor::isRgbColor() const 0033 { 0034 return val.userType() == qMetaTypeId<QColor>() && val.value<QColor>().isValid(); 0035 } 0036 0037 bool XlsxColor::isIndexedColor() const 0038 { 0039 return val.userType() == QMetaType::Int; 0040 } 0041 0042 bool XlsxColor::isThemeColor() const 0043 { 0044 return val.userType() == QMetaType::QStringList; 0045 } 0046 0047 bool XlsxColor::isInvalid() const 0048 { 0049 return !val.isValid(); 0050 } 0051 0052 QColor XlsxColor::rgbColor() const 0053 { 0054 return isRgbColor() ? val.value<QColor>() : QColor(); 0055 } 0056 0057 int XlsxColor::indexedColor() const 0058 { 0059 return isIndexedColor() ? val.toInt() : -1; 0060 } 0061 0062 QStringList XlsxColor::themeColor() const 0063 { 0064 return isThemeColor() ? val.toStringList() : QStringList(); 0065 } 0066 0067 bool XlsxColor::saveToXml(QXmlStreamWriter &writer, const QString &node) const 0068 { 0069 if (!node.isEmpty()) 0070 writer.writeEmptyElement(node); //color, bgColor, fgColor 0071 else 0072 writer.writeEmptyElement(QStringLiteral("color")); 0073 0074 if (val.userType() == qMetaTypeId<QColor>()) { 0075 writer.writeAttribute(QStringLiteral("rgb"), XlsxColor::toARGBString(val.value<QColor>())); 0076 } else if (val.userType() == QMetaType::QStringList) { 0077 QStringList themes = val.toStringList(); 0078 writer.writeAttribute(QStringLiteral("theme"), themes[0]); 0079 if (!themes[1].isEmpty()) 0080 writer.writeAttribute(QStringLiteral("tint"), themes[1]); 0081 } else if (val.userType() == QMetaType::Int) { 0082 writer.writeAttribute(QStringLiteral("indexed"), val.toString()); 0083 } else { 0084 writer.writeAttribute(QStringLiteral("auto"), QStringLiteral("1")); 0085 } 0086 0087 return true; 0088 } 0089 0090 bool XlsxColor::loadFromXml(QXmlStreamReader &reader) 0091 { 0092 const auto& attributes = reader.attributes(); 0093 0094 if (attributes.hasAttribute(QLatin1String("rgb"))) { 0095 const auto& colorString = attributes.value(QLatin1String("rgb")).toString(); 0096 val.setValue(fromARGBString(colorString)); 0097 } else if (attributes.hasAttribute(QLatin1String("indexed"))) { 0098 int index = attributes.value(QLatin1String("indexed")).toInt(); 0099 val.setValue(index); 0100 } else if (attributes.hasAttribute(QLatin1String("theme"))) { 0101 const auto& theme = attributes.value(QLatin1String("theme")).toString(); 0102 const auto& tint = attributes.value(QLatin1String("tint")).toString(); 0103 val.setValue(QStringList()<<theme<<tint); 0104 } 0105 return true; 0106 } 0107 0108 XlsxColor::operator QVariant() const 0109 { 0110 const auto& cref 0111 #if QT_VERSION >= 0x060000 // Qt 6.0 or over 0112 = QMetaType::fromType<XlsxColor>(); 0113 #else 0114 = qMetaTypeId<XlsxColor>() ; 0115 #endif 0116 return QVariant(cref, this); 0117 } 0118 0119 0120 QColor XlsxColor::fromARGBString(const QString &c) 0121 { 0122 QColor color; 0123 if (c.startsWith(u'#')) { 0124 color.setNamedColor(c); 0125 } else { 0126 color.setNamedColor(QLatin1Char('#') + c); 0127 } 0128 return color; 0129 } 0130 0131 QString XlsxColor::toARGBString(const QColor &c) 0132 { 0133 return QString::asprintf("%02X%02X%02X%02X", c.alpha(), c.red(), c.green(), c.blue()); 0134 } 0135 0136 #if !defined(QT_NO_DATASTREAM) 0137 QDataStream &operator<<(QDataStream &s, const XlsxColor &color) 0138 { 0139 if (color.isInvalid()) 0140 s<<0; 0141 else if (color.isRgbColor()) 0142 s<<1<<color.rgbColor(); 0143 else if (color.isIndexedColor()) 0144 s<<2<<color.indexedColor(); 0145 else if (color.isThemeColor()) 0146 s<<3<<color.themeColor(); 0147 else 0148 s<<4; 0149 0150 return s; 0151 } 0152 0153 QDataStream &operator>>(QDataStream &s, XlsxColor &color) 0154 { 0155 int marker(4); 0156 s>>marker; 0157 if (marker == 0) { 0158 color = XlsxColor(); 0159 } else if (marker == 1) { 0160 QColor c; 0161 s>>c; 0162 color = XlsxColor(c); 0163 } else if (marker == 2) { 0164 int indexed; 0165 s>>indexed; 0166 color = XlsxColor(indexed); 0167 } else if (marker == 3) { 0168 QStringList list; 0169 s>>list; 0170 color = XlsxColor(list[0], list[1]); 0171 } 0172 0173 return s; 0174 } 0175 0176 #endif 0177 0178 #ifndef QT_NO_DEBUG_STREAM 0179 QDebug operator<<(QDebug dbg, const XlsxColor &c) 0180 { 0181 if (c.isInvalid()) 0182 dbg.nospace() << "XlsxColor(invalid)"; 0183 else if (c.isRgbColor()) 0184 dbg.nospace() << c.rgbColor(); 0185 else if (c.isIndexedColor()) 0186 dbg.nospace() << "XlsxColor(indexed," << c.indexedColor() << ")"; 0187 else if (c.isThemeColor()) 0188 dbg.nospace() << "XlsxColor(theme," << c.themeColor().join(QLatin1Char(':')) << ')'; 0189 0190 return dbg.space(); 0191 } 0192 0193 #endif 0194 0195 0196 QT_END_NAMESPACE_XLSX