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