QGCView.qml 12.1 KB
Newer Older
Don Gagne's avatar
Don Gagne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*=====================================================================

 QGroundControl Open Source Ground Control Station

 (c) 2009 - 2015 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

 This file is part of the QGROUNDCONTROL project

 QGROUNDCONTROL is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 QGROUNDCONTROL is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

 ======================================================================*/

/// @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
37
FactPanel {
Don Gagne's avatar
Don Gagne committed
38 39
    id: __rootItem

40
    property var qgcView:               __rootItem  ///< Used by Fact controls for validation dialogs
Don Gagne's avatar
Don Gagne committed
41
    property bool completedSignalled:   false
42
    property real topDialogMargin:      0           ///< Set a top margin for dialog
Don Gagne's avatar
Don Gagne committed
43

44
    property var viewPanel
Don Gagne's avatar
Don Gagne committed
45

Don Gagne's avatar
Don Gagne committed
46 47 48 49 50
    /// 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

Don Gagne's avatar
Don Gagne committed
51
    function __setupDialogButtons(buttons) {
Don Gagne's avatar
Don Gagne committed
52 53 54
        __acceptButton.visible = false
        __rejectButton.visible = false

Don Gagne's avatar
Don Gagne committed
55
        // Accept role buttons
Don Gagne's avatar
Don Gagne committed
56
        if (buttons & StandardButton.Ok) {
57
            __acceptButton.text = qsTr("Ok")
Don Gagne's avatar
Don Gagne committed
58 59
            __acceptButton.visible = true
        } else if (buttons & StandardButton.Open) {
60
            __acceptButton.text = qsTr("Open")
Don Gagne's avatar
Don Gagne committed
61 62
            __acceptButton.visible = true
        } else if (buttons & StandardButton.Save) {
63
            __acceptButton.text = qsTr("Save")
Don Gagne's avatar
Don Gagne committed
64 65
            __acceptButton.visible = true
        } else if (buttons & StandardButton.Apply) {
66
            __acceptButton.text = qsTr("Apply")
Don Gagne's avatar
Don Gagne committed
67 68
            __acceptButton.visible = true
        } else if (buttons & StandardButton.Open) {
69
            __acceptButton.text = qsTr("Open")
Don Gagne's avatar
Don Gagne committed
70 71
            __acceptButton.visible = true
        } else if (buttons & StandardButton.SaveAll) {
72
            __acceptButton.text = qsTr("Save All")
Don Gagne's avatar
Don Gagne committed
73 74
            __acceptButton.visible = true
        } else if (buttons & StandardButton.Yes) {
75
            __acceptButton.text = qsTr("Yes")
Don Gagne's avatar
Don Gagne committed
76 77
            __acceptButton.visible = true
        } else if (buttons & StandardButton.YesToAll) {
78
            __acceptButton.text = qsTr("Yes to All")
Don Gagne's avatar
Don Gagne committed
79 80
            __acceptButton.visible = true
        } else if (buttons & StandardButton.Retry) {
81
            __acceptButton.text = qsTr("Retry")
Don Gagne's avatar
Don Gagne committed
82 83
            __acceptButton.visible = true
        } else if (buttons & StandardButton.Reset) {
84
            __acceptButton.text = qsTr("Reset")
Don Gagne's avatar
Don Gagne committed
85 86
            __acceptButton.visible = true
        } else if (buttons & StandardButton.RestoreToDefaults) {
87
            __acceptButton.text = qsTr("Restore to Defaults")
Don Gagne's avatar
Don Gagne committed
88 89
            __acceptButton.visible = true
        } else if (buttons & StandardButton.Ignore) {
90
            __acceptButton.text = qsTr("Ignore")
Don Gagne's avatar
Don Gagne committed
91 92
            __acceptButton.visible = true
        }
Don Gagne's avatar
Don Gagne committed
93 94 95

        // Reject role buttons
        if (buttons & StandardButton.Cancel) {
96
            __rejectButton.text = qsTr("Cancel")
Don Gagne's avatar
Don Gagne committed
97 98
            __rejectButton.visible = true
        } else if (buttons & StandardButton.Close) {
99
            __rejectButton.text = qsTr("Close")
Don Gagne's avatar
Don Gagne committed
100 101
            __rejectButton.visible = true
        } else if (buttons & StandardButton.No) {
102
            __rejectButton.text = qsTr("No")
Don Gagne's avatar
Don Gagne committed
103 104
            __rejectButton.visible = true
        } else if (buttons & StandardButton.NoToAll) {
105
            __rejectButton.text = qsTr("No to All")
Don Gagne's avatar
Don Gagne committed
106 107
            __rejectButton.visible = true
        } else if (buttons & StandardButton.Abort) {
108
            __rejectButton.text = qsTr("Abort")
Don Gagne's avatar
Don Gagne committed
109 110
            __rejectButton.visible = true
        }
Don Gagne's avatar
Don Gagne committed
111 112
    }

113 114 115 116 117 118
    function __stopAllAnimations() {
        if (__animateHideDialog.running) {
            __animateHideDialog.stop()
        }
    }

Don Gagne's avatar
Don Gagne committed
119
    function __checkForEarlyDialog(title) {
120
        if (!completedSignalled) {
121
            console.warn(qsTr("showDialog|Message called before QGCView.completed signalled"), title)
122 123 124
        }
    }

125 126 127
    /// Shows a QGCViewDialog component
    ///     @param compoent QGCViewDialog component
    ///     @param title Title for dialog
128
    ///     @param charWidth Width of dialog in characters
129
    ///     @param buttons Buttons to show in dialog using StandardButton enum
130 131 132 133

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

134
    function showDialog(component, title, charWidth, buttons) {
Don Gagne's avatar
Don Gagne committed
135
        if (__checkForEarlyDialog(title)) {
136 137 138
            return
        }

139 140
        __stopAllAnimations()

Don Gagne's avatar
Don Gagne committed
141 142 143 144
        __dialogCharWidth = charWidth
        __dialogTitle = title

        __setupDialogButtons(buttons)
Don Gagne's avatar
Don Gagne committed
145

Don Gagne's avatar
Don Gagne committed
146
        __dialogComponent = component
147
        viewPanel.enabled = false
Don Gagne's avatar
Don Gagne committed
148
        __dialogOverlay.visible = true
Don Gagne's avatar
Don Gagne committed
149

150
        //__dialogComponentLoader.item.forceActiveFocus()
151

Don Gagne's avatar
Don Gagne committed
152
        __animateShowDialog.start()
Don Gagne's avatar
Don Gagne committed
153 154
    }

Don Gagne's avatar
Don Gagne committed
155
    function showMessage(title, message, buttons) {
Don Gagne's avatar
Don Gagne committed
156
        if (__checkForEarlyDialog(title)) {
157 158 159
            return
        }

160 161
        __stopAllAnimations()

162
        __dialogCharWidth = showDialogDefaultWidth
Don Gagne's avatar
Don Gagne committed
163 164 165 166 167 168
        __dialogTitle = title
        __messageDialogText = message

        __setupDialogButtons(buttons)

        __dialogComponent = __messageDialog
169
        viewPanel.enabled = false
Don Gagne's avatar
Don Gagne committed
170
        __dialogOverlay.visible = true
Don Gagne's avatar
Don Gagne committed
171

172 173
        __dialogComponentLoader.item.forceActiveFocus()

Don Gagne's avatar
Don Gagne committed
174
        __animateShowDialog.start()
Don Gagne's avatar
Don Gagne committed
175 176
    }

Don Gagne's avatar
Don Gagne committed
177
    function hideDialog() {
178
        //__dialogComponentLoader.item.focus = false
179
        viewPanel.enabled = true
Don Gagne's avatar
Don Gagne committed
180
        __animateHideDialog.start()
Don Gagne's avatar
Don Gagne committed
181 182 183 184 185
    }

    QGCPalette { id: __qgcPal; colorGroupEnabled: true }
    QGCLabel { id: __textMeasure; text: "X"; visible: false }

Don Gagne's avatar
Don Gagne committed
186 187
    property real defaultTextHeight: __textMeasure.contentHeight
    property real defaultTextWidth:  __textMeasure.contentWidth
Don Gagne's avatar
Don Gagne committed
188 189 190 191 192 193 194

    /// The width of the dialog panel in characters
    property int __dialogCharWidth: 75

    /// The title for the dialog panel
    property string __dialogTitle

Don Gagne's avatar
Don Gagne committed
195 196
    property string __messageDialogText

Don Gagne's avatar
Don Gagne committed
197 198
    property Component __dialogComponent

Don Gagne's avatar
Don Gagne committed
199 200 201 202
    function __signalCompleted() {
        // 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.
203 204
        if (!completedSignalled && width != 0 && height != 0) {
            completedSignalled = true
Don Gagne's avatar
Don Gagne committed
205 206 207 208 209 210 211
            completed()
        }
    }

    Component.onCompleted:  __signalCompleted()
    onWidthChanged:         __signalCompleted()
    onHeightChanged:        __signalCompleted()
Don Gagne's avatar
Don Gagne committed
212 213 214 215

    Connections {
        target: __dialogComponentLoader.item

Don Gagne's avatar
Don Gagne committed
216
        onHideDialog: __rootItem.hideDialog()
Don Gagne's avatar
Don Gagne committed
217 218 219 220 221 222 223 224
    }

    Item {
        id:             __dialogOverlay
        visible:        false
        anchors.fill:   parent
        z:              5000

Don Gagne's avatar
Don Gagne committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
        readonly property int __animationDuration: 200

        ParallelAnimation {
            id: __animateShowDialog


            NumberAnimation {
                target:     __transparentSection
                properties: "opacity"
                from:       0.0
                to:         0.8
                duration:   __dialogOverlay.__animationDuration
            }

            NumberAnimation {
                target:     __transparentSection
                properties: "width"
                from:       __dialogOverlay.width
                to:         __dialogOverlay.width - __dialogPanel.width
                duration:   __dialogOverlay.__animationDuration
            }
        }

        ParallelAnimation {
            id: __animateHideDialog

            NumberAnimation {
                target:     __transparentSection
                properties: "opacity"
                from:       0.8
                to:         0.0
                duration:   __dialogOverlay.__animationDuration
            }

            NumberAnimation {
                target:     __transparentSection
                properties: "width"
                from:       __dialogOverlay.width - __dialogPanel.width
                to:         __dialogOverlay.width
                duration:   __dialogOverlay.__animationDuration
            }

            onRunningChanged: {
                if (!running) {
                    __dialogComponent = null
                    __dialogOverlay.visible = false
                }
            }
        }

Don Gagne's avatar
Don Gagne committed
275 276
        // This covers the parent with an transparent section
        Rectangle {
Don Gagne's avatar
Don Gagne committed
277
            id:             __transparentSection
278
            height:         ScreenTools.availableHeight ? ScreenTools.availableHeight : parent.height
Don Gagne's avatar
Don Gagne committed
279 280
            anchors.bottom: parent.bottom
            anchors.left:   parent.left
Don Gagne's avatar
Don Gagne committed
281
            anchors.right:  __dialogPanel.left
Don Gagne's avatar
Don Gagne committed
282
            opacity:        0.0
Don Gagne's avatar
Don Gagne committed
283 284 285 286 287
            color:          __qgcPal.window
        }

        // This is the main dialog panel which is anchored to the right edge
        Rectangle {
288
            id:                 __dialogPanel
289
            width:              __dialogCharWidth == showDialogFullWidth ? parent.width : defaultTextWidth * __dialogCharWidth
290
            anchors.topMargin:  topDialogMargin
291
            height:             ScreenTools.availableHeight ? ScreenTools.availableHeight : parent.height
292
            anchors.bottom:     parent.bottom
293
            anchors.right:      parent.right
294
            color:              __qgcPal.windowShadeDark
Don Gagne's avatar
Don Gagne committed
295 296 297 298 299 300 301 302 303 304 305 306

            Rectangle {
                id:     __header
                width:  parent.width
                height: __acceptButton.height
                color:  __qgcPal.windowShade

                function __hidePanel() {
                    __fullPanel.visible = false
                }

                QGCLabel {
Don Gagne's avatar
Don Gagne committed
307
                    x:                  defaultTextWidth
Don Gagne's avatar
Don Gagne committed
308 309 310 311 312 313 314
                    height:             parent.height
                    verticalAlignment:	Text.AlignVCenter
                    text:               __dialogTitle
                }

                QGCButton {
                    id:             __rejectButton
Don Gagne's avatar
Don Gagne committed
315
                    anchors.right:  __acceptButton.visible ?  __acceptButton.left : parent.right
Don Gagne's avatar
Don Gagne committed
316 317 318 319 320 321 322 323 324 325
                    anchors.bottom: parent.bottom

                    onClicked: __dialogComponentLoader.item.reject()
                }

                QGCButton {
                    id:             __acceptButton
                    anchors.right:  parent.right
                    primary:        true

326 327 328
                    onClicked: {
                       __dialogComponentLoader.item.accept()
                    }
Don Gagne's avatar
Don Gagne committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
                }
            }

            Item {
                id:             __spacer
                width:          10
                height:         10
                anchors.top:    __header.bottom
            }

            Loader {
                id:                 __dialogComponentLoader
                anchors.margins:    5
                anchors.left:       parent.left
                anchors.right:      parent.right
                anchors.top:        __spacer.bottom
                anchors.bottom:     parent.bottom
                sourceComponent:    __dialogComponent
347 348 349

                property bool acceptAllowed: __acceptButton.visible
                property bool rejectAllowed: __rejectButton.visible
Don Gagne's avatar
Don Gagne committed
350 351 352
            }
        } // Rectangle - Dialog panel
    } // Item - Dialog overlay
Don Gagne's avatar
Don Gagne committed
353 354 355 356 357 358 359 360

    Component {
        id: __messageDialog

        QGCViewMessage {
            message: __messageDialogText
        }
    }
361
}