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

Don Gagne's avatar
Don Gagne committed
10
import QtQuick          2.11
11
import QtQuick.Controls 2.2
12

Gus Grubba's avatar
Gus Grubba committed
13
import QGroundControl               1.0
14 15
import QGroundControl.ScreenTools   1.0
import QGroundControl.Palette       1.0
16
import QGroundControl.Controls      1.0
17 18 19

Rectangle {
    id:         _root
Gus Grubba's avatar
Gus Grubba committed
20
    color:      qgcPal.globalTheme === QGCPalette.Light ? QGroundControl.corePlugin.options.toolbarBackgroundLight : QGroundControl.corePlugin.options.toolbarBackgroundDark
21
    width:      _idealWidth < repeater.contentWidth ? repeater.contentWidth : _idealWidth
DonLakeFlyer's avatar
DonLakeFlyer committed
22
    height:     Math.min(maxHeight, toolStripColumn.height + (flickable.anchors.margins * 2))
23
    radius:     ScreenTools.defaultFontPixelWidth / 2
24 25

    property alias  model:              repeater.model
26
    property real   maxHeight           ///< Maximum height for control, determines whether text is hidden to make control shorter
27
    property alias  title:              titleLabel.text
28

29
    property AbstractButton lastClickedButton: null
30

DonLakeFlyer's avatar
DonLakeFlyer committed
31
    function simulateClick(buttonIndex) {
32
        buttonIndex = buttonIndex + 1 // skip over title
33 34 35 36
        if (!toolStripColumn.children[buttonIndex].checked) {
            toolStripColumn.children[buttonIndex].checked = true
            toolStripColumn.children[buttonIndex].clicked()
        }
DonLakeFlyer's avatar
DonLakeFlyer committed
37 38
    }

39
    // Ensure we don't get narrower than content
40
    property real _idealWidth: (ScreenTools.isMobile ? ScreenTools.minTouchPixels : ScreenTools.defaultFontPixelWidth * 8) + toolStripColumn.anchors.margins * 2
41

42
    signal clicked(int index, bool checked)
43
    signal dropped(int index)
44

Gus Grubba's avatar
Gus Grubba committed
45 46 47 48 49 50 51 52
    function setChecked(idx, check) {
        repeater.itemAt(idx).checked = check
    }

    function getChecked(idx) {
        return repeater.itemAt(idx).checked
    }

53
    ButtonGroup {
Gus Grubba's avatar
Gus Grubba committed
54 55
        id:         buttonGroup
        buttons:    toolStripColumn.children
56 57
    }

58 59 60 61 62
    DeadMouseArea {
        anchors.fill: parent
    }

    QGCFlickable {
DonLakeFlyer's avatar
DonLakeFlyer committed
63
        id:                 flickable
64
        anchors.margins:    ScreenTools.defaultFontPixelWidth * 0.4
65 66 67
        anchors.top:        parent.top
        anchors.left:       parent.left
        anchors.right:      parent.right
68 69 70
        height:             parent.height
        contentHeight:      toolStripColumn.height
        flickableDirection: Flickable.VerticalFlick
71
        clip:               true
72 73 74 75 76 77 78

        Column {
            id:             toolStripColumn
            anchors.left:   parent.left
            anchors.right:  parent.right
            spacing:        ScreenTools.defaultFontPixelWidth * 0.25

79 80 81 82 83 84 85 86 87
            QGCLabel {
                id:                     titleLabel
                anchors.left:           parent.left
                anchors.right:          parent.right
                horizontalAlignment:    Text.AlignHCenter
                font.pointSize:         ScreenTools.smallFontPointSize
                visible:                title != ""
            }

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
            Repeater {
                id: repeater

                QGCHoverButton {
                    id:             buttonTemplate

                    anchors.left:   toolStripColumn.left
                    anchors.right:  toolStripColumn.right
                    height:         width
                    radius:         ScreenTools.defaultFontPixelWidth / 2
                    fontPointSize:  ScreenTools.smallFontPointSize
                    autoExclusive:  true

                    enabled:        modelData.buttonEnabled
                    visible:        modelData.buttonVisible
                    imageSource:    modelData.showAlternateIcon ? modelData.alternateIconSource : modelData.iconSource
                    text:           modelData.name
                    checked:        modelData.checked !== undefined ? modelData.checked : checked

                    ButtonGroup.group: buttonGroup
                    // Only drop panel and toggleable are checkable
                    checkable: modelData.dropPanelComponent !== undefined || (modelData.toggle !== undefined && modelData.toggle)

                    onClicked: {
                        dropPanel.hide()    // DropPanel will call hide on "lastClickedButton"
                        if (modelData.dropPanelComponent === undefined) {
                            _root.clicked(index, checked)
                        } else if (checked) {
                            var panelEdgeTopPoint = mapToItem(_root, width, 0)
117
                            dropPanel.show(panelEdgeTopPoint, modelData.dropPanelComponent)
118 119 120 121
                            _root.dropped(index)
                        }
                        if(_root && buttonTemplate)
                            _root.lastClickedButton = buttonTemplate
122 123
                    }
                }
124 125 126 127 128 129 130 131 132
            }
        }
    }

    DropPanel {
        id:         dropPanel
        toolStrip:  _root
    }
}