| 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 |
|
| 17 |
import QtQuick 2.4 |
| 18 |
import Lomiri.Components 1.2 |
| 19 |
|
| 20 |
Item { |
| 21 |
id: switchStyle |
| 22 |
|
| 23 |
/*! |
| 24 |
The padding between the thumb and the outside border of the switch. |
| 25 |
*/ |
| 26 |
property real thumbPadding: units.gu(0.33) |
| 27 |
|
| 28 |
/*! |
| 29 |
The padding between the icon and the border of the thumb. |
| 30 |
*/ |
| 31 |
property real iconPadding: thumbPadding |
| 32 |
|
| 33 |
implicitWidth: units.gu(6) |
| 34 |
implicitHeight: units.gu(3) |
| 35 |
opacity: styledItem.enabled ? 1.0 : 0.5 |
| 36 |
LayoutMirroring.enabled: false |
| 37 |
LayoutMirroring.childrenInherit: true |
| 38 |
|
| 39 |
/*! |
| 40 |
The background color of the switch. |
| 41 |
*/ |
| 42 |
property color backgroundColor: Theme.palette.normal.base |
| 43 |
|
| 44 |
/*! |
| 45 |
The background color of the thumb when the switch is checked. |
| 46 |
*/ |
| 47 |
property color checkedThumbColor: LomiriColors.green |
| 48 |
|
| 49 |
/*! |
| 50 |
The background color of the thumb when the switch is not checked. |
| 51 |
*/ |
| 52 |
property color uncheckedThumbColor: Qt.rgba(0, 0, 0, 0.2) |
| 53 |
|
| 54 |
/*! |
| 55 |
The foreground color of the icon that is currently selected. |
| 56 |
*/ |
| 57 |
property color selectedIconColor: Theme.palette.normal.foregroundText |
| 58 |
|
| 59 |
/*! |
| 60 |
The color of the icon that is not currently selected. |
| 61 |
*/ |
| 62 |
property color unselectedIconColor: Theme.palette.normal.backgroundText |
| 63 |
|
| 64 |
/*! |
| 65 |
The source of the selected icon when the switch is checked. |
| 66 |
*/ |
| 67 |
property url checkedIconSource: "image://theme/tick" |
| 68 |
|
| 69 |
/*! |
| 70 |
The source of the selected icon when the switch is not checked. |
| 71 |
*/ |
| 72 |
property url uncheckedIconSource: "image://theme/close" |
| 73 |
|
| 74 |
LomiriShape { |
| 75 |
id: background |
| 76 |
anchors.fill: parent |
| 77 |
color: switchStyle.backgroundColor |
| 78 |
clip: true |
| 79 |
|
| 80 |
LomiriShape { |
| 81 |
id: thumb |
| 82 |
states: [ |
| 83 |
State { |
| 84 |
name: "checked" |
| 85 |
when: styledItem.checked |
| 86 |
PropertyChanges { |
| 87 |
target: thumb |
| 88 |
x: rightThumbPosition.x |
| 89 |
color: switchStyle.checkedThumbColor |
| 90 |
} |
| 91 |
}, |
| 92 |
State { |
| 93 |
name: "unchecked" |
| 94 |
when: !styledItem.checked |
| 95 |
PropertyChanges { |
| 96 |
target: thumb |
| 97 |
x: leftThumbPosition.x |
| 98 |
color: switchStyle.uncheckedThumbColor |
| 99 |
} |
| 100 |
} |
| 101 |
] |
| 102 |
|
| 103 |
transitions: [ |
| 104 |
// Avoid animations on width changes (during initialization) |
| 105 |
// by explicitly setting from and to for the Transitions. |
| 106 |
Transition { |
| 107 |
from: "unchecked" |
| 108 |
to: "checked" |
| 109 |
LomiriNumberAnimation { |
| 110 |
target: thumb |
| 111 |
properties: "x" |
| 112 |
duration: LomiriAnimation.FastDuration |
| 113 |
easing: LomiriAnimation.StandardEasing |
| 114 |
} |
| 115 |
ColorAnimation { |
| 116 |
target: thumb |
| 117 |
properties: "color" |
| 118 |
duration: LomiriAnimation.FastDuration |
| 119 |
easing: LomiriAnimation.StandardEasing |
| 120 |
} |
| 121 |
}, |
| 122 |
Transition { |
| 123 |
from: "checked" |
| 124 |
to: "unchecked" |
| 125 |
LomiriNumberAnimation { |
| 126 |
target: thumb |
| 127 |
properties: "x" |
| 128 |
duration: LomiriAnimation.FastDuration |
| 129 |
easing: LomiriAnimation.StandardEasing |
| 130 |
} |
| 131 |
ColorAnimation { |
| 132 |
target: thumb |
| 133 |
properties: "color" |
| 134 |
duration: LomiriAnimation.FastDuration |
| 135 |
easing: LomiriAnimation.StandardEasing |
| 136 |
} |
| 137 |
} |
| 138 |
] |
| 139 |
|
| 140 |
width: (background.width - switchStyle.thumbPadding * 3.0) / 2.0 |
| 141 |
anchors { |
| 142 |
top: parent.top |
| 143 |
bottom: parent.bottom |
| 144 |
topMargin: switchStyle.thumbPadding |
| 145 |
bottomMargin: switchStyle.thumbPadding |
| 146 |
} |
| 147 |
|
| 148 |
property real iconSize: Math.min(width, height) - 2*switchStyle.iconPadding |
| 149 |
|
| 150 |
PartialColorize { |
| 151 |
anchors { |
| 152 |
verticalCenter: parent.verticalCenter |
| 153 |
right: parent.left |
| 154 |
rightMargin: switchStyle.iconPadding + switchStyle.thumbPadding |
| 155 |
} |
| 156 |
rightColor: switchStyle.unselectedIconColor |
| 157 |
source: Image { |
| 158 |
source: switchStyle.uncheckedIconSource |
| 159 |
sourceSize { |
| 160 |
width: thumb.iconSize |
| 161 |
height: thumb.iconSize |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
PartialColorize { |
| 167 |
anchors { |
| 168 |
verticalCenter: parent.verticalCenter |
| 169 |
left: parent.right |
| 170 |
leftMargin: switchStyle.iconPadding + switchStyle.thumbPadding |
| 171 |
} |
| 172 |
rightColor: switchStyle.unselectedIconColor |
| 173 |
source: Image { |
| 174 |
source: switchStyle.checkedIconSource |
| 175 |
sourceSize { |
| 176 |
width: thumb.iconSize |
| 177 |
height: thumb.iconSize |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
Item { |
| 184 |
id: leftThumbPosition |
| 185 |
anchors { |
| 186 |
left: parent.left |
| 187 |
top: parent.top |
| 188 |
leftMargin: switchStyle.thumbPadding |
| 189 |
topMargin: switchStyle.thumbPadding |
| 190 |
} |
| 191 |
height: thumb.height |
| 192 |
width: thumb.width |
| 193 |
|
| 194 |
PartialColorize { |
| 195 |
anchors.centerIn: parent |
| 196 |
source: Image { |
| 197 |
source: switchStyle.uncheckedIconSource |
| 198 |
sourceSize { |
| 199 |
width: thumb.iconSize |
| 200 |
height: thumb.iconSize |
| 201 |
} |
| 202 |
} |
| 203 |
progress: MathUtils.clamp((thumb.x - parent.x - x) / width, 0.0, 1.0) |
| 204 |
leftColor: "transparent" |
| 205 |
rightColor: switchStyle.selectedIconColor |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
Item { |
| 210 |
id: rightThumbPosition |
| 211 |
anchors { |
| 212 |
right: parent.right |
| 213 |
top: parent.top |
| 214 |
rightMargin: switchStyle.thumbPadding |
| 215 |
topMargin: switchStyle.thumbPadding |
| 216 |
} |
| 217 |
height: thumb.height |
| 218 |
width: thumb.width |
| 219 |
|
| 220 |
PartialColorize { |
| 221 |
anchors.centerIn: parent |
| 222 |
source: Image { |
| 223 |
source: switchStyle.checkedIconSource |
| 224 |
sourceSize { |
| 225 |
width: thumb.iconSize |
| 226 |
height: thumb.iconSize |
| 227 |
} |
| 228 |
} |
| 229 |
progress: MathUtils.clamp((thumb.x + thumb.width - parent.x - x) / width, 0.0, 1.0) |
| 230 |
leftColor: switchStyle.selectedIconColor |
| 231 |
rightColor: "transparent" |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
} |