ToolStrip.qml 4.01 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.
 *
 ****************************************************************************/

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
Don Gagne's avatar
Don Gagne committed
22
    height:     toolStripColumn.height + (toolStripColumn.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

28
    property AbstractButton lastClickedButton: null
29

DonLakeFlyer's avatar
DonLakeFlyer committed
30 31 32 33 34
    function simulateClick(buttonIndex) {
        toolStripColumn.children[buttonIndex].checked = true
        toolStripColumn.children[buttonIndex].clicked()
    }

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

38
    signal clicked(int index, bool checked)
39
    signal dropped(int index)
40

Gus Grubba's avatar
Gus Grubba committed
41 42 43 44 45 46 47 48
    function setChecked(idx, check) {
        repeater.itemAt(idx).checked = check
    }

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

49
    ButtonGroup {
Gus Grubba's avatar
Gus Grubba committed
50 51
        id:         buttonGroup
        buttons:    toolStripColumn.children
52 53
    }

54
    Column {
Don Gagne's avatar
Don Gagne committed
55
        id:                 toolStripColumn
56
        anchors.margins:    ScreenTools.defaultFontPixelWidth * 0.4
57 58 59
        anchors.top:        parent.top
        anchors.left:       parent.left
        anchors.right:      parent.right
Gus Grubba's avatar
Gus Grubba committed
60
        spacing:            ScreenTools.defaultFontPixelWidth * 0.25
61

62 63
        Repeater {
            id: repeater
64

65
            QGCHoverButton {
Gus Grubba's avatar
Gus Grubba committed
66
                id:             buttonTemplate
67

68 69 70 71
                anchors.left:   toolStripColumn.left
                anchors.right:  toolStripColumn.right
                height:         width
                radius:         ScreenTools.defaultFontPixelWidth / 2
72
                fontPointSize:  ScreenTools.smallFontPointSize
Gus Grubba's avatar
Gus Grubba committed
73
                autoExclusive:  true
74

Gus Grubba's avatar
Gus Grubba committed
75 76 77
                enabled:        modelData.buttonEnabled
                visible:        modelData.buttonVisible
                imageSource:    modelData.showAlternateIcon ? modelData.alternateIconSource : modelData.iconSource
78
                text:           modelData.name
Gus Grubba's avatar
Gus Grubba committed
79
                checked:        modelData.checked !== undefined ? modelData.checked : checked
80

81
                ButtonGroup.group: buttonGroup
DonLakeFlyer's avatar
DonLakeFlyer committed
82
                // Only drop panel and toggleable are checkable
83 84 85 86 87 88 89 90 91
                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)
                        dropPanel.show(panelEdgeTopPoint, height, modelData.dropPanelComponent)
92
                        _root.dropped(index)
93
                    }
Gus Grubba's avatar
Gus Grubba committed
94 95
                    if(_root && buttonTemplate)
                        _root.lastClickedButton = buttonTemplate
96
                }
97 98 99 100 101 102 103 104 105
            }
        }
    }

    DropPanel {
        id:         dropPanel
        toolStrip:  _root
    }
}