ToolStrip.qml 11.1 KB
Newer Older
1 2 3 4 5 6 7 8 9
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

10 11
import QtQuick          2.3
import QtQuick.Controls 1.2
12 13 14 15 16 17 18

import QGroundControl.ScreenTools   1.0
import QGroundControl.Palette       1.0

Rectangle {
    id:         _root
    color:      qgcPal.window
19
    width:      ScreenTools.isMobile ? ScreenTools.minTouchPixels : ScreenTools.defaultFontPixelWidth * 6
20 21 22 23 24
    height:     buttonStripColumn.height + (buttonStripColumn.anchors.margins * 2)
    radius:     _radius

    property string title:              "Title"
    property alias  model:              repeater.model
25 26
    property var    showAlternateIcon                   ///< List of bool values, one for each button in strip - true: show alternate icon, false: show normal icon
    property var    rotateImage                         ///< List of bool values, one for each button in strip - true: animation rotation, false: static image
27
    property var    animateImage                        ///< List of bool values, one for each button in strip - true: animate image, false: static image
28 29 30
    property var    buttonEnabled                       ///< List of bool values, one for each button in strip - true: button enabled, false: button disabled
    property var    buttonVisible                       ///< List of bool values, one for each button in strip - true: button visible, false: button invisible
    property real   maxHeight                           ///< Maximum height for control, determines whether text is hidden to make control shorter
31 32 33

    signal clicked(int index, bool checked)

Don Gagne's avatar
Don Gagne committed
34 35 36
    readonly property real  _radius:                ScreenTools.defaultFontPixelWidth / 2
    readonly property real  _margin:                ScreenTools.defaultFontPixelWidth / 2
    readonly property real  _buttonSpacing:         ScreenTools.defaultFontPixelWidth
37 38 39 40 41

    // All of the following values, connections and function are to support the ability to determine
    // whether to show or hide the optional elements on the fly.

    property bool _showOptionalElements:    true
DonLakeFlyer's avatar
DonLakeFlyer committed
42
    property bool _needRecalc:              false
43 44 45

    Component.onCompleted: recalcShowOptionalElements()

DonLakeFlyer's avatar
DonLakeFlyer committed
46 47 48
    onMaxHeightChanged:     recalcShowOptionalElements()
    onModelChanged:         recalcShowOptionalElements()
    onButtonVisibleChanged: recalcShowOptionalElements()
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

    Connections {
        target: ScreenTools

        onDefaultFontPixelWidthChanged:     recalcShowOptionalElements()
        onDefaultFontPixelHeightChanged:    recalcShowOptionalElements()
    }

    onHeightChanged: {
        if (_needRecalc) {
            _needRecalc = false
            if (maxHeight && height > maxHeight) {
                _showOptionalElements = false
            }
        }
    }

    function recalcShowOptionalElements() {
DonLakeFlyer's avatar
DonLakeFlyer committed
67 68 69
        if (maxHeight > 0) {
            _needRecalc = false
            _showOptionalElements =  height <= maxHeight
70 71 72
            _needRecalc = true
        }
    }
73

Don Gagne's avatar
Don Gagne committed
74 75
    QGCPalette { id: qgcPal }
    ExclusiveGroup { id: dropButtonsExclusiveGroup }
76 77 78 79 80

    function uncheckAll() {
        dropButtonsExclusiveGroup.current = null
        // Signal all toggles as off
        for (var i=0; i<model.length; i++) {
81 82
            if (model[i].toggle === true) {
                _root.clicked(i, false)
83 84 85 86 87 88 89 90 91 92 93 94 95 96
            }
        }
    }

    Column {
        id:                 buttonStripColumn
        anchors.margins:    ScreenTools.defaultFontPixelWidth  / 2
        anchors.top:        parent.top
        anchors.left:       parent.left
        anchors.right:      parent.right

        QGCLabel {
            anchors.horizontalCenter:   parent.horizontalCenter
            text:                       title
Don Gagne's avatar
Don Gagne committed
97
            visible:                    _showOptionalElements
98 99
        }

Don Gagne's avatar
Don Gagne committed
100
        Item { width: 1; height: _buttonSpacing; visible: _showOptionalElements }
101 102 103 104 105 106

        Rectangle {
            anchors.left:       parent.left
            anchors.right:      parent.right
            height:             1
            color:              qgcPal.text
Don Gagne's avatar
Don Gagne committed
107
            visible:            _showOptionalElements
108 109 110 111 112 113
        }

        Repeater {
            id: repeater

            delegate: Column {
Don Gagne's avatar
Don Gagne committed
114 115 116
                id:         buttonColumn
                width:      buttonStripColumn.width
                visible:    _root.buttonVisible ? _root.buttonVisible[index] : true
117 118 119 120

                property bool checked: false
                property ExclusiveGroup exclusiveGroup: dropButtonsExclusiveGroup

121 122 123 124
                QGCPalette { id: _repeaterPal; colorGroupEnabled: _buttonEnabled }

                property bool   _buttonEnabled:         _root.buttonEnabled ? _root.buttonEnabled[index] : true
                property var    _iconSource:            modelData.iconSource
Don Gagne's avatar
Don Gagne committed
125 126 127
                property var    _alternateIconSource:   modelData.alternateIconSource
                property var    _source:                (_root.showAlternateIcon && _root.showAlternateIcon[index]) ? _alternateIconSource : _iconSource
                property bool   rotateImage:            _root.rotateImage ? _root.rotateImage[index] : false
128
                property bool   animateImage:           _root.animateImage ? _root.animateImage[index] : false
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

                onExclusiveGroupChanged: {
                    if (exclusiveGroup) {
                        exclusiveGroup.bindCheckable(buttonColumn)
                    }
                }

                onRotateImageChanged: {
                    if (rotateImage) {
                        imageRotation.running = true
                    } else {
                        imageRotation.running = false
                        button.rotation = 0
                    }
                }

145 146 147 148 149 150 151 152 153
                onAnimateImageChanged: {
                    if (animateImage) {
                        opacityAnimation.running = true
                    } else {
                        opacityAnimation.running = false
                        button.opacity = 1
                    }
                }

Don Gagne's avatar
Don Gagne committed
154 155 156 157 158
                Item {
                    width:      1
                    height:     _buttonSpacing
                    visible:    index == 0 ? _showOptionalElements : true
                }
159

160 161
                FocusScope {
                    id:             scope
162 163 164 165
                    anchors.left:   parent.left
                    anchors.right:  parent.right
                    height:         width

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
                    Rectangle {
                        anchors.fill:   parent
                        color:          checked ? _repeaterPal.buttonHighlight : _repeaterPal.button

                        QGCColoredImage {
                            id:                 button
                            anchors.fill:       parent
                            source:             _source
                            sourceSize.height:  parent.height
                            fillMode:           Image.PreserveAspectFit
                            mipmap:             true
                            smooth:             true
                            color:              checked ? _repeaterPal.buttonHighlightText : _repeaterPal.buttonText

                            RotationAnimation on rotation {
                                id:             imageRotation
                                loops:          Animation.Infinite
                                from:           0
                                to:             360
                                duration:       500
                                running:        false
                            }

                            NumberAnimation on opacity {
                                id:         opacityAnimation
                                running:    false
                                from:       0
                                to:         1.0
                                loops:      Animation.Infinite
                                duration:   2000
                            }
197
                        }
198

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
                        QGCMouseArea {
                            // Size of mouse area is expanded to make touch easier
                            anchors.leftMargin:     -buttonStripColumn.anchors.margins
                            anchors.rightMargin:    -buttonStripColumn.anchors.margins
                            anchors.left:           parent.left
                            anchors.right:          parent.right
                            anchors.top:            parent.top
                            height:                 parent.height + (_showOptionalElements? buttonLabel.height + buttonColumn.spacing : 0)
                            visible:                _buttonEnabled
                            preventStealing:        true

                            onClicked: {
                                scope.focus = true
                                if (modelData.dropPanelComponent === undefined) {
                                    dropPanel.hide()
                                    if (modelData.toggle === true) {
                                        checked = !checked
                                    } else {
                                        // dropPanel.hide above will close panel, but we need to do this to clear toggles
                                        uncheckAll()
                                    }
                                    _root.clicked(index, checked)
221
                                } else {
222 223 224 225 226 227 228 229 230
                                    if (checked) {
                                        dropPanel.hide()    // hide affects checked, so this needs to be duplicated inside not outside if
                                    } else {
                                        dropPanel.hide()    // hide affects checked, so this needs to be duplicated inside not outside if
                                        uncheckAll()
                                        checked = true
                                        var panelEdgeTopPoint = mapToItem(_root, width, 0)
                                        dropPanel.show(panelEdgeTopPoint, height, modelData.dropPanelComponent)
                                    }
231 232 233 234 235 236
                                }
                            }
                        }
                    }
                }

237 238 239 240 241 242
                Item {
                    width:      1
                    height:     ScreenTools.defaultFontPixelHeight * 0.25
                    visible:    _showOptionalElements
                }

243 244 245 246 247
                QGCLabel {
                    id:                         buttonLabel
                    anchors.horizontalCenter:   parent.horizontalCenter
                    font.pointSize:             ScreenTools.smallFontPointSize
                    text:                       modelData.name
Don Gagne's avatar
Don Gagne committed
248
                    visible:                    _showOptionalElements
249
                    enabled:                    _buttonEnabled
250 251 252 253 254 255 256 257 258 259
                }
            }
        }
    }

    DropPanel {
        id:         dropPanel
        toolStrip:  _root
    }
}