| 1 |
/* |
| 2 |
* Copyright 2016 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.3 |
| 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: theme.palette.normal.activity |
| 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(2) |
| 47 |
backgroundColor: sliderStyle.backgroundColor |
| 48 |
aspect: LomiriShape.Flat |
| 49 |
overlayColor: sliderStyle.foregroundColor |
| 50 |
overlayRect: Qt.application.layoutDirection == Qt.LeftToRight ? |
| 51 |
Qt.rect(0.0, 0.0, thumb.x / thumb.barMinusThumbWidth, 1.0) : |
| 52 |
Qt.rect(thumb.x / thumb.barMinusThumbWidth, 0.0, 1 - (thumb.x / thumb.barMinusThumbWidth), 1.0) |
| 53 |
} |
| 54 |
|
| 55 |
LomiriShape { |
| 56 |
id: thumb |
| 57 |
|
| 58 |
anchors { |
| 59 |
verticalCenter: parent.verticalCenter |
| 60 |
topMargin: thumbSpacing |
| 61 |
bottomMargin: thumbSpacing |
| 62 |
} |
| 63 |
|
| 64 |
property real barMinusThumbWidth: background.width - (thumb.width + 2.0*thumbSpacing) |
| 65 |
property real position: thumbSpacing + SliderUtils.normalizedValue(styledItem) * barMinusThumbWidth |
| 66 |
property bool pressed: SliderUtils.isPressed(styledItem) |
| 67 |
property bool positionReached: x == position |
| 68 |
x: position |
| 69 |
|
| 70 |
/* Enable the animation on x when pressing the slider. |
| 71 |
Disable it when x has reached the target position. |
| 72 |
*/ |
| 73 |
onPressedChanged: if (pressed) xBehavior.enabled = true; |
| 74 |
onPositionReachedChanged: if (positionReached) xBehavior.enabled = false; |
| 75 |
|
| 76 |
Behavior on x { |
| 77 |
id: xBehavior |
| 78 |
SmoothedAnimation { |
| 79 |
duration: LomiriAnimation.FastDuration |
| 80 |
} |
| 81 |
} |
| 82 |
width: units.gu(2) |
| 83 |
height: units.gu(2) |
| 84 |
backgroundColor: theme.palette.normal.raised |
| 85 |
|
| 86 |
FocusShape { |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
BubbleShape { |
| 91 |
id: bubbleShape |
| 92 |
|
| 93 |
property real minimumWidth: units.gu(6) |
| 94 |
property real horizontalPadding: units.gu(1) |
| 95 |
|
| 96 |
width: label.implicitWidth + 2*horizontalPadding |
| 97 |
height: label.implicitHeight + 2*horizontalPadding |
| 98 |
|
| 99 |
// FIXME: very temporary implementation |
| 100 |
property real minX: 0.0 |
| 101 |
property real maxX: background.width - width |
| 102 |
property real pointerSize: units.dp(6) |
| 103 |
property real targetMargin: units.gu(1) |
| 104 |
property point globalTarget: Qt.point(thumb.x + thumb.width / 2.0, thumb.y - targetMargin) |
| 105 |
|
| 106 |
x: MathUtils.clamp(globalTarget.x - width / 2.0, minX, maxX) |
| 107 |
y: globalTarget.y - height - pointerSize |
| 108 |
target: Qt.point(globalTarget.x - x, globalTarget.y - y) |
| 109 |
|
| 110 |
property bool pressed: SliderUtils.isPressed(styledItem) |
| 111 |
property bool shouldShow: pressed && label.text != "" |
| 112 |
onShouldShowChanged: if (shouldShow) { |
| 113 |
show(); |
| 114 |
} else { |
| 115 |
hide(); |
| 116 |
} |
| 117 |
|
| 118 |
Label { |
| 119 |
id: label |
| 120 |
anchors.centerIn: parent |
| 121 |
text: styledItem.formatValue(SliderUtils.liveValue(styledItem)) |
| 122 |
textSize: Label.Medium |
| 123 |
color: theme.palette.normal.overlayText |
| 124 |
} |
| 125 |
} |
| 126 |
} |