Warning, file /office/calligra/libs/odf/KoShadowStyle.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002  *
0003  * Copyright (C) 2011 Pierre Ducroquet <pinaraf@pinaraf.info>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Library General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #include "KoShadowStyle.h"
0022 
0023 #include <KoUnit.h>
0024 
0025 
0026 // KoShadowStyle private class
0027 class KoShadowStylePrivate: public QSharedData
0028 {
0029 public:
0030     KoShadowStylePrivate();
0031     ~KoShadowStylePrivate();
0032 
0033     QVector<KoShadowStyle::ShadowData> shadows;
0034 };
0035 
0036 KoShadowStylePrivate::KoShadowStylePrivate()
0037 {
0038 }
0039 
0040 KoShadowStylePrivate::~KoShadowStylePrivate()
0041 {
0042 }
0043 
0044 // KoShadowStyle::ShadowData structure
0045 KoShadowStyle::ShadowData::ShadowData()
0046     : color(), offset(0, 0), radius(0.0)
0047 {
0048 }
0049 
0050 bool KoShadowStyle::ShadowData::operator==(const KoShadowStyle::ShadowData &other) const
0051 {
0052     return (color == other.color) && (offset == other.offset) && (radius == other.radius);
0053 }
0054 
0055 // KoShadowStyle class
0056 KoShadowStyle::KoShadowStyle()
0057     : d(new KoShadowStylePrivate)
0058 {
0059 }
0060 
0061 KoShadowStyle::KoShadowStyle(const KoShadowStyle &other)
0062     : d(other.d)
0063 {
0064 }
0065 
0066 KoShadowStyle::~KoShadowStyle()
0067 {
0068 }
0069 
0070 bool KoShadowStyle::operator==(const KoShadowStyle &other) const
0071 {
0072     if (d.data() == other.d.data())
0073         return true;
0074 
0075     if (shadowCount() != other.shadowCount())
0076         return false;
0077 
0078     foreach (const ShadowData &data, d->shadows)
0079     {
0080         if (!other.d->shadows.contains(data))
0081             return false;
0082     }
0083     return true;
0084 }
0085 
0086 bool KoShadowStyle::operator!=(const KoShadowStyle &other) const
0087 {
0088     return !operator==(other);
0089 }
0090 
0091 // load value string as specified by CSS2 §7.16.5 "text-shadow"
0092 bool KoShadowStyle::loadOdf (const QString &data)
0093 {
0094     if (data == QLatin1String("none"))
0095         return true;
0096 
0097     const QStringList sub_shadows = data.split(QLatin1Char(','));
0098     foreach (const QString &shadow, sub_shadows) {
0099         QStringList words = shadow.split(QLatin1Char(' '), QString::SkipEmptyParts);
0100         if (words.isEmpty())
0101             return false;
0102 
0103         KoShadowStyle::ShadowData currentData;
0104 
0105         // look for color at begin
0106         QColor shadowColor(words.first());
0107         if (shadowColor.isValid()) {
0108             currentData.color = shadowColor;
0109             words.removeFirst();
0110         } else if (words.length() > 2) {
0111             // look for color at end, if there could be one
0112             shadowColor = QColor(words.last());
0113             if (shadowColor.isValid()) {
0114                 currentData.color = shadowColor;
0115                 words.removeLast();
0116             }
0117         }
0118         // We keep an invalid color.if none was found
0119 
0120         // "Each shadow effect must specify a shadow offset and may optionally
0121         // specify a blur radius and a shadow color.", from CSS2 §7.16.5 "text-shadow"
0122         // But for some reason also no offset has been accepted before. TODO: which?
0123         if (! words.isEmpty()) {
0124             if ((words.length() < 2) || (words.length() > 3))
0125                 return false;
0126 
0127             // Parse offset
0128             currentData.offset.setX(KoUnit::parseValue(words.at(0), 0.0));
0129             currentData.offset.setY(KoUnit::parseValue(words.at(1), 0.0));
0130             // Parse blur radius if present
0131             if (words.length() == 3)
0132                 currentData.radius = KoUnit::parseValue(words.at(2), 0.0);
0133         }
0134         d->shadows << currentData;
0135     }
0136     return true;
0137 }
0138 
0139 int KoShadowStyle::shadowCount() const
0140 {
0141     return d->shadows.size();
0142 }
0143 
0144 QString KoShadowStyle::saveOdf() const
0145 {
0146     if (d->shadows.isEmpty())
0147         return QLatin1String("none");
0148 
0149     QStringList parts;
0150     const QString pt = QLatin1String("%1pt");
0151     foreach (const ShadowData &data, d->shadows) {
0152         QStringList elements;
0153         if (data.color.isValid()) {
0154             elements << data.color.name();
0155         }
0156         elements << pt.arg(data.offset.x()) << pt.arg(data.offset.y());
0157         if (data.radius != 0)
0158             elements << pt.arg(data.radius);
0159 
0160         parts << elements.join(QLatin1String(" "));
0161     }
0162     return parts.join(QLatin1String(","));
0163 }
0164