qtmobility/examples/logfilepositionsource/logfilepositionsource.cpp
changeset 1 2b40d63a9c3d
child 4 90517678cc4f
equal deleted inserted replaced
0:cfcbf08528c4 1:2b40d63a9c3d
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 #include <QtCore>
       
    42 
       
    43 #include "logfilepositionsource.h"
       
    44 
       
    45 LogFilePositionSource::LogFilePositionSource(QObject *parent)
       
    46     : QGeoPositionInfoSource(parent),
       
    47       logFile(new QFile(this)),
       
    48       timer(new QTimer(this))
       
    49 {
       
    50     connect(timer, SIGNAL(timeout()), this, SLOT(readNextPosition()));
       
    51 
       
    52     logFile->setFileName(QCoreApplication::applicationDirPath()
       
    53             + QDir::separator() + "simplelog.txt");
       
    54     if (!logFile->open(QIODevice::ReadOnly))
       
    55         qWarning() << "Error: cannot open source file" << logFile->fileName();
       
    56 }
       
    57 
       
    58 QGeoPositionInfo LogFilePositionSource::lastKnownPosition(bool /*fromSatellitePositioningMethodsOnly*/) const
       
    59 {
       
    60     return lastPosition;
       
    61 }
       
    62 
       
    63 LogFilePositionSource::PositioningMethods LogFilePositionSource::supportedPositioningMethods() const
       
    64 {
       
    65     return AllPositioningMethods;
       
    66 }
       
    67 
       
    68 int LogFilePositionSource::minimumUpdateInterval() const
       
    69 {
       
    70     return 500;
       
    71 }
       
    72 
       
    73 void LogFilePositionSource::startUpdates()
       
    74 {
       
    75     int interval = updateInterval();
       
    76     if (interval < minimumUpdateInterval())
       
    77         interval = minimumUpdateInterval();
       
    78 
       
    79     timer->start(interval);
       
    80 }
       
    81 
       
    82 void LogFilePositionSource::stopUpdates()
       
    83 {
       
    84     timer->stop();
       
    85 }
       
    86 
       
    87 void LogFilePositionSource::requestUpdate(int /*timeout*/)
       
    88 {
       
    89     // For simplicity, ignore timeout - assume that if data is not available
       
    90     // now, no data will be added to the file later
       
    91     if (logFile->canReadLine())
       
    92         readNextPosition();
       
    93     else
       
    94         emit updateTimeout();
       
    95 }
       
    96 
       
    97 void LogFilePositionSource::readNextPosition()
       
    98 {
       
    99     QByteArray line = logFile->readLine().trimmed();
       
   100     if (!line.isEmpty()) {
       
   101         QList<QByteArray> data = line.split(' ');
       
   102         double latitude;
       
   103         double longitude;
       
   104         bool hasLatitude = false;
       
   105         bool hasLongitude = false;
       
   106         QDateTime dateTime = QDateTime::fromString(QString(data.value(0)), Qt::ISODate);
       
   107         latitude = data.value(1).toDouble(&hasLatitude);
       
   108         longitude = data.value(2).toDouble(&hasLongitude);
       
   109 
       
   110         if (hasLatitude && hasLongitude && dateTime.isValid()) {
       
   111             QGeoCoordinate coordinate(latitude, longitude);
       
   112             QGeoPositionInfo info(coordinate, dateTime);
       
   113             if (info.isValid()) {
       
   114                 lastPosition = info;
       
   115                 emit positionUpdated(info);
       
   116             }
       
   117         }
       
   118     }
       
   119 }