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

Don Gagne's avatar
Don Gagne committed
40 41
    property Component viewComponent

Don Gagne's avatar
Don Gagne committed
42 43 44 45 46
    /// 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
47
    function __setupDialogButtons(buttons) {
Don Gagne's avatar
Don Gagne committed
48 49 50
        __acceptButton.visible = false
        __rejectButton.visible = false

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

        // Reject role buttons
        if (buttons & StandardButton.Cancel) {
            __rejectButton.text = "Cancel"
            __rejectButton.visible = true
        } else if (buttons & StandardButton.Close) {
            __rejectButton.text = "Cancel"
            __rejectButton.visible = true
        } else if (buttons & StandardButton.No) {
            __rejectButton.text = "No"
            __rejectButton.visible = true
        } else if (buttons & StandardButton.NoToAll) {
            __rejectButton.text = "No to All"
            __rejectButton.visible = true
        } else if (buttons & StandardButton.Abort) {
            __rejectButton.text = "Abort"
            __rejectButton.visible = true
        }
Don Gagne's avatar
Don Gagne committed
107 108
    }

109 110 111 112 113 114
    function __stopAllAnimations() {
        if (__animateHideDialog.running) {
            __animateHideDialog.stop()
        }
    }

Don Gagne's avatar
Don Gagne committed
115
    function showDialog(component, title, charWidth, buttons) {
116 117
        __stopAllAnimations()

Don Gagne's avatar
Don Gagne committed
118 119 120 121
        __dialogCharWidth = charWidth
        __dialogTitle = title

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

Don Gagne's avatar
Don Gagne committed
123 124 125
        __dialogComponent = component
        __viewPanel.enabled = false
        __dialogOverlay.visible = true
Don Gagne's avatar
Don Gagne committed
126 127

        __animateShowDialog.start()
Don Gagne's avatar
Don Gagne committed
128 129
    }

Don Gagne's avatar
Don Gagne committed
130
    function showMessage(title, message, buttons) {
131 132
        __stopAllAnimations()

Don Gagne's avatar
Don Gagne committed
133 134 135 136 137 138 139 140 141
        __dialogCharWidth = 50
        __dialogTitle = title
        __messageDialogText = message

        __setupDialogButtons(buttons)

        __dialogComponent = __messageDialog
        __viewPanel.enabled = false
        __dialogOverlay.visible = true
Don Gagne's avatar
Don Gagne committed
142 143

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

Don Gagne's avatar
Don Gagne committed
146
    function hideDialog() {
Don Gagne's avatar
Don Gagne committed
147
        __viewPanel.enabled = true
Don Gagne's avatar
Don Gagne committed
148
        __animateHideDialog.start()
Don Gagne's avatar
Don Gagne committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162
    }

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

    property real __textHeight: __textMeasure.contentHeight
    property real __textWidth:  __textMeasure.contentWidth

    /// 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
163 164
    property string __messageDialogText

Don Gagne's avatar
Don Gagne committed
165 166
    property Component __dialogComponent

Don Gagne's avatar
Don Gagne committed
167
    Component.onCompleted: completed()
Don Gagne's avatar
Don Gagne committed
168 169 170 171

    Connections {
        target: __viewPanel.item

Don Gagne's avatar
Don Gagne committed
172 173 174
        onShowDialog:   __rootItem.showDialog(component, title, charWidth, buttons)
        onShowMessage:  __rootItem.showMessage(title, message, buttons)
        onHideDialog:   __rootItem.hideDialog()
Don Gagne's avatar
Don Gagne committed
175 176 177 178 179
    }

    Connections {
        target: __dialogComponentLoader.item

Don Gagne's avatar
Don Gagne committed
180
        onHideDialog: __rootItem.hideDialog()
Don Gagne's avatar
Don Gagne committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    }

    Loader {
        id:                 __viewPanel
        anchors.fill:       parent
        focus:              true
        sourceComponent:    viewComponent
    }

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

Don Gagne's avatar
Don Gagne committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 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
        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
246 247
        // This covers the parent with an transparent section
        Rectangle {
Don Gagne's avatar
Don Gagne committed
248
            id:             __transparentSection
Don Gagne's avatar
Don Gagne committed
249 250 251
            anchors.top:    parent.top
            anchors.bottom: parent.bottom
            anchors.left:   parent.left
Don Gagne's avatar
Don Gagne committed
252 253
            width:          parent.width
            opacity:        0.0
Don Gagne's avatar
Don Gagne committed
254 255 256 257 258 259
            color:          __qgcPal.window
        }

        // This is the main dialog panel which is anchored to the right edge
        Rectangle {
            id:             __dialogPanel
Don Gagne's avatar
Don Gagne committed
260
            width:          __dialogCharWidth == -1 ? parent.width : __textWidth * __dialogCharWidth
Don Gagne's avatar
Don Gagne committed
261
            height:         parent.height
Don Gagne's avatar
Don Gagne committed
262
            anchors.left:   __transparentSection.right
Don Gagne's avatar
Don Gagne committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
            color:          __qgcPal.windowShadeDark

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

                function __hidePanel() {
                    __fullPanel.visible = false
                }

                QGCLabel {
                    x:                  __textWidth
                    height:             parent.height
                    verticalAlignment:	Text.AlignVCenter
                    text:               __dialogTitle
                }

                QGCButton {
                    id:             __rejectButton
                    anchors.right:  __acceptButton.left
                    anchors.bottom: parent.bottom

                    onClicked: __dialogComponentLoader.item.reject()
                }

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

                    onClicked: __dialogComponentLoader.item.accept()
                }
            }

            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
            }
        } // Rectangle - Dialog panel
    } // Item - Dialog overlay
Don Gagne's avatar
Don Gagne committed
317 318 319 320 321 322 323 324

    Component {
        id: __messageDialog

        QGCViewMessage {
            message: __messageDialogText
        }
    }
Don Gagne's avatar
Don Gagne committed
325
}