QGCView.qml 10.3 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (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
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23


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

import QtQuick 2.3
import QtQuick.Controls 1.3
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
24
FactPanel {
25
    id: _rootItem
Don Gagne's avatar
Don Gagne committed
26

27 28 29 30
    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
31

Don Gagne's avatar
Don Gagne committed
32 33 34
    /// 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.
35
    signal completed        
Don Gagne's avatar
Don Gagne committed
36

37 38 39
    function _setupDialogButtons(buttons) {
        _acceptButton.visible = false
        _rejectButton.visible = false
Don Gagne's avatar
Don Gagne committed
40

Don Gagne's avatar
Don Gagne committed
41
        // Accept role buttons
Don Gagne's avatar
Don Gagne committed
42
        if (buttons & StandardButton.Ok) {
43 44
            _acceptButton.text = qsTr("Ok")
            _acceptButton.visible = true
Don Gagne's avatar
Don Gagne committed
45
        } else if (buttons & StandardButton.Open) {
46 47
            _acceptButton.text = qsTr("Open")
            _acceptButton.visible = true
Don Gagne's avatar
Don Gagne committed
48
        } else if (buttons & StandardButton.Save) {
49 50
            _acceptButton.text = qsTr("Save")
            _acceptButton.visible = true
Don Gagne's avatar
Don Gagne committed
51
        } else if (buttons & StandardButton.Apply) {
52 53
            _acceptButton.text = qsTr("Apply")
            _acceptButton.visible = true
Don Gagne's avatar
Don Gagne committed
54
        } else if (buttons & StandardButton.Open) {
55 56
            _acceptButton.text = qsTr("Open")
            _acceptButton.visible = true
Don Gagne's avatar
Don Gagne committed
57
        } else if (buttons & StandardButton.SaveAll) {
58 59
            _acceptButton.text = qsTr("Save All")
            _acceptButton.visible = true
Don Gagne's avatar
Don Gagne committed
60
        } else if (buttons & StandardButton.Yes) {
61 62
            _acceptButton.text = qsTr("Yes")
            _acceptButton.visible = true
Don Gagne's avatar
Don Gagne committed
63
        } else if (buttons & StandardButton.YesToAll) {
64 65
            _acceptButton.text = qsTr("Yes to All")
            _acceptButton.visible = true
Don Gagne's avatar
Don Gagne committed
66
        } else if (buttons & StandardButton.Retry) {
67 68
            _acceptButton.text = qsTr("Retry")
            _acceptButton.visible = true
Don Gagne's avatar
Don Gagne committed
69
        } else if (buttons & StandardButton.Reset) {
70 71
            _acceptButton.text = qsTr("Reset")
            _acceptButton.visible = true
Don Gagne's avatar
Don Gagne committed
72
        } else if (buttons & StandardButton.RestoreToDefaults) {
73 74
            _acceptButton.text = qsTr("Restore to Defaults")
            _acceptButton.visible = true
Don Gagne's avatar
Don Gagne committed
75
        } else if (buttons & StandardButton.Ignore) {
76 77
            _acceptButton.text = qsTr("Ignore")
            _acceptButton.visible = true
Don Gagne's avatar
Don Gagne committed
78
        }
Don Gagne's avatar
Don Gagne committed
79 80 81

        // Reject role buttons
        if (buttons & StandardButton.Cancel) {
82 83
            _rejectButton.text = qsTr("Cancel")
            _rejectButton.visible = true
Don Gagne's avatar
Don Gagne committed
84
        } else if (buttons & StandardButton.Close) {
85 86
            _rejectButton.text = qsTr("Close")
            _rejectButton.visible = true
Don Gagne's avatar
Don Gagne committed
87
        } else if (buttons & StandardButton.No) {
88 89
            _rejectButton.text = qsTr("No")
            _rejectButton.visible = true
Don Gagne's avatar
Don Gagne committed
90
        } else if (buttons & StandardButton.NoToAll) {
91 92
            _rejectButton.text = qsTr("No to All")
            _rejectButton.visible = true
Don Gagne's avatar
Don Gagne committed
93
        } else if (buttons & StandardButton.Abort) {
94 95
            _rejectButton.text = qsTr("Abort")
            _rejectButton.visible = true
96 97 98
        }
    }

99
    function _checkForEarlyDialog(title) {
100
        if (!completedSignalled) {
101
            console.warn(qsTr("showDialog called before QGCView.completed signalled"), title)
102 103 104
        }
    }

105 106 107
    /// Shows a QGCViewDialog component
    ///     @param compoent QGCViewDialog component
    ///     @param title Title for dialog
108
    ///     @param charWidth Width of dialog in characters
109
    ///     @param buttons Buttons to show in dialog using StandardButton enum
110 111 112 113

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

114
    function showDialog(component, title, charWidth, buttons) {
115
        if (_checkForEarlyDialog(title)) {
116 117 118
            return
        }

119 120
        _rejectButton.enabled = true
        _acceptButton.enabled = true
121

122 123
        _dialogCharWidth = charWidth
        _dialogTitle = title
124

125
        _setupDialogButtons(buttons)
Don Gagne's avatar
Don Gagne committed
126

127
        _dialogComponent = component
128
        viewPanel.enabled = false
129
        _dialogOverlay.visible = true
Don Gagne's avatar
Don Gagne committed
130 131
    }

Don Gagne's avatar
Don Gagne committed
132
    function showMessage(title, message, buttons) {
133
        if (_checkForEarlyDialog(title)) {
134 135 136
            return
        }

137 138
        _rejectButton.enabled = true
        _acceptButton.enabled = true
139

140 141 142
        _dialogCharWidth = showDialogDefaultWidth
        _dialogTitle = title
        _messageDialogText = message
143

144
        _setupDialogButtons(buttons)
Don Gagne's avatar
Don Gagne committed
145

146
        _dialogComponent = _messageDialog
147
        viewPanel.enabled = false
148
        _dialogOverlay.visible = true
Don Gagne's avatar
Don Gagne committed
149 150
    }

Don Gagne's avatar
Don Gagne committed
151
    function hideDialog() {
152
        viewPanel.enabled = true
153 154
        _dialogComponent = null
        _dialogOverlay.visible = false
Don Gagne's avatar
Don Gagne committed
155 156
    }

157 158
    QGCPalette { id: _qgcPal; colorGroupEnabled: true }
    QGCLabel { id: _textMeasure; text: "X"; visible: false }
Don Gagne's avatar
Don Gagne committed
159

160 161
    property real defaultTextHeight: _textMeasure.contentHeight
    property real defaultTextWidth:  _textMeasure.contentWidth
Don Gagne's avatar
Don Gagne committed
162 163

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

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

169
    property string _messageDialogText
Don Gagne's avatar
Don Gagne committed
170

171
    property Component _dialogComponent
Don Gagne's avatar
Don Gagne committed
172

173
    function _signalCompleted() {
Don Gagne's avatar
Don Gagne committed
174 175 176
        // 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.
177 178
        if (!completedSignalled && width != 0 && height != 0) {
            completedSignalled = true
Don Gagne's avatar
Don Gagne committed
179 180 181 182
            completed()
        }
    }

183 184 185
    Component.onCompleted:  _signalCompleted()
    onWidthChanged:         _signalCompleted()
    onHeightChanged:        _signalCompleted()
Don Gagne's avatar
Don Gagne committed
186 187

    Connections {
188
        target: _dialogComponentLoader.item
Don Gagne's avatar
Don Gagne committed
189

190
        onHideDialog: _rootItem.hideDialog()
Don Gagne's avatar
Don Gagne committed
191 192 193
    }

    Item {
194
        id:             _dialogOverlay
Don Gagne's avatar
Don Gagne committed
195 196 197 198 199 200
        visible:        false
        anchors.fill:   parent
        z:              5000

        // This covers the parent with an transparent section
        Rectangle {
201
            id:             _transparentSection
202
            height:         ScreenTools.availableHeight ? ScreenTools.availableHeight : parent.height
Don Gagne's avatar
Don Gagne committed
203 204
            anchors.bottom: parent.bottom
            anchors.left:   parent.left
205
            anchors.right:  _dialogPanel.left
Don Gagne's avatar
Don Gagne committed
206
            opacity:        0.0
207
            color:          _qgcPal.window
Don Gagne's avatar
Don Gagne committed
208 209 210 211
        }

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

            Rectangle {
221
                id:     _header
Don Gagne's avatar
Don Gagne committed
222
                width:  parent.width
223 224
                height: _acceptButton.visible ? _acceptButton.height : _rejectButton.height
                color:  _qgcPal.windowShade
Don Gagne's avatar
Don Gagne committed
225

226 227
                function _hidePanel() {
                    _fullPanel.visible = false
Don Gagne's avatar
Don Gagne committed
228 229 230
                }

                QGCLabel {
Don Gagne's avatar
Don Gagne committed
231
                    x:                  defaultTextWidth
Don Gagne's avatar
Don Gagne committed
232 233
                    height:             parent.height
                    verticalAlignment:	Text.AlignVCenter
234
                    text:               _dialogTitle
Don Gagne's avatar
Don Gagne committed
235 236 237
                }

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

242 243
                    onClicked: {
                        enabled = false // prevent multiple clicks
244 245 246 247 248
                        _dialogComponentLoader.item.reject()
                        if (!viewPanel.enabled) {
                            // Dialog was not closed, re-enable button
                            enabled = true
                        }
249
                    }
Don Gagne's avatar
Don Gagne committed
250 251 252
                }

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

257
                    onClicked: {
258
                        enabled = false // prevent multiple clicks
259 260 261 262 263
                       _dialogComponentLoader.item.accept()
                        if (!viewPanel.enabled) {
                            // Dialog was not closed, re-enable button
                            enabled = true
                        }
264
                    }
Don Gagne's avatar
Don Gagne committed
265 266 267 268
                }
            }

            Item {
269
                id:             _spacer
Don Gagne's avatar
Don Gagne committed
270 271
                width:          10
                height:         10
272
                anchors.top:    _header.bottom
Don Gagne's avatar
Don Gagne committed
273 274 275
            }

            Loader {
276
                id:                 _dialogComponentLoader
Don Gagne's avatar
Don Gagne committed
277 278 279
                anchors.margins:    5
                anchors.left:       parent.left
                anchors.right:      parent.right
280
                anchors.top:        _spacer.bottom
Don Gagne's avatar
Don Gagne committed
281
                anchors.bottom:     parent.bottom
282
                sourceComponent:    _dialogComponent
283

284 285
                property bool acceptAllowed: _acceptButton.visible
                property bool rejectAllowed: _rejectButton.visible
Don Gagne's avatar
Don Gagne committed
286 287 288
            }
        } // Rectangle - Dialog panel
    } // Item - Dialog overlay
Don Gagne's avatar
Don Gagne committed
289 290

    Component {
291
        id: _messageDialog
Don Gagne's avatar
Don Gagne committed
292 293

        QGCViewMessage {
294
            message: _messageDialogText
Don Gagne's avatar
Don Gagne committed
295 296
        }
    }
297
}