Quantum GIS API Documentation  1.8
src/core/symbology-ng/qgssvgcache.h
Go to the documentation of this file.
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 
00026 class QDomElement;
00027 class QImage;
00028 class QPicture;
00029 
00030 struct CORE_EXPORT QgsSvgCacheEntry
00031 {
00032   QgsSvgCacheEntry();
00033   QgsSvgCacheEntry( const QString& file, double size, double outlineWidth, double widthScaleFactor, double rasterScaleFctor, const QColor& fill, const QColor& outline );
00034   ~QgsSvgCacheEntry();
00035 
00036   QString file;
00037   int size; //size in pixel
00038   double outlineWidth;
00039   double widthScaleFactor;
00040   double rasterScaleFactor;
00041   QColor fill;
00042   QColor outline;
00043   QImage* image;
00044   QPicture* picture;
00045   //content (with params replaced)
00046   QByteArray svgContent;
00047 
00048   //keep entries on a least, sorted by last access
00049   QgsSvgCacheEntry* nextEntry;
00050   QgsSvgCacheEntry* previousEntry;
00051 
00053   bool operator==( const QgsSvgCacheEntry& other ) const;
00055   int dataSize() const;
00056 };
00057 
00061 class CORE_EXPORT QgsSvgCache
00062 {
00063   public:
00064 
00065     static QgsSvgCache* instance();
00066     ~QgsSvgCache();
00067 
00068     const QImage& svgAsImage( const QString& file, int size, const QColor& fill, const QColor& outline, double outlineWidth,
00069                               double widthScaleFactor, double rasterScaleFactor );
00070     const QPicture& svgAsPicture( const QString& file, int size, const QColor& fill, const QColor& outline, double outlineWidth,
00071                                   double widthScaleFactor, double rasterScaleFactor );
00072 
00075     void containsParams( const QString& path, bool& hasFillParam, QColor& defaultFillColor, bool& hasOutlineParam, QColor& defaultOutlineColor, bool& hasOutlineWidthParam,
00076                          double& defaultOutlineWidth ) const;
00077 
00078   protected:
00079     QgsSvgCache();
00080 
00082     QgsSvgCacheEntry* insertSVG( const QString& file, int size, const QColor& fill, const QColor& outline, double outlineWidth,
00083                                  double widthScaleFactor, double rasterScaleFactor );
00084 
00085     void replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry );
00086     void cacheImage( QgsSvgCacheEntry* entry );
00087     void cachePicture( QgsSvgCacheEntry* entry );
00089     QgsSvgCacheEntry* cacheEntry( const QString& file, int size, const QColor& fill, const QColor& outline, double outlineWidth,
00090                                   double widthScaleFactor, double rasterScaleFactor );
00091 
00093     void trimToMaximumSize();
00094 
00095     //Removes entry from the ordered list (but does not delete the entry itself)
00096     void takeEntryFromList( QgsSvgCacheEntry* entry );
00097 
00098   private:
00099     static QgsSvgCache* mInstance;
00100 
00102     QMultiHash< QString, QgsSvgCacheEntry* > mEntryLookup;
00104     long mTotalSize;
00105 
00106     //The svg cache keeps the entries on a double connected list, moving the current entry to the front.
00107     //That way, removing entries for more space can start with the least used objects.
00108     QgsSvgCacheEntry* mLeastRecentEntry;
00109     QgsSvgCacheEntry* mMostRecentEntry;
00110 
00111     //Maximum cache size
00112     static const long mMaximumSize = 20000000;
00113 
00115     void replaceElemParams( QDomElement& elem, const QColor& fill, const QColor& outline, double outlineWidth );
00116 
00117     void containsElemParams( const QDomElement& elem, bool& hasFillParam, QColor& defaultFill, bool& hasOutlineParam, QColor& defaultOutline,
00118                              bool& hasOutlineWidthParam, double& defaultOutlineWidth ) const;
00119 
00121     void removeCacheEntry( QString s, QgsSvgCacheEntry* entry );
00122 
00124     void printEntryList();
00125 };
00126 
00127 #endif // QGSSVGCACHE_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines