on 8809451 help sign in
unity8/qml/Components/Background.qml
83 lines 2.4 kB history 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 */
17import QtQuick 2.0
18import Utils 0.1 as Utils
20Loader {
21 id: root
22 anchors.fill: parent
24 property url style
25 readonly property var parsedStyle: String(style)
26 .match(/^(color|gradient):\/\/\/(?:(#(?:[0-9a-f]{3,4}){1,2}|[a-z]{3,}))(?:\/(#(?:[0-9a-f]{3,4}){1,2}|[a-z]{3,}))?\/?$/i)
27 readonly property var luminance: {
28 if (!parsedStyle) return 0.5;
29 if (parsedStyle[1] === "color") return Utils.Style.luminance(parsedStyle[2]);
30 else if (parsedStyle[1] === "gradient") return Utils.Style.luminance(parsedStyle[2], parsedStyle[3]);
31 }
33 sourceComponent: {
34 if (style == "") return null;
35 if (!parsedStyle) return image;
36 if (parsedStyle[1] === "color") return solid
37 if (parsedStyle[1] === "gradient") return gradient
38 }
40 onLoaded: if (item.hasOwnProperty("parsedStyle")) {
41 item.parsedStyle = Qt.binding(function() { return root.parsedStyle } );
42 }
44 Component {
45 id: solid
47 Rectangle {
48 objectName: "solid"
50 property var parsedStyle
52 color: parsedStyle ? parsedStyle[2] : "#ffffff"
53 }
54 }
56 Component {
57 id: gradient
59 Rectangle {
60 objectName: "gradient"
62 property var parsedStyle
64 gradient: Gradient {
65 GradientStop { position: 0; color: parsedStyle ? parsedStyle[2] : "#000000" }
66 GradientStop { position: 1; color: parsedStyle ? parsedStyle[3] : "#000000" }
67 }
68 }
69 }
71 Component {
72 id: image
74 Image {
75 objectName: "image"
77 source: width > 0 && height > 0 && root.style
79 sourceSize.width: width
80 sourceSize.height: height
81 }
82 }