QGCTextField.qml 4.41 KB
Newer Older
1 2
import QtQuick                  2.3
import QtQuick.Controls         1.2
3
import QtQuick.Controls.Styles  1.4
4
import QtQuick.Layouts          1.2
5

6 7
import QGroundControl.Palette       1.0
import QGroundControl.ScreenTools   1.0
8 9

TextField {
10 11 12 13
    id:                 root
    textColor:          qgcPal.textFieldText
    implicitHeight:     ScreenTools.implicitTextFieldHeight
    activeFocusOnPress: true
14
    antialiasing:       true
15

16 17
    property bool   showUnits:  false
    property bool   showHelp:   false
18
    property string unitsLabel: ""
19

20 21 22 23
    signal helpClicked

    property real _helpLayoutWidth: 0

24 25
    Component.onCompleted: selectAllIfActiveFocus()
    onActiveFocusChanged: selectAllIfActiveFocus()
26

27
    QGCPalette { id: qgcPal; colorGroupEnabled: enabled }
28

29 30 31 32 33 34 35
    onEditingFinished: {
        if (ScreenTools.isMobile) {
            // Toss focus on mobile after Done on virtual keyboard. Prevent strange interactions.
            focus = false
        }
    }

36 37 38 39 40 41
    function selectAllIfActiveFocus() {
        if (activeFocus) {
            selectAll()
        }
    }

Don Gagne's avatar
Don Gagne committed
42
    QGCLabel {
43 44
        id:             unitsLabelWidthGenerator
        text:           unitsLabel
dogmaphobic's avatar
dogmaphobic committed
45
        width:          contentWidth + parent.__contentHeight * 0.666
46 47
        visible:        false
        antialiasing:   true
Don Gagne's avatar
Don Gagne committed
48
    }
49

Don Gagne's avatar
Don Gagne committed
50
    style: TextFieldStyle {
51
        id:             tfs
52
        font.pointSize: ScreenTools.defaultFontPointSize
53 54 55
        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
56 57 58
        background: Item {
            id: backgroundItem

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

Don Gagne's avatar
Don Gagne committed
61
            Rectangle {
dogmaphobic's avatar
dogmaphobic committed
62 63 64
                anchors.fill:           parent
                anchors.bottomMargin:   -1
                color:                  "#44ffffff"
Don Gagne's avatar
Don Gagne committed
65 66 67
            }

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

74 75 76 77 78 79
            RowLayout {
                id:                     unitsHelpLayout
                anchors.top:            parent.top
                anchors.bottom:         parent.bottom
                anchors.rightMargin:    backgroundItem.showHelp ? 0 : control.__contentHeight * 0.333
                anchors.right:          parent.right
80
                spacing:                ScreenTools.defaultFontPixelWidth / 4
81 82 83 84 85

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

                Text {
86 87 88 89 90 91 92
                    Layout.alignment:   Qt.AlignVCenter
                    text:               control.unitsLabel
                    font.pointSize:     backgroundItem.showHelp ? ScreenTools.smallFontPointSize : ScreenTools.defaultFontPointSize
                    font.family:        ScreenTools.normalFontFamily
                    antialiasing:       true
                    color:              control.textColor
                    visible:            control.showUnits
93 94 95
                }

                Rectangle {
96
                    Layout.margins:     2
97 98
                    Layout.leftMargin:  0
                    Layout.rightMargin: 1
99
                    Layout.fillHeight:  true
100
                    width:              helpLabel.contentWidth * 3
101 102 103 104
                    color:              control.textColor
                    visible:            backgroundItem.showHelp

                    QGCLabel {
105 106 107 108
                        id:                 helpLabel
                        anchors.centerIn:   parent
                        color:              qgcPal.textField
                        text:               qsTr("?")
109 110 111
                    }
                }
            }
112

113
            MouseArea {
Don Gagne's avatar
Don Gagne committed
114 115 116 117
                anchors.margins:    ScreenTools.isMobile ? -(ScreenTools.defaultFontPixelWidth * 0.66) : 0 // Larger touch area for mobile
                anchors.fill:       unitsHelpLayout
                enabled:            control.activeFocus
                onClicked:          root.helpClicked()
Don Gagne's avatar
Don Gagne committed
118 119 120
            }
        }

121
        padding.right: control._helpLayoutWidth //control.showUnits ? unitsLabelWidthGenerator.width : control.__contentHeight * 0.333
Don Gagne's avatar
Don Gagne committed
122
    }
123
}