2011-04-27 21:14:04 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 by Daker Fernandes Pinheiro <dakerfp@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import QtQuick 1.0
|
|
|
|
import org.kde.plasma.core 0.1 as PlasmaCore
|
|
|
|
|
|
|
|
// TODO: create a value indicator for plasma?
|
2011-04-28 12:43:18 +02:00
|
|
|
Item {
|
2011-04-27 21:14:04 +02:00
|
|
|
id: slider
|
|
|
|
|
|
|
|
// Common API
|
|
|
|
property alias stepSize: range.stepSize
|
|
|
|
property alias minimumValue: range.minimumValue
|
|
|
|
property alias maximumValue: range.maximumValue
|
|
|
|
property alias value: range.value
|
|
|
|
property int orientation: Qt.Horizontal
|
|
|
|
property alias pressed: mouseArea.pressed
|
|
|
|
property bool valueIndicatorVisible: false
|
|
|
|
property string valueIndicatorText: value
|
|
|
|
|
|
|
|
// Plasma API
|
|
|
|
property bool animated: false
|
|
|
|
property alias inverted: range.inverted
|
|
|
|
property bool updateValueWhileDragging: true
|
2011-07-16 01:45:08 +02:00
|
|
|
property real handleSize: 22
|
2011-04-27 21:14:04 +02:00
|
|
|
|
2011-11-08 15:12:44 +01:00
|
|
|
// Convenience API
|
2011-04-27 21:14:04 +02:00
|
|
|
property bool _isVertical: orientation == Qt.Vertical
|
|
|
|
|
2011-11-17 20:14:47 +01:00
|
|
|
width: _isVertical ? theme.defaultFont.mSize.height*1.6 : 200
|
|
|
|
height: _isVertical ? 200 : theme.defaultFont.mSize.height*1.6
|
2011-07-19 00:09:50 +02:00
|
|
|
// TODO: needs to define if there will be specific graphics for
|
|
|
|
// disabled sliders
|
|
|
|
opacity: enabled ? 1.0 : 0.5
|
2011-04-27 21:14:04 +02:00
|
|
|
|
2011-07-08 18:18:58 +02:00
|
|
|
Keys.onUpPressed: {
|
2011-07-19 00:09:50 +02:00
|
|
|
if (!enabled || !_isVertical)
|
2011-07-08 18:18:58 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (inverted)
|
|
|
|
value -= stepSize;
|
|
|
|
else
|
|
|
|
value += stepSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
Keys.onDownPressed: {
|
2011-07-19 00:09:50 +02:00
|
|
|
if (!enabled || !enabled)
|
|
|
|
return;
|
|
|
|
|
2011-07-08 18:18:58 +02:00
|
|
|
if (!_isVertical)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (inverted)
|
|
|
|
value += stepSize;
|
|
|
|
else
|
|
|
|
value -= stepSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
Keys.onLeftPressed: {
|
2011-07-19 00:09:50 +02:00
|
|
|
if (!enabled || _isVertical)
|
2011-07-08 18:18:58 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (inverted)
|
|
|
|
value += stepSize;
|
|
|
|
else
|
|
|
|
value -= stepSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
Keys.onRightPressed: {
|
2011-07-19 00:09:50 +02:00
|
|
|
if (!enabled || _isVertical)
|
2011-07-08 18:18:58 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (inverted)
|
|
|
|
value -= stepSize;
|
|
|
|
else
|
|
|
|
value += stepSize;
|
|
|
|
}
|
|
|
|
|
2011-04-27 21:14:04 +02:00
|
|
|
Item {
|
|
|
|
id: contents
|
|
|
|
|
|
|
|
width: _isVertical ? slider.height : slider.width
|
|
|
|
height: _isVertical ? slider.width : slider.height
|
|
|
|
rotation: _isVertical ? -90 : 0
|
|
|
|
|
|
|
|
anchors.centerIn: parent
|
|
|
|
|
2011-09-13 21:21:29 +02:00
|
|
|
RangeModel {
|
2011-04-27 21:14:04 +02:00
|
|
|
id: range
|
|
|
|
|
|
|
|
minimumValue: 0.0
|
|
|
|
maximumValue: 1.0
|
|
|
|
value: 0
|
|
|
|
stepSize: 0.0
|
|
|
|
inverted: false
|
2011-11-08 15:12:44 +01:00
|
|
|
positionAtMinimum: 0
|
|
|
|
positionAtMaximum: contents.width - handle.width
|
2011-04-27 21:14:04 +02:00
|
|
|
}
|
|
|
|
|
2011-10-11 14:26:51 +02:00
|
|
|
PlasmaCore.Svg {
|
|
|
|
id: grooveSvg
|
2011-10-11 17:35:23 +02:00
|
|
|
imagePath: "widgets/slider"
|
2011-10-11 14:26:51 +02:00
|
|
|
}
|
|
|
|
PlasmaCore.FrameSvgItem {
|
2011-04-27 21:14:04 +02:00
|
|
|
id: groove
|
2011-10-11 17:35:23 +02:00
|
|
|
imagePath: "widgets/slider"
|
|
|
|
prefix: "groove"
|
2011-10-11 14:26:51 +02:00
|
|
|
//FIXME: frameSvg should have a minimumSize attribute, could be added to kdelibs 4.7(maybe just the qml binding is enough)?
|
2011-10-11 17:35:23 +02:00
|
|
|
height: grooveSvg.elementSize("groove-topleft").height + grooveSvg.elementSize("groove-bottomleft").height
|
2011-04-27 21:14:04 +02:00
|
|
|
anchors {
|
|
|
|
left: parent.left
|
|
|
|
right: parent.right
|
|
|
|
verticalCenter: parent.verticalCenter
|
|
|
|
}
|
2011-10-11 14:26:51 +02:00
|
|
|
}
|
|
|
|
PlasmaCore.FrameSvgItem {
|
|
|
|
id: highlight
|
2011-10-11 17:35:23 +02:00
|
|
|
imagePath: "widgets/slider"
|
|
|
|
prefix: "groove-highlight"
|
2011-10-11 14:26:51 +02:00
|
|
|
height: groove.height
|
2011-11-08 15:12:44 +01:00
|
|
|
width: inverted ? groove.width - handle.x : fakeHandle.x + handle.width/2
|
2011-10-11 14:52:39 +02:00
|
|
|
x: inverted ? handle.x : 0
|
|
|
|
anchors.verticalCenter: parent.verticalCenter
|
|
|
|
|
2011-10-11 14:26:51 +02:00
|
|
|
visible: range.position > 0 && slider.enabled
|
2011-04-27 21:14:04 +02:00
|
|
|
}
|
|
|
|
|
2011-11-08 15:12:44 +01:00
|
|
|
RoundShadow {
|
|
|
|
id: shadow
|
|
|
|
imagePath: "widgets/slider"
|
|
|
|
focusElement: "horizontal-slider-focus"
|
|
|
|
hoverElement: "horizontal-slider-hover"
|
|
|
|
shadowElement: "horizontal-slider-shadow"
|
2011-11-10 17:23:51 +01:00
|
|
|
state: slider.activeFocus ? "focus" : (mouseArea.containsMouse ? "hover" : "shadow")
|
2011-11-08 15:12:44 +01:00
|
|
|
anchors.fill: handle
|
2011-04-27 21:14:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PlasmaCore.SvgItem {
|
|
|
|
id: handle
|
|
|
|
|
|
|
|
x: fakeHandle.x
|
|
|
|
anchors {
|
|
|
|
verticalCenter: groove.verticalCenter
|
|
|
|
}
|
2011-07-16 01:45:08 +02:00
|
|
|
width: handleSize
|
|
|
|
height: handleSize
|
2011-04-27 21:14:04 +02:00
|
|
|
svg: PlasmaCore.Svg { imagePath: "widgets/slider" }
|
|
|
|
elementId: "horizontal-slider-handle"
|
|
|
|
|
|
|
|
Behavior on x {
|
|
|
|
id: behavior
|
|
|
|
enabled: !mouseArea.drag.active && slider.animated
|
|
|
|
|
|
|
|
PropertyAnimation {
|
|
|
|
duration: behavior.enabled ? 150 : 0
|
|
|
|
easing.type: Easing.OutSine
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Item {
|
|
|
|
id: fakeHandle
|
|
|
|
width: handle.width
|
|
|
|
height: handle.height
|
|
|
|
}
|
|
|
|
|
|
|
|
MouseArea {
|
|
|
|
id: mouseArea
|
|
|
|
|
|
|
|
anchors.fill: parent
|
2011-07-19 00:09:50 +02:00
|
|
|
enabled: slider.enabled
|
2011-04-27 21:14:04 +02:00
|
|
|
drag {
|
|
|
|
target: fakeHandle
|
|
|
|
axis: Drag.XAxis
|
|
|
|
minimumX: range.positionAtMinimum
|
|
|
|
maximumX: range.positionAtMaximum
|
|
|
|
}
|
|
|
|
hoverEnabled: true
|
|
|
|
|
|
|
|
onPressed: {
|
|
|
|
// Clamp the value
|
2011-11-08 15:12:44 +01:00
|
|
|
var newX = Math.max(mouse.x, drag.minimumX)
|
|
|
|
newX = Math.min(newX, drag.maximumX)
|
2011-04-27 21:14:04 +02:00
|
|
|
|
|
|
|
// Debounce the press: a press event inside the handler will not
|
|
|
|
// change its position, the user needs to drag it.
|
2011-11-08 15:12:44 +01:00
|
|
|
if (Math.abs(newX - fakeHandle.x) > handle.width / 2) {
|
|
|
|
range.position = newX
|
|
|
|
}
|
2011-04-27 21:14:04 +02:00
|
|
|
|
2011-11-08 15:12:44 +01:00
|
|
|
slider.forceActiveFocus()
|
2011-04-27 21:14:04 +02:00
|
|
|
}
|
|
|
|
onReleased: {
|
|
|
|
// If we don't update while dragging, this is the only
|
|
|
|
// moment that the range is updated.
|
2011-11-08 15:12:44 +01:00
|
|
|
if (!slider.updateValueWhileDragging) {
|
|
|
|
range.position = fakeHandle.x
|
|
|
|
}
|
2011-04-27 21:14:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Range position normally follow fakeHandle, except when
|
|
|
|
// 'updateValueWhileDragging' is false. In this case it will only follow
|
|
|
|
// if the user is not pressing the handle.
|
|
|
|
Binding {
|
|
|
|
when: updateValueWhileDragging || !mouseArea.pressed
|
|
|
|
target: range
|
|
|
|
property: "position"
|
|
|
|
value: fakeHandle.x
|
|
|
|
}
|
|
|
|
|
|
|
|
// During the drag, we simply ignore position set from the range, this
|
|
|
|
// means that setting a value while dragging will not "interrupt" the
|
|
|
|
// dragging activity.
|
|
|
|
Binding {
|
|
|
|
when: !mouseArea.drag.active
|
|
|
|
target: fakeHandle
|
|
|
|
property: "x"
|
|
|
|
value: range.position
|
|
|
|
}
|
2011-04-28 12:43:18 +02:00
|
|
|
}
|