diff options
author | Qt Continuous Integration System <qt-info@nokia.com> | 2011-04-06 22:41:16 (GMT) |
---|---|---|
committer | Qt Continuous Integration System <qt-info@nokia.com> | 2011-04-06 22:41:16 (GMT) |
commit | f9fce9b432bfcb5c2f48b2b34056a63bd805d8d3 (patch) | |
tree | 01cdd02ff5c5c1b2c7dccaabf7addcb940feb7cd /demos/embedded/lightmaps | |
parent | 3ec5f0c801645cf8beac52f7eab0c97190f9b46d (diff) | |
parent | 4409d1b454f02fa1e5e07560a1db6012074172eb (diff) | |
download | Qt-f9fce9b432bfcb5c2f48b2b34056a63bd805d8d3.zip Qt-f9fce9b432bfcb5c2f48b2b34056a63bd805d8d3.tar.gz Qt-f9fce9b432bfcb5c2f48b2b34056a63bd805d8d3.tar.bz2 |
Merge branch '4.7' of scm.dev.nokia.troll.no:qt/qt-doc-staging into 4.7-integration
* '4.7' of scm.dev.nokia.troll.no:qt/qt-doc-staging:
Fixed the declarative headers and includes within qdoc.
Removed invalid public slots section.
Minor feature fix led to refactoring for i18n's sake.
Fixed string-int concatenation issue.
qdoc: Allowed multiple values for certain metadata tags.
qdoc: modified \include to take a 2nd arg, snippet id.
Doc: Minor fix to title.
Doc: Fixed markup.
Doc: Added a missing command.
Doc: Removed duplicate documentation.
Removed some links and prettified others.
Doc: Squashed commit of Roland Wolf's threading tutorial.
Diffstat (limited to 'demos/embedded/lightmaps')
-rw-r--r-- | demos/embedded/lightmaps/lightmaps.cpp | 693 | ||||
-rw-r--r-- | demos/embedded/lightmaps/lightmaps.h | 88 | ||||
-rw-r--r-- | demos/embedded/lightmaps/lightmaps.pro | 8 | ||||
-rw-r--r-- | demos/embedded/lightmaps/main.cpp | 63 | ||||
-rw-r--r-- | demos/embedded/lightmaps/mapzoom.cpp | 147 | ||||
-rw-r--r-- | demos/embedded/lightmaps/mapzoom.h | 69 | ||||
-rw-r--r-- | demos/embedded/lightmaps/slippymap.cpp | 213 | ||||
-rw-r--r-- | demos/embedded/lightmaps/slippymap.h | 87 |
8 files changed, 859 insertions, 509 deletions
diff --git a/demos/embedded/lightmaps/lightmaps.cpp b/demos/embedded/lightmaps/lightmaps.cpp index dfbef06..8e64e73 100644 --- a/demos/embedded/lightmaps/lightmaps.cpp +++ b/demos/embedded/lightmaps/lightmaps.cpp @@ -45,6 +45,9 @@ #include <math.h> +#include "lightmaps.h" +#include "slippymap.h" + #ifndef M_PI #define M_PI 3.14159265358979323846 #endif @@ -59,552 +62,226 @@ // Hint: see above to find why I picked this one :) #define MAX_MAGNIFIER 229 -uint qHash(const QPoint& p) +LightMaps::LightMaps(QWidget *parent) + : QWidget(parent), pressed(false), snapped(false), zoomed(false), + invert(false) { - return p.x() * 17 ^ p.y(); + m_normalMap = new SlippyMap(this); + m_largeMap = new SlippyMap(this); + connect(m_normalMap, SIGNAL(updated(QRect)), SLOT(updateMap(QRect))); + connect(m_largeMap, SIGNAL(updated(QRect)), SLOT(update())); } -// tile size in pixels -const int tdim = 256; - -QPointF tileForCoordinate(qreal lat, qreal lng, int zoom) +void LightMaps::setCenter(qreal lat, qreal lng) { - qreal zn = static_cast<qreal>(1 << zoom); - qreal tx = (lng + 180.0) / 360.0; - qreal ty = (1.0 - log(tan(lat * M_PI / 180.0) + - 1.0 / cos(lat * M_PI / 180.0)) / M_PI) / 2.0; - return QPointF(tx * zn, ty * zn); + m_normalMap->latitude = lat; + m_normalMap->longitude = lng; + m_normalMap->invalidate(); + m_largeMap->latitude = lat; + m_largeMap->longitude = lng; + m_largeMap->invalidate(); } -qreal longitudeFromTile(qreal tx, int zoom) +void LightMaps::toggleNightMode() { - qreal zn = static_cast<qreal>(1 << zoom); - qreal lat = tx / zn * 360.0 - 180.0; - return lat; + invert = !invert; + update(); } -qreal latitudeFromTile(qreal ty, int zoom) +void LightMaps::updateMap(const QRect &r) { - qreal zn = static_cast<qreal>(1 << zoom); - qreal n = M_PI - 2 * M_PI * ty / zn; - qreal lng = 180.0 / M_PI * atan(0.5 * (exp(n) - exp(-n))); - return lng; + update(r); } -class SlippyMap: public QObject +void LightMaps::activateZoom() { - Q_OBJECT - -public: - int width; - int height; - int zoom; - qreal latitude; - qreal longitude; - - SlippyMap(QObject *parent = 0) - : QObject(parent) - , width(400) - , height(300) - , zoom(15) - , latitude(59.9138204) - , longitude(10.7387413) { - m_emptyTile = QPixmap(tdim, tdim); - m_emptyTile.fill(Qt::lightGray); - - QNetworkDiskCache *cache = new QNetworkDiskCache; - cache->setCacheDirectory(QDesktopServices::storageLocation - (QDesktopServices::CacheLocation)); - m_manager.setCache(cache); - connect(&m_manager, SIGNAL(finished(QNetworkReply*)), - this, SLOT(handleNetworkData(QNetworkReply*))); - } - - void invalidate() { - if (width <= 0 || height <= 0) - return; - - QPointF ct = tileForCoordinate(latitude, longitude, zoom); - qreal tx = ct.x(); - qreal ty = ct.y(); - - // top-left corner of the center tile - int xp = width / 2 - (tx - floor(tx)) * tdim; - int yp = height / 2 - (ty - floor(ty)) * tdim; - - // first tile vertical and horizontal - int xa = (xp + tdim - 1) / tdim; - int ya = (yp + tdim - 1) / tdim; - int xs = static_cast<int>(tx) - xa; - int ys = static_cast<int>(ty) - ya; - - // offset for top-left tile - m_offset = QPoint(xp - xa * tdim, yp - ya * tdim); - - // last tile vertical and horizontal - int xe = static_cast<int>(tx) + (width - xp - 1) / tdim; - int ye = static_cast<int>(ty) + (height - yp - 1) / tdim; - - // build a rect - m_tilesRect = QRect(xs, ys, xe - xs + 1, ye - ys + 1); - - if (m_url.isEmpty()) - download(); - - emit updated(QRect(0, 0, width, height)); - } - - void render(QPainter *p, const QRect &rect) { - for (int x = 0; x <= m_tilesRect.width(); ++x) - for (int y = 0; y <= m_tilesRect.height(); ++y) { - QPoint tp(x + m_tilesRect.left(), y + m_tilesRect.top()); - QRect box = tileRect(tp); - if (rect.intersects(box)) { - if (m_tilePixmaps.contains(tp)) - p->drawPixmap(box, m_tilePixmaps.value(tp)); - else - p->drawPixmap(box, m_emptyTile); - } - } - } - - void pan(const QPoint &delta) { - QPointF dx = QPointF(delta) / qreal(tdim); - QPointF center = tileForCoordinate(latitude, longitude, zoom) - dx; - latitude = latitudeFromTile(center.y(), zoom); - longitude = longitudeFromTile(center.x(), zoom); - invalidate(); - } - -private slots: - - void handleNetworkData(QNetworkReply *reply) { - QImage img; - QPoint tp = reply->request().attribute(QNetworkRequest::User).toPoint(); - QUrl url = reply->url(); - if (!reply->error()) - if (!img.load(reply, 0)) - img = QImage(); - reply->deleteLater(); - m_tilePixmaps[tp] = QPixmap::fromImage(img); - if (img.isNull()) - m_tilePixmaps[tp] = m_emptyTile; - emit updated(tileRect(tp)); - - // purge unused spaces - QRect bound = m_tilesRect.adjusted(-2, -2, 2, 2); - foreach(QPoint tp, m_tilePixmaps.keys()) - if (!bound.contains(tp)) - m_tilePixmaps.remove(tp); - - download(); - } - - void download() { - QPoint grab(0, 0); - for (int x = 0; x <= m_tilesRect.width(); ++x) - for (int y = 0; y <= m_tilesRect.height(); ++y) { - QPoint tp = m_tilesRect.topLeft() + QPoint(x, y); - if (!m_tilePixmaps.contains(tp)) { - grab = tp; - break; - } - } - if (grab == QPoint(0, 0)) { - m_url = QUrl(); - return; - } - - QString path = "http://tile.openstreetmap.org/%1/%2/%3.png"; - m_url = QUrl(path.arg(zoom).arg(grab.x()).arg(grab.y())); - QNetworkRequest request; - request.setUrl(m_url); - request.setRawHeader("User-Agent", "Nokia (Qt) Graphics Dojo 1.0"); - request.setAttribute(QNetworkRequest::User, QVariant(grab)); - m_manager.get(request); - } - -signals: - void updated(const QRect &rect); - -protected: - QRect tileRect(const QPoint &tp) { - QPoint t = tp - m_tilesRect.topLeft(); - int x = t.x() * tdim + m_offset.x(); - int y = t.y() * tdim + m_offset.y(); - return QRect(x, y, tdim, tdim); - } - -private: - QPoint m_offset; - QRect m_tilesRect; - QPixmap m_emptyTile; - QHash<QPoint, QPixmap> m_tilePixmaps; - QNetworkAccessManager m_manager; - QUrl m_url; -}; + zoomed = true; + tapTimer.stop(); + m_largeMap->zoom = m_normalMap->zoom + 1; + m_largeMap->width = m_normalMap->width * 2; + m_largeMap->height = m_normalMap->height * 2; + m_largeMap->latitude = m_normalMap->latitude; + m_largeMap->longitude = m_normalMap->longitude; + m_largeMap->invalidate(); + update(); +} -class LightMaps: public QWidget +void LightMaps::resizeEvent(QResizeEvent *) { - Q_OBJECT - -public: - LightMaps(QWidget *parent = 0) - : QWidget(parent) - , pressed(false) - , snapped(false) - , zoomed(false) - , invert(false) { - m_normalMap = new SlippyMap(this); - m_largeMap = new SlippyMap(this); - connect(m_normalMap, SIGNAL(updated(QRect)), SLOT(updateMap(QRect))); - connect(m_largeMap, SIGNAL(updated(QRect)), SLOT(update())); - } - - void setCenter(qreal lat, qreal lng) { - m_normalMap->latitude = lat; - m_normalMap->longitude = lng; - m_normalMap->invalidate(); - m_largeMap->invalidate(); - } - -public slots: - void toggleNightMode() { - invert = !invert; - update(); - } - -private slots: - void updateMap(const QRect &r) { - update(r); - } - -protected: - - void activateZoom() { - zoomed = true; - tapTimer.stop(); - m_largeMap->zoom = m_normalMap->zoom + 1; - m_largeMap->width = m_normalMap->width * 2; - m_largeMap->height = m_normalMap->height * 2; - m_largeMap->latitude = m_normalMap->latitude; - m_largeMap->longitude = m_normalMap->longitude; - m_largeMap->invalidate(); - update(); - } - - void resizeEvent(QResizeEvent *) { - m_normalMap->width = width(); - m_normalMap->height = height(); - m_normalMap->invalidate(); - m_largeMap->width = m_normalMap->width * 2; - m_largeMap->height = m_normalMap->height * 2; - m_largeMap->invalidate(); - } + m_normalMap->width = width(); + m_normalMap->height = height(); + m_normalMap->invalidate(); + m_largeMap->width = m_normalMap->width * 2; + m_largeMap->height = m_normalMap->height * 2; + m_largeMap->invalidate(); +} - void paintEvent(QPaintEvent *event) { - QPainter p; - p.begin(this); - m_normalMap->render(&p, event->rect()); - p.setPen(Qt::black); +void LightMaps::paintEvent(QPaintEvent *event) +{ + QPainter p; + p.begin(this); + m_normalMap->render(&p, event->rect()); + p.setPen(Qt::black); #if defined(Q_OS_SYMBIAN) - QFont font = p.font(); - font.setPixelSize(13); - p.setFont(font); + QFont font = p.font(); + font.setPixelSize(13); + p.setFont(font); #endif - p.drawText(rect(), Qt::AlignBottom | Qt::TextWordWrap, - "Map data CCBYSA 2009 OpenStreetMap.org contributors"); - p.end(); - - if (zoomed) { - int dim = qMin(width(), height()); - int magnifierSize = qMin(MAX_MAGNIFIER, dim * 2 / 3); - int radius = magnifierSize / 2; - int ring = radius - 15; - QSize box = QSize(magnifierSize, magnifierSize); - - // reupdate our mask - if (maskPixmap.size() != box) { - maskPixmap = QPixmap(box); - maskPixmap.fill(Qt::transparent); - - QRadialGradient g; - g.setCenter(radius, radius); - g.setFocalPoint(radius, radius); - g.setRadius(radius); - g.setColorAt(1.0, QColor(255, 255, 255, 0)); - g.setColorAt(0.5, QColor(128, 128, 128, 255)); - - QPainter mask(&maskPixmap); - mask.setRenderHint(QPainter::Antialiasing); - mask.setCompositionMode(QPainter::CompositionMode_Source); - mask.setBrush(g); - mask.setPen(Qt::NoPen); - mask.drawRect(maskPixmap.rect()); - mask.setBrush(QColor(Qt::transparent)); - mask.drawEllipse(g.center(), ring, ring); - mask.end(); - } - - QPoint center = dragPos - QPoint(0, radius); - center = center + QPoint(0, radius / 2); - QPoint corner = center - QPoint(radius, radius); - - QPoint xy = center * 2 - QPoint(radius, radius); + p.drawText(rect(), Qt::AlignBottom | Qt::TextWordWrap, + "Map data CCBYSA 2009 OpenStreetMap.org contributors"); + p.end(); + + if (zoomed) { + int dim = qMin(width(), height()); + int magnifierSize = qMin(MAX_MAGNIFIER, dim * 2 / 3); + int radius = magnifierSize / 2; + int ring = radius - 15; + QSize box = QSize(magnifierSize, magnifierSize); + + // reupdate our mask + if (maskPixmap.size() != box) { + maskPixmap = QPixmap(box); + maskPixmap.fill(Qt::transparent); + + QRadialGradient g; + g.setCenter(radius, radius); + g.setFocalPoint(radius, radius); + g.setRadius(radius); + g.setColorAt(1.0, QColor(255, 255, 255, 0)); + g.setColorAt(0.5, QColor(128, 128, 128, 255)); + + QPainter mask(&maskPixmap); + mask.setRenderHint(QPainter::Antialiasing); + mask.setCompositionMode(QPainter::CompositionMode_Source); + mask.setBrush(g); + mask.setPen(Qt::NoPen); + mask.drawRect(maskPixmap.rect()); + mask.setBrush(QColor(Qt::transparent)); + mask.drawEllipse(g.center(), ring, ring); + mask.end(); + } - // only set the dimension to the magnified portion - if (zoomPixmap.size() != box) { - zoomPixmap = QPixmap(box); - zoomPixmap.fill(Qt::lightGray); - } - if (true) { - QPainter p(&zoomPixmap); - p.translate(-xy); - m_largeMap->render(&p, QRect(xy, box)); - p.end(); - } + QPoint center = dragPos - QPoint(0, radius); + center = center + QPoint(0, radius / 2); + QPoint corner = center - QPoint(radius, radius); - QPainterPath clipPath; - clipPath.addEllipse(center, ring, ring); + QPoint xy = center * 2 - QPoint(radius, radius); - QPainter p(this); - p.setRenderHint(QPainter::Antialiasing); - p.setClipPath(clipPath); - p.drawPixmap(corner, zoomPixmap); - p.setClipping(false); - p.drawPixmap(corner, maskPixmap); - p.setPen(Qt::gray); - p.drawPath(clipPath); + // only set the dimension to the magnified portion + if (zoomPixmap.size() != box) { + zoomPixmap = QPixmap(box); + zoomPixmap.fill(Qt::lightGray); } - if (invert) { - QPainter p(this); - p.setCompositionMode(QPainter::CompositionMode_Difference); - p.fillRect(event->rect(), Qt::white); + if (true) { + QPainter p(&zoomPixmap); + p.translate(-xy); + m_largeMap->render(&p, QRect(xy, box)); p.end(); } - } - void timerEvent(QTimerEvent *) { - if (!zoomed) - activateZoom(); - update(); + QPainterPath clipPath; + clipPath.addEllipse(center, ring, ring); + + QPainter p(this); + p.setRenderHint(QPainter::Antialiasing); + p.setClipPath(clipPath); + p.drawPixmap(corner, zoomPixmap); + p.setClipping(false); + p.drawPixmap(corner, maskPixmap); + p.setPen(Qt::gray); + p.drawPath(clipPath); } - - void mousePressEvent(QMouseEvent *event) { - if (event->buttons() != Qt::LeftButton) - return; - pressed = snapped = true; - pressPos = dragPos = event->pos(); - tapTimer.stop(); - tapTimer.start(HOLD_TIME, this); + if (invert) { + QPainter p(this); + p.setCompositionMode(QPainter::CompositionMode_Difference); + p.fillRect(event->rect(), Qt::white); + p.end(); } +} - void mouseMoveEvent(QMouseEvent *event) { - if (!event->buttons()) - return; - if (!zoomed) { - if (!pressed || !snapped) { - QPoint delta = event->pos() - pressPos; - pressPos = event->pos(); - m_normalMap->pan(delta); - return; - } else { - const int threshold = 10; - QPoint delta = event->pos() - pressPos; - if (snapped) { - snapped &= delta.x() < threshold; - snapped &= delta.y() < threshold; - snapped &= delta.x() > -threshold; - snapped &= delta.y() > -threshold; - } - if (!snapped) - tapTimer.stop(); - } - } else { - dragPos = event->pos(); - update(); - } - } +void LightMaps::timerEvent(QTimerEvent *) +{ + if (!zoomed) + activateZoom(); + update(); +} - void mouseReleaseEvent(QMouseEvent *) { - zoomed = false; - update(); - } +void LightMaps::mousePressEvent(QMouseEvent *event) +{ + if (event->buttons() != Qt::LeftButton) + return; + pressed = snapped = true; + pressPos = dragPos = event->pos(); + tapTimer.stop(); + tapTimer.start(HOLD_TIME, this); +} - void keyPressEvent(QKeyEvent *event) { - if (!zoomed) { - if (event->key() == Qt::Key_Left) - m_normalMap->pan(QPoint(20, 0)); - if (event->key() == Qt::Key_Right) - m_normalMap->pan(QPoint(-20, 0)); - if (event->key() == Qt::Key_Up) - m_normalMap->pan(QPoint(0, 20)); - if (event->key() == Qt::Key_Down) - m_normalMap->pan(QPoint(0, -20)); - if (event->key() == Qt::Key_Z || event->key() == Qt::Key_Select) { - dragPos = QPoint(width() / 2, height() / 2); - activateZoom(); - } +void LightMaps::mouseMoveEvent(QMouseEvent *event) +{ + if (!event->buttons()) + return; + if (!zoomed) { + if (!pressed || !snapped) { + QPoint delta = event->pos() - pressPos; + pressPos = event->pos(); + m_normalMap->pan(delta); + return; } else { - if (event->key() == Qt::Key_Z || event->key() == Qt::Key_Select) { - zoomed = false; - update(); - } - QPoint delta(0, 0); - if (event->key() == Qt::Key_Left) - delta = QPoint(-15, 0); - if (event->key() == Qt::Key_Right) - delta = QPoint(15, 0); - if (event->key() == Qt::Key_Up) - delta = QPoint(0, -15); - if (event->key() == Qt::Key_Down) - delta = QPoint(0, 15); - if (delta != QPoint(0, 0)) { - dragPos += delta; - update(); + const int threshold = 10; + QPoint delta = event->pos() - pressPos; + if (snapped) { + snapped &= delta.x() < threshold; + snapped &= delta.y() < threshold; + snapped &= delta.x() > -threshold; + snapped &= delta.y() > -threshold; } + if (!snapped) + tapTimer.stop(); } + } else { + dragPos = event->pos(); + update(); } +} -private: - SlippyMap *m_normalMap; - SlippyMap *m_largeMap; - bool pressed; - bool snapped; - QPoint pressPos; - QPoint dragPos; - QBasicTimer tapTimer; - bool zoomed; - QPixmap zoomPixmap; - QPixmap maskPixmap; - bool invert; -}; - -class MapZoom : public QMainWindow +void LightMaps::mouseReleaseEvent(QMouseEvent *) { - Q_OBJECT - -private: - LightMaps *map; - QNetworkSession *networkSession; - -public: - MapZoom(): QMainWindow(0) { - map = new LightMaps(this); - setCentralWidget(map); - map->setFocus(); - - QAction *osloAction = new QAction("&Oslo", this); - QAction *berlinAction = new QAction("&Berlin", this); - QAction *jakartaAction = new QAction("&Jakarta", this); - QAction *nightModeAction = new QAction("Night Mode", this); - nightModeAction->setCheckable(true); - nightModeAction->setChecked(false); - QAction *osmAction = new QAction("About OpenStreetMap", this); - connect(osloAction, SIGNAL(triggered()), SLOT(chooseOslo())); - connect(berlinAction, SIGNAL(triggered()), SLOT(chooseBerlin())); - connect(jakartaAction, SIGNAL(triggered()), SLOT(chooseJakarta())); - connect(nightModeAction, SIGNAL(triggered()), map, SLOT(toggleNightMode())); - connect(osmAction, SIGNAL(triggered()), SLOT(aboutOsm())); - -#if defined(Q_OS_SYMBIAN) || defined(Q_OS_WINCE_WM) - menuBar()->addAction(osloAction); - menuBar()->addAction(berlinAction); - menuBar()->addAction(jakartaAction); - menuBar()->addAction(nightModeAction); - menuBar()->addAction(osmAction); -#else - QMenu *menu = menuBar()->addMenu("&Options"); - menu->addAction(osloAction); - menu->addAction(berlinAction); - menu->addAction(jakartaAction); - menu->addSeparator(); - menu->addAction(nightModeAction); - menu->addAction(osmAction); -#endif - - QNetworkConfigurationManager manager; - if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { - // Get saved network configuration - QSettings settings(QSettings::UserScope, QLatin1String("Trolltech")); - settings.beginGroup(QLatin1String("QtNetwork")); - const QString id = - settings.value(QLatin1String("DefaultNetworkConfiguration")).toString(); - settings.endGroup(); - - // If the saved network configuration is not currently discovered use the system - // default - QNetworkConfiguration config = manager.configurationFromIdentifier(id); - if ((config.state() & QNetworkConfiguration::Discovered) != - QNetworkConfiguration::Discovered) { - config = manager.defaultConfiguration(); - } - - networkSession = new QNetworkSession(config, this); - connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened())); + zoomed = false; + update(); +} - networkSession->open(); - } else { - networkSession = 0; +void LightMaps::keyPressEvent(QKeyEvent *event) +{ + if (!zoomed) { + if (event->key() == Qt::Key_Left) + m_normalMap->pan(QPoint(20, 0)); + if (event->key() == Qt::Key_Right) + m_normalMap->pan(QPoint(-20, 0)); + if (event->key() == Qt::Key_Up) + m_normalMap->pan(QPoint(0, 20)); + if (event->key() == Qt::Key_Down) + m_normalMap->pan(QPoint(0, -20)); + if (event->key() == Qt::Key_Z || event->key() == Qt::Key_Select) { + dragPos = QPoint(width() / 2, height() / 2); + activateZoom(); } - } - -private slots: - - void sessionOpened() { - // Save the used configuration - QNetworkConfiguration config = networkSession->configuration(); - QString id; - if (config.type() == QNetworkConfiguration::UserChoice) { - id = networkSession->sessionProperty( - QLatin1String("UserChoiceConfiguration")).toString(); - } else { - id = config.identifier(); + } else { + if (event->key() == Qt::Key_Z || event->key() == Qt::Key_Select) { + zoomed = false; + update(); + } + QPoint delta(0, 0); + if (event->key() == Qt::Key_Left) + delta = QPoint(-15, 0); + if (event->key() == Qt::Key_Right) + delta = QPoint(15, 0); + if (event->key() == Qt::Key_Up) + delta = QPoint(0, -15); + if (event->key() == Qt::Key_Down) + delta = QPoint(0, 15); + if (delta != QPoint(0, 0)) { + dragPos += delta; + update(); } - - QSettings settings(QSettings::UserScope, QLatin1String("Trolltech")); - settings.beginGroup(QLatin1String("QtNetwork")); - settings.setValue(QLatin1String("DefaultNetworkConfiguration"), id); - settings.endGroup(); - } - - void chooseOslo() { - map->setCenter(59.9138204, 10.7387413); - } - - void chooseBerlin() { - map->setCenter(52.52958999943302, 13.383053541183472); - } - - void chooseJakarta() { - map->setCenter(-6.211544, 106.845172); - } - - void aboutOsm() { - QDesktopServices::openUrl(QUrl("http://www.openstreetmap.org")); } -}; - - -#include "lightmaps.moc" - -int main(int argc, char **argv) -{ -#if defined(Q_WS_X11) - QApplication::setGraphicsSystem("raster"); -#endif - - QApplication app(argc, argv); - app.setApplicationName("LightMaps"); - - MapZoom w; - w.setWindowTitle("OpenStreetMap"); -#if defined(Q_OS_SYMBIAN) || defined(Q_OS_WINCE_WM) - w.showMaximized(); -#else - w.resize(600, 450); - w.show(); -#endif - - return app.exec(); } diff --git a/demos/embedded/lightmaps/lightmaps.h b/demos/embedded/lightmaps/lightmaps.h new file mode 100644 index 0000000..42a80c9 --- /dev/null +++ b/demos/embedded/lightmaps/lightmaps.h @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef LIGHTMAPS_H +#define LIGHTMAPS_H + +#include <QBasicTimer> +#include <QWidget> + +class SlippyMap; + +class LightMaps: public QWidget +{ + Q_OBJECT + +public: + LightMaps(QWidget *parent = 0); + void setCenter(qreal lat, qreal lng); + +public slots: + void toggleNightMode(); + +protected: + void activateZoom(); + void resizeEvent(QResizeEvent *); + void paintEvent(QPaintEvent *event); + void timerEvent(QTimerEvent *); + void mousePressEvent(QMouseEvent *event); + void mouseMoveEvent(QMouseEvent *event); + void mouseReleaseEvent(QMouseEvent *); + void keyPressEvent(QKeyEvent *event); + +private slots: + void updateMap(const QRect &r); + +private: + SlippyMap *m_normalMap; + SlippyMap *m_largeMap; + bool pressed; + bool snapped; + QPoint pressPos; + QPoint dragPos; + QBasicTimer tapTimer; + bool zoomed; + QPixmap zoomPixmap; + QPixmap maskPixmap; + bool invert; +}; + +#endif
\ No newline at end of file diff --git a/demos/embedded/lightmaps/lightmaps.pro b/demos/embedded/lightmaps/lightmaps.pro index 9d83721..cf1180b 100644 --- a/demos/embedded/lightmaps/lightmaps.pro +++ b/demos/embedded/lightmaps/lightmaps.pro @@ -1,5 +1,11 @@ TEMPLATE = app -SOURCES = lightmaps.cpp +HEADERS = lightmaps.h \ + mapzoom.h \ + slippymap.h +SOURCES = lightmaps.cpp \ + main.cpp \ + mapzoom.cpp \ + slippymap.cpp QT += network symbian { diff --git a/demos/embedded/lightmaps/main.cpp b/demos/embedded/lightmaps/main.cpp new file mode 100644 index 0000000..34d8d3d --- /dev/null +++ b/demos/embedded/lightmaps/main.cpp @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> +#include "mapzoom.h" + +int main(int argc, char **argv) +{ +#if defined(Q_WS_X11) + QApplication::setGraphicsSystem("raster"); +#endif + + QApplication app(argc, argv); + app.setApplicationName("LightMaps"); + + MapZoom w; +#if defined(Q_OS_SYMBIAN) || defined(Q_OS_WINCE_WM) + w.showMaximized(); +#else + w.resize(600, 450); + w.show(); +#endif + + return app.exec(); +} diff --git a/demos/embedded/lightmaps/mapzoom.cpp b/demos/embedded/lightmaps/mapzoom.cpp new file mode 100644 index 0000000..6cafc59 --- /dev/null +++ b/demos/embedded/lightmaps/mapzoom.cpp @@ -0,0 +1,147 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> +#include <QtNetwork> +#include "lightmaps.h" +#include "mapzoom.h" + +MapZoom::MapZoom() + : QMainWindow(0) +{ + map = new LightMaps(this); + setCentralWidget(map); + map->setFocus(); + + QAction *osloAction = new QAction(tr("&Oslo"), this); + QAction *berlinAction = new QAction(tr("&Berlin"), this); + QAction *jakartaAction = new QAction(tr("&Jakarta"), this); + QAction *nightModeAction = new QAction(tr("Night Mode"), this); + nightModeAction->setCheckable(true); + nightModeAction->setChecked(false); + QAction *osmAction = new QAction(tr("About OpenStreetMap"), this); + connect(osloAction, SIGNAL(triggered()), SLOT(chooseOslo())); + connect(berlinAction, SIGNAL(triggered()), SLOT(chooseBerlin())); + connect(jakartaAction, SIGNAL(triggered()), SLOT(chooseJakarta())); + connect(nightModeAction, SIGNAL(triggered()), map, SLOT(toggleNightMode())); + connect(osmAction, SIGNAL(triggered()), SLOT(aboutOsm())); + +#if defined(Q_OS_SYMBIAN) || defined(Q_OS_WINCE_WM) + menuBar()->addAction(osloAction); + menuBar()->addAction(berlinAction); + menuBar()->addAction(jakartaAction); + menuBar()->addAction(nightModeAction); + menuBar()->addAction(osmAction); +#else + QMenu *menu = menuBar()->addMenu(tr("&Options")); + menu->addAction(osloAction); + menu->addAction(berlinAction); + menu->addAction(jakartaAction); + menu->addSeparator(); + menu->addAction(nightModeAction); + menu->addAction(osmAction); +#endif + + QNetworkConfigurationManager manager; + if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { + // Get saved network configuration + QSettings settings(QSettings::UserScope, QLatin1String("Trolltech")); + settings.beginGroup(QLatin1String("QtNetwork")); + const QString id = + settings.value(QLatin1String("DefaultNetworkConfiguration")).toString(); + settings.endGroup(); + + // If the saved network configuration is not currently discovered use the system + // default + QNetworkConfiguration config = manager.configurationFromIdentifier(id); + if ((config.state() & QNetworkConfiguration::Discovered) != + QNetworkConfiguration::Discovered) { + config = manager.defaultConfiguration(); + } + + networkSession = new QNetworkSession(config, this); + connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened())); + + networkSession->open(); + } else { + networkSession = 0; + } + + setWindowTitle(tr("Light Maps")); +} + +void MapZoom::sessionOpened() +{ + // Save the used configuration + QNetworkConfiguration config = networkSession->configuration(); + QString id; + if (config.type() == QNetworkConfiguration::UserChoice) { + id = networkSession->sessionProperty( + QLatin1String("UserChoiceConfiguration")).toString(); + } else { + id = config.identifier(); + } + + QSettings settings(QSettings::UserScope, QLatin1String("Trolltech")); + settings.beginGroup(QLatin1String("QtNetwork")); + settings.setValue(QLatin1String("DefaultNetworkConfiguration"), id); + settings.endGroup(); +} + +void MapZoom::chooseOslo() +{ + map->setCenter(59.9138204, 10.7387413); +} + +void MapZoom::chooseBerlin() +{ + map->setCenter(52.52958999943302, 13.383053541183472); +} + +void MapZoom::chooseJakarta() +{ + map->setCenter(-6.211544, 106.845172); +} + +void MapZoom::aboutOsm() +{ + QDesktopServices::openUrl(QUrl("http://www.openstreetmap.org")); +} diff --git a/demos/embedded/lightmaps/mapzoom.h b/demos/embedded/lightmaps/mapzoom.h new file mode 100644 index 0000000..0b1ea8b --- /dev/null +++ b/demos/embedded/lightmaps/mapzoom.h @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAPZOOM_H +#define MAPZOOM_H + +#include <QMainWindow> + +class QNetworkSession; +class LightMaps; + +class MapZoom : public QMainWindow +{ + Q_OBJECT + +public: + MapZoom(); + +private slots: + void sessionOpened(); + void chooseOslo(); + void chooseBerlin(); + void chooseJakarta(); + void aboutOsm(); + +private: + LightMaps *map; + QNetworkSession *networkSession; +}; + +#endif
\ No newline at end of file diff --git a/demos/embedded/lightmaps/slippymap.cpp b/demos/embedded/lightmaps/slippymap.cpp new file mode 100644 index 0000000..2c76f74 --- /dev/null +++ b/demos/embedded/lightmaps/slippymap.cpp @@ -0,0 +1,213 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <math.h> + +#include <QtGui> +#include <QtNetwork> +#include "slippymap.h" + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +uint qHash(const QPoint& p) +{ + return p.x() * 17 ^ p.y(); +} + +// tile size in pixels +const int tdim = 256; + +QPointF tileForCoordinate(qreal lat, qreal lng, int zoom) +{ + qreal zn = static_cast<qreal>(1 << zoom); + qreal tx = (lng + 180.0) / 360.0; + qreal ty = (1.0 - log(tan(lat * M_PI / 180.0) + + 1.0 / cos(lat * M_PI / 180.0)) / M_PI) / 2.0; + return QPointF(tx * zn, ty * zn); +} + +qreal longitudeFromTile(qreal tx, int zoom) +{ + qreal zn = static_cast<qreal>(1 << zoom); + qreal lat = tx / zn * 360.0 - 180.0; + return lat; +} + +qreal latitudeFromTile(qreal ty, int zoom) +{ + qreal zn = static_cast<qreal>(1 << zoom); + qreal n = M_PI - 2 * M_PI * ty / zn; + qreal lng = 180.0 / M_PI * atan(0.5 * (exp(n) - exp(-n))); + return lng; +} + + +SlippyMap::SlippyMap(QObject *parent) + : QObject(parent), width(400), height(300), zoom(15), + latitude(59.9138204), longitude(10.7387413) +{ + m_emptyTile = QPixmap(tdim, tdim); + m_emptyTile.fill(Qt::lightGray); + + QNetworkDiskCache *cache = new QNetworkDiskCache; + cache->setCacheDirectory(QDesktopServices::storageLocation + (QDesktopServices::CacheLocation)); + m_manager.setCache(cache); + connect(&m_manager, SIGNAL(finished(QNetworkReply*)), + this, SLOT(handleNetworkData(QNetworkReply*))); +} + +void SlippyMap::invalidate() +{ + if (width <= 0 || height <= 0) + return; + + QPointF ct = tileForCoordinate(latitude, longitude, zoom); + qreal tx = ct.x(); + qreal ty = ct.y(); + + // top-left corner of the center tile + int xp = width / 2 - (tx - floor(tx)) * tdim; + int yp = height / 2 - (ty - floor(ty)) * tdim; + + // first tile vertical and horizontal + int xa = (xp + tdim - 1) / tdim; + int ya = (yp + tdim - 1) / tdim; + int xs = static_cast<int>(tx) - xa; + int ys = static_cast<int>(ty) - ya; + + // offset for top-left tile + m_offset = QPoint(xp - xa * tdim, yp - ya * tdim); + + // last tile vertical and horizontal + int xe = static_cast<int>(tx) + (width - xp - 1) / tdim; + int ye = static_cast<int>(ty) + (height - yp - 1) / tdim; + + // build a rect + m_tilesRect = QRect(xs, ys, xe - xs + 1, ye - ys + 1); + + if (m_url.isEmpty()) + download(); + + emit updated(QRect(0, 0, width, height)); +} + +void SlippyMap::render(QPainter *p, const QRect &rect) +{ + for (int x = 0; x <= m_tilesRect.width(); ++x) + for (int y = 0; y <= m_tilesRect.height(); ++y) { + QPoint tp(x + m_tilesRect.left(), y + m_tilesRect.top()); + QRect box = tileRect(tp); + if (rect.intersects(box)) { + if (m_tilePixmaps.contains(tp)) + p->drawPixmap(box, m_tilePixmaps.value(tp)); + else + p->drawPixmap(box, m_emptyTile); + } + } +} + +void SlippyMap::pan(const QPoint &delta) +{ + QPointF dx = QPointF(delta) / qreal(tdim); + QPointF center = tileForCoordinate(latitude, longitude, zoom) - dx; + latitude = latitudeFromTile(center.y(), zoom); + longitude = longitudeFromTile(center.x(), zoom); + invalidate(); +} + +void SlippyMap::handleNetworkData(QNetworkReply *reply) +{ + QImage img; + QPoint tp = reply->request().attribute(QNetworkRequest::User).toPoint(); + QUrl url = reply->url(); + if (!reply->error()) + if (!img.load(reply, 0)) + img = QImage(); + reply->deleteLater(); + m_tilePixmaps[tp] = QPixmap::fromImage(img); + if (img.isNull()) + m_tilePixmaps[tp] = m_emptyTile; + emit updated(tileRect(tp)); + + // purge unused spaces + QRect bound = m_tilesRect.adjusted(-2, -2, 2, 2); + foreach(QPoint tp, m_tilePixmaps.keys()) + if (!bound.contains(tp)) + m_tilePixmaps.remove(tp); + + download(); +} + +void SlippyMap::download() +{ + QPoint grab(0, 0); + for (int x = 0; x <= m_tilesRect.width(); ++x) + for (int y = 0; y <= m_tilesRect.height(); ++y) { + QPoint tp = m_tilesRect.topLeft() + QPoint(x, y); + if (!m_tilePixmaps.contains(tp)) { + grab = tp; + break; + } + } + if (grab == QPoint(0, 0)) { + m_url = QUrl(); + return; + } + + QString path = "http://tile.openstreetmap.org/%1/%2/%3.png"; + m_url = QUrl(path.arg(zoom).arg(grab.x()).arg(grab.y())); + QNetworkRequest request; + request.setUrl(m_url); + request.setRawHeader("User-Agent", "Nokia (Qt) Graphics Dojo 1.0"); + request.setAttribute(QNetworkRequest::User, QVariant(grab)); + m_manager.get(request); +} + +QRect SlippyMap::tileRect(const QPoint &tp) +{ + QPoint t = tp - m_tilesRect.topLeft(); + int x = t.x() * tdim + m_offset.x(); + int y = t.y() * tdim + m_offset.y(); + return QRect(x, y, tdim, tdim); +} diff --git a/demos/embedded/lightmaps/slippymap.h b/demos/embedded/lightmaps/slippymap.h new file mode 100644 index 0000000..ba43e40 --- /dev/null +++ b/demos/embedded/lightmaps/slippymap.h @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SLIPPYMAP_H +#define SLIPPYMAP_H + +#include <QNetworkAccessManager> +#include <QPixmap> +#include <QUrl> + +class QNetworkReply; +class QPainter; + +class SlippyMap: public QObject +{ + Q_OBJECT + +public: + SlippyMap(QObject *parent = 0); + void invalidate(); + void render(QPainter *p, const QRect &rect); + void pan(const QPoint &delta); + + int width; + int height; + int zoom; + qreal latitude; + qreal longitude; + +signals: + void updated(const QRect &rect); + +private slots: + void handleNetworkData(QNetworkReply *reply); + void download(); + +protected: + QRect tileRect(const QPoint &tp); + +private: + QPoint m_offset; + QRect m_tilesRect; + QPixmap m_emptyTile; + QHash<QPoint, QPixmap> m_tilePixmaps; + QNetworkAccessManager m_manager; + QUrl m_url; +}; + +#endif
\ No newline at end of file |