on xenial help sign in
Introduce "scaleTo" property instead of abusing sourceSize.
16545fdMichał Sawicz
Components/LazyImage.qml+9−15
@@ -24,11 +24,13 @@ Item {
24 id: root
24 id: root
25
25
26 property url source
26 property url source
27+ // TODO convert into enums when available in QML
28+ property string scaleTo
27
29
28− property real initialWidth: sourceSize.width > -1 ? sourceSize.width : units.gu(10)
30+ property real initialWidth: units.gu(10)
29− property real initialHeight: sourceSize.height > -1 ? sourceSize.height : units.gu(10)
31+ property real initialHeight: units.gu(10)
30− property size sourceSize
31
32
33+ property alias sourceSize: image.sourceSize
32 property alias fillMode: image.fillMode
34 property alias fillMode: image.fillMode
33 property alias asynchronous: image.asynchronous
35 property alias asynchronous: image.asynchronous
34 property alias cache: image.cache
36 property alias cache: image.cache
@@ -87,23 +89,15 @@ Item {
87
89
88 property url nextSource
90 property url nextSource
89
91
90− width: {
91− if (root.sourceSize.width > -1) return root.sourceSize.width;
92− if (root.sourceSize.height > -1) return implicitWidth * root.sourceSize.height / implicitHeight
92+ width: root.scaleTo == "height" ? implicitWidth * root.height / implicitHeight : implicitWidth
93− return implicitWidth
93+ height: root.scaleTo == "width" ? implicitHeight * root.width / implicitWidth : implicitHeight
94− }
95− height: {
96− if (root.sourceSize.height > -1) return root.sourceSize.height;
97− if (root.sourceSize.width > -1) return implicitHeight* root.sourceSize.width / implicitWidth
98− return implicitHeight
99− }
100 fillMode: Image.PreserveAspectCrop
94 fillMode: Image.PreserveAspectCrop
101 asynchronous: true
95 asynchronous: true
102 cache: false
96 cache: false
103 horizontalAlignment: Image.AlignHCenter
97 horizontalAlignment: Image.AlignHCenter
104 verticalAlignment: Image.AlignVCenter
98 verticalAlignment: Image.AlignVCenter
105− sourceSize.width: root.sourceSize.width
99+ sourceSize.width: root.scaleTo == "width" ? root.width : 0
106− sourceSize.height: root.sourceSize.height
100+ sourceSize.height: root.scaleTo == "height" ? root.height : 0
107 }
101 }
108 }
102 }
109
103
tests/qmltests/Components/tst_LazyImage.qml+4−2
@@ -57,7 +57,8 @@ Rectangle {
57
57
58 LazyImage {
58 LazyImage {
59 id: lazy2
59 id: lazy2
60− sourceSize.width: units.gu(30)
60+ width: units.gu(30)
61+ scaleTo: "width"
61 }
62 }
62
63
63 Label {
64 Label {
@@ -69,7 +70,8 @@ Rectangle {
69
70
70 LazyImage {
71 LazyImage {
71 id: lazy3
72 id: lazy3
72− sourceSize.height: units.gu(10)
73+ height: units.gu(10)
74+ scaleTo: "height"
73 }
75 }
74 }
76 }
75 }
77 }