2008-11-04 00:08:39 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2006-2007 Aaron Seigo <aseigo@kde.org>
|
|
|
|
*
|
|
|
|
* This program 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, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef PLASMA_SVG_H
|
|
|
|
#define PLASMA_SVG_H
|
|
|
|
|
|
|
|
#include <QtCore/QObject>
|
|
|
|
#include <QtGui/QPixmap>
|
|
|
|
|
|
|
|
#include <plasma/plasma_export.h>
|
|
|
|
|
|
|
|
class QPainter;
|
|
|
|
class QPoint;
|
|
|
|
class QPointF;
|
|
|
|
class QRect;
|
|
|
|
class QRectF;
|
|
|
|
class QSize;
|
|
|
|
class QSizeF;
|
|
|
|
class QMatrix;
|
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
class FrameSvgPrivate;
|
2009-05-18 14:26:57 +02:00
|
|
|
class SvgPrivate;
|
|
|
|
class Theme;
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @class Svg plasma/svg.h <Plasma/Svg>
|
|
|
|
*
|
|
|
|
* @short A theme aware image-centric SVG class
|
|
|
|
*
|
|
|
|
* Plasma::Svg provides a class for rendering SVG images to a QPainter in a
|
|
|
|
* convenient manner. Unless an absolute path to a file is provided, it loads
|
|
|
|
* the SVG document using Plasma::Theme. It also provides a number of internal
|
|
|
|
* optimizations to help lower the cost of painting SVGs, such as caching.
|
|
|
|
*
|
|
|
|
* @see Plasma::FrameSvg
|
|
|
|
**/
|
|
|
|
class PLASMA_EXPORT Svg : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
Q_ENUMS(ContentType)
|
2010-12-06 22:54:29 +01:00
|
|
|
Q_PROPERTY(QSize size READ size WRITE resize NOTIFY sizeChanged)
|
2008-11-04 00:08:39 +01:00
|
|
|
Q_PROPERTY(bool multipleImages READ containsMultipleImages WRITE setContainsMultipleImages)
|
|
|
|
Q_PROPERTY(QString imagePath READ imagePath WRITE setImagePath)
|
2009-05-13 18:13:56 +02:00
|
|
|
Q_PROPERTY(bool usingRenderingCache READ isUsingRenderingCache WRITE setUsingRenderingCache)
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
2011-05-05 02:18:41 +02:00
|
|
|
* Constructs an SVG object that implicitly shares and caches rendering.
|
|
|
|
*
|
|
|
|
* Unlike QSvgRenderer, which this class uses internally,
|
2008-11-04 00:08:39 +01:00
|
|
|
* Plasma::Svg represents an image generated from an SVG. As such, it
|
|
|
|
* has a related size and transform matrix (the latter being provided
|
|
|
|
* by the painter used to paint the image).
|
|
|
|
*
|
|
|
|
* The size is initialized to be the SVG's native size.
|
|
|
|
*
|
|
|
|
* @arg parent options QObject to parent this to
|
|
|
|
*
|
|
|
|
* @related Plasma::Theme
|
|
|
|
*/
|
|
|
|
explicit Svg(QObject *parent = 0);
|
|
|
|
~Svg();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a pixmap of the SVG represented by this object.
|
|
|
|
*
|
2011-05-05 02:18:41 +02:00
|
|
|
* The size of the pixmap will be the size of this Svg object (size())
|
|
|
|
* if containsMultipleImages is @c true; otherwise, it will be the
|
|
|
|
* size of the requested element after the whole SVG has been scaled
|
|
|
|
* to size().
|
|
|
|
*
|
|
|
|
* @arg elementId the ID string of the element to render, or an empty
|
|
|
|
* string for the whole SVG (the default)
|
2008-11-04 00:08:39 +01:00
|
|
|
* @return a QPixmap of the rendered SVG
|
|
|
|
*/
|
|
|
|
Q_INVOKABLE QPixmap pixmap(const QString &elementID = QString());
|
|
|
|
|
|
|
|
/**
|
2011-05-05 02:18:41 +02:00
|
|
|
* Paints all or part of the SVG represented by this object
|
|
|
|
*
|
|
|
|
* The size of the painted area will be the size of this Svg object
|
|
|
|
* (size()) if containsMultipleImages is @c true; otherwise, it will
|
|
|
|
* be the size of the requested element after the whole SVG has been
|
|
|
|
* scaled to size().
|
|
|
|
*
|
|
|
|
* @arg painter the QPainter to use
|
|
|
|
* @arg point the position to start drawing; the entire svg will be
|
|
|
|
* drawn starting at this point.
|
|
|
|
* @arg elementId the ID string of the element to render, or an empty
|
|
|
|
* string for the whole SVG (the default)
|
2008-11-04 00:08:39 +01:00
|
|
|
*/
|
|
|
|
Q_INVOKABLE void paint(QPainter *painter, const QPointF &point,
|
|
|
|
const QString &elementID = QString());
|
|
|
|
|
|
|
|
/**
|
2011-05-05 02:18:41 +02:00
|
|
|
* Paints all or part of the SVG represented by this object
|
|
|
|
*
|
|
|
|
* The size of the painted area will be the size of this Svg object
|
|
|
|
* (size()) if containsMultipleImages is @c true; otherwise, it will
|
|
|
|
* be the size of the requested element after the whole SVG has been
|
|
|
|
* scaled to size().
|
|
|
|
*
|
|
|
|
* @arg painter the QPainter to use
|
|
|
|
* @arg x the horizontal coordinate to start painting from
|
|
|
|
* @arg y the vertical coordinate to start painting from
|
|
|
|
* @arg elementId the ID string of the element to render, or an empty
|
|
|
|
* string for the whole SVG (the default)
|
2008-11-04 00:08:39 +01:00
|
|
|
*/
|
|
|
|
Q_INVOKABLE void paint(QPainter *painter, int x, int y,
|
|
|
|
const QString &elementID = QString());
|
|
|
|
|
|
|
|
/**
|
2011-05-05 02:18:41 +02:00
|
|
|
* Paints all or part of the SVG represented by this object
|
|
|
|
*
|
|
|
|
* @arg painter the QPainter to use
|
|
|
|
* @arg rect the rect to draw into; if smaller than the current size
|
|
|
|
* the drawing is starting at this point.
|
|
|
|
* @arg elementId the ID string of the element to render, or an empty
|
|
|
|
* string for the whole SVG (the default)
|
2008-11-04 00:08:39 +01:00
|
|
|
*/
|
|
|
|
Q_INVOKABLE void paint(QPainter *painter, const QRectF &rect,
|
|
|
|
const QString &elementID = QString());
|
|
|
|
|
|
|
|
/**
|
2011-05-05 02:18:41 +02:00
|
|
|
* Paints all or part of the SVG represented by this object
|
|
|
|
*
|
|
|
|
* @arg painter the QPainter to use
|
|
|
|
* @arg x the horizontal coordinate to start painting from
|
|
|
|
* @arg y the vertical coordinate to start painting from
|
|
|
|
* @arg width the width of the element to draw
|
|
|
|
* @arg height the height of the element do draw
|
|
|
|
* @arg elementId the ID string of the element to render, or an empty
|
|
|
|
* string for the whole SVG (the default)
|
2008-11-04 00:08:39 +01:00
|
|
|
*/
|
|
|
|
Q_INVOKABLE void paint(QPainter *painter, int x, int y, int width,
|
|
|
|
int height, const QString &elementID = QString());
|
|
|
|
|
|
|
|
/**
|
2011-05-05 02:18:41 +02:00
|
|
|
* The size of the SVG.
|
|
|
|
*
|
|
|
|
* If the SVG has been resized with resize(), that size will be
|
|
|
|
* returned; otherwise, the natural size of the SVG will be returned.
|
|
|
|
*
|
|
|
|
* If containsMultipleImages is @c true, each element of the SVG
|
|
|
|
* will be rendered at this size by default.
|
|
|
|
*
|
2008-12-16 17:44:51 +01:00
|
|
|
* @return the current size of the SVG
|
2008-11-04 00:08:39 +01:00
|
|
|
**/
|
|
|
|
QSize size() const;
|
|
|
|
|
|
|
|
/**
|
2011-05-05 02:18:41 +02:00
|
|
|
* Resizes the rendered image.
|
|
|
|
*
|
|
|
|
* Rendering will actually take place on the next call to paint.
|
|
|
|
*
|
|
|
|
* If containsMultipleImages is @c true, each element of the SVG
|
|
|
|
* will be rendered at this size by default; otherwise, the entire
|
|
|
|
* image will be scaled to this size and each element will be
|
|
|
|
* scaled appropriately.
|
|
|
|
*
|
|
|
|
* @arg width the new width
|
|
|
|
* @arg height the new height
|
2008-11-04 00:08:39 +01:00
|
|
|
**/
|
|
|
|
Q_INVOKABLE void resize(qreal width, qreal height);
|
|
|
|
|
|
|
|
/**
|
2011-05-05 02:18:41 +02:00
|
|
|
* Resizes the rendered image.
|
|
|
|
*
|
|
|
|
* Rendering will actually take place on the next call to paint.
|
|
|
|
*
|
|
|
|
* If containsMultipleImages is @c true, each element of the SVG
|
|
|
|
* will be rendered at this size by default; otherwise, the entire
|
|
|
|
* image will be scaled to this size and each element will be
|
|
|
|
* scaled appropriately.
|
|
|
|
*
|
|
|
|
* @arg size the new size of the image
|
2008-11-04 00:08:39 +01:00
|
|
|
**/
|
|
|
|
Q_INVOKABLE void resize(const QSizeF &size);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resizes the rendered image to the natural size of the SVG.
|
2011-05-05 02:18:41 +02:00
|
|
|
*
|
2008-11-04 00:08:39 +01:00
|
|
|
* Rendering will actually take place on the next call to paint.
|
|
|
|
**/
|
|
|
|
Q_INVOKABLE void resize();
|
|
|
|
|
|
|
|
/**
|
2011-05-05 02:18:41 +02:00
|
|
|
* Find the size of a given element.
|
|
|
|
*
|
|
|
|
* This is the size of the element with ID @p elementId after the SVG
|
|
|
|
* has been scaled (see resize()). Note that this is unaffected by
|
|
|
|
* the containsMultipleImages property.
|
|
|
|
*
|
|
|
|
* @arg elementId the id of the element to check
|
|
|
|
* @return the size of a given element, given the current size of the SVG
|
2008-11-04 00:08:39 +01:00
|
|
|
**/
|
|
|
|
Q_INVOKABLE QSize elementSize(const QString &elementId) const;
|
|
|
|
|
|
|
|
/**
|
2011-05-05 02:18:41 +02:00
|
|
|
* The bounding rect of a given element.
|
|
|
|
*
|
|
|
|
* This is the bounding rect of the element with ID @p elementId after
|
|
|
|
* the SVG has been scaled (see resize()). Note that this is
|
|
|
|
* unaffected by the containsMultipleImages property.
|
|
|
|
*
|
|
|
|
* @arg elementId the id of the element to check
|
|
|
|
* @return the current rect of a given element, given the current size of the SVG
|
2008-11-04 00:08:39 +01:00
|
|
|
**/
|
|
|
|
Q_INVOKABLE QRectF elementRect(const QString &elementId) const;
|
|
|
|
|
|
|
|
/**
|
2011-05-05 02:18:41 +02:00
|
|
|
* Check whether an element exists in the loaded SVG.
|
|
|
|
*
|
|
|
|
* @arg elementId the id of the element to check for
|
|
|
|
* @return @c true if the element is defined in the SVG, otherwise @c false
|
2008-11-04 00:08:39 +01:00
|
|
|
**/
|
|
|
|
Q_INVOKABLE bool hasElement(const QString &elementId) const;
|
|
|
|
|
|
|
|
/**
|
2011-05-05 02:18:41 +02:00
|
|
|
* Returns the element (by id) at the given point.
|
|
|
|
*
|
|
|
|
* An empty string is returned if there no element is at @p point.
|
|
|
|
*
|
|
|
|
* NOTE: not implemented! This will currently return an empty string!
|
|
|
|
*
|
|
|
|
* @arg point a point in SVG co-ordinates
|
|
|
|
* @return an empty string
|
2008-11-04 00:08:39 +01:00
|
|
|
*/
|
|
|
|
Q_INVOKABLE QString elementAtPoint(const QPoint &point) const;
|
|
|
|
|
|
|
|
/**
|
2011-05-05 02:18:41 +02:00
|
|
|
* Check whether this object is backed by a valid SVG file.
|
|
|
|
*
|
|
|
|
* This method can be expensive as it causes disk access.
|
|
|
|
*
|
|
|
|
* @return @c true if the SVG file exists and the document is valid,
|
|
|
|
* otherwise @c false.
|
2008-11-04 00:08:39 +01:00
|
|
|
**/
|
|
|
|
Q_INVOKABLE bool isValid() const;
|
|
|
|
|
|
|
|
/**
|
2011-05-05 02:18:41 +02:00
|
|
|
* Set whether the SVG contains a single image or multiple ones.
|
|
|
|
*
|
|
|
|
* If this is set to @c true, the SVG will be treated as a
|
|
|
|
* collection of related images, rather than a consistent
|
|
|
|
* drawing.
|
|
|
|
*
|
|
|
|
* In particular, when individual elements are rendered, this
|
|
|
|
* affects whether the elements are resized to size() by default.
|
|
|
|
* See paint() and pixmap().
|
|
|
|
*
|
2008-11-04 00:08:39 +01:00
|
|
|
* @arg multiple true if the svg contains multiple images
|
|
|
|
*/
|
|
|
|
void setContainsMultipleImages(bool multiple);
|
|
|
|
|
|
|
|
/**
|
2011-05-05 02:18:41 +02:00
|
|
|
* Whether the SVG contains multiple images.
|
|
|
|
*
|
|
|
|
* If this is @c true, the SVG will be treated as a
|
|
|
|
* collection of related images, rather than a consistent
|
|
|
|
* drawing.
|
|
|
|
*
|
|
|
|
* @return @c true if the SVG will be treated as containing
|
|
|
|
* multiple images, @c false if it will be treated
|
|
|
|
* as a coherent image.
|
2008-11-04 00:08:39 +01:00
|
|
|
*/
|
|
|
|
bool containsMultipleImages() const;
|
|
|
|
|
|
|
|
/**
|
2011-05-05 18:41:40 +02:00
|
|
|
* Set the SVG file to render.
|
2011-05-05 02:18:41 +02:00
|
|
|
*
|
2011-05-05 18:41:40 +02:00
|
|
|
* Relative paths are looked for in the current Plasma theme,
|
|
|
|
* and should not include the file extension (.svg and .svgz
|
|
|
|
* files will be searched for). See Theme::imagePath().
|
2011-05-05 02:18:41 +02:00
|
|
|
*
|
2011-05-05 18:41:40 +02:00
|
|
|
* If the parent object of this Svg is a Plasma::Applet,
|
|
|
|
* relative paths will be searched for in the applet's package
|
|
|
|
* first.
|
|
|
|
*
|
|
|
|
* @arg svgFilePath either an absolute path to an SVG file, or
|
|
|
|
* an image name
|
2008-11-04 00:08:39 +01:00
|
|
|
*/
|
2011-05-20 08:16:01 +02:00
|
|
|
virtual void setImagePath(const QString &svgFilePath);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
/**
|
2011-05-05 18:41:40 +02:00
|
|
|
* The SVG file to render.
|
|
|
|
*
|
|
|
|
* If this SVG is themed, this will be a relative path, and will not
|
|
|
|
* include a file extension.
|
2011-05-05 02:18:41 +02:00
|
|
|
*
|
2011-05-05 18:41:40 +02:00
|
|
|
* @return either an absolute path to an SVG file, or an image name
|
|
|
|
* @see Theme::imagePath()
|
2008-11-04 00:08:39 +01:00
|
|
|
*/
|
|
|
|
QString imagePath() const;
|
|
|
|
|
2009-05-13 18:07:58 +02:00
|
|
|
/**
|
|
|
|
* Sets whether or not to cache the results of rendering to pixmaps.
|
2011-05-05 02:18:41 +02:00
|
|
|
*
|
|
|
|
* If the SVG is resized and re-rendered often (and does not keep using the
|
|
|
|
* same small set of pixmap dimensions), then it may be less efficient to do
|
|
|
|
* disk caching. A good example might be a progress meter that uses an Svg
|
|
|
|
* object to paint itself: the meter will be changing often enough, with
|
|
|
|
* enough unpredictability and without re-use of the previous pixmaps to
|
|
|
|
* not get a gain from caching.
|
2009-05-13 18:07:58 +02:00
|
|
|
*
|
|
|
|
* Most Svg objects should use the caching feature, however.
|
|
|
|
* Therefore, the default is to use the render cache.
|
|
|
|
*
|
|
|
|
* @param useCache true to cache rendered pixmaps
|
|
|
|
* @since 4.3
|
|
|
|
*/
|
2009-05-13 18:13:56 +02:00
|
|
|
void setUsingRenderingCache(bool useCache);
|
2009-05-13 18:07:58 +02:00
|
|
|
|
|
|
|
/**
|
2011-05-05 02:18:41 +02:00
|
|
|
* Whether the rendering cache is being used.
|
|
|
|
*
|
|
|
|
* @return @c true if the Svg object is using caching for rendering results
|
2009-05-18 14:26:57 +02:00
|
|
|
* @since 4.3
|
2009-05-13 18:07:58 +02:00
|
|
|
*/
|
2009-05-13 18:13:56 +02:00
|
|
|
bool isUsingRenderingCache() const;
|
2009-05-13 18:07:58 +02:00
|
|
|
|
2009-05-18 14:26:57 +02:00
|
|
|
/**
|
2011-05-05 02:18:41 +02:00
|
|
|
* Sets the Plasma::Theme to use with this Svg object.
|
|
|
|
*
|
|
|
|
* By default, Svg objects use Plasma::Theme::default().
|
|
|
|
*
|
|
|
|
* This determines how relative image paths are interpreted.
|
|
|
|
*
|
|
|
|
* @arg theme the theme object to use
|
2009-05-18 14:26:57 +02:00
|
|
|
* @since 4.3
|
|
|
|
*/
|
2009-05-18 15:43:49 +02:00
|
|
|
void setTheme(Plasma::Theme *theme);
|
2009-05-18 14:26:57 +02:00
|
|
|
|
|
|
|
/**
|
2011-05-05 02:18:41 +02:00
|
|
|
* The Plasma::Theme used by this Svg object.
|
|
|
|
*
|
|
|
|
* This determines how relative image paths are interpreted.
|
|
|
|
*
|
|
|
|
* @return the theme used by this Svg
|
2009-05-18 14:26:57 +02:00
|
|
|
*/
|
2009-05-18 15:43:49 +02:00
|
|
|
Theme *theme() const;
|
2009-05-18 14:26:57 +02:00
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
Q_SIGNALS:
|
|
|
|
void repaintNeeded();
|
2010-12-06 22:54:29 +01:00
|
|
|
void sizeChanged();
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
SvgPrivate *const d;
|
|
|
|
|
|
|
|
Q_PRIVATE_SLOT(d, void themeChanged())
|
|
|
|
Q_PRIVATE_SLOT(d, void colorsChanged())
|
|
|
|
|
|
|
|
friend class SvgPrivate;
|
|
|
|
friend class FrameSvgPrivate;
|
2010-10-14 14:09:23 +02:00
|
|
|
friend class FrameSvg;
|
2008-11-04 00:08:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // Plasma namespace
|
|
|
|
|
|
|
|
#endif // multiple inclusion guard
|
|
|
|
|