// // SpinBox.qml // // Created by David Rowe on 26 Feb 2016 // Copyright 2016 High Fidelity, Inc. // // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // import QtQuick 2.7 import QtQuick.Controls 2.2 import "../styles-uit" import "../controls-uit" as HifiControls SpinBox { id: spinBox HifiConstants { id: hifi } property int colorScheme: hifi.colorSchemes.light readonly property bool isLightColorScheme: colorScheme === hifi.colorSchemes.light property string label: "" property string suffix: "" property string labelInside: "" property color colorLabelInside: hifi.colors.white property color backgroundColor: isLightColorScheme ? (spinBox.activeFocus ? hifi.colors.white : hifi.colors.lightGray) : (spinBox.activeFocus ? hifi.colors.black : hifi.colors.baseGrayShadow) property real controlHeight: height + (spinBoxLabel.visible ? spinBoxLabel.height + spinBoxLabel.anchors.bottomMargin : 0) property int decimals: 2; property real factor: Math.pow(10, decimals) property real minimumValue: 0.0 property real maximumValue: 0.0 property real realValue: 0.0 property real realFrom: minimumValue property real realTo: maximumValue property real realStepSize: 1.0 signal editingFinished() implicitHeight: height implicitWidth: width editable: true padding: 0 leftPadding: 0 rightPadding: padding + (up.indicator ? up.indicator.width : 0) topPadding: 0 bottomPadding: 0 locale: Qt.locale("en_US") onValueModified: realValue = value/factor onValueChanged: realValue = value/factor onRealValueChanged: { var newValue = Math.round(realValue*factor); if(value != newValue) { value = newValue; } } stepSize: realStepSize*factor to : realTo*factor from : realFrom*factor font.family: "Fira Sans SemiBold" font.pixelSize: hifi.fontSizes.textFieldInput height: hifi.fontSizes.textFieldInput + 13 // Match height of TextField control. y: spinBoxLabel.visible ? spinBoxLabel.height + spinBoxLabel.anchors.bottomMargin : 0 background: Rectangle { color: backgroundColor border.color: spinBoxLabelInside.visible ? spinBoxLabelInside.color : hifi.colors.primaryHighlight border.width: spinBox.activeFocus ? spinBoxLabelInside.visible ? 2 : 1 : 0 } validator: DoubleValidator { bottom: Math.min(spinBox.from, spinBox.to) top: Math.max(spinBox.from, spinBox.to) } textFromValue: function(value, locale) { return parseFloat(value/factor).toFixed(decimals); } valueFromText: function(text, locale) { spinBox.value = 0; // Force valueChanged signal to be emitted so that validator fires. return Number.fromLocaleString(locale, text)*factor; } contentItem: TextInput { z: 2 color: isLightColorScheme ? (spinBox.activeFocus ? hifi.colors.black : hifi.colors.lightGray) : (spinBox.activeFocus ? hifi.colors.white : hifi.colors.lightGrayText) selectedTextColor: hifi.colors.black selectionColor: hifi.colors.primaryHighlight text: spinBox.textFromValue(spinBox.value, spinBox.locale) + suffix verticalAlignment: Qt.AlignVCenter leftPadding: spinBoxLabelInside.visible ? 30 : hifi.dimensions.textPadding //rightPadding: hifi.dimensions.spinnerSize width: spinBox.width - hifi.dimensions.spinnerSize onEditingFinished: spinBox.editingFinished() } up.indicator: Item { x: spinBox.width - implicitWidth - 5 y: 1 clip: true implicitHeight: spinBox.implicitHeight/2 implicitWidth: spinBox.implicitHeight/2 HiFiGlyphs { anchors.centerIn: parent text: hifi.glyphs.caratUp size: hifi.dimensions.spinnerSize color: spinBox.up.pressed || spinBox.up.hovered ? (isLightColorScheme ? hifi.colors.black : hifi.colors.white) : hifi.colors.gray } } up.onPressedChanged: { if(value) { spinBox.forceActiveFocus(); } } down.indicator: Item { x: spinBox.width - implicitWidth - 5 y: spinBox.implicitHeight/2 clip: true implicitHeight: spinBox.implicitHeight/2 implicitWidth: spinBox.implicitHeight/2 HiFiGlyphs { anchors.centerIn: parent text: hifi.glyphs.caratDn size: hifi.dimensions.spinnerSize color: spinBox.down.pressed || spinBox.down.hovered ? (isLightColorScheme ? hifi.colors.black : hifi.colors.white) : hifi.colors.gray } } down.onPressedChanged: { if(value) { spinBox.forceActiveFocus(); } } HifiControls.Label { id: spinBoxLabel text: spinBox.label colorScheme: spinBox.colorScheme anchors.left: parent.left anchors.bottom: parent.top anchors.bottomMargin: 4 visible: label != "" } HifiControls.Label { id: spinBoxLabelInside text: spinBox.labelInside anchors.left: parent.left anchors.leftMargin: 10 font.bold: true anchors.verticalCenter: parent.verticalCenter color: spinBox.colorLabelInside visible: spinBox.labelInside != "" } MouseArea { anchors.fill: parent acceptedButtons: Qt.NoButton onWheel: { if (wheel.angleDelta.y > 0) value += stepSize else value -= stepSize } } }