ToolStrip.qml 4.58 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 13 14

import QGroundControl.ScreenTools   1.0
import QGroundControl.Palette       1.0
15
import QGroundControl.Controls      1.0
16 17 18

Rectangle {
    id:         _root
19
    color:      qgcPal.window
20
    width:      _idealWidth < repeater.contentWidth ? repeater.contentWidth : _idealWidth
Don Gagne's avatar
Don Gagne committed
21
    height:     toolStripColumn.height + (toolStripColumn.anchors.margins * 2)
22
    radius:     ScreenTools.defaultFontPixelWidth / 2
23 24

    property alias  model:              repeater.model
25 26 27 28 29 30
    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
31

32
    property AbstractButton lastClickedButton: null
33

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

37
    signal clicked(int index, bool checked)
38

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

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

52 53
        Repeater {
            id: repeater
54

55 56
            QGCHoverButton {
                id: buttonTemplate
57

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

                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
71

72 73 74 75 76 77 78 79 80 81 82 83 84
                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
85 86
                        }
                    }
87

88 89 90 91 92 93 94 95
                    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
                }
96 97 98 99 100 101 102 103 104
            }
        }
    }

    DropPanel {
        id:         dropPanel
        toolStrip:  _root
    }
}