Kirigami-addons

FormSpinBoxDelegate.qml
1// Copyright 2023 Carl Schwan <carl@carlschwan.eu>
2// SPDX-License-Identifier: LGPL-2.0-or-later
3
4import QtQuick
5import QtQuick.Controls as QQC2
6import QtQuick.Layouts
7
8import org.kde.kirigami as Kirigami
9import 'private' as P
10
11/**
12 * @brief A Form delegate that corresponds to a spinbox.
13 *
14 * This component is used to select a number. By default, the spinbox will be
15 * initialized with a minimum of 0 and a maximum of 99.
16 *
17 * Example code:
18 * ```qml
19 * FormCard.FormCardHeader {
20 * title: "Information"
21 * }
22 *
23 * FormCard.FormCard {
24 * FormCard.FormSpinBoxDelegate {
25 * label: "Amount"
26 * }
27 * }
28 * ```
29 *
30 * @since KirigamiAddons 0.11.0
31 *
32 * @inherit AbstractFormDelegate
33 */
34AbstractFormDelegate {
35 id: root
36
37 /**
38 * @brief A label that appears above the spinbox.
39 *
40 */
41 required property string label
42
43 /**
44 * @brief This property holds the `value` of the internal spinbox.
45 */
46 property alias value: spinbox.value
47
48 /**
49 * @brief This property holds the `from` of the internal spinbox.
50 */
51 property alias from: spinbox.from
52
53 /**
54 * @brief This property holds the `to` of the internal spinbox.
55 */
56 property alias to: spinbox.to
58 /**
59 * @brief This property holds the `stepSize` of the internal spinbox.
60 */
61 property alias stepSize: spinbox.stepSize
62
63 /**
64 * @brief This property holds the `textFromValue` of the internal spinbox.
65 */
66 property alias textFromValue: spinbox.textFromValue
67
68 /**
69 * @brief This property holds the `valueFromText` of the internal spinbox.
70 */
71 property alias valueFromText: spinbox.valueFromText
72
73 /**
74 * @brief This property holds the `displayText` of the internal spinbox.
75 */
76 property alias displayText: spinbox.displayText
77
78 /**
79 * @brief This property holds the `validator` of the internal spinbox.
80 */
81 property alias validator: spinbox.validator
82
83 /**
84 * @brief This property holds the current type of status displayed in
85 * the text field.
86 *
87 * Depending on the status of the text field, the statusMessage property
88 * will look different
89 *
90 * Accepted values:
91 * - Kirigami.MessageType.Information
92 * - Kirigami.MessageType.Positive
93 * - Kirigami.MessageType.Warning
94 * - Kirigami.MessageType.Error
95 *
96 * @see Kirigami.MessageType
97 */
98 property var status: Kirigami.MessageType.Information
99
100 /**
101 * This property holds the current status message of the text field.
102 */
103 property string statusMessage: ""
104
105 /**
106 * Increases the value by stepSize, or 1 if stepSize is not defined.
107 */
108 function increase() {
109 spinbox.increase();
110 }
111
112 /**
113 * Decreases the value by stepSize, or 1 if stepSize is not defined.
114 */
115 function decrease() {
116 spinbox.decrease();
117 }
118
119 focusPolicy: Kirigami.Settings.isMobile ? Qt.StrongFocus : Qt.NoFocus
120
121 onClicked: spinbox.forceActiveFocus()
122 background: null
123
124 contentItem: ColumnLayout {
125 RowLayout {
126 Layout.fillWidth: true
127 spacing: 0
128
129 QQC2.Label {
130 Layout.fillWidth: true
131 text: label
132 elide: Text.ElideRight
133 color: root.enabled ? Kirigami.Theme.textColor : Kirigami.Theme.disabledTextColor
134 wrapMode: Text.Wrap
135 maximumLineCount: 2
136 }
137
138 P.SpinButton {
139 onClicked: root.decrease()
140 icon.name: 'arrow-down'
141 visible: Kirigami.Settings.isMobile
142
143 isStart: true
144 isEnd: false
145 }
146
147 QQC2.Pane {
148 focusPolicy: Qt.NoFocus
149 topPadding: 0
150 bottomPadding: 0
151 leftPadding: Kirigami.Units.largeSpacing * 2
152 rightPadding: Kirigami.Units.largeSpacing * 2
153 visible: Kirigami.Settings.isMobile
154 contentItem: QQC2.Label {
155 verticalAlignment: Text.AlignVCenter
156 height: Kirigami.Units.gridUnit * 2
157 text: root.textFromValue(root.value, root.locale)
158 }
159 background: Item {
160 implicitHeight: Kirigami.Units.gridUnit * 2
161 Rectangle {
162 color: Kirigami.ColorUtils.linearInterpolation(Kirigami.Theme.backgroundColor, Kirigami.Theme.textColor, Kirigami.Theme.frameContrast)
163 height: 1
164 anchors {
165 left: parent.left
166 right: parent.right
167 top: parent.top
168 }
169 }
170
171 Rectangle {
172 color: Kirigami.ColorUtils.linearInterpolation(Kirigami.Theme.backgroundColor, Kirigami.Theme.textColor, Kirigami.Theme.frameContrast)
173 height: 1
174 anchors {
175 left: parent.left
176 right: parent.right
177 bottom: parent.bottom
178 }
179 }
180 }
181 }
182
183 P.SpinButton {
184 onClicked: root.increase()
185 visible: Kirigami.Settings.isMobile
186 icon.name: 'arrow-up'
187
188 isStart: false
189 isEnd: true
190 }
191 }
192
193 QQC2.SpinBox {
194 id: spinbox
195 Layout.fillWidth: true
196 visible: !Kirigami.Settings.isMobile
197 locale: root.locale
198 }
199
200 Kirigami.InlineMessage {
201 id: formErrorHandler
202 visible: root.statusMessage.length > 0
203 Layout.topMargin: visible ? Kirigami.Units.smallSpacing : 0
204 Layout.fillWidth: true
205 text: root.statusMessage
206 type: root.status
207 }
208 }
209}
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:10:34 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.