Debian Codegraph @

src/imports/Components/1.3/CrossFadeImage.qml

raw
1 /*
2 * Copyright (C) 2014 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.3 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 property string fadeStyle: "overlay"
41
42 readonly property bool running: nextImageFadeIn.running
43
44 readonly property int status: internals.loadingImage ? internals.loadingImage.status : Image.Null
45
46 Binding {
47 target: crossFadeImage
48 property: "sourceSize"
49 value: internals.loadingImage ? Qt.size(internals.loadingImage.sourceSize.width, internals.loadingImage.sourceSize.height) : Qt.size(0, 0)
50 when: internals.forcedSourceSize === undefined
51 }
52
53 /*!
54 \internal
55 */
56 onSourceSizeChanged: {
57 if (internals.loadingImage && (sourceSize != Qt.size(internals.loadingImage.sourceSize.width, internals.loadingImage.sourceSize.height))) {
58 internals.forcedSourceSize = sourceSize;
59 }
60 }
61
62 QtObject {
63 id: internals
64
65 /*! \internal
66 Source size specified by the setting crossFadeImage.sourceSize.
67 */
68 property size forcedSourceSize
69
70 /*! \internal
71 Defines the image currently being shown
72 */
73 property Image currentImage: image1
74
75 /*! \internal
76 Defines the image being changed to
77 */
78 property Image nextImage: image2
79
80 property Image loadingImage: currentImage
81
82 function swapImages() {
83 internals.currentImage.z = 0;
84 internals.nextImage.z = 1;
85 nextImageFadeIn.start();
86
87 var tmpImage = internals.currentImage;
88 internals.currentImage = internals.nextImage;
89 internals.nextImage = tmpImage;
90 }
91 }
92
93 QtObject {
94 // dummy object used to disable crossfade animation
95 id: fadeOutDummy
96 property real opacity
97 }
98
99 Image {
100 id: image1
101 anchors.fill: parent
102 cache: false
103 asynchronous: true
104 fillMode: parent.fillMode
105 z: 1
106 Binding {
107 target: image1
108 property: "sourceSize"
109 value: internals.forcedSourceSize
110 when: internals.forcedSourceSize !== undefined
111 }
112 }
113
114 Image {
115 id: image2
116 anchors.fill: parent
117 cache: false
118 asynchronous: true
119 fillMode: parent.fillMode
120 z: 0
121 Binding {
122 target: image2
123 property: "sourceSize"
124 value: internals.forcedSourceSize
125 when: internals.forcedSourceSize !== undefined
126 }
127 }
128
129 /*!
130 \internal
131 Do the fading when the source is updated
132 */
133 onSourceChanged: {
134 // On creation, the souce handler is called before image pointers are set.
135 if (internals.currentImage === null) {
136 internals.currentImage = image1;
137 internals.nextImage = image2;
138 }
139
140 nextImageFadeIn.stop();
141
142 // Don't fade in initial picture, only fade changes
143 if (internals.currentImage.source == "") {
144 internals.currentImage.source = source;
145 internals.loadingImage = internals.currentImage;
146 } else {
147 nextImageFadeIn.stop();
148 internals.nextImage.opacity = 0.0;
149 internals.nextImage.source = source;
150 internals.loadingImage = internals.nextImage;
151
152 // If case the image is still in QML's cache, status will be "Ready" immediately
153 if (internals.nextImage.status === Image.Ready || internals.nextImage.source === "") {
154 internals.swapImages();
155 }
156 }
157 }
158
159 Connections {
160 target: internals.nextImage
161 function onStatusChanged() {
162 if (internals.nextImage.status == Image.Ready) {
163 internals.swapImages();
164 }
165 }
166 }
167
168 ParallelAnimation {
169 id: nextImageFadeIn
170
171 Lomiri.LomiriNumberAnimation {
172 id: currentImageFadeOut
173 target: fadeStyle == "cross" ? internals.currentImage : fadeOutDummy
174 property: "opacity"
175 to: 0.0
176 duration: crossFadeImage.fadeDuration
177 }
178
179 Lomiri.LomiriNumberAnimation {
180 target: internals.nextImage
181 property: "opacity"
182 to: 1.0
183 duration: crossFadeImage.fadeDuration
184 }
185
186 onRunningChanged: {
187 if (!running) {
188 internals.nextImage.source = "";
189 }
190 }
191 }
192 }