Example #1
0
/*!
    Sets the preferred positioning methods for this source to \a methods.

    If \a methods includes a method that is not supported by the source, the
    unsupported method will be ignored.

    If \a methods does not include any methods supported by the source, the
    preferred methods will be set to the set of methods which the source supports.

    \bold {Note:} When reimplementing this method, subclasses must call the
    base method implementation to ensure preferredPositioningMethods() returns the correct value.

    \since 1.0
    \sa supportedPositioningMethods()
*/
void QGeoPositionInfoSource::setPreferredPositioningMethods(PositioningMethods methods)
{
    d->methods = methods & supportedPositioningMethods();
    if (d->methods == 0) {
        d->methods = supportedPositioningMethods();
    }
}
/*!
    \internal
*/
void QDeclarativePositionSource::socketConnected()
{
#ifdef QDECLARATIVE_POSITION_DEBUG
    qDebug() << "Socket connected: " << m_nmeaSocket->peerName();
#endif
    PositioningMethods previousPositioningMethods = supportedPositioningMethods();

    // The current position source needs to be deleted
    // because QNmeaPositionInfoSource can be bound only to a one file.
    delete m_nmeaFile;
    m_nmeaFile = 0;
    delete m_positionSource;

    m_positionSource = new QNmeaPositionInfoSource(QNmeaPositionInfoSource::RealTimeMode);
    (qobject_cast<QNmeaPositionInfoSource *>(m_positionSource))->setDevice(m_nmeaSocket);

    connect(m_positionSource, &QNmeaPositionInfoSource::positionUpdated,
            this, &QDeclarativePositionSource::positionUpdateReceived);
    connect(m_positionSource, SIGNAL(error(QGeoPositionInfoSource::Error)),
            this, SLOT(sourceErrorReceived(QGeoPositionInfoSource::Error)));
    connect(m_positionSource, SIGNAL(updateTimeout()),
            this, SLOT(updateTimeoutReceived()));

    setPosition(m_positionSource->lastKnownPosition());

    if (m_active && !m_singleUpdate) {
        // Keep on updating even though source changed
        QTimer::singleShot(0, this, SLOT(start()));
    }

    if (previousPositioningMethods != supportedPositioningMethods())
        emit supportedPositioningMethodsChanged();
}
void QDeclarativePositionSource::setName(const QString &newName)
{
    if (m_positionSource && m_positionSource->sourceName() == newName)
        return;

    const QString previousName = name();
    int previousUpdateInterval = updateInterval();
    PositioningMethods previousPositioningMethods = supportedPositioningMethods();
    PositioningMethods previousPreferredPositioningMethods = preferredPositioningMethods();

    delete m_positionSource;
    if (newName.isEmpty())
        m_positionSource = QGeoPositionInfoSource::createDefaultSource(this);
    else
        m_positionSource = QGeoPositionInfoSource::createSource(newName, this);

    if (m_positionSource) {
        connect(m_positionSource, SIGNAL(positionUpdated(QGeoPositionInfo)),
                this, SLOT(positionUpdateReceived(QGeoPositionInfo)));
        connect(m_positionSource, SIGNAL(error(QGeoPositionInfoSource::Error)),
                this, SLOT(sourceErrorReceived(QGeoPositionInfoSource::Error)));
        connect(m_positionSource, SIGNAL(updateTimeout()),
                this, SLOT(updateTimeoutReceived()));

        m_positionSource->setUpdateInterval(m_updateInterval);
        m_positionSource->setPreferredPositioningMethods(
            static_cast<QGeoPositionInfoSource::PositioningMethods>(int(m_preferredPositioningMethods)));

        setPosition(m_positionSource->lastKnownPosition());
    }

    if (previousUpdateInterval != updateInterval())
        emit updateIntervalChanged();

    if (previousPreferredPositioningMethods != preferredPositioningMethods())
        emit preferredPositioningMethodsChanged();

    if (previousPositioningMethods != supportedPositioningMethods())
        emit supportedPositioningMethodsChanged();

    emit validityChanged();

    if (m_active) {
        m_active = false;
        emit activeChanged();
    }

    if (previousName != name())
        emit nameChanged();
}
void QGeoPositionInfoSourceAndroid::setPreferredPositioningMethods(PositioningMethods methods)
{

    if(methods & supportedPositioningMethods())
    {
        setProvider((QtLocationJni::LocationProviders)(long)methods);
    }
    QGeoPositionInfoSource::setPreferredPositioningMethods(methods);
}
void QDeclarativePositionSource::componentComplete()
{
    if (!m_positionSource) {
        int previousUpdateInterval = updateInterval();
        PositioningMethods previousPositioningMethods = supportedPositioningMethods();
        PositioningMethods previousPreferredPositioningMethods = preferredPositioningMethods();

        m_positionSource = QGeoPositionInfoSource::createDefaultSource(this);
        if (m_positionSource) {
            connect(m_positionSource, SIGNAL(positionUpdated(QGeoPositionInfo)),
                    this, SLOT(positionUpdateReceived(QGeoPositionInfo)));
            connect(m_positionSource, SIGNAL(error(QGeoPositionInfoSource::Error)),
                    this, SLOT(sourceErrorReceived(QGeoPositionInfoSource::Error)));
            connect(m_positionSource, SIGNAL(updateTimeout()),
                    this, SLOT(updateTimeoutReceived()));

            m_positionSource->setUpdateInterval(m_updateInterval);
            m_positionSource->setPreferredPositioningMethods(
                static_cast<QGeoPositionInfoSource::PositioningMethods>(int(m_preferredPositioningMethods)));

            setPosition(m_positionSource->lastKnownPosition());
        }

        if (previousUpdateInterval != updateInterval())
            emit updateIntervalChanged();

        if (previousPreferredPositioningMethods != preferredPositioningMethods())
            emit preferredPositioningMethodsChanged();

        if (previousPositioningMethods != supportedPositioningMethods())
            emit supportedPositioningMethodsChanged();

        emit validityChanged();

        if (m_active) {
            m_active = false;
            emit activeChanged();
        }

        emit nameChanged();
    }
}
/*!
    \internal
*/
void QDeclarativePositionSource::setNmeaSource(const QUrl &nmeaSource)
{
    if (nmeaSource.scheme() == QLatin1String("socket")) {
        if (m_nmeaSocket
                && nmeaSource.host() == m_nmeaSocket->peerName()
                && nmeaSource.port() == m_nmeaSocket->peerPort()) {
            return;
        }

        delete m_nmeaSocket;
        m_nmeaSocket = new QTcpSocket();

        connect(m_nmeaSocket, static_cast<void (QTcpSocket::*)(QAbstractSocket::SocketError)> (&QAbstractSocket::error),
                this, &QDeclarativePositionSource::socketError);
        connect(m_nmeaSocket, &QTcpSocket::connected,
                this, &QDeclarativePositionSource::socketConnected);

        m_nmeaSocket->connectToHost(nmeaSource.host(), nmeaSource.port(), QTcpSocket::ReadOnly);
    } else {
        // Strip the filename. This is clumsy but the file may be prefixed in several
        // ways: "file:///", "qrc:///", "/", "" in platform dependent manner.
        QString localFileName = nmeaSource.toString();
        if (!QFile::exists(localFileName)) {
            if (localFileName.startsWith("qrc:///")) {
                localFileName.remove(0, 7);
            } else if (localFileName.startsWith("file:///")) {
                localFileName.remove(0, 7);
            } else if (localFileName.startsWith("qrc:/")) {
                localFileName.remove(0, 5);
            }
            if (!QFile::exists(localFileName) && localFileName.startsWith('/')) {
                localFileName.remove(0,1);
            }
        }
        if (m_nmeaFileName == localFileName)
            return;
        m_nmeaFileName = localFileName;

        PositioningMethods previousPositioningMethods = supportedPositioningMethods();

        // The current position source needs to be deleted
        // because QNmeaPositionInfoSource can be bound only to a one file.
        delete m_nmeaSocket;
        m_nmeaSocket = 0;
        delete m_positionSource;
        m_positionSource = 0;
        setPosition(QGeoPositionInfo());
        // Create the NMEA source based on the given data. QML has automatically set QUrl
        // type to point to correct path. If the file is not found, check if the file actually
        // was an embedded resource file.
        delete m_nmeaFile;
        m_nmeaFile = new QFile(localFileName);
        if (!m_nmeaFile->exists()) {
            localFileName.prepend(":");
            m_nmeaFile->setFileName(localFileName);
        }
        if (m_nmeaFile->exists()) {
#ifdef QDECLARATIVE_POSITION_DEBUG
            qDebug() << "QDeclarativePositionSource NMEA File was found: " << localFileName;
#endif
            m_positionSource = new QNmeaPositionInfoSource(QNmeaPositionInfoSource::SimulationMode);
            (qobject_cast<QNmeaPositionInfoSource *>(m_positionSource))->setDevice(m_nmeaFile);
            connect(m_positionSource, SIGNAL(positionUpdated(QGeoPositionInfo)),
                    this, SLOT(positionUpdateReceived(QGeoPositionInfo)));
            connect(m_positionSource, SIGNAL(error(QGeoPositionInfoSource::Error)),
                    this, SLOT(sourceErrorReceived(QGeoPositionInfoSource::Error)));
            connect(m_positionSource, SIGNAL(updateTimeout()),
                    this, SLOT(updateTimeoutReceived()));

            setPosition(m_positionSource->lastKnownPosition());
            if (m_active && !m_singleUpdate) {
                // Keep on updating even though source changed
                QTimer::singleShot(0, this, SLOT(start()));
            }
        } else {
            qmlInfo(this) << QStringLiteral("Nmea file not found") << localFileName;
#ifdef QDECLARATIVE_POSITION_DEBUG
            qDebug() << "QDeclarativePositionSource NMEA File was not found: " << localFileName;
#endif
            if (m_active) {
                m_active = false;
                m_singleUpdate = false;
                emit activeChanged();
            }
        }

        if (previousPositioningMethods != supportedPositioningMethods())
            emit supportedPositioningMethodsChanged();
    }

    m_nmeaSource = nmeaSource;
    emit nmeaSourceChanged();
}