1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24**   * Redistributions of source code must retain the above copyright
25**     notice, this list of conditions and the following disclaimer.
26**   * Redistributions in binary form must reproduce the above copyright
27**     notice, this list of conditions and the following disclaimer in
28**     the documentation and/or other materials provided with the
29**     distribution.
30**   * Neither the name of The Qt Company Ltd nor the names of its
31**     contributors may be used to endorse or promote products derived
32**     from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51import QtQuick 2.4
52import QtQuick.Window 2.2
53import QtPositioning 5.5
54import QtLocation 5.6
55
56Window {
57    width: 700
58    height: 500
59    visible: true
60
61    property variant topLeftEurope: QtPositioning.coordinate(60.5, 0.0)
62    property variant bottomRightEurope: QtPositioning.coordinate(51.0, 14.0)
63    property variant viewOfEurope:
64            QtPositioning.rectangle(topLeftEurope, bottomRightEurope)
65
66    property variant berlin: QtPositioning.coordinate(52.5175, 13.384)
67    property variant oslo: QtPositioning.coordinate(59.9154, 10.7425)
68    property variant london: QtPositioning.coordinate(51.5, 0.1275)
69
70    Map {
71        id: mapOfEurope
72        anchors.centerIn: parent;
73        anchors.fill: parent
74        plugin: Plugin {
75            name: "osm" // "mapboxgl", "esri", ...
76        }
77
78        Plane {
79            id: qmlPlane
80            pilotName: "QML"
81            coordinate: oslo2Berlin.position
82
83            SequentialAnimation {
84                id: qmlPlaneAnimation
85                property real rotationDirection : 0;
86                NumberAnimation {
87                    target: qmlPlane; property: "bearing"; duration: 1000
88                    easing.type: Easing.InOutQuad
89                    to: qmlPlaneAnimation.rotationDirection
90                }
91                //! [QmlPlane1]
92                CoordinateAnimation {
93                    id: coordinateAnimation; duration: 5000
94                    target: oslo2Berlin; property: "position"
95                    easing.type: Easing.InOutQuad
96                }
97                //! [QmlPlane1]
98
99                onStopped: {
100                    if (coordinateAnimation.to === berlin)
101                        qmlPlane.showMessage(qsTr("Hello Berlin!"))
102                    else if (coordinateAnimation.to === oslo)
103                        qmlPlane.showMessage(qsTr("Hello Oslo!"))
104                }
105                onStarted: {
106                    if (coordinateAnimation.from === oslo)
107                        qmlPlane.showMessage(qsTr("See you Oslo!"))
108                    else if (coordinateAnimation.from === berlin)
109                        qmlPlane.showMessage(qsTr("See you Berlin!"))
110                }
111            }
112
113            //! [QmlPlane2]
114            MouseArea {
115                anchors.fill: parent
116                onClicked: {
117                    if (qmlPlaneAnimation.running) {
118                        console.log("Plane still in the air.");
119                        return;
120                    }
121
122                    if (oslo2Berlin.position === berlin) {
123                        coordinateAnimation.from = berlin;
124                        coordinateAnimation.to = oslo;
125                    } else if (oslo2Berlin.position === oslo) {
126                        coordinateAnimation.from = oslo;
127                        coordinateAnimation.to = berlin;
128                    }
129
130                    qmlPlaneAnimation.rotationDirection = oslo2Berlin.position.azimuthTo(coordinateAnimation.to)
131                    qmlPlaneAnimation.start()
132                }
133            }
134            //! [QmlPlane2]
135            Component.onCompleted: {
136                oslo2Berlin.position = oslo;
137            }
138        }
139
140        //! [CppPlane1]
141        Plane {
142            id: cppPlane
143            pilotName: "C++"
144            coordinate: berlin2London.position
145
146            MouseArea {
147                anchors.fill: parent
148                onClicked: {
149                    if (cppPlaneAnimation.running || berlin2London.isFlying()) {
150                        console.log("Plane still in the air.");
151                        return;
152                    }
153
154                    berlin2London.swapDestinations();
155                    cppPlaneAnimation.rotationDirection = berlin2London.position.azimuthTo(berlin2London.to)
156                    cppPlaneAnimation.start();
157                    cppPlane.departed();
158                }
159            }
160        //! [CppPlane1]
161            //! [CppPlane3]
162            SequentialAnimation {
163                id: cppPlaneAnimation
164                property real rotationDirection : 0;
165                NumberAnimation {
166                    target: cppPlane; property: "bearing"; duration: 1000
167                    easing.type: Easing.InOutQuad
168                    to: cppPlaneAnimation.rotationDirection
169                }
170                ScriptAction { script: berlin2London.startFlight() }
171            }
172            //! [CppPlane3]
173
174            Component.onCompleted: {
175                berlin2London.position = berlin;
176                berlin2London.to = london;
177                berlin2London.from = berlin;
178                berlin2London.arrived.connect(arrived)
179            }
180
181            function arrived(){
182                if (berlin2London.to === berlin)
183                    cppPlane.showMessage(qsTr("Hello Berlin!"))
184                else if (berlin2London.to === london)
185                    cppPlane.showMessage(qsTr("Hello London!"))
186            }
187
188            function departed(){
189                if (berlin2London.from === berlin)
190                    cppPlane.showMessage(qsTr("See you Berlin!"))
191                else if (berlin2London.from === london)
192                    cppPlane.showMessage(qsTr("See you London!"))
193            }
194        //! [CppPlane2]
195        }
196        //! [CppPlane2]
197
198        visibleRegion: viewOfEurope
199    }
200
201    Rectangle {
202        id: infoBox
203        anchors.centerIn: parent
204        color: "white"
205        border.width: 1
206        width: text.width * 1.3
207        height: text.height * 1.3
208        radius: 5
209        Text {
210            id: text
211            anchors.centerIn: parent
212            text: qsTr("Hit the plane to start the flight!")
213        }
214
215        Timer {
216            interval: 5000; running: true; repeat: false;
217            onTriggered: fadeOut.start()
218        }
219
220        NumberAnimation {
221            id: fadeOut; target: infoBox;
222            property: "opacity";
223            to: 0.0;
224            duration: 200
225            easing.type: Easing.InOutQuad
226        }
227    }
228}
229