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 current type of status displayed in
80 * the text field.
81 *
82 * Depending on the status of the text field, the statusMessage property
83 * will look different
84 *
85 * Accepted values:
86 * - Kirigami.MessageType.Information
87 * - Kirigami.MessageType.Positive
88 * - Kirigami.MessageType.Warning
89 * - Kirigami.MessageType.Error
90 *
91 * @see Kirigami.MessageType
92 */
93 property var status: Kirigami.MessageType.Information
94
95 /**
96 * This property holds the current status message of the text field.
97 */
98 property string statusMessage: ""
99
100 /**
101 * Increases the value by stepSize, or 1 if stepSize is not defined.
102 */
103 function increase() {
104 spinbox.increase();
105 }
106
107 /**
108 * Decreases the value by stepSize, or 1 if stepSize is not defined.
109 */
110 function decrease() {
111 spinbox.decrease();
112 }
113
114 focusPolicy: Kirigami.Settings.isMobile ? Qt.StrongFocus : Qt.NoFocus
115
116 onClicked: spinbox.forceActiveFocus()
117 background: null
118
119 contentItem: ColumnLayout {
120 RowLayout {
121 Layout.fillWidth: true
122 spacing: 0
123
124 QQC2.Label {
125 Layout.fillWidth: true
126 text: label
127 elide: Text.ElideRight
128 color: root.enabled ? Kirigami.Theme.textColor : Kirigami.Theme.disabledTextColor
129 wrapMode: Text.Wrap
130 maximumLineCount: 2
131 }
132
133 P.SpinButton {
134 onClicked: root.decrease()
135 icon.name: 'list-remove-symbolic'
136 visible: Kirigami.Settings.isMobile
137
138 isStart: true
139 isEnd: false
140 }
141
142 QQC2.Pane {
143 focusPolicy: Qt.NoFocus
144 topPadding: 0
145 bottomPadding: 0
146 leftPadding: Kirigami.Units.largeSpacing * 2
147 rightPadding: Kirigami.Units.largeSpacing * 2
148 visible: Kirigami.Settings.isMobile
149 contentItem: QQC2.Label {
150 verticalAlignment: Text.AlignVCenter
151 height: Kirigami.Units.gridUnit * 2
152 text: root.textFromValue(root.value, root.locale)
153 }
154 background: Item {
155 implicitHeight: Kirigami.Units.gridUnit * 2
156 Rectangle {
157 color: Kirigami.ColorUtils.linearInterpolation(Kirigami.Theme.backgroundColor, Kirigami.Theme.textColor, Kirigami.Theme.frameContrast)
158 height: 1
159 anchors {
160 left: parent.left
161 right: parent.right
162 top: parent.top
163 }
164 }
165
166 Rectangle {
167 color: Kirigami.ColorUtils.linearInterpolation(Kirigami.Theme.backgroundColor, Kirigami.Theme.textColor, Kirigami.Theme.frameContrast)
168 height: 1
169 anchors {
170 left: parent.left
171 right: parent.right
172 bottom: parent.bottom
173 }
174 }
175 }
176 }
177
178 P.SpinButton {
179 onClicked: root.increase()
180 visible: Kirigami.Settings.isMobile
181 icon.name: 'list-add'
182
183 isStart: false
184 isEnd: true
185 }
186 }
187
188 QQC2.SpinBox {
189 id: spinbox
190 Layout.fillWidth: true
191 visible: !Kirigami.Settings.isMobile
192 locale: root.locale
193 }
194
195 Kirigami.InlineMessage {
196 id: formErrorHandler
197 visible: root.statusMessage.length > 0
198 Layout.topMargin: visible ? Kirigami.Units.smallSpacing : 0
199 Layout.fillWidth: true
200 text: root.statusMessage
201 type: root.status
202 }
203 }
204}
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 3 2024 11:46:57 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.