QGIS API Documentation  2.3.0-Master
 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 <QString>
25 #include <QUrl>
26 
27 class QDomElement;
28 class QImage;
29 class QPicture;
30 
31 class CORE_EXPORT QgsSvgCacheEntry
32 {
33  public:
44  QgsSvgCacheEntry( const QString& file, double size, double outlineWidth, double widthScaleFactor, double rasterScaleFactor, const QColor& fill, const QColor& outline );
46 
47  QString file;
48  double size; //size in pixels (cast to int for QImage)
49  double outlineWidth;
52  QColor fill;
53  QColor outline;
54  QImage* image;
55  QPicture* picture;
56  //content (with params replaced)
57  QByteArray svgContent;
58 
59  //keep entries on a least, sorted by last access
62 
64  bool operator==( const QgsSvgCacheEntry& other ) const;
66  int dataSize() const;
67 };
68 
72 class CORE_EXPORT QgsSvgCache : public QObject
73 {
74  Q_OBJECT
75 
76  public:
77 
78  static QgsSvgCache* instance();
79  ~QgsSvgCache();
80 
91  const QImage& svgAsImage( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
92  double widthScaleFactor, double rasterScaleFactor, bool& fitsInCache );
103  const QPicture& svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
104  double widthScaleFactor, double rasterScaleFactor, bool forceVectorOutput = false );
105 
108  void containsParams( const QString& path, bool& hasFillParam, QColor& defaultFillColor, bool& hasOutlineParam, QColor& defaultOutlineColor, bool& hasOutlineWidthParam,
109  double& defaultOutlineWidth ) const;
110 
112  QByteArray getImageData( const QString &path ) const;
113 
114  signals:
116  void statusChanged( const QString& theStatusQString );
117 
118  protected:
120  QgsSvgCache( QObject * parent = 0 );
121 
132  QgsSvgCacheEntry* insertSVG( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
133  double widthScaleFactor, double rasterScaleFactor );
134 
135  void replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry );
136  void cacheImage( QgsSvgCacheEntry* entry );
137  void cachePicture( QgsSvgCacheEntry* entry, bool forceVectorOutput = false );
139  QgsSvgCacheEntry* cacheEntry( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
140  double widthScaleFactor, double rasterScaleFactor );
141 
143  void trimToMaximumSize();
144 
145  //Removes entry from the ordered list (but does not delete the entry itself)
146  void takeEntryFromList( QgsSvgCacheEntry* entry );
147 
148  private slots:
149  void downloadProgress( qint64, qint64 );
150 
151  private:
153  QMultiHash< QString, QgsSvgCacheEntry* > mEntryLookup;
156 
157  //The svg cache keeps the entries on a double connected list, moving the current entry to the front.
158  //That way, removing entries for more space can start with the least used objects.
161 
162  //Maximum cache size
163  static const long mMaximumSize = 20000000;
164 
166  void replaceElemParams( QDomElement& elem, const QColor& fill, const QColor& outline, double outlineWidth );
167 
168  void containsElemParams( const QDomElement& elem, bool& hasFillParam, QColor& defaultFill, bool& hasOutlineParam, QColor& defaultOutline,
169  bool& hasOutlineWidthParam, double& defaultOutlineWidth ) const;
170 
172  void removeCacheEntry( QString s, QgsSvgCacheEntry* entry );
173 
175  void printEntryList();
176 
178  QByteArray mMissingSvg;
179 };
180 
181 #endif // QGSSVGCACHE_H
QgsSvgCacheEntry * previousEntry
Definition: qgssvgcache.h:61
double outlineWidth
Definition: qgssvgcache.cpp:78
QImage * image
Definition: qgssvgcache.h:54
QColor outline
Definition: qgssvgcache.cpp:82
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
A cache for images / pictures derived from svg files.
Definition: qgssvgcache.h:72
QgsSvgCacheEntry * mLeastRecentEntry
Definition: qgssvgcache.h:159
QByteArray mMissingSvg
SVG content to be rendered if SVG file was not found.
Definition: qgssvgcache.h:178
QPicture * picture
Definition: qgssvgcache.h:55
QColor fill
Definition: qgssvgcache.cpp:81
QgsSvgCacheEntry * mMostRecentEntry
Definition: qgssvgcache.h:160
double rasterScaleFactor
Definition: qgssvgcache.cpp:80
long mTotalSize
Estimated total size of all images, pictures and svgContent.
Definition: qgssvgcache.h:155
QString file
Definition: qgssvgcache.cpp:76
double widthScaleFactor
Definition: qgssvgcache.cpp:79
double outlineWidth
Definition: qgssvgcache.h:49
double rasterScaleFactor
Definition: qgssvgcache.h:51
double size
Definition: qgssvgcache.cpp:77
double widthScaleFactor
Definition: qgssvgcache.h:50
QByteArray svgContent
Definition: qgssvgcache.h:57
QMultiHash< QString, QgsSvgCacheEntry * > mEntryLookup
Entry pointers accessible by file name.
Definition: qgssvgcache.h:153
QgsSvgCacheEntry * nextEntry
Definition: qgssvgcache.h:60