qtmobility/examples/fetchgooglemaps/mapwindow.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 5 453da2cfceef
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
    43 #include <QtWebKit>
    43 #include <QtWebKit>
    44 
    44 
    45 #include <qgeopositioninfosource.h>
    45 #include <qgeopositioninfosource.h>
    46 #include <qnmeapositioninfosource.h>
    46 #include <qnmeapositioninfosource.h>
    47 #include <qgeosatelliteinfosource.h>
    47 #include <qgeosatelliteinfosource.h>
       
    48 #ifndef Q_WS_MAEMO_5
    48 #include <qnetworksession.h>
    49 #include <qnetworksession.h>
    49 #include <qnetworkconfigmanager.h>
    50 #include <qnetworkconfigmanager.h>
       
    51 #endif
    50 
    52 
    51 #include "satellitedialog.h"
    53 #include "satellitedialog.h"
    52 
    54 
    53 #include "mapwindow.h"
    55 #include "mapwindow.h"
       
    56 
       
    57 QTM_USE_NAMESPACE
    54 
    58 
    55 // Use the special 'localhost' key for the Google Maps key
    59 // 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";
    60 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 
    61 
    58 
    62 
    59 MapWindow::MapWindow(QWidget *parent, Qt::WFlags flags)
    63 MapWindow::MapWindow(QWidget *parent, Qt::WFlags flags)
    60         : QMainWindow(parent, flags),
    64         : QMainWindow(parent, flags),
    61         webView(new QWebView),
       
    62         posLabel(new QLabel),
       
    63         headingAndSpeedLabel(new QLabel),
       
    64         dateTimeLabel(new QLabel),
       
    65         loading(false),
    65         loading(false),
    66         usingLogFile(false),
    66         usingLogFile(false),
    67         location(0),
    67         location(0),
    68         waitingForFix(false)
    68         waitingForFix(false)
    69 {
    69 {
       
    70     webView = new QWebView(this);
       
    71     webView->setMaximumSize(640, 480);
       
    72 
       
    73     posLabel = new QLabel(this);
       
    74     posLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
       
    75 
       
    76     headingAndSpeedLabel = new QLabel(this);
       
    77     headingAndSpeedLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
       
    78 
       
    79     dateTimeLabel = new QLabel(this);
       
    80     dateTimeLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
       
    81 
    70     location = QGeoPositionInfoSource::createDefaultSource(this);
    82     location = QGeoPositionInfoSource::createDefaultSource(this);
    71     if (!location) {
    83     if (!location) {
    72         QNmeaPositionInfoSource *nmeaSource = new QNmeaPositionInfoSource(QNmeaPositionInfoSource::SimulationMode, this);
    84         QNmeaPositionInfoSource *nmeaSource = new QNmeaPositionInfoSource(QNmeaPositionInfoSource::SimulationMode, this);
    73         QFile *logFile = new QFile(QApplication::applicationDirPath()
    85         QFile *logFile = new QFile(QApplication::applicationDirPath()
    74                                    + QDir::separator() + "nmealog.txt", this);
    86                                    + QDir::separator() + "nmealog.txt", this);
    77 
    89 
    78         usingLogFile = true;
    90         usingLogFile = true;
    79     }
    91     }
    80 
    92 
    81     location->setUpdateInterval(5000);
    93     location->setUpdateInterval(5000);
       
    94 
    82     connect(location, SIGNAL(positionUpdated(QGeoPositionInfo)),
    95     connect(location, SIGNAL(positionUpdated(QGeoPositionInfo)),
    83             this, SLOT(positionUpdated(QGeoPositionInfo)));
    96             this, SLOT(positionUpdated(QGeoPositionInfo)));
    84 
    97 
    85     connect(webView, SIGNAL(loadStarted()), this, SLOT(loadStarted()));
    98     connect(webView, SIGNAL(loadStarted()), this, SLOT(loadStarted()));
    86     connect(webView, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool)));
    99     connect(webView, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool)));
    87 
   100 
    88     QWidget *mainWidget = new QWidget;
   101     QWidget *mainWidget = new QWidget;
    89     QVBoxLayout *layout = new QVBoxLayout(mainWidget);
   102     QVBoxLayout *layout = new QVBoxLayout(mainWidget);
    90     layout->addWidget(webView);
   103     layout->addWidget(webView, 1);
    91     layout->addWidget(posLabel);
   104     
    92     layout->addWidget(headingAndSpeedLabel);
   105     QVBoxLayout *labelLayout = new QVBoxLayout();
    93     layout->addWidget(dateTimeLabel);
   106     labelLayout->addWidget(posLabel);
       
   107     labelLayout->addWidget(headingAndSpeedLabel);
       
   108     labelLayout->addWidget(dateTimeLabel);
       
   109 
       
   110     layout->addLayout(labelLayout, 0);
       
   111     layout->setSizeConstraint(QLayout::SetMaximumSize);
       
   112 
       
   113     int maxHeight = webView->maximumSize().height();
       
   114     maxHeight += posLabel->size().height();
       
   115     maxHeight += headingAndSpeedLabel->size().height();
       
   116     maxHeight += dateTimeLabel->size().height();
       
   117 
       
   118     setMaximumHeight(maxHeight);
       
   119 
       
   120     int maxWidth = webView->maximumWidth();
       
   121     maxWidth = qMin(maxWidth, posLabel->maximumWidth());
       
   122     maxWidth = qMin(maxWidth, headingAndSpeedLabel->maximumWidth());
       
   123     maxWidth = qMin(maxWidth, dateTimeLabel->maximumWidth());
       
   124     
       
   125     setMaximumWidth(maxWidth);
       
   126 
    94     setCentralWidget(mainWidget);
   127     setCentralWidget(mainWidget);
    95 
   128 
    96 #if !defined(Q_OS_SYMBIAN)
   129 #if !defined(Q_OS_SYMBIAN)
    97     resize(300, 300);
   130     resize(300, 300);
    98 #endif
   131 #endif
   102 }
   135 }
   103 
   136 
   104 MapWindow::~MapWindow()
   137 MapWindow::~MapWindow()
   105 {
   138 {
   106     location->stopUpdates();
   139     location->stopUpdates();
       
   140 #ifndef Q_WS_MAEMO_5
   107     session->close();
   141     session->close();
       
   142 #endif
   108 }
   143 }
   109 
   144 
   110 void MapWindow::delayedInit()
   145 void MapWindow::delayedInit()
   111 {
   146 {
   112     if (usingLogFile) {
   147     if (usingLogFile) {
   115     } else {
   150     } else {
   116         waitForFix();
   151         waitForFix();
   117         location->stopUpdates();
   152         location->stopUpdates();
   118     }
   153     }
   119 
   154 
       
   155 #ifndef Q_WS_MAEMO_5
   120     // Set Internet Access Point
   156     // Set Internet Access Point
   121     QNetworkConfigurationManager manager;
   157     QNetworkConfigurationManager manager;
   122     const bool canStartIAP = (manager.capabilities()
   158     const bool canStartIAP = (manager.capabilities()
   123                               & QNetworkConfigurationManager::CanStartAndStopInterfaces);
   159                               & QNetworkConfigurationManager::CanStartAndStopInterfaces);
   124     // Is there default access point, use it
   160     // Is there default access point, use it
   125     QNetworkConfiguration cfg = manager.defaultConfiguration();
   161     QTM_PREPEND_NAMESPACE(QNetworkConfiguration) cfg = manager.defaultConfiguration();
   126     if (!cfg.isValid() || (!canStartIAP && cfg.state() != QNetworkConfiguration::Active)) {
   162     if (!cfg.isValid() || (!canStartIAP && cfg.state() != QTM_PREPEND_NAMESPACE(QNetworkConfiguration)::Active)) {
   127         QMessageBox::information(this, tr("Flickr Demo"), tr(
   163         QMessageBox::information(this, tr("Flickr Demo"), tr(
   128                                      "Available Access Points not found."));
   164                                      "Available Access Points not found."));
   129         return;
   165         return;
   130     }
   166     }
   131 
   167 
   132     session = new QNetworkSession(cfg, this);
   168     session = new QTM_PREPEND_NAMESPACE(QNetworkSession)(cfg, this);
   133     session->open();
   169     session->open();
   134     session->waitForOpened(-1);
   170     session->waitForOpened(-1);
   135 
   171 #endif
   136     connect(location, SIGNAL(updateTimeout()), this, SLOT(waitForFix()));
   172     connect(location, SIGNAL(updateTimeout()), this, SLOT(waitForFix()));
   137 
   173 
   138     location->startUpdates();
   174     location->startUpdates();
   139 }
   175 }
   140 
   176 
   186         speed = QString::number(info.attribute(QGeoPositionInfo::GroundSpeed) * 3.6, 'f', 1);
   222         speed = QString::number(info.attribute(QGeoPositionInfo::GroundSpeed) * 3.6, 'f', 1);
   187     posLabel->setText(tr("Position: %1").arg(info.coordinate().toString()));
   223     posLabel->setText(tr("Position: %1").arg(info.coordinate().toString()));
   188     headingAndSpeedLabel->setText(tr("Bearing %1, travelling at %2 km/h").arg(heading).arg(speed));
   224     headingAndSpeedLabel->setText(tr("Bearing %1, travelling at %2 km/h").arg(heading).arg(speed));
   189 
   225 
   190     dateTimeLabel->setText(tr("(Last update: %1)").
   226     dateTimeLabel->setText(tr("(Last update: %1)").
   191                            arg(info.dateTime().toLocalTime().time().toString()));
   227                            arg(info.timestamp().toLocalTime().time().toString()));
   192 
   228 
   193     if (!loading) {
   229     if (!loading) {
   194         // Google Maps does not provide maps larger than 640x480
   230         // Google Maps does not provide maps larger than 640x480
   195         int width = qMin(webView->width(), 640);
   231         int width = qMin(webView->width(), 640);
   196         int height = qMin(webView->height(), 480);
   232         int height = qMin(webView->height(), 480);
       
   233 
       
   234         if ((width == 0) || (height == 0))
       
   235             return;
       
   236 
   197         QString url = GMAPS_STATICMAP_URL_TEMPLATE
   237         QString url = GMAPS_STATICMAP_URL_TEMPLATE
   198                       .arg(QString::number(info.coordinate().latitude()))
   238                       .arg(QString::number(info.coordinate().latitude()))
   199                       .arg(QString::number(info.coordinate().longitude()))
   239                       .arg(QString::number(info.coordinate().longitude()))
   200                       .arg(QString::number(width))
   240                       .arg(QString::number(width))
   201                       .arg(QString::number(height));
   241                       .arg(QString::number(height));