QGIS API Documentation  2.10.1-Pisa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
28 class QDomElement;
29 class QImage;
30 class QPicture;
31 
32 class CORE_EXPORT QgsSvgCacheEntry
33 {
34  public:
46  QgsSvgCacheEntry( const QString& file, double size, double outlineWidth, double widthScaleFactor, double rasterScaleFactor, const QColor& fill, const QColor& outline, const QString& lookupKey = QString() );
48 
53  double size; //size in pixels (cast to int for QImage)
54  double outlineWidth;
61  //content (with params replaced)
63 
64  //keep entries on a least, sorted by last access
67 
69  bool operator==( const QgsSvgCacheEntry& other ) const;
71  int dataSize() const;
72 };
73 
77 class CORE_EXPORT QgsSvgCache : public QObject
78 {
79  Q_OBJECT
80 
81  public:
82 
83  static QgsSvgCache* instance();
84  ~QgsSvgCache();
85 
96  const QImage& svgAsImage( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
97  double widthScaleFactor, double rasterScaleFactor, bool& fitsInCache );
108  const QPicture& svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
109  double widthScaleFactor, double rasterScaleFactor, bool forceVectorOutput = false );
110 
113  void containsParams( const QString& path, bool& hasFillParam, QColor& defaultFillColor, bool& hasOutlineParam, QColor& defaultOutlineColor, bool& hasOutlineWidthParam,
114  double& defaultOutlineWidth ) const;
115 
117  QByteArray getImageData( const QString &path ) const;
118 
120  const QByteArray& svgContent( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
121  double widthScaleFactor, double rasterScaleFactor );
122 
123  signals:
125  void statusChanged( const QString& theStatusQString );
126 
127  protected:
129  QgsSvgCache( QObject * parent = 0 );
130 
141  QgsSvgCacheEntry* insertSVG( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
142  double widthScaleFactor, double rasterScaleFactor );
143 
144  void replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry );
145  void cacheImage( QgsSvgCacheEntry* entry );
146  void cachePicture( QgsSvgCacheEntry* entry, bool forceVectorOutput = false );
148  QgsSvgCacheEntry* cacheEntry( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
149  double widthScaleFactor, double rasterScaleFactor );
150 
152  void trimToMaximumSize();
153 
154  //Removes entry from the ordered list (but does not delete the entry itself)
155  void takeEntryFromList( QgsSvgCacheEntry* entry );
156 
157  private slots:
158  void downloadProgress( qint64, qint64 );
159 
160  private:
164  long mTotalSize;
165 
166  //The svg cache keeps the entries on a double connected list, moving the current entry to the front.
167  //That way, removing entries for more space can start with the least used objects.
168  QgsSvgCacheEntry* mLeastRecentEntry;
169  QgsSvgCacheEntry* mMostRecentEntry;
170 
171  //Maximum cache size
172  static const long mMaximumSize = 20000000;
173 
175  void replaceElemParams( QDomElement& elem, const QColor& fill, const QColor& outline, double outlineWidth );
176 
177  void containsElemParams( const QDomElement& elem, bool& hasFillParam, QColor& defaultFill, bool& hasOutlineParam, QColor& defaultOutline,
178  bool& hasOutlineWidthParam, double& defaultOutlineWidth ) const;
179 
181  void removeCacheEntry( QString s, QgsSvgCacheEntry* entry );
182 
184  void printEntryList();
185 
187  QByteArray mMissingSvg;
188 
190  QMutex mMutex;
191 };
192 
193 #endif // QGSSVGCACHE_H
QgsSvgCacheEntry * previousEntry
Definition: qgssvgcache.h:66
QImage * image
Definition: qgssvgcache.h:59
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
A cache for images / pictures derived from svg files.
Definition: qgssvgcache.h:77
QPicture * picture
Definition: qgssvgcache.h:60
QString lookupKey
Lookup key used by QgsSvgCache's hashtable (relative or absolute path). Needed for removal from the h...
Definition: qgssvgcache.h:52
double outlineWidth
Definition: qgssvgcache.h:54
double rasterScaleFactor
Definition: qgssvgcache.h:56
double widthScaleFactor
Definition: qgssvgcache.h:55
QString file
Absolute path to SVG file.
Definition: qgssvgcache.h:50
QByteArray svgContent
Definition: qgssvgcache.h:62
QgsSvgCacheEntry * nextEntry
Definition: qgssvgcache.h:65