QGIS API Documentation  2.99.0-Master (19b062c)
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 #include <QDateTime>
31 #include <QElapsedTimer>
32 #include <QPicture>
33 #include <QImage>
34 
35 #include "qgis_core.h"
36 
37 class QDomElement;
38 
39 #ifndef SIP_RUN
40 
42 
47 class CORE_EXPORT QgsSvgCacheEntry
48 {
49  public:
50 
51  QgsSvgCacheEntry() = delete;
52 
63  QgsSvgCacheEntry( const QString &path, double size, double strokeWidth, double widthScaleFactor, const QColor &fill, const QColor &stroke,
64  double fixedAspectRatio = 0 ) ;
65 
67  QgsSvgCacheEntry( const QgsSvgCacheEntry &rh ) = delete;
69  QgsSvgCacheEntry &operator=( const QgsSvgCacheEntry &rh ) = delete;
70 
72  QString path;
73 
75  QDateTime fileModified;
77  QElapsedTimer fileModifiedLastCheckTimer;
78  int mFileModifiedCheckTimeout = 30000;
79 
80  double size = 0.0; //size in pixels (cast to int for QImage)
81  double strokeWidth = 0;
82  double widthScaleFactor = 1.0;
83 
85  double fixedAspectRatio = 0;
86 
91  QSizeF viewboxSize;
92 
93  QColor fill = Qt::black;
94  QColor stroke = Qt::black;
95  std::unique_ptr< QImage > image;
96  std::unique_ptr< QPicture > picture;
97  //content (with params replaced)
98  QByteArray svgContent;
99 
100  //keep entries on a least, sorted by last access
101  QgsSvgCacheEntry *nextEntry = nullptr;
102  QgsSvgCacheEntry *previousEntry = nullptr;
103 
105  bool operator==( const QgsSvgCacheEntry &other ) const;
107  int dataSize() const;
108 
109  private:
110 #ifdef SIP_RUN
111  QgsSvgCacheEntry( const QgsSvgCacheEntry &rh );
112 #endif
113 
114 };
115 
117 #endif
118 
128 class CORE_EXPORT QgsSvgCache : public QObject
129 {
130  Q_OBJECT
131 
132  public:
133 
137  QgsSvgCache( QObject *parent SIP_TRANSFERTHIS = 0 );
138 
139  ~QgsSvgCache();
140 
152  QImage svgAsImage( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
153  double widthScaleFactor, bool &fitsInCache, double fixedAspectRatio = 0 );
154 
166  QPicture svgAsPicture( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
167  double widthScaleFactor, bool forceVectorOutput = false, double fixedAspectRatio = 0 );
168 
181  QSizeF svgViewboxSize( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
182  double widthScaleFactor, double fixedAspectRatio = 0 );
183 
187  void containsParams( const QString &path, bool &hasFillParam, QColor &defaultFillColor, bool &hasStrokeParam, QColor &defaultStrokeColor, bool &hasStrokeWidthParam,
188  double &defaultStrokeWidth ) const;
189 
212  void containsParams( const QString &path, bool &hasFillParam, bool &hasDefaultFillParam, QColor &defaultFillColor,
213  bool &hasFillOpacityParam, bool &hasDefaultFillOpacity, double &defaultFillOpacity,
214  bool &hasStrokeParam, bool &hasDefaultStrokeColor, QColor &defaultStrokeColor,
215  bool &hasStrokeWidthParam, bool &hasDefaultStrokeWidth, double &defaultStrokeWidth,
216  bool &hasStrokeOpacityParam, bool &hasDefaultStrokeOpacity, double &defaultStrokeOpacity ) const SIP_PYNAME( containsParamsV3 );
217 
219  QByteArray getImageData( const QString &path ) const;
220 
222  QByteArray svgContent( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
223  double widthScaleFactor, double fixedAspectRatio = 0 );
224 
225  signals:
227  void statusChanged( const QString &statusQString );
228 
229  private slots:
230  void downloadProgress( qint64, qint64 );
231 
232  private:
233 
244  QgsSvgCacheEntry *insertSvg( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
245  double widthScaleFactor, double fixedAspectRatio = 0 );
246 
247  void replaceParamsAndCacheSvg( QgsSvgCacheEntry *entry );
248  void cacheImage( QgsSvgCacheEntry *entry );
249  void cachePicture( QgsSvgCacheEntry *entry, bool forceVectorOutput = false );
251  QgsSvgCacheEntry *cacheEntry( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
252  double widthScaleFactor, double fixedAspectRatio = 0 );
253 
255  void trimToMaximumSize();
256 
257  //Removes entry from the ordered list (but does not delete the entry itself)
258  void takeEntryFromList( QgsSvgCacheEntry *entry );
259 
261  int mFileModifiedCheckTimeout = 30000;
262 
264  QMultiHash< QString, QgsSvgCacheEntry * > mEntryLookup;
266  long mTotalSize = 0;
267 
268  //The svg cache keeps the entries on a double connected list, moving the current entry to the front.
269  //That way, removing entries for more space can start with the least used objects.
270  QgsSvgCacheEntry *mLeastRecentEntry = nullptr;
271  QgsSvgCacheEntry *mMostRecentEntry = nullptr;
272 
274  static const long MAXIMUM_SIZE = 20000000;
275 
277  void replaceElemParams( QDomElement &elem, const QColor &fill, const QColor &stroke, double strokeWidth );
278 
279  void containsElemParams( const QDomElement &elem,
280  bool &hasFillParam, bool &hasDefaultFill, QColor &defaultFill,
281  bool &hasFillOpacityParam, bool &hasDefaultFillOpacity, double &defaultFillOpacity,
282  bool &hasStrokeParam, bool &hasDefaultStroke, QColor &defaultStroke,
283  bool &hasStrokeWidthParam, bool &hasDefaultStrokeWidth, double &defaultStrokeWidth,
284  bool &hasStrokeOpacityParam, bool &hasDefaultStrokeOpacity, double &defaultStrokeOpacity ) const SIP_PYNAME( containsParamsV3 );
285 
287  double calcSizeScaleFactor( QgsSvgCacheEntry *entry, const QDomElement &docElem, QSizeF &viewboxSize ) const;
288 
290  void removeCacheEntry( const QString &s, QgsSvgCacheEntry *entry );
291 
293  void printEntryList();
294 
299  QSize sizeForImage( const QgsSvgCacheEntry &entry, QSizeF &viewBoxSize, QSizeF &scaledSize ) const;
300 
304  QImage imageFromCachedPicture( const QgsSvgCacheEntry &entry ) const;
305 
307  QByteArray mMissingSvg;
308 
310  QMutex mMutex;
311 
312  friend class TestQgsSvgCache;
313 };
314 
315 #endif // QGSSVGCACHE_H
#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:128
#define SIP_PYNAME(name)
Definition: qgis_sip.h:74