Changeset View
Changeset View
Standalone View
Standalone View
src/declarativeimports/plasmacomponents3/SpinBox.qml
1 | 1 | | |||
---|---|---|---|---|---|
2 | 2 | | |||
3 | import QtQuick 2.6 | 3 | import QtQuick 2.14 | ||
4 | import QtQuick.Controls @QQC2_VERSION@ | 4 | import QtQuick.Controls @QQC2_VERSION@ | ||
5 | import QtQuick.Templates @QQC2_VERSION@ as T | 5 | import QtQuick.Templates @QQC2_VERSION@ as T | ||
6 | import org.kde.plasma.core 2.0 as PlasmaCore | 6 | import org.kde.plasma.core 2.0 as PlasmaCore | ||
7 | import "private" as Private | 7 | import "private" as Private | ||
8 | 8 | | |||
9 | T.SpinBox { | 9 | T.SpinBox { | ||
10 | id: control | 10 | id: control | ||
11 | 11 | | |||
12 | implicitWidth: Math.max(units.gridUnit * 6, contentItem.implicitWidth + 2 * padding + up.indicator.implicitWidth + down.indicator.implicitWidth) | 12 | implicitWidth: Math.max(units.gridUnit * 6, contentItem.implicitWidth + 2 * padding + up.indicator.implicitWidth + down.indicator.implicitWidth) | ||
13 | implicitHeight: units.gridUnit * 1.6 | 13 | implicitHeight: units.gridUnit * 1.6 | ||
14 | 14 | | |||
15 | padding: 6 | 15 | padding: 6 | ||
16 | leftPadding: padding + height | 16 | leftPadding: padding + height | ||
17 | rightPadding: padding + height | 17 | rightPadding: padding + height | ||
18 | 18 | | |||
19 | validator: IntValidator { | 19 | validator: IntValidator { | ||
20 | locale: control.locale.name | 20 | locale: control.locale.name | ||
21 | bottom: Math.min(control.from, control.to) | 21 | bottom: Math.min(control.from, control.to) | ||
22 | top: Math.max(control.from, control.to) | 22 | top: Math.max(control.from, control.to) | ||
23 | } | 23 | } | ||
24 | 24 | | |||
25 | contentItem: TextInput { | 25 | contentItem: TextInput { | ||
26 | id: textField | ||||
27 | property int originalValue | ||||
28 | | ||||
26 | text: control.textFromValue(control.value, control.locale) | 29 | text: control.textFromValue(control.value, control.locale) | ||
27 | opacity: control.enabled ? 1 : 0.6 | 30 | opacity: control.enabled ? 1 : 0.6 | ||
28 | 31 | | |||
29 | font: control.font | 32 | font: control.font | ||
30 | color: theme.viewTextColor | 33 | color: theme.viewTextColor | ||
31 | selectionColor: theme.highlightColor | 34 | selectionColor: theme.highlightColor | ||
32 | selectedTextColor: theme.selectedTextColor | 35 | selectedTextColor: theme.selectedTextColor | ||
33 | horizontalAlignment: Qt.AlignHCenter | 36 | horizontalAlignment: Qt.AlignHCenter | ||
34 | verticalAlignment: Qt.AlignVCenter | 37 | verticalAlignment: Qt.AlignVCenter | ||
35 | 38 | | |||
36 | readOnly: !control.editable | 39 | readOnly: !control.editable | ||
37 | validator: control.validator | 40 | validator: control.validator | ||
38 | inputMethodHints: Qt.ImhFormattedNumbersOnly | 41 | inputMethodHints: Qt.ImhFormattedNumbersOnly | ||
42 | | ||||
43 | // Allow adjusting the value by scrolling over it | ||||
44 | MouseArea { | ||||
45 | anchors.fill: parent | ||||
46 | | ||||
47 | // Because we're stomping the Text Input's own mouse handling | ||||
48 | cursorShape: Qt.IBeamCursor | ||||
49 | | ||||
50 | // Have to cache the original value for the drag handler | ||||
51 | onPressed: { | ||||
52 | textField.originalValue = control.value | ||||
cblack: `control.value` is already a numeric type as `control` is a SpinBox, so you don't need to… | |||||
53 | // Because we're stomping the Text Input's own mouse handling | ||||
I think you would need to explicitly call this on control? control.valueModified(). cblack: I think you would need to explicitly call this on `control`? `control.valueModified()`. | |||||
54 | mouse.accepted = false | ||||
You can do mouse.accepted = false instead of forceActiveFocus to allow the mouse press to go to items below it. However, you might instead want to use DragHandler's onActiveChanged for this, as that signals when the actual drag is active. ahiemstra: You can do `mouse.accepted = false` instead of forceActiveFocus to allow the mouse press to go… | |||||
55 | } | ||||
cblack: `control.value =- control.stepSize` | |||||
56 | onWheel: { | ||||
cblack: `control.valueModified()`. | |||||
57 | if (wheel.angleDelta.y > 0 && control.value <= control.to) { | ||||
58 | control.value += control.stepSize | ||||
59 | control.valueModified() | ||||
60 | } else if (wheel.angleDelta.y < 0 && control.value >= control.from) { | ||||
61 | control.value -= control.stepSize | ||||
62 | control.valueModified() | ||||
63 | } | ||||
64 | } | ||||
65 | } | ||||
66 | | ||||
67 | // Allow adjusting the value by dragging it | ||||
68 | DragHandler { | ||||
69 | // How many pixels you have to drag to change the value by one unit | ||||
70 | // of 'control.stepSize'; bigger magnitudes will require more movement | ||||
71 | // to achieve the same change in spinbox value | ||||
72 | property int magnitude: units.gridUnit | ||||
73 | | ||||
74 | target: null // Don't actually move anything, we just want drag data | ||||
75 | xAxis.enabled: true | ||||
76 | yAxis.enabled: true | ||||
77 | | ||||
78 | onTranslationChanged: { | ||||
79 | // Allow dragging along both X and Y axis, and use whichever one | ||||
80 | // is bigger | ||||
81 | var distance | ||||
82 | if (Math.abs(translation.y) > Math.abs(translation.x)) { | ||||
83 | // Invert the value since the origin is in the top left corner, | ||||
84 | // but we want dragging up make the value bigger | ||||
85 | distance = -translation.y | ||||
86 | } else { | ||||
87 | distance = translation.x | ||||
88 | } | ||||
89 | control.value = textField.originalValue + (Math.floor(distance / magnitude) * control.stepSize) | ||||
90 | control.valueModified() | ||||
91 | } | ||||
92 | } | ||||
39 | } | 93 | } | ||
40 | 94 | | |||
41 | up.indicator: Item { | 95 | up.indicator: Item { | ||
42 | x: control.mirrored ? 0 : parent.width - width | 96 | x: control.mirrored ? 0 : parent.width - width | ||
43 | implicitHeight: parent.height | 97 | implicitHeight: parent.height | ||
44 | implicitWidth: implicitHeight | 98 | implicitWidth: implicitHeight | ||
45 | PlasmaCore.FrameSvgItem { | 99 | PlasmaCore.FrameSvgItem { | ||
46 | anchors { | 100 | anchors { | ||
▲ Show 20 Lines • Show All 43 Lines • Show Last 20 Lines |
control.value is already a numeric type as control is a SpinBox, so you don't need to parseInt here. control.value =+ control.stepSize