QGIS API Documentation  2.11.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 <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:
45  QgsSvgCacheEntry( const QString& file, double size, double outlineWidth, double widthScaleFactor, double rasterScaleFactor, const QColor& fill, const QColor& outline );
47 
49  double size; //size in pixels (cast to int for QImage)
50  double outlineWidth;
57  //content (with params replaced)
59 
60  //keep entries on a least, sorted by last access
63 
65  bool operator==( const QgsSvgCacheEntry& other ) const;
67  int dataSize() const;
68 };
69 
73 class CORE_EXPORT QgsSvgCache : public QObject
74 {
75  Q_OBJECT
76 
77  public:
78 
79  static QgsSvgCache* instance();
80  ~QgsSvgCache();
81 
92  const QImage& svgAsImage( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
93  double widthScaleFactor, double rasterScaleFactor, bool& fitsInCache );
104  const QPicture& svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
105  double widthScaleFactor, double rasterScaleFactor, bool forceVectorOutput = false );
106 
109  void containsParams( const QString& path, bool& hasFillParam, QColor& defaultFillColor, bool& hasOutlineParam, QColor& defaultOutlineColor, bool& hasOutlineWidthParam,
110  double& defaultOutlineWidth ) const;
111 
113  QByteArray getImageData( const QString &path ) const;
114 
116  const QByteArray& svgContent( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
117  double widthScaleFactor, double rasterScaleFactor );
118 
119  signals:
121  void statusChanged( const QString& theStatusQString );
122 
123  protected:
125  QgsSvgCache( QObject * parent = 0 );
126 
137  QgsSvgCacheEntry* insertSVG( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
138  double widthScaleFactor, double rasterScaleFactor );
139 
140  void replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry );
141  void cacheImage( QgsSvgCacheEntry* entry );
142  void cachePicture( QgsSvgCacheEntry* entry, bool forceVectorOutput = false );
144  QgsSvgCacheEntry* cacheEntry( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
145  double widthScaleFactor, double rasterScaleFactor );
146 
148  void trimToMaximumSize();
149 
150  //Removes entry from the ordered list (but does not delete the entry itself)
151  void takeEntryFromList( QgsSvgCacheEntry* entry );
152 
153  private slots:
154  void downloadProgress( qint64, qint64 );
155 
156  private:
160  long mTotalSize;
161 
162  //The svg cache keeps the entries on a double connected list, moving the current entry to the front.
163  //That way, removing entries for more space can start with the least used objects.
164  QgsSvgCacheEntry* mLeastRecentEntry;
165  QgsSvgCacheEntry* mMostRecentEntry;
166 
167  //Maximum cache size
168  static const long mMaximumSize = 20000000;
169 
171  void replaceElemParams( QDomElement& elem, const QColor& fill, const QColor& outline, double outlineWidth );
172 
173  void containsElemParams( const QDomElement& elem, bool& hasFillParam, QColor& defaultFill, bool& hasOutlineParam, QColor& defaultOutline,
174  bool& hasOutlineWidthParam, double& defaultOutlineWidth ) const;
175 
177  void removeCacheEntry( QString s, QgsSvgCacheEntry* entry );
178 
180  void printEntryList();
181 
183  QByteArray mMissingSvg;
184 
186  QMutex mMutex;
187 };
188 
189 #endif // QGSSVGCACHE_H
QgsSvgCacheEntry * previousEntry
Definition: qgssvgcache.h:62
QImage * image
Definition: qgssvgcache.h:55
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
A cache for images / pictures derived from svg files.
Definition: qgssvgcache.h:73
QPicture * picture
Definition: qgssvgcache.h:56
double outlineWidth
Definition: qgssvgcache.h:50
double rasterScaleFactor
Definition: qgssvgcache.h:52
double widthScaleFactor
Definition: qgssvgcache.h:51
QByteArray svgContent
Definition: qgssvgcache.h:58
QgsSvgCacheEntry * nextEntry
Definition: qgssvgcache.h:61