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 Private
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 spacing: Private.FormCardUnits.verticalSpacing
126
127 RowLayout {
128 Layout.fillWidth: true
129 spacing: 0
130
131 QQC2.Label {
132 Layout.fillWidth: true
133 text: label
134 elide: Text.ElideRight
135 color: root.enabled ? Kirigami.Theme.textColor : Kirigami.Theme.disabledTextColor
136 wrapMode: Text.Wrap
137 maximumLineCount: 2
138 }
139
140 Private.SpinButton {
141 onClicked: root.decrease()
142 icon.name: 'arrow-down'
143 visible: Kirigami.Settings.isMobile
144
145 isStart: true
146 isEnd: false
147 }
148
149 QQC2.Pane {
150 focusPolicy: Qt.NoFocus
151 topPadding: 0
152 bottomPadding: 0
153 leftPadding: Kirigami.Units.largeSpacing * 2
154 rightPadding: Kirigami.Units.largeSpacing * 2
155 visible: Kirigami.Settings.isMobile
156 contentItem: QQC2.Label {
157 verticalAlignment: Text.AlignVCenter
158 height: Kirigami.Units.gridUnit * 2
159 text: root.textFromValue(root.value, root.locale)
160 }
161 background: Item {
162 implicitHeight: Kirigami.Units.gridUnit * 2
163 Rectangle {
164 color: Kirigami.ColorUtils.linearInterpolation(Kirigami.Theme.backgroundColor, Kirigami.Theme.textColor, Kirigami.Theme.frameContrast)
165 height: 1
166 anchors {
167 left: parent.left
168 right: parent.right
169 top: parent.top
170 }
171 }
172
173 Rectangle {
174 color: Kirigami.ColorUtils.linearInterpolation(Kirigami.Theme.backgroundColor, Kirigami.Theme.textColor, Kirigami.Theme.frameContrast)
175 height: 1
176 anchors {
177 left: parent.left
178 right: parent.right
179 bottom: parent.bottom
180 }
181 }
182 }
183 }
184
185 Private.SpinButton {
186 onClicked: root.increase()
187 visible: Kirigami.Settings.isMobile
188 icon.name: 'arrow-up'
189
190 isStart: false
191 isEnd: true
192 }
193 }
194
195 QQC2.SpinBox {
196 id: spinbox
197 Layout.fillWidth: true
198 visible: !Kirigami.Settings.isMobile
199 locale: root.locale
200 }
201
202 Kirigami.InlineMessage {
203 id: formErrorHandler
204 visible: root.statusMessage.length > 0
205 Layout.topMargin: visible ? Kirigami.Units.smallSpacing : 0
206 Layout.fillWidth: true
207 text: root.statusMessage
208 type: root.status
209 }
210 }
211}
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:05:53 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.