1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtPositioning module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 #include "widget.h"
29 #include "ui_widget.h"
30 #include <QGeoPositionInfoSource>
31 #include <QDebug>
32 
Widget(LogWidget * logWidget,QWidget * parent)33 Widget::Widget(LogWidget *logWidget, QWidget *parent) :
34     QWidget(parent),
35     log(logWidget),
36     ui(new Ui::Widget)
37 {
38     ui->setupUi(this);
39     qDebug() << "Available:" << QGeoPositionInfoSource::availableSources();
40     m_posSource = QGeoPositionInfoSource::createDefaultSource(this);
41     if (!m_posSource)
42         qFatal("No Position Source created!");
43     connect(m_posSource, SIGNAL(positionUpdated(QGeoPositionInfo)),
44             this, SLOT(positionUpdated(QGeoPositionInfo)));
45 
46     connect(ui->horizontalSlider, SIGNAL(valueChanged(int)),
47             this, SLOT(setInterval(int)));
48     connect(m_posSource, SIGNAL(updateTimeout()),
49             this, SLOT(positionTimedOut()));
50 
51     ui->groupBox->setLayout(ui->gridLayout);
52     ui->horizontalSlider->setMinimum(m_posSource->minimumUpdateInterval());
53     ui->labelTimeOut->setVisible(false);
54 
55     connect(m_posSource, SIGNAL(error(QGeoPositionInfoSource::Error)),
56             this, SLOT(errorChanged(QGeoPositionInfoSource::Error)));
57     connect(m_posSource, &QGeoPositionInfoSource::supportedPositioningMethodsChanged,
58             this, [this]() {
59         auto methods = m_posSource->supportedPositioningMethods();
60         const QString status = QStringLiteral("Satellite: %1 ").arg(bool(methods & QGeoPositionInfoSource::SatellitePositioningMethods))
61                 + QStringLiteral("Non-Satellite: %1").arg(bool(methods & QGeoPositionInfoSource::NonSatellitePositioningMethods));
62 
63         qDebug() << "Available Positioning Methods Changed" << status;
64         log->appendLog(status);
65     });
66 }
67 
positionUpdated(QGeoPositionInfo gpsPos)68 void Widget::positionUpdated(QGeoPositionInfo gpsPos)
69 {
70     QGeoCoordinate coord = gpsPos.coordinate();
71     ui->labelLatitude->setText(QString::number(coord.latitude()));
72     ui->labelLongitude->setText(QString::number(coord.longitude()));
73     ui->labelAltitude->setText(QString::number(coord.altitude()));
74     ui->labelTimeStamp->setText(gpsPos.timestamp().toString());
75     if (gpsPos.hasAttribute(QGeoPositionInfo::HorizontalAccuracy))
76         ui->labelHAccuracy->setText(QString::number(gpsPos.attribute(QGeoPositionInfo::HorizontalAccuracy)));
77     else
78         ui->labelHAccuracy->setText(QStringLiteral("N/A"));
79 
80     if (gpsPos.hasAttribute(QGeoPositionInfo::VerticalAccuracy))
81         ui->labelVAccuracy->setText(QString::number(gpsPos.attribute(QGeoPositionInfo::VerticalAccuracy)));
82     else
83         ui->labelVAccuracy->setText(QStringLiteral("N/A"));
84 
85     if (gpsPos.hasAttribute(QGeoPositionInfo::Direction))
86         ui->labelDirection->setText(QString::number(gpsPos.attribute(QGeoPositionInfo::Direction)));
87     else
88         ui->labelDirection->setText(QStringLiteral("N/A"));
89 
90     if (gpsPos.hasAttribute(QGeoPositionInfo::GroundSpeed))
91         ui->labelSpeed->setText(QString::number(gpsPos.attribute(QGeoPositionInfo::GroundSpeed)));
92     else
93         ui->labelSpeed->setText(QStringLiteral("N/A"));
94 
95     log->appendLog(coord.toString());
96 }
97 
positionTimedOut()98 void Widget::positionTimedOut()
99 {
100     ui->labelTimeOut->setVisible(true);
101 }
102 
errorChanged(QGeoPositionInfoSource::Error err)103 void Widget::errorChanged(QGeoPositionInfoSource::Error err)
104 {
105     ui->labelErrorState->setText(QString::number(err));
106     m_posSource->stopUpdates();
107     ui->checkBox->setChecked(false);
108 }
109 
~Widget()110 Widget::~Widget()
111 {
112     delete ui;
113 }
114 
setInterval(int msec)115 void Widget::setInterval(int msec)
116 {
117     m_posSource->setUpdateInterval(msec);
118 }
119 
on_buttonRetrieve_clicked()120 void Widget::on_buttonRetrieve_clicked()
121 {
122     // Requesting current position for _one_ time
123     m_posSource->requestUpdate(10000);
124 }
125 
on_buttonStart_clicked()126 void Widget::on_buttonStart_clicked()
127 {
128     // Either start or stop the current position info source
129     bool running = ui->checkBox->isChecked();
130     if (running) {
131         ui->checkBox->setChecked(false);
132         m_posSource->stopUpdates();
133     } else {
134         ui->checkBox->setChecked(true);
135         m_posSource->startUpdates();
136     }
137 }
138 
on_radioButton_clicked()139 void Widget::on_radioButton_clicked()
140 {
141     m_posSource->setPreferredPositioningMethods(QGeoPositionInfoSource::NoPositioningMethods);
142 }
143 
on_radioButton_2_clicked()144 void Widget::on_radioButton_2_clicked()
145 {
146     m_posSource->setPreferredPositioningMethods(QGeoPositionInfoSource::SatellitePositioningMethods);
147 }
148 
on_radioButton_3_clicked()149 void Widget::on_radioButton_3_clicked()
150 {
151     m_posSource->setPreferredPositioningMethods(QGeoPositionInfoSource::NonSatellitePositioningMethods);
152 }
153 
on_radioButton_4_clicked()154 void Widget::on_radioButton_4_clicked()
155 {
156     m_posSource->setPreferredPositioningMethods(QGeoPositionInfoSource::AllPositioningMethods);
157 }
158 
on_buttonUpdateSupported_clicked()159 void Widget::on_buttonUpdateSupported_clicked()
160 {
161     QGeoPositionInfoSource::PositioningMethods m = m_posSource->supportedPositioningMethods();
162     QString text;
163     switch (m) {
164     case QGeoPositionInfoSource::NoPositioningMethods:
165         text = QStringLiteral("None");
166         break;
167     case QGeoPositionInfoSource::SatellitePositioningMethods:
168         text = QStringLiteral("Satellite");
169         break;
170     case QGeoPositionInfoSource::NonSatellitePositioningMethods:
171         text = QStringLiteral("Non Satellite");
172         break;
173     case QGeoPositionInfoSource::AllPositioningMethods:
174         text = QStringLiteral("All");
175         break;
176     }
177 
178     ui->labelSupported->setText(text);
179 }
180 
on_buttonResetError_clicked()181 void Widget::on_buttonResetError_clicked()
182 {
183     ui->labelErrorState->setText(QStringLiteral("N/A"));
184 }
185