Repository navigation
Expand file tree
/
Copy pathCompactField.qml
More file actions
132 lines (116 loc) · 3.44 KB
/
Copy pathCompactField.qml
File metadata and controls
132 lines (116 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import QtQuick
import QtQuick.Controls as QQC
import qs.Commons
import qs.Ui
Column {
id: root
property string label: ""
property int value: 0
property int from: 0
property int to: 100
property int stepSize: 1
property color foreground: Color.foreground
property color accent: Color.accent
property string fontFamily: Style.font.family
property real fontSize: Style.font.body
property real fieldWidth: Style.spacing.numberFieldWidth
property bool hasCursor: false
property bool hovered: false
property bool canceling: false
property alias field: spin
signal modified(int value)
signal editingDone()
signal navigationRequested(int direction)
signal hoveredChangedByPointer(bool hovered)
function beginEditing() {
spin.forceActiveFocus()
editor.selectAll()
}
function commitText(text) {
var parsed = parseInt(String(text), 10)
if (isNaN(parsed)) parsed = root.value
parsed = Math.max(root.from, Math.min(root.to, parsed))
root.modified(parsed)
}
spacing: Style.spacing.md
Text {
visible: root.label !== ""
text: root.label
color: Qt.darker(root.foreground, 1.4)
font.family: root.fontFamily
font.pixelSize: Style.font.bodySmall
}
QQC.SpinBox {
id: spin
width: root.fieldWidth
implicitHeight: Math.max(Style.spacing.controlHeight, root.fontSize + Style.spacing.controlPaddingY * 2)
from: root.from
to: root.to
stepSize: root.stepSize
value: root.value
editable: true
activeFocusOnTab: true
font.family: root.fontFamily
font.pixelSize: root.fontSize
readonly property var _borderSpec: Border.controlSpec(
activeFocus ? "focus" : (root.hovered || root.hasCursor ? "hover-cursor" : "normal"),
root.foreground,
root.accent
)
leftPadding: Border.left(_borderSpec) + Style.spacing.controlPaddingX
rightPadding: Border.right(_borderSpec) + Style.spacing.controlPaddingX
topPadding: Border.top(_borderSpec)
bottomPadding: Border.bottom(_borderSpec)
onValueModified: root.modified(value)
background: BorderSurface {
color: Style.controlFill(spin.activeFocus, root.hovered || root.hasCursor, root.foreground, root.accent)
borderSpec: spin._borderSpec
radius: Style.cornerRadius
HoverHandler {
onHoveredChanged: {
root.hovered = hovered
root.hoveredChangedByPointer(hovered)
}
}
}
contentItem: TextInput {
id: editor
text: spin.displayText
font: spin.font
color: root.foreground
selectionColor: Style.selectionFillFor(root.foreground, root.accent)
selectedTextColor: root.foreground
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
readOnly: !spin.editable
validator: spin.validator
inputMethodHints: Qt.ImhFormattedNumbersOnly
selectByMouse: true
onEditingFinished: {
if (!root.canceling) root.commitText(text)
root.canceling = false
}
Keys.onReturnPressed: {
focus = false
root.editingDone()
}
Keys.onEnterPressed: {
focus = false
root.editingDone()
}
Keys.onEscapePressed: {
root.canceling = true
focus = false
root.editingDone()
}
Keys.onTabPressed: {
focus = false
root.navigationRequested(1)
}
Keys.onBacktabPressed: {
focus = false
root.navigationRequested(-1)
}
}
}
}