Debao Zhang
11 years ago
8 changed files with 336 additions and 171 deletions
@ -0,0 +1,172 @@ |
|||||
|
#include "xlsxcolor_p.h" |
||||
|
#include "xlsxstyles_p.h" |
||||
|
#include "xlsxutility_p.h" |
||||
|
|
||||
|
#include <QXmlStreamReader> |
||||
|
#include <QXmlStreamWriter> |
||||
|
|
||||
|
namespace QXlsx { |
||||
|
|
||||
|
|
||||
|
XlsxColor::XlsxColor(const QColor &color) |
||||
|
:val(color) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
XlsxColor::XlsxColor(const QString &theme, const QString &tint) |
||||
|
:val(QStringList()<<theme<<tint) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
XlsxColor::XlsxColor(int index) |
||||
|
:val(index) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
bool XlsxColor::isRgbColor() const |
||||
|
{ |
||||
|
if (val.userType() == qMetaTypeId<QColor>() && val.value<QColor>().isValid()) |
||||
|
return true; |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
bool XlsxColor::isIndexedColor() const |
||||
|
{ |
||||
|
return val.userType() == QMetaType::Int; |
||||
|
} |
||||
|
|
||||
|
bool XlsxColor::isThemeColor() const |
||||
|
{ |
||||
|
return val.userType() == QMetaType::QStringList; |
||||
|
} |
||||
|
|
||||
|
bool XlsxColor::isInvalid() const |
||||
|
{ |
||||
|
if (val.userType() == qMetaTypeId<QColor>() && !val.value<QColor>().isValid()) |
||||
|
return true; |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
QColor XlsxColor::rgbColor() const |
||||
|
{ |
||||
|
if (isRgbColor()) |
||||
|
return val.value<QColor>(); |
||||
|
return QColor(); |
||||
|
} |
||||
|
|
||||
|
int XlsxColor::indexedColor() const |
||||
|
{ |
||||
|
if (isIndexedColor()) |
||||
|
return val.toInt(); |
||||
|
return -1; |
||||
|
} |
||||
|
|
||||
|
QStringList XlsxColor::themeColor() const |
||||
|
{ |
||||
|
if (isThemeColor()) |
||||
|
return val.toStringList(); |
||||
|
return QStringList(); |
||||
|
} |
||||
|
|
||||
|
bool XlsxColor::saveToXml(QXmlStreamWriter &writer, const QString &node) const |
||||
|
{ |
||||
|
if (!node.isEmpty()) |
||||
|
writer.writeEmptyElement(node); //color, bgColor, fgColor
|
||||
|
else |
||||
|
writer.writeEmptyElement(QStringLiteral("color")); |
||||
|
|
||||
|
if (val.userType() == qMetaTypeId<QColor>()) { |
||||
|
QColor color = val.value<QColor>(); |
||||
|
if (color.isValid()) |
||||
|
writer.writeAttribute(QStringLiteral("rgb"), QStringLiteral("FF")+color.name().mid(1));//remove #
|
||||
|
else |
||||
|
writer.writeAttribute(QStringLiteral("auto"), QStringLiteral("1")); |
||||
|
} else if (val.userType() == QMetaType::QStringList) { |
||||
|
QStringList themes = val.toStringList(); |
||||
|
writer.writeAttribute(QStringLiteral("theme"), themes[0]); |
||||
|
if (!themes[1].isEmpty()) |
||||
|
writer.writeAttribute(QStringLiteral("tint"), themes[1]); |
||||
|
} else if (val.userType() == QMetaType::Int) { |
||||
|
writer.writeAttribute(QStringLiteral("indexed"), val.toString()); |
||||
|
} else { |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
bool XlsxColor::loadFromXml(QXmlStreamReader &reader, Styles *styles) |
||||
|
{ |
||||
|
QXmlStreamAttributes attributes = reader.attributes(); |
||||
|
|
||||
|
if (attributes.hasAttribute(QLatin1String("rgb"))) { |
||||
|
QString colorString = attributes.value(QLatin1String("rgb")).toString(); |
||||
|
val.setValue(fromARGBString(colorString)); |
||||
|
} else if (attributes.hasAttribute(QLatin1String("indexed"))) { |
||||
|
int index = attributes.value(QLatin1String("indexed")).toString().toInt(); |
||||
|
if (styles) { |
||||
|
//Convert to rgb color is possible
|
||||
|
QColor c = styles->getColorByIndex(index); |
||||
|
if (c.isValid()) |
||||
|
val.setValue(c); |
||||
|
} else { |
||||
|
val.setValue(index); |
||||
|
} |
||||
|
} else if (attributes.hasAttribute(QLatin1String("theme"))) { |
||||
|
QString theme = attributes.value(QLatin1String("theme")).toString(); |
||||
|
QString tint = attributes.value(QLatin1String("tint")).toString(); |
||||
|
val.setValue(QStringList()<<theme<<tint); |
||||
|
} |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
XlsxColor::operator QVariant() const |
||||
|
{ |
||||
|
return QVariant(qMetaTypeId<XlsxColor>(), this); |
||||
|
} |
||||
|
|
||||
|
#if !defined(QT_NO_DATASTREAM) |
||||
|
QDataStream &operator<<(QDataStream &s, const XlsxColor &color) |
||||
|
{ |
||||
|
if (color.isInvalid()) |
||||
|
s<<0; |
||||
|
else if (color.isRgbColor()) |
||||
|
s<<1<<color.rgbColor(); |
||||
|
else if (color.isIndexedColor()) |
||||
|
s<<2<<color.indexedColor(); |
||||
|
else if (color.isThemeColor()) |
||||
|
s<<3<<color.themeColor(); |
||||
|
else |
||||
|
s<<4; |
||||
|
|
||||
|
return s; |
||||
|
} |
||||
|
|
||||
|
QDataStream &operator>>(QDataStream &s, XlsxColor &color) |
||||
|
{ |
||||
|
int marker(4); |
||||
|
s>>marker; |
||||
|
if (marker == 0) { |
||||
|
color = XlsxColor(); |
||||
|
} else if (marker == 1) { |
||||
|
QColor c; |
||||
|
s>>c; |
||||
|
color = XlsxColor(c); |
||||
|
} else if (marker == 2) { |
||||
|
int indexed; |
||||
|
s>>indexed; |
||||
|
color = XlsxColor(indexed); |
||||
|
} else if (marker == 3) { |
||||
|
QStringList list; |
||||
|
s>>list; |
||||
|
color = XlsxColor(list[0], list[1]); |
||||
|
} |
||||
|
|
||||
|
return s; |
||||
|
} |
||||
|
|
||||
|
#endif |
||||
|
|
||||
|
} // namespace QXlsx
|
@ -0,0 +1,85 @@ |
|||||
|
/****************************************************************************
|
||||
|
** Copyright (c) 2013 Debao Zhang <hello@debao.me> |
||||
|
** All right reserved. |
||||
|
** |
||||
|
** Permission is hereby granted, free of charge, to any person obtaining |
||||
|
** a copy of this software and associated documentation files (the |
||||
|
** "Software"), to deal in the Software without restriction, including |
||||
|
** without limitation the rights to use, copy, modify, merge, publish, |
||||
|
** distribute, sublicense, and/or sell copies of the Software, and to |
||||
|
** permit persons to whom the Software is furnished to do so, subject to |
||||
|
** the following conditions: |
||||
|
** |
||||
|
** The above copyright notice and this permission notice shall be |
||||
|
** included in all copies or substantial portions of the Software. |
||||
|
** |
||||
|
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
||||
|
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
||||
|
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
||||
|
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
||||
|
** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
||||
|
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
||||
|
** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
||||
|
** |
||||
|
****************************************************************************/ |
||||
|
|
||||
|
#ifndef QXLSX_XLSXCOLOR_P_H |
||||
|
#define QXLSX_XLSXCOLOR_P_H |
||||
|
|
||||
|
//
|
||||
|
// W A R N I N G
|
||||
|
// -------------
|
||||
|
//
|
||||
|
// This file is not part of the Qt Xlsx API. It exists for the convenience
|
||||
|
// of the Qt Xlsx. This header file may change from
|
||||
|
// version to version without notice, or even be removed.
|
||||
|
//
|
||||
|
// We mean it.
|
||||
|
//
|
||||
|
|
||||
|
#include "xlsxglobal.h" |
||||
|
#include <QVariant> |
||||
|
#include <QColor> |
||||
|
|
||||
|
class QXmlStreamWriter; |
||||
|
class QXmlStreamReader; |
||||
|
|
||||
|
namespace QXlsx { |
||||
|
|
||||
|
class Styles; |
||||
|
|
||||
|
class XlsxColor |
||||
|
{ |
||||
|
public: |
||||
|
explicit XlsxColor(const QColor &color = QColor()); |
||||
|
explicit XlsxColor(const QString &theme, const QString &tint=QString()); |
||||
|
explicit XlsxColor (int index); |
||||
|
|
||||
|
bool isThemeColor() const; |
||||
|
bool isIndexedColor() const; |
||||
|
bool isRgbColor() const; |
||||
|
bool isInvalid() const; |
||||
|
|
||||
|
QColor rgbColor() const; |
||||
|
int indexedColor() const; |
||||
|
QStringList themeColor() const; |
||||
|
|
||||
|
operator QVariant() const; |
||||
|
|
||||
|
bool saveToXml(QXmlStreamWriter &writer, const QString &node=QString()) const; |
||||
|
bool loadFromXml(QXmlStreamReader &reader, Styles *styles=0); |
||||
|
|
||||
|
private: |
||||
|
QVariant val; |
||||
|
}; |
||||
|
|
||||
|
#if !defined(QT_NO_DATASTREAM) |
||||
|
QDataStream &operator<<(QDataStream &, const XlsxColor &); |
||||
|
QDataStream &operator>>(QDataStream &, XlsxColor &); |
||||
|
#endif |
||||
|
|
||||
|
} // namespace QXlsx
|
||||
|
|
||||
|
Q_DECLARE_METATYPE(QXlsx::XlsxColor) |
||||
|
|
||||
|
#endif // QXLSX_XLSXCOLOR_P_H
|
Loading…
Reference in new issue