QGIS API Documentation  2.12.0-Lyon
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 <QMap>
23 #include <QMultiHash>
24 #include <QMutex>
25 #include <QString>
26 #include <QUrl>
27 #include <QObject>
28 
29 class QDomElement;
30 class QImage;
31 class QPicture;
32 
33 class CORE_EXPORT QgsSvgCacheEntry
34 {
35  public:
47  QgsSvgCacheEntry( const QString& file, double size, double outlineWidth, double widthScaleFactor, double rasterScaleFactor, const QColor& fill, const QColor& outline, const QString& lookupKey = QString() );
49 
54  double size; //size in pixels (cast to int for QImage)
55  double outlineWidth;
62  //content (with params replaced)
64 
65  //keep entries on a least, sorted by last access
68 
70  bool operator==( const QgsSvgCacheEntry& other ) const;
72  int dataSize() const;
73 };
74 
78 class CORE_EXPORT QgsSvgCache : public QObject
79 {
80  Q_OBJECT
81 
82  public:
83 
84  static QgsSvgCache* instance();
85  ~QgsSvgCache();
86 
97  const QImage& svgAsImage( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
98  double widthScaleFactor, double rasterScaleFactor, bool& fitsInCache );
109  const QPicture& svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
110  double widthScaleFactor, double rasterScaleFactor, bool forceVectorOutput = false );
111 
114  void containsParams( const QString& path, bool& hasFillParam, QColor& defaultFillColor, bool& hasOutlineParam, QColor& defaultOutlineColor, bool& hasOutlineWidthParam,
115  double& defaultOutlineWidth ) const;
116 
132  void containsParams( const QString& path, bool& hasFillParam, bool& hasDefaultFillParam, QColor& defaultFillColor,
133  bool& hasOutlineParam, bool& hasDefaultOutlineColor, QColor& defaultOutlineColor,
134  bool& hasOutlineWidthParam, bool& hasDefaultOutlineWidth, double& defaultOutlineWidth ) const;
135 
137  QByteArray getImageData( const QString &path ) const;
138 
140  const QByteArray& svgContent( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
141  double widthScaleFactor, double rasterScaleFactor );
142 
143  signals:
145  void statusChanged( const QString& theStatusQString );
146 
147  protected:
149  QgsSvgCache( QObject * parent = 0 );
150 
161  QgsSvgCacheEntry* insertSVG( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
162  double widthScaleFactor, double rasterScaleFactor );
163 
164  void replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry );
165  void cacheImage( QgsSvgCacheEntry* entry );
166  void cachePicture( QgsSvgCacheEntry* entry, bool forceVectorOutput = false );
168  QgsSvgCacheEntry* cacheEntry( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
169  double widthScaleFactor, double rasterScaleFactor );
170 
172  void trimToMaximumSize();
173 
174  //Removes entry from the ordered list (but does not delete the entry itself)
175  void takeEntryFromList( QgsSvgCacheEntry* entry );
176 
177  private slots:
178  void downloadProgress( qint64, qint64 );
179 
180  private:
184  long mTotalSize;
185 
186  //The svg cache keeps the entries on a double connected list, moving the current entry to the front.
187  //That way, removing entries for more space can start with the least used objects.
188  QgsSvgCacheEntry* mLeastRecentEntry;
189  QgsSvgCacheEntry* mMostRecentEntry;
190 
191  //Maximum cache size
192  static const long mMaximumSize = 20000000;
193 
195  void replaceElemParams( QDomElement& elem, const QColor& fill, const QColor& outline, double outlineWidth );
196 
197  void containsElemParams( const QDomElement& elem,
198  bool& hasFillParam, bool& hasDefaultFill, QColor& defaultFill,
199  bool& hasOutlineParam, bool& hasDefaultOutline, QColor& defaultOutline,
200  bool& hasOutlineWidthParam, bool& hasDefaultOutlineWidth, double& defaultOutlineWidth ) const;
201 
203  double calcSizeScaleFactor( QgsSvgCacheEntry* entry, const QDomElement& docElem ) const;
204 
206  void removeCacheEntry( const QString& s, QgsSvgCacheEntry* entry );
207 
209  void printEntryList();
210 
212  QByteArray mMissingSvg;
213 
215  QMutex mMutex;
216 
217 };
218 
219 #endif // QGSSVGCACHE_H
QgsSvgCacheEntry * previousEntry
Definition: qgssvgcache.h:67
QImage * image
Definition: qgssvgcache.h:60
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
A cache for images / pictures derived from svg files.
Definition: qgssvgcache.h:78
QPicture * picture
Definition: qgssvgcache.h:61
QString lookupKey
Lookup key used by QgsSvgCache's hashtable (relative or absolute path). Needed for removal from the h...
Definition: qgssvgcache.h:53
double outlineWidth
Definition: qgssvgcache.h:55
double rasterScaleFactor
Definition: qgssvgcache.h:57
double widthScaleFactor
Definition: qgssvgcache.h:56
QString file
Absolute path to SVG file.
Definition: qgssvgcache.h:51
QByteArray svgContent
Definition: qgssvgcache.h:63
QgsSvgCacheEntry * nextEntry
Definition: qgssvgcache.h:66