FlightDisplayView.qml 13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*=====================================================================

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/>.

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

Don Gagne's avatar
Don Gagne committed
24
import QtQuick                  2.5
25 26 27
import QtQuick.Controls         1.3
import QtQuick.Controls.Styles  1.2
import QtQuick.Dialogs          1.2
28
import QtLocation               5.3
29
import QtPositioning            5.2
30

31
import QGroundControl               1.0
Don Gagne's avatar
Don Gagne committed
32
import QGroundControl.FlightDisplay 1.0
33 34 35 36
import QGroundControl.FlightMap     1.0
import QGroundControl.ScreenTools   1.0
import QGroundControl.Controls      1.0
import QGroundControl.Palette       1.0
37
import QGroundControl.Vehicle       1.0
38
import QGroundControl.Controllers   1.0
39 40 41 42 43

/// Flight Display View
Item {
    id: root

44
    QGCPalette { id: qgcPal; colorGroupEnabled: enabled }
45

Don Gagne's avatar
Don Gagne committed
46
    property real availableHeight: parent.height
dogmaphobic's avatar
dogmaphobic committed
47
    property bool interactive:    true
dogmaphobic's avatar
dogmaphobic committed
48 49 50 51

    readonly property bool isBackgroundDark: _mainIsMap ? (_flightMap ? _flightMap.isSatelliteMap : true) : true

    property var _activeVehicle:  multiVehicleManager.activeVehicle
52

53
    readonly property var  _defaultVehicleCoordinate:   mainWindow.tabletPosition
54 55 56 57 58 59 60
    readonly property real _defaultRoll:                0
    readonly property real _defaultPitch:               0
    readonly property real _defaultHeading:             0
    readonly property real _defaultAltitudeWGS84:       0
    readonly property real _defaultGroundSpeed:         0
    readonly property real _defaultAirSpeed:            0
    readonly property real _defaultClimbRate:           0
61

62 63
    readonly property string _mapName:                  "FlightDisplayView"
    readonly property string _showMapBackgroundKey:     "/showMapBackground"
64
    readonly property string _mainIsMapKey:             "MainFlyWindowIsMap"
dogmaphobic's avatar
dogmaphobic committed
65
    readonly property string _PIPVisibleKey:            "IsPIPVisible"
Don Gagne's avatar
Don Gagne committed
66

dogmaphobic's avatar
dogmaphobic committed
67 68
    property bool _mainIsMap:           QGroundControl.loadBoolGlobalSetting(_mainIsMapKey,  true)
    property bool _isPipVisible:        QGroundControl.loadBoolGlobalSetting(_PIPVisibleKey, true)
Don Gagne's avatar
Don Gagne committed
69

70 71
    property real _roll:                _activeVehicle ? (isNaN(_activeVehicle.roll)    ? _defaultRoll    : _activeVehicle.roll)    : _defaultRoll
    property real _pitch:               _activeVehicle ? (isNaN(_activeVehicle.pitch)   ? _defaultPitch   : _activeVehicle.pitch)   : _defaultPitch
72
    property real _heading:             _activeVehicle ? (isNaN(_activeVehicle.heading) ? _defaultHeading : _activeVehicle.heading) : _defaultHeading
73

74
    property var  _vehicleCoordinate:   _activeVehicle ? (_activeVehicle.coordinateValid ? _activeVehicle.coordinate : _defaultVehicleCoordinate) : _defaultVehicleCoordinate
75

76
    property real _altitudeWGS84:       _activeVehicle ? _activeVehicle.altitudeWGS84 : _defaultAltitudeWGS84
77 78 79
    property real _groundSpeed:         _activeVehicle ? _activeVehicle.groundSpeed   : _defaultGroundSpeed
    property real _airSpeed:            _activeVehicle ? _activeVehicle.airSpeed      : _defaultAirSpeed
    property real _climbRate:           _activeVehicle ? _activeVehicle.climbRate     : _defaultClimbRate
80

81 82 83
    property var  _flightMap:           null
    property var  _flightVideo:         null
    property var  _savedZoomLevel:      0
Don Gagne's avatar
Don Gagne committed
84

dogmaphobic's avatar
dogmaphobic committed
85
    property real _pipSize:             mainWindow.width * 0.2
86

87
    FlightDisplayViewController { id: _controller }
88

dogmaphobic's avatar
dogmaphobic committed
89 90 91 92 93
    onInteractiveChanged: {
        if(_flightMap)
            _flightMap.interactive = interactive
    }

94 95 96 97 98 99 100 101 102 103 104
    function reloadContents() {
        if(_flightVideo) {
            _flightVideo.visible = false
        }
        if(_mainIsMap) {
            mainLoader.source   = "FlightDisplayViewMap.qml"
            pipLoader.source    = "FlightDisplayViewVideo.qml"
        } else {
            mainLoader.source   = "FlightDisplayViewVideo.qml"
            pipLoader.source    = "FlightDisplayViewMap.qml"
        }
Don Gagne's avatar
Don Gagne committed
105 106
    }

107
    Component.onCompleted: {
108 109
        reloadContents();
        widgetsLoader.source    = "FlightDisplayViewWidgets.qml"
110 111
    }

112 113 114 115 116 117 118 119 120 121 122 123 124 125
    //-- Main Window
    Loader {
        id:                 mainLoader
        anchors.fill:       parent
        onLoaded: {
            if(_mainIsMap) {
                _flightMap   = item
                if(_savedZoomLevel != 0)
                    _flightMap.zoomLevel = _savedZoomLevel
                else
                    _savedZoomLevel = _flightMap.zoomLevel
                _flightMap.updateMapPosition(true /* force */)
            } else {
                _flightVideo = item
Don Gagne's avatar
Don Gagne committed
126 127
            }
        }
128
    }
dogmaphobic's avatar
dogmaphobic committed
129

130
    //-- PIP Window
dogmaphobic's avatar
dogmaphobic committed
131
    Item {
132
        id:                 pip
dogmaphobic's avatar
dogmaphobic committed
133
        visible:            _controller.hasVideo && _isPipVisible
134 135 136
        anchors.margins:    ScreenTools.defaultFontPixelHeight
        anchors.left:       parent.left
        anchors.bottom:     parent.bottom
dogmaphobic's avatar
dogmaphobic committed
137 138
        width:              _pipSize
        height:             _pipSize * (9/16)
139 140 141 142 143 144 145 146 147 148
        Loader {
            id:                 pipLoader
            anchors.fill:       parent
            onLoaded: {
                if(_mainIsMap) {
                    _flightVideo = item
                } else {
                    _flightMap = item
                    _savedZoomLevel = _flightMap.zoomLevel
                    _flightMap.zoomLevel = _savedZoomLevel - 3
149
                }
150
            }
151
        }
152 153
        MouseArea {
            anchors.fill: parent
154 155 156
            onClicked: {
                _mainIsMap = !_mainIsMap
                reloadContents();
157
                QGroundControl.saveBoolGlobalSetting(_mainIsMapKey, _mainIsMap)
158
            }
159
        }
dogmaphobic's avatar
dogmaphobic committed
160 161
        Image {
            id:             closePIP
dogmaphobic's avatar
dogmaphobic committed
162
            source:         "/qmlimages/PiP.svg"
dogmaphobic's avatar
dogmaphobic committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
            mipmap:         true
            fillMode:       Image.PreserveAspectFit
            anchors.left:   parent.left
            anchors.bottom: parent.bottom
            height:         ScreenTools.defaultFontPixelSize * 2.5
            width:          ScreenTools.defaultFontPixelSize * 2.5
            MouseArea {
                anchors.fill: parent
                onClicked: {
                    _isPipVisible = false
                    QGroundControl.saveBoolGlobalSetting(_PIPVisibleKey, false)
                }
            }
        }
    }

    //-- Show PIP
    Rectangle {
        id:                     openPIP
        anchors.left :          parent.left
        anchors.bottom:         parent.bottom
        anchors.margins:        ScreenTools.defaultFontPixelHeight
        height:                 ScreenTools.defaultFontPixelSize * 2
        width:                  ScreenTools.defaultFontPixelSize * 2
        radius:                 ScreenTools.defaultFontPixelSize / 3
        visible:                _controller.hasVideo && !_isPipVisible
dogmaphobic's avatar
dogmaphobic committed
189
        color:                  isBackgroundDark ? Qt.rgba(0,0,0,0.75) : Qt.rgba(0,0,0,0.5)
dogmaphobic's avatar
dogmaphobic committed
190 191 192
        Image {
            width:              parent.width  * 0.75
            height:             parent.height * 0.75
dogmaphobic's avatar
dogmaphobic committed
193
            source:             "/res/buttonRight.svg"
dogmaphobic's avatar
dogmaphobic committed
194 195 196 197 198 199 200 201 202 203 204 205
            mipmap:             true
            fillMode:           Image.PreserveAspectFit
            anchors.verticalCenter:     parent.verticalCenter
            anchors.horizontalCenter:   parent.horizontalCenter
        }
        MouseArea {
            anchors.fill: parent
            onClicked: {
                _isPipVisible = true
                QGroundControl.saveBoolGlobalSetting(_PIPVisibleKey, true)
            }
        }
206
    }
Don Gagne's avatar
Don Gagne committed
207

208
    //-- Widgets
Don Gagne's avatar
Don Gagne committed
209
    Loader {
210
        id:                 widgetsLoader
dogmaphobic's avatar
dogmaphobic committed
211 212 213
        anchors.right:      parent.right
        anchors.left:       parent.left
        anchors.bottom:     parent.bottom
Don Gagne's avatar
Don Gagne committed
214
        height:             availableHeight
215 216

        property bool isBackgroundDark: root.isBackgroundDark
Don Gagne's avatar
Don Gagne committed
217
    }
dogmaphobic's avatar
dogmaphobic committed
218

219
    //-- Virtual Joystick
Don Gagne's avatar
Don Gagne committed
220 221
    Item {
        id:             multiTouchItem
dogmaphobic's avatar
dogmaphobic committed
222
        width:          parent.width  - (pip.width / 2)
Don Gagne's avatar
Don Gagne committed
223 224
        height:         thumbAreaHeight
        visible:        QGroundControl.virtualTabletJoystick
dogmaphobic's avatar
dogmaphobic committed
225 226 227
        anchors.bottom: pip.top
        anchors.bottomMargin: ScreenTools.defaultFontPixelHeight * 2
        anchors.horizontalCenter: parent.horizontalCenter
Don Gagne's avatar
Don Gagne committed
228

229
        readonly property real thumbAreaHeight: Math.min(parent.height * 0.25, ScreenTools.defaultFontPixelWidth * 16)
Don Gagne's avatar
Don Gagne committed
230 231 232 233 234

        QGCMapPalette { id: mapPal; lightColors: !isBackgroundDark }

        MultiPointTouchArea {
            anchors.fill:       parent
Don Gagne's avatar
Don Gagne committed
235 236 237 238
            touchPoints: [
                TouchPoint { id: point1 },
                TouchPoint { id: point2 }
            ]
Don Gagne's avatar
Don Gagne committed
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

            property var leftRect:  Qt.rect(0, 0, parent.thumbAreaHeight, parent.thumbAreaHeight)
            property var rightRect: Qt.rect(parent.width - parent.thumbAreaHeight, 0, parent.thumbAreaHeight, parent.thumbAreaHeight)

            function pointInRect(rect, point) {
                return point.x >= rect.x &&
                        point.y >= rect.y &&
                        point.x <= rect.x + rect.width &&
                        point.y <= rect.y + rect.height
            }

            function newTouchPoints(touchPoints)
            {
                var point1Location = 0
                var point2Location = 0

                var point1
                if (touchPoints.length > 0) {
                    point1 = touchPoints[0]
                    if (pointInRect(leftRect, point1)) {
                        point1Location = -1
                    } else if (pointInRect(rightRect, point1)) {
                        point1Location = 1
                    }
                }

                var point2
                if (touchPoints.length == 2) {
                    point2 = touchPoints[1]
                    if (pointInRect(leftRect, point2)) {
                        point2Location = -1
                    } else if (pointInRect(rightRect, point2)) {
                        point2Location = 1
                    }
                }

Don Gagne's avatar
Don Gagne committed
275 276 277
                var leftStickSet = false
                var rightStickSet = false

Don Gagne's avatar
Don Gagne committed
278 279 280 281 282
                // Make sure points are not both in the same rect
                if (point1Location != point2Location) {
                    if (point1Location != 0) {
                        if (point1Location == -1) {
                            leftStick.stickPosition = point1
Don Gagne's avatar
Don Gagne committed
283
                            leftStickSet = true
Don Gagne's avatar
Don Gagne committed
284 285
                        } else {
                            rightStick.stickPosition = Qt.point(point1.x - (multiTouchItem.width - multiTouchItem.thumbAreaHeight), point1.y)
Don Gagne's avatar
Don Gagne committed
286
                            rightStickSet = true
Don Gagne's avatar
Don Gagne committed
287 288 289 290 291
                        }
                    }
                    if (point2Location != 0) {
                        if (point2Location == -1) {
                            leftStick.stickPosition = point2
Don Gagne's avatar
Don Gagne committed
292 293 294 295
                            leftStickSet = true
                        } else {
                            rightStick.stickPosition = Qt.point(point2.x - (multiTouchItem.width - multiTouchItem.thumbAreaHeight), point2.y)
                            rightStickSet = true
Don Gagne's avatar
Don Gagne committed
296 297 298
                        }
                    }
                }
Don Gagne's avatar
Don Gagne committed
299 300 301 302 303 304
                if (!leftStickSet) {
                    leftStick.reCenter()
                }
                if (!rightStickSet) {
                    rightStick.reCenter()
                }
Don Gagne's avatar
Don Gagne committed
305 306 307 308 309 310
            }

            onTouchUpdated: newTouchPoints(touchPoints)
        }

        Timer {
Don Gagne's avatar
Don Gagne committed
311 312
            interval:   40  // 25Hz, same as real joystick rate
            running:    QGroundControl.virtualTabletJoystick && _activeVehicle
Don Gagne's avatar
Don Gagne committed
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
            repeat:     true
            onTriggered: {
                if (_activeVehicle) {
                    _activeVehicle.virtualTabletJoystickValue(rightStick.xAxis, rightStick.yAxis, leftStick.xAxis, leftStick.yAxis)
                }
            }
        }

        JoystickThumbPad {
            id:             leftStick
            anchors.left:   parent.left
            anchors.bottom: parent.bottom
            width:          parent.thumbAreaHeight
            height:         parent.thumbAreaHeight
            yAxisThrottle:  true
            lightColors:    !isBackgroundDark
        }

        JoystickThumbPad {
            id:             rightStick
            anchors.right:  parent.right
            anchors.bottom: parent.bottom
            width:          parent.thumbAreaHeight
            height:         parent.thumbAreaHeight
            lightColors:    !isBackgroundDark
        }
    }
340
}