Skip to content

Commit

Permalink
Implement sliders
Browse files Browse the repository at this point in the history
  • Loading branch information
vicr123 committed Nov 16, 2024
1 parent c2f14f8 commit 5304c68
Show file tree
Hide file tree
Showing 4 changed files with 333 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ qt_add_qml_module(contemporary-corecontrols
Label.qml
ProgressBar.qml
ComboBox.qml
Slider.qml
PLUGIN_TARGET contemporary-corecontrols-plugin
NO_GENERATE_PLUGIN_SOURCE
NO_PLUGIN_OPTIONAL
Expand Down
129 changes: 129 additions & 0 deletions controls/com/vicr123/Contemporary/CoreStyles/Slider.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import QtQuick
import QtQuick.Controls.impl
import QtQuick.Templates as T
import Contemporary
import com.vicr123.Contemporary.impl

T.Slider {
id: control

implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset, implicitHandleWidth + leftPadding + rightPadding)
implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset, implicitHandleHeight + topPadding + bottomPadding)

padding: 6

readonly property var foreground: control.enabled ? Contemporary.foreground : Contemporary.disabled(Contemporary.foreground)
readonly property var accent: control.enabled ? Contemporary.accent : Contemporary.disabled(Contemporary.accent)
readonly property int thickness: 24

handle: Item {
x: control.leftPadding + (control.horizontal ? control.visualPosition * (control.availableWidth - width) : (control.availableWidth - width) / 2)
y: control.topPadding + (control.horizontal ? (control.availableHeight - height) / 2 : control.visualPosition * (control.availableHeight - height))
implicitWidth: control.thickness
implicitHeight: control.thickness

Rectangle {
id: thumb
property int adjustment: 0

anchors.centerIn: parent
implicitWidth: control.thickness - adjustment
implicitHeight: control.thickness - adjustment

Behavior on adjustment {
NumberAnimation {
duration: 50
easing.type: Easing.OutCubic
}
}

states: [
State {
name: "PRESSED"
when: control.pressed
PropertyChanges {
target: thumb
color: Contemporary.pressed(control.foreground)
adjustment: 6
}
},
State {
name: "RELEASED"
when: !control.pressed
PropertyChanges {
target: thumb
color: control.foreground
adjustment: 0
}
}
]

transitions: [
Transition {
from: "PRESSED"
to: "RELEASED"
ColorAnimation {
target: thumb
properties: "color"
duration: 100
easing.type: Easing.OutCubic
}
NumberAnimation {
target: thumb
properties: "adjustment"
duration: 100
easing.type: Easing.OutCubic
}
},
Transition {
from: "RELEASED"
to: "PRESSED"
ColorAnimation {
target: thumb
properties: "color"
duration: 100
easing.type: Easing.OutCubic
}
NumberAnimation {
target: thumb
properties: "adjustment"
duration: 100
easing.type: Easing.OutCubic
}
}
]

radius: 4
color: control.foreground
}

FocusDecoration {
anchors.fill: parent
focused: control.visualFocus
radius: 4
}
}

background: Rectangle {
x: control.leftPadding + (control.horizontal ? 0 : (control.availableWidth - width) / 2)
y: control.topPadding + (control.horizontal ? (control.availableHeight - height) / 2 : 0)
implicitWidth: control.horizontal ? 200 : control.thickness
implicitHeight: control.horizontal ? control.thickness : 200
width: control.horizontal ? control.availableWidth : implicitWidth
height: control.horizontal ? implicitHeight : control.availableHeight
radius: 4
color: "transparent"
scale: control.horizontal && control.mirrored ? -1 : 1
border.color: control.foreground

Rectangle {
y: control.horizontal ? 0 : control.visualPosition * (parent.height - control.thickness)
width: control.horizontal ? control.thickness + control.position * (parent.width - control.thickness) : control.thickness
height: control.horizontal ? control.thickness : control.thickness + control.position * (parent.height - control.thickness)

radius: 4
color: control.accent
border.color: control.foreground
}
}
}
122 changes: 91 additions & 31 deletions playground/components/Ranges.qml
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,130 @@ import com.vicr123.Contemporary
import Contemporary

Control {
id: rectangle
id: root

property int value: 50

LayerCalculator {
id: layer2
layer: 2
}

Grandstand {
id: grandstand
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right

id: grandstand

innerTopMargin: SafeZone.top

z: 10
text: "Ranges";
text: "Ranges"
color: layer2.color
}

ColumnLayout {
anchors.top: grandstand.bottom
anchors.left: parent.left
anchors.right: parent.right
anchors.bottomMargin: SafeZone.bottom
anchors.topMargin: 6

RowLayout {
SpinBox {
id: spin1
from: 0
to: 100
value: 50
onValueChanged: slider1.value = spin1.value
}
GroupBox {
title: qsTr("Spin Boxes")
implicitWidth: Math.min(600, parent.width - 12)

Layout.alignment: Qt.AlignHCenter | Qt.AlignTop

ColumnLayout {
anchors.fill: parent

SpinBox {
id: spin1
from: 0
to: 100
value: root.value
onValueChanged: root.value = spin1.value
}

SpinBox {
from: 0
to: items.length - 1
value: 1 // "Medium"

property var items: ["Small", "Medium", "Large"]

Slider {
id: slider1
from: 0
to: 100
value: 50
onValueChanged: spin1.value = slider1.value;
textFromValue: function (value) {
return items[value];
}

valueFromText: function (text) {
for (var i = 0; i < items.length; ++i) {
if (items[i].toLowerCase().indexOf(text.toLowerCase()) === 0)
return i;
}
return sb.value;
}
}
}
}

GroupBox {
title: qsTr("Horizontal Sliders")
implicitWidth: Math.min(600, parent.width - 12)

SpinBox {
from: 0
to: items.length - 1
value: 1 // "Medium"
Layout.alignment: Qt.AlignHCenter | Qt.AlignTop

property var items: ["Small", "Medium", "Large"]
ColumnLayout {
anchors.fill: parent

textFromValue: function(value) {
return items[value];
}
Slider {
id: slider1
from: 0
to: 100
value: root.value
onValueChanged: root.value = slider1.value
}

valueFromText: function(text) {
for (var i = 0; i < items.length; ++i) {
if (items[i].toLowerCase().indexOf(text.toLowerCase()) === 0)
return i
Slider {
id: slider2
from: 0
to: 100
value: root.value
onValueChanged: root.value = slider2.value
enabled: false
}
return sb.value
}
}

GroupBox {
title: qsTr("Vertical Sliders")
implicitWidth: Math.min(600, parent.width - 12)

Layout.alignment: Qt.AlignHCenter | Qt.AlignTop

RowLayout {
anchors.fill: parent

Slider {
id: slider3
from: 0
to: 100
value: root.value
onValueChanged: root.value = slider3.value
orientation: Qt.Vertical
}

Slider {
id: slider4
from: 0
to: 100
value: root.value
onValueChanged: root.value = slider4.value
enabled: false
orientation: Qt.Vertical
}
}
}
}
}
Loading

0 comments on commit 5304c68

Please sign in to comment.