Commit 05b04699 authored by Boudewijn Rempt's avatar Boudewijn Rempt

Add simple to/from xml methods to KoColor

This uses an undocumented extension to the format in which
KoColor saves to store/restore the channel depth.
parent 43f89ce8
......@@ -304,13 +304,13 @@ qreal KoColor::opacityF() const
return m_colorSpace->opacityF(m_data);
}
KoColor KoColor::fromXML(const QDomElement& elt, const QString& bitDepthId)
KoColor KoColor::fromXML(const QDomElement& elt, const QString& channelDepthId)
{
bool ok;
return fromXML(elt, bitDepthId, &ok);
return fromXML(elt, channelDepthId, &ok);
}
KoColor KoColor::fromXML(const QDomElement& elt, const QString& bitDepthId, bool* ok)
KoColor KoColor::fromXML(const QDomElement& elt, const QString& channelDepthId, bool* ok)
{
*ok = true;
QString modelId;
......@@ -336,7 +336,7 @@ KoColor KoColor::fromXML(const QDomElement& elt, const QString& bitDepthId, bool
profileName.clear();
}
}
const KoColorSpace* cs = KoColorSpaceRegistry::instance()->colorSpace(modelId, bitDepthId, profileName);
const KoColorSpace* cs = KoColorSpaceRegistry::instance()->colorSpace(modelId, channelDepthId, profileName);
if (cs == 0) {
QList<KoID> list = KoColorSpaceRegistry::instance()->colorDepthList(modelId, KoColorSpaceRegistry::AllColorSpaces);
if (!list.empty()) {
......@@ -354,6 +354,29 @@ KoColor KoColor::fromXML(const QDomElement& elt, const QString& bitDepthId, bool
}
}
QString KoColor::toXML() const
{
QDomDocument cdataDoc = QDomDocument("color");
QDomElement cdataRoot = cdataDoc.createElement("color");
cdataDoc.appendChild(cdataRoot);
cdataRoot.setAttribute("channeldepth", colorSpace()->colorDepthId().id());
toXML(cdataDoc, cdataRoot);
return cdataDoc.toString();
}
KoColor KoColor::fromXML(const QString &xml)
{
KoColor c;
QDomDocument doc;
if (doc.setContent(xml)) {
QDomElement e = doc.documentElement().firstChild().toElement();
QString channelDepthID = e.attribute("channeldepth", Integer16BitsColorDepthID.id());
bool ok;
c = KoColor::fromXML(e, channelDepthID, &ok);
}
return c;
}
QString KoColor::toQString(const KoColor &color)
{
QStringList ls;
......
......@@ -218,21 +218,21 @@ public:
* at http://create.freedesktop.org/wiki/index.php/Swatches_-_colour_file_format
*
* @param elt the element to unserialize (\<CMYK /\>, \<sRGB /\>, \<RGB /\>)
* @param bitDepthId the bit depth is unspecified by the spec, this allow to select
* @param channelDepthId the bit depth is unspecified by the spec, this allow to select
* a preferred bit depth for creating the KoColor object (if that
* bit depth isn't available, this function will randomly select
* an other bit depth)
* @return the unserialize color, or an empty color object if the function failed
* to unserialize the color
*/
static KoColor fromXML(const QDomElement& elt, const QString & bitDepthId);
static KoColor fromXML(const QDomElement& elt, const QString & channelDepthId);
/**
* Unserialize a color following Create's swatch color specification available
* at http://create.freedesktop.org/wiki/index.php/Swatches_-_colour_file_format
*
* @param elt the element to unserialize (\<CMYK /\>, \<sRGB /\>, \<RGB /\>)
* @param bitDepthId the bit depth is unspecified by the spec, this allow to select
* @param channelDepthId the bit depth is unspecified by the spec, this allow to select
* a preferred bit depth for creating the KoColor object (if that
* bit depth isn't available, this function will randomly select
* an other bit depth)
......@@ -240,7 +240,24 @@ public:
* @return the unserialize color, or an empty color object if the function failed
* to unserialize the color
*/
static KoColor fromXML(const QDomElement& elt, const QString & bitDepthId, bool* ok);
static KoColor fromXML(const QDomElement& elt, const QString & channelDepthId, bool* ok);
/**
* @brief toXML creates a string with XML that represents the current color. The XML
* is extended with a "channeldepth" attribute so we can restore the color to the same
* channel depth.
* @return a valid XML document in a string
*/
QString toXML() const;
/**
* @brief fromXML restores a KoColor from a string saved with toXML(). If the
* string does not contain the "channeldepth" attribute, 16 bit integer is assumed.
* @param xml a valid XML document
* @return a new KoColor object
*/
static KoColor fromXML(const QString &xml);
/**
* @brief toQString create a user-visible string of the channel names and the channel values
......
......@@ -87,4 +87,14 @@ void TestKoColor::testConversion()
kc.convertTo(csDst);
}
void TestKoColor::testSimpleSerialization()
{
QColor c = Qt::green;
KoColor k;
k.fromQColor(c);
QString xml = k.toXML();
KoColor k2 = KoColor::fromXML(xml);
QVERIFY(k2.colorSpace() == k.colorSpace());
}
KISTEST_MAIN(TestKoColor)
......@@ -31,6 +31,7 @@ private:
private Q_SLOTS:
void testSerialization();
void testConversion();
void testSimpleSerialization();
};
#endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment