| 1 |
/* |
| 2 |
* Copyright 2014 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 |
| 19 |
import Lomiri.Components.Styles 2.0 as Style |
| 20 |
|
| 21 |
Style.ComboButtonStyle { |
| 22 |
id: comboStyle |
| 23 |
|
| 24 |
// configurations |
| 25 |
dropDownWidth: units.gu(5) |
| 26 |
comboListMargin: units.gu(0.8) |
| 27 |
comboListHolder: comboListContent |
| 28 |
comboListPanel: panelItem |
| 29 |
defaultColor: mainButton.defaultColor |
| 30 |
defaultGradient: mainButton.defaultGradient |
| 31 |
defaultDropdownColor: mainButton.defaultColor |
| 32 |
defaultFont: mainButton.defaultFont |
| 33 |
|
| 34 |
width: styledItem.width |
| 35 |
height: styledItem.collapsedHeight |
| 36 |
|
| 37 |
implicitWidth: mainButton.implicitWidth |
| 38 |
implicitHeight: mainButton.implicitHeight |
| 39 |
|
| 40 |
LayoutMirroring.enabled: Qt.application.layoutDirection === Qt.RightToLeft |
| 41 |
LayoutMirroring.childrenInherit: true |
| 42 |
|
| 43 |
Item { |
| 44 |
id: mainButton |
| 45 |
|
| 46 |
property real minimumWidth: units.gu(36) |
| 47 |
property real horizontalPadding: units.gu(4) |
| 48 |
property color defaultColor: theme.palette.normal.foreground |
| 49 |
property font defaultFont: Qt.font({family: "Ubuntu", pixelSize: FontUtils.sizeToPixels("medium")}) |
| 50 |
property Gradient defaultGradient: null |
| 51 |
property real buttonFaceOffset: -dropDownWidth/2 |
| 52 |
property bool stroke: false |
| 53 |
|
| 54 |
/*! |
| 55 |
The property overrides the button's default background with an item. This |
| 56 |
item can be used by derived styles to reuse the ButtonStyle and override |
| 57 |
the default coloured background with an image or any other drawing. |
| 58 |
The default value is null. |
| 59 |
*/ |
| 60 |
property Item backgroundSource: comboFace |
| 61 |
|
| 62 |
anchors { |
| 63 |
left: parent.left |
| 64 |
top: parent.top |
| 65 |
right: parent.right |
| 66 |
} |
| 67 |
width: styledItem.width |
| 68 |
height: styledItem.collapsedHeight |
| 69 |
implicitWidth: Math.max( |
| 70 |
minimumWidth, foreground.implicitWidth + 2 * mainButton.horizontalPadding |
| 71 |
) |
| 72 |
implicitHeight: units.gu(4) |
| 73 |
|
| 74 |
LayoutMirroring.enabled: Qt.application.layoutDirection === Qt.RightToLeft |
| 75 |
LayoutMirroring.childrenInherit: true |
| 76 |
|
| 77 |
LomiriShape { |
| 78 |
id: background |
| 79 |
anchors.fill: parent |
| 80 |
borderSource: "radius_idle.sci" // Deprecated, use a dedicated shape. |
| 81 |
visible: mainButton.stroke ? false : ((backgroundColor.a !== 0.0) || mainButton.backgroundSource) |
| 82 |
source: mainButton.backgroundSource |
| 83 |
|
| 84 |
backgroundColor: defaultColor |
| 85 |
backgroundMode: LomiriShape.SolidColor |
| 86 |
opacity: styledItem.enabled ? 1.0 : 0.6 |
| 87 |
} |
| 88 |
|
| 89 |
Item { |
| 90 |
id: foreground |
| 91 |
|
| 92 |
property string iconPosition: styledItem.iconPosition |
| 93 |
property real iconSize: units.gu(2) |
| 94 |
property real spacing: mainButton.horizontalPadding |
| 95 |
property bool hasIcon: styledItem.iconSource.toString() !== "" |
| 96 |
property bool hasText: styledItem.text !== "" |
| 97 |
|
| 98 |
anchors { |
| 99 |
centerIn: parent |
| 100 |
horizontalCenterOffset: mainButton.buttonFaceOffset |
| 101 |
} |
| 102 |
transformOrigin: Item.Top |
| 103 |
width: parent.width - 2*mainButton.horizontalPadding |
| 104 |
implicitHeight: Math.max(foregroundIcon.height, foregroundLabel.height) |
| 105 |
scale: styledItem.pressed ? 0.98 : 1.0 |
| 106 |
Behavior on scale { |
| 107 |
NumberAnimation { |
| 108 |
duration: LomiriAnimation.SnapDuration |
| 109 |
easing.type: Easing.Linear |
| 110 |
} |
| 111 |
} |
| 112 |
state: foreground.hasIcon && hasText ? iconPosition : "center" |
| 113 |
|
| 114 |
Icon { |
| 115 |
id: foregroundIcon |
| 116 |
visible: foreground.hasIcon |
| 117 |
anchors.verticalCenter: parent.verticalCenter |
| 118 |
width: foreground.iconSize |
| 119 |
height: foreground.iconSize |
| 120 |
color: foregroundLabel.color |
| 121 |
source: styledItem.iconSource |
| 122 |
} |
| 123 |
|
| 124 |
Label { |
| 125 |
id: foregroundLabel |
| 126 |
anchors.verticalCenter: parent.verticalCenter |
| 127 |
elide: Text.ElideRight |
| 128 |
color: theme.palette.normal.foregroundText |
| 129 |
font: styledItem.font |
| 130 |
text: styledItem.text |
| 131 |
} |
| 132 |
|
| 133 |
states: [ |
| 134 |
State { |
| 135 |
name: "left" |
| 136 |
AnchorChanges { |
| 137 |
target: foregroundIcon |
| 138 |
anchors.left: foreground.left |
| 139 |
} |
| 140 |
AnchorChanges { |
| 141 |
target: foregroundLabel |
| 142 |
anchors.left: foregroundIcon.right |
| 143 |
} |
| 144 |
PropertyChanges { |
| 145 |
target: foregroundLabel |
| 146 |
anchors.leftMargin: spacing |
| 147 |
width: foreground.width - foregroundIcon.width - spacing |
| 148 |
} |
| 149 |
PropertyChanges { |
| 150 |
target: foreground |
| 151 |
implicitWidth: foregroundIcon.implicitWidth + spacing + foregroundLabel.implicitWidth |
| 152 |
} |
| 153 |
}, |
| 154 |
State { |
| 155 |
name: "right" |
| 156 |
AnchorChanges { |
| 157 |
target: foregroundIcon |
| 158 |
anchors.right: foreground.right |
| 159 |
} |
| 160 |
AnchorChanges { |
| 161 |
target: foregroundLabel |
| 162 |
anchors.right: foregroundIcon.left |
| 163 |
} |
| 164 |
PropertyChanges { |
| 165 |
target: foregroundLabel |
| 166 |
anchors.rightMargin: spacing |
| 167 |
width: foreground.width - foregroundIcon.width - spacing |
| 168 |
} |
| 169 |
PropertyChanges { |
| 170 |
target: foreground |
| 171 |
implicitWidth: foregroundIcon.implicitWidth + spacing + foregroundLabel.implicitWidth |
| 172 |
} |
| 173 |
}, |
| 174 |
State { |
| 175 |
name: "center" |
| 176 |
AnchorChanges { |
| 177 |
target: foregroundIcon |
| 178 |
anchors.horizontalCenter: foreground.horizontalCenter |
| 179 |
} |
| 180 |
AnchorChanges { |
| 181 |
target: foregroundLabel |
| 182 |
anchors.horizontalCenter: foreground.horizontalCenter |
| 183 |
} |
| 184 |
PropertyChanges { |
| 185 |
target: foregroundLabel |
| 186 |
width: foreground.width |
| 187 |
horizontalAlignment: Text.AlignHCenter |
| 188 |
} |
| 189 |
PropertyChanges { |
| 190 |
target: foreground |
| 191 |
implicitWidth: foreground.hasText ? foregroundLabel.implicitWidth : foregroundIcon.implicitWidth |
| 192 |
} |
| 193 |
} |
| 194 |
] |
| 195 |
} |
| 196 |
|
| 197 |
ShaderEffectSource { |
| 198 |
id: comboFace |
| 199 |
sourceItem: content |
| 200 |
hideSource: true |
| 201 |
// FIXME: visible: false prevents rendering so make it a nearly |
| 202 |
// transparent 1x1 pixel instead |
| 203 |
opacity: 0.01 |
| 204 |
width: 1 |
| 205 |
height: 1 |
| 206 |
} |
| 207 |
|
| 208 |
Item { |
| 209 |
id: content |
| 210 |
width: mainButton.width |
| 211 |
height: mainButton.height |
| 212 |
|
| 213 |
// button background |
| 214 |
Rectangle { |
| 215 |
anchors { |
| 216 |
fill: parent |
| 217 |
rightMargin: comboStyle.dropDownWidth |
| 218 |
} |
| 219 |
color: defaultColor |
| 220 |
} |
| 221 |
|
| 222 |
Rectangle { |
| 223 |
id: dropDownButton |
| 224 |
objectName: "combobutton_dropdown_visuals" |
| 225 |
anchors { |
| 226 |
right: parent.right |
| 227 |
top: parent.top |
| 228 |
bottom: parent.bottom |
| 229 |
} |
| 230 |
width: comboStyle.dropDownWidth |
| 231 |
color: defaultDropdownColor |
| 232 |
Icon { |
| 233 |
anchors.centerIn: parent |
| 234 |
height: parent.height / 2 |
| 235 |
width: height |
| 236 |
name: styledItem.expanded? "up" : "down" |
| 237 |
color: foregroundLabel.color |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
FocusShape { |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
Item { |
| 247 |
id: panelItem |
| 248 |
objectName: "combobutton_combopanel" |
| 249 |
anchors { |
| 250 |
left: parent.left |
| 251 |
top: mainButton.bottom |
| 252 |
right: parent.right |
| 253 |
} |
| 254 |
// Hide panel to suppress keyboard input including (Shift)Tab |
| 255 |
visible: styledItem.expanded && (styledItem.comboList.length > 0) |
| 256 |
|
| 257 |
ShaderEffectSource { |
| 258 |
id: listContent |
| 259 |
sourceItem: comboListContent |
| 260 |
hideSource: true |
| 261 |
// FIXME: visible: false prevents rendering so make it a nearly |
| 262 |
// transparent 1x1 pixel instead |
| 263 |
opacity: 0.01 |
| 264 |
width: 1 |
| 265 |
height: 1 |
| 266 |
} |
| 267 |
Rectangle { |
| 268 |
id: comboListContent |
| 269 |
anchors { |
| 270 |
fill: parent |
| 271 |
topMargin: comboListMargin |
| 272 |
} |
| 273 |
clip: true |
| 274 |
color: defaultDropdownColor |
| 275 |
} |
| 276 |
|
| 277 |
LomiriShape { |
| 278 |
id: shape |
| 279 |
anchors { |
| 280 |
fill: parent |
| 281 |
topMargin: comboListMargin |
| 282 |
} |
| 283 |
source: listContent |
| 284 |
} |
| 285 |
|
| 286 |
Behavior on height { |
| 287 |
NumberAnimation { |
| 288 |
duration: LomiriAnimation.FastDuration |
| 289 |
easing: LomiriAnimation.StandardEasing |
| 290 |
} |
| 291 |
} |
| 292 |
Behavior on opacity { |
| 293 |
NumberAnimation { |
| 294 |
duration: LomiriAnimation.FastDuration |
| 295 |
easing: LomiriAnimation.StandardEasing |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
} |