2011-04-21 17:01:42 +02:00
|
|
|
/*
|
2011-04-28 12:50:31 +02:00
|
|
|
* Copyright (C) 2011 by Daker Fernandes Pinheiro <dakerfp@gmail.com>
|
2011-04-21 17:01:42 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2011-05-18 21:38:21 +02:00
|
|
|
import QtQuick 1.1
|
2011-04-21 17:01:42 +02:00
|
|
|
import org.kde.plasma.core 0.1 as PlasmaCore
|
|
|
|
|
2011-04-28 12:50:31 +02:00
|
|
|
// TODO: add support mouse wheel and key events
|
|
|
|
Item {
|
|
|
|
id: scrollbar
|
|
|
|
|
|
|
|
// Common API
|
|
|
|
property Flickable flickableItem: null
|
2011-10-27 21:24:40 +02:00
|
|
|
property bool interactive
|
2011-04-28 12:50:31 +02:00
|
|
|
|
|
|
|
// Plasma API
|
2011-10-28 22:32:45 +02:00
|
|
|
property int orientation: Qt.Vertical
|
2011-05-18 21:38:21 +02:00
|
|
|
property bool animated: true
|
2011-04-28 12:50:31 +02:00
|
|
|
property bool inverted: false
|
|
|
|
property bool updateValueWhileDragging: true
|
|
|
|
property alias stepSize: range.stepSize
|
|
|
|
property alias pressed: mouseArea.pressed
|
2011-04-29 00:22:51 +02:00
|
|
|
property real scrollButtonInterval: 50
|
2011-04-28 12:50:31 +02:00
|
|
|
|
|
|
|
// Convinience API
|
|
|
|
property bool _isVertical: orientation == Qt.Vertical
|
2011-04-29 00:22:51 +02:00
|
|
|
property bool _showButtons: stepSize != 0
|
|
|
|
property bool _inverted: _isVertical ?
|
|
|
|
!scrollbar.inverted : scrollbar.inverted
|
2011-04-28 12:50:31 +02:00
|
|
|
|
2011-05-18 21:38:21 +02:00
|
|
|
implicitWidth: _isVertical ? 22 : 200
|
|
|
|
implicitHeight: _isVertical ? 200 : 22
|
2011-07-19 00:17:19 +02:00
|
|
|
// TODO: needs to define if there will be specific graphics for
|
|
|
|
// disabled scroll bars
|
|
|
|
opacity: enabled ? 1.0 : 0.5
|
2011-04-28 12:50:31 +02:00
|
|
|
|
|
|
|
visible: flickableItem && handle.width < contents.width
|
|
|
|
|
2011-10-28 22:32:45 +02:00
|
|
|
anchors {
|
|
|
|
right: flickableItem.right
|
|
|
|
left: (orientation == Qt.Vertical) ? undefined : flickableItem.left
|
|
|
|
top: (orientation == Qt.Vertical) ? flickableItem.top : undefined
|
|
|
|
bottom: flickableItem.bottom
|
|
|
|
}
|
|
|
|
|
2011-07-15 15:03:17 +02:00
|
|
|
function incrementValue(increment) {
|
|
|
|
if (!flickableItem)
|
|
|
|
return;
|
|
|
|
|
2011-07-21 22:48:31 +02:00
|
|
|
if (_isVertical) {
|
|
|
|
flickableItem.contentY = Math.min(flickableItem.contentHeight,
|
|
|
|
flickableItem.contentY + increment);
|
|
|
|
} else {
|
|
|
|
flickableItem.contentX = Math.min(flickableItem.contentWidth,
|
|
|
|
flickableItem.contentX + increment);
|
|
|
|
}
|
2011-07-15 15:03:17 +02:00
|
|
|
}
|
|
|
|
|
2011-07-08 01:43:24 +02:00
|
|
|
Keys.onUpPressed: {
|
2011-07-19 00:17:19 +02:00
|
|
|
if (!enabled || !_isVertical)
|
2011-07-08 01:43:24 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (_inverted)
|
2011-07-15 15:03:17 +02:00
|
|
|
incrementValue(-stepSize);
|
2011-07-08 01:43:24 +02:00
|
|
|
else
|
2011-07-15 15:03:17 +02:00
|
|
|
incrementValue(stepSize);
|
2011-07-08 01:43:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Keys.onDownPressed: {
|
2011-07-19 00:17:19 +02:00
|
|
|
if (!enabled || !_isVertical)
|
2011-07-08 01:43:24 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (_inverted)
|
2011-07-15 15:03:17 +02:00
|
|
|
incrementValue(stepSize);
|
2011-07-08 01:43:24 +02:00
|
|
|
else
|
2011-07-15 15:03:17 +02:00
|
|
|
incrementValue(-stepSize);
|
2011-07-08 01:43:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Keys.onLeftPressed: {
|
2011-07-19 00:17:19 +02:00
|
|
|
if (!enabled || _isVertical)
|
2011-07-08 01:43:24 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (_inverted)
|
2011-07-15 15:03:17 +02:00
|
|
|
incrementValue(stepSize);
|
2011-07-08 01:43:24 +02:00
|
|
|
else
|
2011-07-15 15:03:17 +02:00
|
|
|
incrementValue(-stepSize);
|
2011-07-08 01:43:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Keys.onRightPressed: {
|
2011-07-19 00:17:19 +02:00
|
|
|
if (!enabled || _isVertical)
|
2011-07-08 01:43:24 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (_inverted)
|
2011-07-15 15:03:17 +02:00
|
|
|
incrementValue(-stepSize);
|
2011-07-08 01:43:24 +02:00
|
|
|
else
|
2011-07-15 15:03:17 +02:00
|
|
|
incrementValue(stepSize);
|
2011-07-08 01:43:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-28 12:50:31 +02:00
|
|
|
Item {
|
|
|
|
|
|
|
|
width: _isVertical ? scrollbar.height : scrollbar.width
|
|
|
|
height: _isVertical ? scrollbar.width : scrollbar.height
|
|
|
|
rotation: _isVertical ? -90 : 0
|
|
|
|
|
|
|
|
anchors.centerIn: parent
|
2011-04-29 00:22:51 +02:00
|
|
|
PlasmaCore.Svg {
|
|
|
|
id: scrollbarSvg
|
|
|
|
imagePath: "widgets/scrollbar"
|
|
|
|
}
|
2011-04-28 12:50:31 +02:00
|
|
|
|
2011-04-29 00:22:51 +02:00
|
|
|
PlasmaCore.SvgItem {
|
|
|
|
id: leftButton
|
2011-04-28 12:50:31 +02:00
|
|
|
|
2011-04-29 00:22:51 +02:00
|
|
|
anchors {
|
|
|
|
left: parent.left
|
|
|
|
verticalCenter: parent.verticalCenter
|
|
|
|
}
|
|
|
|
height: 18
|
|
|
|
width: _showButtons ? 18 : 0
|
|
|
|
svg: scrollbarSvg
|
|
|
|
elementId: {
|
|
|
|
if (leftMousArea.pressed)
|
|
|
|
return "sunken-arrow-left";
|
|
|
|
|
|
|
|
if (scrollbar.activeFocus || leftMousArea.containsMouse)
|
|
|
|
return "mouseover-arrow-left";
|
2011-04-28 12:50:31 +02:00
|
|
|
else
|
2011-04-29 00:22:51 +02:00
|
|
|
return "arrow-left";
|
|
|
|
}
|
|
|
|
|
|
|
|
MouseArea {
|
|
|
|
id: leftMousArea
|
|
|
|
|
|
|
|
anchors.fill: parent
|
2011-07-19 00:17:19 +02:00
|
|
|
enabled: scrollbar.enabled
|
2011-04-29 00:22:51 +02:00
|
|
|
Timer {
|
|
|
|
id: leftTimer
|
|
|
|
interval: scrollbar.scrollButtonInterval;
|
|
|
|
running: parent.pressed
|
|
|
|
repeat: true
|
|
|
|
onTriggered: {
|
2011-07-08 01:43:24 +02:00
|
|
|
scrollbar.forceActiveFocus();
|
2011-04-29 00:22:51 +02:00
|
|
|
if (_inverted)
|
2011-07-15 15:03:17 +02:00
|
|
|
incrementValue(stepSize);
|
2011-04-29 00:22:51 +02:00
|
|
|
else
|
2011-07-15 15:03:17 +02:00
|
|
|
incrementValue(-stepSize);
|
2011-04-29 00:22:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-28 12:50:31 +02:00
|
|
|
|
2011-04-29 00:22:51 +02:00
|
|
|
PlasmaCore.SvgItem {
|
|
|
|
id: rightButton
|
|
|
|
|
|
|
|
anchors {
|
|
|
|
right: parent.right
|
|
|
|
verticalCenter: parent.verticalCenter
|
2011-04-21 17:01:42 +02:00
|
|
|
}
|
2011-04-29 00:22:51 +02:00
|
|
|
height: 18
|
|
|
|
width: _showButtons ? 18 : 0
|
|
|
|
svg: scrollbarSvg
|
|
|
|
elementId: {
|
|
|
|
if (rightMousArea.pressed)
|
|
|
|
return "sunken-arrow-right";
|
|
|
|
|
|
|
|
if (scrollbar.activeFocus || rightMousArea.containsMouse)
|
|
|
|
return "mouseover-arrow-right";
|
2011-04-28 12:50:31 +02:00
|
|
|
else
|
2011-04-29 00:22:51 +02:00
|
|
|
return "arrow-right";
|
2011-04-21 17:01:42 +02:00
|
|
|
}
|
2011-04-28 12:50:31 +02:00
|
|
|
|
2011-04-29 00:22:51 +02:00
|
|
|
MouseArea {
|
|
|
|
id: rightMousArea
|
|
|
|
|
|
|
|
anchors.fill: parent
|
2011-07-19 00:17:19 +02:00
|
|
|
enabled: scrollbar.enabled
|
2011-04-29 00:22:51 +02:00
|
|
|
Timer {
|
|
|
|
id: rightTimer
|
|
|
|
interval: scrollbar.scrollButtonInterval;
|
|
|
|
running: parent.pressed;
|
|
|
|
repeat: true
|
|
|
|
onTriggered: {
|
2011-07-08 01:43:24 +02:00
|
|
|
scrollbar.forceActiveFocus();
|
2011-04-29 00:22:51 +02:00
|
|
|
if (_inverted)
|
2011-07-15 15:03:17 +02:00
|
|
|
incrementValue(-stepSize);
|
2011-04-29 00:22:51 +02:00
|
|
|
else
|
2011-07-15 15:03:17 +02:00
|
|
|
incrementValue(stepSize);
|
2011-04-29 00:22:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-21 17:01:42 +02:00
|
|
|
}
|
2011-04-28 12:50:31 +02:00
|
|
|
|
2011-04-29 00:22:51 +02:00
|
|
|
Item {
|
|
|
|
id: contents
|
2011-04-28 12:50:31 +02:00
|
|
|
|
2011-04-29 00:22:51 +02:00
|
|
|
anchors {
|
|
|
|
left: leftButton.right
|
|
|
|
top: parent.top
|
|
|
|
bottom: parent.bottom
|
|
|
|
right: rightButton.left
|
|
|
|
}
|
2011-04-28 12:50:31 +02:00
|
|
|
|
2011-09-13 21:21:29 +02:00
|
|
|
RangeModel {
|
2011-04-29 00:22:51 +02:00
|
|
|
id: range
|
2011-04-28 12:50:31 +02:00
|
|
|
|
2011-04-29 00:22:51 +02:00
|
|
|
minimumValue: 0
|
|
|
|
maximumValue: {
|
|
|
|
var diff;
|
|
|
|
if (_isVertical)
|
|
|
|
diff = flickableItem.contentHeight - flickableItem.height;
|
|
|
|
else
|
|
|
|
diff = flickableItem.contentWidth - flickableItem.width;
|
|
|
|
|
|
|
|
return Math.max(0, diff);
|
|
|
|
}
|
|
|
|
stepSize: 0.0
|
|
|
|
inverted: _inverted
|
|
|
|
positionAtMinimum: 0 + handle.width / 2
|
|
|
|
positionAtMaximum: contents.width - handle.width / 2
|
|
|
|
value: _isVertical ? flickableItem.contentY : flickableItem.contentX
|
|
|
|
onValueChanged: {
|
|
|
|
if (flickableItem.flicking)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (_isVertical)
|
|
|
|
flickableItem.contentY = value;
|
|
|
|
else
|
|
|
|
flickableItem.contentX = value;
|
|
|
|
}
|
|
|
|
position: handle.x
|
|
|
|
onPositionChanged: { handle.x = position; }
|
2011-04-21 17:01:42 +02:00
|
|
|
}
|
2011-04-28 12:50:31 +02:00
|
|
|
|
2011-04-29 00:22:51 +02:00
|
|
|
PlasmaCore.FrameSvgItem {
|
|
|
|
id: groove
|
|
|
|
|
|
|
|
anchors.fill: parent
|
|
|
|
imagePath: "widgets/scrollbar"
|
|
|
|
prefix: "background-horizontal"
|
2011-04-21 17:01:42 +02:00
|
|
|
}
|
2011-04-28 12:50:31 +02:00
|
|
|
|
2011-04-29 00:22:51 +02:00
|
|
|
PlasmaCore.FrameSvgItem {
|
|
|
|
id: handle
|
2011-04-28 12:50:31 +02:00
|
|
|
|
2011-04-29 00:22:51 +02:00
|
|
|
transform: Translate { x: - handle.width / 2 }
|
|
|
|
x: fakeHandle.x
|
|
|
|
anchors.verticalCenter: groove.verticalCenter
|
|
|
|
width: {
|
|
|
|
var ratio;
|
|
|
|
if (_isVertical)
|
|
|
|
ratio = flickableItem.visibleArea.heightRatio;
|
|
|
|
else
|
|
|
|
ratio = flickableItem.visibleArea.widthRatio;
|
|
|
|
|
|
|
|
return ratio * parent.width;
|
|
|
|
}
|
|
|
|
height: parent.height - margins.top // TODO: check mergin
|
|
|
|
imagePath: "widgets/scrollbar"
|
|
|
|
prefix: {
|
|
|
|
if (scrollbar.pressed)
|
|
|
|
return "sunken-slider";
|
|
|
|
|
|
|
|
if (scrollbar.activeFocus || mouseArea.containsMouse)
|
|
|
|
return "mouseover-slider";
|
|
|
|
else
|
|
|
|
return "slider";
|
2011-04-28 12:50:31 +02:00
|
|
|
}
|
|
|
|
|
2011-04-29 00:22:51 +02:00
|
|
|
Behavior on x {
|
|
|
|
id: behavior
|
|
|
|
enabled: !mouseArea.drag.active && scrollbar.animated &&
|
|
|
|
!flickableItem.flicking
|
2011-04-28 12:50:31 +02:00
|
|
|
|
2011-04-29 00:22:51 +02:00
|
|
|
PropertyAnimation {
|
|
|
|
duration: behavior.enabled ? 150 : 0
|
|
|
|
easing.type: Easing.OutSine
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-28 12:50:31 +02:00
|
|
|
|
2011-04-29 00:22:51 +02:00
|
|
|
Item {
|
|
|
|
id: fakeHandle
|
|
|
|
width: handle.width
|
|
|
|
height: handle.height
|
|
|
|
transform: Translate { x: - handle.width / 2 }
|
2011-04-28 12:50:31 +02:00
|
|
|
}
|
|
|
|
|
2011-04-29 00:22:51 +02:00
|
|
|
MouseArea {
|
|
|
|
id: mouseArea
|
2011-04-28 12:50:31 +02:00
|
|
|
|
2011-04-29 00:22:51 +02:00
|
|
|
anchors.fill: parent
|
2011-07-19 00:17:19 +02:00
|
|
|
enabled: scrollbar.enabled
|
2011-04-29 00:22:51 +02:00
|
|
|
drag {
|
|
|
|
target: fakeHandle
|
|
|
|
axis: Drag.XAxis
|
|
|
|
minimumX: range.positionAtMinimum
|
|
|
|
maximumX: range.positionAtMaximum
|
|
|
|
}
|
2011-04-28 12:50:31 +02:00
|
|
|
|
2011-04-29 00:22:51 +02:00
|
|
|
onPressed: {
|
|
|
|
// Clamp the value
|
|
|
|
var newX = Math.max(mouse.x, drag.minimumX);
|
|
|
|
newX = Math.min(newX, drag.maximumX);
|
|
|
|
|
|
|
|
// Debounce the press: a press event inside the handler will not
|
|
|
|
// change its position, the user needs to drag it.
|
|
|
|
if (Math.abs(newX - fakeHandle.x) > handle.width / 2)
|
|
|
|
range.position = newX;
|
|
|
|
|
|
|
|
scrollbar.forceActiveFocus();
|
|
|
|
}
|
|
|
|
onReleased: {
|
|
|
|
// If we don't update while dragging, this is the only
|
|
|
|
// moment that the range is updated.
|
|
|
|
if (!scrollbar.updateValueWhileDragging)
|
|
|
|
range.position = fakeHandle.x;
|
|
|
|
}
|
2011-04-21 17:01:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-29 00:22:51 +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
|
|
|
|
}
|
2011-04-21 17:01:42 +02:00
|
|
|
|
2011-04-29 00:22:51 +02:00
|
|
|
// 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:50:31 +02:00
|
|
|
}
|
|
|
|
}
|