Debian Codegraph @

src/imports/Components/1.2/CrossFadeImage10.qml

raw
1 /*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 import QtQuick 2.4
18 // FIXME: When a module contains QML, C++ and JavaScript elements exported,
19 // we need to use named imports otherwise namespace collision is reported
20 // by the QML engine. As workaround, we use Lomiri named import.
21 // Bug to watch: https://bugreports.qt-project.org/browse/QTBUG-27645
22 import Lomiri.Components 1.2 as Lomiri
23
24 /*!
25 \internal
26 Documentation is in CrossFadeImage.qdoc
27 */
28 Item {
29 id: crossFadeImage
30
31 property url source
32
33 property int fillMode : Image.PreserveAspectFit
34
35 property int fadeDuration: Lomiri.LomiriAnimation.FastDuration
36
37 // FIXME: Support resetting sourceSize
38 property size sourceSize: internals.loadingImage ? Qt.size(internals.loadingImage.sourceSize.width, internals.loadingImage.sourceSize.height) : Qt.size(0, 0)
39
40 readonly property bool running: nextImageFadeIn.running
41
42 readonly property int status: internals.loadingImage ? internals.loadingImage.status : Image.Null
43
44 Binding {
45 target: crossFadeImage
46 property: "sourceSize"
47 value: internals.loadingImage ? Qt.size(internals.loadingImage.sourceSize.width, internals.loadingImage.sourceSize.height) : Qt.size(0, 0)
48 when: internals.forcedSourceSize === undefined
49 }
50
51 /*!
52 \internal
53 */
54 onSourceSizeChanged: {
55 if (internals.loadingImage && (sourceSize != Qt.size(internals.loadingImage.sourceSize.width, internals.loadingImage.sourceSize.height))) {
56 internals.forcedSourceSize = sourceSize;
57 }
58 }
59
60 QtObject {
61 id: internals
62
63 /*! \internal
64 Source size specified by the setting crossFadeImage.sourceSize.
65 */
66 property size forcedSourceSize
67
68 /*! \internal
69 Defines the image currently being shown
70 */
71 property Image currentImage: image1
72
73 /*! \internal
74 Defines the image being changed to
75 */
76 property Image nextImage: image2
77
78 property Image loadingImage: currentImage
79
80 function swapImages() {
81 internals.currentImage.z = 0;
82 internals.nextImage.z = 1;
83 nextImageFadeIn.start();
84
85 var tmpImage = internals.currentImage;
86 internals.currentImage = internals.nextImage;
87 internals.nextImage = tmpImage;
88 }
89 }
90
91 Image {
92 id: image1
93 anchors.fill: parent
94 cache: false
95 asynchronous: true
96 fillMode: parent.fillMode
97 z: 1
98 Binding {
99 target: image1
100 property: "sourceSize"
101 value: internals.forcedSourceSize
102 when: internals.forcedSourceSize !== undefined
103 }
104 }
105
106 Image {
107 id: image2
108 anchors.fill: parent
109 cache: false
110 asynchronous: true
111 fillMode: parent.fillMode
112 z: 0
113 Binding {
114 target: image2
115 property: "sourceSize"
116 value: internals.forcedSourceSize
117 when: internals.forcedSourceSize !== undefined
118 }
119 }
120
121 /*!
122 \internal
123 Do the fading when the source is updated
124 */
125 onSourceChanged: {
126 // On creation, the souce handler is called before image pointers are set.
127 if (internals.currentImage === null) {
128 internals.currentImage = image1;
129 internals.nextImage = image2;
130 }
131
132 nextImageFadeIn.stop();
133
134 // Don't fade in initial picture, only fade changes
135 if (internals.currentImage.source == "") {
136 internals.currentImage.source = source;
137 internals.loadingImage = internals.currentImage;
138 } else {
139 nextImageFadeIn.stop();
140 internals.nextImage.opacity = 0.0;
141 internals.nextImage.source = source;
142 internals.loadingImage = internals.nextImage;
143
144 // If case the image is still in QML's cache, status will be "Ready" immediately
145 if (internals.nextImage.status === Image.Ready || internals.nextImage.source === "") {
146 internals.swapImages();
147 }
148 }
149 }
150
151 Connections {
152 target: internals.nextImage
153 function onStatusChanged() {
154 if (internals.nextImage.status == Image.Ready) {
155 internals.swapImages();
156 }
157 }
158 }
159
160 LomiriNumberAnimation {
161 id: nextImageFadeIn
162 target: internals.nextImage
163 property: "opacity"
164 to: 1.0
165 duration: crossFadeImage.fadeDuration
166
167 onRunningChanged: {
168 if (!running) {
169 internals.nextImage.source = "";
170 }
171 }
172 }
173 }