QGIS API Documentation  2.99.0-Master (9fdd060)
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 
21 #include <QColor>
22 #include "qgis.h"
23 #include <QMap>
24 #include <QMultiHash>
25 #include <QMutex>
26 #include <QString>
27 #include <QUrl>
28 #include <QObject>
29 #include <QSizeF>
30 
31 #include "qgis_core.h"
32 
33 class QDomElement;
34 class QImage;
35 class QPicture;
36 
41 class CORE_EXPORT QgsSvgCacheEntry
42 {
43  public:
44 
46 
57  QgsSvgCacheEntry( const QString &path, double size, double strokeWidth, double widthScaleFactor, const QColor &fill, const QColor &stroke,
58  double fixedAspectRatio = 0 ) ;
60 
62  QgsSvgCacheEntry( const QgsSvgCacheEntry &rh ) = delete;
64  QgsSvgCacheEntry &operator=( const QgsSvgCacheEntry &rh ) = delete;
65 
67  QString path;
68  double size = 0.0; //size in pixels (cast to int for QImage)
69  double strokeWidth = 0;
70  double widthScaleFactor = 1.0;
71 
73  double fixedAspectRatio = 0;
74 
79  QSizeF viewboxSize;
80 
81  QColor fill;
82  QColor stroke;
83  QImage *image = nullptr;
84  QPicture *picture = nullptr;
85  //content (with params replaced)
86  QByteArray svgContent;
87 
88  //keep entries on a least, sorted by last access
89  QgsSvgCacheEntry *nextEntry = nullptr;
90  QgsSvgCacheEntry *previousEntry = nullptr;
91 
93  bool operator==( const QgsSvgCacheEntry &other ) const;
95  int dataSize() const;
96 
97  private:
98 #ifdef SIP_RUN
100 #endif
101 
102 };
103 
113 class CORE_EXPORT QgsSvgCache : public QObject
114 {
115  Q_OBJECT
116 
117  public:
118 
122  QgsSvgCache( QObject *parent SIP_TRANSFERTHIS = 0 );
123 
124  ~QgsSvgCache();
125 
137  QImage svgAsImage( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
138  double widthScaleFactor, bool &fitsInCache, double fixedAspectRatio = 0 );
139 
151  QPicture svgAsPicture( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
152  double widthScaleFactor, bool forceVectorOutput = false, double fixedAspectRatio = 0 );
153 
166  QSizeF svgViewboxSize( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
167  double widthScaleFactor, double fixedAspectRatio = 0 );
168 
172  void containsParams( const QString &path, bool &hasFillParam, QColor &defaultFillColor, bool &hasStrokeParam, QColor &defaultStrokeColor, bool &hasStrokeWidthParam,
173  double &defaultStrokeWidth ) const;
174 
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 ) const SIP_PYNAME( containsParamsV3 );
202 
204  QByteArray getImageData( const QString &path ) const;
205 
207  QByteArray svgContent( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
208  double widthScaleFactor, double fixedAspectRatio = 0 );
209 
210  signals:
212  void statusChanged( const QString &statusQString );
213 
214  protected:
215 
226  QgsSvgCacheEntry *insertSvg( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
227  double widthScaleFactor, double fixedAspectRatio = 0 );
228 
229  void replaceParamsAndCacheSvg( QgsSvgCacheEntry *entry );
230  void cacheImage( QgsSvgCacheEntry *entry );
231  void cachePicture( QgsSvgCacheEntry *entry, bool forceVectorOutput = false );
233  QgsSvgCacheEntry *cacheEntry( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
234  double widthScaleFactor, double fixedAspectRatio = 0 );
235 
237  void trimToMaximumSize();
238 
239  //Removes entry from the ordered list (but does not delete the entry itself)
240  void takeEntryFromList( QgsSvgCacheEntry *entry );
241 
242  private slots:
243  void downloadProgress( qint64, qint64 );
244 
245  private:
247  QMultiHash< QString, QgsSvgCacheEntry * > mEntryLookup;
249  long mTotalSize = 0;
250 
251  //The svg cache keeps the entries on a double connected list, moving the current entry to the front.
252  //That way, removing entries for more space can start with the least used objects.
253  QgsSvgCacheEntry *mLeastRecentEntry = nullptr;
254  QgsSvgCacheEntry *mMostRecentEntry = nullptr;
255 
257  static const long MAXIMUM_SIZE = 20000000;
258 
260  void replaceElemParams( QDomElement &elem, const QColor &fill, const QColor &stroke, double strokeWidth );
261 
262  void containsElemParams( const QDomElement &elem,
263  bool &hasFillParam, bool &hasDefaultFill, QColor &defaultFill,
264  bool &hasFillOpacityParam, bool &hasDefaultFillOpacity, double &defaultFillOpacity,
265  bool &hasStrokeParam, bool &hasDefaultStroke, QColor &defaultStroke,
266  bool &hasStrokeWidthParam, bool &hasDefaultStrokeWidth, double &defaultStrokeWidth,
267  bool &hasStrokeOpacityParam, bool &hasDefaultStrokeOpacity, double &defaultStrokeOpacity ) const SIP_PYNAME( containsParamsV3 );
268 
270  double calcSizeScaleFactor( QgsSvgCacheEntry *entry, const QDomElement &docElem, QSizeF &viewboxSize ) const;
271 
273  void removeCacheEntry( const QString &s, QgsSvgCacheEntry *entry );
274 
276  void printEntryList();
277 
279  QByteArray mMissingSvg;
280 
282  QMutex mMutex;
283 
284 };
285 
286 #endif // QGSSVGCACHE_H
QSizeF viewboxSize
SVG viewbox size.
Definition: qgssvgcache.h:79
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
A cache for images / pictures derived from svg files.
Definition: qgssvgcache.h:113
QString path
Absolute path to SVG file.
Definition: qgssvgcache.h:67
QByteArray svgContent
Definition: qgssvgcache.h:86
#define SIP_PYNAME(name)
Definition: qgis_sip.h:74