plasma-framework/declarativeimports/plasmacomponents/qml/private/ScrollBarDelegate.qml

252 lines
8.2 KiB
QML
Raw Normal View History

/*
* Copyright (C) 2011 by Daker Fernandes Pinheiro <dakerfp@gmail.com>
* Copyright (C) 2011 Marco Martin <mart@kde.org>
*
* 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.1
import org.kde.plasma.core 0.1 as PlasmaCore
PlasmaCore.FrameSvgItem {
id: background
anchors.fill: parent
imagePath:"widgets/scrollbar"
2011-11-09 17:05:21 +01:00
prefix: internalLoader.isVertical ? "background-vertical" : "background-horizontal"
Keys.onUpPressed: {
2011-11-09 17:05:21 +01:00
if (!enabled || !internalLoader.isVertical)
return;
if (inverted)
internalLoader.incrementValue(stepSize);
else
internalLoader.incrementValue(-stepSize);
}
Keys.onDownPressed: {
2011-11-09 17:05:21 +01:00
if (!enabled || !internalLoader.isVertical)
return;
if (inverted)
internalLoader.incrementValue(-stepSize);
else
internalLoader.incrementValue(stepSize);
}
Keys.onLeftPressed: {
2011-11-09 17:05:21 +01:00
if (!enabled || internalLoader.isVertical)
return;
if (inverted)
internalLoader.incrementValue(stepSize);
else
internalLoader.incrementValue(-stepSize);
}
Keys.onRightPressed: {
2011-11-09 17:05:21 +01:00
if (!enabled || internalLoader.isVertical)
return;
if (inverted)
internalLoader.incrementValue(-stepSize);
else
internalLoader.incrementValue(stepSize);
}
property Item handle: handle
property Item contents: contents
Item {
id: contents
anchors {
fill: parent
2011-11-09 17:05:21 +01:00
leftMargin: internalLoader.isVertical || stepSize <= 0 ? 0 : leftButton.width
rightMargin: internalLoader.isVertical || stepSize <= 0 ? 0 : rightButton.width
topMargin: internalLoader.isVertical && stepSize > 0 ? leftButton.height : 0
bottomMargin: internalLoader.isVertical && stepSize > 0 ? rightButton.height : 0
}
PlasmaCore.FrameSvgItem {
id: handle
imagePath:"widgets/scrollbar"
prefix: {
if (mouseArea.pressed) {
return "sunken-slider"
}
if (scrollbar.activeFocus || mouseArea.containsMouse) {
return "mouseover-slider"
} else {
return "slider"
}
}
2011-11-09 17:05:21 +01:00
property int length: internalLoader.isVertical? flickableItem.visibleArea.heightRatio * parent.height : flickableItem.visibleArea.widthRatio * parent.width
2011-11-09 17:05:21 +01:00
width: internalLoader.isVertical ? parent.width : length
height: internalLoader.isVertical ? length : parent.height
}
}
PlasmaCore.Svg {
id: scrollbarSvg
imagePath: "widgets/scrollbar"
}
PlasmaCore.SvgItem {
id: leftButton
visible: stepSize > 0
anchors {
2011-11-09 17:05:21 +01:00
left: internalLoader.isVertical ? undefined : parent.left
verticalCenter: internalLoader.isVertical ? undefined : parent.verticalCenter
top: internalLoader.isVertical ? parent.top : undefined
horizontalCenter: internalLoader.isVertical ? parent.horizontalCenter : undefined
}
2011-11-09 17:05:21 +01:00
width: 18
height: 18
svg: scrollbarSvg
elementId: {
if (leftMouseArea.pressed) {
2011-11-09 17:05:21 +01:00
return internalLoader.isVertical ? "sunken-arrow-up" : "sunken-arrow-left"
}
if (scrollbar.activeFocus || leftMouseArea.containsMouse) {
2011-11-09 17:05:21 +01:00
return internalLoader.isVertical ? "mouseover-arrow-up" : "mouseover-arrow-left"
} else {
2011-11-09 17:05:21 +01:00
return internalLoader.isVertical ? "mouseover-arrow-up" : "arrow-left"
}
}
MouseArea {
id: leftMouseArea
anchors.fill: parent
enabled: scrollbar.enabled
Timer {
id: leftTimer
interval: scrollbar.scrollButtonInterval;
running: parent.pressed
repeat: true
onTriggered: {
background.forceActiveFocus()
if (inverted) {
internalLoader.incrementValue(stepSize);
} else {
internalLoader.incrementValue(-stepSize);
}
}
}
}
}
PlasmaCore.SvgItem {
id: rightButton
visible: stepSize > 0
anchors {
2011-11-09 17:05:21 +01:00
right: internalLoader.isVertical ? undefined : parent.right
verticalCenter: internalLoader.isVertical ? undefined : parent.verticalCenter
bottom: internalLoader.isVertical ? parent.bottom : undefined
horizontalCenter: internalLoader.isVertical ? parent.horizontalCenter : undefined
}
2011-11-09 17:05:21 +01:00
width: 18
height: 18
svg: scrollbarSvg
elementId: {
if (leftMouseArea.pressed) {
2011-11-09 17:05:21 +01:00
return internalLoader.isVertical ? "sunken-arrow-down" : "sunken-arrow-right"
}
if (scrollbar.activeFocus || leftMouseArea.containsMouse) {
2011-11-09 17:05:21 +01:00
return internalLoader.isVertical ? "mouseover-arrow-down" : "mouseover-arrow-right"
} else {
2011-11-09 17:05:21 +01:00
return internalLoader.isVertical ? "mouseover-arrow-down" : "arrow-right"
}
}
MouseArea {
id: rightMouseArea
anchors.fill: parent
enabled: scrollbar.enabled
Timer {
id: rightTimer
interval: scrollbar.scrollButtonInterval;
running: parent.pressed;
repeat: true
onTriggered: {
background.forceActiveFocus();
if (inverted)
internalLoader.incrementValue(-stepSize);
else
internalLoader.incrementValue(stepSize);
}
}
}
}
property MouseArea mouseArea: mouseArea
MouseArea {
id: mouseArea
anchors.fill: contents
enabled: scrollbar.enabled
hoverEnabled: true
drag {
target: handle
2011-11-09 17:05:21 +01:00
axis: internalLoader.isVertical ? Drag.YAxis : Drag.XAxis
minimumX: range.positionAtMinimum
maximumX: range.positionAtMaximum
minimumY: range.positionAtMinimum
maximumY: range.positionAtMaximum
}
onPressed: {
2011-11-09 17:05:21 +01:00
if (internalLoader.isVertical) {
// Clamp the value
var newY = Math.max(mouse.y, drag.minimumY);
newY = Math.min(newY, drag.maximumY);
// Debounce the press: a press event inside the handler will not
// change its position, the user needs to drag it.
if (newY > handle.y + handle.height) {
handle.y = mouse.y - handle.height
} else if (newY < handle.y) {
handle.y = mouse.y
}
} else {
// 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 (newX > handle.x + handle.width) {
handle.x = mouse.x - handle.width
} else if (newX < handle.x) {
handle.x = mouse.x
}
}
background.forceActiveFocus();
}
}
}