~ubuntu-branches/ubuntu/trusty/qtdeclarative-opensource-src/trusty-updates

« back to all changes in this revision

Viewing changes to src/quick/items/qquickimagebase.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 14:17:19 UTC
  • Revision ID: package-import@ubuntu.com-20130205141719-qqeyml8wslpyez52
Tags: upstream-5.0.1
ImportĀ upstreamĀ versionĀ 5.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the QtQml module of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:LGPL$
 
9
** Commercial License Usage
 
10
** Licensees holding valid commercial Qt licenses may use this file in
 
11
** accordance with the commercial license agreement provided with the
 
12
** Software or, alternatively, in accordance with the terms contained in
 
13
** a written agreement between you and Digia.  For licensing terms and
 
14
** conditions see http://qt.digia.com/licensing.  For further information
 
15
** use the contact form at http://qt.digia.com/contact-us.
 
16
**
 
17
** GNU Lesser General Public License Usage
 
18
** Alternatively, this file may be used under the terms of the GNU Lesser
 
19
** General Public License version 2.1 as published by the Free Software
 
20
** Foundation and appearing in the file LICENSE.LGPL included in the
 
21
** packaging of this file.  Please review the following information to
 
22
** ensure the GNU Lesser General Public License version 2.1 requirements
 
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
24
**
 
25
** In addition, as a special exception, Digia gives you certain additional
 
26
** rights.  These rights are described in the Digia Qt LGPL Exception
 
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
28
**
 
29
** GNU General Public License Usage
 
30
** Alternatively, this file may be used under the terms of the GNU
 
31
** General Public License version 3.0 as published by the Free Software
 
32
** Foundation and appearing in the file LICENSE.GPL included in the
 
33
** packaging of this file.  Please review the following information to
 
34
** ensure the GNU General Public License version 3.0 requirements will be
 
35
** met: http://www.gnu.org/copyleft/gpl.html.
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
#include "qquickimagebase_p.h"
 
43
#include "qquickimagebase_p_p.h"
 
44
 
 
45
#include <QtQml/qqmlinfo.h>
 
46
 
 
47
QT_BEGIN_NAMESPACE
 
48
 
 
49
QQuickImageBase::QQuickImageBase(QQuickItem *parent)
 
50
: QQuickImplicitSizeItem(*(new QQuickImageBasePrivate), parent)
 
51
{
 
52
    setFlag(ItemHasContents);
 
53
}
 
54
 
 
55
QQuickImageBase::QQuickImageBase(QQuickImageBasePrivate &dd, QQuickItem *parent)
 
56
: QQuickImplicitSizeItem(dd, parent)
 
57
{
 
58
    setFlag(ItemHasContents);
 
59
}
 
60
 
 
61
QQuickImageBase::~QQuickImageBase()
 
62
{
 
63
}
 
64
 
 
65
QQuickImageBase::Status QQuickImageBase::status() const
 
66
{
 
67
    Q_D(const QQuickImageBase);
 
68
    return d->status;
 
69
}
 
70
 
 
71
 
 
72
qreal QQuickImageBase::progress() const
 
73
{
 
74
    Q_D(const QQuickImageBase);
 
75
    return d->progress;
 
76
}
 
77
 
 
78
 
 
79
bool QQuickImageBase::asynchronous() const
 
80
{
 
81
    Q_D(const QQuickImageBase);
 
82
    return d->async;
 
83
}
 
84
 
 
85
void QQuickImageBase::setAsynchronous(bool async)
 
86
{
 
87
    Q_D(QQuickImageBase);
 
88
    if (d->async != async) {
 
89
        d->async = async;
 
90
        emit asynchronousChanged();
 
91
    }
 
92
}
 
93
 
 
94
QUrl QQuickImageBase::source() const
 
95
{
 
96
    Q_D(const QQuickImageBase);
 
97
    return d->url;
 
98
}
 
99
 
 
100
void QQuickImageBase::setSource(const QUrl &url)
 
101
{
 
102
    Q_D(QQuickImageBase);
 
103
 
 
104
    if (url == d->url)
 
105
        return;
 
106
 
 
107
    d->url = url;
 
108
    emit sourceChanged(d->url);
 
109
 
 
110
    if (isComponentComplete())
 
111
        load();
 
112
}
 
113
 
 
114
void QQuickImageBase::setSourceSize(const QSize& size)
 
115
{
 
116
    Q_D(QQuickImageBase);
 
117
    if (d->sourcesize == size)
 
118
        return;
 
119
 
 
120
    d->sourcesize = size;
 
121
    emit sourceSizeChanged();
 
122
    if (isComponentComplete())
 
123
        load();
 
124
}
 
125
 
 
126
QSize QQuickImageBase::sourceSize() const
 
127
{
 
128
    Q_D(const QQuickImageBase);
 
129
 
 
130
    int width = d->sourcesize.width();
 
131
    int height = d->sourcesize.height();
 
132
    return QSize(width != -1 ? width : d->pix.width(), height != -1 ? height : d->pix.height());
 
133
}
 
134
 
 
135
void QQuickImageBase::resetSourceSize()
 
136
{
 
137
    setSourceSize(QSize());
 
138
}
 
139
 
 
140
bool QQuickImageBase::cache() const
 
141
{
 
142
    Q_D(const QQuickImageBase);
 
143
    return d->cache;
 
144
}
 
