| 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: checkBoxStyle |
| 22 |
|
| 23 |
/*! |
| 24 |
The padding between the background shape and the outside border of the checkbox. |
| 25 |
*/ |
| 26 |
property real backgroundPadding: units.gu(0.33) |
| 27 |
|
| 28 |
/*! |
| 29 |
The background color when the checkbox is not checked. |
| 30 |
*/ |
| 31 |
property color uncheckedBackgroundColor: Qt.rgba(Theme.palette.normal.foreground.r, |
| 32 |
Theme.palette.normal.foreground.g, |
| 33 |
Theme.palette.normal.foreground.b, 0.2) |
| 34 |
|
| 35 |
/*! |
| 36 |
The background color when the checkbox is checked. |
| 37 |
*/ |
| 38 |
property color checkedBackgroundColor: LomiriColors.green |
| 39 |
|
| 40 |
/*! |
| 41 |
The image to show inside the checkbox when it is checked. |
| 42 |
*/ |
| 43 |
property url iconSource: "image://theme/tick" |
| 44 |
|
| 45 |
/*! |
| 46 |
The color of the icon. |
| 47 |
*/ |
| 48 |
property color iconColor: Theme.palette.normal.foregroundText |
| 49 |
|
| 50 |
/*! |
| 51 |
The padding between the icon and the border of the thumb. |
| 52 |
*/ |
| 53 |
property real iconPadding: backgroundPadding |
| 54 |
|
| 55 |
opacity: enabled ? 1.0 : 0.5 |
| 56 |
|
| 57 |
implicitWidth: units.gu(3) |
| 58 |
implicitHeight: units.gu(3) |
| 59 |
|
| 60 |
LomiriShape { |
| 61 |
id: background |
| 62 |
anchors { |
| 63 |
fill: parent |
| 64 |
margins: checkBoxStyle.backgroundPadding |
| 65 |
} |
| 66 |
property real iconSize: Math.min(width, height) - 2*checkBoxStyle.iconPadding |
| 67 |
|
| 68 |
Icon { |
| 69 |
color: checkBoxStyle.iconColor |
| 70 |
width: background.iconSize |
| 71 |
height: background.iconSize |
| 72 |
id: tick |
| 73 |
anchors.centerIn: parent |
| 74 |
smooth: true |
| 75 |
source: checkBoxStyle.iconSource |
| 76 |
visible: styledItem.checked || transitionToChecked.running || transitionToUnchecked.running |
| 77 |
} |
| 78 |
|
| 79 |
state: styledItem.checked ? "checked" : "unchecked" |
| 80 |
states: [ |
| 81 |
State { |
| 82 |
name: "checked" |
| 83 |
PropertyChanges { |
| 84 |
target: tick |
| 85 |
anchors.verticalCenterOffset: 0 |
| 86 |
} |
| 87 |
PropertyChanges { |
| 88 |
target: background |
| 89 |
color: checkBoxStyle.checkedBackgroundColor |
| 90 |
} |
| 91 |
}, |
| 92 |
State { |
| 93 |
name: "unchecked" |
| 94 |
PropertyChanges { |
| 95 |
target: tick |
| 96 |
anchors.verticalCenterOffset: checkBoxStyle.height |
| 97 |
} |
| 98 |
PropertyChanges { |
| 99 |
target: background |
| 100 |
color: checkBoxStyle.uncheckedBackgroundColor |
| 101 |
} |
| 102 |
} |
| 103 |
] |
| 104 |
|
| 105 |
transitions: [ |
| 106 |
Transition { |
| 107 |
id: transitionToUnchecked |
| 108 |
to: "unchecked" |
| 109 |
ColorAnimation { |
| 110 |
target: background |
| 111 |
duration: LomiriAnimation.FastDuration |
| 112 |
easing: LomiriAnimation.StandardEasingReverse |
| 113 |
} |
| 114 |
SequentialAnimation { |
| 115 |
PropertyAction { |
| 116 |
target: background |
| 117 |
property: "clip" |
| 118 |
value: true |
| 119 |
} |
| 120 |
NumberAnimation { |
| 121 |
target: tick |
| 122 |
property: "anchors.verticalCenterOffset" |
| 123 |
duration: LomiriAnimation.FastDuration |
| 124 |
easing: LomiriAnimation.StandardEasingReverse |
| 125 |
} |
| 126 |
PropertyAction { |
| 127 |
target: background |
| 128 |
property: "clip" |
| 129 |
value: false |
| 130 |
} |
| 131 |
} |
| 132 |
}, |
| 133 |
Transition { |
| 134 |
id: transitionToChecked |
| 135 |
to: "checked" |
| 136 |
ColorAnimation { |
| 137 |
target: background |
| 138 |
duration: LomiriAnimation.FastDuration |
| 139 |
easing: LomiriAnimation.StandardEasing |
| 140 |
} |
| 141 |
SequentialAnimation { |
| 142 |
PropertyAction { |
| 143 |
target: background |
| 144 |
property: "clip" |
| 145 |
value: true |
| 146 |
} |
| 147 |
NumberAnimation { |
| 148 |
target: tick |
| 149 |
property: "anchors.verticalCenterOffset" |
| 150 |
duration: LomiriAnimation.FastDuration |
| 151 |
easing: LomiriAnimation.StandardEasing |
| 152 |
} |
| 153 |
PropertyAction { |
| 154 |
target: background |
| 155 |
property: "clip" |
| 156 |
value: false |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
] |
| 161 |
} |
| 162 |
} |