QGIS API Documentation  2.0.1-Dufour
 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:
35  QgsSvgCacheEntry( const QString& file, double size, double outlineWidth, double widthScaleFactor, double rasterScaleFctor, const QColor& fill, const QColor& outline );
37 
38  QString file;
39  double size; //size in pixels (cast to int for QImage)
40  double outlineWidth;
43  QColor fill;
44  QColor outline;
45  QImage* image;
46  QPicture* picture;
47  //content (with params replaced)
48  QByteArray svgContent;
49 
50  //keep entries on a least, sorted by last access
53 
55  bool operator==( const QgsSvgCacheEntry& other ) const;
57  int dataSize() const;
58 };
59 
63 class CORE_EXPORT QgsSvgCache : public QObject
64 {
65  Q_OBJECT
66 
67  public:
68 
69  static QgsSvgCache* instance();
70  ~QgsSvgCache();
71 
72  const QImage& svgAsImage( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
73  double widthScaleFactor, double rasterScaleFactor, bool& fitsInCache );
74  const QPicture& svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
75  double widthScaleFactor, double rasterScaleFactor, bool forceVectorOutput = false );
76 
79  void containsParams( const QString& path, bool& hasFillParam, QColor& defaultFillColor, bool& hasOutlineParam, QColor& defaultOutlineColor, bool& hasOutlineWidthParam,
80  double& defaultOutlineWidth ) const;
81 
83  QByteArray getImageData( const QString &path ) const;
84 
85  signals:
87  void statusChanged( const QString& theStatusQString );
88 
89  protected:
91  QgsSvgCache( QObject * parent = 0 );
92 
94  QgsSvgCacheEntry* insertSVG( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
95  double widthScaleFactor, double rasterScaleFactor );
96 
97  void replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry );
98  void cacheImage( QgsSvgCacheEntry* entry );
99  void cachePicture( QgsSvgCacheEntry* entry, bool forceVectorOutput = false );
101  QgsSvgCacheEntry* cacheEntry( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
102  double widthScaleFactor, double rasterScaleFactor );
103 
105  void trimToMaximumSize();
106 
107  //Removes entry from the ordered list (but does not delete the entry itself)
108  void takeEntryFromList( QgsSvgCacheEntry* entry );
109 
110  private slots:
111  void downloadProgress( qint64, qint64 );
112 
113  private:
115 
117  QMultiHash< QString, QgsSvgCacheEntry* > mEntryLookup;
120 
121  //The svg cache keeps the entries on a double connected list, moving the current entry to the front.
122  //That way, removing entries for more space can start with the least used objects.
125 
126  //Maximum cache size
127  static const long mMaximumSize = 20000000;
128 
130  void replaceElemParams( QDomElement& elem, const QColor& fill, const QColor& outline, double outlineWidth );
131 
132  void containsElemParams( const QDomElement& elem, bool& hasFillParam, QColor& defaultFill, bool& hasOutlineParam, QColor& defaultOutline,
133  bool& hasOutlineWidthParam, double& defaultOutlineWidth ) const;
134 
136  void removeCacheEntry( QString s, QgsSvgCacheEntry* entry );
137 
139  void printEntryList();
140 };
141 
142 #endif // QGSSVGCACHE_H