summaryrefslogtreecommitdiffstats
path: root/examples/positioning/satelliteinfo/SkyView.qml
blob: 0ce751ca220b9cdada39dc812ad15b0918679652 (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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
189
190
191
192
193
194
195
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
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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

pragma ComponentBehavior: Bound
import QtQuick
import QtQuick.Shapes
import SatelliteInformation

Rectangle {
    id: root

    // Multisample this item to get rid of aliasing in our custom shapes.
    layer.enabled: true
    layer.samples: 4
    // Sample text to calculate sky view size properly
    Text {
        id: sampleText
        visible: false
        text: "270°"
        font.pixelSize: Theme.smallFontSize
        font.weight: Theme.fontLightWeight
    }

    required property SatelliteModel satellitesModel
    required property color inViewColor
    required property color inUseColor


    readonly property real fullRadius: width < height ? width / 2 - sampleText.width * 1.1
                                                      : height / 2 - sampleText.height * 1.1

    readonly property real maxUsedRadius: 0.9 * fullRadius
    readonly property real radiusStep: maxUsedRadius / 9
    readonly property int centerX: width / 2
    readonly property int centerY: height / 2

    color: Theme.darkBackgroundColor

    LegendBox {
        id: legend
        anchors {
            top: root.top
            right: root.right
            margins: Theme.defaultSpacing
        }
    }

    // Shape for the SkyView
    Shape {
        id: rootShape
        readonly property color mainColor: Theme.textMainColor
        readonly property color dashedColor: Qt.rgba(mainColor.r,
                                                     mainColor.g,
                                                     mainColor.b,
                                                     0.5)

        // Circles
        component ElevationCircle: ShapePath {
            id: circle

            property color color: "white"
            property real radius: 0
            property bool dashed: false

            strokeColor: circle.color
            fillColor: "transparent"
            strokeStyle: circle.dashed ? ShapePath.DashLine : ShapePath.SolidLine
            dashPattern: [4, 4]
            PathAngleArc {
                centerX: root.centerX
                centerY: root.centerY
                radiusX: circle.radius
                radiusY: circle.radius
                startAngle: 0
                sweepAngle: 360
            }
        }

        ElevationCircle {
            radius: root.fullRadius
            color: Theme.greenColor
        }
        ElevationCircle {
            radius: root.radiusStep * 9
            color: rootShape.dashedColor
            dashed: true
        }
        ElevationCircle {
            radius: root.radiusStep * 7
            color: rootShape.dashedColor
            dashed: true
        }
        ElevationCircle {
            radius: root.radiusStep * 5
            color: rootShape.dashedColor
            dashed: true
        }
        ElevationCircle {
            radius: root.radiusStep * 3
            color: rootShape.dashedColor
            dashed: true
        }
        ElevationCircle {
            radius: root.radiusStep
            color: rootShape.mainColor
        }

        // Lines
        component Line: ShapePath {
            id: line
            readonly property color green: Theme.greenColor
            strokeColor: Qt.rgba(green.r, green.g, green.b, 0.5)
            property real angle
            property real radianAngle: line.angle / 180 * Math.PI
            startX: root.centerX + root.fullRadius * Math.cos(radianAngle)
            startY: root.centerY + root.fullRadius * Math.sin(radianAngle)
            PathLine {
                x: root.centerX + root.fullRadius * Math.cos(line.radianAngle + Math.PI)
                y: root.centerY + root.fullRadius * Math.sin(line.radianAngle + Math.PI)
            }
        }

        Line {
            angle: 0
        }
        Line {
            angle: 30
        }
        Line {
            angle: 60
        }
        Line {
            angle: 90
        }
        Line {
            angle: 120
        }
        Line {
            angle: 150
        }

        // Azimuth captions
        component AzimuthCaption: Text {
            id: textElement
            property real angle
            property real offsetX: 0
            property real offsetY: 0
            // Subtract 90, because 0 should be on top, not on the right
            property real radianAngle: (angle - 90) / 180 * Math.PI
            // end of line coordinates
            property int lineX: root.centerX + root.fullRadius * Math.cos(radianAngle)
            property int lineY: root.centerY + root.fullRadius * Math.sin(radianAngle)
            x: lineX + offsetX
            y: lineY + offsetY
            text: angle.toFixed(0) + '°'
            color: Theme.textMainColor
            font.pixelSize: Theme.smallFontSize
            font.weight: Theme.fontLightWeight
        }

        AzimuthCaption {
            text: 'N'
            angle: 0
            offsetY: -height
            offsetX: -width / 2
        }
        AzimuthCaption {
            angle: 30
            offsetY: -height
        }
        AzimuthCaption {
            angle: 60
            offsetX: width * 0.5
            offsetY: -height / 2
        }
        AzimuthCaption {
            text: 'E'
            angle: 90
            offsetX: width * 0.5
            offsetY: -height / 2
        }
        AzimuthCaption {
            angle: 120
        }
        AzimuthCaption {
            angle: 150
        }
        AzimuthCaption {
            text: 'S'
            angle: 180
            offsetX: -width / 2
        }
        AzimuthCaption {
            angle: 210
            offsetX: -width / 2
            offsetY: height / 2
        }
        AzimuthCaption {
            angle: 240
            offsetX: -width
        }
        AzimuthCaption {
            text: 'W'
            angle: 270
            offsetX: -width * 1.5
            offsetY: -height / 2
        }
        AzimuthCaption {
            angle: 300
            offsetX: -width
            offsetY: -height
        }
        AzimuthCaption {
            angle: 330
            offsetX: -width
            offsetY: -height
        }

        // Elevation captions
        component ElevationCaption: Text {
            property real step
            x: root.centerX - implicitWidth / 2
            y: root.centerY - (step * root.radiusStep + implicitHeight)
            color: Theme.textMainColor
            font.pixelSize: Theme.smallFontSize
            font.weight: Theme.fontLightWeight
        }

        ElevationCaption {
            text: "80°"
            step: 1
        }
        ElevationCaption {
            text: "60°"
            step: 3
        }
        ElevationCaption {
            text: "40°"
            step: 5
        }
        ElevationCaption {
            text: "20°"
            step: 7
        }
        ElevationCaption {
            text: "0°"
            step: 9
        }
    }

    // Actual satellite positions
    Item {
        id: satelliteItemView
        anchors.fill: parent

        property string selectedName: ""
        property var selectedData: ({"name": "", "azimuth": 0, "elevation": 0})
        // selected{X,Y} holds the center of the selected circle
        property int selectedX: -1
        property int selectedY: -1

        Repeater {
            model: root.satellitesModel
            delegate: Rectangle {
                id: satItem

                required property var modelData
                property bool selected: satelliteItemView.selectedName === modelData.name

                readonly property color normalColor: modelData.inUse ? root.inUseColor
                                                                     : root.inViewColor
                readonly property color selectedColor: Qt.rgba(normalColor.r,
                                                               normalColor.g,
                                                               normalColor.b,
                                                               0.5)
                readonly property color centerColor: selected ? Theme.whiteColor
                                                              : Theme.blackColor
                readonly property int normalWidth: 10
                readonly property int centerWidth: 6
                readonly property int outerWidth: selected ? normalWidth + 10 : normalWidth

                width: outerWidth
                height: width
                radius: width / 2
                color: selectedColor
                visible: modelData.azimuth > -1 && modelData.elevation > -1
                property real angle: (modelData.azimuth - 90) / 180 * Math.PI
                property real distance: root.maxUsedRadius * (90 - modelData.elevation) / 90
                x: root.centerX + distance * Math.cos(angle) - width / 2
                y: root.centerY + distance * Math.sin(angle) - height / 2
                Rectangle {
                    anchors.centerIn: parent
                    width: satItem.normalWidth
                    height: width
                    radius: width / 2
                    color: satItem.normalColor

                    Rectangle {
                        anchors.centerIn: parent
                        width: satItem.centerWidth
                        height: width
                        radius: width / 2
                        color: satItem.centerColor
                    }
                }
                MouseArea {
                    anchors {
                        fill: parent
                        margins: -20 // so that it's easier to select the item
                    }
                    onClicked: {
                        satelliteItemView.selectedName = satItem.modelData.name
                        satelliteItemView.selectedData = satItem.modelData
                        satelliteItemView.selectedX = satItem.x + satItem.width / 2
                        satelliteItemView.selectedY = satItem.y + satItem.height / 2
                    }
                }

                onXChanged: {
                    if (satItem.selected)
                        satelliteItemView.selectedX = satItem.x + satItem.width / 2
                }
                onYChanged: {
                    if (satItem.selected)
                        satelliteItemView.selectedY = satItem.y + satItem.height / 2
                }

                Component.onCompleted: {
                    if (satItem.selected) {
                        satelliteItemView.selectedData = satItem.modelData
                        satelliteItemView.selectedX = satItem.x + satItem.width / 2
                        satelliteItemView.selectedY = satItem.y + satItem.height / 2
                    }
                }
            }
        }

        Rectangle {
            id: blurRect
            anchors.fill: parent
            color: Theme.backgroundBlurColor
            visible: satelliteItemView.selectedName !== ""
            MouseArea {
                anchors.fill: parent
                onClicked: satelliteItemView.selectedName = ""
            }
        }

        Rectangle {
            id: satInfoRect
            visible: satelliteItemView.selectedName !== ""
            implicitWidth: Math.max(satName.width, satAzimuth.width, satElevation.width)
                           + satInfoCol.anchors.leftMargin + satInfoCol.anchors.rightMargin
            implicitHeight: satName.height + satAzimuth.height + satElevation.height
                            + satInfoCol.anchors.topMargin + satInfoCol.anchors.bottomMargin
                            + 2 * satInfoCol.spacing
            // We need to make sure that the popup fits into the view.
            // This depends on the position of the actual satellite.
            // We try to show the popup top-right from the satellite,
            // but adjust if it's too close to the window border.
            // Keep in mind that (0, 0) is the top left corner.
            readonly property int xOffset: 15
            readonly property int yOffset: 25
            x: satelliteItemView.selectedX + satInfoRect.xOffset + satInfoRect.width
               < satelliteItemView.width
               ? satelliteItemView.selectedX + satInfoRect.xOffset
               : satelliteItemView.selectedX - satInfoRect.xOffset - satInfoRect.width
            y: satelliteItemView.selectedY - satInfoRect.yOffset - satInfoRect.height > 0
               ? satelliteItemView.selectedY - satInfoRect.yOffset - satInfoRect.height
               : satelliteItemView.selectedY + satInfoRect.yOffset
            color: Theme.whiteColor
            border {
                color: Theme.lightGrayColor
                width: 1
            }
            radius: 8
            MouseArea {
                anchors.fill: parent
                onClicked: {} // suppress mouse area of blurRect
            }
            Column {
                id: satInfoCol
                anchors {
                    fill: parent
                    topMargin: Theme.defaultSpacing
                    bottomMargin: Theme.defaultSpacing
                    leftMargin: Theme.defaultSpacing * 2
                    rightMargin: Theme.defaultSpacing * 2
                }
                spacing: 0
                Text {
                    id: satName
                    text: qsTr("Sat ") + satelliteItemView.selectedData.name
                    font.pixelSize: Theme.smallFontSize
                    font.weight: Theme.fontDefaultWeight
                    color: Theme.darkGrayColor
                }
                Text {
                    id: satAzimuth
                    text: qsTr("Azimuth:   ")
                          + satelliteItemView.selectedData.azimuth.toFixed(0) + "°"
                    font.pixelSize: Theme.smallFontSize
                    font.weight: Theme.fontLightWeight
                    color: Theme.grayColor
                }
                Text {
                    id: satElevation
                    text: qsTr("Elevation: ")
                          + satelliteItemView.selectedData.elevation.toFixed(0) + "°"
                    font.pixelSize: Theme.smallFontSize
                    font.weight: Theme.fontLightWeight
                    color: Theme.grayColor
                }
            }
        }

        Shape {
            id: connectionLine
            visible: satelliteItemView.selectedName !== ""
            ShapePath {
                strokeColor: Theme.whiteColor
                strokeWidth: 1
                startX: satelliteItemView.selectedX
                startY: satelliteItemView.selectedY
                PathLine {
                    x: satInfoRect.x > satelliteItemView.selectedX
                       ? satInfoRect.x : satInfoRect.x + satInfoRect.width
                    y: satInfoRect.y > satelliteItemView.selectedY
                       ? satInfoRect.y : satInfoRect.y + satInfoRect.height
                }
            }
        }
    }
}