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.0
52import QtPositioning 5.2
53
54Rectangle {
55    id: container
56    property int maxX: parent.width; property int maxY: parent.height
57//! [props]
58    property variant coordinate
59//! [props]
60
61    Binding {
62        target: container
63        property: "coordinate"
64        value: positionSource.position.coordinate
65    }
66
67    width: 300; height: 130
68    color: "blue"
69    opacity: 0.7
70    border.color: "black"
71    border.width: 1
72    radius: 5
73    gradient: Gradient {
74        GradientStop {position: 0.0; color: "grey"}
75        GradientStop {position: 1.0; color: "black"}
76    }
77    MouseArea {
78        anchors.fill: parent
79        drag.target: parent
80        drag.axis: Drag.XandYAxis
81        drag.minimumX: -(parent.width * (2/3)); drag.maximumX: parent.maxX - (parent.width/3)
82        drag.minimumY: -(parent.height/2); drag.maximumY: parent.maxY - (parent.height/2)
83    }
84//! [locatebutton-top]
85    Button {
86        id: locateButton
87        text: "Locate & update"
88//! [locatebutton-top]
89        anchors {left: parent.left; leftMargin: 5}
90        y: 3; height: 32; width: parent.width - 10
91//! [locatebutton-clicked]
92        onClicked: {
93            if (positionSource.supportedPositioningMethods ===
94                    PositionSource.NoPositioningMethods) {
95                positionSource.nmeaSource = "nmealog.txt";
96                sourceText.text = "(filesource): " + printableMethod(positionSource.supportedPositioningMethods);
97            }
98            positionSource.update();
99        }
100    }
101//! [locatebutton-clicked]
102//! [possrc]
103    PositionSource {
104        id: positionSource
105        onPositionChanged: { planet.source = "images/sun.png"; }
106
107        onSourceErrorChanged: {
108            if (sourceError == PositionSource.NoError)
109                return
110
111            console.log("Source error: " + sourceError)
112            activityText.fadeOut = true
113            stop()
114        }
115
116        onUpdateTimeout: {
117            activityText.fadeOut = true
118        }
119    }
120//! [possrc]
121    function printableMethod(method) {
122        if (method === PositionSource.SatellitePositioningMethods)
123            return "Satellite";
124        else if (method === PositionSource.NoPositioningMethods)
125            return "Not available"
126        else if (method === PositionSource.NonSatellitePositioningMethods)
127            return "Non-satellite"
128        else if (method === PositionSource.AllPositioningMethods)
129            return "Multiple"
130        return "source error";
131    }
132
133    Grid {
134        id: locationGrid
135        columns: 2
136        anchors {left: parent.left; leftMargin: 5; top: locateButton.bottom; topMargin: 5}
137        spacing: 5
138        Text {color: "white"; font.bold: true
139            text: "Lat:"; style: Text.Raised; styleColor: "black"
140        }
141        Text {id: latitudeValue; color: "white"; font.bold: true
142            text: positionSource.position.coordinate.latitude; style: Text.Raised; styleColor: "black";
143        }
144        Text {color: "white"; font.bold: true
145            text: "Lon:"; style: Text.Raised; styleColor: "black"
146        }
147        Text {id: longitudeValue; color: "white"; font.bold: true
148            text: positionSource.position.coordinate.longitude; style: Text.Raised; styleColor: "black"
149        }
150    }
151    Image {
152        id: planet
153        anchors {top: locationGrid.bottom; left: parent.left; leftMargin: locationGrid.anchors.leftMargin}
154        source: "images/moon.png"
155        width: 30; height: 30
156    }
157    Text {id: sourceText; color: "white"; font.bold: true;
158        anchors {left: planet.right; leftMargin: 5; verticalCenter: planet.verticalCenter}
159        text: "Source: " + printableMethod(positionSource.supportedPositioningMethods); style: Text.Raised; styleColor: "black";
160    }
161
162    Text {
163        id: activityText; color: "white"; font.bold: true;
164        anchors { top: planet.bottom; horizontalCenter: parent.horizontalCenter }
165        property bool fadeOut: false
166
167        text: {
168            if (fadeOut)
169                return qsTr("Timeout occurred!");
170            else if (positionSource.active)
171                return qsTr("Retrieving update...")
172            else
173                return ""
174        }
175
176        Timer {
177            id: fadeoutTimer; repeat: false; interval: 3000; running: activityText.fadeOut
178            onTriggered: { activityText.fadeOut = false; }
179        }
180    }
181}
182