Unity 8
 All Classes Functions
LauncherDelegate.qml
1 /*
2  * Copyright (C) 2013 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 
17 import QtQuick 2.0
18 import Ubuntu.Components 0.1
19 
20 Item {
21  id: root
22 
23  property string iconName
24  property int count: 0
25  property bool countVisible: false
26  property int progress: -1
27  property bool itemFocused: false
28  property real maxAngle: 0
29  property bool inverted: false
30  property bool clipCorner: false
31 
32  readonly property int effectiveHeight: Math.cos(angle * Math.PI / 180) * itemHeight
33  readonly property real foldedHeight: Math.cos(maxAngle * Math.PI / 180) * itemHeight
34 
35  property int itemWidth
36  property int itemHeight
37  // The angle used for rotating
38  property real angle: 0
39  // This is the offset that keeps the items inside the panel
40  property real offset: 0
41  property real itemOpacity: 1
42  property real brightness: 0
43 
44  Item {
45  id: iconItem
46  width: parent.itemWidth + units.gu(1)
47  height: parent.itemHeight + units.gu(1)
48  anchors.centerIn: parent
49 
50  UbuntuShape {
51  id: iconShape
52  anchors.fill: parent
53  anchors.margins: units.gu(1)
54  radius: "medium"
55  borderSource: "none"
56 
57  image: Image {
58  id: iconImage
59  sourceSize.width: iconShape.width
60  sourceSize.height: iconShape.height
61  fillMode: Image.PreserveAspectCrop
62  source: root.iconName
63  }
64  }
65 
66  BorderImage {
67  id: itemGlow
68  anchors.centerIn: iconItem
69  source: "graphics/icon-top-highlight.png"
70  width: root.itemWidth - units.gu(1)
71  height: root.itemHeight - units.gu(1)
72  }
73 
74  UbuntuShape {
75  objectName: "countEmblem"
76  anchors {
77  right: parent.right
78  top: parent.top
79  margins: units.dp(3)
80  }
81  width: Math.min(root.itemWidth, Math.max(units.gu(2), countLabel.implicitWidth + units.gu(1)))
82  height: units.gu(2)
83  color: UbuntuColors.orange
84  visible: root.countVisible
85  borderSource: "none"
86 
87  Label {
88  id: countLabel
89  objectName: "countLabel"
90  text: root.count
91  anchors.centerIn: parent
92  // FIXME: verticalCenter seems to be off wee bit and QML doesn't have a centerLine
93  // property for Text: https://bugreports.qt-project.org/browse/QTBUG-40479
94  anchors.verticalCenterOffset: -units.dp(.5)
95  width: root.itemWidth - units.gu(1)
96  horizontalAlignment: Text.AlignHCenter
97  elide: Text.ElideRight
98  color: "white"
99  fontSize: "x-small"
100  }
101  }
102 
103  BorderImage {
104  id: progressOverlay
105  objectName: "progressOverlay"
106  anchors {
107  left: iconItem.left
108  right: iconItem.right
109  bottom: iconItem.bottom
110  leftMargin: units.gu(1)
111  rightMargin: units.gu(1)
112  bottomMargin: units.gu(1)
113  }
114  height: units.gu(1.5)
115  visible: root.progress > -1
116  source: "graphics/progressbar-trough.sci"
117 
118  // For fill calculation we need to remove the 2 units of border defined in .sci file
119  property int adjustedWidth: width - units.gu(2)
120 
121  Item {
122  anchors {
123  left: parent.left
124  top: parent.top
125  bottom: parent.bottom
126  }
127  width: Math.min(100, root.progress) / 100 * parent.adjustedWidth + units.gu(1)
128  clip: true
129 
130  BorderImage {
131  anchors {
132  left: parent.left
133  top: parent.top
134  bottom: parent.bottom
135  }
136  width: progressOverlay.width
137  source: "graphics/progressbar-fill.sci"
138  }
139  }
140  }
141  Image {
142  objectName: "focusedHighlight"
143  anchors {
144  right: parent.right
145  verticalCenter: parent.verticalCenter
146  }
147  visible: root.itemFocused
148  source: "graphics/focused_app_arrow.png"
149  }
150  }
151 
152  Item {
153  id: clipper
154  anchors.centerIn: parent
155  width: iconItem.width
156  height: iconItem.height
157  Rectangle {
158  anchors {
159  fill: parent
160  topMargin: -units.gu(2)
161  leftMargin: -units.gu(2)
162  rightMargin: -units.gu(2)
163  bottomMargin: units.gu(1.2)
164  }
165  color: "red"
166  rotation: root.rotation + 45
167  }
168  }
169 
170  ShaderEffect {
171  id: transformEffect
172  anchors.centerIn: parent
173  anchors.verticalCenterOffset: root.offset
174  width: iconItem.width
175  height: iconItem.height
176  property real itemOpacity: root.itemOpacity
177  property real brightness: Math.max(-1, root.brightness)
178  property real angle: root.angle
179  property bool clipCorner: root.clipCorner
180  rotation: root.inverted ? 180 : 0
181 
182  property variant source: ShaderEffectSource {
183  id: shaderEffectSource
184  sourceItem: iconItem
185  hideSource: true
186  }
187 
188  property var mask: ShaderEffectSource {
189  sourceItem: clipper
190  hideSource: true
191  }
192 
193  transform: [
194  // Rotating 3 times at top/bottom because that increases the perspective.
195  // This is a hack, but as QML does not support real 3D coordinates
196  // getting a higher perspective can only be done by a hack. This is the most
197  // readable/understandable one I could come up with.
198  Rotation {
199  axis { x: 1; y: 0; z: 0 }
200  origin { x: iconItem.width / 2; y: angle > 0 ? 0 : iconItem.height; z: 0 }
201  angle: root.angle * 0.7
202  },
203  Rotation {
204  axis { x: 1; y: 0; z: 0 }
205  origin { x: iconItem.width / 2; y: angle > 0 ? 0 : iconItem.height; z: 0 }
206  angle: root.angle * 0.7
207  },
208  Rotation {
209  axis { x: 1; y: 0; z: 0 }
210  origin { x: iconItem.width / 2; y: angle > 0 ? 0 : iconItem.height; z: 0 }
211  angle: root.angle * 0.7
212  },
213  // Because rotating it 3 times moves it more to the front/back, i.e. it gets
214  // bigger/smaller and we need a scale to compensate that again.
215  Scale {
216  xScale: 1 - (Math.abs(angle) / 500)
217  yScale: 1 - (Math.abs(angle) / 500)
218  origin { x: iconItem.width / 2; y: iconItem.height / 2}
219  }
220  ]
221 
222  // Using a fragment shader instead of QML's opacity and BrightnessContrast
223  // to be able to do both in one step which gives quite some better performance
224  fragmentShader: "
225  varying highp vec2 qt_TexCoord0;
226  uniform sampler2D source;
227  uniform sampler2D mask;
228  uniform lowp float brightness;
229  uniform lowp float itemOpacity;
230  uniform bool clipCorner;
231  void main(void)
232  {
233  highp vec4 sourceColor = texture2D(source, qt_TexCoord0);
234  highp vec4 maskColor = texture2D(mask, qt_TexCoord0);
235  sourceColor.rgb = mix(sourceColor.rgb, vec3(step(0.0, brightness)), abs(brightness));
236  sourceColor *= itemOpacity;
237  if (clipCorner) {
238  sourceColor *= maskColor.a;
239  }
240  gl_FragColor = sourceColor;
241  }"
242  }
243 }