Skip to content
QGCView.qml 10.3 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


/// @file
///     @author Don Gagne <don@thegagnes.com>

import QtQuick 2.3
import QtQuick.Controls 1.2
Don Gagne's avatar
Don Gagne committed
import QtQuick.Dialogs 1.2

import QGroundControl.Controls 1.0
import QGroundControl.Palette 1.0
import QGroundControl.ScreenTools 1.0
import QGroundControl.FactSystem 1.0
import QGroundControl.FactControls 1.0

Don Gagne's avatar
Don Gagne committed
FactPanel {
    id: _rootItem
    property var    qgcView:               _rootItem  ///< Used by Fact controls for validation dialogs
    property bool   completedSignalled:   false
    property real   topDialogMargin:      0           ///< Set a top margin for dialog
    property var    viewPanel
Don Gagne's avatar
Don Gagne committed
    /// This is signalled when the top level Item reaches Component.onCompleted. This allows
    /// the view subcomponent to connect to this signal and do work once the full ui is ready
    /// to go.
    signal completed        
    function _setupDialogButtons(buttons) {
        _acceptButton.visible = false
        _rejectButton.visible = false
Don Gagne's avatar
Don Gagne committed
        // Accept role buttons
Don Gagne's avatar
Don Gagne committed
        if (buttons & StandardButton.Ok) {
            _acceptButton.text = qsTr("Ok")
            _acceptButton.visible = true
Don Gagne's avatar
Don Gagne committed
        } else if (buttons & StandardButton.Open) {
            _acceptButton.text = qsTr("Open")
            _acceptButton.visible = true
Don Gagne's avatar
Don Gagne committed
        } else if (buttons & StandardButton.Save) {
            _acceptButton.text = qsTr("Save")
            _acceptButton.visible = true
Don Gagne's avatar
Don Gagne committed
        } else if (buttons & StandardButton.Apply) {
            _acceptButton.text = qsTr("Apply")
            _acceptButton.visible = true
Don Gagne's avatar
Don Gagne committed
        } else if (buttons & StandardButton.Open) {
            _acceptButton.text = qsTr("Open")
            _acceptButton.visible = true
Don Gagne's avatar
Don Gagne committed
        } else if (buttons & StandardButton.SaveAll) {
            _acceptButton.text = qsTr("Save All")
            _acceptButton.visible = true
Don Gagne's avatar
Don Gagne committed
        } else if (buttons & StandardButton.Yes) {
            _acceptButton.text = qsTr("Yes")
            _acceptButton.visible = true
Don Gagne's avatar
Don Gagne committed
        } else if (buttons & StandardButton.YesToAll) {
            _acceptButton.text = qsTr("Yes to All")
            _acceptButton.visible = true
Don Gagne's avatar
Don Gagne committed
        } else if (buttons & StandardButton.Retry) {
            _acceptButton.text = qsTr("Retry")
            _acceptButton.visible = true
Don Gagne's avatar
Don Gagne committed
        } else if (buttons & StandardButton.Reset) {
            _acceptButton.text = qsTr("Reset")
            _acceptButton.visible = true
Don Gagne's avatar
Don Gagne committed
        } else if (buttons & StandardButton.RestoreToDefaults) {
            _acceptButton.text = qsTr("Restore to Defaults")
            _acceptButton.visible = true
Don Gagne's avatar
Don Gagne committed
        } else if (buttons & StandardButton.Ignore) {
            _acceptButton.text = qsTr("Ignore")
            _acceptButton.visible = true
Don Gagne's avatar
Don Gagne committed
        }
Don Gagne's avatar
Don Gagne committed

        // Reject role buttons
        if (buttons & StandardButton.Cancel) {
            _rejectButton.text = qsTr("Cancel")
            _rejectButton.visible = true
Don Gagne's avatar
Don Gagne committed
        } else if (buttons & StandardButton.Close) {
            _rejectButton.text = qsTr("Close")
            _rejectButton.visible = true
Don Gagne's avatar
Don Gagne committed
        } else if (buttons & StandardButton.No) {
            _rejectButton.text = qsTr("No")
            _rejectButton.visible = true
Don Gagne's avatar
Don Gagne committed
        } else if (buttons & StandardButton.NoToAll) {
            _rejectButton.text = qsTr("No to All")
            _rejectButton.visible = true
Don Gagne's avatar
Don Gagne committed
        } else if (buttons & StandardButton.Abort) {
            _rejectButton.text = qsTr("Abort")
            _rejectButton.visible = true
    function _checkForEarlyDialog(title) {
        if (!completedSignalled) {
            console.warn(qsTr("showDialog called before QGCView.completed signalled"), title)
    /// Shows a QGCViewDialog component
    ///     @param compoent QGCViewDialog component
    ///     @param title Title for dialog
    ///     @param charWidth Width of dialog in characters
    ///     @param buttons Buttons to show in dialog using StandardButton enum

    readonly property int showDialogFullWidth:      -1  ///< Use for full width dialog
    readonly property int showDialogDefaultWidth:   40  ///< Use for default dialog width

    function showDialog(component, title, charWidth, buttons) {
        if (_checkForEarlyDialog(title)) {
        _rejectButton.enabled = true
        _acceptButton.enabled = true
        _dialogCharWidth = charWidth
        _dialogTitle = title
        _setupDialogButtons(buttons)
Don Gagne's avatar
Don Gagne committed

        _dialogComponent = component
        viewPanel.enabled = false
        _dialogOverlay.visible = true
Don Gagne's avatar
Don Gagne committed
    function showMessage(title, message, buttons) {
        if (_checkForEarlyDialog(title)) {
        _rejectButton.enabled = true
        _acceptButton.enabled = true
        _dialogCharWidth = showDialogDefaultWidth
        _dialogTitle = title
        _messageDialogText = message
        _setupDialogButtons(buttons)
Don Gagne's avatar
Don Gagne committed

        _dialogComponent = _messageDialog
        viewPanel.enabled = false
        _dialogOverlay.visible = true
Don Gagne's avatar
Don Gagne committed
    function hideDialog() {
        viewPanel.enabled = true
        _dialogComponent = null
        _dialogOverlay.visible = false
    QGCPalette { id: _qgcPal; colorGroupEnabled: true }
    QGCLabel { id: _textMeasure; text: "X"; visible: false }
    property real defaultTextHeight: _textMeasure.contentHeight
    property real defaultTextWidth:  _textMeasure.contentWidth
Don Gagne's avatar
Don Gagne committed

    /// The width of the dialog panel in characters
    property int _dialogCharWidth: 75
Don Gagne's avatar
Don Gagne committed

    /// The title for the dialog panel
    property string _dialogTitle
    property string _messageDialogText
Don Gagne's avatar
Don Gagne committed

    property Component _dialogComponent
    function _signalCompleted() {
Don Gagne's avatar
Don Gagne committed
        // When we use this control inside a QGCQmlWidgetHolder Component.onCompleted is signalled
        // before the width and height are adjusted. So we need to wait for width and heigth to be
        // set before we signal our own completed signal.
        if (!completedSignalled && width != 0 && height != 0) {
            completedSignalled = true
    Component.onCompleted:  _signalCompleted()
    onWidthChanged:         _signalCompleted()
    onHeightChanged:        _signalCompleted()
Don Gagne's avatar
Don Gagne committed

    Connections {
        target: _dialogComponentLoader.item
        onHideDialog: _rootItem.hideDialog()
        id:             _dialogOverlay
Don Gagne's avatar
Don Gagne committed
        visible:        false
        anchors.fill:   parent
        z:              5000

        // This covers the parent with an transparent section
        Rectangle {
            id:             _transparentSection
            height:         ScreenTools.availableHeight ? ScreenTools.availableHeight : parent.height
Don Gagne's avatar
Don Gagne committed
            anchors.bottom: parent.bottom
            anchors.left:   parent.left
            anchors.right:  _dialogPanel.left
Don Gagne's avatar
Don Gagne committed
            opacity:        0.0
            color:          _qgcPal.window
Don Gagne's avatar
Don Gagne committed
        }

        // This is the main dialog panel which is anchored to the right edge
        Rectangle {
            id:                 _dialogPanel
            width:              _dialogCharWidth == showDialogFullWidth ? parent.width : defaultTextWidth * _dialogCharWidth
            anchors.topMargin:  topDialogMargin
            height:             ScreenTools.availableHeight ? ScreenTools.availableHeight : parent.height
            anchors.bottom:     parent.bottom
            anchors.right:      parent.right
            color:              _qgcPal.windowShadeDark
Don Gagne's avatar
Don Gagne committed

            Rectangle {
Don Gagne's avatar
Don Gagne committed
                width:  parent.width
                height: _acceptButton.visible ? _acceptButton.height : _rejectButton.height
                color:  _qgcPal.windowShade
                function _hidePanel() {
                    _fullPanel.visible = false
Don Gagne's avatar
Don Gagne committed
                }

                QGCLabel {
Don Gagne's avatar
Don Gagne committed
                    x:                  defaultTextWidth
Don Gagne's avatar
Don Gagne committed
                    height:             parent.height
                    verticalAlignment:	Text.AlignVCenter
                    text:               _dialogTitle
Don Gagne's avatar
Don Gagne committed
                }

                QGCButton {
                    id:             _rejectButton
                    anchors.right:  _acceptButton.visible ?  _acceptButton.left : parent.right
Don Gagne's avatar
Don Gagne committed
                    anchors.bottom: parent.bottom

                    onClicked: {
                        enabled = false // prevent multiple clicks
                        _dialogComponentLoader.item.reject()
                        if (!viewPanel.enabled) {
                            // Dialog was not closed, re-enable button
                            enabled = true
                        }
Don Gagne's avatar
Don Gagne committed
                }

                QGCButton {
                    id:             _acceptButton
Don Gagne's avatar
Don Gagne committed
                    anchors.right:  parent.right
                    primary:        true

                        enabled = false // prevent multiple clicks
                       _dialogComponentLoader.item.accept()
                        if (!viewPanel.enabled) {
                            // Dialog was not closed, re-enable button
                            enabled = true
                        }
Don Gagne's avatar
Don Gagne committed
                width:          10
                height:         10
                anchors.top:    _header.bottom
Don Gagne's avatar
Don Gagne committed
            }

            Loader {
                id:                 _dialogComponentLoader
Don Gagne's avatar
Don Gagne committed
                anchors.margins:    5
                anchors.left:       parent.left
                anchors.right:      parent.right
                anchors.top:        _spacer.bottom
Don Gagne's avatar
Don Gagne committed
                anchors.bottom:     parent.bottom
                sourceComponent:    _dialogComponent
                property bool acceptAllowed: _acceptButton.visible
                property bool rejectAllowed: _rejectButton.visible
Don Gagne's avatar
Don Gagne committed
            }
        } // Rectangle - Dialog panel
    } // Item - Dialog overlay
Don Gagne's avatar
Don Gagne committed

    Component {
        id: _messageDialog
Don Gagne's avatar
Don Gagne committed

        QGCViewMessage {
            message: _messageDialogText
Don Gagne's avatar
Don Gagne committed
        }
    }