| 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 |
/* |
| 21 |
The default slider style consists of a bar and a thumb shape. |
| 22 |
|
| 23 |
This style is themed using the following properties: |
| 24 |
- thumbSpacing: spacing between the thumb and the bar |
| 25 |
*/ |
| 26 |
Item { |
| 27 |
id: sliderStyle |
| 28 |
|
| 29 |
property color foregroundColor: LomiriColors.orange |
| 30 |
property color backgroundColor: Theme.palette.normal.base |
| 31 |
|
| 32 |
property real thumbSpacing: units.gu(0) |
| 33 |
property Item bar: background |
| 34 |
property Item thumb: thumb |
| 35 |
|
| 36 |
implicitWidth: units.gu(38) |
| 37 |
implicitHeight: units.gu(5) |
| 38 |
|
| 39 |
LomiriShapeOverlay { |
| 40 |
id: background |
| 41 |
anchors { |
| 42 |
verticalCenter: parent.verticalCenter |
| 43 |
right: parent.right |
| 44 |
left: parent.left |
| 45 |
} |
| 46 |
height: units.dp(4) |
| 47 |
backgroundColor: sliderStyle.backgroundColor |
| 48 |
overlayColor: sliderStyle.foregroundColor |
| 49 |
overlayRect: Qt.application.layoutDirection == Qt.LeftToRight ? |
| 50 |
Qt.rect(0.0, 0.0, thumb.x / thumb.barMinusThumbWidth, 1.0) : |
| 51 |
Qt.rect(thumb.x / thumb.barMinusThumbWidth, 0.0, 1 - (thumb.x / thumb.barMinusThumbWidth), 1.0) |
| 52 |
} |
| 53 |
|
| 54 |
LomiriShape { |
| 55 |
id: thumb |
| 56 |
|
| 57 |
anchors { |
| 58 |
verticalCenter: parent.verticalCenter |
| 59 |
topMargin: thumbSpacing |
| 60 |
bottomMargin: thumbSpacing |
| 61 |
} |
| 62 |
|
| 63 |
property real barMinusThumbWidth: background.width - (thumb.width + 2.0*thumbSpacing) |
| 64 |
property real position: thumbSpacing + SliderUtils.normalizedValue(styledItem) * barMinusThumbWidth |
| 65 |
property bool pressed: SliderUtils.isPressed(styledItem) |
| 66 |
property bool positionReached: x == position |
| 67 |
x: position |
| 68 |
|
| 69 |
/* Enable the animation on x when pressing the slider. |
| 70 |
Disable it when x has reached the target position. |
| 71 |
*/ |
| 72 |
onPressedChanged: if (pressed) xBehavior.enabled = true; |
| 73 |
onPositionReachedChanged: if (positionReached) xBehavior.enabled = false; |
| 74 |
|
| 75 |
Behavior on x { |
| 76 |
id: xBehavior |
| 77 |
SmoothedAnimation { |
| 78 |
duration: LomiriAnimation.FastDuration |
| 79 |
} |
| 80 |
} |
| 81 |
width: units.gu(2) |
| 82 |
height: units.gu(2) |
| 83 |
opacity: 0.97 |
| 84 |
color: Theme.palette.normal.overlay |
| 85 |
} |
| 86 |
|
| 87 |
BubbleShape { |
| 88 |
id: bubbleShape |
| 89 |
|
| 90 |
property real minimumWidth: units.gu(8) |
| 91 |
property real horizontalPadding: units.gu(1) |
| 92 |
|
| 93 |
width: Math.max(minimumWidth, label.implicitWidth + 2*horizontalPadding) |
| 94 |
height: units.gu(6) |
| 95 |
|
| 96 |
// FIXME: very temporary implementation |
| 97 |
property real minX: 0.0 |
| 98 |
property real maxX: background.width - width |
| 99 |
property real pointerSize: units.dp(6) |
| 100 |
property real targetMargin: units.gu(1) |
| 101 |
property point globalTarget: Qt.point(thumb.x + thumb.width / 2.0, thumb.y - targetMargin) |
| 102 |
|
| 103 |
x: MathUtils.clamp(globalTarget.x - width / 2.0, minX, maxX) |
| 104 |
y: globalTarget.y - height - pointerSize |
| 105 |
target: Qt.point(globalTarget.x - x, globalTarget.y - y) |
| 106 |
|
| 107 |
property bool pressed: SliderUtils.isPressed(styledItem) |
| 108 |
property bool shouldShow: pressed && label.text != "" |
| 109 |
onShouldShowChanged: if (shouldShow) { |
| 110 |
show(); |
| 111 |
} else { |
| 112 |
hide(); |
| 113 |
} |
| 114 |
|
| 115 |
Label { |
| 116 |
id: label |
| 117 |
anchors.centerIn: parent |
| 118 |
text: styledItem.formatValue(SliderUtils.liveValue(styledItem)) |
| 119 |
fontSize: "large" |
| 120 |
color: Theme.palette.normal.overlayText |
| 121 |
} |
| 122 |
} |
| 123 |
} |