1
/***************************************************************************
2
qgssvgcache.cpp - SVG rendering and pixmap caching
4
begin : Sat Jul 17 2004
5
copyright : (C) 2004 by Lars Luthman
6
email : larsl at users dot sourceforge dot org
7
***************************************************************************/
9
/***************************************************************************
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. *
16
***************************************************************************/
17
/* $Id: qgssvgcache.cpp,v 1.8 2004/11/27 19:19:17 gsherman Exp $ */
24
#include <qsettings.h>
25
#include <qmessagebox.h>
27
#include "qgssvgcache.h"
30
QgsSVGCache::QgsSVGCache() {
32
oversampling = settings.readNumEntry("/qgis/svgoversampling", 4);
33
pixelLimit = settings.readNumEntry("/qgis/svgcachesize", 200000);
38
QPixmap QgsSVGCache::getPixmap(QString filename, double scaleFactor) {
40
// make the symbols smaller
43
PixmapMap::const_iterator iter;
44
PixmapMap::key_type key(filename, scaleFactor);
45
iter = pixmapMap.find(key);
47
// if we already have the pixmap, return it
48
if (iter != pixmapMap.end()) {
49
std::cerr<<"SVGCACHE: "<<filename<<"["<<scaleFactor
50
<<"] is already loaded"<<std::endl;
54
// if not, try to load it
55
std::cerr<<"SVGCACHE: loading "<<filename<<"["<<scaleFactor<<"]"<<std::endl;
57
pic.load(filename,"svg");
58
int width=pic.boundingRect().width();
59
width=static_cast<int>(static_cast<double>(width)*scaleFactor);
60
int height=pic.boundingRect().height();
61
height=static_cast<int>(static_cast<double>(height)*scaleFactor);
63
//prevent 0 width or height, which would cause a crash
71
// render and rescale it (with smoothing)
72
QPixmap osPixmap(oversampling*width,oversampling*height);
73
osPixmap.fill(QColor(qRgb(255, 255, 0))); QPainter p(&osPixmap);
74
p.scale(scaleFactor*oversampling,scaleFactor*oversampling);
75
p.drawPicture(0,0,pic);
76
QImage osImage = osPixmap.convertToImage();
77
// set a mask - this is probably terribly inefficient
78
osImage.setAlphaBuffer(true);
79
for (int i = 0; i < osImage.width(); ++i) {
80
for (int j = 0; j < osImage.height(); ++j) {
81
if (osImage.pixel(i, j) == qRgb(255, 255, 0)) {
82
osImage.setPixel(i, j, qRgba(255, 255, 0, 0));
86
if (oversampling != 1)
87
osImage = osImage.smoothScale(width, height);
88
QPixmap pixmap = QPixmap(osImage);
90
// cache it if possible, and remove other pixmaps from the cache
91
// if it grows too large
92
if (width * height < pixelLimit) {
93
std::cerr<<"SVGCACHE: Caching "<<filename<<"["<<scaleFactor<<"]"
95
pixmapMap[key] = pixmap;
97
totalPixels += width * height;
98
while (totalPixels > pixelLimit) {
99
std::cerr<<"SVGCACHE: Deleting "<<fifo.front().first<<"["
100
<<fifo.front().second<<"] from cache"<<std::endl;
101
QPixmap& oldPM(pixmapMap[fifo.front()]);
103
totalPixels -= oldPM.width() * oldPM.height();
111
void QgsSVGCache::clear() {
113
fifo = std::queue<PixmapMap::key_type>();
118
void QgsSVGCache::setOversampling(int oversamplingFactor) {
119
oversampling = oversamplingFactor;
123
int QgsSVGCache::getOversampling() const {