Qt 4.8
Classes | Macros | Functions | Variables
qdeclarativepixmapcache.cpp File Reference
#include "private/qdeclarativepixmapcache_p.h"
#include "qdeclarativenetworkaccessmanagerfactory.h"
#include "qdeclarativeimageprovider.h"
#include <qdeclarativeengine.h>
#include <private/qdeclarativeglobal_p.h>
#include <private/qdeclarativeengine_p.h>
#include <QCoreApplication>
#include <QImageReader>
#include <QHash>
#include <QNetworkReply>
#include <QPixmapCache>
#include <QFile>
#include <QThread>
#include <QMutex>
#include <QMutexLocker>
#include <QWaitCondition>
#include <QBuffer>
#include <QtCore/qdebug.h>
#include <private/qobject_p.h>
#include <QSslError>
#include <qdeclarativepixmapcache.moc>

Go to the source code of this file.

Classes

class  QDeclarativePixmapData
 
class  QDeclarativePixmapKey
 
struct  QDeclarativePixmapNull
 
class  QDeclarativePixmapReader
 
class  QDeclarativePixmapReaderThreadObject
 
class  QDeclarativePixmapReply
 
class  QDeclarativePixmapReply::Event
 
class  QDeclarativePixmapStore
 

Macros

#define CACHE_EXPIRE_TIME   30
 
#define CACHE_REMOVAL_FRACTION   4
 
#define IMAGEREQUEST_MAX_REDIRECT_RECURSION   16
 
#define IMAGEREQUEST_MAX_REQUEST_COUNT   8
 

Functions

static QDeclarativePixmapDatacreatePixmapDataSync (QDeclarativeEngine *engine, const QUrl &url, const QSize &requestSize, bool *ok)
 
bool operator== (const QDeclarativePixmapKey &lhs, const QDeclarativePixmapKey &rhs)
 
 Q_GLOBAL_STATIC (QDeclarativePixmapStore, pixmapStore)
 
 Q_GLOBAL_STATIC (QDeclarativePixmapNull, nullPixmap)
 
uint qHash (const QDeclarativePixmapKey &key)
 
static bool readImage (const QUrl &url, QIODevice *dev, QImage *image, QString *errorString, QSize *impsize, const QSize &requestSize)
 

Variables

static int cache_limit = 2048 * 1024
 

Macro Definition Documentation

◆ CACHE_EXPIRE_TIME

#define CACHE_EXPIRE_TIME   30

◆ CACHE_REMOVAL_FRACTION

#define CACHE_REMOVAL_FRACTION   4

Definition at line 69 of file qdeclarativepixmapcache.cpp.

Referenced by QDeclarativePixmapStore::timerEvent().

◆ IMAGEREQUEST_MAX_REDIRECT_RECURSION

#define IMAGEREQUEST_MAX_REDIRECT_RECURSION   16

◆ IMAGEREQUEST_MAX_REQUEST_COUNT

#define IMAGEREQUEST_MAX_REQUEST_COUNT   8

Definition at line 66 of file qdeclarativepixmapcache.cpp.

Referenced by QDeclarativePixmapReader::processJobs().

Function Documentation

◆ createPixmapDataSync()

static QDeclarativePixmapData* createPixmapDataSync ( QDeclarativeEngine engine,
const QUrl url,
const QSize requestSize,
bool *  ok 
)
static

Definition at line 810 of file qdeclarativepixmapcache.cpp.

Referenced by QDeclarativePixmap::load().

811 {
812  if (url.scheme() == QLatin1String("image")) {
813  QSize readSize;
816 
817  switch (imageType) {
819  {
820  QImage image = ep->getImageFromProvider(url, &readSize, requestSize);
821  if (!image.isNull()) {
822  *ok = true;
823  return new QDeclarativePixmapData(url, QPixmap::fromImage(image), readSize, requestSize);
824  }
825  }
827  {
828  QPixmap pixmap = ep->getPixmapFromProvider(url, &readSize, requestSize);
829  if (!pixmap.isNull()) {
830  *ok = true;
831  return new QDeclarativePixmapData(url, pixmap, readSize, requestSize);
832  }
833  }
834  }
835 
836  // no matching provider, or provider has bad image type, or provider returned null image
837  return new QDeclarativePixmapData(url, requestSize,
838  QDeclarativePixmap::tr("Failed to get image from provider: %1").arg(url.toString()));
839  }
840 
842  if (localFile.isEmpty())
843  return 0;
844 
845  QFile f(localFile);
846  QSize readSize;
848 
849  if (f.open(QIODevice::ReadOnly)) {
850  QImage image;
851  if (readImage(url, &f, &image, &errorString, &readSize, requestSize)) {
852  *ok = true;
853  return new QDeclarativePixmapData(url, QPixmap::fromImage(image), readSize, requestSize);
854  }
855  } else {
856  errorString = QDeclarativePixmap::tr("Cannot open: %1").arg(url.toString());
857  }
858  return new QDeclarativePixmapData(url, requestSize, errorString);
859 }
ImageType
Defines the type of image supported by this image provider.
static QString urlToLocalFileOrQrc(const QUrl &url)
static QPixmap fromImage(const QImage &image, Qt::ImageConversionFlags flags=Qt::AutoColor)
Converts the given image to a pixmap using the specified flags to control the conversion.
Definition: qpixmap.cpp:2197
QString toString(FormattingOptions options=None) const
Returns the human-displayable string representation of the URL.
Definition: qurl.cpp:5896
bool isNull() const
Returns true if it is a null image, otherwise returns false.
Definition: qimage.cpp:1542
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QDeclarativeEnginePrivate * get(QDeclarativeEngine *e)
Q_GUI_EXPORT QString errorString(EGLint code=eglGetError())
Definition: qegl.cpp:743
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QDeclarativeImageProvider::ImageType getImageProviderType(const QUrl &url)
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
QString scheme() const
Returns the scheme of the URL.
Definition: qurl.cpp:4550
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
QImage getImageFromProvider(const QUrl &url, QSize *size, const QSize &req_size)
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
static bool readImage(const QUrl &url, QIODevice *dev, QImage *image, QString *errorString, QSize *impsize, const QSize &requestSize)
bool isNull() const
Returns true if this is a null pixmap; otherwise returns false.
Definition: qpixmap.cpp:615
QPixmap getPixmapFromProvider(const QUrl &url, QSize *size, const QSize &req_size)

