Warning, file /office/calligra/libs/flake/KoGradientBackground.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  * Copyright (C) 2008 Jan Hambrecht <jaham@gmx.net>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Library General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Library General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this library; see the file COPYING.LIB.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "KoGradientBackground.h"
0021 #include "KoShapeBackground_p.h"
0022 #include "KoFlake.h"
0023 #include <KoStyleStack.h>
0024 #include <KoXmlNS.h>
0025 #include <KoOdfLoadingContext.h>
0026 #include <KoOdfGraphicStyles.h>
0027 #include <KoShapeSavingContext.h>
0028 
0029 #include <FlakeDebug.h>
0030 
0031 #include <QSharedPointer>
0032 #include <QBrush>
0033 #include <QPainter>
0034 
0035 class KoGradientBackgroundPrivate : public KoShapeBackgroundPrivate
0036 {
0037 public:
0038     KoGradientBackgroundPrivate()
0039         : gradient(0)
0040     {}
0041 
0042     QGradient *gradient;
0043     QTransform matrix;
0044 };
0045 
0046 KoGradientBackground::KoGradientBackground(QGradient * gradient, const QTransform &matrix)
0047     : KoShapeBackground(*(new KoGradientBackgroundPrivate()))
0048 {
0049     Q_D(KoGradientBackground);
0050     d->gradient = gradient;
0051     d->matrix = matrix;
0052     Q_ASSERT(d->gradient);
0053     Q_ASSERT(d->gradient->coordinateMode() == QGradient::ObjectBoundingMode);
0054 }
0055 
0056 KoGradientBackground::KoGradientBackground(const QGradient & gradient, const QTransform &matrix)
0057     : KoShapeBackground(*(new KoGradientBackgroundPrivate()))
0058 {
0059     Q_D(KoGradientBackground);
0060     d->gradient = KoFlake::cloneGradient(&gradient);
0061     d->matrix = matrix;
0062     Q_ASSERT(d->gradient);
0063     Q_ASSERT(d->gradient->coordinateMode() == QGradient::ObjectBoundingMode);
0064 }
0065 
0066 KoGradientBackground::~KoGradientBackground()
0067 {
0068     Q_D(KoGradientBackground);
0069     delete d->gradient;
0070 }
0071 
0072 void KoGradientBackground::setTransform(const QTransform &matrix)
0073 {
0074     Q_D(KoGradientBackground);
0075     d->matrix = matrix;
0076 }
0077 
0078 QTransform KoGradientBackground::transform() const
0079 {
0080     Q_D(const KoGradientBackground);
0081     return d->matrix;
0082 }
0083 
0084 void KoGradientBackground::setGradient(const QGradient &gradient)
0085 {
0086     Q_D(KoGradientBackground);
0087     delete d->gradient;
0088 
0089     d->gradient = KoFlake::cloneGradient(&gradient);
0090     Q_ASSERT(d->gradient);
0091     Q_ASSERT(d->gradient->coordinateMode() == QGradient::ObjectBoundingMode);
0092 }
0093 
0094 const QGradient * KoGradientBackground::gradient() const
0095 {
0096     Q_D(const KoGradientBackground);
0097     return d->gradient;
0098 }
0099 
0100 void KoGradientBackground::paint(QPainter &painter, const KoViewConverter &/*converter*/, KoShapePaintingContext &/*context*/, const QPainterPath &fillPath) const
0101 {
0102     Q_D(const KoGradientBackground);
0103     if (!d->gradient) return;
0104     QBrush brush(*d->gradient);
0105     brush.setTransform(d->matrix);
0106 
0107     painter.setBrush(brush);
0108     painter.drawPath(fillPath);
0109 }
0110 
0111 void KoGradientBackground::fillStyle(KoGenStyle &style, KoShapeSavingContext &context)
0112 {
0113     Q_D(KoGradientBackground);
0114     if (!d->gradient) return;
0115     QBrush brush(*d->gradient);
0116     brush.setTransform(d->matrix);
0117     KoOdfGraphicStyles::saveOdfFillStyle(style, context.mainStyles(), brush);
0118 }
0119 
0120 bool KoGradientBackground::loadStyle(KoOdfLoadingContext &context, const QSizeF &shapeSize)
0121 {
0122     Q_D(KoGradientBackground);
0123     KoStyleStack &styleStack = context.styleStack();
0124     if (! styleStack.hasProperty(KoXmlNS::draw, "fill"))
0125         return false;
0126 
0127     QString fillStyle = styleStack.property(KoXmlNS::draw, "fill");
0128     if (fillStyle == "gradient") {
0129         QBrush brush = KoOdfGraphicStyles::loadOdfGradientStyle(styleStack, context.stylesReader(), shapeSize);
0130         const QGradient * gradient = brush.gradient();
0131         if (gradient) {
0132             d->gradient = KoFlake::cloneGradient(gradient);
0133             d->matrix = brush.transform();
0134 
0135             //Gopalakrishna Bhat: If the brush has transparency then we ignore the draw:opacity property and use the brush transparency.
0136             // Brush will have transparency if the svg:linearGradient stop point has stop-opacity property otherwise it is opaque
0137             if (brush.isOpaque() && styleStack.hasProperty(KoXmlNS::draw, "opacity")) {
0138                 QString opacityPercent = styleStack.property(KoXmlNS::draw, "opacity");
0139                 if (! opacityPercent.isEmpty() && opacityPercent.right(1) == "%") {
0140                     float opacity = qMin(opacityPercent.leftRef(opacityPercent.length() - 1).toDouble(), 100.0) / 100;
0141                     QGradientStops stops;
0142                     foreach(QGradientStop stop, d->gradient->stops()) {
0143                         stop.second.setAlphaF(opacity);
0144                         stops << stop;
0145                     }
0146                     d->gradient->setStops(stops);
0147                 }
0148             }
0149 
0150             return true;
0151         }
0152     }
0153     return false;
0154 }