~ubuntu-branches/ubuntu/quantal/qgis/quantal

« back to all changes in this revision

Viewing changes to src/qgssvgcache.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Steve Halasz
  • Date: 2004-12-21 09:46:27 UTC
  • Revision ID: james.westby@ubuntu.com-20041221094627-r9lb6mlz2o3yp8gn
Tags: upstream-0.6.0
ImportĀ upstreamĀ versionĀ 0.6.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
              qgssvgcache.cpp  -  SVG rendering and pixmap caching
 
3
                             -------------------
 
4
    begin                : Sat Jul 17 2004
 
5
    copyright            : (C) 2004 by Lars Luthman
 
6
    email                : larsl at users dot sourceforge dot org
 
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
/*  $Id: qgssvgcache.cpp,v 1.8 2004/11/27 19:19:17 gsherman Exp $ */
 
18
 
 
19
#include <iostream>
 
20
 
 
21
#include <qimage.h>
 
22
#include <qpainter.h>
 
23
#include <qpicture.h>
 
24
#include <qsettings.h>
 
25
#include <qmessagebox.h>
 
26
 
 
27
#include "qgssvgcache.h"
 
28
 
 
29
 
 
30
QgsSVGCache::QgsSVGCache() {
 
31
  QSettings settings;
 
32
  oversampling = settings.readNumEntry("/qgis/svgoversampling", 4);
 
33
  pixelLimit = settings.readNumEntry("/qgis/svgcachesize", 200000);
 
34
  totalPixels = 0;
 
35
}
 
36
 
 
37
 
 
38
QPixmap QgsSVGCache::getPixmap(QString filename, double scaleFactor) {
 
39
  
 
40
  // make the symbols smaller
 
41
  scaleFactor *= 0.30;
 
42
  
 
43
  PixmapMap::const_iterator iter;
 
44
  PixmapMap::key_type key(filename, scaleFactor);
 
45
  iter = pixmapMap.find(key);
 
46
  
 
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;
 
51
    return iter->second;
 
52
  }
 
53
  
 
54
  // if not, try to load it
 
55
  std::cerr<<"SVGCACHE: loading "<<filename<<"["<<scaleFactor<<"]"<<std::endl;
 
56
  QPicture pic;
 
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);
 
62
  
 
63
  //prevent 0 width or height, which would cause a crash
 
64
  if (width == 0) {
 
65
    width = 1;
 
66
  }
 
67
  if (height == 0) {
 
68
    height = 1;
 
69
  }
 
70
  
 
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));
 
83
      }
 
84
    }
 
85
  }
 
86
  if (oversampling != 1)
 
87
    osImage = osImage.smoothScale(width, height);
 
88
  QPixmap pixmap = QPixmap(osImage);
 
89
 
 
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<<"]"
 
94
             <<std::endl;
 
95
    pixmapMap[key] = pixmap;
 
96
    fifo.push(key);
 
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()]);
 
102
      fifo.pop();
 
103
      totalPixels -= oldPM.width() * oldPM.height();
 
104
    }
 
105
  }
 
106
  
 
107
  return pixmap;
 
108
}
 
109
  
 
110
 
 
111
void QgsSVGCache::clear() {
 
112
  pixmapMap.clear();
 
113
  fifo = std::queue<PixmapMap::key_type>();
 
114
  totalPixels = 0;
 
115
}
 
116
 
 
117
 
 
118
void QgsSVGCache::setOversampling(int oversamplingFactor) {
 
119
  oversampling = oversamplingFactor;
 
120
}
 
121
  
 
122
 
 
123
int QgsSVGCache::getOversampling() const {
 
124
  return oversampling;
 
125
}