1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
/* This file is part of the KDE project
*
* Copyright (C) 2011 Lukáš Tvrdý <lukas.tvrdy@ixonos.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <QColor>
#include <QImage>
#include <qmath.h>
#include <KoOdfGradientBackground.h>
#include <KoUnit.h>
#include <KoXmlNS.h>
#include <KoXmlReader.h>
#include <KoGenStyles.h>
#include <QPainter>
#include "KoShapeSavingContext.h"
#include <KoOdfLoadingContext.h>
#include "KoShapeBackground_p.h"
class KoOdfGradientBackgroundPrivate : public KoShapeBackgroundPrivate
{
public:
KoOdfGradientBackgroundPrivate()
: style(), cx(0), cy(0), startColor(), endColor(), angle(0), border(0) {};
~KoOdfGradientBackgroundPrivate(){};
//data
QString style;
int cx;
int cy;
QColor startColor;
QColor endColor;
qreal angle;
qreal border;
mutable QImage buffer;
};
KoOdfGradientBackground::KoOdfGradientBackground()
: KoShapeBackground(*(new KoOdfGradientBackgroundPrivate()))
{
}
KoOdfGradientBackground::~KoOdfGradientBackground()
{
}
bool KoOdfGradientBackground::loadOdf(const KoXmlElement& e)
{
Q_D(KoOdfGradientBackground);
d->style = e.attributeNS(KoXmlNS::draw, "style", QString());
//TODO: support ellipsoid here too
if ((d->style != "rectangular") && (d->style != "square")) {
return false;
}
d->cx = KoUnit::parseValue(e.attributeNS(KoXmlNS::draw, "cx", QString()).remove('%'));
d->cy = KoUnit::parseValue(e.attributeNS(KoXmlNS::draw, "cy", QString()).remove('%'));
d->border = qBound(0.0,0.01 * e.attributeNS(KoXmlNS::draw, "border", "0").remove('%').toDouble(),1.0);
d->startColor = QColor(e.attributeNS(KoXmlNS::draw, "start-color", QString()));
d->startColor.setAlphaF((0.01 * e.attributeNS(KoXmlNS::draw, "start-intensity", "100").remove('%').toDouble()));
d->endColor = QColor(e.attributeNS(KoXmlNS::draw, "end-color", QString()));
d->endColor.setAlphaF(0.01 * e.attributeNS(KoXmlNS::draw, "end-intensity", "100").remove('%').toDouble());
d->angle = e.attributeNS(KoXmlNS::draw, "angle", "0").toDouble() / 10;
return true;
}
void KoOdfGradientBackground::saveOdf(KoGenStyle& styleFill, KoGenStyles& mainStyles) const
{
Q_D(const KoOdfGradientBackground);
KoGenStyle::Type type = styleFill.type();
KoGenStyle::PropertyType propertyType = (type == KoGenStyle::GraphicStyle || type == KoGenStyle::GraphicAutoStyle ||
type == KoGenStyle::DrawingPageStyle || type == KoGenStyle::DrawingPageAutoStyle )
? KoGenStyle::DefaultType : KoGenStyle::GraphicType;
KoGenStyle gradientStyle(KoGenStyle::GradientStyle);
gradientStyle.addAttribute("draw:style", d->style); // draw:style="square"
gradientStyle.addAttribute("draw:cx", QString("%1%").arg(d->cx));
gradientStyle.addAttribute("draw:cy", QString("%1%").arg(d->cy));
gradientStyle.addAttribute("draw:start-color", d->startColor.name());
gradientStyle.addAttribute("draw:end-color", d->endColor.name());
gradientStyle.addAttribute("draw:start-intensity", QString("%1%").arg(qRound(d->startColor.alphaF() * 100)) );
gradientStyle.addAttribute("draw:end-intensity", QString("%1%").arg(qRound(d->endColor.alphaF() * 100)) );
gradientStyle.addAttribute("draw:angle", QString("%1").arg(d->angle * 10));
gradientStyle.addAttribute("draw:border", QString("%1%").arg(qRound(d->border * 100.0)));
QString gradientStyleName = mainStyles.insert(gradientStyle, "gradient");
styleFill.addProperty("draw:fill", "gradient", propertyType);
styleFill.addProperty("draw:fill-gradient-name", gradientStyleName, propertyType);
}
void KoOdfGradientBackground::paint(QPainter& painter, const QPainterPath& fillPath) const
{
Q_D(const KoOdfGradientBackground);
QRectF targetRect = fillPath.boundingRect();
QRectF pixels = painter.transform().mapRect(QRectF(0,0,targetRect.width(), targetRect.height()));
QSize currentSize( qCeil(pixels.size().width()), qCeil(pixels.size().height()) );
if (d->buffer.isNull() || d->buffer.size() != currentSize){
d->buffer = QImage(currentSize, QImage::Format_ARGB32_Premultiplied);
if (d->style == "square") {
renderSquareGradient(d->buffer);
} else {
renderRectangleGradient(d->buffer);
}
}
painter.setClipPath(fillPath);
painter.drawImage(targetRect, d->buffer, QRectF(QPointF(0,0), d->buffer.size()));
}
void KoOdfGradientBackground::fillStyle(KoGenStyle& style, KoShapeSavingContext& context)
{
saveOdf(style, context.mainStyles());
}
bool KoOdfGradientBackground::loadStyle(KoOdfLoadingContext& context, const QSizeF& shapeSize)
{
Q_UNUSED(shapeSize);
KoStyleStack &styleStack = context.styleStack();
if (!styleStack.hasProperty(KoXmlNS::draw, "fill")) {
return false;
}
QString fillStyle = styleStack.property(KoXmlNS::draw, "fill");
if (fillStyle == "gradient") {
QString styleName = styleStack.property(KoXmlNS::draw, "fill-gradient-name");
KoXmlElement * e = context.stylesReader().drawStyles("gradient")[styleName];
return loadOdf(*e);
}
return false;
}
void KoOdfGradientBackground::renderSquareGradient(QImage& buffer) const
{
Q_D(const KoOdfGradientBackground);
buffer.fill(d->startColor.rgba());
QPainter painter(&buffer);
painter.setPen(Qt::NoPen);
painter.setRenderHint(QPainter::Antialiasing, false);
int width = buffer.width();
int height = buffer.height();
qreal gradientCenterX = qRound(width * d->cx * 0.01);
qreal gradientCenterY = qRound(height * d->cy * 0.01);
qreal centerX = width * 0.5;
qreal centerY = height * 0.5;
qreal areaCenterX = qRound(centerX);
qreal areaCenterY = qRound(centerY);
QTransform m;
m.translate(gradientCenterX, gradientCenterY);
m.rotate(-d->angle);
m.scale(1.0 - d->border, 1.0 - d->border);
m.translate(-gradientCenterX, -gradientCenterY);
m.translate(gradientCenterX - areaCenterX,gradientCenterY - areaCenterY);
painter.setTransform(m);
QLinearGradient linearGradient;
linearGradient.setColorAt(1, d->startColor);
linearGradient.setColorAt(0, d->endColor);
// from center going North
linearGradient.setStart(centerX, centerY);
linearGradient.setFinalStop(centerX, 0);
painter.setBrush(linearGradient);
painter.drawRect(0, 0, width, centerY);
// from center going South
linearGradient.setFinalStop(centerX, height);
painter.setBrush(linearGradient);
painter.drawRect(0, centerY, width, centerY);
// clip the East and West portion
QPainterPath clip;
clip.moveTo(width, 0);
clip.lineTo(width, height);
clip.lineTo(0, 0);
clip.lineTo(0, height);
clip.closeSubpath();
painter.setClipPath(clip);
// from center going East
linearGradient.setFinalStop(width, centerY);
painter.setBrush(linearGradient);
painter.drawRect(centerX, 0, width, height);
// from center going West
linearGradient.setFinalStop( 0, centerY);
painter.setBrush(linearGradient);
painter.drawRect(0, 0, centerX, height);
}
void KoOdfGradientBackground::renderRectangleGradient(QImage& buffer) const
{
Q_D(const KoOdfGradientBackground);
buffer.fill(d->startColor.rgba());
QPainter painter(&buffer);
painter.setPen(Qt::NoPen);
painter.setRenderHint(QPainter::Antialiasing, false);
int width = buffer.width();
int height = buffer.height();
qreal gradientCenterX = qRound(width * d->cx * 0.01);
qreal gradientCenterY = qRound(height * d->cy * 0.01);
qreal centerX = width * 0.5;
qreal centerY = height * 0.5;
qreal areaCenterY = qRound(centerY);
qreal areaCenterX = qRound(centerX);
QTransform m;
m.translate(gradientCenterX, gradientCenterY);
// m.rotate(-d->angle); // OOo rotates the gradient differently
m.scale(1.0 - d->border, 1.0 - d->border);
m.translate(-gradientCenterX, -gradientCenterY);
m.translate(gradientCenterX - areaCenterX,gradientCenterY - areaCenterY);
painter.setTransform(m);
QLinearGradient linearGradient;
linearGradient.setColorAt(1, d->startColor);
linearGradient.setColorAt(0, d->endColor);
// render background
QPainterPath clipPath;
if (width < height) {
QRectF west(0,0,centerX, height);
QRectF east(centerX, 0, centerX, height);
linearGradient.setStart(centerX, centerY);
linearGradient.setFinalStop(0, centerY);
painter.setBrush(linearGradient);
painter.drawRect(west);
linearGradient.setFinalStop(width, centerY);
painter.setBrush(linearGradient);
painter.drawRect(east);
QRectF north(0,0,width, centerX);
QRectF south(0,height - centerX, width, centerX);
clipPath.moveTo(0,0);
clipPath.lineTo(width, 0);
clipPath.lineTo(centerX, centerX);
clipPath.closeSubpath();
clipPath.moveTo(width, height);
clipPath.lineTo(0, height);
clipPath.lineTo(centerX, south.y());
clipPath.closeSubpath();
linearGradient.setStart(centerX, centerX);
linearGradient.setFinalStop(centerX, 0);
painter.setClipPath(clipPath);
painter.setBrush(linearGradient);
painter.drawRect(north);
linearGradient.setStart(centerX, south.y());
linearGradient.setFinalStop(centerX, height);
painter.setBrush(linearGradient);
painter.drawRect(south);
} else {
QRectF north(0,0,width, centerY);
QRectF south(0, centerY, width, centerY);
linearGradient.setStart(centerX, centerY);
linearGradient.setFinalStop(centerX, 0);
painter.setBrush(linearGradient);
painter.drawRect(north);
linearGradient.setFinalStop(centerX, height);
painter.setBrush(linearGradient);
painter.drawRect(south);
QRectF west(0,0,centerY, height);
QRectF east(width - centerY, 0, centerY, height);
clipPath.moveTo(0,0);
clipPath.lineTo(centerY, centerY);
clipPath.lineTo(0,height);
clipPath.closeSubpath();
clipPath.moveTo(width, height);
clipPath.lineTo(east.x(), centerY);
clipPath.lineTo(width,0);
clipPath.closeSubpath();
linearGradient.setStart(centerY, centerY);
linearGradient.setFinalStop(0, centerY);
painter.setClipPath(clipPath);
painter.setBrush(linearGradient);
painter.drawRect(west);
linearGradient.setStart(east.x(), centerY);
linearGradient.setFinalStop(width, centerY);
painter.setBrush(linearGradient);
painter.drawRect(east);
}
}
void KoOdfGradientBackground::debug() const
{
Q_D(const KoOdfGradientBackground);
qDebug() << "cx,cy: "<< d->cx << d->cy;
qDebug() << "style" << d->style;
qDebug() << "colors" << d->startColor << d->endColor;
qDebug() << "angle:" << d->angle;
qDebug() << "border" << d->border;
}
|