QGIS API Documentation 4.1.0-Master (60fea48833c)
Loading...
Searching...
No Matches
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 "qgis.h"
23
24#include <QPicture>
25
26class QDomElement;
27
28#ifndef SIP_RUN
29
31
36class CORE_EXPORT QgsSvgCacheEntry : public QgsAbstractContentCacheEntry
37{
38 public:
50 QgsSvgCacheEntry(
51 const QString &path,
52 double size,
53 double strokeWidth,
54 double widthScaleFactor,
55 const QColor &fill,
56 const QColor &stroke,
57 double fixedAspectRatio = 0,
58 const QMap<QString, QString> &parameters = QMap<QString, QString>()
59 );
60
61 QgsSvgCacheEntry( const QgsSvgCacheEntry &rh ) = delete;
62 QgsSvgCacheEntry &operator=( const QgsSvgCacheEntry &rh ) = delete;
63
64 double size = 0.0; //size in pixels (cast to int for QImage)
65 double strokeWidth = 0;
66 double widthScaleFactor = 1.0;
67
69 double fixedAspectRatio = 0;
70
74 QSizeF viewboxSize;
75
76 QColor fill = Qt::black;
77 QColor stroke = Qt::black;
78 QMap<QString, QString> parameters;
79
80 std::unique_ptr< QImage > image;
81 std::unique_ptr< QPicture > picture;
82 //content (with params replaced)
83 QByteArray svgContent;
84
90 bool isMissingImage = false;
91
92 bool isEqual( const QgsAbstractContentCacheEntry *other ) const override;
93 int dataSize() const override;
94 void dump() const override;
95};
96
98#endif
99
123#ifdef SIP_RUN
124class CORE_EXPORT QgsSvgCache : public QgsAbstractContentCacheBase // for sip we skip to the base class and avoid the template difficulty
125{
126#else
127class CORE_EXPORT QgsSvgCache : public QgsAbstractContentCache< QgsSvgCacheEntry >
128{
129#endif
130 Q_OBJECT
131
132 public:
136 QgsSvgCache( QObject *parent SIP_TRANSFERTHIS = nullptr );
137
155 QImage svgAsImage(
156 const QString &path,
157 double size,
158 const QColor &fill,
159 const QColor &stroke,
160 double strokeWidth,
161 double widthScaleFactor,
162 bool &fitsInCache,
163 double fixedAspectRatio = 0,
164 bool blocking = false,
165 const QMap<QString, QString> &parameters = QMap<QString, QString>()
166 );
167
190 QPicture svgAsPicture(
191 const QString &path,
192 double size,
193 const QColor &fill,
194 const QColor &stroke,
195 double strokeWidth,
196 double widthScaleFactor,
197 bool forceVectorOutput = false,
198 double fixedAspectRatio = 0,
199 bool blocking = false,
200 const QMap<QString, QString> &parameters = QMap<QString, QString>()
201 );
202
220 QSizeF svgViewboxSize(
221 const QString &path,
222 double size,
223 const QColor &fill,
224 const QColor &stroke,
225 double strokeWidth,
226 double widthScaleFactor,
227 double fixedAspectRatio = 0,
228 bool blocking = false,
229 const QMap<QString, QString> &parameters = QMap<QString, QString>()
230 );
231
238 void containsParams(
239 const QString &path, bool &hasFillParam, QColor &defaultFillColor, bool &hasStrokeParam, QColor &defaultStrokeColor, bool &hasStrokeWidthParam, double &defaultStrokeWidth, bool blocking = false
240 ) const;
241
269 void containsParams(
270 const QString &path,
271 bool &hasFillParam,
272 bool &hasDefaultFillParam,
273 QColor &defaultFillColor,
274 bool &hasFillOpacityParam,
275 bool &hasDefaultFillOpacity,
276 double &defaultFillOpacity,
277 bool &hasStrokeParam,
278 bool &hasDefaultStrokeColor,
279 QColor &defaultStrokeColor,
280 bool &hasStrokeWidthParam,
281 bool &hasDefaultStrokeWidth,
282 double &defaultStrokeWidth,
283 bool &hasStrokeOpacityParam,
284 bool &hasDefaultStrokeOpacity,
285 double &defaultStrokeOpacity,
286 bool blocking = false
287 ) const SIP_PYNAME( containsParamsV3 );
288
303 QByteArray getImageData( const QString &path, bool blocking = false ) const;
304
320#ifndef SIP_RUN
321 QByteArray svgContent(
322 const QString &path,
323 double size,
324 const QColor &fill,
325 const QColor &stroke,
326 double strokeWidth,
327 double widthScaleFactor,
328 double fixedAspectRatio = 0,
329 bool blocking = false,
330 const QMap<QString, QString> &parameters = QMap<QString, QString>(),
331 bool *isMissingImage = nullptr
332 );
333#else
334 QByteArray svgContent(
335 const QString &path,
336 double size,
337 const QColor &fill,
338 const QColor &stroke,
339 double strokeWidth,
340 double widthScaleFactor,
341 double fixedAspectRatio = 0,
342 bool blocking = false,
343 const QMap<QString, QString> &parameters = QMap<QString, QString>()
344 );
345#endif
346
347 signals:
348
353 Q_DECL_DEPRECATED void statusChanged( const QString &statusQString ) SIP_DEPRECATED;
354
359 void remoteSvgFetched( const QString &url );
360
361 protected:
362 bool checkReply( QNetworkReply *reply, const QString &path ) const override;
363
364 private:
365 void replaceParamsAndCacheSvg( QgsSvgCacheEntry *entry, bool blocking = false );
366 void cacheImage( QgsSvgCacheEntry *entry );
367 void cachePicture( QgsSvgCacheEntry *entry, bool forceVectorOutput = false );
369 QgsSvgCacheEntry *cacheEntry(
370 const QString &path,
371 double size,
372 const QColor &fill,
373 const QColor &stroke,
374 double strokeWidth,
375 double widthScaleFactor,
376 double fixedAspectRatio = 0,
377 const QMap<QString, QString> &parameters = QMap<QString, QString>(),
378 bool blocking = false,
379 bool *isMissingImage = nullptr
380 );
381
383 void replaceElemParams( QDomElement &elem, const QColor &fill, const QColor &stroke, double strokeWidth, const QMap<QString, QString> &parameters );
384
385 void containsElemParams(
386 const QDomElement &elem,
387 bool &hasFillParam,
388 bool &hasDefaultFill,
389 QColor &defaultFill,
390 bool &hasFillOpacityParam,
391 bool &hasDefaultFillOpacity,
392 double &defaultFillOpacity,
393 bool &hasStrokeParam,
394 bool &hasDefaultStroke,
395 QColor &defaultStroke,
396 bool &hasStrokeWidthParam,
397 bool &hasDefaultStrokeWidth,
398 double &defaultStrokeWidth,
399 bool &hasStrokeOpacityParam,
400 bool &hasDefaultStrokeOpacity,
401 double &defaultStrokeOpacity
402 ) const SIP_PYNAME( containsParamsV3 );
403
405 double calcSizeScaleFactor( QgsSvgCacheEntry *entry, const QDomElement &docElem, QSizeF &viewboxSize ) const;
406
411 QSize sizeForImage( const QgsSvgCacheEntry &entry, QSizeF &viewBoxSize, QSizeF &scaledSize ) const;
412
416 QImage imageFromCachedPicture( const QgsSvgCacheEntry &entry ) const;
417
419 QByteArray mMissingSvg;
420
421 QByteArray mFetchingSvg;
422
423 friend class TestQgsSvgCache;
424};
425
426#endif // QGSSVGCACHE_H
A QObject derived base class for QgsAbstractContentCache.
Base class for entries in a QgsAbstractContentCache.
virtual int dataSize() const =0
Returns the memory usage in bytes for the entry.
virtual void dump() const =0
Dumps debugging strings containing the item's properties.
QgsAbstractContentCacheEntry & operator=(const QgsAbstractContentCacheEntry &rh)=delete
virtual bool isEqual(const QgsAbstractContentCacheEntry *other) const =0
Tests whether this entry matches another entry.
QgsAbstractContentCache(QObject *parent=nullptr, const QString &typeString=QString(), long maxCacheSize=20000000, int fileModifiedCheckTimeout=30000)
A cache for images / pictures derived from SVG files.
QSizeF svgViewboxSize(const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth, double widthScaleFactor, double fixedAspectRatio=0, bool blocking=false, const QMap< QString, QString > &parameters=QMap< QString, QString >())
Calculates the viewbox size of a (possibly cached) SVG file.
Q_DECL_DEPRECATED void statusChanged(const QString &statusQString)
Emit a signal to be caught by qgisapp and display a msg on status bar.
QByteArray getImageData(const QString &path, bool blocking=false) const
Gets the SVG content corresponding to the given path.
QPicture svgAsPicture(const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth, double widthScaleFactor, bool forceVectorOutput=false, double fixedAspectRatio=0, bool blocking=false, const QMap< QString, QString > &parameters=QMap< QString, QString >())
Returns an SVG drawing as a QPicture.
QgsSvgCache(QObject *parent=nullptr)
Constructor for QgsSvgCache.
bool checkReply(QNetworkReply *reply, const QString &path) const override
Runs additional checks on a network reply to ensure that the reply content is consistent with that re...
void containsParams(const QString &path, bool &hasFillParam, QColor &defaultFillColor, bool &hasStrokeParam, QColor &defaultStrokeColor, bool &hasStrokeWidthParam, double &defaultStrokeWidth, bool blocking=false) const
Tests if an SVG file contains parameters for fill, stroke color, stroke width.
void remoteSvgFetched(const QString &url)
Emitted when the cache has finished retrieving an SVG file from a remote url.
QImage svgAsImage(const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth, double widthScaleFactor, bool &fitsInCache, double fixedAspectRatio=0, bool blocking=false, const QMap< QString, QString > &parameters=QMap< QString, QString >())
Returns an SVG drawing as a QImage.
QByteArray svgContent(const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth, double widthScaleFactor, double fixedAspectRatio=0, bool blocking=false, const QMap< QString, QString > &parameters=QMap< QString, QString >(), bool *isMissingImage=nullptr)
Gets the SVG content corresponding to the given path.
friend class TestQgsSvgCache
#define SIP_DEPRECATED
Definition qgis_sip.h:113
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:52
#define SIP_PYNAME(name)
Definition qgis_sip.h:88