QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 
78  bool isEqual( const QgsAbstractContentCacheEntry *other ) const override;
79  int dataSize() const override;
80  void dump() const override;
81 
82 };
83 
85 #endif
86 
96 #ifdef SIP_RUN
97 class CORE_EXPORT QgsSvgCache : public QgsAbstractContentCacheBase // for sip we skip to the base class and avoid the template difficulty
98 {
99 #else
100 class CORE_EXPORT QgsSvgCache : public QgsAbstractContentCache< QgsSvgCacheEntry >
101 {
102 #endif
103  Q_OBJECT
104 
105  public:
106 
110  QgsSvgCache( QObject *parent SIP_TRANSFERTHIS = nullptr );
111 
126  QImage svgAsImage( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
127  double widthScaleFactor, bool &fitsInCache, double fixedAspectRatio = 0, bool blocking = false );
128 
143  QPicture svgAsPicture( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
144  double widthScaleFactor, bool forceVectorOutput = false, double fixedAspectRatio = 0, bool blocking = false );
145 
161  QSizeF svgViewboxSize( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
162  double widthScaleFactor, double fixedAspectRatio = 0, bool blocking = false );
163 
169  void containsParams( const QString &path, bool &hasFillParam, QColor &defaultFillColor, bool &hasStrokeParam, QColor &defaultStrokeColor, bool &hasStrokeWidthParam,
170  double &defaultStrokeWidth, bool blocking = false ) const;
171 
197  void containsParams( const QString &path, bool &hasFillParam, bool &hasDefaultFillParam, QColor &defaultFillColor,
198  bool &hasFillOpacityParam, bool &hasDefaultFillOpacity, double &defaultFillOpacity,
199  bool &hasStrokeParam, bool &hasDefaultStrokeColor, QColor &defaultStrokeColor,
200  bool &hasStrokeWidthParam, bool &hasDefaultStrokeWidth, double &defaultStrokeWidth,
201  bool &hasStrokeOpacityParam, bool &hasDefaultStrokeOpacity, double &defaultStrokeOpacity,
202  bool blocking = false ) const SIP_PYNAME( containsParamsV3 );
203 
217  QByteArray getImageData( const QString &path, bool blocking = false ) const;
218 
220 
235  QByteArray svgContent( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
236  double widthScaleFactor, double fixedAspectRatio = 0, bool blocking = false );
237 
238  signals:
239 
244  Q_DECL_DEPRECATED void statusChanged( const QString &statusQString ) SIP_DEPRECATED;
245 
250  void remoteSvgFetched( const QString &url );
251 
252  protected:
253 
254  bool checkReply( QNetworkReply *reply, const QString &path ) const override;
255 
256  private:
257 
258  void replaceParamsAndCacheSvg( QgsSvgCacheEntry *entry, bool blocking = false );
259  void cacheImage( QgsSvgCacheEntry *entry );
260  void cachePicture( QgsSvgCacheEntry *entry, bool forceVectorOutput = false );
262  QgsSvgCacheEntry *cacheEntry( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
263  double widthScaleFactor, double fixedAspectRatio = 0, bool blocking = false );
264 
266  void replaceElemParams( QDomElement &elem, const QColor &fill, const QColor &stroke, double strokeWidth );
267 
268  void containsElemParams( const QDomElement &elem,
269  bool &hasFillParam, bool &hasDefaultFill, QColor &defaultFill,
270  bool &hasFillOpacityParam, bool &hasDefaultFillOpacity, double &defaultFillOpacity,
271  bool &hasStrokeParam, bool &hasDefaultStroke, QColor &defaultStroke,
272  bool &hasStrokeWidthParam, bool &hasDefaultStrokeWidth, double &defaultStrokeWidth,
273  bool &hasStrokeOpacityParam, bool &hasDefaultStrokeOpacity, double &defaultStrokeOpacity ) const SIP_PYNAME( containsParamsV3 );
274 
276  double calcSizeScaleFactor( QgsSvgCacheEntry *entry, const QDomElement &docElem, QSizeF &viewboxSize ) const;
277 
282  QSize sizeForImage( const QgsSvgCacheEntry &entry, QSizeF &viewBoxSize, QSizeF &scaledSize ) const;
283 
287  QImage imageFromCachedPicture( const QgsSvgCacheEntry &entry ) const;
288 
290  QByteArray mMissingSvg;
291 
292  QByteArray mFetchingSvg;
293 
294  friend class TestQgsSvgCache;
295 };
296 
297 #endif // QGSSVGCACHE_H
Abstract base class for file content caches, such as SVG or raster image caches.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
A cache for images / pictures derived from svg files.
Definition: qgssvgcache.h:100
Base class for entries in a QgsAbstractContentCache.
virtual int dataSize() const =0
Returns the memory usage in bytes for the entry.
virtual bool isEqual(const QgsAbstractContentCacheEntry *other) const =0
Tests whether this entry matches another entry.
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
virtual void dump() const =0
Dumps debugging strings containing the item&#39;s properties.
QgsAbstractContentCacheEntry & operator=(const QgsAbstractContentCacheEntry &rh)=delete
QgsAbstractContentCacheEntry cannot be copied.
A QObject derived base class for QgsAbstractContentCache.
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...
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81