Debian Codegraph @

tests/manual/planets-qml/PlanetsMain.qml

raw
1 // Copyright (C) 2016 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 import QtQuick 2.0
5 import QtQuick.Window 2.2
6 import QtQuick.Scene3D 2.0
7 import "planets.js" as Planets
8
9 Item {
10 id: mainview
11 width: 1280
12 height: 768
13 visible: true
14 property int focusedPlanet: 100
15 property int oldPlanet: 0
16 property int frames: 0
17 property int sliderLength: (width < height) ? width / 2 : height / 2
18 property real textSize: sliderLength / 20
19 property real planetButtonSize: (height < 2200) ? (height / 13) : 200
20
21 Connections {
22 target: networkController
23
24 function onCommandAccepted() {
25 var focusedItem = mainview.Window.window.activeFocusItem
26 if (focusedItem && focusedItem.panningEnabled) {
27 focusedItem.panningEnabled = false
28 }
29
30 switch (command) {
31 case "selectPlanet":
32 mainview.focusedPlanet = Planets.planetId(decodeURIComponent(value))
33 planetButtonView.forceActiveFocus()
34 planetButtonView.currentIndex = Planets.planetIndex(value)
35 break
36 case "setRotationSpeed":
37 rotationSpeedSlider.forceActiveFocus()
38 rotationSpeedSlider.value = rotationSpeedSlider.from +
39 ((rotationSpeedSlider.to - rotationSpeedSlider.from) * value)
40 break
41 case "setViewingDistance":
42 viewingDistanceSlider.forceActiveFocus()
43 viewingDistanceSlider.value = viewingDistanceSlider.from +
44 ((viewingDistanceSlider.to - viewingDistanceSlider.from) * value)
45 break
46 case "setPlanetSize":
47 planetSizeSlider.forceActiveFocus()
48 planetSizeSlider.value = planetSizeSlider.from +
49 ((planetSizeSlider.to - planetSizeSlider.from) * value)
50 break
51 }
52 }
53 }
54
55 //! [0]
56 Scene3D {
57 anchors.fill: parent
58 aspects: ["render", "logic", "input"]
59
60 SolarSystem { id: solarsystem }
61 }
62 //! [0]
63
64 MouseArea {
65 anchors.fill: parent
66 acceptedButtons: Qt.LeftButton
67 onClicked:
68 focusedPlanet = 100
69 }
70
71 //! [1]
72 onFocusedPlanetChanged: {
73 if (focusedPlanet == 100) {
74 info.opacity = 0
75 updatePlanetInfo()
76 } else {
77 updatePlanetInfo()
78 info.opacity = 1
79 }
80
81 solarsystem.changePlanetFocus(oldPlanet, focusedPlanet)
82 oldPlanet = focusedPlanet
83 }
84 //! [1]
85
86 ListModel {
87 id: planetModel
88
89 ListElement {
90 name: "Sun"
91 radius: "109 x Earth"
92 temperature: "5 778 K"
93 orbitalPeriod: ""
94 distance: ""
95 planetImageSource: "qrc:/images/sun.png"
96 planetNumber: 0
97 }
98 ListElement {
99 name: "Mercury"
100 radius: "0.3829 x Earth"
101 temperature: "80-700 K"
102 orbitalPeriod: "87.969 d"
103 distance: "0.387 098 AU"
104 planetImageSource: "qrc:/images/mercury.png"
105 planetNumber: 1
106 }
107 ListElement {
108 name: "Venus"
109 radius: "0.9499 x Earth"
110 temperature: "737 K"
111 orbitalPeriod: "224.701 d"
112 distance: "0.723 327 AU"
113 planetImageSource: "qrc:/images/venus.png"
114 planetNumber: 2
115 }
116 ListElement {
117 name: "Earth"
118 radius: "6 378.1 km"
119 temperature: "184-330 K"
120 orbitalPeriod: "365.256 d"
121 distance: "149598261 km (1 AU)"
122 planetImageSource: "qrc:/images/earth.png"
123 planetNumber: 3
124 }
125 ListElement {
126 name: "Mars"
127 radius: "0.533 x Earth"
128 temperature: "130-308 K"
129 orbitalPeriod: "686.971 d"
130 distance: "1.523679 AU"
131 planetImageSource: "qrc:/images/mars.png"
132 planetNumber: 4
133 }
134 ListElement {
135 name: "Jupiter"
136 radius: "11.209 x Earth"
137 temperature: "112-165 K"
138 orbitalPeriod: "4332.59 d"
139 distance: "5.204267 AU"
140 planetImageSource: "qrc:/images/jupiter.png"
141 planetNumber: 5
142 }
143 ListElement {
144 name: "Saturn"
145 radius: "9.4492 x Earth"
146 temperature: "84-134 K"
147 orbitalPeriod: "10759.22 d"
148 distance: "9.5820172 AU"
149 planetImageSource: "qrc:/images/saturn.png"
150 planetNumber: 6
151 }
152 ListElement {
153 name: "Uranus"
154 radius: "4.007 x Earth"
155 temperature: "49-76 K"
156 orbitalPeriod: "30687.15 d"
157 distance: "19.189253 AU"
158 planetImageSource: "qrc:/images/uranus.png"
159 planetNumber: 7
160 }
161 ListElement {
162 name: "Neptune"
163 radius: "3.883 x Earth"
164 temperature: "55-72 K"
165 orbitalPeriod: "60190.03 d"
166 distance: "30.070900 AU"
167 planetImageSource: "qrc:/images/neptune.png"
168 planetNumber: 8
169 }
170 ListElement {
171 name: "Solar System"
172 planetImageSource: ""
173 planetNumber: 100 // Defaults to solar system
174 }
175 }
176
177 Component {
178 id: planetButtonDelegate
179 PlanetButton {
180 source: model.planetImageSource
181 text: model.name
182 focusPlanet: model.planetNumber
183 planetSelector: mainview
184 buttonSize: planetButtonSize
185 fontSize: textSize
186
187 scale: activeFocus ? 1.4 : 1.0
188
189 Behavior on scale {
190 PropertyAnimation {
191 duration: 200
192 }
193 }
194
195 signal swipeUp()
196 signal swipeDown()
197 signal swipeLeft()
198
199 onSwipeUp: {
200 if (planetButtonView.currentIndex > 0) {
201 planetButtonView.currentIndex--
202 } else {
203 rotationSpeedSlider.forceActiveFocus()
204 }
205 }
206
207 onSwipeDown: {
208 if (planetButtonView.currentIndex < planetButtonView.count - 1) {
209 planetButtonView.currentIndex++
210 } else {
211 planetSizeSlider.forceActiveFocus()
212 }
213 }
214
215 onSwipeLeft: {
216 if (index <= planetButtonView.count / 2) {
217 rotationSpeedSlider.forceActiveFocus()
218 } else {
219 planetSizeSlider.forceActiveFocus()
220 }
221 }
222
223 Keys.onPressed: {
224 if (event.key === Qt.Key_Select) {
225 planetSelector.focusedPlanet = focusPlanet
226 }
227 }
228 }
229 }
230
231 ListView {
232 id: planetButtonView
233 anchors.verticalCenter: parent.verticalCenter
234 anchors.right: parent.right
235 anchors.rightMargin: planetButtonSize / 2
236 height: childrenRect.height
237 spacing: planetButtonSize / 6
238 width: planetButtonSize * 1.4
239 interactive: false
240 model: planetModel
241 delegate: planetButtonDelegate
242 }
243
244 InfoSheet {
245 id: info
246 width: 400
247 anchors.right: planetButtonView.left
248 anchors.rightMargin: 40
249 opacity: 1
250
251 // Set initial information for Solar System
252 planet: "Solar System"
253 exampleDetails: "This example shows a 3D model of the Solar</p>" +
254 "<p>System comprised of the Sun and the eight</p>" +
255 "<p>planets orbiting the Sun.</p></br>" +
256 "<p>The example is implemented using Qt3D.</p>" +
257 "<p>The textures and images used in the example</p>" +
258 "<p>are Copyright (c) by James Hastings-Trew,</p>" +
259 "<a href=\"http://planetpixelemporium.com/planets.html\">" +
260 "http://planetpixelemporium.com/planets.html</a>"
261 }
262
263 function updatePlanetInfo() {
264 info.width = 200
265
266 if (focusedPlanet !== 100) {
267 info.planet = planetModel.get(focusedPlanet).name
268 info.radius = planetModel.get(focusedPlanet).radius
269 info.temperature = planetModel.get(focusedPlanet).temperature
270 info.orbitalPeriod = planetModel.get(focusedPlanet).orbitalPeriod
271 info.distance = planetModel.get(focusedPlanet).distance
272 }
273 }
274
275 Row {
276 anchors.top: parent.top
277 anchors.topMargin: 10
278 anchors.horizontalCenter: parent.horizontalCenter
279
280 spacing: 10
281 scale: rotationSpeedSlider.activeFocus ? 1.4 : 1.0
282 opacity: rotationSpeedSlider.activeFocus ? 1.0 : 0.5
283
284 Behavior on scale {
285 PropertyAnimation {
286 duration: 200
287 }
288 }
289
290 Text {
291 anchors.verticalCenter: parent.verticalCenter
292
293 font.family: "Helvetica"
294 font.pixelSize: textSize
295 font.weight: Font.Light
296 color: rotationSpeedSlider.panningEnabled ? "#80c342" : "#ffffff"
297 text: "Rotation Speed"
298 }
299
300 StyledSlider {
301 id: rotationSpeedSlider
302 anchors.verticalCenter: parent.verticalCenter
303 width: sliderLength
304 value: 0.2
305 from: 0
306 to: 1
307 onValueChanged: solarsystem.changeSpeed(value)
308
309 focus: Qt.platform.os === "tvos" ? true : false
310
311 property bool panningEnabled: false
312 signal swipeDown()
313 signal swipeLeft()
314 signal swipeRight()
315 signal pannedHorizontally(real p)
316 signal pannedVertically(real p)
317
318 onSwipeDown: {
319 planetSizeSlider.forceActiveFocus()
320 }
321
322 onSwipeLeft: {
323 viewingDistanceSlider.forceActiveFocus()
324 }
325
326 onSwipeRight: {
327 planetButtonView.currentIndex = 0
328 planetButtonView.forceActiveFocus()
329 }
330
331 onPannedHorizontally: {
332 var step = (to - from) / 30
333
334 if (p > 0) {
335 value += step
336 } else {
337 value -= step
338 }
339 }
340
341 Keys.onPressed: {
342 if (event.key === Qt.Key_Select) {
343 panningEnabled = !panningEnabled
344 }
345 }
346 }
347 }
348
349 Column {
350 anchors.left: parent.left
351 anchors.leftMargin: 30
352 anchors.verticalCenter: parent.verticalCenter
353
354 spacing: 10
355 scale: viewingDistanceSlider.activeFocus ? 1.4 : 1.0
356 opacity: viewingDistanceSlider.activeFocus ? 1.0 : 0.5
357
358 Behavior on scale {
359 PropertyAnimation {
360 duration: 200
361 }
362 }
363
364 StyledSlider {
365 id: viewingDistanceSlider
366
367 anchors.horizontalCenter: parent.horizontalCenter
368 orientation: Qt.Vertical
369 height: sliderLength
370 value: 1
371 from: 1
372 to: 2
373 //! [2]
374 onValueChanged: solarsystem.changeCameraDistance(value)
375 //! [2]
376
377 property bool panningEnabled: false
378 signal swipeUp()
379 signal swipeDown()
380 signal swipeRight()
381 signal pannedHorizontally(real p)
382 signal pannedVertically(real p)
383
384 onSwipeUp: {
385 rotationSpeedSlider.forceActiveFocus()
386 }
387
388 onSwipeDown: {
389 planetSizeSlider.forceActiveFocus()
390 }
391
392 onSwipeRight: {
393 planetButtonView.currentIndex = 0
394 planetButtonView.forceActiveFocus()
395 }
396
397 onPannedVertically: {
398 var step = (to - from) / 30
399
400 if (p > 0) {
401 value += step
402 } else {
403 value -= step
404 }
405 }
406
407 Keys.onPressed: {
408 if (event.key === Qt.Key_Select) {
409 panningEnabled = !panningEnabled
410 }
411 }
412 }
413
414 Text {
415 anchors.horizontalCenter: parent.horizontalCenter
416
417 font.family: "Helvetica"
418 font.pixelSize: textSize
419 font.weight: Font.Light
420 color: viewingDistanceSlider.panningEnabled ? "#80c342" : "#ffffff"
421 text: "Viewing\nDistance"
422 }
423 }
424
425 Row {
426 anchors.bottom: parent.bottom
427 anchors.bottomMargin: 10
428 anchors.horizontalCenter: parent.horizontalCenter
429
430 spacing: 10
431 scale: planetSizeSlider.activeFocus ? 1.4 : 1.0
432 opacity: planetSizeSlider.activeFocus ? 1.0 : 0.5
433
434 Behavior on scale {
435 PropertyAnimation {
436 duration: 200
437 }
438 }
439
440 Text {
441 anchors.verticalCenter: parent.verticalCenter
442 font.family: "Helvetica"
443 font.pixelSize: textSize
444 font.weight: Font.Light
445 color: planetSizeSlider.panningEnabled ? "#80c342" : "#ffffff"
446 text: "Planet Size"
447 }
448
449 StyledSlider {
450 id: planetSizeSlider
451 anchors.verticalCenter: parent.verticalCenter
452 width: sliderLength
453 value: 1200
454 from: 1
455 to: 2000
456 onValueChanged: solarsystem.changeScale(value, false)
457
458 property bool panningEnabled: false
459 signal swipeUp()
460 signal swipeLeft()
461 signal swipeRight()
462 signal pannedHorizontally(real p)
463 signal pannedVertically(real p)
464
465 onSwipeUp: {
466 rotationSpeedSlider.forceActiveFocus()
467 }
468
469 onSwipeLeft: {
470 viewingDistanceSlider.forceActiveFocus()
471 }
472
473 onSwipeRight: {
474 planetButtonView.currentIndex = planetButtonView.count - 1
475 planetButtonView.forceActiveFocus()
476 }
477
478 onPannedHorizontally: {
479 var step = (to - from) / 30
480
481 if (p > 0) {
482 value += step
483 } else {
484 value -= step
485 }
486 }
487
488 Keys.onPressed: {
489 if (event.key === Qt.Key_Select) {
490 panningEnabled = !panningEnabled
491 }
492 }
493 }
494 }
495
496 // FPS display, initially hidden, clicking will show it
497 FpsDisplay {
498 id: fpsDisplay
499 anchors.left: parent.left
500 anchors.bottom: parent.bottom
501 width: 32
502 height: 64
503 hidden: true
504 }
505
506 Timer {
507 interval: 1000
508 repeat: true
509 running: !fpsDisplay.hidden
510 onTriggered: {
511 fpsDisplay.fps = frames
512 frames = 0
513 }
514 onRunningChanged: frames = 0
515 }
516
517 Loader {
518 anchors.fill: parent
519
520 source: Qt.platform.os === "tvos" ? "AppleTVInput.qml" : ""
521 }
522 }