ToolStrip.qml 4.76 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 27 28 29 30 31
    property var    rotateImage         ///< List of bool values, one for each button in strip - true: animation rotation, false: static image
    property var    animateImage        ///< List of bool values, one for each button in strip - true: animate image, false: static image
    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
    property var    showAlternateIcon   ///< List of bool values, one for each button in strip - true: show alternate icon, false: show normal icon
32

33
    property AbstractButton lastClickedButton: null
34

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

40 41 42
    ButtonGroup {
        id: buttonGroup
        exclusive: false
43 44
    }

45
    Column {
Don Gagne's avatar
Don Gagne committed
46
        id:                 toolStripColumn
47
        anchors.margins:    ScreenTools.defaultFontPixelWidth * 0.4
48 49 50
        anchors.top:        parent.top
        anchors.left:       parent.left
        anchors.right:      parent.right
Gus Grubba's avatar
Gus Grubba committed
51
        spacing:            ScreenTools.defaultFontPixelWidth * 0.25
52

53 54
        Repeater {
            id: repeater
55

56 57
            QGCHoverButton {
                id: buttonTemplate
58

59 60 61 62
                anchors.left:   toolStripColumn.left
                anchors.right:  toolStripColumn.right
                height:         width
                radius:         ScreenTools.defaultFontPixelWidth / 2
63
                fontPointSize:  ScreenTools.smallFontPointSize
64 65 66 67 68 69 70 71

                enabled:        _root.buttonEnabled ? _root.buttonEnabled[index] : true
                visible:        _root.buttonVisible ? _root.buttonVisible[index] : true
                imageSource:    (_root.showAlternateIcon && _root.showAlternateIcon[index]) ? _alternateIconSource : _iconSource
                text:           modelData.name

                property var    _iconSource:            modelData.iconSource
                property var    _alternateIconSource:   modelData.alternateIconSource
72

73 74 75 76 77 78 79 80 81 82 83 84 85
                ButtonGroup.group: buttonGroup
                // Only drop pannel and toggleable are checkable
                checkable: modelData.dropPanelComponent !== undefined || (modelData.toggle !== undefined && modelData.toggle)

                onClicked: {
                    dropPanel.hide()    // DropPanel will call hide on "lastClickedButton"

                    // Uncheck other checked buttons
                    // TODO: Implement ButtonGroup exclusive with checkable and uncheckable and get rid of this workaround
                    for(var i = 0; i < buttonGroup.buttons.length; i++) {
                        var b = buttonGroup.buttons[i]
                        if(b !== buttonTemplate) {
                            b.checked = false;
Don Gagne's avatar
Don Gagne committed
86 87
                        }
                    }
88

89 90 91 92 93 94 95 96
                    if (modelData.dropPanelComponent === undefined) {
                        _root.clicked(index, checked)
                    } else if (checked) {
                        var panelEdgeTopPoint = mapToItem(_root, width, 0)
                        dropPanel.show(panelEdgeTopPoint, height, modelData.dropPanelComponent)
                    }
                    lastClickedButton = buttonTemplate
                }
97 98 99 100 101 102 103 104 105
            }
        }
    }

    DropPanel {
        id:         dropPanel
        toolStrip:  _root
    }
}