|
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 "satellitedialog.h" |
|
42 |
|
43 #include <QtAlgorithms> |
|
44 |
|
45 #include <QApplication> |
|
46 #include <QPushButton> |
|
47 #include <QVBoxLayout> |
|
48 #include <QHBoxLayout> |
|
49 #include <QLabel> |
|
50 #include <QTimer> |
|
51 #include <QPainter> |
|
52 #include <QSize> |
|
53 #include <QRectF> |
|
54 #include <QSizePolicy> |
|
55 #include <QAction> |
|
56 #include <QMenuBar> |
|
57 |
|
58 #include "qgeopositioninfosource.h" |
|
59 #include "qgeosatelliteinfosource.h" |
|
60 #include "qgeopositioninfo.h" |
|
61 #include "qgeosatelliteinfo.h" |
|
62 |
|
63 class SatelliteWidget : public QWidget |
|
64 { |
|
65 public: |
|
66 SatelliteWidget(QWidget *parent, |
|
67 SatelliteDialog::Ordering ordering, |
|
68 SatelliteDialog::StrengthScaling scaling); |
|
69 |
|
70 void clearSatellites(); |
|
71 void satellitesInViewUpdated(const QList<QGeoSatelliteInfo> &list); |
|
72 void satellitesInUseUpdated(const QList<QGeoSatelliteInfo> &list); |
|
73 |
|
74 QSize sizeHint() const; |
|
75 |
|
76 SatelliteDialog::Ordering ordering() const; |
|
77 void setOrdering(SatelliteDialog::Ordering ordering); |
|
78 |
|
79 SatelliteDialog::StrengthScaling strengthScaling() const; |
|
80 void setStrengthScaling(SatelliteDialog::StrengthScaling scaling); |
|
81 |
|
82 protected: |
|
83 void paintEvent(QPaintEvent *event); |
|
84 |
|
85 private: |
|
86 static const int numBars = 32; |
|
87 static const int gapWidth = 1; |
|
88 static const int barWidth = 3; |
|
89 static const int spanWidth = gapWidth + barWidth; |
|
90 static const int borderOffset = 4; |
|
91 static const int legendTextOffset = 5; |
|
92 |
|
93 int textHeight; |
|
94 int legendHeight; |
|
95 |
|
96 QColor inUseColor; |
|
97 QColor inViewColor; |
|
98 |
|
99 SatelliteDialog::Ordering m_ordering; |
|
100 SatelliteDialog::StrengthScaling m_scaling; |
|
101 int m_maximumSignalStrength; |
|
102 |
|
103 void updateSatelliteList(); |
|
104 void paintSatellite(QPainter &painter, const QRect &bounds, int index); |
|
105 void paintLegend(QPainter &painter, const QRect &bounds); |
|
106 |
|
107 QList<QGeoSatelliteInfo> satellitesInView; |
|
108 QList<QGeoSatelliteInfo> satellitesInUse; |
|
109 QList<QPair<QGeoSatelliteInfo, bool> > satellites; |
|
110 }; |
|
111 |
|
112 SatelliteWidget::SatelliteWidget(QWidget *parent, |
|
113 SatelliteDialog::Ordering ordering, |
|
114 SatelliteDialog::StrengthScaling scaling) : QWidget(parent), |
|
115 m_ordering(ordering), |
|
116 m_scaling(scaling) |
|
117 { |
|
118 QPainter painter(this); |
|
119 textHeight = painter.fontMetrics().height(); |
|
120 legendHeight = borderOffset + textHeight + 2; |
|
121 |
|
122 inViewColor = QColor(192, 192, 255); |
|
123 inUseColor = QColor(64, 64, 255); |
|
124 } |
|
125 |
|
126 SatelliteDialog::Ordering SatelliteWidget::ordering() const |
|
127 { |
|
128 return m_ordering; |
|
129 } |
|
130 |
|
131 void SatelliteWidget::setOrdering(SatelliteDialog::Ordering ordering) |
|
132 { |
|
133 if (ordering != m_ordering) { |
|
134 m_ordering = ordering; |
|
135 updateSatelliteList(); |
|
136 } |
|
137 } |
|
138 |
|
139 SatelliteDialog::StrengthScaling SatelliteWidget::strengthScaling() const |
|
140 { |
|
141 return m_scaling; |
|
142 } |
|
143 |
|
144 void SatelliteWidget::setStrengthScaling(SatelliteDialog::StrengthScaling scaling) |
|
145 { |
|
146 if (scaling != m_scaling) { |
|
147 m_scaling = scaling; |
|
148 updateSatelliteList(); |
|
149 } |
|
150 } |
|
151 |
|
152 bool sortByPrn(const QGeoSatelliteInfo &s1, const QGeoSatelliteInfo &s2) |
|
153 { |
|
154 return s1.prnNumber() < s2.prnNumber(); |
|
155 } |
|
156 |
|
157 bool sortBySignalStrength(const QGeoSatelliteInfo &s1, const QGeoSatelliteInfo &s2) |
|
158 { |
|
159 return s1.signalStrength() < s2.signalStrength(); |
|
160 } |
|
161 |
|
162 void SatelliteWidget::clearSatellites() |
|
163 { |
|
164 satellitesInView.clear(); |
|
165 satellitesInUse.clear(); |
|
166 updateSatelliteList(); |
|
167 } |
|
168 |
|
169 void SatelliteWidget::satellitesInViewUpdated(const QList<QGeoSatelliteInfo> &list) |
|
170 { |
|
171 satellitesInView = list; |
|
172 qSort(satellitesInView.begin(), satellitesInView.end(), sortByPrn); |
|
173 updateSatelliteList(); |
|
174 } |
|
175 |
|
176 void SatelliteWidget::satellitesInUseUpdated(const QList<QGeoSatelliteInfo> &list) |
|
177 { |
|
178 satellitesInUse = list; |
|
179 qSort(satellitesInUse.begin(), satellitesInUse.end(), sortByPrn); |
|
180 updateSatelliteList(); |
|
181 } |
|
182 |
|
183 bool sortPairsByPrn(const QPair<QGeoSatelliteInfo, bool> &p1, const QPair<QGeoSatelliteInfo, bool> &p2) |
|
184 { |
|
185 return sortByPrn(p1.first, p2.first); |
|
186 } |
|
187 |
|
188 bool sortPairsBySignalStrength(const QPair<QGeoSatelliteInfo, bool> &p1, const QPair<QGeoSatelliteInfo, bool> &p2) |
|
189 { |
|
190 return sortBySignalStrength(p1.first, p2.first); |
|
191 } |
|
192 |
|
193 void SatelliteWidget::updateSatelliteList() |
|
194 { |
|
195 satellites.clear(); |
|
196 |
|
197 int useSize = satellitesInUse.size(); |
|
198 int viewSize = satellitesInView.size(); |
|
199 |
|
200 if ((useSize == 0) && (viewSize == 0)) { |
|
201 update(); |
|
202 return; |
|
203 } |
|
204 |
|
205 for (int i = 0; i < useSize; ++i) { |
|
206 if (satellitesInUse.at(i).signalStrength() != 0) |
|
207 satellites << QPair<QGeoSatelliteInfo, bool>(satellitesInUse.at(i), true); |
|
208 } |
|
209 |
|
210 QList<QGeoSatelliteInfo>::iterator end = satellitesInUse.end(); |
|
211 |
|
212 for (int i = 0; i < viewSize; ++i) { |
|
213 if (satellitesInView.at(i).signalStrength() == 0) |
|
214 continue; |
|
215 |
|
216 QList<QGeoSatelliteInfo>::iterator j = |
|
217 qBinaryFind( |
|
218 satellitesInUse.begin(), |
|
219 end, |
|
220 satellitesInView.at(i), |
|
221 sortByPrn |
|
222 ); |
|
223 |
|
224 if (j == end) { |
|
225 satellites << QPair<QGeoSatelliteInfo, bool>(satellitesInView.at(i), false); |
|
226 } |
|
227 } |
|
228 |
|
229 int satSize = satellites.size(); |
|
230 |
|
231 if (m_ordering == SatelliteDialog::OrderByPrnNumber) { |
|
232 qSort(satellites.begin(), satellites.end(), sortPairsByPrn); |
|
233 m_maximumSignalStrength = 0; |
|
234 for (int i = 0; i < satSize; ++i) { |
|
235 if (satellites.at(i).first.signalStrength() > m_maximumSignalStrength) |
|
236 m_maximumSignalStrength = satellites.at(i).first.signalStrength(); |
|
237 } |
|
238 |
|
239 } else { |
|
240 qSort(satellites.begin(), satellites.end(), sortPairsBySignalStrength); |
|
241 m_maximumSignalStrength = satellites.at(satSize - 1).first.signalStrength(); |
|
242 } |
|
243 |
|
244 update(); |
|
245 } |
|
246 |
|
247 void SatelliteWidget::paintEvent(QPaintEvent * /*event*/) |
|
248 { |
|
249 QPainter painter(this); |
|
250 painter.setRenderHint(QPainter::Antialiasing); |
|
251 |
|
252 QRect satBounds = QRect(rect().x() + borderOffset, |
|
253 rect().y() + borderOffset, |
|
254 rect().width() - 2 * borderOffset, |
|
255 rect().height() - legendHeight - 2 * borderOffset); |
|
256 |
|
257 |
|
258 painter.setPen(QApplication::palette().color(QPalette::WindowText)); |
|
259 painter.setBrush(QApplication::palette().color(QPalette::Base)); |
|
260 painter.drawRect(satBounds); |
|
261 |
|
262 int size = satellites.size(); |
|
263 for (int i = 0; i < size; ++i) { |
|
264 paintSatellite(painter, satBounds, i); |
|
265 } |
|
266 |
|
267 QRect legendBounds = QRect(rect().x() + borderOffset, |
|
268 rect().height() - legendHeight, |
|
269 rect().width() - 2 * borderOffset, |
|
270 legendHeight); |
|
271 |
|
272 paintLegend(painter, legendBounds); |
|
273 } |
|
274 |
|
275 void SatelliteWidget::paintSatellite(QPainter &painter, const QRect &bounds, int index) |
|
276 { |
|
277 int bars = numBars; |
|
278 if (m_ordering == SatelliteDialog::OrderBySignalStrength) |
|
279 bars = satellites.size(); |
|
280 |
|
281 double pixelsPerUnit = (double) bounds.width() / (double)(bars * spanWidth + gapWidth); |
|
282 double spanPixels = pixelsPerUnit * spanWidth; |
|
283 double gapPixels = pixelsPerUnit * gapWidth; |
|
284 double barPixels = pixelsPerUnit * barWidth; |
|
285 |
|
286 painter.setPen(QApplication::palette().color(QPalette::WindowText)); |
|
287 if (!satellites.at(index).second) { |
|
288 painter.setBrush(inViewColor); |
|
289 } else { |
|
290 painter.setBrush(inUseColor); |
|
291 } |
|
292 |
|
293 int maximum = 100; |
|
294 if (m_scaling == SatelliteDialog::ScaleToMaxAvailable) { |
|
295 maximum = m_maximumSignalStrength; |
|
296 } |
|
297 |
|
298 int i = index; |
|
299 if (m_ordering == SatelliteDialog::OrderByPrnNumber) |
|
300 i = satellites.at(index).first.prnNumber() - 1; |
|
301 |
|
302 double height = ((double) satellites.at(index).first.signalStrength() / (double) maximum) * bounds.height(); |
|
303 |
|
304 QRectF r(bounds.x() + gapPixels + i * spanPixels, bounds.y() + bounds.height() - 1 - height, barPixels, height); |
|
305 |
|
306 painter.drawRect(r); |
|
307 } |
|
308 |
|
309 void SatelliteWidget::paintLegend(QPainter &painter, const QRect &bounds) |
|
310 { |
|
311 double halfWidth = (double) bounds.width() / 2.0; |
|
312 |
|
313 double keyX = bounds.x() + 1; |
|
314 double textX = keyX + legendHeight + 2 + legendTextOffset; |
|
315 double y = bounds.y() + 1; |
|
316 double keyWidth = legendHeight - 2 - borderOffset; |
|
317 double textWidth = halfWidth - legendHeight - 3 - legendTextOffset; |
|
318 double height = legendHeight - 2 - borderOffset; |
|
319 |
|
320 QRectF viewKeyRect(keyX, y, keyWidth, height); |
|
321 QRectF viewTextRect(textX, y, textWidth, height); |
|
322 QRectF useKeyRect(keyX + halfWidth, y, keyWidth, height); |
|
323 QRectF useTextRect(textX + halfWidth, y, textWidth, height); |
|
324 |
|
325 painter.setPen(QApplication::palette().color(QPalette::WindowText)); |
|
326 |
|
327 painter.setBrush(inViewColor); |
|
328 painter.drawRect(viewKeyRect); |
|
329 |
|
330 painter.setBrush(inUseColor); |
|
331 painter.drawRect(useKeyRect); |
|
332 |
|
333 painter.setPen(QApplication::palette().color(QPalette::Text)); |
|
334 painter.drawText(viewTextRect, Qt::AlignLeft, tr("In View")); |
|
335 painter.drawText(useTextRect, Qt::AlignLeft, tr("In Use")); |
|
336 } |
|
337 |
|
338 |
|
339 QSize SatelliteWidget::sizeHint() const |
|
340 { |
|
341 return QSize(parentWidget()->width(), parentWidget()->width() / 2 + legendHeight); |
|
342 } |
|
343 |
|
344 SatelliteDialog::SatelliteDialog(QWidget *parent, |
|
345 int noSatelliteTimeoutSeconds, |
|
346 ExitBehaviour exitBehaviour, |
|
347 Ordering ordering, |
|
348 StrengthScaling scaling) : QDialog(parent), |
|
349 m_noSatelliteTimeoutSeconds(noSatelliteTimeoutSeconds), |
|
350 m_exitBehaviour(exitBehaviour), |
|
351 m_ordering(ordering), |
|
352 m_scaling(scaling) |
|
353 { |
|
354 noSatelliteTimer = new QTimer(this); |
|
355 noSatelliteTimer->setInterval(m_noSatelliteTimeoutSeconds * 1000); |
|
356 noSatelliteTimer->setSingleShot(true); |
|
357 |
|
358 connect(noSatelliteTimer, SIGNAL(timeout()), this, SLOT(noSatelliteTimeout())); |
|
359 |
|
360 satelliteWidget = new SatelliteWidget(this, ordering, scaling); |
|
361 |
|
362 QLabel *titleLabel = new QLabel(tr("Satellite Signal Strength")); |
|
363 titleLabel->setAlignment(Qt::AlignCenter | Qt::AlignBottom); |
|
364 |
|
365 QVBoxLayout *mainLayout = new QVBoxLayout; |
|
366 mainLayout->addWidget(titleLabel); |
|
367 mainLayout->addWidget(satelliteWidget); |
|
368 |
|
369 #if defined(Q_OS_SYMBIAN) || defined(Q_OS_WINCE_WM) || defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6) |
|
370 QAction *switchAction = new QAction(tr("Switch"), this); |
|
371 switchAction->setSoftKeyRole(QAction::PositiveSoftKey); |
|
372 |
|
373 connect(switchAction, SIGNAL(triggered()), this, SLOT(switchButtonClicked())); |
|
374 addAction(switchAction); |
|
375 |
|
376 QAction *cancelAction = new QAction(tr("Cancel"), this); |
|
377 cancelAction->setSoftKeyRole(QAction::NegativeSoftKey); |
|
378 |
|
379 connect(cancelAction, SIGNAL(triggered()), this, SLOT(reject())); |
|
380 addAction(cancelAction); |
|
381 |
|
382 QMenuBar *menuBar = new QMenuBar(this); |
|
383 menuBar->addAction(switchAction); |
|
384 menuBar->addAction(cancelAction); |
|
385 |
|
386 #if defined(Q_OS_WINCE) |
|
387 menuBar->setDefaultAction(cancelAction); |
|
388 #endif |
|
389 |
|
390 #else |
|
391 cancelButton = new QPushButton(tr("Cancel")); |
|
392 connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject())); |
|
393 |
|
394 switchButton = new QPushButton(tr("Switch")); |
|
395 connect(switchButton, SIGNAL(clicked()), this, SLOT(switchButtonClicked())); |
|
396 |
|
397 QHBoxLayout *buttonLayout = new QHBoxLayout; |
|
398 buttonLayout->addWidget(switchButton); |
|
399 buttonLayout->addStretch(1); |
|
400 buttonLayout->addWidget(cancelButton); |
|
401 |
|
402 mainLayout->addLayout(buttonLayout); |
|
403 #endif |
|
404 |
|
405 setLayout(mainLayout); |
|
406 |
|
407 /*#if defined(Q_OS_SYMBIAN) |
|
408 // workaround for QTBUG-4771 |
|
409 oldTitle = windowTitle(); |
|
410 connect(this, SIGNAL(finished(int)), this, SLOT(restoreWindowTitle())); |
|
411 #endif*/ |
|
412 |
|
413 setWindowTitle(tr("Waiting for GPS fix")); |
|
414 |
|
415 setModal(true); |
|
416 } |
|
417 |
|
418 /*#if defined(Q_OS_SYMBIAN) |
|
419 // workaround for QTBUG-4771 |
|
420 void SatelliteDialog::restoreWindowTitle() |
|
421 { |
|
422 setWindowTitle(oldTitle); |
|
423 } |
|
424 #endif*/ |
|
425 |
|
426 void SatelliteDialog::connectSources(QGeoPositionInfoSource *posSource, QGeoSatelliteInfoSource *satSource) |
|
427 { |
|
428 connect(posSource, SIGNAL(positionUpdated(const QGeoPositionInfo &)), |
|
429 this, SLOT(positionUpdated(const QGeoPositionInfo &))); |
|
430 |
|
431 connect(satSource, SIGNAL(satellitesInViewUpdated(const QList<QGeoSatelliteInfo> &)), |
|
432 this, SLOT(satellitesInViewUpdated(const QList<QGeoSatelliteInfo> &))); |
|
433 connect(satSource, SIGNAL(satellitesInUseUpdated(const QList<QGeoSatelliteInfo> &)), |
|
434 this, SLOT(satellitesInUseUpdated(const QList<QGeoSatelliteInfo> &))); |
|
435 } |
|
436 |
|
437 void SatelliteDialog::switchButtonClicked() |
|
438 { |
|
439 SatelliteDialog::Ordering o = ordering(); |
|
440 if (o == SatelliteDialog::OrderByPrnNumber) |
|
441 setOrdering(SatelliteDialog::OrderBySignalStrength); |
|
442 else if (o == SatelliteDialog::OrderBySignalStrength) |
|
443 setOrdering(SatelliteDialog::OrderByPrnNumber); |
|
444 } |
|
445 |
|
446 SatelliteDialog::ExitBehaviour SatelliteDialog::exitBehaviour() const |
|
447 { |
|
448 return m_exitBehaviour; |
|
449 } |
|
450 |
|
451 void SatelliteDialog::setExitBehaviour(SatelliteDialog::ExitBehaviour exitBehaviour) |
|
452 { |
|
453 m_exitBehaviour = exitBehaviour; |
|
454 } |
|
455 |
|
456 SatelliteDialog::Ordering SatelliteDialog::ordering() const |
|
457 { |
|
458 return satelliteWidget->ordering(); |
|
459 } |
|
460 |
|
461 void SatelliteDialog::setOrdering(SatelliteDialog::Ordering ordering) |
|
462 { |
|
463 satelliteWidget->setOrdering(ordering); |
|
464 } |
|
465 |
|
466 SatelliteDialog::StrengthScaling SatelliteDialog::strengthScaling() const |
|
467 { |
|
468 return satelliteWidget->strengthScaling(); |
|
469 } |
|
470 |
|
471 void SatelliteDialog::setStrengthScaling(SatelliteDialog::StrengthScaling scaling) |
|
472 { |
|
473 satelliteWidget->setStrengthScaling(scaling); |
|
474 } |
|
475 |
|
476 void SatelliteDialog::noSatelliteTimeout() |
|
477 { |
|
478 satelliteWidget->clearSatellites(); |
|
479 } |
|
480 |
|
481 void SatelliteDialog::positionUpdated(const QGeoPositionInfo &/*pos*/) |
|
482 { |
|
483 if (m_exitBehaviour == ExitOnFixOrCancel) { |
|
484 accept(); |
|
485 } |
|
486 } |
|
487 |
|
488 void SatelliteDialog::satellitesInViewUpdated(const QList<QGeoSatelliteInfo> &list) |
|
489 { |
|
490 noSatelliteTimer->stop(); |
|
491 satelliteWidget->satellitesInViewUpdated(list); |
|
492 noSatelliteTimer->start(); |
|
493 } |
|
494 |
|
495 void SatelliteDialog::satellitesInUseUpdated(const QList<QGeoSatelliteInfo> &list) |
|
496 { |
|
497 noSatelliteTimer->stop(); |
|
498 satelliteWidget->satellitesInUseUpdated(list); |
|
499 noSatelliteTimer->start(); |
|
500 } |
|
501 |
|
502 |