src/declarative/graphicsitems/qdeclarativeimagebase.cpp
changeset 33 3e2da88830cd
parent 30 5dc02b23752f
child 37 758a864f9613
equal deleted inserted replaced
30:5dc02b23752f 33:3e2da88830cd
    48 
    48 
    49 #include <QFile>
    49 #include <QFile>
    50 
    50 
    51 QT_BEGIN_NAMESPACE
    51 QT_BEGIN_NAMESPACE
    52 
    52 
       
    53 
       
    54 /*!
       
    55     \class QDeclarativeImageBase
       
    56     \internal
       
    57     \brief The base class for declarative images.
       
    58  */
       
    59 
    53 QDeclarativeImageBase::QDeclarativeImageBase(QDeclarativeImageBasePrivate &dd, QDeclarativeItem *parent)
    60 QDeclarativeImageBase::QDeclarativeImageBase(QDeclarativeImageBasePrivate &dd, QDeclarativeItem *parent)
    54   : QDeclarativeItem(dd, parent)
    61   : QDeclarativeItem(dd, parent)
    55 {
    62 {
    56 }
    63 }
    57 
    64 
    58 QDeclarativeImageBase::~QDeclarativeImageBase()
    65 QDeclarativeImageBase::~QDeclarativeImageBase()
    59 {
    66 {
    60     Q_D(QDeclarativeImageBase);
       
    61     if (d->pendingPixmapCache)
       
    62         QDeclarativePixmapCache::cancel(d->url, this);
       
    63 }
    67 }
    64 
    68 
    65 QDeclarativeImageBase::Status QDeclarativeImageBase::status() const
    69 QDeclarativeImageBase::Status QDeclarativeImageBase::status() const
    66 {
    70 {
    67     Q_D(const QDeclarativeImageBase);
    71     Q_D(const QDeclarativeImageBase);
    89         d->async = async;
    93         d->async = async;
    90         emit asynchronousChanged();
    94         emit asynchronousChanged();
    91     }
    95     }
    92 }
    96 }
    93 
    97 
    94 
       
    95 QUrl QDeclarativeImageBase::source() const
    98 QUrl QDeclarativeImageBase::source() const
    96 {
    99 {
    97     Q_D(const QDeclarativeImageBase);
   100     Q_D(const QDeclarativeImageBase);
    98     return d->url;
   101     return d->url;
    99 }
   102 }
   103     Q_D(QDeclarativeImageBase);
   106     Q_D(QDeclarativeImageBase);
   104     //equality is fairly expensive, so we bypass for simple, common case
   107     //equality is fairly expensive, so we bypass for simple, common case
   105     if ((d->url.isEmpty() == url.isEmpty()) && url == d->url)
   108     if ((d->url.isEmpty() == url.isEmpty()) && url == d->url)
   106         return;
   109         return;
   107 
   110 
   108     if (d->pendingPixmapCache) {
       
   109         QDeclarativePixmapCache::cancel(d->url, this);
       
   110         d->pendingPixmapCache = false;
       
   111     }
       
   112 
       
   113     d->url = url;
   111     d->url = url;
   114     emit sourceChanged(d->url);
   112     emit sourceChanged(d->url);
   115 
   113 
   116     if (isComponentComplete())
   114     if (isComponentComplete())
   117         load();
   115         load();
   120 void QDeclarativeImageBase::setSourceSize(const QSize& size)
   118 void QDeclarativeImageBase::setSourceSize(const QSize& size)
   121 {
   119 {
   122     Q_D(QDeclarativeImageBase);
   120     Q_D(QDeclarativeImageBase);
   123     if (d->sourcesize == size)
   121     if (d->sourcesize == size)
   124         return;
   122         return;
       
   123 
   125     d->sourcesize = size;
   124     d->sourcesize = size;
   126     emit sourceSizeChanged();
   125     emit sourceSizeChanged();
   127     if (isComponentComplete())
   126     if (isComponentComplete())
   128         load();
   127         load();
   129 }
   128 }
   141         d->progress = 0.0;
   140         d->progress = 0.0;
   142         emit progressChanged(d->progress);
   141         emit progressChanged(d->progress);
   143     }
   142     }
   144 
   143 
   145     if (d->url.isEmpty()) {
   144     if (d->url.isEmpty()) {
   146         d->pix = QPixmap();
   145         d->pix.clear();
   147         d->status = Null;
   146         d->status = Null;
   148         setImplicitWidth(0);
   147         setImplicitWidth(0);
   149         setImplicitHeight(0);
   148         setImplicitHeight(0);
   150         emit statusChanged(d->status);
   149         emit statusChanged(d->status);
   151         pixmapChange();
   150         pixmapChange();
   152         update();
   151         update();
   153     } else {
   152     } else {
   154         d->status = Loading;
   153         d->status = Loading;
   155         int reqwidth = d->sourcesize.width();
   154 
   156         int reqheight = d->sourcesize.height();
   155         d->pix.load(qmlEngine(this), d->url, d->sourcesize, d->async);
   157         QSize impsize;
   156 
   158         QString errorString;
   157         if (d->pix.isLoading()) {
   159         QDeclarativePixmapReply::Status status = QDeclarativePixmapCache::get(d->url, &d->pix, &errorString, &impsize, d->async, reqwidth, reqheight);
   158 
   160         if (status != QDeclarativePixmapReply::Ready && status != QDeclarativePixmapReply::Error) {
       
   161             QDeclarativePixmapReply *reply = QDeclarativePixmapCache::request(qmlEngine(this), d->url, reqwidth, reqheight);
       
   162             d->pendingPixmapCache = true;
       
   163 
       
   164             static int replyDownloadProgress = -1;
       
   165             static int replyFinished = -1;
       
   166             static int thisRequestProgress = -1;
   159             static int thisRequestProgress = -1;
   167             static int thisRequestFinished = -1;
   160             static int thisRequestFinished = -1;
   168             if (replyDownloadProgress == -1) {
   161             if (thisRequestProgress == -1) {
   169                 replyDownloadProgress =
       
   170                     QDeclarativePixmapReply::staticMetaObject.indexOfSignal("downloadProgress(qint64,qint64)");
       
   171                 replyFinished =
       
   172                     QDeclarativePixmapReply::staticMetaObject.indexOfSignal("finished()");
       
   173                 thisRequestProgress =
   162                 thisRequestProgress =
   174                     QDeclarativeImageBase::staticMetaObject.indexOfSlot("requestProgress(qint64,qint64)");
   163                     QDeclarativeImageBase::staticMetaObject.indexOfSlot("requestProgress(qint64,qint64)");
   175                 thisRequestFinished =
   164                 thisRequestFinished =
   176                     QDeclarativeImageBase::staticMetaObject.indexOfSlot("requestFinished()");
   165                     QDeclarativeImageBase::staticMetaObject.indexOfSlot("requestFinished()");
   177             }
   166             }
   178 
   167 
   179             QMetaObject::connect(reply, replyFinished, this,
   168             d->pix.connectFinished(this, thisRequestFinished);
   180                                  thisRequestFinished, Qt::DirectConnection);
   169             d->pix.connectDownloadProgress(this, thisRequestProgress);
   181             QMetaObject::connect(reply, replyDownloadProgress, this,
   170 
   182                                  thisRequestProgress, Qt::DirectConnection);
       
   183         } else {
   171         } else {
   184             //### should be unified with requestFinished
   172             QSize impsize = d->pix.implicitSize();
   185             if (status == QDeclarativePixmapReply::Ready) {
   173             setImplicitWidth(impsize.width());
   186                 setImplicitWidth(impsize.width());
   174             setImplicitHeight(impsize.height());
   187                 setImplicitHeight(impsize.height());
   175 
   188 
   176             if (d->pix.isReady()) {
   189                 if (d->status == Loading)
   177                 d->status = Ready;
   190                     d->status = Ready;
       
   191 
   178 
   192                 if (!d->sourcesize.isValid())
   179                 if (!d->sourcesize.isValid())
   193                     emit sourceSizeChanged();
   180                     emit sourceSizeChanged();
       
   181 
   194             } else {
   182             } else {
   195                 d->status = Error;
   183                 d->status = Error;
   196                 qmlInfo(this) << errorString;
   184                 qmlInfo(this) << d->pix.error();
   197             }
   185             }
   198             d->progress = 1.0;
   186             d->progress = 1.0;
   199             emit statusChanged(d->status);
   187             emit statusChanged(d->status);
   200             emit progressChanged(d->progress);
   188             emit progressChanged(d->progress);
   201             pixmapChange();
   189             pixmapChange();
   202             update();
   190             update();
   203         }
   191         }
       
   192 
   204     }
   193     }
   205 
   194 
   206     emit statusChanged(d->status);
   195     emit statusChanged(d->status);
   207 }
   196 }
   208 
   197 
   209 void QDeclarativeImageBase::requestFinished()
   198 void QDeclarativeImageBase::requestFinished()
   210 {
   199 {
   211     Q_D(QDeclarativeImageBase);
   200     Q_D(QDeclarativeImageBase);
   212 
   201 
   213     d->pendingPixmapCache = false;
   202     QSize impsize = d->pix.implicitSize();
   214 
   203 
   215     QSize impsize;
   204     if (d->pix.isError()) {
   216     QString errorString;
       
   217     if (QDeclarativePixmapCache::get(d->url, &d->pix, &errorString, &impsize, d->async, d->sourcesize.width(), d->sourcesize.height()) != QDeclarativePixmapReply::Ready) {
       
   218         d->status = Error;
   205         d->status = Error;
   219         qmlInfo(this) << errorString;
   206         qmlInfo(this) << d->pix.error();
   220     }
   207     }
       
   208 
   221     setImplicitWidth(impsize.width());
   209     setImplicitWidth(impsize.width());
   222     setImplicitHeight(impsize.height());
   210     setImplicitHeight(impsize.height());
   223 
   211 
   224     if (d->status == Loading)
   212     if (d->status == Loading)
   225         d->status = Ready;
   213         d->status = Ready;