145
 
 
146
void QQuickImageBase::setCache(bool cache)
 
147
{
 
148
    Q_D(QQuickImageBase);
 
149
    if (d->cache == cache)
 
150
        return;
 
151
 
 
152
    d->cache = cache;
 
153
    emit cacheChanged();
 
154
    if (isComponentComplete())
 
155
        load();
 
156
}
 
157
 
 
158
QImage QQuickImageBase::image() const
 
159
{
 
160
    Q_D(const QQuickImageBase);
 
161
    return d->pix.image();
 
162
}
 
163
 
 
164
void QQuickImageBase::setMirror(bool mirror)
 
165
{
 
166
    Q_D(QQuickImageBase);
 
167
    if (mirror == d->mirror)
 
168
        return;
 
169
 
 
170
    d->mirror = mirror;
 
171
 
 
172
    if (isComponentComplete())
 
173
        update();
 
174
 
 
175
    emit mirrorChanged();
 
176
}
 
177
 
 
178
bool QQuickImageBase::mirror() const
 
179
{
 
180
    Q_D(const QQuickImageBase);
 
181
    return d->mirror;
 
182
}
 
183
 
 
184
void QQuickImageBase::load()
 
185
{
 
186
    Q_D(QQuickImageBase);
 
187
 
 
188
    if (d->url.isEmpty()) {
 
189
        d->pix.clear(this);
 
190
        if (d->progress != 0.0) {
 
191
            d->progress = 0.0;
 
192
            emit progressChanged(d->progress);
 
193
        }
 
194
        pixmapChange();
 
195
        d->status = Null;
 
196
        emit statusChanged(d->status);
 
197
 
 
198
        if (sourceSize() != d->oldSourceSize) {
 
199
            d->oldSourceSize = sourceSize();
 
200
            emit sourceSizeChanged();
 
201
        }
 
202
        update();
 
203
 
 
204
    } else {
 
205
        QQuickPixmap::Options options;
 
206
        if (d->async)
 
207
            options |= QQuickPixmap::Asynchronous;
 
208
        if (d->cache)
 
209
            options |= QQuickPixmap::Cache;
 
210
        d->pix.load(qmlEngine(this), d->url, d->sourcesize, options);
 
211
 
 
212
        if (d->pix.isLoading()) {
 
213
            if (d->progress != 0.0) {
 
214
                d->progress = 0.0;
 
215
                emit progressChanged(d->progress);
 
216
            }
 
217
            if (d->status != Loading) {
 
218
                d->status = Loading;
 
219
                emit statusChanged(d->status);
 
220
            }
 
221
 
 
222
            static int thisRequestProgress = -1;
 
223
            static int thisRequestFinished = -1;
 
224
            if (thisRequestProgress == -1) {
 
225
                thisRequestProgress =
 
226
                    QQuickImageBase::staticMetaObject.indexOfSlot("requestProgress(qint64,qint64)");
 
227
                thisRequestFinished =
 
228
                    QQuickImageBase::staticMetaObject.indexOfSlot("requestFinished()");
 
229
            }
 
230
 
 
231
            d->pix.connectFinished(this, thisRequestFinished);
 
232
            d->pix.connectDownloadProgress(this, thisRequestProgress);
 
233
            update(); //pixmap may have invalidated texture, updatePaintNode needs to be called before the next repaint
 
234
        } else {
 
235
            requestFinished();
 
236
        }
 
237
    }
 
238
}
 
239
 
 
240
void QQuickImageBase::requestFinished()
 
241
{
 
242
    Q_D(QQuickImageBase);
 
243
 
 
244
    if (d->pix.isError()) {
 
245
        qmlInfo(this) << d->pix.error();
 
246
        d->pix.clear(this);
 
247
        d->status = Error;
 
248
        if (d->progress != 0.0) {
 
249
            d->progress = 0.0;
 
250
            emit progressChanged(d->progress);
 
251
        }
 
252
    } else {
 
253
        d->status = Ready;
 
254
        if (d->progress != 1.0) {
 
255
            d->progress = 1.0;
 
256
            emit progressChanged(d->progress);
 
257
        }
 
258
    }
 
259
    pixmapChange();
 
260
    emit statusChanged(d->status);
 
261
    if (sourceSize() != d->oldSourceSize) {
 
262
        d->oldSourceSize = sourceSize();
 
263
        emit sourceSizeChanged();
 
264
    }
 
265
    update();
 
266
}
 
267
 
 
268
void QQuickImageBase::requestProgress(qint64 received, qint64 total)
 
269
{
 
270
    Q_D(QQuickImageBase);
 
271
    if (d->status == Loading && total > 0) {
 
272
        d->progress = qreal(received)/total;
 
273
        emit progressChanged(d->progress);
 
274
    }
 
275
}
 
276
 
 
277
void QQuickImageBase::componentComplete()
 
278
{
 
279
    Q_D(QQuickImageBase);
 
280
    QQuickItem::componentComplete();
 
281
    if (d->url.isValid())
 
282
        load();
 
283
}
 
284
 
 
285
void QQuickImageBase::pixmapChange()
 
286
{
 
287
    Q_D(QQuickImageBase);
 
288
    setImplicitSize(d->pix.width(), d->pix.height());
 
289
}
 
290
 
 
291
QT_END_NAMESPACE