Commit 242b946b authored by Jens Bache-Wiig's avatar Jens Bache-Wiig Committed by Daniel Molkentin
Browse files

Updated scrollbar with pagestep behavior

Change-Id: I67b78d878fbf7f895231b5e98f6fe0dc5ed8b8e0
Reviewed-on: http://codereview.qt.nokia.com/1565

Reviewed-by: default avatarQt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: default avatarDaniel Molkentin <daniel.molkentin@nokia.com>
parent d557bb81
......@@ -32,31 +32,36 @@
import QtQuick 1.0
import "custom" as Components
import "plugin"
Item {
id: scrollbar
property bool upPressed
property bool downPressed
property int orientation : Qt.Horizontal
property alias minimumValue: slider.minimumValue
property alias maximumValue: slider.maximumValue
property int pageStep: styleitem.horizontal ? width : height
property int singleStep: 20
property alias value: slider.value
property bool scrollToClickposition: styleitem.styleHint("scrollToClickPosition")
width: orientation == Qt.Horizontal ? 200 : internal.scrollbarExtent
height: orientation == Qt.Horizontal ? internal.scrollbarExtent : 200
onValueChanged: internal.updateHandle()
// onMaximumValueChanged: internal.updateHandle()
// onMinimumValueChanged: internal.updateHandle()
MouseArea {
id: internal
anchors.fill: parent
property bool upPressed
property bool downPressed
property bool pageUpPressed
property bool pageDownPressed
property bool autoincrement: false
property int scrollbarExtent : styleitem.pixelMetric("scrollbarExtent");
property bool handlePressed
// Update hover item
onEntered: styleitem.activeControl = styleitem.hitTest(mouseX, mouseY)
......@@ -64,8 +69,14 @@ Item {
onMouseXChanged: styleitem.activeControl = styleitem.hitTest(mouseX, mouseY)
hoverEnabled: true
property variant control
property variant pressedX
property variant pressedY
property int oldPosition
property int grooveSize
Timer {
running: upPressed || downPressed
running: internal.upPressed || internal.downPressed || internal.pageUpPressed || internal.pageDownPressed
interval: 350
onTriggered: internal.autoincrement = true
}
......@@ -74,37 +85,81 @@ Item {
running: internal.autoincrement
interval: 60
repeat: true
onTriggered: upPressed ? internal.decrement() : internal.increment()
onTriggered: internal.upPressed ? internal.decrement() : internal.downPressed ? internal.increment() :
internal.pageUpPressed ? internal.decrementPage() :
internal.incrementPage()
}
onMousePositionChanged: {
if (pressed && control === "handle") {
//slider.positionAtMaximum = grooveSize
if (!styleitem.horizontal)
slider.position = oldPosition + (mouseY - pressedY)
else
slider.position = oldPosition + (mouseX - pressedX)
}
}
onPressed: {
var control = styleitem.hitTest(mouseX,mouseY)
if (control == "up") {
control = styleitem.hitTest(mouseX,mouseY)
scrollToClickposition = styleitem.styleHint("scrollToClickPosition")
grooveSize = styleitem.horizontal? styleitem.subControlRect("groove").width -
styleitem.subControlRect("handle").width:
styleitem.subControlRect("groove").height -
styleitem.subControlRect("handle").height;
if (control == "handle") {
pressedX = mouseX
pressedY = mouseY
oldPosition = slider.position
} else if (control == "up") {
decrement();
upPressed = true
} else if (control == "down") {
increment();
downPressed = true
} else if (!scrollToClickposition){
if (control == "upPage") {
decrementPage();
pageUpPressed = true
} else if (control == "downPage") {
incrementPage();
pageDownPressed = true
}
} else {
slider.position = styleitem.horizontal ? mouseX - handleRect.width/2
: mouseY - handleRect.height/2
}
}
onReleased: {
autoincrement = false;
if (upPressed) {
upPressed = false;
decrement()
} else if (downPressed) {
increment()
downPressed = false;
}
upPressed = false;
downPressed = false;
pageUpPressed = false
pageDownPressed = false
control = ""
}
function incrementPage() {
value += pageStep
if (value > maximumValue)
value = maximumValue
}
function decrementPage() {
value -= pageStep
if (value < minimumValue)
value = minimumValue
}
function increment() {
value += 30
value += singleStep
if (value > maximumValue)
value = maximumValue
}
function decrement() {
value -= 30
value -= singleStep
if (value < minimumValue)
value = minimumValue
}
......@@ -115,7 +170,7 @@ Item {
elementType: "scrollbar"
hover: activeControl != "none"
activeControl: "none"
sunken: upPressed | downPressed
sunken: internal.upPressed | internal.downPressed
minimum: slider.minimumValue
maximum: slider.maximumValue
value: slider.value
......@@ -124,35 +179,20 @@ Item {
}
property variant handleRect: Qt.rect(0,0,0,0)
property variant grooveRect: Qt.rect(0,0,0,0)
function updateHandle() {
internal.handleRect = styleitem.subControlRect("handle")
var grooveRect = styleitem.subControlRect("groove");
var extra = 0
if (orientation == Qt.Vertical) {
slider.anchors.topMargin = grooveRect.y + extra
slider.anchors.bottomMargin = height - grooveRect.y - grooveRect.height + extra
} else {
slider.anchors.leftMargin = grooveRect.x + extra
slider.anchors.rightMargin = width - grooveRect.x - grooveRect.width + extra
}
grooveRect = styleitem.subControlRect("groove");
}
Components.Slider {
RangeModel {
id: slider
hoverEnabled: false // Handled by the scrollbar background
orientation: scrollbar.orientation
anchors.fill: parent
leftMargin: (orientation === Qt.Horizontal) ? internal.handleRect.width / 2 : internal.handleRect.height / 2
rightMargin: leftMargin
handle: Item {
width: orientation == Qt.Vertical ? internal.handleRect.height : internal.handleRect.width;
height: orientation == Qt.Vertical ? internal.handleRect.width : internal.handleRect.height
}
groove:null
containsMouse: false
valueIndicator:null
inverted:orientation != Qt.Horizontal
minimumValue: 0.0
maximumValue: 1.0
value: 0
stepSize: 0.0
inverted: false
positionAtMaximum: internal.grooveSize
}
}
}
......@@ -2,7 +2,7 @@
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (info@qt.nokia.com)
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
......@@ -302,7 +302,21 @@ void QStyleItem::initStyleOption()
opt->maximum = maximum();
// ### fixme - workaround for KDE inverted dial
opt->sliderPosition = value();
opt->tickInterval = opt->maximum != opt->minimum ? 1200 / (opt->maximum - opt->minimum) : 0;
opt->singleStep = step();
if (opt->singleStep)
{
qreal numOfSteps = (opt->maximum - opt->minimum) / opt->singleStep;
// at least 5 pixels between tick marks
if (numOfSteps && (width() / numOfSteps < 5))
opt->tickInterval = qRound((5*numOfSteps / width()) + 0.5)*step();
else
opt->tickInterval = opt->singleStep;
}
else // default Qt-components implementation
opt->tickInterval = opt->maximum != opt->minimum ? 1200 / (opt->maximum - opt->minimum) : 0;
if (style() == QLatin1String("oxygen") && type == QLatin1String("dial"))
opt->sliderValue = maximum() - value();
else
......@@ -487,19 +501,21 @@ QString QStyleItem::hitTest(int px, int py)
}
break;
case ScrollBar: {
subcontrol = qApp->style()->hitTestComplexControl(QStyle::CC_ScrollBar,
qstyleoption_cast<QStyleOptionComplex*>(m_styleoption),
QPoint(px,py), 0);
if (subcontrol == QStyle::SC_ScrollBarSlider)
return "handle";
if (subcontrol == QStyle::SC_ScrollBarSubLine
|| subcontrol == QStyle::SC_ScrollBarSubPage)
return "up";
if (subcontrol == QStyle::SC_ScrollBarAddLine
|| subcontrol == QStyle::SC_ScrollBarAddPage)
return "down";
subcontrol = qApp->style()->hitTestComplexControl(QStyle::CC_ScrollBar,
qstyleoption_cast<QStyleOptionComplex*>(m_styleoption),
QPoint(px,py), 0);
if (subcontrol == QStyle::SC_ScrollBarSlider)
return "handle";
if (subcontrol == QStyle::SC_ScrollBarSubLine)
return "up";
else if (subcontrol == QStyle::SC_ScrollBarSubPage)
return "upPage";
if (subcontrol == QStyle::SC_ScrollBarAddLine)
return "down";
else if (subcontrol == QStyle::SC_ScrollBarAddPage)
return "downPage";
}
break;
default:
......@@ -621,15 +637,15 @@ QVariant QStyleItem::styleHint(const QString &metric)
return qApp->palette().text().color().name();
} else if (metric == "focuswidget") {
return qApp->style()->styleHint(QStyle::SH_FocusFrame_AboveWidget);
} else if (metric == "tabbaralignment") {
int result = qApp->style()->styleHint(QStyle::SH_TabBar_Alignment);
if (result == Qt::AlignCenter)
return "center";
return "left";
} else if (metric == "framearoundcontents")
} else if (metric == "framearoundcontents") {
return qApp->style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents);
} else if (metric == "scrollToClickPosition")
return qApp->style()->styleHint(QStyle::SH_ScrollBar_LeftClickAbsolutePosition);
return 0;
}
......
......@@ -2,7 +2,7 @@
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (info@qt.nokia.com)
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
......@@ -70,6 +70,7 @@ class QStyleItem: public QDeclarativeItem
Q_PROPERTY( int minimum READ minimum WRITE setMinimum NOTIFY minimumChanged)
Q_PROPERTY( int maximum READ maximum WRITE setMaximum NOTIFY maximumChanged)
Q_PROPERTY( int value READ value WRITE setValue NOTIFY valueChanged)
Q_PROPERTY( int step READ step WRITE setStep NOTIFY stepChanged)
Q_PROPERTY( int paintMargins READ paintMargins WRITE setPaintMargins NOTIFY paintMarginsChanged)
Q_PROPERTY( QString fontFamily READ fontFamily)
......@@ -122,6 +123,7 @@ public:
int minimum() const { return m_minimum; }
int maximum() const { return m_maximum; }
int step() const { return m_step; }
int value() const { return m_value; }
int paintMargins() const { return m_paintMargins; }
......@@ -144,6 +146,7 @@ public:
void setMinimum(int minimum) { if (m_minimum!= minimum) {m_minimum = minimum; emit minimumChanged();}}
void setMaximum(int maximum) { if (m_maximum != maximum) {m_maximum = maximum; emit maximumChanged();}}
void setValue(int value) { if (m_value!= value) {m_value = value; emit valueChanged();}}
void setStep(int step) { if (m_step != step) { m_step = step; emit stepChanged(); }}
void setPaintMargins(int value) {
Q_UNUSED(value)
#ifdef Q_WS_WIN //only vista style needs this hack
......@@ -189,6 +192,7 @@ Q_SIGNALS:
void horizontalChanged();
void minimumChanged();
void maximumChanged();
void stepChanged();
void valueChanged();
void activeControlChanged();
void infoChanged();
......@@ -223,6 +227,7 @@ protected:
int m_minimum;
int m_maximum;
int m_value;
int m_step;
int m_paintMargins;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment