andre@1: #include "xlsxcolor_p.h" andre@1: #include "xlsxstyles_p.h" andre@1: #include "xlsxutility_p.h" andre@1: andre@1: #include andre@1: #include andre@1: #include andre@1: #include andre@1: andre@1: namespace QXlsx { andre@1: andre@1: andre@1: XlsxColor::XlsxColor(const QColor &color) andre@1: { andre@1: if (color.isValid()) andre@1: val.setValue(color); andre@1: } andre@1: andre@1: XlsxColor::XlsxColor(const QString &theme, const QString &tint) andre@1: :val(QStringList()<() && val.value().isValid()) andre@1: return true; andre@1: return false; andre@1: } andre@1: andre@1: bool XlsxColor::isIndexedColor() const andre@1: { andre@1: return val.userType() == QMetaType::Int; andre@1: } andre@1: andre@1: bool XlsxColor::isThemeColor() const andre@1: { andre@1: return val.userType() == QMetaType::QStringList; andre@1: } andre@1: andre@1: bool XlsxColor::isInvalid() const andre@1: { andre@1: return !val.isValid(); andre@1: } andre@1: andre@1: QColor XlsxColor::rgbColor() const andre@1: { andre@1: if (isRgbColor()) andre@1: return val.value(); andre@1: return QColor(); andre@1: } andre@1: andre@1: int XlsxColor::indexedColor() const andre@1: { andre@1: if (isIndexedColor()) andre@1: return val.toInt(); andre@1: return -1; andre@1: } andre@1: andre@1: QStringList XlsxColor::themeColor() const andre@1: { andre@1: if (isThemeColor()) andre@1: return val.toStringList(); andre@1: return QStringList(); andre@1: } andre@1: andre@1: bool XlsxColor::saveToXml(QXmlStreamWriter &writer, const QString &node) const andre@1: { andre@1: if (!node.isEmpty()) andre@1: writer.writeEmptyElement(node); //color, bgColor, fgColor andre@1: else andre@1: writer.writeEmptyElement(QStringLiteral("color")); andre@1: andre@1: if (val.userType() == qMetaTypeId()) { andre@1: writer.writeAttribute(QStringLiteral("rgb"), XlsxColor::toARGBString(val.value())); andre@1: } else if (val.userType() == QMetaType::QStringList) { andre@1: QStringList themes = val.toStringList(); andre@1: writer.writeAttribute(QStringLiteral("theme"), themes[0]); andre@1: if (!themes[1].isEmpty()) andre@1: writer.writeAttribute(QStringLiteral("tint"), themes[1]); andre@1: } else if (val.userType() == QMetaType::Int) { andre@1: writer.writeAttribute(QStringLiteral("indexed"), val.toString()); andre@1: } else { andre@1: writer.writeAttribute(QStringLiteral("auto"), QStringLiteral("1")); andre@1: } andre@1: andre@1: return true; andre@1: } andre@1: andre@1: bool XlsxColor::loadFromXml(QXmlStreamReader &reader) andre@1: { andre@1: QXmlStreamAttributes attributes = reader.attributes(); andre@1: andre@1: if (attributes.hasAttribute(QLatin1String("rgb"))) { andre@1: QString colorString = attributes.value(QLatin1String("rgb")).toString(); andre@1: val.setValue(fromARGBString(colorString)); andre@1: } else if (attributes.hasAttribute(QLatin1String("indexed"))) { andre@1: int index = attributes.value(QLatin1String("indexed")).toString().toInt(); andre@1: val.setValue(index); andre@1: } else if (attributes.hasAttribute(QLatin1String("theme"))) { andre@1: QString theme = attributes.value(QLatin1String("theme")).toString(); andre@1: QString tint = attributes.value(QLatin1String("tint")).toString(); andre@1: val.setValue(QStringList()<(), this); andre@1: } andre@1: andre@1: andre@1: QColor XlsxColor::fromARGBString(const QString &c) andre@1: { andre@1: Q_ASSERT(c.length() == 8); andre@1: QColor color; andre@1: color.setAlpha(c.mid(0, 2).toInt(0, 16)); andre@1: color.setRed(c.mid(2, 2).toInt(0, 16)); andre@1: color.setGreen(c.mid(4, 2).toInt(0, 16)); andre@1: color.setBlue(c.mid(6, 2).toInt(0, 16)); andre@1: return color; andre@1: } andre@1: andre@1: QString XlsxColor::toARGBString(const QColor &c) andre@1: { andre@1: QString color; andre@1: color.sprintf("%02X%02X%02X%02X", c.alpha(), c.red(), c.green(), c.blue()); andre@1: return color; andre@1: } andre@1: andre@1: #if !defined(QT_NO_DATASTREAM) andre@1: QDataStream &operator<<(QDataStream &s, const XlsxColor &color) andre@1: { andre@1: if (color.isInvalid()) andre@1: s<<0; andre@1: else if (color.isRgbColor()) andre@1: s<<1<>(QDataStream &s, XlsxColor &color) andre@1: { andre@1: int marker(4); andre@1: s>>marker; andre@1: if (marker == 0) { andre@1: color = XlsxColor(); andre@1: } else if (marker == 1) { andre@1: QColor c; andre@1: s>>c; andre@1: color = XlsxColor(c); andre@1: } else if (marker == 2) { andre@1: int indexed; andre@1: s>>indexed; andre@1: color = XlsxColor(indexed); andre@1: } else if (marker == 3) { andre@1: QStringList list; andre@1: s>>list; andre@1: color = XlsxColor(list[0], list[1]); andre@1: } andre@1: andre@1: return s; andre@1: } andre@1: andre@1: #endif andre@1: andre@1: #ifndef QT_NO_DEBUG_STREAM andre@1: QDebug operator<<(QDebug dbg, const XlsxColor &c) andre@1: { andre@1: if (c.isInvalid()) andre@1: dbg.nospace() << "XlsxColor(invalid)"; andre@1: else if (c.isRgbColor()) andre@1: dbg.nospace() << c.rgbColor(); andre@1: else if (c.isIndexedColor()) andre@1: dbg.nospace() << "XlsxColor(indexed," << c.indexedColor() << ")"; andre@1: else if (c.isThemeColor()) andre@1: dbg.nospace() << "XlsxColor(theme," << c.themeColor().join(QLatin1Char(':')) << ")"; andre@1: andre@1: return dbg.space(); andre@1: } andre@1: andre@1: #endif andre@1: andre@1: } // namespace QXlsx