QGIS API Documentation  3.2.0-Bonn (bc43194)
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 #include <QCache>
35 #include <QSet>
36 
37 #include "qgis_core.h"
38 
39 class QDomElement;
40 
41 #ifndef SIP_RUN
42 
44 
49 class CORE_EXPORT QgsSvgCacheEntry
50 {
51  public:
52 
53  QgsSvgCacheEntry() = delete;
54 
65  QgsSvgCacheEntry( const QString &path, double size, double strokeWidth, double widthScaleFactor, const QColor &fill, const QColor &stroke,
66  double fixedAspectRatio = 0 ) ;
67 
69  QgsSvgCacheEntry( const QgsSvgCacheEntry &rh ) = delete;
71  QgsSvgCacheEntry &operator=( const QgsSvgCacheEntry &rh ) = delete;
72 
74  QString path;
75 
77  QDateTime fileModified;
79  QElapsedTimer fileModifiedLastCheckTimer;
80  int mFileModifiedCheckTimeout = 30000;
81 
82  double size = 0.0; //size in pixels (cast to int for QImage)
83  double strokeWidth = 0;
84  double widthScaleFactor = 1.0;
85 
87  double fixedAspectRatio = 0;
88 
93  QSizeF viewboxSize;
94 
95  QColor fill = Qt::black;
96  QColor stroke = Qt::black;
97  std::unique_ptr< QImage > image;
98  std::unique_ptr< QPicture > picture;
99  //content (with params replaced)
100  QByteArray svgContent;
101 
102  //keep entries on a least, sorted by last access
103  QgsSvgCacheEntry *nextEntry = nullptr;
104  QgsSvgCacheEntry *previousEntry = nullptr;
105 
107  bool operator==( const QgsSvgCacheEntry &other ) const;
109  int dataSize() const;
110 
111  private:
112 #ifdef SIP_RUN
113  QgsSvgCacheEntry( const QgsSvgCacheEntry &rh );
114 #endif
115 
116 };
117 
119 #endif
120 
130 class CORE_EXPORT QgsSvgCache : public QObject
131 {
132  Q_OBJECT
133 
134  public:
135 
139  QgsSvgCache( QObject *parent SIP_TRANSFERTHIS = nullptr );
140 
141  ~QgsSvgCache() override;
142 
154  QImage svgAsImage( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
155  double widthScaleFactor, bool &fitsInCache, double fixedAspectRatio = 0 );
156 
168  QPicture svgAsPicture( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
169  double widthScaleFactor, bool forceVectorOutput = false, double fixedAspectRatio = 0 );
170 
183  QSizeF svgViewboxSize( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
184  double widthScaleFactor, double fixedAspectRatio = 0 );
185 
189  void containsParams( const QString &path, bool &hasFillParam, QColor &defaultFillColor, bool &hasStrokeParam, QColor &defaultStrokeColor, bool &hasStrokeWidthParam,
190  double &defaultStrokeWidth ) const;
191 
214  void containsParams( const QString &path, bool &hasFillParam, bool &hasDefaultFillParam, QColor &defaultFillColor,
215  bool &hasFillOpacityParam, bool &hasDefaultFillOpacity, double &defaultFillOpacity,
216  bool &hasStrokeParam, bool &hasDefaultStrokeColor, QColor &defaultStrokeColor,
217  bool &hasStrokeWidthParam, bool &hasDefaultStrokeWidth, double &defaultStrokeWidth,
218  bool &hasStrokeOpacityParam, bool &hasDefaultStrokeOpacity, double &defaultStrokeOpacity ) const SIP_PYNAME( containsParamsV3 );
219 
221  QByteArray getImageData( const QString &path ) const;
222 
224  QByteArray svgContent( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
225  double widthScaleFactor, double fixedAspectRatio = 0 );
226 
227  signals:
229  void statusChanged( const QString &statusQString );
230 
235  void remoteSvgFetched( const QString &url );
236 
237  private slots:
238  void downloadProgress( qint64, qint64 );
239 
240  void onRemoteSvgFetched( const QString &url, bool success );
241 
242  private:
243 
254  QgsSvgCacheEntry *insertSvg( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
255  double widthScaleFactor, double fixedAspectRatio = 0 );
256 
257  void replaceParamsAndCacheSvg( QgsSvgCacheEntry *entry );
258  void cacheImage( QgsSvgCacheEntry *entry );
259  void cachePicture( QgsSvgCacheEntry *entry, bool forceVectorOutput = false );
261  QgsSvgCacheEntry *cacheEntry( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
262  double widthScaleFactor, double fixedAspectRatio = 0 );
263 
265  void trimToMaximumSize();
266 
267  //Removes entry from the ordered list (but does not delete the entry itself)
268  void takeEntryFromList( QgsSvgCacheEntry *entry );
269 
271  int mFileModifiedCheckTimeout = 30000;
272 
274  QMultiHash< QString, QgsSvgCacheEntry * > mEntryLookup;
276  long mTotalSize = 0;
277 
278  //The svg cache keeps the entries on a double connected list, moving the current entry to the front.
279  //That way, removing entries for more space can start with the least used objects.
280  QgsSvgCacheEntry *mLeastRecentEntry = nullptr;
281  QgsSvgCacheEntry *mMostRecentEntry = nullptr;
282 
284  static const long MAXIMUM_SIZE = 20000000;
285 
287  void replaceElemParams( QDomElement &elem, const QColor &fill, const QColor &stroke, double strokeWidth );
288 
289  void containsElemParams( const QDomElement &elem,
290  bool &hasFillParam, bool &hasDefaultFill, QColor &defaultFill,
291  bool &hasFillOpacityParam, bool &hasDefaultFillOpacity, double &defaultFillOpacity,
292  bool &hasStrokeParam, bool &hasDefaultStroke, QColor &defaultStroke,
293  bool &hasStrokeWidthParam, bool &hasDefaultStrokeWidth, double &defaultStrokeWidth,
294  bool &hasStrokeOpacityParam, bool &hasDefaultStrokeOpacity, double &defaultStrokeOpacity ) const SIP_PYNAME( containsParamsV3 );
295 
297  double calcSizeScaleFactor( QgsSvgCacheEntry *entry, const QDomElement &docElem, QSizeF &viewboxSize ) const;
298 
300  void removeCacheEntry( const QString &s, QgsSvgCacheEntry *entry );
301 
303  void printEntryList();
304 
309  QSize sizeForImage( const QgsSvgCacheEntry &entry, QSizeF &viewBoxSize, QSizeF &scaledSize ) const;
310 
314  QImage imageFromCachedPicture( const QgsSvgCacheEntry &entry ) const;
315 
316  QByteArray fetchImageData( const QString &path, bool &ok ) const;
317 
319  QByteArray mMissingSvg;
320 
321  QByteArray mFetchingSvg;
322 
324  mutable QMutex mMutex;
325 
326  mutable QCache< QString, QByteArray > mRemoteContentCache;
327  mutable QSet< QString > mPendingRemoteUrls;
328 
329  friend class TestQgsSvgCache;
330 };
331 
332 #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:130
#define SIP_PYNAME(name)
Definition: qgis_sip.h:74