Skip to content
QGCTextField.qml 4.96 KiB
Newer Older
import QtQuick                  2.3
import QtQuick.Controls         1.2
import QtQuick.Controls.Styles  1.4
import QtQuick.Layouts          1.2
import QGroundControl.Palette       1.0
import QGroundControl.ScreenTools   1.0
    id:                 root
    textColor:          qgcPal.textFieldText
    implicitHeight:     ScreenTools.implicitTextFieldHeight
    activeFocusOnPress: true
Don Gagne's avatar
 
Don Gagne committed
    antialiasing:       true
Don Gagne's avatar
 
Don Gagne committed
    property bool   showUnits:          false
    property bool   showHelp:           false
    property string unitsLabel:         ""
    property string extraUnitsLabel:    ""
    signal helpClicked

    property real _helpLayoutWidth: 0

    Component.onCompleted: selectAllIfActiveFocus()
    onActiveFocusChanged: selectAllIfActiveFocus()
    QGCPalette { id: qgcPal; colorGroupEnabled: enabled }
    onEditingFinished: {
        if (ScreenTools.isMobile) {
            // Toss focus on mobile after Done on virtual keyboard. Prevent strange interactions.
            focus = false
        }
    }

    function selectAllIfActiveFocus() {
        if (activeFocus) {
            selectAll()
        }
    }

Don Gagne's avatar
Don Gagne committed
    QGCLabel {
        id:             unitsLabelWidthGenerator
        text:           unitsLabel
dogmaphobic's avatar
dogmaphobic committed
        width:          contentWidth + parent.__contentHeight * 0.666
        visible:        false
        antialiasing:   true
Don Gagne's avatar
Don Gagne committed
    }
Don Gagne's avatar
Don Gagne committed
    style: TextFieldStyle {
Don Gagne's avatar
 
Don Gagne committed
        id:             tfs
dogmaphobic's avatar
dogmaphobic committed
        font.pointSize: ScreenTools.defaultFontPointSize
Don Gagne's avatar
 
Don Gagne committed
        font.family:    ScreenTools.normalFontFamily
        renderType:     ScreenTools.isWindows ? Text.QtRendering : tfs.renderType   // This works around font rendering problems on windows

Don Gagne's avatar
Don Gagne committed
        background: Item {
            id: backgroundItem

            property bool showHelp: control.showHelp && control.activeFocus

Don Gagne's avatar
Don Gagne committed
            Rectangle {
dogmaphobic's avatar
dogmaphobic committed
                anchors.fill:           parent
                anchors.bottomMargin:   -1
                color:                  "#44ffffff"
Don Gagne's avatar
Don Gagne committed
            }

            Rectangle {
dogmaphobic's avatar
dogmaphobic committed
                anchors.fill:           parent
                border.width:           enabled ? 1 : 0
                border.color:           root.activeFocus ? "#47b" : "#999"
                color:                  qgcPal.textField
Don Gagne's avatar
Don Gagne committed
            }

            RowLayout {
                id:                     unitsHelpLayout
                anchors.top:            parent.top
                anchors.bottom:         parent.bottom
                anchors.rightMargin:    backgroundItem.showHelp ? 0 : control.__contentHeight * 0.333
                anchors.right:          parent.right
                spacing:                ScreenTools.defaultFontPixelWidth / 4

                Component.onCompleted:  control._helpLayoutWidth = unitsHelpLayout.width
                onWidthChanged:         control._helpLayoutWidth = unitsHelpLayout.width

                Text {
                    Layout.alignment:   Qt.AlignVCenter
                    text:               control.unitsLabel
                    font.pointSize:     backgroundItem.showHelp ? ScreenTools.smallFontPointSize : ScreenTools.defaultFontPointSize
                    font.family:        ScreenTools.normalFontFamily
                    antialiasing:       true
                    color:              control.textColor
Don Gagne's avatar
 
Don Gagne committed
                    visible:            control.showUnits && text !== ""
                }

                Text {
                    Layout.alignment:   Qt.AlignVCenter
                    text:               control.extraUnitsLabel
                    font.pointSize:     ScreenTools.smallFontPointSize
                    font.family:        ScreenTools.normalFontFamily
                    antialiasing:       true
                    color:              control.textColor
                    visible:            control.showUnits && text !== ""
                    Layout.margins:     2
                    Layout.leftMargin:  0
                    Layout.rightMargin: 1
                    Layout.fillHeight:  true
                    width:              helpLabel.contentWidth * 3
                    color:              control.textColor
                    visible:            backgroundItem.showHelp

                    QGCLabel {
                        id:                 helpLabel
                        anchors.centerIn:   parent
                        color:              qgcPal.textField
                        text:               qsTr("?")
            MouseArea {
Don Gagne's avatar
Don Gagne committed
                anchors.margins:    ScreenTools.isMobile ? -(ScreenTools.defaultFontPixelWidth * 0.66) : 0 // Larger touch area for mobile
                anchors.fill:       unitsHelpLayout
                enabled:            control.activeFocus
                onClicked:          root.helpClicked()
        padding.right: control._helpLayoutWidth //control.showUnits ? unitsLabelWidthGenerator.width : control.__contentHeight * 0.333
Don Gagne's avatar
Don Gagne committed
    }