Warning, /libraries/kirigami-addons/src/formcard/FormSpinBoxDelegate.qml is written in an unsupported language. File is not indexed.

0001 // Copyright 2023 Carl Schwan <carl@carlschwan.eu>
0002 // SPDX-License-Identifier: LGPL-2.0-or-later
0003 
0004 import QtQuick 2.15
0005 import QtQuick.Controls 2.15 as QQC2
0006 import QtQuick.Layouts 1.15
0007 
0008 import org.kde.kirigami 2.19 as Kirigami
0009 import 'private' as P
0010 
0011 /**
0012  * @brief A Form delegate that corresponds to a spinbox.
0013  *
0014  * This component is used to select a number. By default, the spinbox will be
0015  * initialized with a minimum of 0 and a maximum of 99.
0016  *
0017  * Example code:
0018  * ```qml
0019  * MobileForm.FormCard {
0020  *     contentItem: ColumnLayout {
0021  *         spacing: 0
0022  *
0023  *         MobileForm.FormCardHeader {
0024  *             title: "Information"
0025  *         }
0026  *
0027  *         MobileForm.FormSpinBoxDelegate {
0028  *             label: "Amount"
0029  *         }
0030  *     }
0031  * }
0032  * ```
0033  *
0034  * @since KirigamiAddons 0.11.0
0035  *
0036  * @inherit AbstractFormDelegate
0037  */
0038 AbstractFormDelegate {
0039     id: root
0040 
0041     /**
0042      * @brief A label that appears above the spinbox.
0043      *
0044      */
0045     required property string label
0046 
0047     /**
0048      * @brief This property holds the `value` of the internal spinbox.
0049      */
0050     property alias value: spinbox.value
0051 
0052     /**
0053      * @brief This property holds the `from` of the internal spinbox.
0054      */
0055     property alias from: spinbox.from
0056 
0057     /**
0058      * @brief This property holds the `to` of the internal spinbox.
0059      */
0060     property alias to: spinbox.to
0061 
0062     /**
0063      * @brief This property holds the `stepSize` of the internal spinbox.
0064      */
0065     property alias stepSize: spinbox.stepSize
0066 
0067     /**
0068      * @brief This property holds the `textFromValue` of the internal spinbox.
0069      */
0070     property alias textFromValue: spinbox.textFromValue
0071 
0072     /**
0073      * @brief This property holds the `valueFromText` of the internal spinbox.
0074      */
0075     property alias valueFromText: spinbox.valueFromText
0076 
0077     /**
0078      * @brief This property holds the `displayText` of the internal spinbox.
0079      */
0080     property alias displayText: spinbox.displayText
0081 
0082     /**
0083      * @brief This property holds the current type of status displayed in
0084      * the text field.
0085      *
0086      * Depending on the status of the text field, the statusMessage property
0087      * will look different
0088      *
0089      * Accepted values:
0090      * - Kirigami.MessageType.Information
0091      * - Kirigami.MessageType.Positive
0092      * - Kirigami.MessageType.Warning
0093      * - Kirigami.MessageType.Error
0094      *
0095      * @see Kirigami.MessageType
0096      */
0097     property var status: Kirigami.MessageType.Information
0098 
0099     /**
0100      * This property holds the current status message of the text field.
0101      */
0102     property string statusMessage: ""
0103 
0104     /**
0105      * Increases the value by stepSize, or 1 if stepSize is not defined.
0106      */
0107     function increase() {
0108         spinbox.increase();
0109     }
0110 
0111     /**
0112      * Decreases the value by stepSize, or 1 if stepSize is not defined.
0113      */
0114     function decrease() {
0115         spinbox.decrease();
0116     }
0117 
0118     focusPolicy: Kirigami.Settings.isMobile ? Qt.StrongFocus : Qt.NoFocus
0119 
0120     onClicked: spinbox.forceActiveFocus()
0121     background: null
0122 
0123     contentItem: ColumnLayout {
0124         RowLayout {
0125             Layout.fillWidth: true
0126             spacing: 0
0127 
0128             QQC2.Label {
0129                 Layout.fillWidth: true
0130                 text: label
0131                 elide: Text.ElideRight
0132                 color: root.enabled ? Kirigami.Theme.textColor : Kirigami.Theme.disabledTextColor
0133                 wrapMode: Text.Wrap
0134                 maximumLineCount: 2
0135             }
0136 
0137             P.SpinButton {
0138                 onClicked: root.decrease()
0139                 icon.name: 'list-remove-symbolic'
0140                 visible: Kirigami.Settings.isMobile
0141 
0142                 isStart: true
0143                 isEnd: false
0144             }
0145 
0146             QQC2.Pane {
0147                 focusPolicy: Qt.NoFocus
0148                 topPadding: 0
0149                 bottomPadding: 0
0150                 leftPadding: Kirigami.Units.largeSpacing * 2
0151                 rightPadding: Kirigami.Units.largeSpacing * 2
0152                 visible: Kirigami.Settings.isMobile
0153                 contentItem: QQC2.Label {
0154                     verticalAlignment: Text.AlignVCenter
0155                     height: Kirigami.Units.gridUnit * 2
0156                     text: root.textFromValue(root.value, root.locale)
0157                 }
0158                 background: Item {
0159                     implicitHeight: Kirigami.Units.gridUnit * 2
0160                     Rectangle {
0161                         color: Kirigami.ColorUtils.linearInterpolation(Kirigami.Theme.backgroundColor, Kirigami.Theme.textColor, Kirigami.Theme.frameContrast)
0162                         height: 1
0163                         anchors {
0164                             left: parent.left
0165                             right: parent.right
0166                             top: parent.top
0167                         }
0168                     }
0169 
0170                     Rectangle {
0171                         color: Kirigami.ColorUtils.linearInterpolation(Kirigami.Theme.backgroundColor, Kirigami.Theme.textColor, Kirigami.Theme.frameContrast)
0172                         height: 1
0173                         anchors {
0174                             left: parent.left
0175                             right: parent.right
0176                             bottom: parent.bottom
0177                         }
0178                     }
0179                 }
0180             }
0181 
0182             P.SpinButton {
0183                 onClicked: root.increase()
0184                 visible: Kirigami.Settings.isMobile
0185                 icon.name: 'list-add'
0186 
0187                 isStart: false
0188                 isEnd: true
0189             }
0190         }
0191 
0192         QQC2.SpinBox {
0193             id: spinbox
0194             Layout.fillWidth: true
0195             visible: !Kirigami.Settings.isMobile
0196             locale: root.locale
0197         }
0198 
0199         Kirigami.InlineMessage {
0200             id: formErrorHandler
0201             visible: root.statusMessage.length > 0
0202             Layout.topMargin: visible ? Kirigami.Units.smallSpacing : 0
0203             Layout.fillWidth: true
0204             text: root.statusMessage
0205             type: root.status
0206         }
0207     }
0208 }