Commit ceb6c7de authored by Jan Hambrecht's avatar Jan Hambrecht

Introduce KoShapeBackground, KoColorBackground, KoGradientBackground and KoPatternBackground

which replace QBrush as the background of KoShapes. This will make it possible to implement
background features not possible with QBrush, e.g. advanced background image options as
defined in the ODF spec 15.14.9.


svn path=/trunk/koffice/; revision=819746
parent baeba5c7
......@@ -42,8 +42,7 @@ void KisShapeSelectionModel::add(KoShape *child) {
return;
child->setBorder(0);
child->setBackground(Qt::NoBrush);
child->setBackground(0);
m_shapeMap.insert(child, child->boundingRect());
m_shapeSelection->setDirty();
......
......@@ -53,6 +53,10 @@ set(flake_SRCS
KoShapeBorderRegistry.cpp
KoLineBorder.cpp
KoLineBorderFactory.cpp
KoShapeBackground.cpp
KoColorBackground.cpp
KoGradientBackground.cpp
KoPatternBackground.cpp
KoShapeStyleWriter.cpp
KoShapeConfigWidgetBase.cpp
KoDrag.cpp
......
/* This file is part of the KDE project
* Copyright (C) 2008 Jan Hambrecht <jaham@gmx.net>
*
* 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 "KoColorBackground.h"
#include "KoShapeSavingContext.h"
#include <KoOdfGraphicStyles.h>
#include <KoOdfLoadingContext.h>
#include <KoXmlNS.h>
#include <QtGui/QColor>
#include <QtGui/QPainter>
class KoColorBackground::Private
{
public:
Private()
{
color = Qt::black;
style = Qt::SolidPattern;
};
Qt::BrushStyle style;
QColor color;
};
KoColorBackground::KoColorBackground()
: d( new Private() )
{
}
KoColorBackground::KoColorBackground( const QColor &color, Qt::BrushStyle style )
: d( new Private() )
{
if( style < Qt::SolidPattern || style >= Qt::LinearGradientPattern )
style = Qt::SolidPattern;
d->style = style;
d->color = color;
}
KoColorBackground::~KoColorBackground()
{
delete d;
}
QColor KoColorBackground::color() const
{
return d->color;
}
Qt::BrushStyle KoColorBackground::style() const
{
return d->style;
}
void KoColorBackground::paint( QPainter &painter, const QPainterPath &fillPath ) const
{
painter.setBrush( QBrush( d->color, d->style ) );
painter.drawPath( fillPath );
}
void KoColorBackground::fillStyle( KoGenStyle &style, KoShapeSavingContext &context )
{
KoOdfGraphicStyles::saveOasisFillStyle( style, context.mainStyles(), QBrush( d->color, d->style ) );
}
bool KoColorBackground::loadStyle( KoOdfLoadingContext & context, const QSizeF & )
{
KoStyleStack &styleStack = context.styleStack();
if( ! styleStack.hasProperty( KoXmlNS::draw, "fill" ) )
return false;
QString fillStyle = styleStack.property( KoXmlNS::draw, "fill" );
if( fillStyle == "solid" || fillStyle == "hatch" )
{
QBrush brush = KoOdfGraphicStyles::loadOasisFillStyle( styleStack, fillStyle, context.stylesReader() );
d->color = brush.color();
d->style = brush.style();
return true;
}
return false;
}
/* This file is part of the KDE project
* Copyright (C) 2008 Jan Hambrecht <jaham@gmx.net>
*
* 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.
*/
#ifndef KOCOLORBACKGROUND_H
#define KOCOLORBACKGROUND_H
#include "KoShapeBackground.h"
#include "flake_export.h"
#include <Qt>
class QColor;
class FLAKE_EXPORT KoColorBackground : public KoShapeBackground
{
public:
KoColorBackground();
KoColorBackground( const QColor &color, Qt::BrushStyle style = Qt::SolidPattern );
virtual ~KoColorBackground();
/// Returns the background color
QColor color() const;
/// Returns the brush style
Qt::BrushStyle style() const;
// reimplemented from KoShapeBackground
virtual void paint( QPainter &painter, const QPainterPath &fillPath ) const;
// reimplemented from KoShapeBackground
virtual void fillStyle( KoGenStyle &style, KoShapeSavingContext &context );
// reimplemented from KoShapeBackground
virtual bool loadStyle( KoOdfLoadingContext & context, const QSizeF &shapeSize );
private:
class Private;
Private * const d;
};
#endif // KOCOLORBACKGROUND_H
/* This file is part of the KDE project
* Copyright (C) 2008 Jan Hambrecht <jaham@gmx.net>
*
* 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 "KoGradientBackground.h"
#include <KoStyleStack.h>
#include <KoXmlNS.h>
#include <KoOdfLoadingContext.h>
#include <KoOdfGraphicStyles.h>
#include <KoShapeSavingContext.h>
#include <KDebug>
#include <QtGui/QBrush>
#include <QtGui/QPainter>
class KoGradientBackground::Private
{
public:
Private( QGradient * g ) : gradient(g) {};
~Private()
{
delete gradient;
}
QGradient * gradient;
QMatrix matrix;
};
KoGradientBackground::KoGradientBackground( QGradient * gradient )
: d( new Private( gradient ) )
{
Q_ASSERT(d->gradient);
}
KoGradientBackground::KoGradientBackground( const QGradient & gradient )
: d( new Private( cloneGradient( &gradient ) ) )
{
Q_ASSERT(d->gradient);
}
KoGradientBackground::~KoGradientBackground()
{
delete d;
}
void KoGradientBackground::setMatrix( const QMatrix &matrix )
{
d->matrix = matrix;
}
QMatrix KoGradientBackground::matrix() const
{
return d->matrix;
}
void KoGradientBackground::setGradient( QGradient * gradient )
{
if( d->gradient )
delete d->gradient;
d->gradient = gradient;
Q_ASSERT(d->gradient);
}
void KoGradientBackground::setGradient( const QGradient & gradient )
{
if( d->gradient )
delete d->gradient;
d->gradient = cloneGradient( &gradient );
Q_ASSERT(d->gradient);
}
const QGradient * KoGradientBackground::gradient() const
{
return d->gradient;
}
KoGradientBackground& KoGradientBackground::operator = ( const KoGradientBackground &rhs )
{
if( this == &rhs )
return *this;
d->matrix = rhs.d->matrix;
delete d->gradient;
d->gradient = cloneGradient( rhs.d->gradient );
Q_ASSERT( d->gradient );
return *this;
}
void KoGradientBackground::paint( QPainter &painter, const QPainterPath &fillPath ) const
{
QBrush brush( *d->gradient );
brush.setMatrix( d->matrix );
painter.setBrush( brush );
painter.drawPath( fillPath );
}
void KoGradientBackground::fillStyle( KoGenStyle &style, KoShapeSavingContext &context )
{
QBrush brush( *d->gradient );
brush.setMatrix( d->matrix );
KoOdfGraphicStyles::saveOasisFillStyle( style, context.mainStyles(), brush );
}
bool KoGradientBackground::loadStyle( KoOdfLoadingContext & context, const QSizeF &shapeSize )
{
KoStyleStack &styleStack = context.styleStack();
if( ! styleStack.hasProperty( KoXmlNS::draw, "fill" ) )
return false;
QString fillStyle = styleStack.property( KoXmlNS::draw, "fill" );
if ( fillStyle == "solid" || fillStyle == "hatch" )
{
QBrush brush = KoOdfGraphicStyles::loadOasisGradientStyle( styleStack, context.stylesReader(), shapeSize );
// TODO copy gradient here
d->matrix = brush.matrix();
return true;
}
return false;
}
QGradient * KoGradientBackground::cloneGradient( const QGradient * gradient ) const
{
if( ! gradient )
return 0;
QGradient * clone = 0;
switch( gradient->type() )
{
case QGradient::LinearGradient:
{
const QLinearGradient * lg = static_cast<const QLinearGradient*>( gradient );
clone = new QLinearGradient( lg->start(), lg->finalStop() );
break;
}
case QGradient::RadialGradient:
{
const QRadialGradient * rg = static_cast<const QRadialGradient*>( gradient );
clone = new QRadialGradient( rg->center(), rg->radius(), rg->focalPoint() );
break;
}
case QGradient::ConicalGradient:
{
const QConicalGradient * cg = static_cast<const QConicalGradient*>( gradient );
clone = new QConicalGradient( cg->center(), cg->angle() );
break;
}
default:
return 0;
}
clone->setSpread( gradient->spread() );
clone->setStops( gradient->stops() );
return clone;
}
/* This file is part of the KDE project
* Copyright (C) 2008 Jan Hambrecht <jaham@gmx.net>
*
* 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.
*/
#ifndef KOGRADIENTBACKGROUND_H
#define KOGRADIENTBACKGROUND_H
#include "KoShapeBackground.h"
#include "flake_export.h"
#include <QtGui/QMatrix>
#include <QtGui/QGradient>
class FLAKE_EXPORT KoGradientBackground : public KoShapeBackground
{
public:
/**
* Creates new gradient background from given gradient.
* The background takes ownership of the given gradient.
*/
KoGradientBackground( QGradient * gradient );
/**
* Create new gradient background from the given gradient.
* A clone of the given gradient is used.
*/
KoGradientBackground( const QGradient & gradient );
/// Destroys the background
virtual ~KoGradientBackground();
/// Sets the transform matrix
void setMatrix( const QMatrix &matrix );
/// Returns the transform matrix
QMatrix matrix() const;
/**
* Sets a new gradient.
* The background takes ownership of the given gradient.
*/
void setGradient( QGradient * gradient );
/**
* Sets a new gradient.
* A clone of the given gradient is used.
*/
void setGradient( const QGradient & gradient );
/// Returns the gradient
const QGradient * gradient() const;
/// Assignment operator
KoGradientBackground& operator = ( const KoGradientBackground &rhs );
/// reimplemented from KoShapeBackground
virtual void paint( QPainter &painter, const QPainterPath &fillPath ) const;
/// reimplemented from KoShapeBackground
virtual void fillStyle( KoGenStyle &style, KoShapeSavingContext &context );
/// reimplemented from KoShapeBackground
virtual bool loadStyle( KoOdfLoadingContext & context, const QSizeF &shapeSize );
private:
QGradient * cloneGradient( const QGradient * gradient ) const;
class Private;
Private * const d;
};
#endif // KOGRADIENTBACKGROUND_H
......@@ -28,6 +28,7 @@
#include "KoShapeSavingContext.h"
#include "KoShapeLoadingContext.h"
#include "KoShapeShadow.h"
#include "KoShapeBackground.h"
#include <KoXmlReader.h>
#include <KoXmlWriter.h>
......@@ -232,8 +233,8 @@ void KoPathShape::paint( QPainter &painter, const KoViewConverter &converter )
QPainterPath path( outline() );
path.setFillRule( d->fillRule );
painter.setBrush( background() );
painter.drawPath( path );
if( background() )
background()->paint( painter, path );
//paintDebug( painter );
}
......
/* This file is part of the KDE project
* Copyright (C) 2008 Jan Hambrecht <jaham@gmx.net>
*
* 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 "KoPatternBackground.h"
#include "KoShapeStyleWriter.h"
#include <KoStyleStack.h>
#include <KoXmlNS.h>
#include <KoOdfLoadingContext.h>
#include <KoOdfGraphicStyles.h>
#include <QtGui/QBrush>
#include <QtGui/QPainter>
class KoPatternBackground::Private
{
public:
Private()
{
}
QImage pattern;
QMatrix matrix;
};
KoPatternBackground::KoPatternBackground()
: d( new Private() )
{
}
KoPatternBackground::KoPatternBackground( const QImage &pattern )
: d( new Private() )
{
d->pattern = pattern;
}
KoPatternBackground::~KoPatternBackground()
{
delete d;
}
void KoPatternBackground::setMatrix( const QMatrix &matrix )
{
d->matrix = matrix;
}
QMatrix KoPatternBackground::matrix() const
{
return d->matrix;
}
void KoPatternBackground::setPattern( const QImage &pattern )
{
d->pattern = pattern;
}
QImage KoPatternBackground::pattern()
{
return d->pattern;
}
KoPatternBackground& KoPatternBackground::operator = ( const KoPatternBackground &rhs )
{
if( this == &rhs )
return *this;
d->matrix = rhs.d->matrix;
d->pattern = rhs.d->pattern;
return *this;
}
void KoPatternBackground::paint( QPainter &painter, const QPainterPath &fillPath ) const
{
QBrush brush( d->pattern );
brush.setMatrix( d->matrix );
painter.setBrush( brush );
painter.drawPath( fillPath );
}
void KoPatternBackground::fillStyle( KoGenStyle &style, KoShapeSavingContext &context )
{
QBrush brush( d->pattern );
brush.setMatrix( d->matrix );
KoShapeStyleWriter styleWriter( context );
styleWriter.addFillStyle( style, brush );
}
bool KoPatternBackground::loadStyle( KoOdfLoadingContext & context, const QSizeF &shapeSize )
{
KoStyleStack &styleStack = context.styleStack();
if( ! styleStack.hasProperty( KoXmlNS::draw, "fill" ) )
return false;
QString fillStyle = styleStack.property( KoXmlNS::draw, "fill" );
if( fillStyle == "bitmap" )
{
QBrush brush = KoOdfGraphicStyles::loadOasisPatternStyle( styleStack, context, shapeSize );
d->matrix = brush.matrix();
d->pattern = brush.textureImage();
return true;
}
return false;
}
/* This file is part of the KDE project
* Copyright (C) 2008 Jan Hambrecht <jaham@gmx.net>
*
* 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.
*/
#ifndef KOPATTERNBACKGROUND_H
#define KOPATTERNBACKGROUND_H
#include "KoShapeBackground.h"
#include "flake_export.h"
#include <QtGui/QMatrix>
class FLAKE_EXPORT KoPatternBackground : public KoShapeBackground
{
public:
KoPatternBackground();
KoPatternBackground( const QImage &pattern );
virtual ~KoPatternBackground();
/// Sets the transform matrix
void setMatrix( const QMatrix &matrix );
/// Returns the transform matrix
QMatrix matrix() const;
/// Sets a new pattern
void setPattern( const QImage &pattern );
/// Returns the pattern
QImage pattern();
/// Assignment operator
KoPatternBackground& operator = ( const KoPatternBackground &rhs );
/// reimplemented from KoShapeBackground
virtual void paint( QPainter &painter, const QPainterPath &fillPath ) const;
/// reimplemented from KoShapeBackground
virtual void fillStyle( KoGenStyle &style, KoShapeSavingContext &context );
/// reimplemented from KoShapeBackground
virtual bool loadStyle( KoOdfLoadingContext & context, const QSizeF &shapeSize );
private:
class Private;
Private * const d;
};
#endif // KOPATTERNBACKGROUND_H
......@@ -28,6 +28,10 @@
#include "KoPointerEvent.h"
#include "KoInsets.h"
#include "KoShapeBorderModel.h"
#include "KoShapeBackground.h"
#include "KoColorBackground.h"
#include "KoGradientBackground.h"
#include "KoPatternBackground.h"
#include "KoShapeManager.h"
#include "KoShapeUserData.h"
#include "KoShapeApplicationData.h"
......@@ -45,6 +49,7 @@
#include <KoXmlWriter.h>
#include <KoXmlNS.h>
#include <KoGenStyle.h>
#include <KoGenStyles.h>
#include <KoUnit.h>
#include <KoOdfStylesReader.h>
#include <KoOdfGraphicStyles.h>
......@@ -75,7 +80,7 @@ public:
detectCollision( false ),
userData(0),
appData(0),
backgroundBrush(Qt::NoBrush),
fill(0),
border(0),
me(shape),
shadow(0)
......@@ -96,6 +101,8 @@ public:
}
if( shadow && shadow->removeUser() == 0 )
delete shadow;
if( fill && fill->removeUser() == 0 )
delete fill;
qDeleteAll( eventActions );
}
......@@ -128,7 +135,7 @@ public:
QSet<KoShapeManager *> shapeManagers;
KoShapeUserData *userData;
KoShapeApplicationData *appData;
QBrush backgroundBrush; ///< Stands for the background color / fill etc.
KoShapeBackground * fill; ///< Stands for the background color / fill etc.
KoShapeBorderModel *border; ///< points to a border, or 0 if there is no border
KoShape *me;
QList<KoShape*> dependees; ///< list of shape dependent on this shape
......@@ -472,9 +479,10 @@ KoShapeApplicationData *KoShape::applicationData() const {