Sophie

Sophie

distrib > Mandriva > current > i586 > media > main-updates > by-pkgid > 8e6051afcdb111a0317a58fb64c2abf5 > files > 321

qt4-doc-4.6.3-0.2mdv2010.2.i586.rpm

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Qt 4.6: lightmaps.cpp Example File (demos/embedded/lightmaps/lightmaps.cpp)</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://qt.nokia.com/"><img src="images/qt-logo.png" align="left" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">All&nbsp;Functions</font></a>&nbsp;&middot; <a href="overviews.html"><font color="#004faf">Overviews</font></a></td></tr></table><h1 class="title">lightmaps.cpp Example File<br /><span class="small-subtitle">demos/embedded/lightmaps/lightmaps.cpp</span>
</h1>
<pre><span class="comment"> /****************************************************************************
 **
 ** Copyright (C) 2010 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$
 ** Commercial Usage
 ** Licensees holding valid Qt Commercial licenses may use this file in
 ** accordance with the Qt Commercial License Agreement provided with the
 ** Software or, alternatively, in accordance with the terms contained in
 ** a written agreement between you and Nokia.
 **
 ** 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.
 **
 ** GNU General Public License Usage
 ** Alternatively, this file may be used under the terms of the GNU
 ** General Public License version 3.0 as published by the Free Software
 ** Foundation and appearing in the file LICENSE.GPL included in the
 ** packaging of this file.  Please review the following information to
 ** ensure the GNU General Public License version 3.0 requirements will be
 ** met: http://www.gnu.org/copyleft/gpl.html.
 **
 ** If you have questions regarding the use of this file, please contact
 ** Nokia at qt-info@nokia.com.
 ** $QT_END_LICENSE$
 **
 ****************************************************************************/</span>

 #include &lt;QtCore&gt;
 #include &lt;QtGui&gt;
 #include &lt;QtNetwork&gt;

 #if defined (Q_OS_SYMBIAN)
 #include &quot;sym_iap_util.h&quot;
 #endif

 #include &lt;math.h&gt;

 #ifndef M_PI
 #define M_PI 3.14159265358979323846
 #endif

<span class="comment"> // how long (milliseconds) the user need to hold (after a tap on the screen)</span>
<span class="comment"> // before triggering the magnifying glass feature</span>
<span class="comment"> // 701, a prime number, is the sum of 229, 233, 239</span>
<span class="comment"> // (all three are also prime numbers, consecutive</span>!)
 #define HOLD_TIME 701

<span class="comment"> // maximum size of the magnifier</span>
<span class="comment"> // Hint: see above to find why I picked this one :)</span>
 #define MAX_MAGNIFIER 229

 uint qHash(const QPoint&amp; p)
 {
     return p.x() * 17 ^ p.y();
 }

