qtmobility/examples/fetchgooglemaps/mapwindow.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 
       
    42 #include <QtGui>
       
    43 #include <QtWebKit>
       
    44 
       
    45 #include <qgeopositioninfosource.h>
       
    46 #include <qnmeapositioninfosource.h>
       
    47 #include <qgeosatelliteinfosource.h>
       
    48 #include <qnetworksession.h>
       
    49 #include <qnetworkconfigmanager.h>
       
    50 
       
    51 #include "satellitedialog.h"
       
    52 
       
    53 #include "mapwindow.h"
       
    54 
       
    55 // Use the special 'localhost' key for the Google Maps key
       
    56 const QString GMAPS_STATICMAP_URL_TEMPLATE =  "http://maps.google.com/staticmap?center=%1,%2&zoom=14&size=%3x%4&map type=mobile&markers=%1,%2&key=ABQIAAAAnfs7bKE82qgb3Zc2YyS-oBT2yXp_ZAY8_ufC3CFXhHIE1NvwkxSySz_REpPq-4WZA27OwgbtyR3VcA&sensor=false";
       
    57 
       
    58 
       
    59 MapWindow::MapWindow(QWidget *parent, Qt::WFlags flags)
       
    60         : QMainWindow(parent, flags),
       
    61         webView(new QWebView),
       
    62         posLabel(new QLabel),
       
    63         headingAndSpeedLabel(new QLabel),
       
    64         dateTimeLabel(new QLabel),
       
    65         loading(false),
       
    66         usingLogFile(false),
       
    67         location(0),
       
    68         waitingForFix(false)
       
    69 {
       
    70     location = QGeoPositionInfoSource::createDefaultSource(this);
       
    71     if (!location) {
       
    72         QNmeaPositionInfoSource *nmeaSource = new QNmeaPositionInfoSource(QNmeaPositionInfoSource::SimulationMode, this);
       
    73         QFile *logFile = new QFile(QApplication::applicationDirPath()
       
    74                                    + QDir::separator() + "nmealog.txt", this);
       
    75         nmeaSource->setDevice(logFile);
       
    76         location = nmeaSource;
       
    77 
       
    78         usingLogFile = true;
       
    79     }
       
    80 
       
    81     location->setUpdateInterval(5000);
       
    82     connect(location, SIGNAL(positionUpdated(QGeoPositionInfo)),
       
    83             this, SLOT(positionUpdated(QGeoPositionInfo)));
       
    84 
       
    85     connect(webView, SIGNAL(loadStarted()), this, SLOT(loadStarted()));
       
    86     connect(webView, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool)));
       
    87 
       
    88     QWidget *mainWidget = new QWidget;
       
    89     QVBoxLayout *layout = new QVBoxLayout(mainWidget);
       
    90     layout->addWidget(webView);
       
    91     layout->addWidget(posLabel);
       
    92     layout->addWidget(headingAndSpeedLabel);
       
    93     layout->addWidget(dateTimeLabel);
       
    94     setCentralWidget(mainWidget);
       
    95 
       
    96 #if !defined(Q_OS_SYMBIAN)
       
    97     resize(300, 300);
       
    98 #endif
       
    99     setWindowTitle(tr("Google Maps Demo"));
       
   100 
       
   101     QTimer::singleShot(0, this, SLOT(delayedInit()));
       
   102 }
       
   103 
       
   104 MapWindow::~MapWindow()
       
   105 {
       
   106     location->stopUpdates();
       
   107     session->close();
       
   108 }
       
   109 
       
   110 void MapWindow::delayedInit()
       
   111 {
       
   112     if (usingLogFile) {
       
   113         QMessageBox::information(this, tr("Fetch Google Maps"),
       
   114                                  tr("No GPS support detected, using GPS data from a sample log file instead."));
       
   115     } else {
       
   116         waitForFix();
       
   117         location->stopUpdates();
       
   118     }
       
   119 
       
   120     // Set Internet Access Point
       
   121     QNetworkConfigurationManager manager;
       
   122     const bool canStartIAP = (manager.capabilities()
       
   123                               & QNetworkConfigurationManager::CanStartAndStopInterfaces);
       
   124     // Is there default access point, use it
       
   125     QNetworkConfiguration cfg = manager.defaultConfiguration();
       
   126     if (!cfg.isValid() || (!canStartIAP && cfg.state() != QNetworkConfiguration::Active)) {
       
   127         QMessageBox::information(this, tr("Flickr Demo"), tr(
       
   128                                      "Available Access Points not found."));
       
   129         return;
       
   130     }
       
   131 
       
   132     session = new QNetworkSession(cfg, this);
       
   133     session->open();
       
   134     session->waitForOpened(-1);
       
   135 
       
   136     connect(location, SIGNAL(updateTimeout()), this, SLOT(waitForFix()));
       
   137 
       
   138     location->startUpdates();
       
   139 }
       
   140 
       
   141 // Brings up a satellite strength dialog box until a position fix is received.
       
   142 // This will also start the position updates if they are not already started.
       
   143 void MapWindow::waitForFix() {
       
   144     if (waitingForFix)
       
   145         return;
       
   146 
       
   147     waitingForFix = true;
       
   148 
       
   149     QGeoSatelliteInfoSource *satellite = QGeoSatelliteInfoSource::createDefaultSource(this);
       
   150 
       
   151     if (satellite) {
       
   152         SatelliteDialog *dialog = new SatelliteDialog(this,
       
   153                 30,
       
   154                 SatelliteDialog::ExitOnFixOrCancel,
       
   155                 SatelliteDialog::OrderByPrnNumber,
       
   156                 SatelliteDialog::ScaleToMaxPossible);
       
   157 
       
   158         dialog->connectSources(location, satellite);
       
   159 
       
   160         location->startUpdates();
       
   161         satellite->startUpdates();
       
   162 
       
   163         dialog->exec();
       
   164 
       
   165         satellite->stopUpdates();
       
   166 
       
   167         delete dialog;
       
   168         delete satellite;
       
   169     }
       
   170 
       
   171     waitingForFix = false;
       
   172 }
       
   173 
       
   174 void MapWindow::start()
       
   175 {
       
   176     location->startUpdates();
       
   177 }
       
   178 
       
   179 void MapWindow::positionUpdated(const QGeoPositionInfo &info)
       
   180 {
       
   181     QString heading = "?";
       
   182     QString speed = "?";
       
   183     if (info.hasAttribute(QGeoPositionInfo::Direction))
       
   184         heading = QString("%1%2").arg(info.attribute(QGeoPositionInfo::Direction)).arg(QChar(0x00b0));
       
   185     if (info.hasAttribute(QGeoPositionInfo::GroundSpeed))
       
   186         speed = QString::number(info.attribute(QGeoPositionInfo::GroundSpeed) * 3.6, 'f', 1);
       
   187     posLabel->setText(tr("Position: %1").arg(info.coordinate().toString()));
       
   188     headingAndSpeedLabel->setText(tr("Bearing %1, travelling at %2 km/h").arg(heading).arg(speed));
       
   189 
       
   190     dateTimeLabel->setText(tr("(Last update: %1)").
       
   191                            arg(info.dateTime().toLocalTime().time().toString()));
       
   192 
       
   193     if (!loading) {
       
   194         // Google Maps does not provide maps larger than 640x480
       
   195         int width = qMin(webView->width(), 640);
       
   196         int height = qMin(webView->height(), 480);
       
   197         QString url = GMAPS_STATICMAP_URL_TEMPLATE
       
   198                       .arg(QString::number(info.coordinate().latitude()))
       
   199                       .arg(QString::number(info.coordinate().longitude()))
       
   200                       .arg(QString::number(width))
       
   201                       .arg(QString::number(height));
       
   202         webView->load(url);
       
   203     }
       
   204 }
       
   205 
       
   206 void MapWindow::loadStarted()
       
   207 {
       
   208     loading = true;
       
   209     webView->setUpdatesEnabled(false);
       
   210 }
       
   211 
       
   212 void MapWindow::loadFinished(bool)
       
   213 {
       
   214     loading = false;
       
   215     webView->setUpdatesEnabled(true);
       
   216 }