| 1 |
/* |
| 2 |
* Copyright 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 Lesser 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 Lesser General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU Lesser General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
* |
| 16 |
* Author: Florian Boucault <[email protected]> |
| 17 |
*/ |
| 18 |
|
| 19 |
import QtQuick 2.4 |
| 20 |
import Lomiri.Components 1.2 |
| 21 |
|
| 22 |
Item { |
| 23 |
id: buttonStyle |
| 24 |
|
| 25 |
property var button: styledItem |
| 26 |
property real minimumWidth: units.gu(10) |
| 27 |
property real horizontalPadding: units.gu(1) |
| 28 |
// FIXME: Add this color to the palette |
| 29 |
property color defaultColor: "#b2b2b2" |
| 30 |
property font defaultFont: Qt.font({family: "Ubuntu", pixelSize: FontUtils.sizeToPixels("medium")}) |
| 31 |
property Gradient defaultGradient |
| 32 |
property real buttonFaceOffset: 0 |
| 33 |
property bool stroke: button.hasOwnProperty("strokeColor") && button.strokeColor != Qt.rgba(0.0, 0.0, 0.0, 0.0) |
| 34 |
/*! |
| 35 |
The property overrides the button's default background with an item. This |
| 36 |
item can be used by derived styles to reuse the ButtonStyle and override |
| 37 |
the default coloured background with an image or any other drawing. |
| 38 |
The default value is null. |
| 39 |
*/ |
| 40 |
property Item backgroundSource: null |
| 41 |
|
| 42 |
width: button.width |
| 43 |
height: button.height |
| 44 |
implicitWidth: Math.max(minimumWidth, foreground.implicitWidth + 2*horizontalPadding) |
| 45 |
implicitHeight: units.gu(4) |
| 46 |
|
| 47 |
LayoutMirroring.enabled: Qt.application.layoutDirection == Qt.RightToLeft |
| 48 |
LayoutMirroring.childrenInherit: true |
| 49 |
|
| 50 |
/*! \internal */ |
| 51 |
// Color properties in a JS ternary operator don't work as expected in |
| 52 |
// QML because it overwrites alpha values with 1. A workaround is to use |
| 53 |
// Qt.rgba(). For more information, see |
| 54 |
// https://bugs.launchpad.net/lomiri-ui-toolkit/+bug/1197802 and |
| 55 |
// https://bugreports.qt-project.org/browse/QTBUG-32238. |
| 56 |
function __colorHack(color) { return Qt.rgba(color.r, color.g, color.b, color.a); } |
| 57 |
|
| 58 |
|
| 59 |
/* The proxy is necessary because Gradient.stops and GradientStop.color are |
| 60 |
non-NOTIFYable properties. They cannot be written to so it is fine but |
| 61 |
the proxy avoids the warnings. |
| 62 |
*/ |
| 63 |
property QtObject gradientProxy: gradientProxyObject |
| 64 |
QtObject { |
| 65 |
id: gradientProxyObject |
| 66 |
property color topColor |
| 67 |
property color bottomColor |
| 68 |
|
| 69 |
function updateGradient() { |
| 70 |
if (button.gradient) { |
| 71 |
topColor = button.gradient.stops[0].color; |
| 72 |
bottomColor = button.gradient.stops[1].color; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
Component.onCompleted: { |
| 77 |
updateGradient(); |
| 78 |
button.gradientChanged.connect(updateGradient); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
// Use the gradient if it is defined and the color has not been set manually |
| 83 |
// or the gradient has been set manually |
| 84 |
property bool isGradient: button.gradient && (button.color == defaultColor || |
| 85 |
button.gradient != defaultGradient) |
| 86 |
|
| 87 |
Image { |
| 88 |
id: strokeBorder |
| 89 |
anchors.fill: parent |
| 90 |
anchors.margins: -units.gu(0.5) |
| 91 |
// FIXME: this PNG is way too big (462x108) and do not scale properly |
| 92 |
// ie. the corners are visually incorrect at most sizes |
| 93 |
source: stroke ? Qt.resolvedUrl("../artwork/stroke_button.png") : "" |
| 94 |
visible: false |
| 95 |
cache: false |
| 96 |
asynchronous: true |
| 97 |
} |
| 98 |
|
| 99 |
ShaderEffect { |
| 100 |
id: colorizedImage |
| 101 |
|
| 102 |
anchors.fill: parent |
| 103 |
visible: stroke && strokeBorder.status == Image.Ready |
| 104 |
|
| 105 |
property Item source: visible ? strokeBorder : null |
| 106 |
property color keyColorOut: stroke ? strokeColor : Qt.rgba(0.0, 0.0, 0.0, 0.0) |
| 107 |
property color keyColorIn: Qt.rgba(1.0, 1.0, 1.0, 1.0) |
| 108 |
property real threshold: 1.0 |
| 109 |
|
| 110 |
fragmentShader: " |
| 111 |
varying highp vec2 qt_TexCoord0; |
| 112 |
uniform sampler2D source; |
| 113 |
uniform highp vec4 keyColorOut; |
| 114 |
uniform highp vec4 keyColorIn; |
| 115 |
uniform lowp float threshold; |
| 116 |
uniform lowp float qt_Opacity; |
| 117 |
void main() { |
| 118 |
lowp vec4 sourceColor = texture2D(source, qt_TexCoord0); |
| 119 |
gl_FragColor = mix(vec4(keyColorOut.rgb, 1.0) * sourceColor.a, sourceColor, step(threshold, distance(sourceColor.rgb / sourceColor.a, keyColorIn.rgb))) * qt_Opacity; |
| 120 |
}" |
| 121 |
} |
| 122 |
|
| 123 |
LomiriShape { |
| 124 |
id: background |
| 125 |
anchors.fill: parent |
| 126 |
borderSource: "radius_idle.sci" |
| 127 |
visible: stroke ? false : ((color.a != 0.0) || backgroundSource) |
| 128 |
image: backgroundSource |
| 129 |
|
| 130 |
color: stroke ? "" : (backgroundSource ? "#00000000" : (isGradient ? __colorHack(gradientProxy.topColor) : __colorHack(button.color))) |
| 131 |
opacity: styledItem.enabled ? 1.0 : 0.6 |
| 132 |
gradientColor: stroke ? "" : (backgroundSource ? "#00000000" : (isGradient ? __colorHack(gradientProxy.bottomColor) : __colorHack(button.color))) |
| 133 |
} |
| 134 |
|
| 135 |
LomiriShape { |
| 136 |
id: backgroundPressed |
| 137 |
anchors.fill: parent |
| 138 |
color: stroke ? strokeColor : background.color |
| 139 |
gradientColor: stroke ? strokeColor : background.gradientColor |
| 140 |
borderSource: "radius_pressed.sci" |
| 141 |
opacity: button.pressed ? 1.0 : 0.0 |
| 142 |
Behavior on opacity { |
| 143 |
NumberAnimation { |
| 144 |
duration: LomiriAnimation.SnapDuration |
| 145 |
easing.type: Easing.Linear |
| 146 |
} |
| 147 |
} |
| 148 |
visible: stroke || background.visible |
| 149 |
} |
| 150 |
|
| 151 |
ButtonForeground { |
| 152 |
id: foreground |
| 153 |
width: parent.width - 2*horizontalPadding |
| 154 |
anchors { |
| 155 |
centerIn: parent |
| 156 |
horizontalCenterOffset: buttonFaceOffset |
| 157 |
} |
| 158 |
text: button.text |
| 159 |
/* Pick either a clear or dark text color depending on the luminance of the |
| 160 |
background color to maintain good contrast (works in most cases) |
| 161 |
*/ |
| 162 |
textColor: ColorUtils.luminance(button.color) <= 0.85 && !(stroke && !pressed) ? "#FFFFFF" : "#888888" |
| 163 |
iconSource: button.iconSource |
| 164 |
iconPosition: button.iconPosition |
| 165 |
iconSize: units.gu(3) |
| 166 |
font: button.font |
| 167 |
spacing: horizontalPadding |
| 168 |
transformOrigin: Item.Top |
| 169 |
scale: button.pressed ? 0.98 : 1.0 |
| 170 |
Behavior on scale { |
| 171 |
NumberAnimation { |
| 172 |
duration: LomiriAnimation.SnapDuration |
| 173 |
easing.type: Easing.Linear |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
} |