|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 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:BSD$ |
|
10 ** You may use this file under the terms of the BSD license as follows: |
|
11 ** |
|
12 ** "Redistribution and use in source and binary forms, with or without |
|
13 ** modification, are permitted provided that the following conditions are |
|
14 ** met: |
|
15 ** * Redistributions of source code must retain the above copyright |
|
16 ** notice, this list of conditions and the following disclaimer. |
|
17 ** * Redistributions in binary form must reproduce the above copyright |
|
18 ** notice, this list of conditions and the following disclaimer in |
|
19 ** the documentation and/or other materials provided with the |
|
20 ** distribution. |
|
21 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
|
22 ** the names of its contributors may be used to endorse or promote |
|
23 ** products derived from this software without specific prior written |
|
24 ** permission. |
|
25 ** |
|
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
|
37 ** $QT_END_LICENSE$ |
|
38 ** |
|
39 ****************************************************************************/ |
|
40 |
|
41 #include <QtGui> |
|
42 #include <QtWebKit> |
|
43 |
|
44 #include <qgeopositioninfosource.h> |
|
45 #include <qnmeapositioninfosource.h> |
|
46 #include <qgeosatelliteinfosource.h> |
|
47 #include <qnetworksession.h> |
|
48 #include <qnetworkconfigmanager.h> |
|
49 |
|
50 #include "satellitedialog.h" |
|
51 |
|
52 #include "mapwindow.h" |
|
53 |
|
54 QTM_USE_NAMESPACE |
|
55 |
|
56 // Use the special 'localhost' key for the Google Maps key |
|
57 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"; |
|
58 |
|
59 |
|
60 MapWindow::MapWindow(QWidget *parent, Qt::WFlags flags) |
|
61 : QMainWindow(parent, flags), |
|
62 loading(false), |
|
63 usingLogFile(false), |
|
64 location(0), |
|
65 waitingForFix(false) |
|
66 { |
|
67 webView = new QWebView(this); |
|
68 webView->setMaximumSize(640, 480); |
|
69 |
|
70 posLabel = new QLabel(this); |
|
71 posLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); |
|
72 |
|
73 headingAndSpeedLabel = new QLabel(this); |
|
74 headingAndSpeedLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); |
|
75 |
|
76 dateTimeLabel = new QLabel(this); |
|
77 dateTimeLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); |
|
78 |
|
79 location = QGeoPositionInfoSource::createDefaultSource(this); |
|
80 if (!location) { |
|
81 QNmeaPositionInfoSource *nmeaSource = new QNmeaPositionInfoSource(QNmeaPositionInfoSource::SimulationMode, this); |
|
82 QFile *logFile = new QFile(QApplication::applicationDirPath() |
|
83 + QDir::separator() + "nmealog.txt", this); |
|
84 nmeaSource->setDevice(logFile); |
|
85 location = nmeaSource; |
|
86 |
|
87 usingLogFile = true; |
|
88 } |
|
89 |
|
90 location->setUpdateInterval(5000); |
|
91 |
|
92 connect(location, SIGNAL(positionUpdated(QGeoPositionInfo)), |
|
93 this, SLOT(positionUpdated(QGeoPositionInfo))); |
|
94 |
|
95 connect(webView, SIGNAL(loadStarted()), this, SLOT(loadStarted())); |
|
96 connect(webView, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool))); |
|
97 |
|
98 QWidget *mainWidget = new QWidget; |
|
99 QVBoxLayout *layout = new QVBoxLayout(mainWidget); |
|
100 layout->addWidget(webView, 1); |
|
101 |
|
102 QVBoxLayout *labelLayout = new QVBoxLayout(); |
|
103 labelLayout->addWidget(posLabel); |
|
104 labelLayout->addWidget(headingAndSpeedLabel); |
|
105 labelLayout->addWidget(dateTimeLabel); |
|
106 |
|
107 layout->addLayout(labelLayout, 0); |
|
108 layout->setSizeConstraint(QLayout::SetMaximumSize); |
|
109 |
|
110 int maxHeight = webView->maximumSize().height(); |
|
111 maxHeight += posLabel->size().height(); |
|
112 maxHeight += headingAndSpeedLabel->size().height(); |
|
113 maxHeight += dateTimeLabel->size().height(); |
|
114 |
|
115 setMaximumHeight(maxHeight); |
|
116 |
|
117 int maxWidth = webView->maximumWidth(); |
|
118 maxWidth = qMin(maxWidth, posLabel->maximumWidth()); |
|
119 maxWidth = qMin(maxWidth, headingAndSpeedLabel->maximumWidth()); |
|
120 maxWidth = qMin(maxWidth, dateTimeLabel->maximumWidth()); |
|
121 |
|
122 setMaximumWidth(maxWidth); |
|
123 |
|
124 setCentralWidget(mainWidget); |
|
125 |
|
126 #if !defined(Q_OS_SYMBIAN) |
|
127 resize(300, 300); |
|
128 #endif |
|
129 setWindowTitle(tr("Google Maps Demo")); |
|
130 |
|
131 QTimer::singleShot(0, this, SLOT(delayedInit())); |
|
132 } |
|
133 |
|
134 MapWindow::~MapWindow() |
|
135 { |
|
136 location->stopUpdates(); |
|
137 session->close(); |
|
138 } |
|
139 |
|
140 void MapWindow::delayedInit() |
|
141 { |
|
142 if (usingLogFile) { |
|
143 QMessageBox::information(this, tr("Fetch Google Maps"), |
|
144 tr("No GPS support detected, using GPS data from a sample log file instead.")); |
|
145 } else { |
|
146 waitForFix(); |
|
147 location->stopUpdates(); |
|
148 } |
|
149 |
|
150 // Set Internet Access Point |
|
151 QNetworkConfigurationManager manager; |
|
152 const bool canStartIAP = (manager.capabilities() |
|
153 & QNetworkConfigurationManager::CanStartAndStopInterfaces); |
|
154 // Is there default access point, use it |
|
155 QTM_PREPEND_NAMESPACE(QNetworkConfiguration) cfg = manager.defaultConfiguration(); |
|
156 if (!cfg.isValid() || (!canStartIAP && cfg.state() != QTM_PREPEND_NAMESPACE(QNetworkConfiguration)::Active)) { |
|
157 QMessageBox::information(this, tr("Fetch Google Maps"), tr( |
|
158 "Available Access Points not found.")); |
|
159 return; |
|
160 } |
|
161 |
|
162 session = new QTM_PREPEND_NAMESPACE(QNetworkSession)(cfg, this); |
|
163 session->open(); |
|
164 session->waitForOpened(-1); |
|
165 connect(location, SIGNAL(updateTimeout()), this, SLOT(waitForFix())); |
|
166 |
|
167 location->startUpdates(); |
|
168 } |
|
169 |
|
170 // Brings up a satellite strength dialog box until a position fix is received. |
|
171 // This will also start the position updates if they are not already started. |
|
172 void MapWindow::waitForFix() { |
|
173 if (waitingForFix) |
|
174 return; |
|
175 |
|
176 waitingForFix = true; |
|
177 |
|
178 QGeoSatelliteInfoSource *satellite = QGeoSatelliteInfoSource::createDefaultSource(this); |
|
179 |
|
180 if (satellite) { |
|
181 SatelliteDialog *dialog = new SatelliteDialog(this, |
|
182 30, |
|
183 SatelliteDialog::ExitOnFixOrCancel, |
|
184 SatelliteDialog::OrderByPrnNumber, |
|
185 SatelliteDialog::ScaleToMaxPossible); |
|
186 |
|
187 dialog->connectSources(location, satellite); |
|
188 |
|
189 location->startUpdates(); |
|
190 satellite->startUpdates(); |
|
191 |
|
192 dialog->exec(); |
|
193 |
|
194 satellite->stopUpdates(); |
|
195 |
|
196 delete dialog; |
|
197 delete satellite; |
|
198 } |
|
199 |
|
200 waitingForFix = false; |
|
201 } |
|
202 |
|
203 void MapWindow::start() |
|
204 { |
|
205 location->startUpdates(); |
|
206 } |
|
207 |
|
208 void MapWindow::positionUpdated(const QGeoPositionInfo &info) |
|
209 { |
|
210 QString heading = "?"; |
|
211 QString speed = "?"; |
|
212 if (info.hasAttribute(QGeoPositionInfo::Direction)) |
|
213 heading = QString("%1%2").arg(info.attribute(QGeoPositionInfo::Direction)).arg(QChar(0x00b0)); |
|
214 if (info.hasAttribute(QGeoPositionInfo::GroundSpeed)) |
|
215 speed = QString::number(info.attribute(QGeoPositionInfo::GroundSpeed) * 3.6, 'f', 1); |
|
216 posLabel->setText(tr("Position: %1").arg(info.coordinate().toString())); |
|
217 headingAndSpeedLabel->setText(tr("Bearing %1, travelling at %2 km/h").arg(heading).arg(speed)); |
|
218 |
|
219 dateTimeLabel->setText(tr("(Last update: %1)"). |
|
220 arg(info.timestamp().toLocalTime().time().toString())); |
|
221 |
|
222 if (!loading) { |
|
223 // Google Maps does not provide maps larger than 640x480 |
|
224 int width = qMin(webView->width(), 640); |
|
225 int height = qMin(webView->height(), 480); |
|
226 |
|
227 if ((width == 0) || (height == 0)) |
|
228 return; |
|
229 |
|
230 QString url = GMAPS_STATICMAP_URL_TEMPLATE |
|
231 .arg(QString::number(info.coordinate().latitude())) |
|
232 .arg(QString::number(info.coordinate().longitude())) |
|
233 .arg(QString::number(width)) |
|
234 .arg(QString::number(height)); |
|
235 webView->load(url); |
|
236 } |
|
237 } |
|
238 |
|
239 void MapWindow::loadStarted() |
|
240 { |
|
241 loading = true; |
|
242 webView->setUpdatesEnabled(false); |
|
243 } |
|
244 |
|
245 void MapWindow::loadFinished(bool) |
|
246 { |
|
247 loading = false; |
|
248 webView->setUpdatesEnabled(true); |
|
249 } |