◆ operator==()

bool operator== ( const QDeclarativePixmapKey lhs,
const QDeclarativePixmapKey rhs 
)
inline

Definition at line 589 of file qdeclarativepixmapcache.cpp.

590 {
591  return *lhs.size == *rhs.size && *lhs.url == *rhs.url;
592 }

◆ Q_GLOBAL_STATIC() [1/2]

Q_GLOBAL_STATIC ( QDeclarativePixmapStore  ,
pixmapStore   
)

◆ Q_GLOBAL_STATIC() [2/2]

Q_GLOBAL_STATIC ( QDeclarativePixmapNull  ,
nullPixmap   
)

◆ qHash()

uint qHash ( const QDeclarativePixmapKey key)
inline

Definition at line 594 of file qdeclarativepixmapcache.cpp.

595 {
596  return qHash(*key.url) ^ key.size->width() ^ key.size->height();
597 }
int width() const
Returns the width.
Definition: qsize.h:126
uint qHash(const QUrl &url)
Definition: qurl.h:285
int height() const
Returns the height.
Definition: qsize.h:129

◆ readImage()

static bool readImage ( const QUrl url,
QIODevice dev,
QImage image,
QString errorString,
QSize impsize,
const QSize requestSize 
)
static

Definition at line 275 of file qdeclarativepixmapcache.cpp.

Referenced by createPixmapDataSync(), QDeclarativePixmapReader::networkRequestDone(), and QDeclarativePixmapReader::processJob().

277 {
278  QImageReader imgio(dev);
279 
280  bool force_scale = false;
281  if (url.path().endsWith(QLatin1String(".svg"),Qt::CaseInsensitive)) {
282  imgio.setFormat("svg"); // QSvgPlugin::capabilities bug QTBUG-9053
283  force_scale = true;
284  }
285 
286  bool scaled = false;
287  if (requestSize.width() > 0 || requestSize.height() > 0) {
288  QSize s = imgio.size();
289  if (requestSize.width() && (force_scale || requestSize.width() < s.width())) {
290  if (requestSize.height() <= 0)
291  s.setHeight(s.height()*requestSize.width()/s.width());
292  s.setWidth(requestSize.width()); scaled = true;
293  }
294  if (requestSize.height() && (force_scale || requestSize.height() < s.height())) {
295  if (requestSize.width() <= 0)
296  s.setWidth(s.width()*requestSize.height()/s.height());
297  s.setHeight(requestSize.height()); scaled = true;
298  }
299  if (scaled) { imgio.setScaledSize(s); }
300  }
301 
302  if (impsize)
303  *impsize = imgio.size();
304 
305  if (imgio.read(image)) {
306  if (impsize && impsize->width() < 0)
307  *impsize = image->size();
308  return true;
309  } else {
310  if (errorString)
311  *errorString = QDeclarativePixmap::tr("Error decoding: %1: %2").arg(url.toString())
312  .arg(imgio.errorString());
313  return false;
314  }
315 }
QString toString(FormattingOptions options=None) const
Returns the human-displayable string representation of the URL.
Definition: qurl.cpp:5896
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
void setHeight(int h)
Sets the height to the given height.
Definition: qsize.h:135
QString path() const
Returns the path of the URL.
Definition: qurl.cpp:4977
void setWidth(int w)
Sets the width to the given width.
Definition: qsize.h:132
int width() const
Returns the width.
Definition: qsize.h:126
The QImageReader class provides a format independent interface for reading images from files or other...
Definition: qimagereader.h:62
QSize size() const
Returns the size of the image, i.
Definition: qimage.cpp:1587
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
int height() const
Returns the height.
Definition: qsize.h:129
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition: qstring.cpp:3796

Variable Documentation

◆ cache_limit

int cache_limit = 2048 * 1024
static

Definition at line 76 of file qdeclarativepixmapcache.cpp.

Referenced by QDeclarativePixmapStore::shrinkCache().