QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgssvgcache.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgssvgcache.h
3  ------------------------------
4  begin : 2011
5  copyright : (C) 2011 by Marco Hugentobler
6  email : marco dot hugentobler at sourcepole dot ch
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGSSVGCACHE_H
19 #define QGSSVGCACHE_H
20 
22 #include "qgis.h"
23 
24 #include <QPicture>
25 
26 class QDomElement;
27 
28 #ifndef SIP_RUN
29 
31 
36 class CORE_EXPORT QgsSvgCacheEntry : public QgsAbstractContentCacheEntry
37 {
38  public:
39 
50  QgsSvgCacheEntry( const QString &path, double size, double strokeWidth, double widthScaleFactor, const QColor &fill, const QColor &stroke,
51  double fixedAspectRatio = 0 ) ;
52 
54  QgsSvgCacheEntry( const QgsSvgCacheEntry &rh ) = delete;
56  QgsSvgCacheEntry &operator=( const QgsSvgCacheEntry &rh ) = delete;
57 
58  double size = 0.0; //size in pixels (cast to int for QImage)
59  double strokeWidth = 0;
60  double widthScaleFactor = 1.0;
61 
63  double fixedAspectRatio = 0;
64 
69  QSizeF viewboxSize;
70 
71  QColor fill = Qt::black;
72  QColor stroke = Qt::black;
73  std::unique_ptr< QImage > image;
74  std::unique_ptr< QPicture > picture;
75  //content (with params replaced)
76  QByteArray svgContent;
77 
83  bool isMissingImage = false;
84 
85  bool isEqual( const QgsAbstractContentCacheEntry *other ) const override;
86  int dataSize() const override;
87  void dump() const override;
88 
89 };
90 
92 #endif
93 
103 #ifdef SIP_RUN
104 class CORE_EXPORT QgsSvgCache : public QgsAbstractContentCacheBase // for sip we skip to the base class and avoid the template difficulty
105 {
106 #else
107 class CORE_EXPORT QgsSvgCache : public QgsAbstractContentCache< QgsSvgCacheEntry >
108 {
109 #endif
110  Q_OBJECT
111 
112  public:
113 
117  QgsSvgCache( QObject *parent SIP_TRANSFERTHIS = nullptr );
118 
133  QImage svgAsImage( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
134  double widthScaleFactor, bool &fitsInCache, double fixedAspectRatio = 0, bool blocking = false );
135 
150  QPicture svgAsPicture( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
151  double widthScaleFactor, bool forceVectorOutput = false, double fixedAspectRatio = 0, bool blocking = false );
152 
168  QSizeF svgViewboxSize( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
169  double widthScaleFactor, double fixedAspectRatio = 0, bool blocking = false );
170 
177  void containsParams( const QString &path, bool &hasFillParam, QColor &defaultFillColor, bool &hasStrokeParam, QColor &defaultStrokeColor, bool &hasStrokeWidthParam,
178  double &defaultStrokeWidth, bool blocking = false ) const;
179 
205  void containsParams( const QString &path, bool &hasFillParam, bool &hasDefaultFillParam, QColor &defaultFillColor,
206  bool &hasFillOpacityParam, bool &hasDefaultFillOpacity, double &defaultFillOpacity,
207  bool &hasStrokeParam, bool &hasDefaultStrokeColor, QColor &defaultStrokeColor,
208  bool &hasStrokeWidthParam, bool &hasDefaultStrokeWidth, double &defaultStrokeWidth,
209  bool &hasStrokeOpacityParam, bool &hasDefaultStrokeOpacity, double &defaultStrokeOpacity,
210  bool blocking = false ) const SIP_PYNAME( containsParamsV3 );
211 
225  QByteArray getImageData( const QString &path, bool blocking = false ) const;
226 
228 
243 #ifndef SIP_RUN
244  QByteArray svgContent( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
245  double widthScaleFactor, double fixedAspectRatio = 0, bool blocking = false, bool *isMissingImage = nullptr );
246 #else
247  QByteArray svgContent( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
248  double widthScaleFactor, double fixedAspectRatio = 0, bool blocking = false );
249 #endif
250 
251  signals:
252 
257  Q_DECL_DEPRECATED void statusChanged( const QString &statusQString ) SIP_DEPRECATED;
258 
263  void remoteSvgFetched( const QString &url );
264 
265  protected:
266 
267  bool checkReply( QNetworkReply *reply, const QString &path ) const override;
268 
269  private:
270 
271  void replaceParamsAndCacheSvg( QgsSvgCacheEntry *entry, bool blocking = false );
272  void cacheImage( QgsSvgCacheEntry *entry );
273  void cachePicture( QgsSvgCacheEntry *entry, bool forceVectorOutput = false );
275  QgsSvgCacheEntry *cacheEntry( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
276  double widthScaleFactor, double fixedAspectRatio = 0, bool blocking = false, bool *isMissingImage = nullptr );
277 
279  void replaceElemParams( QDomElement &elem, const QColor &fill, const QColor &stroke, double strokeWidth );
280 
281  void containsElemParams( const QDomElement &elem,
282  bool &hasFillParam, bool &hasDefaultFill, QColor &defaultFill,
283  bool &hasFillOpacityParam, bool &hasDefaultFillOpacity, double &defaultFillOpacity,
284  bool &hasStrokeParam, bool &hasDefaultStroke, QColor &defaultStroke,
285  bool &hasStrokeWidthParam, bool &hasDefaultStrokeWidth, double &defaultStrokeWidth,
286  bool &hasStrokeOpacityParam, bool &hasDefaultStrokeOpacity, double &defaultStrokeOpacity ) const SIP_PYNAME( containsParamsV3 );
287 
289  double calcSizeScaleFactor( QgsSvgCacheEntry *entry, const QDomElement &docElem, QSizeF &viewboxSize ) const;
290 
295  QSize sizeForImage( const QgsSvgCacheEntry &entry, QSizeF &viewBoxSize, QSizeF &scaledSize ) const;
296 
300  QImage imageFromCachedPicture( const QgsSvgCacheEntry &entry ) const;
301 
303  QByteArray mMissingSvg;
304 
305  QByteArray mFetchingSvg;
306 
307  friend class TestQgsSvgCache;
308 };
309 
310 #endif // QGSSVGCACHE_H
SIP_PYNAME
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
qgis.h
QgsAbstractContentCacheEntry::isEqual
virtual bool isEqual(const QgsAbstractContentCacheEntry *other) const =0
Tests whether this entry matches another entry.
QgsAbstractContentCacheEntry::dump
virtual void dump() const =0
Dumps debugging strings containing the item's properties.
SIP_DEPRECATED
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
QgsSvgCache
A cache for images / pictures derived from svg files.
Definition: qgssvgcache.h:108
QgsAbstractContentCacheEntry::dataSize
virtual int dataSize() const =0
Returns the memory usage in bytes for the entry.
QgsAbstractContentCacheBase::checkReply
virtual bool checkReply(QNetworkReply *reply, const QString &path) const
Runs additional checks on a network reply to ensure that the reply content is consistent with that re...
Definition: qgsabstractcontentcache.h:154
QgsAbstractContentCacheEntry
Base class for entries in a QgsAbstractContentCache.
Definition: qgsabstractcontentcache.h:48
QgsSvgCache::statusChanged
Q_DECL_DEPRECATED void statusChanged(const QString &statusQString)
Emit a signal to be caught by qgisapp and display a msg on status bar.
QgsAbstractContentCacheEntry::operator=
QgsAbstractContentCacheEntry & operator=(const QgsAbstractContentCacheEntry &rh)=delete
QgsAbstractContentCacheEntry cannot be copied.
QgsAbstractContentCacheBase
A QObject derived base class for QgsAbstractContentCache.
Definition: qgsabstractcontentcache.h:131
qgsabstractcontentcache.h
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QgsAbstractContentCache
Abstract base class for file content caches, such as SVG or raster image caches.
Definition: qgsabstractcontentcache.h:190
QgsSvgCache::remoteSvgFetched
void remoteSvgFetched(const QString &url)
Emitted when the cache has finished retrieving an SVG file from a remote url.