QGCView.qml 11.9 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 {
Don Gagne's avatar
Don Gagne committed
25 26
    id: __rootItem

27
    property var qgcView:               __rootItem  ///< Used by Fact controls for validation dialogs
Don Gagne's avatar
Don Gagne committed
28
    property bool completedSignalled:   false
29
    property real topDialogMargin:      0           ///< Set a top margin for dialog
Don Gagne's avatar
Don Gagne committed
30

31
    property var viewPanel
Don Gagne's avatar
Don Gagne committed
32

Don Gagne's avatar
Don Gagne committed
33 34 35 36 37
    /// 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
38
    function __setupDialogButtons(buttons) {
Don Gagne's avatar
Don Gagne committed
39 40 41
        __acceptButton.visible = false
        __rejectButton.visible = false

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

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

100 101 102 103 104 105
    function __stopAllAnimations() {
        if (__animateHideDialog.running) {
            __animateHideDialog.stop()
        }
    }

Don Gagne's avatar
Don Gagne committed
106
    function __checkForEarlyDialog(title) {
107
        if (!completedSignalled) {
108
            console.warn(qsTr("showDialog|Message called before QGCView.completed signalled"), title)
109 110 111
        }
    }

112 113 114
    /// Shows a QGCViewDialog component
    ///     @param compoent QGCViewDialog component
    ///     @param title Title for dialog
115
    ///     @param charWidth Width of dialog in characters
116
    ///     @param buttons Buttons to show in dialog using StandardButton enum
117 118 119 120

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

121
    function showDialog(component, title, charWidth, buttons) {
Don Gagne's avatar
Don Gagne committed
122
        if (__checkForEarlyDialog(title)) {
123 124 125
            return
        }

126 127 128
        __rejectButton.enabled = true
        __acceptButton.enabled = true

129 130
        __stopAllAnimations()

Don Gagne's avatar
Don Gagne committed
131 132 133 134
        __dialogCharWidth = charWidth
        __dialogTitle = title

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

Don Gagne's avatar
Don Gagne committed
136
        __dialogComponent = component
137
        viewPanel.enabled = false
Don Gagne's avatar
Don Gagne committed
138
        __dialogOverlay.visible = true
Don Gagne's avatar
Don Gagne committed
139

140
        //__dialogComponentLoader.item.forceActiveFocus()
141

Don Gagne's avatar
Don Gagne committed
142
        __animateShowDialog.start()
Don Gagne's avatar
Don Gagne committed
143 144
    }

Don Gagne's avatar
Don Gagne committed
145
    function showMessage(title, message, buttons) {
Don Gagne's avatar
Don Gagne committed
146
        if (__checkForEarlyDialog(title)) {
147 148 149
            return
        }

150 151 152
        __rejectButton.enabled = true
        __acceptButton.enabled = true

153 154
        __stopAllAnimations()

155
        __dialogCharWidth = showDialogDefaultWidth
Don Gagne's avatar
Don Gagne committed
156 157 158 159 160 161
        __dialogTitle = title
        __messageDialogText = message

        __setupDialogButtons(buttons)

        __dialogComponent = __messageDialog
162
        viewPanel.enabled = false
Don Gagne's avatar
Don Gagne committed
163
        __dialogOverlay.visible = true
Don Gagne's avatar
Don Gagne committed
164

165 166
        __dialogComponentLoader.item.forceActiveFocus()

Don Gagne's avatar
Don Gagne committed
167
        __animateShowDialog.start()
Don Gagne's avatar
Don Gagne committed
168 169
    }

Don Gagne's avatar
Don Gagne committed
170
    function hideDialog() {
171
        //__dialogComponentLoader.item.focus = false
172
        viewPanel.enabled = true
Don Gagne's avatar
Don Gagne committed
173
        __animateHideDialog.start()
Don Gagne's avatar
Don Gagne committed
174 175 176 177 178
    }

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

Don Gagne's avatar
Don Gagne committed
179 180
    property real defaultTextHeight: __textMeasure.contentHeight
    property real defaultTextWidth:  __textMeasure.contentWidth
Don Gagne's avatar
Don Gagne committed
181 182 183 184 185 186 187

    /// 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
188 189
    property string __messageDialogText

Don Gagne's avatar
Don Gagne committed
190 191
    property Component __dialogComponent

Don Gagne's avatar
Don Gagne committed
192 193 194 195
    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.
196 197
        if (!completedSignalled && width != 0 && height != 0) {
            completedSignalled = true
Don Gagne's avatar
Don Gagne committed
198 199 200 201 202 203 204
            completed()
        }
    }

    Component.onCompleted:  __signalCompleted()
    onWidthChanged:         __signalCompleted()
    onHeightChanged:        __signalCompleted()
Don Gagne's avatar
Don Gagne committed
205 206 207 208

    Connections {
        target: __dialogComponentLoader.item

Don Gagne's avatar
Don Gagne committed
209
        onHideDialog: __rootItem.hideDialog()
Don Gagne's avatar
Don Gagne committed
210 211 212 213 214 215 216 217
    }

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

Don Gagne's avatar
Don Gagne committed
218 219 220 221 222 223 224 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
        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
268 269
        // This covers the parent with an transparent section
        Rectangle {
Don Gagne's avatar
Don Gagne committed
270
            id:             __transparentSection
271
            height:         ScreenTools.availableHeight ? ScreenTools.availableHeight : parent.height
Don Gagne's avatar
Don Gagne committed
272 273
            anchors.bottom: parent.bottom
            anchors.left:   parent.left
Don Gagne's avatar
Don Gagne committed
274
            anchors.right:  __dialogPanel.left
Don Gagne's avatar
Don Gagne committed
275
            opacity:        0.0
Don Gagne's avatar
Don Gagne committed
276 277 278 279 280
            color:          __qgcPal.window
        }

        // This is the main dialog panel which is anchored to the right edge
        Rectangle {
281
            id:                 __dialogPanel
282
            width:              __dialogCharWidth == showDialogFullWidth ? parent.width : defaultTextWidth * __dialogCharWidth
283
            anchors.topMargin:  topDialogMargin
284
            height:             ScreenTools.availableHeight ? ScreenTools.availableHeight : parent.height
285
            anchors.bottom:     parent.bottom
286
            anchors.right:      parent.right
287
            color:              __qgcPal.windowShadeDark
Don Gagne's avatar
Don Gagne committed
288 289 290 291

            Rectangle {
                id:     __header
                width:  parent.width
Don Gagne's avatar
Don Gagne committed
292
                height: __acceptButton.visible ? __acceptButton.height : __rejectButton.height
Don Gagne's avatar
Don Gagne committed
293 294 295 296 297 298 299
                color:  __qgcPal.windowShade

                function __hidePanel() {
                    __fullPanel.visible = false
                }

                QGCLabel {
Don Gagne's avatar
Don Gagne committed
300
                    x:                  defaultTextWidth
Don Gagne's avatar
Don Gagne committed
301 302 303 304 305 306 307
                    height:             parent.height
                    verticalAlignment:	Text.AlignVCenter
                    text:               __dialogTitle
                }

                QGCButton {
                    id:             __rejectButton
Don Gagne's avatar
Don Gagne committed
308
                    anchors.right:  __acceptButton.visible ?  __acceptButton.left : parent.right
Don Gagne's avatar
Don Gagne committed
309 310
                    anchors.bottom: parent.bottom

311 312 313 314
                    onClicked: {
                        enabled = false // prevent multiple clicks
                        __dialogComponentLoader.item.reject()
                    }
Don Gagne's avatar
Don Gagne committed
315 316 317 318 319 320 321
                }

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

322
                    onClicked: {
323
                        enabled = false // prevent multiple clicks
324 325
                       __dialogComponentLoader.item.accept()
                    }
Don Gagne's avatar
Don Gagne committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
                }
            }

            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
344 345 346

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

    Component {
        id: __messageDialog

        QGCViewMessage {
            message: __messageDialogText
        }
    }
358
}