Warning, /frameworks/qqc2-desktop-style/org.kde.desktop/SpinBox.qml is written in an unsupported language. File is not indexed.

0001 /*
0002     SPDX-FileCopyrightText: 2017 Marco Martin <mart@kde.org>
0003     SPDX-FileCopyrightText: 2017 The Qt Company Ltd.
0004 
0005     SPDX-License-Identifier: LGPL-3.0-only OR GPL-2.0-or-later
0006 */
0007 
0008 
0009 import QtQuick
0010 import QtQuick.Window
0011 import QtQuick.Templates as T
0012 import org.kde.kirigami as Kirigami
0013 import org.kde.qqc2desktopstyle.private as StylePrivate
0014 
0015 T.SpinBox {
0016     id: controlRoot
0017     Kirigami.Theme.colorSet: Kirigami.Theme.View
0018     Kirigami.Theme.inherit: false
0019 
0020     implicitWidth: Math.max(styleitem.fullRectSizeHint.width,
0021                             implicitBackgroundWidth + leftInset + rightInset,
0022                             implicitContentWidth + leftPadding + rightPadding)
0023     implicitHeight: Math.max(styleitem.fullRectSizeHint.height,
0024                              implicitBackgroundHeight + topInset + bottomInset,
0025                              implicitContentHeight + topPadding + bottomPadding)
0026 
0027     padding: 6
0028     leftPadding: controlRoot.mirrored ? ___rPadding : ___lPadding
0029     rightPadding: controlRoot.mirrored ? ___lPadding : ___rPadding
0030 
0031     readonly property int ___lPadding: styleitem.upRectSizeHint.x === styleitem.downRectSizeHint.x ? horizontalPadding : styleitem.downRectSizeHint.width
0032     readonly property int ___rPadding: styleitem.upRectSizeHint.x === styleitem.downRectSizeHint.x ? styleitem.downRectSizeHint.width : styleitem.upRectSizeHint.width
0033 
0034     hoverEnabled: true
0035     wheelEnabled: true
0036     editable: true
0037 
0038     validator: IntValidator {
0039         locale: controlRoot.locale.name
0040         bottom: Math.min(controlRoot.from, controlRoot.to)
0041         top: Math.max(controlRoot.from, controlRoot.to)
0042     }
0043 
0044     inputMethodHints: Qt.ImhFormattedNumbersOnly
0045 
0046     contentItem: T.TextField {
0047         readonly property TextMetrics _textMetrics: TextMetrics {
0048             text: controlRoot.textFromValue(controlRoot.to, controlRoot.locale)
0049             font: controlRoot.font
0050         }
0051         // Sometimes textMetrics.width isn't as wide as the actual text; add 2
0052         implicitWidth: Math.max(_textMetrics.width + 2, Math.round(contentWidth))
0053             + leftPadding + rightPadding
0054         implicitHeight: Math.round(contentHeight) + topPadding + bottomPadding
0055         z: 2
0056         font: controlRoot.font
0057         palette: controlRoot.palette
0058         text: controlRoot.textFromValue(controlRoot.value, controlRoot.locale)
0059         color: Kirigami.Theme.textColor
0060         selectionColor: Kirigami.Theme.highlightColor
0061         selectedTextColor: Kirigami.Theme.highlightedTextColor
0062         selectByMouse: true
0063         hoverEnabled: false // let hover events propagate to SpinBox
0064         verticalAlignment: Qt.AlignVCenter
0065         readOnly: !controlRoot.editable
0066         validator: controlRoot.validator
0067         inputMethodHints: controlRoot.inputMethodHints
0068 
0069         // SpinBox does not update its value during editing, see QTBUG-91281
0070         onTextEdited: if (controlRoot.contentItem.text.length > 0 && acceptableInput) {
0071             controlRoot.value = controlRoot.valueFromText(controlRoot.contentItem.text, controlRoot.locale)
0072             controlRoot.valueModified()
0073         }
0074 
0075         // Since the contentItem receives focus (we make them editable by default),
0076         // the screen reader reads its Accessible properties instead of the SpinBox's
0077         Accessible.name: controlRoot.Accessible.name
0078         Accessible.description: controlRoot.Accessible.description
0079     }
0080 
0081     up.indicator: Item {
0082         implicitWidth: styleitem.upRect.width
0083         implicitHeight: styleitem.upRect.height
0084 
0085         x: styleitem.upRect.x
0086         y: styleitem.upRect.y
0087     }
0088     down.indicator: Item {
0089         implicitWidth: styleitem.downRect.width
0090         implicitHeight: styleitem.downRect.height
0091 
0092         x: styleitem.downRect.x
0093         y: styleitem.downRect.y
0094     }
0095 
0096     background: StylePrivate.StyleItem {
0097         id: styleitem
0098         control: controlRoot
0099         elementType: "spinbox"
0100         anchors.fill: parent
0101         hover: controlRoot.hovered
0102         hasFocus: controlRoot.activeFocus
0103         enabled: controlRoot.enabled
0104 
0105         // static hints calculated once for minimum sizes
0106         property rect upRectSizeHint: styleitem.subControlRect("up")
0107         property rect downRectSizeHint: styleitem.subControlRect("down")
0108         property rect editRectSizeHint: styleitem.subControlRect("edit")
0109         property rect fullRectSizeHint: styleitem.computeBoundingRect([upRectSizeHint, downRectSizeHint, editRectSizeHint])
0110 
0111         // dynamic hints calculated every resize to keep the buttons in place
0112         property rect upRect: upRectSizeHint
0113         property rect downRect: downRectSizeHint
0114 
0115         function recompute() {
0116             upRect = styleitem.subControlRect("up")
0117             downRect = styleitem.subControlRect("down")
0118         }
0119 
0120         onWidthChanged: recompute()
0121         onHeightChanged: recompute()
0122 
0123         value: {
0124             var value = 0;
0125             if (controlRoot.up.hovered || controlRoot.up.pressed) {
0126                 value |= 1 << 0;
0127             }
0128             if (controlRoot.down.hovered || controlRoot.down.pressed) {
0129                 value |= 1 << 1;
0130             }
0131             if (controlRoot.value !== controlRoot.to) {
0132                 value |= 1 << 2;
0133             }
0134             if (controlRoot.value !== controlRoot.from) {
0135                 value |= 1 << 3;
0136             }
0137             return value;
0138         }
0139     }
0140 }