/
qgsimagecache.cpp
279 lines (235 loc) · 8.87 KB
/
qgsimagecache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/***************************************************************************
qgsimagecache.cpp
-----------------
begin : December 2018
copyright : (C) 2018 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsimagecache.h"
#include "qgis.h"
#include "qgsimageoperation.h"
#include "qgslogger.h"
#include "qgsnetworkaccessmanager.h"
#include "qgsmessagelog.h"
#include "qgsnetworkcontentfetchertask.h"
#include <QApplication>
#include <QCoreApplication>
#include <QCursor>
#include <QDomDocument>
#include <QDomElement>
#include <QFile>
#include <QImage>
#include <QPainter>
#include <QPicture>
#include <QFileInfo>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QBuffer>
#include <QImageReader>
#include <QSvgRenderer>
///@cond PRIVATE
QgsImageCacheEntry::QgsImageCacheEntry( const QString &path, QSize size, const bool keepAspectRatio, const double opacity )
: QgsAbstractContentCacheEntry( path )
, size( size )
, keepAspectRatio( keepAspectRatio )
, opacity( opacity )
{
}
bool QgsImageCacheEntry::isEqual( const QgsAbstractContentCacheEntry *other ) const
{
const QgsImageCacheEntry *otherImage = dynamic_cast< const QgsImageCacheEntry * >( other );
// cheapest checks first!
if ( !otherImage || otherImage->keepAspectRatio != keepAspectRatio || otherImage->size != size || otherImage->path != path || otherImage->opacity != opacity )
return false;
return true;
}
int QgsImageCacheEntry::dataSize() const
{
int size = 0;
if ( !image.isNull() )
{
size += ( image.width() * image.height() * 32 );
}
return size;
}
void QgsImageCacheEntry::dump() const
{
QgsDebugMsgLevel( QStringLiteral( "path: %1, size %2x%3" ).arg( path ).arg( size.width() ).arg( size.height() ), 3 );
}
///@endcond
QgsImageCache::QgsImageCache( QObject *parent )
: QgsAbstractContentCache< QgsImageCacheEntry >( parent, QObject::tr( "Image" ) )
{
mMissingSvg = QStringLiteral( "<svg width='10' height='10'><text x='5' y='10' font-size='10' text-anchor='middle'>?</text></svg>" ).toLatin1();
const QString downloadingSvgPath = QgsApplication::defaultThemePath() + QStringLiteral( "downloading_svg.svg" );
if ( QFile::exists( downloadingSvgPath ) )
{
QFile file( downloadingSvgPath );
if ( file.open( QIODevice::ReadOnly ) )
{
mFetchingSvg = file.readAll();
}
}
if ( mFetchingSvg.isEmpty() )
{
mFetchingSvg = QStringLiteral( "<svg width='10' height='10'><text x='5' y='10' font-size='10' text-anchor='middle'>?</text></svg>" ).toLatin1();
}
connect( this, &QgsAbstractContentCacheBase::remoteContentFetched, this, &QgsImageCache::remoteImageFetched );
}
QImage QgsImageCache::pathAsImage( const QString &f, const QSize size, const bool keepAspectRatio, const double opacity, bool &fitsInCache, bool blocking, bool *isMissing )
{
const QString file = f.trimmed();
if ( isMissing )
*isMissing = true;
if ( file.isEmpty() )
return QImage();
QMutexLocker locker( &mMutex );
fitsInCache = true;
QgsImageCacheEntry *currentEntry = findExistingEntry( new QgsImageCacheEntry( file, size, keepAspectRatio, opacity ) );
QImage result;
//if current entry image is null: create the image
// checks to see if image will fit into cache
//update stats for memory usage
if ( currentEntry->image.isNull() )
{
long cachedDataSize = 0;
bool isBroken = false;
result = renderImage( file, size, keepAspectRatio, opacity, isBroken, blocking );
cachedDataSize += result.width() * result.height() * 32;
if ( cachedDataSize > mMaxCacheSize / 2 )
{
fitsInCache = false;
currentEntry->image = QImage();
}
else
{
mTotalSize += ( result.width() * result.height() * 32 );
currentEntry->image = result;
}
if ( isMissing )
*isMissing = isBroken;
currentEntry->isMissingImage = isBroken;
trimToMaximumSize();
}
else
{
result = currentEntry->image;
if ( isMissing )
*isMissing = currentEntry->isMissingImage;
}
return result;
}
QSize QgsImageCache::originalSize( const QString &path, bool blocking ) const
{
if ( path.isEmpty() )
return QSize();
// direct read if path is a file -- maybe more efficient than going the bytearray route? (untested!)
if ( !path.startsWith( QStringLiteral( "base64:" ) ) && QFile::exists( path ) )
{
QImageReader reader( path );
if ( reader.size().isValid() )
return reader.size();
else
return QImage( path ).size();
}
else
{
QByteArray ba = getContent( path, QByteArray( "broken" ), QByteArray( "fetching" ), blocking );
if ( ba != "broken" && ba != "fetching" )
{
QBuffer buffer( &ba );
buffer.open( QIODevice::ReadOnly );
QImageReader reader( &buffer );
// if QImageReader::size works, then it's more efficient as it doesn't
// read the whole image (see Qt docs)
const QSize s = reader.size();
if ( s.isValid() )
return s;
QImage im = reader.read();
return im.isNull() ? QSize() : im.size();
}
}
return QSize();
}
QImage QgsImageCache::renderImage( const QString &path, QSize size, const bool keepAspectRatio, const double opacity, bool &isBroken, bool blocking ) const
{
QImage im;
isBroken = false;
// direct read if path is a file -- maybe more efficient than going the bytearray route? (untested!)
if ( !path.startsWith( QStringLiteral( "base64:" ) ) && QFile::exists( path ) )
{
im = QImage( path );
}
else
{
QByteArray ba = getContent( path, QByteArray( "broken" ), QByteArray( "fetching" ), blocking );
if ( ba == "broken" )
{
isBroken = true;
// if the size parameter is not valid, skip drawing of missing image symbol
if ( !size.isValid() )
return im;
// if image size is set to respect aspect ratio, correct for broken image aspect ratio
if ( size.width() == 0 )
size.setWidth( size.height() );
if ( size.height() == 0 )
size.setHeight( size.width() );
// render "broken" svg
im = QImage( size, QImage::Format_ARGB32_Premultiplied );
im.fill( 0 ); // transparent background
QPainter p( &im );
QSvgRenderer r( mMissingSvg );
QSizeF s( r.viewBox().size() );
s.scale( size.width(), size.height(), Qt::KeepAspectRatio );
QRectF rect( ( size.width() - s.width() ) / 2, ( size.height() - s.height() ) / 2, s.width(), s.height() );
r.render( &p, rect );
}
else if ( ba == "fetching" )
{
// if image size is set to respect aspect ratio, correct for broken image aspect ratio
if ( size.width() == 0 )
size.setWidth( size.height() );
if ( size.height() == 0 )
size.setHeight( size.width() );
// render "fetching" svg
im = QImage( size, QImage::Format_ARGB32_Premultiplied );
im.fill( 0 ); // transparent background
QPainter p( &im );
QSvgRenderer r( mFetchingSvg );
QSizeF s( r.viewBox().size() );
s.scale( size.width(), size.height(), Qt::KeepAspectRatio );
QRectF rect( ( size.width() - s.width() ) / 2, ( size.height() - s.height() ) / 2, s.width(), s.height() );
r.render( &p, rect );
}
else
{
QBuffer buffer( &ba );
buffer.open( QIODevice::ReadOnly );
QImageReader reader( &buffer );
im = reader.read();
}
}
if ( !im.hasAlphaChannel() )
im = im.convertToFormat( QImage::Format_ARGB32 );
if ( opacity < 1.0 )
QgsImageOperation::multiplyOpacity( im, opacity );
// render image at desired size -- null size means original size
if ( !size.isValid() || size.isNull() || im.size() == size )
return im;
// when original aspect ratio is respected and provided height value is 0, automatically compute height
else if ( keepAspectRatio && size.height() == 0 )
return im.scaledToWidth( size.width(), Qt::SmoothTransformation );
// when original aspect ratio is respected and provided width value is 0, automatically compute width
else if ( keepAspectRatio && size.width() == 0 )
return im.scaledToHeight( size.height(), Qt::SmoothTransformation );
else
return im.scaled( size, keepAspectRatio ? Qt::KeepAspectRatio : Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
}