on xenial help sign in
Card.qml binding loops are gone. hooray \o/

Also made the aspect properties readonly

commita8baaadae3a9ef890faee66fcffc92e16c26b61e authorAlbert Astals <albert.astals@canonical.com> dateTue 1 Apr 2014, 22:56 (13 years ago) parent62557ed30065ae
1 file changed+30−12
qml/Dash/Card.qml+30−12
qml/Dash/Card.qml+30−12
@@ -73,23 +73,27 @@ AbstractButton {
7373 id: artShape
7474 radius: "medium"
7575 objectName: "artShape"
76− width: {
77− if (!visible) return 0
78− return image.fillMode === Image.PreserveAspectCrop || aspect < image.aspect ? image.width : height * image.aspect
79− }
80− height: {
81− if (!visible) return 0
82− return image.fillMode === Image.PreserveAspectCrop || aspect > image.aspect ? image.height : width / image.aspect
83− }
8476 anchors.horizontalCenter: template && template["card-layout"] === "horizontal" ? undefined : parent.horizontalCenter
8577 anchors.left: template && template["card-layout"] === "horizontal" ? parent.left : undefined
8678 visible: cardData && cardData["art"] || false
8779
88− property real aspect: components !== undefined ? components["art"]["aspect-ratio"] : 1
80+ readonly property real aspect: components !== undefined ? components["art"]["aspect-ratio"] : 1
81+ readonly property bool aspectSmallerThanImageAspect: aspect < image.aspect
82+
83+ Component.onCompleted: updateWidthHeightBindings();
84+ onAspectSmallerThanImageAspectChanged: updateWidthHeightBindings();
85+
86+ function updateWidthHeightBindings() {
87+ if (aspectSmallerThanImageAspect) {
88+ width = Qt.binding(function() { return !visible ? 0 : image.width });
89+ height = Qt.binding(function() { return !visible ? 0 : image.fillMode === Image.PreserveAspectCrop ? image.height : width / image.aspect });
90+ } else {
91+ width = Qt.binding(function() { return !visible ? 0 : image.fillMode === Image.PreserveAspectCrop ? image.width : height * image.aspect });
92+ height = Qt.binding(function() { return !visible ? 0 : image.height });
93+ }
94+ }
8995
9096 image: Image {
91− width: template && template["card-layout"] === "horizontal" ? height * artShape.aspect : root.width
92− height: template && template["card-layout"] === "horizontal" ? header.height : width / artShape.aspect
9397 objectName: "artImage"
9498 source: cardData && cardData["art"] || ""
9599 cache: true
@@ -98,7 +102,21 @@ AbstractButton {
98102 //sourceSize.height: height > width ? height : 0
99103 fillMode: components && components["art"]["fill-mode"] === "fit" ? Image.PreserveAspectFit: Image.PreserveAspectCrop
100104
101− property real aspect: implicitWidth / implicitHeight
105+ readonly property real aspect: implicitWidth / implicitHeight
106+ readonly property bool isHorizontal: template && template["card-layout"] === "horizontal"
107+
108+ Component.onCompleted: updateWidthHeightBindings();
109+ onIsHorizontalChanged: updateWidthHeightBindings();
110+
111+ function updateWidthHeightBindings() {
112+ if (isHorizontal) {
113+ width = Qt.binding(function() { return height * artShape.aspect });
114+ height = Qt.binding(function() { return header.height });
115+ } else {
116+ width = Qt.binding(function() { return root.width });
117+ height = Qt.binding(function() { return width / artShape.aspect });
118+ }
119+ }
102120 }
103121 }
104122