1 #include "gpscontrol.h"
2 #include <QtNetwork/QNetworkConfigurationManager>
4 GpsControl::GpsControl(QObject *parent) :
7 GpsSource(QGeoPositionInfoSource::createDefaultSource(this)),
11 qDebug() << "* GpsControl::GpsControl";
13 GpsTimeout.setSingleShot(true);
14 GpsInterval.setSingleShot(true);
16 setPositioningMethod(GpsSettings.value("gps_method", "cell").toString());
17 setTimeOut(GpsSettings.value("gps_timeout", 60).toInt());
18 setInterval(GpsSettings.value("gps_interval", 15*60).toInt());
20 if (!GpsSource) qDebug() << "* GpsControl::GpsControl" << "Not GpsSource";
22 if (GpsSource) connect(GpsSource, SIGNAL(positionUpdated(QGeoPositionInfo)),
23 this, SLOT(setCurrentLocation(QGeoPositionInfo)));
25 connect(this, SIGNAL(gotUpdate()),
26 this, SLOT(onGotUpdate()));
28 connect(this, SIGNAL(gotFix()),
29 this, SLOT(onGotFix()));
33 void GpsControl::setTimeOut(int sec) {
34 qDebug() << "* GpsControl::setTimeOut" << sec;
35 GpsSettings.setValue("gps_timeout", sec);
38 int GpsControl::getTimeOut() {
39 qDebug() << "* GpsControl::getTimeOut";
40 return GpsSettings.value("gps_timeout").toInt();
43 void GpsControl::setInterval(int sec) {
44 qDebug() << "* GpsControl::setInterval" << sec;
45 GpsSettings.setValue("gps_interval", sec);
48 int GpsControl::getInterval() {
49 qDebug() << "* GpsControl::getInterval";
50 return GpsSettings.value("gps_interval").toInt();
53 void GpsControl::setPositioningMethod(QString method) {
54 qDebug() << "* GpsControl::setPositioningMethod" << method;
55 if (method == "gps") {
56 GpsSettings.setValue("gps_method", method);
57 if (GpsSource) GpsSource->setPreferredPositioningMethods(
58 QGeoPositionInfoSource::SatellitePositioningMethods);
60 if (method == "cell") {
61 GpsSettings.setValue("gps_method", method);
62 if (GpsSource) GpsSource->setPreferredPositioningMethods(
63 QGeoPositionInfoSource::NonSatellitePositioningMethods);
65 if (method == "all") {
66 GpsSettings.setValue("gps_method", method);
67 if (GpsSource) GpsSource->setPreferredPositioningMethods(
68 QGeoPositionInfoSource::AllPositioningMethods);
72 void GpsControl::startUpdates() {
73 qDebug() << "* GpsControl::startUpdates";
75 GpsTimeout.singleShot(GpsSettings.value("gps_timeout").toInt()*1000,
76 this, SLOT(stopUpdates()));
77 GpsInterval.singleShot(GpsSettings.value("gps_interval").toInt()*1000,
78 this, SLOT(startUpdates()));
80 QNetworkConfigurationManager mgr;
81 if (!GpsSettings.value("net_auto").toBool() && !mgr.isOnline()) {
82 qDebug() << "* GpsControl::startUpdates" << "offline";
86 if (GpsSource) GpsSource->startUpdates();
89 void GpsControl::stopUpdates(bool force) {
90 qDebug() << "* GpsControl::stopUpdates" << force;
92 if (GpsSource) GpsSource->stopUpdates();
98 if (GpsSource && GpsSource->lastKnownPosition(GpsSettings.value("gps_method")
99 == "gps" ? true : false).isValid() ) {
105 double GpsControl::getCurrentLatitude() {
106 qDebug() << "* GpsControl::getCurrentLatitude";
107 return GpsPosition.coordinate().latitude();
110 double GpsControl::getCurrentLongitude() {
111 qDebug() << "* GpsControl::getCurrentLongitude";
112 return GpsPosition.coordinate().longitude();
115 double GpsControl::getCurrentAccuracy() {
116 qDebug() << "* GpsControl::getCurrentAccuracy";
117 return GpsPosition.attribute(QGeoPositionInfo::HorizontalAccuracy);
120 unsigned int GpsControl::getCurrentTimestamp() {
121 qDebug() << "* GpsControl::getCurrentTimestamp";
122 return GpsPosition.timestamp().toTime_t();
125 void GpsControl::setCurrentLocation(QGeoPositionInfo pos) {
126 qDebug() << "* GpsControl::setCurrentLocation" << pos;