|
QGIS API Documentation
master-59fd5e0
|
00001 /*************************************************************************** 00002 qgssvgcache.h 00003 ------------------------------ 00004 begin : 2011 00005 copyright : (C) 2011 by Marco Hugentobler 00006 email : marco dot hugentobler at sourcepole dot ch 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 00018 #ifndef QGSSVGCACHE_H 00019 #define QGSSVGCACHE_H 00020 00021 #include <QColor> 00022 #include <QMap> 00023 #include <QMultiHash> 00024 #include <QString> 00025 #include <QUrl> 00026 00027 class QDomElement; 00028 class QImage; 00029 class QPicture; 00030 00031 class CORE_EXPORT QgsSvgCacheEntry 00032 { 00033 public: 00034 QgsSvgCacheEntry(); 00035 QgsSvgCacheEntry( const QString& file, double size, double outlineWidth, double widthScaleFactor, double rasterScaleFctor, const QColor& fill, const QColor& outline ); 00036 ~QgsSvgCacheEntry(); 00037 00038 QString file; 00039 double size; //size in pixels (cast to int for QImage) 00040 double outlineWidth; 00041 double widthScaleFactor; 00042 double rasterScaleFactor; 00043 QColor fill; 00044 QColor outline; 00045 QImage* image; 00046 QPicture* picture; 00047 //content (with params replaced) 00048 QByteArray svgContent; 00049 00050 //keep entries on a least, sorted by last access 00051 QgsSvgCacheEntry* nextEntry; 00052 QgsSvgCacheEntry* previousEntry; 00053 00055 bool operator==( const QgsSvgCacheEntry& other ) const; 00057 int dataSize() const; 00058 }; 00059 00063 class CORE_EXPORT QgsSvgCache : public QObject 00064 { 00065 Q_OBJECT 00066 00067 public: 00068 00069 static QgsSvgCache* instance(); 00070 ~QgsSvgCache(); 00071 00072 const QImage& svgAsImage( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth, 00073 double widthScaleFactor, double rasterScaleFactor, bool& fitsInCache ); 00074 const QPicture& svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth, 00075 double widthScaleFactor, double rasterScaleFactor ); 00076 00079 void containsParams( const QString& path, bool& hasFillParam, QColor& defaultFillColor, bool& hasOutlineParam, QColor& defaultOutlineColor, bool& hasOutlineWidthParam, 00080 double& defaultOutlineWidth ) const; 00081 00083 QByteArray getImageData( const QString &path ) const; 00084 00085 signals: 00087 void statusChanged( const QString& theStatusQString ); 00088 00089 protected: 00091 QgsSvgCache( QObject * parent = 0 ); 00092 00094 QgsSvgCacheEntry* insertSVG( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth, 00095 double widthScaleFactor, double rasterScaleFactor ); 00096 00097 void replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry ); 00098 void cacheImage( QgsSvgCacheEntry* entry ); 00099 void cachePicture( QgsSvgCacheEntry* entry ); 00101 QgsSvgCacheEntry* cacheEntry( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth, 00102 double widthScaleFactor, double rasterScaleFactor ); 00103 00105 void trimToMaximumSize(); 00106 00107 //Removes entry from the ordered list (but does not delete the entry itself) 00108 void takeEntryFromList( QgsSvgCacheEntry* entry ); 00109 00110 private slots: 00111 void downloadProgress( qint64, qint64 ); 00112 00113 private: 00114 static QgsSvgCache* mInstance; 00115 00117 QMultiHash< QString, QgsSvgCacheEntry* > mEntryLookup; 00119 long mTotalSize; 00120 00121 //The svg cache keeps the entries on a double connected list, moving the current entry to the front. 00122 //That way, removing entries for more space can start with the least used objects. 00123 QgsSvgCacheEntry* mLeastRecentEntry; 00124 QgsSvgCacheEntry* mMostRecentEntry; 00125 00126 //Maximum cache size 00127 static const long mMaximumSize = 20000000; 00128 00130 void replaceElemParams( QDomElement& elem, const QColor& fill, const QColor& outline, double outlineWidth ); 00131 00132 void containsElemParams( const QDomElement& elem, bool& hasFillParam, QColor& defaultFill, bool& hasOutlineParam, QColor& defaultOutline, 00133 bool& hasOutlineWidthParam, double& defaultOutlineWidth ) const; 00134 00136 void removeCacheEntry( QString s, QgsSvgCacheEntry* entry ); 00137 00139 void printEntryList(); 00140 }; 00141 00142 #endif // QGSSVGCACHE_H