Skip to content
ToolStrip.qml 3.93 KiB
Newer Older
/****************************************************************************
 *
 *   (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
import QtQuick          2.11
import QtQuick.Controls 2.2
Gus Grubba's avatar
Gus Grubba committed
import QGroundControl               1.0
import QGroundControl.ScreenTools   1.0
import QGroundControl.Palette       1.0
import QGroundControl.Controls      1.0

Rectangle {
    id:         _root
Gus Grubba's avatar
Gus Grubba committed
    color:      qgcPal.globalTheme === QGCPalette.Light ? QGroundControl.corePlugin.options.toolbarBackgroundLight : QGroundControl.corePlugin.options.toolbarBackgroundDark
    width:      _idealWidth < repeater.contentWidth ? repeater.contentWidth : _idealWidth
Don Gagne's avatar
 
Don Gagne committed
    height:     toolStripColumn.height + (toolStripColumn.anchors.margins * 2)
    radius:     ScreenTools.defaultFontPixelWidth / 2

    property alias  model:              repeater.model
    property real   maxHeight           ///< Maximum height for control, determines whether text is hidden to make control shorter
    property AbstractButton lastClickedButton: null
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    function simulateClick(buttonIndex) {
        toolStripColumn.children[buttonIndex].checked = true
        toolStripColumn.children[buttonIndex].clicked()
    }

    // Ensure we don't get narrower than content
    property real _idealWidth: (ScreenTools.isMobile ? ScreenTools.minTouchPixels : ScreenTools.defaultFontPixelWidth * 8) + toolStripColumn.anchors.margins * 2
    signal clicked(int index, bool checked)
Gus Grubba's avatar
Gus Grubba committed
    function setChecked(idx, check) {
        repeater.itemAt(idx).checked = check
    }

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

    ButtonGroup {
Gus Grubba's avatar
Gus Grubba committed
        id:         buttonGroup
        buttons:    toolStripColumn.children
    Column {
Don Gagne's avatar
 
Don Gagne committed
        id:                 toolStripColumn
        anchors.margins:    ScreenTools.defaultFontPixelWidth * 0.4
        anchors.top:        parent.top
        anchors.left:       parent.left
        anchors.right:      parent.right
Gus Grubba's avatar
Gus Grubba committed
        spacing:            ScreenTools.defaultFontPixelWidth * 0.25
        Repeater {
            id: repeater
            QGCHoverButton {
Gus Grubba's avatar
Gus Grubba committed
                id:             buttonTemplate
                anchors.left:   toolStripColumn.left
                anchors.right:  toolStripColumn.right
                height:         width
                radius:         ScreenTools.defaultFontPixelWidth / 2
                fontPointSize:  ScreenTools.smallFontPointSize
Gus Grubba's avatar
Gus Grubba committed
                autoExclusive:  true
Gus Grubba's avatar
Gus Grubba committed
                enabled:        modelData.buttonEnabled
                visible:        modelData.buttonVisible
                imageSource:    modelData.showAlternateIcon ? modelData.alternateIconSource : modelData.iconSource
                text:           modelData.name
Gus Grubba's avatar
Gus Grubba committed
                checked:        modelData.checked !== undefined ? modelData.checked : checked
                ButtonGroup.group: buttonGroup
DonLakeFlyer's avatar
 
DonLakeFlyer committed
                // 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)
                        dropPanel.show(panelEdgeTopPoint, height, modelData.dropPanelComponent)
                    }
Gus Grubba's avatar
Gus Grubba committed
                    if(_root && buttonTemplate)
                        _root.lastClickedButton = buttonTemplate
            }
        }
    }

    DropPanel {
        id:         dropPanel
        toolStrip:  _root
    }
}