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-11-09 14:34:53 +01:00
|
|
|
* Copyright (C) 2011 Marco Martin <mart@kde.org>
|
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-12-22 14:16:01 +01:00
|
|
|
import "private" as Private
|
2011-11-09 15:28:21 +01:00
|
|
|
|
|
|
|
/**
|
2012-12-17 22:02:40 +01:00
|
|
|
* A simple Scroll Bar using the plasma theme.
|
|
|
|
*
|
|
|
|
* This component does not belong to the QtComponents API specification but it
|
|
|
|
* was based on the ScrollDecorator component. You should not use it for touch
|
|
|
|
* interfaces, use a flickable and a ScrollDecorator instead.
|
|
|
|
*
|
|
|
|
* By default, this component looks and behaves like a scroll decorator
|
|
|
|
* on touchscreens.
|
2011-11-09 15:28:21 +01:00
|
|
|
*/
|
|
|
|
// TODO: add support mouse wheel events
|
2011-04-28 12:50:31 +02:00
|
|
|
Item {
|
|
|
|
id: scrollbar
|
|
|
|
|
|
|
|
// Common API
|
2012-12-17 22:02:40 +01:00
|
|
|
/**
|
|
|
|
* The Flickable component which the ScrollBar will interact with.
|
|
|
|
*/
|
2011-04-28 12:50:31 +02:00
|
|
|
property Flickable flickableItem: null
|
2012-12-17 22:02:40 +01:00
|
|
|
/**
|
|
|
|
* The orientation where the ScrollBar will scroll.
|
|
|
|
* The orientation * can be either Qt.Horizontal or Qt.Vertical.
|
|
|
|
*
|
|
|
|
* The default value is Qt.Vertical.
|
|
|
|
*/
|
2011-11-09 14:21:54 +01:00
|
|
|
property int orientation: Qt.Vertical
|
2012-12-17 22:02:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the ScrollBar is interactive.
|
|
|
|
*
|
|
|
|
* The default value is true.
|
|
|
|
*/
|
2011-11-09 14:21:54 +01:00
|
|
|
property bool interactive: true
|
2011-04-28 12:50:31 +02:00
|
|
|
|
|
|
|
// Plasma API
|
2012-12-17 22:02:40 +01:00
|
|
|
/**
|
|
|
|
* Whether the ScrollBar will increase the Flickable content in the normal
|
|
|
|
* direction (Left to Right or Top to Bottom) or if this will be inverted.
|
|
|
|
*
|
|
|
|
* The default value is false.
|
|
|
|
*/
|
2011-04-28 12:50:31 +02:00
|
|
|
property bool inverted: false
|
2012-12-17 22:02:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* type:bool
|
|
|
|
* How many steps exist while moving the handler. If you want the
|
|
|
|
* ScrollBar buttons to appear you must set this property to a value
|
|
|
|
* bigger than 0.
|
|
|
|
*
|
|
|
|
* The default value is 0.
|
|
|
|
*/
|
2011-04-28 12:50:31 +02:00
|
|
|
property alias stepSize: range.stepSize
|
2012-12-17 22:02:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates if the ScrollBar is pressed.
|
|
|
|
*/
|
2011-11-09 16:53:58 +01:00
|
|
|
property bool pressed: internalLoader.item.mouseArea?internalLoader.item.mouseArea.pressed:false
|
2012-12-17 22:02:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The interval time used by the ScrollBar button to increase or decrease
|
|
|
|
* steps.
|
|
|
|
*/
|
2011-04-29 00:22:51 +02:00
|
|
|
property real scrollButtonInterval: 50
|
2011-04-28 12:50:31 +02:00
|
|
|
|
2012-10-09 19:38:23 +02:00
|
|
|
implicitWidth: internalLoader.isVertical ? (internalLoader.item ? internalLoader.item.implicitWidth : 12) : 200
|
|
|
|
implicitHeight: internalLoader.isVertical ? 200 : (internalLoader.item ? internalLoader.item.implicitHeight : 12)
|
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
|
|
|
|
2011-11-09 14:21:54 +01:00
|
|
|
visible: flickableItem && internalLoader.handleEnabled
|
2011-04-28 12:50:31 +02:00
|
|
|
|
2011-10-28 22:32:45 +02:00
|
|
|
anchors {
|
2012-10-12 14:09:00 +02:00
|
|
|
right: internalLoader.anchorableWithFlickable ? flickableItem.right : undefined
|
|
|
|
left: (orientation == Qt.Vertical) ? undefined : (internalLoader.anchorableWithFlickable ? flickableItem.left : undefined)
|
|
|
|
top: (orientation == Qt.Vertical) ? (internalLoader.anchorableWithFlickable ? flickableItem.top : undefined) : undefined
|
|
|
|
bottom: internalLoader.anchorableWithFlickable ? flickableItem.bottom : undefined
|
2011-10-28 22:32:45 +02:00
|
|
|
}
|
|
|
|
|
2011-11-09 14:21:54 +01:00
|
|
|
Loader {
|
|
|
|
id: internalLoader
|
|
|
|
anchors.fill: parent
|
2011-11-09 17:05:21 +01:00
|
|
|
//property bool handleEnabled: internalLoader.isVertical ? item.handle.height < item.contents.height : item.handle.width < item.contents.width
|
|
|
|
property bool handleEnabled: internalLoader.isVertical ? flickableItem.contentHeight > flickableItem.height : flickableItem.contentWidth > flickableItem.width
|
|
|
|
property bool isVertical: orientation == Qt.Vertical
|
2012-12-09 18:35:48 +01:00
|
|
|
property bool anchorableWithFlickable: scrollbar.parent == flickableItem || scrollbar.parent == flickableItem.parent
|
2011-11-09 14:21:54 +01:00
|
|
|
|
|
|
|
function incrementValue(increment)
|
|
|
|
{
|
|
|
|
if (!flickableItem)
|
|
|
|
return;
|
|
|
|
|
2011-11-09 17:05:21 +01:00
|
|
|
if (internalLoader.isVertical) {
|
2012-06-16 14:55:40 +02:00
|
|
|
flickableItem.contentY = Math.max(0, Math.min(flickableItem.contentHeight - flickableItem.height,
|
2011-11-09 14:21:54 +01:00
|
|
|
flickableItem.contentY + increment))
|
|
|
|
} else {
|
2012-06-16 14:55:40 +02:00
|
|
|
flickableItem.contentX = Math.max(0, Math.min(flickableItem.contentWidth - flickableItem.width,
|
2011-11-09 14:21:54 +01:00
|
|
|
flickableItem.contentX + increment))
|
2011-04-29 00:22:51 +02:00
|
|
|
}
|
|
|
|
}
|
2011-04-28 12:50:31 +02:00
|
|
|
|
2012-07-31 10:36:25 +02:00
|
|
|
|
|
|
|
Connections {
|
|
|
|
target: flickableItem
|
2012-08-01 13:36:22 +02:00
|
|
|
onContentHeightChanged: {
|
|
|
|
range.value = flickableItem.contentY
|
|
|
|
}
|
2012-07-31 10:36:25 +02:00
|
|
|
onContentYChanged: {
|
|
|
|
if (internalLoader.isVertical) {
|
|
|
|
range.value = flickableItem.contentY
|
|
|
|
}
|
|
|
|
}
|
|
|
|
onContentXChanged: {
|
|
|
|
if (!internalLoader.isVertical) {
|
|
|
|
range.value = flickableItem.contentX
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Connections {
|
|
|
|
target: internalLoader.item.handle
|
2012-08-01 11:16:47 +02:00
|
|
|
onYChanged: updateFromHandleTimer.running = true
|
|
|
|
onXChanged: updateFromHandleTimer.running = true
|
2012-07-31 10:36:25 +02:00
|
|
|
}
|
2011-11-09 14:21:54 +01:00
|
|
|
RangeModel {
|
|
|
|
id: range
|
2011-04-29 00:22:51 +02:00
|
|
|
|
2011-11-09 14:21:54 +01:00
|
|
|
minimumValue: 0
|
|
|
|
maximumValue: {
|
|
|
|
var diff;
|
2011-11-09 17:05:21 +01:00
|
|
|
if (internalLoader.isVertical) {
|
2011-11-09 14:21:54 +01:00
|
|
|
diff = flickableItem.contentHeight - flickableItem.height
|
|
|
|
} else {
|
|
|
|
diff = flickableItem.contentWidth - flickableItem.width
|
2011-04-29 00:22:51 +02:00
|
|
|
}
|
2011-04-28 12:50:31 +02:00
|
|
|
|
2011-11-09 14:21:54 +01:00
|
|
|
return Math.max(0, diff)
|
2011-04-29 00:22:51 +02:00
|
|
|
}
|
2011-04-28 12:50:31 +02:00
|
|
|
|
2011-11-09 14:21:54 +01:00
|
|
|
stepSize: 10
|
|
|
|
inverted: scrollbar.inverted
|
|
|
|
positionAtMinimum: 0
|
|
|
|
positionAtMaximum: {
|
2011-11-09 17:05:21 +01:00
|
|
|
if (internalLoader.isVertical) {
|
2011-11-09 14:21:54 +01:00
|
|
|
internalLoader.item.contents.height - internalLoader.item.handle.height
|
|
|
|
} else {
|
|
|
|
internalLoader.item.contents.width - internalLoader.item.handle.width
|
2011-04-29 00:22:51 +02:00
|
|
|
}
|
2011-04-21 17:01:42 +02:00
|
|
|
}
|
2012-07-31 10:36:25 +02:00
|
|
|
|
2011-11-09 14:21:54 +01:00
|
|
|
onValueChanged: {
|
|
|
|
if (flickableItem.moving) {
|
|
|
|
return
|
2011-04-28 12:50:31 +02:00
|
|
|
}
|
|
|
|
|
2011-11-09 17:05:21 +01:00
|
|
|
if (internalLoader.isVertical) {
|
2011-11-09 14:21:54 +01:00
|
|
|
flickableItem.contentY = value
|
|
|
|
} else {
|
|
|
|
flickableItem.contentX = value
|
2011-04-29 00:22:51 +02:00
|
|
|
}
|
|
|
|
}
|
2011-04-28 12:50:31 +02:00
|
|
|
|
|
|
|
|
2011-11-09 14:21:54 +01:00
|
|
|
onPositionChanged: {
|
2011-11-09 15:03:29 +01:00
|
|
|
if (internalLoader.item.mouseArea && internalLoader.item.mouseArea.pressed) {
|
2011-11-09 14:21:54 +01:00
|
|
|
return
|
2011-04-29 00:22:51 +02:00
|
|
|
}
|
2011-04-28 12:50:31 +02:00
|
|
|
|
2011-11-09 17:05:21 +01:00
|
|
|
if (internalLoader.isVertical) {
|
2011-11-09 14:21:54 +01:00
|
|
|
internalLoader.item.handle.y = position
|
|
|
|
} else {
|
|
|
|
internalLoader.item.handle.x = position
|
2011-04-29 00:22:51 +02:00
|
|
|
}
|
2011-04-21 17:01:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 11:16:47 +02:00
|
|
|
Timer {
|
|
|
|
id: updateFromHandleTimer
|
|
|
|
interval: 10
|
|
|
|
onTriggered: {
|
2012-10-09 16:21:21 +02:00
|
|
|
if(!enabled || !interactive)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (internalLoader.isVertical) {
|
2012-08-01 11:16:47 +02:00
|
|
|
range.position = internalLoader.item.handle.y
|
|
|
|
} else {
|
|
|
|
range.position = internalLoader.item.handle.x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-22 16:34:09 +01:00
|
|
|
source: interactive ? "private/ScrollBarDelegate.qml" : "private/ScrollDecoratorDelegate.qml"
|
2011-04-28 12:50:31 +02:00
|
|
|
}
|
|
|
|
}
|