<span class="comment"> // tile size in pixels</span>
 const int tdim = 256;

 QPointF tileForCoordinate(qreal lat, qreal lng, int zoom)
 {
     qreal zn = static_cast&lt;qreal&gt;(1 &lt;&lt; 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&lt;qreal&gt;(1 &lt;&lt; zoom);
     qreal lat = tx / zn * 360.0 - 180.0;
     return lat;
 }

 qreal latitudeFromTile(qreal ty, int zoom)
 {
     qreal zn = static_cast&lt;qreal&gt;(1 &lt;&lt; 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;
 }

 class SlippyMap: public QObject
 {
     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-&gt;setCacheDirectory(QDesktopServices::storageLocation
                                  (QDesktopServices::CacheLocation));
         m_manager.setCache(cache);
         connect(&amp;m_manager, SIGNAL(finished(QNetworkReply*)),
                 this, SLOT(handleNetworkData(QNetworkReply*)));
     }

     void invalidate() {
         if (width &lt;= 0 || height &lt;= 0)
             return;

         QPointF ct = tileForCoordinate(latitude, longitude, zoom);
         qreal tx = ct.x();
         qreal ty = ct.y();

         <span class="comment">// top-left corner of the center tile</span>
         int xp = width / 2 - (tx - floor(tx)) * tdim;
         int yp = height / 2 - (ty - floor(ty)) * tdim;

         <span class="comment">// first tile vertical and horizontal</span>
         int xa = (xp + tdim - 1) / tdim;
         int ya = (yp + tdim - 1) / tdim;
         int xs = static_cast&lt;int&gt;(tx) - xa;
         int ys = static_cast&lt;int&gt;(ty) - ya;

         <span class="comment">// offset for top-left tile</span>
         m_offset = QPoint(xp - xa * tdim, yp - ya * tdim);

         <span class="comment">// last tile vertical and horizontal</span>
         int xe = static_cast&lt;int&gt;(tx) + (width - xp - 1) / tdim;
         int ye = static_cast&lt;int&gt;(ty) + (height - yp - 1) / tdim;

         <span class="comment">// build a rect</span>
         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 &amp;rect) {
         for (int x = 0; x &lt;= m_tilesRect.width(); ++x)
             for (int y = 0; y &lt;= 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-&gt;drawPixmap(box, m_tilePixmaps.value(tp));
                     else
                         p-&gt;drawPixmap(box, m_emptyTile);
                 }
             }
     }

     void pan(const QPoint &amp;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-&gt;request().attribute(QNetworkRequest::User).toPoint();
         QUrl url = reply-&gt;url();
         if (!reply-&gt;error())
             if (!img.load(reply, 0))
                 img = QImage();
         reply-&gt;deleteLater();
         m_tilePixmaps[tp] = QPixmap::fromImage(img);
         if (img.isNull())
             m_tilePixmaps[tp] = m_emptyTile;
         emit updated(tileRect(tp));

         <span class="comment">// purge unused spaces</span>
         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 &lt;= m_tilesRect.width(); ++x)
             for (int y = 0; y &lt;= 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 = &quot;http:<span class="comment">//tile.openstreetmap.org/%1/%2/%3.png&quot;;</span>
         m_url = QUrl(path.arg(zoom).arg(grab.x()).arg(grab.y()));
         QNetworkRequest request;
         request.setUrl(m_url);
         request.setRawHeader(&quot;User-Agent&quot;, &quot;Nokia (Qt) Graphics Dojo 1.0&quot;);
         request.setAttribute(QNetworkRequest::User, QVariant(grab));
         m_manager.get(request);
     }

 signals:
     void updated(const QRect &amp;rect);

 protected:
     QRect tileRect(const QPoint &amp;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&lt;QPoint, QPixmap&gt; m_tilePixmaps;
     QNetworkAccessManager m_manager;
     QUrl m_url;
 };

 class LightMaps: public QWidget
 {
     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-&gt;latitude = lat;
         m_normalMap-&gt;longitude = lng;
         m_normalMap-&gt;invalidate();
         m_largeMap-&gt;invalidate();
     }

 public slots:
     void toggleNightMode() {
         invert = !invert;
         update();
     }

 private slots:
     void updateMap(const QRect &amp;r) {
         update(r);
     }

 protected:

     void activateZoom() {
         zoomed = true;
         tapTimer.stop();
         m_largeMap-&gt;zoom = m_normalMap-&gt;zoom + 1;
         m_largeMap-&gt;width = m_normalMap-&gt;width * 2;
         m_largeMap-&gt;height = m_normalMap-&gt;height * 2;
         m_largeMap-&gt;latitude = m_normalMap-&gt;latitude;
         m_largeMap-&gt;longitude = m_normalMap-&gt;longitude;
         m_largeMap-&gt;invalidate();
         update();
     }

     void resizeEvent(QResizeEvent *) {
         m_normalMap-&gt;width = width();
         m_normalMap-&gt;height = height();
         m_normalMap-&gt;invalidate();
         m_largeMap-&gt;width = m_normalMap-&gt;width * 2;
         m_largeMap-&gt;height = m_normalMap-&gt;height * 2;
         m_largeMap-&gt;invalidate();
     }

     void paintEvent(QPaintEvent *event) {
         QPainter p;
         p.begin(this);
         m_normalMap-&gt;render(&amp;p, event-&gt;rect());
         p.setPen(Qt::black);
 #if defined(Q_OS_SYMBIAN)
         QFont font = p.font();
         font.setPixelSize(13);
         p.setFont(font);
 #endif
         p.drawText(rect(),  Qt::AlignBottom | Qt::TextWordWrap,
                    &quot;Map data CCBYSA 2009 OpenStreetMap.org contributors&quot;);
         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);

             <span class="comment">// reupdate our mask</span>
             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(&amp;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);

             <span class="comment">// only set the dimension to the magnified portion</span>
             if (zoomPixmap.size() != box) {
                 zoomPixmap = QPixmap(box);
                 zoomPixmap.fill(Qt::lightGray);
             }
             if (true) {
                 QPainter p(&amp;zoomPixmap);
                 p.translate(-xy);
                 m_largeMap-&gt;render(&amp;p, QRect(xy, box));
                 p.end();
             }

             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);
         }
         if (invert) {
             QPainter p(this);
             p.setCompositionMode(QPainter::CompositionMode_Difference);
             p.fillRect(event-&gt;rect(), Qt::white);
             p.end();
         }
     }

     void timerEvent(QTimerEvent *) {
         if (!zoomed)
             activateZoom();
         update();
     }

     void mousePressEvent(QMouseEvent *event) {
         if (event-&gt;buttons() != Qt::LeftButton)
             return;
         pressed = snapped = true;
         pressPos = dragPos = event-&gt;pos();
         tapTimer.stop();
         tapTimer.start(HOLD_TIME, this);
     }

     void mouseMoveEvent(QMouseEvent *event) {
         if (!event-&gt;buttons())
             return;
         if (!zoomed) {
             if (!pressed || !snapped) {
                 QPoint delta = event-&gt;pos() - pressPos;
                 pressPos = event-&gt;pos();
                 m_normalMap-&gt;pan(delta);
                 return;
             } else {
                 const int threshold = 10;
                 QPoint delta = event-&gt;pos() - pressPos;
                 if (snapped) {
                     snapped &amp;= delta.x() &lt; threshold;
                     snapped &amp;= delta.y() &lt; threshold;
                     snapped &amp;= delta.x() &gt; -threshold;
                     snapped &amp;= delta.y() &gt; -threshold;
                 }
                 if (!snapped)
                     tapTimer.stop();
             }
         } else {
             dragPos = event-&gt;pos();
             update();
         }
     }

     void mouseReleaseEvent(QMouseEvent *) {
         zoomed = false;
         update();
     }

     void keyPressEvent(QKeyEvent *event) {
         if (!zoomed) {
             if (event-&gt;key() == Qt::Key_Left)
                 m_normalMap-&gt;pan(QPoint(20, 0));
             if (event-&gt;key() == Qt::Key_Right)
                 m_normalMap-&gt;pan(QPoint(-20, 0));
             if (event-&gt;key() == Qt::Key_Up)
                 m_normalMap-&gt;pan(QPoint(0, 20));
             if (event-&gt;key() == Qt::Key_Down)
                 m_normalMap-&gt;pan(QPoint(0, -20));
             if (event-&gt;key() == Qt::Key_Z || event-&gt;key() == Qt::Key_Select) {
                 dragPos = QPoint(width() / 2, height() / 2);
                 activateZoom();
             }
         } else {
             if (event-&gt;key() == Qt::Key_Z || event-&gt;key() == Qt::Key_Select) {
                 zoomed = false;
                 update();
             }
             QPoint delta(0, 0);
             if (event-&gt;key() == Qt::Key_Left)
                 delta = QPoint(-15, 0);
             if (event-&gt;key() == Qt::Key_Right)
                 delta = QPoint(15, 0);
             if (event-&gt;key() == Qt::Key_Up)
                 delta = QPoint(0, -15);
             if (event-&gt;key() == Qt::Key_Down)
                 delta = QPoint(0, 15);
             if (delta != QPoint(0, 0)) {
                 dragPos += delta;
                 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
 {
     Q_OBJECT

 private:
     LightMaps *map;

 public:
     MapZoom(): QMainWindow(0) {
         map = new LightMaps(this);
         setCentralWidget(map);
         map-&gt;setFocus();

         QAction *osloAction = new QAction(&quot;&amp;Oslo&quot;, this);
         QAction *berlinAction = new QAction(&quot;&amp;Berlin&quot;, this);
         QAction *jakartaAction = new QAction(&quot;&amp;Jakarta&quot;, this);
         QAction *nightModeAction = new QAction(&quot;Night Mode&quot;, this);
         nightModeAction-&gt;setCheckable(true);
         nightModeAction-&gt;setChecked(false);
         QAction *osmAction = new QAction(&quot;About OpenStreetMap&quot;, 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()-&gt;addAction(osloAction);
         menuBar()-&gt;addAction(berlinAction);
         menuBar()-&gt;addAction(jakartaAction);
         menuBar()-&gt;addAction(nightModeAction);
         menuBar()-&gt;addAction(osmAction);
 #else
         QMenu *menu = menuBar()-&gt;addMenu(&quot;&amp;Options&quot;);
         menu-&gt;addAction(osloAction);
         menu-&gt;addAction(berlinAction);
         menu-&gt;addAction(jakartaAction);
         menu-&gt;addSeparator();
         menu-&gt;addAction(nightModeAction);
         menu-&gt;addAction(osmAction);
 #endif

         QTimer::singleShot(0, this, SLOT(delayedInit()));
     }

 private slots:

     void delayedInit() {
 #if defined(Q_OS_SYMBIAN)
         qt_SetDefaultIap();
 #endif
     }

     void chooseOslo() {
         map-&gt;setCenter(59.9138204, 10.7387413);
     }

     void chooseBerlin() {
         map-&gt;setCenter(52.52958999943302, 13.383053541183472);
     }

     void chooseJakarta() {
         map-&gt;setCenter(-6.211544, 106.845172);
     }

     void aboutOsm() {
         QDesktopServices::openUrl(QUrl(&quot;http:<span class="comment">//www.openstreetmap.org&quot;));</span>
     }
 };

 #include &quot;lightmaps.moc&quot;

 int main(int argc, char **argv)
 {
 #if defined(Q_WS_X11)
     QApplication::setGraphicsSystem(&quot;raster&quot;);
 #endif

     QApplication app(argc, argv);
     app.setApplicationName(&quot;LightMaps&quot;);

     MapZoom w;
     w.setWindowTitle(&quot;OpenStreetMap&quot;);
 #if defined(Q_OS_SYMBIAN) || defined(Q_OS_WINCE_WM)
     w.showMaximized();
 #else
     w.resize(600, 450);
     w.show();
 #endif

     return app.exec();
 }</pre>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="40%" align="left">Copyright &copy; 2010 Nokia Corporation and/or its subsidiary(-ies)</td>
<td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="40%" align="right"><div align="right">Qt 4.6.3</div></td>
</tr></table></div></address></body>
</html>