Debian Codegraph @

src/imports/Components/1.3/Icon.qml

raw
1 /*
2 * Copyright (C) 2015 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 * Authors: Zsombor Egri <[email protected]>
17 * Loic Molinari <[email protected]>
18 */
19
20 import QtQuick 2.4
21
22 /*!
23 \qmltype Icon
24 \inqmlmodule Lomiri.Components
25 \inherits Item
26 \ingroup lomiri
27 \brief The Icon component displays an icon from the icon theme.
28
29 The icon theme contains a set of standard icons referred to by their name.
30 Using icons whenever possible enhances consistency across applications.
31 Each icon has a name and can have different visual representations depending
32 on the size requested.
33
34 Icons can also be colorized. Setting the \l color property will make all pixels
35 with the \l keyColor (by default #808080) colored.
36
37 Example:
38 \qml
39 Icon {
40 width: 64
41 height: 64
42 name: "toolkit_input-search"
43 }
44 \endqml
45
46 Example of colorization:
47 \qml
48 Icon {
49 width: 64
50 height: 64
51 name: "toolkit_input-search"
52 color: LomiriColors.warmGrey
53 }
54 \endqml
55
56 Icon themes are created following the
57 \l{http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html}{Freedesktop Icon Theme Specification}.
58 */
59
60 Item {
61 id: icon
62
63 /*!
64 The name of the icon to display.
65 \qmlproperty string Icon::name
66
67 If both name and source are set, name will be ignored.
68
69 \note The complete list of icons available in Lomiri is not published yet.
70 For now please refer to the folders where the icon themes are installed:
71 \list
72 \li Lomiri Touch: \l file:/usr/share/icons/suru
73 \li Lomiri Desktop: \l file:/usr/share/icons/lomiri-mono-dark
74 \endlist
75 These 2 separate icon themes will be merged soon.
76 */
77 property string name
78
79 /*!
80 The color that all pixels that originally are of color \l keyColor should take.
81 \qmlproperty color Icon::color
82 */
83
84 property alias color: colorizedImage.keyColorOut
85
86 /*!
87 The color of the pixels that should be colorized.
88 By default it is set to #808080.
89 \qmlproperty color Icon::keyColor
90 */
91 property alias keyColor: colorizedImage.keyColorIn
92
93 /*!
94 The source url of the icon to display. It has precedence over name.
95
96 If both name and source are set, name will be ignored.
97
98 \since Lomiri.Components 1.1
99 \qmlproperty url Icon::source
100 */
101
102 property url source: ""
103
104 /*!
105 \qmlproperty bool Icon::asynchronous
106 The property drives the image loading of the icon. Defaults to false.
107 */
108 property alias asynchronous: image.asynchronous
109
110 implicitWidth: image.implicitWidth
111 implicitHeight: image.implicitHeight
112
113 Component.onCompleted: image.completed = true
114
115 Image {
116 id: image
117 objectName: "image"
118 anchors.fill: parent
119 fillMode: Image.PreserveAspectFit
120
121 sourceSize {
122 width: icon.width
123 height: icon.height
124 }
125
126 property bool completed: false
127 source: icon.source.toString() !== "" ? icon.source : (completed && icon.name ? "image://theme/%1".arg(icon.name) : "")
128
129 cache: true
130 visible: !colorizedImage.visible
131 asynchronous: false
132 }
133
134 ShaderEffect {
135 id: colorizedImage
136 objectName: "shader"
137
138 anchors.fill: parent
139
140 // Whether or not a color has been set.
141 visible: image.status == Image.Ready && keyColorOut != Qt.rgba(0.0, 0.0, 0.0, 0.0)
142
143 property Image source: image
144 property color keyColorOut: Qt.rgba(0.0, 0.0, 0.0, 0.0)
145 property color keyColorIn: "#808080"
146 property real threshold: 0.1
147
148 fragmentShader: "
149 varying highp vec2 qt_TexCoord0;
150 uniform sampler2D source;
151 uniform highp vec4 keyColorOut;
152 uniform highp vec4 keyColorIn;
153 uniform lowp float threshold;
154 uniform lowp float qt_Opacity;
155 void main() {
156 lowp vec4 sourceColor = texture2D(source, qt_TexCoord0);
157 gl_FragColor = mix(keyColorOut * vec4(sourceColor.a), sourceColor, step(threshold, distance(sourceColor.rgb / sourceColor.a, keyColorIn.rgb))) * qt_Opacity;
158 }"
159 }
160 }