Browse Source

Improve QColor <==> #AARRGGBB conversion

master
Debao Zhang 11 years ago
parent
commit
ca33d5e1b4
  1. 20
      src/xlsx/xlsxcolor.cpp
  2. 3
      src/xlsx/xlsxcolor_p.h
  3. 2
      src/xlsx/xlsxstyles.cpp
  4. 10
      src/xlsx/xlsxutility.cpp
  5. 1
      src/xlsx/xlsxutility_p.h
  6. 2
      tests/auto/styles/tst_stylestest.cpp
  7. 2
      tests/auto/xlsxconditionalformatting/tst_conditionalformattingtest.cpp

20
src/xlsx/xlsxcolor.cpp

@ -77,7 +77,7 @@ bool XlsxColor::saveToXml(QXmlStreamWriter &writer, const QString &node) const
writer.writeEmptyElement(QStringLiteral("color")); writer.writeEmptyElement(QStringLiteral("color"));
if (val.userType() == qMetaTypeId<QColor>()) { if (val.userType() == qMetaTypeId<QColor>()) {
writer.writeAttribute(QStringLiteral("rgb"), QStringLiteral("FF")+val.value<QColor>().name().mid(1));//remove # writer.writeAttribute(QStringLiteral("rgb"), XlsxColor::toARGBString(val.value<QColor>()));
} else if (val.userType() == QMetaType::QStringList) { } else if (val.userType() == QMetaType::QStringList) {
QStringList themes = val.toStringList(); QStringList themes = val.toStringList();
writer.writeAttribute(QStringLiteral("theme"), themes[0]); writer.writeAttribute(QStringLiteral("theme"), themes[0]);
@ -155,6 +155,24 @@ QDataStream &operator>>(QDataStream &s, XlsxColor &color)
return s; return s;
} }
QColor XlsxColor::fromARGBString(const QString &c)
{
Q_ASSERT(c.length() == 8);
QColor color;
color.setAlpha(c.mid(0, 2).toInt(0, 16));
color.setRed(c.mid(2, 2).toInt(0, 16));
color.setGreen(c.mid(4, 2).toInt(0, 16));
color.setBlue(c.mid(6, 2).toInt(0, 16));
return color;
}
QString XlsxColor::toARGBString(const QColor &c)
{
QString color;
color.sprintf("%02X%02X%02X%02X", c.alpha(), c.red(), c.green(), c.blue());
return color;
}
#endif #endif
} // namespace QXlsx } // namespace QXlsx

3
src/xlsx/xlsxcolor_p.h

@ -66,6 +66,9 @@ public:
operator QVariant() const; operator QVariant() const;
static QColor fromARGBString(const QString &c);
static QString toARGBString(const QColor &c);
bool saveToXml(QXmlStreamWriter &writer, const QString &node=QString()) const; bool saveToXml(QXmlStreamWriter &writer, const QString &node=QString()) const;
bool loadFromXml(QXmlStreamReader &reader); bool loadFromXml(QXmlStreamReader &reader);

2
src/xlsx/xlsxstyles.cpp

@ -1222,7 +1222,7 @@ bool Styles::readIndexedColors(QXmlStreamReader &reader)
if (reader.tokenType() == QXmlStreamReader::StartElement) { if (reader.tokenType() == QXmlStreamReader::StartElement) {
if (reader.name() == QLatin1String("rgbColor")) { if (reader.name() == QLatin1String("rgbColor")) {
QString color = reader.attributes().value(QLatin1String("rgb")).toString(); QString color = reader.attributes().value(QLatin1String("rgb")).toString();
m_indexedColors.append(fromARGBString(color)); m_indexedColors.append(XlsxColor::fromARGBString(color));
} }
} }
} }

10
src/xlsx/xlsxutility.cpp

@ -54,16 +54,6 @@ QStringList splitPath(const QString &path)
return QStringList()<<path.left(idx)<<path.mid(idx+1); return QStringList()<<path.left(idx)<<path.mid(idx+1);
} }
QColor fromARGBString(const QString &c)
{
Q_ASSERT(c.length() == 8);
QColor color;
color.setRed(c.mid(2, 2).toInt(0, 16));
color.setGreen(c.mid(4, 2).toInt(0, 16));
color.setBlue(c.mid(6, 2).toInt(0, 16));
return color;
}
double datetimeToNumber(const QDateTime &dt, bool is1904) double datetimeToNumber(const QDateTime &dt, bool is1904)
{ {
//Note, for number 0, Excel2007 shown as 1900-1-0, which should be 1899-12-31 //Note, for number 0, Excel2007 shown as 1900-1-0, which should be 1899-12-31

1
src/xlsx/xlsxutility_p.h

@ -47,7 +47,6 @@ namespace QXlsx {
XLSX_AUTOTEST_EXPORT int intPow(int x, int p); XLSX_AUTOTEST_EXPORT int intPow(int x, int p);
XLSX_AUTOTEST_EXPORT QStringList splitPath(const QString &path); XLSX_AUTOTEST_EXPORT QStringList splitPath(const QString &path);
XLSX_AUTOTEST_EXPORT QColor fromARGBString(const QString &c);
XLSX_AUTOTEST_EXPORT double datetimeToNumber(const QDateTime &dt, bool is1904=false); XLSX_AUTOTEST_EXPORT double datetimeToNumber(const QDateTime &dt, bool is1904=false);
XLSX_AUTOTEST_EXPORT QDateTime datetimeFromNumber(double num, bool is1904=false); XLSX_AUTOTEST_EXPORT QDateTime datetimeFromNumber(double num, bool is1904=false);

2
tests/auto/styles/tst_stylestest.cpp

@ -81,7 +81,7 @@ void StylesTest::testSolidFillBackgroundColor()
QByteArray xmlData = styles.saveToXmlData(); QByteArray xmlData = styles.saveToXmlData();
QVERIFY(xmlData.contains("<patternFill patternType=\"solid\"><fgColor rgb=\"FFff0000\"/>")); QVERIFY(xmlData.contains("<patternFill patternType=\"solid\"><fgColor rgb=\"FFFF0000\"/>"));
} }
void StylesTest::testWriteBorders() void StylesTest::testWriteBorders()

2
tests/auto/xlsxconditionalformatting/tst_conditionalformattingtest.cpp

@ -93,7 +93,7 @@ void ConditionalFormattingTest::testDataBarRules()
QByteArray res = "<cfRule type=\"dataBar\" priority=\"1\">" QByteArray res = "<cfRule type=\"dataBar\" priority=\"1\">"
"<dataBar><cfvo type=\"min\" val=\"0\"/>" "<dataBar><cfvo type=\"min\" val=\"0\"/>"
"<cfvo type=\"max\" val=\"0\"/>" "<cfvo type=\"max\" val=\"0\"/>"
"<color rgb=\"FF0000ff\"/></dataBar>" "<color rgb=\"FF0000FF\"/></dataBar>"
"</cfRule>"; "</cfRule>";
QVERIFY(buffer.buffer().contains(res)); QVERIFY(buffer.buffer().contains(res));
} }

Loading…
Cancel
Save