QGIS API Documentation  2.6.0-Brighton
 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 
48  QString file;
49  double size; //size in pixels (cast to int for QImage)
50  double outlineWidth;
53  QColor fill;
54  QColor outline;
55  QImage* image;
56  QPicture* picture;
57  //content (with params replaced)
58  QByteArray svgContent;
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 
115  signals:
117  void statusChanged( const QString& theStatusQString );
118 
119  protected:
121  QgsSvgCache( QObject * parent = 0 );
122 
133  QgsSvgCacheEntry* insertSVG( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
134  double widthScaleFactor, double rasterScaleFactor );
135 
136  void replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry );
137  void cacheImage( QgsSvgCacheEntry* entry );
138  void cachePicture( QgsSvgCacheEntry* entry, bool forceVectorOutput = false );
140  QgsSvgCacheEntry* cacheEntry( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
141  double widthScaleFactor, double rasterScaleFactor );
142 
144  void trimToMaximumSize();
145 
146  //Removes entry from the ordered list (but does not delete the entry itself)
147  void takeEntryFromList( QgsSvgCacheEntry* entry );
148 
149  private slots:
150  void downloadProgress( qint64, qint64 );
151 
152  private:
154  QMultiHash< QString, QgsSvgCacheEntry* > mEntryLookup;
156  long mTotalSize;
157 
158  //The svg cache keeps the entries on a double connected list, moving the current entry to the front.
159  //That way, removing entries for more space can start with the least used objects.
160  QgsSvgCacheEntry* mLeastRecentEntry;
161  QgsSvgCacheEntry* mMostRecentEntry;
162 
163  //Maximum cache size
164  static const long mMaximumSize = 20000000;
165 
167  void replaceElemParams( QDomElement& elem, const QColor& fill, const QColor& outline, double outlineWidth );
168 
169  void containsElemParams( const QDomElement& elem, bool& hasFillParam, QColor& defaultFill, bool& hasOutlineParam, QColor& defaultOutline,
170  bool& hasOutlineWidthParam, double& defaultOutlineWidth ) const;
171 
173  void removeCacheEntry( QString s, QgsSvgCacheEntry* entry );
174 
176  void printEntryList();
177 
179  QByteArray mMissingSvg;
180 
182  QMutex mMutex;
183 };
184 
185 #endif // QGSSVGCACHE_H