Lomiri
Loading...
Searching...
No Matches
Background.qml
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
17import QtQuick 2.12
18import Lomiri.Components 1.3
19import Utils 0.1 as Utils
20
21Loader {
22 id: root
23 anchors.fill: parent
24
25 property url style
26 readonly property var parsedStyle: String(style)
27 .match(/^(color|gradient):\/\/\/(?:(#(?:[0-9a-f]{3,4}){1,2}|[a-z]{3,}))(?:\/(#(?:[0-9a-f]{3,4}){1,2}|[a-z]{3,}))?\/?$/i)
28 readonly property var luminance: {
29 if (!parsedStyle) return 0.5;
30 if (parsedStyle[1] === "color") return Utils.Style.luminance(parsedStyle[2]);
31 else if (parsedStyle[1] === "gradient") return Utils.Style.luminance(parsedStyle[2], parsedStyle[3]);
32 }
33
34 // FIXME this is only here for highlight purposes
35 readonly property color topColor: parsedStyle ? parsedStyle[2] : LomiriColors.lightGrey
36
37 sourceComponent: {
38 if (style == "") return null;
39 if (!parsedStyle) return image;
40 if (parsedStyle[1] === "color") return solid
41 if (parsedStyle[1] === "gradient") return gradient
42 }
43
44 onLoaded: if (item.hasOwnProperty("parsedStyle")) {
45 item.parsedStyle = Qt.binding(function() { return root.parsedStyle } );
46 }
47
48 Component {
49 id: solid
50
51 Rectangle {
52 objectName: "solid"
53
54 property var parsedStyle
55
56 color: parsedStyle ? parsedStyle[2] : "#ffffff"
57 }
58 }
59
60 Component {
61 id: gradient
62
63 Rectangle {
64 objectName: "gradient"
65
66 property var parsedStyle
67
68 gradient: Gradient {
69 GradientStop { position: 0; color: parsedStyle ? parsedStyle[2] : "#000000" }
70 GradientStop { position: 1; color: parsedStyle ? parsedStyle[3] : "#000000" }
71 }
72 }
73 }
74
75 Component {
76 id: image
77
78 Image {
79 objectName: "image"
80
81 source: width > 0 && height > 0 && root.style || ""
82
83 sourceSize.width: width
84 sourceSize.height: height
85 }
86 }
87}