42 #include "private/qdeclarativepixmapcache_p.h" 47 #include <private/qdeclarativeglobal_p.h> 48 #include <private/qdeclarativeengine_p.h> 50 #include <QCoreApplication> 51 #include <QImageReader> 53 #include <QNetworkReply> 54 #include <QPixmapCache> 58 #include <QMutexLocker> 59 #include <QWaitCondition> 61 #include <QWaitCondition> 62 #include <QtCore/qdebug.h> 63 #include <private/qobject_p.h> 66 #define IMAGEREQUEST_MAX_REQUEST_COUNT 8 67 #define IMAGEREQUEST_MAX_REDIRECT_RECURSION 16 68 #define CACHE_EXPIRE_TIME 30 69 #define CACHE_REMOVAL_FRACTION 4 75 #if defined(Q_WS_QWS) || defined(Q_WS_WINCE) 134 void networkRequestDone();
191 prevUnreferencedPtr(0), nextUnreferenced(0)
197 url(u),
requestSize(r), reply(0), prevUnreferenced(0), prevUnreferencedPtr(0),
203 : refCount(1), inCache(false), privatePixmap(false), pixmapStatus(
QDeclarativePixmap::Ready),
205 prevUnreferencedPtr(0), nextUnreferenced(0)
210 : refCount(1), inCache(false), privatePixmap(true), pixmapStatus(
QDeclarativePixmap::Ready),
212 prevUnreferencedPtr(0), nextUnreferenced(0)
220 void removeFromCache();
268 if (!accessManager) {
272 return accessManager;
280 bool force_scale =
false;
287 if (requestSize.
width() > 0 || requestSize.
height() > 0) {
289 if (requestSize.
width() && (force_scale || requestSize.
width() < s.
width())) {
290 if (requestSize.
height() <= 0)
295 if (requestSize.
width() <= 0)
303 *impsize = imgio.
size();
305 if (imgio.
read(image)) {
306 if (impsize && impsize->
width() < 0)
307 *impsize = image->
size();
311 *errorString = QDeclarativePixmap::tr(
"Error decoding: %1: %2").
arg(url.
toString())
318 :
QThread(eng), engine(eng), threadObject(0), accessManager(0)
380 if (reply->
error()) {
437 if (cancelled.count()) {
438 for (
int i = 0; i < cancelled.count(); ++i) {
443 replies.remove(reply);
458 processJob(runningJob, url, requestSize);
465 const QSize &requestSize)
478 errorStr = QDeclarativePixmap::tr(
"Failed to get image from provider: %1").
arg(url.
toString());
481 if (!cancelled.contains(runningJob)) runningJob->
postReply(errorCode, errorStr, readSize, image);
493 if (!
readImage(url, &f, &image, &errorStr, &readSize, requestSize))
496 errorStr = QDeclarativePixmap::tr(
"Cannot open: %1").
arg(url.
toString());
500 if (!cancelled.contains(runningJob)) runningJob->
postReply(errorCode, errorStr, readSize, image);
509 replies.insert(reply, runningJob);
520 readers.insert(engine, reader);
529 return readers.value(engine, 0);
539 if (threadObject) threadObject->processJobs();
548 cancelled.append(reply);
551 if (threadObject) threadObject->processJobs();
553 jobs.removeAll(reply);
561 if (replyDownloadProgress == -1) {
565 replyDownloadProgress = nr->
indexOfSignal(
"downloadProgress(qint64,qint64)");
567 downloadProgress = pr->
indexOfSignal(
"downloadProgress(qint64,qint64)");
568 threadNetworkRequestDone = ir->
indexOfSlot(
"networkRequestDone()");
616 void shrinkCache(
int remove);
627 : m_unreferencedPixmaps(0), m_lastUnreferencedPixmap(0), m_unreferencedCost(0), m_timerId(-1)
687 remove -= data->
cost();
715 :
data(d), engineForReader(0), requestSize(d->requestSize), url(d->url), loading(false), redirectCount(0)
756 return (pixmap.width() * pixmap.height() * pixmap.depth()) / 8;
762 if (prevUnreferencedPtr)
763 pixmapStore()->referencePixmap(
this);
776 QDeclarativePixmapReader::readerMutex.
lock();
779 reader->
cancel(cancelReply);
780 QDeclarativePixmapReader::readerMutex.
unlock();
784 pixmapStore()->unreferencePixmap(
this);
796 pixmapStore()->m_cache.insert(key,
this);
805 pixmapStore()->m_cache.remove(key);
838 QDeclarativePixmap::tr(
"Failed to get image from provider: %1").arg(url.
toString()));
851 if (
readImage(url, &f, &image, &errorString, &readSize, requestSize)) {
856 errorString = QDeclarativePixmap::tr(
"Cannot open: %1").
arg(url.
toString());
883 load(engine, url, size);
935 return nullPixmap()->url;
943 return nullPixmap()->size;
951 return nullPixmap()->size;
959 return nullPixmap()->pixmap;
1027 if (!(options & QDeclarativePixmap::Asynchronous)) {
1045 QDeclarativePixmapReader::readerMutex.
lock();
1047 QDeclarativePixmapReader::readerMutex.
unlock();
1075 qWarning(
"QDeclarativePixmap: connectFinished() called when not loading.");
1085 qWarning(
"QDeclarativePixmap: connectFinished() called when not loading.");
1095 qWarning(
"QDeclarativePixmap: connectDownloadProgress() called when not loading.");
1105 qWarning(
"QDeclarativePixmap: connectDownloadProgress() called when not loading.");
1114 pixmapStore()->flushCache();
1119 #include <qdeclarativepixmapcache.moc>
ImageType
Defines the type of image supported by this image provider.
The QVariant class acts like a union for the most common Qt data types.
int startTimer(int interval)
Starts a timer and returns a timer identifier, or returns zero if it could not start a timer...
static QString urlToLocalFileOrQrc(const QUrl &url)
#define CACHE_REMOVAL_FRACTION
QHash< QNetworkReply *, QDeclarativePixmapReply * > replies
QDeclarativePixmapData * data
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.
NetworkError error() const
Returns the error that was found during the processing of this request.
void processJob(QDeclarativePixmapReply *, const QUrl &, const QSize &)
#define QT_END_NAMESPACE
This macro expands to.
void lock()
Locks the mutex.
The QMutex class provides access serialization between threads.
int width() const
Returns the width of the pixmap.
int remove(const Key &key)
Removes all the items that have the key from the hash.
QString toString(FormattingOptions options=None) const
Returns the human-displayable string representation of the URL.
QDeclarativePixmapReply(QDeclarativePixmapData *)
bool connectDownloadProgress(QObject *, const char *)
QDeclarativePixmapData(const QUrl &u, const QSize &r)
void setScaledSize(const QSize &size)
Sets the scaled size of the image to size.
bool open(OpenMode flags)
Opens the file using OpenMode mode, returning true if successful; otherwise false.
QDeclarativePixmapData * d
QObject * eventLoopQuitHack
static void postEvent(QObject *receiver, QEvent *event)
Adds the event event, with the object receiver as the receiver of the event, to an event queue and re...
QVariant attribute(QNetworkRequest::Attribute code) const
Returns the attribute associated with the code code.
#define IMAGEREQUEST_MAX_REQUEST_COUNT
bool open(OpenMode openMode)
Reimplemented Function
bool isNull() const
Returns true if it is a null image, otherwise returns false.
The QNetworkReply class contains the data and headers for a request sent with QNetworkAccessManager.
The QByteArray class provides an array of bytes.
static int downloadProgressIndex
bool isRunning() const
Returns true when the request is still processing and the reply has not finished or was aborted yet...
QString errorString() const
Returns a human-readable description of the last device error that occurred.
virtual void timerEvent(QTimerEvent *)
This event handler can be reimplemented in a subclass to receive timer events for the object...
void unlock()
Unlocks this mutex locker.
static int replyDownloadProgress
static const QMetaObject staticMetaObject
This variable stores the meta-object for the class.
QDeclarativePixmapReader(QDeclarativeEngine *eng)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
#define Q_DISABLE_COPY(Class)
Disables the use of copy constructors and assignment operators for the given Class.
void unreferencePixmap(QDeclarativePixmapData *)
void cancel(QDeclarativePixmapReply *rep)
The QBuffer class provides a QIODevice interface for a QByteArray.
QDeclarativeEngine * engine
The QUrl class provides a convenient interface for working with URLs.
The QString class provides a Unicode character string.
void setHeight(int h)
Sets the height to the given height.
T take(const Key &key)
Removes the item with the key from the hash and returns the value associated with it...
The QHash class is a template class that provides a hash-table-based dictionary.
The QObject class is the base class of all Qt objects.
Q_GLOBAL_STATIC(QDeclarativePixmapStore, pixmapStore)
virtual bool event(QEvent *)
This virtual function receives events to an object and should return true if the event e was recogniz...
#define CACHE_EXPIRE_TIME
static QDeclarativeEnginePrivate * get(QDeclarativeEngine *e)
void postReply(ReadError, const QString &, const QSize &, const QImage &)
static QMutex readerMutex
void quit()
Tells the thread's event loop to exit with return code 0 (success).
void setAttribute(Attribute code, const QVariant &value)
Sets the attribute associated with code code to be value value.
QString path() const
Returns the path of the URL.
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
QObject * sender() const
Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; othe...
QDeclarativePixmapStore()
void setFormat(const QByteArray &format)
Sets the format QImageReader will use when reading images, to format.
Q_GUI_EXPORT QString errorString(EGLint code=eglGetError())
void setWidth(int w)
Sets the width to the given width.
void relock()
Relocks an unlocked mutex locker.
virtual void timerEvent(QTimerEvent *)
This event handler can be reimplemented in a subclass to receive timer events for the object...
QDeclarativeEngine * engineForReader
int width() const
Returns the width.
void append(const T &t)
Inserts value at the end of the list.
QString errorString() const
Returns a human readable description of the last error that occurred.
#define QT_BEGIN_NAMESPACE
This macro expands to.
QList< QDeclarativePixmapReply * > jobs
QBool contains(const T &t) const
Returns true if the list contains an occurrence of value; otherwise returns false.
const QPixmap & pixmap() const
QDeclarativePixmapReaderThreadObject(QDeclarativePixmapReader *)
virtual void close()
Closes this device for reading.
void destroyed(QObject *=0)
This signal is emitted immediately before the object obj is destroyed, and can not be blocked...
void referencePixmap(QDeclarativePixmapData *)
static QImage getImage(QTextDocument *doc, const QTextImageFormat &format)
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
void networkRequestDone()
QDeclarativeImageProvider::ImageType getImageProviderType(const QUrl &url)
QDeclarativePixmapReply * reply
QDeclarativePixmapData * m_lastUnreferencedPixmap
Q_CORE_EXPORT void qWarning(const char *,...)
The QImage class provides a hardware-independent image representation that allows direct access to th...
static const char * data(const QByteArray &arr)
~QDeclarativePixmapReader()
void moveToThread(QThread *thread)
Changes the thread affinity for this object and its children.
void clear()
Removes all items from the list.
QDeclarativePixmapData * nextUnreferenced
const QSize & requestSize() const
QNetworkAccessManager * networkAccessManager()
const QSize & implicitSize() const
QDeclarativePixmapReply * getImage(QDeclarativePixmapData *)
void setPixmap(const QPixmap &)
QNetworkReply * get(const QNetworkRequest &request)
Posts a request to obtain the contents of the target request and returns a new QNetworkReply object o...
QUrl toUrl() const
Returns the variant as a QUrl if the variant has type() Url ; otherwise returns an invalid QUrl...
The QNetworkAccessManager class allows the application to send network requests and receive replies...
void unlock()
Unlocks the mutex.
The QImageReader class provides a format independent interface for reading images from files or other...
QSize size() const
Returns the size of the image, i.
static int threadNetworkRequestDone
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
Disconnects signal in object sender from method in object receiver.
QDeclarativePixmapData(const QPixmap &p)
bool event(QEvent *event)
This virtual function receives events to an object and should return true if the event e was recogniz...
QString scheme() const
Returns the scheme of the URL.
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
void networkRequestDone(QNetworkReply *)
QImage getImageFromProvider(const QUrl &url, QSize *size, const QSize &req_size)
QDeclarativePixmapData * prevUnreferenced
The QFile class provides an interface for reading from and writing to files.
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash...
The QTimerEvent class contains parameters that describe a timer event.
bool connectFinished(QObject *, const char *)
The QDeclarativeEngine class provides an environment for instantiating QML components.
QDeclarativePixmapData(const QUrl &u, const QSize &s, const QString &e)
void run()
The starting point for the thread.
void start(Priority=InheritPriority)
Begins execution of the thread by calling run().
Q_INVOKABLE QObject(QObject *parent=0)
Constructs an object with parent object parent.
QList< T > values() const
Returns a list containing all the values in the hash, in an arbitrary order.
bool wait(unsigned long time=ULONG_MAX)
Blocks the thread until either of these conditions is met:
static QDeclarativePixmapData * createPixmapDataSync(QDeclarativeEngine *engine, const QUrl &url, const QSize &requestSize, bool *ok)
The QHash::iterator class provides an STL-style non-const iterator for QHash and QMultiHash.
QNetworkAccessManager * accessManager
QDeclarativePixmapData * m_unreferencedPixmaps
Event(ReadError, const QString &, const QSize &, const QImage &)
int height() const
Returns the height.
The QRect class defines a rectangle in the plane using integer precision.
QDeclarativePixmapReader * reader
void shrinkCache(int remove)
QByteArray readAll()
Reads all available data from the device, and returns it as a QByteArray.
The QNetworkRequest class holds a request to be sent with QNetworkAccessManager.
QRect rect() const
Returns the pixmap's enclosing rectangle.
The QPixmap class is an off-screen image representation that can be used as a paint device...
static int downloadProgress
QFuture< T > run(Function function,...)
~QDeclarativePixmapReply()
int height() const
Returns the height of the pixmap.
The QSize class defines the size of a two-dimensional object using integer point precision.
static QHash< QDeclarativeEngine *, QDeclarativePixmapReader * > readers
virtual bool event(QEvent *e)
This virtual function receives events to an object and should return true if the event e was recogniz...
#define IMAGEREQUEST_MAX_REDIRECT_RECURSION
QSize size() const
Returns the size of the image, without actually reading the image contents.
QHash< QDeclarativePixmapKey, QDeclarativePixmapData * > m_cache
iterator find(const Key &key)
Returns an iterator pointing to the item with the key in the hash.
QList< QDeclarativePixmapReply * > cancelled
QWaitCondition waitCondition
QUrl url() const
Returns the URL of the content downloaded or uploaded.
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.
void downloadProgress(qint64, qint64)
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
QUrl resolved(const QUrl &relative) const
Returns the result of the merge of this URL with relative.
bool isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid; otherwise returns false...
The QIODevice class is the base interface class of all I/O devices in Qt.
QDeclarativePixmapData ** prevUnreferencedPtr
uint qHash(const QDeclarativePixmapKey &key)
The QEvent class is the base class of all event classes.
Type type() const
Returns the event type.
QDeclarativePixmap::Status pixmapStatus
The QThread class provides a platform-independent way to manage threads.
QNetworkAccessManager * createNetworkAccessManager(QObject *parent) const
void deleteLater()
Schedules this object for deletion.
void load(QDeclarativeEngine *, const QUrl &)
QImage read()
Reads an image from the device.
static QDeclarativePixmapReader * instance(QDeclarativeEngine *engine)
QPixmap getPixmapFromProvider(const QUrl &url, QSize *size, const QSize &req_size)
QDeclarativePixmapData(const QUrl &u, const QPixmap &p, const QSize &s, const QSize &r)
static QDeclarativePixmapReader * existingInstance(QDeclarativeEngine *engine)
The QList class is a template class that provides lists.
void killTimer(int id)
Kills the timer with timer identifier, id.
QDeclarativePixmapReaderThreadObject * threadObject
bool operator==(const QDeclarativePixmapKey &lhs, const QDeclarativePixmapKey &rhs)