introduce ContentType; this lets us have svgs which are a single image which may be made up of a number of elements (in which case, the elements need to be scaled from the size on the svg when painted to have the right size) or made up of a number of discreet images each of which is already the right size (think: a deck of cards)

svn path=/trunk/KDE/kdebase/workspace/plasma/lib/; revision=669616
This commit is contained in:
Aaron J. Seigo 2007-05-29 20:27:51 +00:00
parent 429d8fd708
commit c321d2f03e
2 changed files with 70 additions and 13 deletions

43
svg.cpp
View File

@ -59,8 +59,9 @@ class SharedSvgRenderer : public KSvgRenderer, public QSharedData
class Svg::Private
{
public:
Private( const QString& imagePath )
: renderer( 0 )
Private(const QString& imagePath)
: renderer(0),
contentType(Svg::SingleImage)
{
if (QDir::isAbsolutePath(themePath)) {
path = imagePath;
@ -101,6 +102,7 @@ class Svg::Private
if (!elementId.isEmpty()) {
id.append(elementId);
}
//kDebug() << "id is " << id << endl;
if (QPixmapCache::find(id, p)) {
//kDebug() << "found cached version of " << id << endl;
@ -111,7 +113,7 @@ class Svg::Private
// we have to re-render this puppy
QSize s;
if (elementId.isEmpty()) {
if (elementId.isEmpty() || contentType == Svg::ImageSet) {
s = size.toSize();
} else {
s = elementSize(elementId);
@ -146,7 +148,7 @@ class Svg::Private
QHash<QString, SharedSvgRenderer::Ptr>::const_iterator it = renderers.find(path);
if (it != renderers.end()) {
kDebug() << "gots us an existing one!" << endl;
//kDebug() << "gots us an existing one!" << endl;
renderer = it.value();
} else {
renderer = new SharedSvgRenderer(path);
@ -154,14 +156,14 @@ class Svg::Private
}
}
QSize elementSize( const QString& elementId )
QSize elementSize(const QString& elementId)
{
createRenderer();
QSizeF elementSize = renderer->boundsOnElement(elementId).size();
QSizeF naturalSize = renderer->defaultSize();
qreal dx = size.width() / naturalSize.width();
qreal dy = size.height() / naturalSize.height();
elementSize.scale( elementSize.width() * dx, elementSize.height() * dy, Qt::IgnoreAspectRatio );
elementSize.scale(elementSize.width() * dx, elementSize.height() * dy, Qt::IgnoreAspectRatio);
return elementSize.toSize();
}
@ -173,13 +175,14 @@ class Svg::Private
QString id;
QSizeF size;
bool themed;
Svg::ContentType contentType;
};
QHash<QString, SharedSvgRenderer::Ptr> Svg::Private:: renderers;
Svg::Svg( const QString& imagePath, QObject* parent )
: QObject( parent ),
d( new Private( imagePath ) )
Svg::Svg(const QString& imagePath, QObject* parent)
: QObject(parent),
d(new Private(imagePath))
{
if (d->themed) {
connect(Plasma::Theme::self(), SIGNAL(changed()), this, SLOT(themeChanged()));
@ -195,18 +198,20 @@ void Svg::paint(QPainter* painter, const QPointF& point, const QString& elementI
{
QPixmap pix;
d->findInCache(pix, elementID);
painter->drawPixmap(point.toPoint(), pix);
//kDebug() << "pix size is " << pix.size() << endl;
painter->drawPixmap(QRectF(point, pix.size()), pix, QRectF(QPointF(0,0), pix.size()));
}
void Svg::paint(QPainter* painter, int x, int y, const QString& elementID)
{
paint(painter, QPointF( x, y ), elementID);
paint(painter, QPointF(x, y), elementID);
}
void Svg::paint(QPainter* painter, const QRectF& rect, const QString& elementID)
{
QPixmap pix;
d->findInCache(pix, elementID);
//kDebug() << "pix size is " << pix.size() << endl;
painter->drawPixmap(rect, pix, QRectF(QPointF(0,0), pix.size()));
}
@ -231,11 +236,27 @@ QSize Svg::elementSize(const QString& elementId) const
return d->elementSize(elementId);
}
bool Svg::elementExists(const QString& elementId) const
{
d->createRenderer();
return d->renderer->elementExists(elementId);
}
QSize Svg::size() const
{
return d->size.toSize();
}
void Svg::setContentType(ContentType contentType)
{
d->contentType = contentType;
}
Svg::ContentType Svg::contentType()
{
return d->contentType;
}
void Svg::themeChanged()
{
d->removeFromCache();

40
svg.h
View File

@ -48,6 +48,20 @@ class PLASMA_EXPORT Svg : public QObject
Q_OBJECT
public:
/**
* Describes the contents of the Svg, which determines how elements
* are drawn.
*/
enum ContentType { SingleImage = 0 /**< A set of elements that together
make an image. Elements may be
drawn separately to accomplish
this. */,
ImageSet /**< A set of elements, each of which
constitutes a whole image. Each
element will therefore be rendered
to the set size of the SVG */
};
/**
* Constructs an SVG object that implicitly shares and caches rendering
* As opposed to QSvgRenderer, which this class uses internally,
@ -58,14 +72,15 @@ class PLASMA_EXPORT Svg : public QObject
* The size is initialized to be the SVG's native size.
*
* @arg imagePath the image to show. If a relative path is passed, then
* Plasma::Theme is used to load the SVG.
* Plasma::Theme is used to load the SVG.
* @arg parent options QObject to parent this to
*
* @related Plasma::Theme
*/
explicit Svg( const QString& imagePath, QObject* parent = 0 );
explicit Svg(const QString& imagePath, QObject* parent = 0);
~Svg();
/**
* Paints the SVG represented by this object
* @arg painter the QPainter to use
@ -122,12 +137,33 @@ class PLASMA_EXPORT Svg : public QObject
**/
QSize elementSize( const QString& elementId ) const;
/**
* Check when an element exists in the loaded Svg
* @arg elementId the id of the element to check
* @return true if the element is defined in the Svg, otherwise false
**/
bool elementExists( const QString& elementId ) const;
/**
* Currently set size of the SVG
* @return the current size of a given element
**/
QSize size() const;
/**
* Sets what sort of content is in the Svg.
* @see ContentType
* @arg contents whether the Svg is a single image or a set of images
*/
void setContentType(ContentType contentType);
/**
* Returns the content type of the Svg
* @see SetContentType
* @arg contents whether the Svg is a single image or a set of images
*/
ContentType contentType();
Q_SIGNALS:
void repaintNeeded();