QGCComboBox.qml 9.16 KB
Newer Older
1 2
import QtQuick 2.3
import QtQuick.Controls 1.2
3
import QtQuick.Controls.Styles 1.4
4 5

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

8
Button {
Don Gagne's avatar
Don Gagne committed
9 10
    id:                 combo
    implicitHeight:     ScreenTools.implicitComboBoxHeight
11 12

    property real   pointSize:      ScreenTools.defaultFontPointSize    ///< Point size for button text
DonLakeFlyer's avatar
DonLakeFlyer committed
13
    property bool   centeredLabel:  false
14 15 16 17 18 19 20
    property alias  model:          popupItems.model
    property alias  textRole:       popup.textRole
    property alias  currentIndex:   popup.__selectedIndex

    readonly property alias count:          popupItems.count
    readonly property alias currentText:    popup.currentText

21 22 23 24
    property bool   _showBorder:        _qgcPal.globalTheme === QGCPalette.Light
    property var    _qgcPal:            QGCPalette { colorGroupEnabled: enabled }
    property int    _horizontalPadding: ScreenTools.defaultFontPixelWidth
    property int    _verticalPadding:   Math.round(ScreenTools.defaultFontPixelHeight / 2)
DonLakeFlyer's avatar
DonLakeFlyer committed
25 26
    property real   _dropImageWidth:    ScreenTools.defaultFontPixelHeight / 2
    property real   _dropImageMargin:   _dropImageWidth / 2
27
    property var    __popup:            popup
28 29 30 31 32 33 34 35 36 37 38 39 40 41

    signal activated(int index)

    style: ButtonStyle {
        /*! The padding between the background and the label components. */
        padding {
            top:    _verticalPadding
            bottom: _verticalPadding
            left:   _horizontalPadding
            right:  _horizontalPadding
        }

        /*! This defines the background of the button. */
        background: Rectangle {
42 43
            implicitWidth:  ScreenTools.implicitComboBoxWidth
            implicitHeight: ScreenTools.implicitComboBoxHeight
44 45 46
            color:          control._qgcPal.textField
            border.width:   enabled ? 1 : 0
            border.color:   "#999"
47

48 49
            QGCColoredImage {
                id:                     image
DonLakeFlyer's avatar
DonLakeFlyer committed
50 51
                width:                  _dropImageWidth
                height:                 _dropImageWidth
52
                anchors.verticalCenter: parent.verticalCenter
DonLakeFlyer's avatar
DonLakeFlyer committed
53
                anchors.rightMargin:    _dropImageMargin
54 55
                anchors.right:          parent.right
                source:                 "/qmlimages/arrow-down.png"
56
                color:                  control._qgcPal.textFieldText
57 58 59 60 61
            }
        }

        /*! This defines the label of the button.  */
        label: Item {
DonLakeFlyer's avatar
DonLakeFlyer committed
62
            implicitWidth:  text.implicitWidth + _dropImageWidth
63 64 65
            implicitHeight: text.implicitHeight
            baselineOffset: text.y + text.baselineOffset

Don Gagne's avatar
Don Gagne committed
66
            QGCLabel {
DonLakeFlyer's avatar
DonLakeFlyer committed
67 68 69 70
                id:                         text
                anchors.verticalCenter:     parent.verticalCenter
                anchors.horizontalCenter:   centeredLabel ? parent.horizontalCenter : undefined
                text:                       control.currentText
71
                color:                      control._qgcPal.textFieldText
DonLakeFlyer's avatar
DonLakeFlyer committed
72
                font.pointSize:             pointSize
73 74 75
            }
        }
    }
76

77 78 79 80
    onClicked: {
        combo.focus = true
        popup.toggleShow()
    }
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

    Component.onCompleted: {
        if (currentIndex === -1) {
            currentIndex = 0
        }
        popup.resolveTextValue(textRole)
    }

    function textAt (index) {
        if (index >= count || index < 0)
            return null;
        return popupItems.objectAt(index).text;
    }

    function find (text) {
        for (var i = 0 ; i < popupItems.count ; ++i) {
            var currentString = popupItems.objectAt(i).text
            if (text === currentString) {
                return i
            }
        }
        return -1
    }

105 106
    ExclusiveGroup { id: eg }

107
    QGCMenu {
108 109 110 111 112
        id:             popup
        __minimumWidth: combo.width
        __visualItem:   combo

        style: MenuStyle {
Don Gagne's avatar
Don Gagne committed
113 114 115 116
            font.pointSize:             ScreenTools.defaultFontPointSize
            font.family:                ScreenTools.normalFontFamily
            __labelColor:               combo._qgcPal.buttonText
            __selectedLabelColor:       combo._qgcPal.buttonHighlightText
117
            __selectedBackgroundColor:  combo._qgcPal.buttonHighlight
Don Gagne's avatar
Don Gagne committed
118
            __backgroundColor:          combo._qgcPal.button
119 120 121
            __maxPopupHeight:           600
            __menuItemType:             "comboboxitem"
            __scrollerStyle:            ScrollViewStyle { }
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
        }

        property string textRole: ""
        property bool   showing: false

        property string currentText:    selectedText
        property string selectedText

        onSelectedTextChanged: popup.currentText = selectedText

        on__SelectedIndexChanged: {
            if (__selectedIndex === -1)
                popup.currentText = ""
            else
                updateSelectedText()
        }

        property int    _y:             combo.height
        property bool   _modelIsArray:  false

        onAboutToShow: showing = true
        onAboutToHide: showing = false

        function toggleShow() {
            if (popup._popupVisible) {
                popup.__dismissAndDestroy()
            } else {
                __popup(Qt.rect(0, _y, 0, 0), 0)
            }
        }

        function resolveTextValue(initialTextRole) {
            if (!model) {
                return
            }

            var get = model['get'];
            if (!get && popup._modelIsArray && !!model[0]) {
                if (model[0].constructor !== String && model[0].constructor !== Number)
                    get = function(i) { return model[i]; }
            }

            var modelMayHaveRoles = get !== undefined
            textRole = initialTextRole
            if (textRole === "" && modelMayHaveRoles && get(0)) {
                // No text role set, check whether model has a suitable role
                // If 'text' is found, or there's only one role, pick that.
                var listElement = get(0)
                var roleName = ""
                var roleCount = 0
                for (var role in listElement) {
                    if (listElement[role].constructor === Function)
                        continue;
                    if (role === "text") {
                        roleName = role
                        break
                    } else if (!roleName) {
                        roleName = role
                    }
                    ++roleCount
                }
                if (roleCount > 1 && roleName !== "text") {
                    console.warn("No suitable 'textRole' found for ComboBox.")
                } else {
                    textRole = roleName
                }
            }
            updateSelectedText()
        }

        function updateSelectedText() {
            var selectedItem
            if (__selectedIndex !== -1 && (selectedItem = items[__selectedIndex])) {
                selectedText = Qt.binding(function () { return selectedItem.text })
                if (currentText !== selectedText) // __selectedIndex went form -1 to 0
                    selectedTextChanged()
            }
        }

        Instantiator {
            id: popupItems

            onModelChanged: {
                popup._modelIsArray = !!model ? model.constructor === Array : false
                popup.resolveTextValue(popup.textRole)
            }

            onObjectAdded: {
                // There is a bug in Instantiator which can cause objects to be added out of order from an index standpoint.
Patrick José Pereira's avatar
Patrick José Pereira committed
211
                // If not handled correctly this will cause menu items to be added incorrectly due to the way Menu.insertItem works.
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
                //console.log("menu add", index, object.text)
                if (index === popup.__selectedIndex) {
                    popup.selectedText = object["text"]
                }

                // Find the correct place for menu item. We can't just add at index, due to possible bad ordering
                var insertIndex = -1
                for (var i=0; i<popup.items.length;  i++) {
                    //console.log("position search", i, popup.items[i].itemIndex)
                    if (popup.items[i].itemIndex > index) {
                        insertIndex = i
                        break
                    }
                }
                if (insertIndex === -1) {
                    popup.insertItem(popup.items.length, object)
                } else {
                    //console.log("out of order menu add", index, insertIndex)
                    popup.insertItem(insertIndex, object)
                }
            }

            onObjectRemoved: popup.removeItem(object)

236
            QGCMenuItem {
237 238 239 240
                text:           popup.textRole === '' ? modelData : ((popup._modelIsArray ? modelData[popup.textRole] : model[popup.textRole]) || '')
                checked:        index == currentIndex
                checkable:      true
                exclusiveGroup: eg
241 242 243 244 245 246 247 248 249 250 251

                property int itemIndex: index

                onTriggered: {
                    //console.log("onTriggered", index, currentIndex)
                    if (index !== currentIndex) {
                        //console.log("activated", index)
                        activated(index)
                    }
                }
            }
252 253
        }
    }
254
}