|
| 1 | +#include "Lightmaps.h" |
| 2 | + |
| 3 | +LightMaps::LightMaps(QWidget *parent) : QWidget(parent), pressed(false), snapped(false), dragging(false) |
| 4 | +{ |
| 5 | + m_Map = new SlippyMap(this); |
| 6 | + connect(m_Map, SIGNAL(updated(QRect)), SLOT(updateMap(QRect))); |
| 7 | + connect(m_Map, SIGNAL(updated(QRect)), this, SIGNAL(updated())); |
| 8 | +} |
| 9 | + |
| 10 | +void LightMaps::setCenter(qreal lat, qreal lng) |
| 11 | +{ |
| 12 | + if ( m_Map->latitude == lat && m_Map->longitude == lng ) return; |
| 13 | + |
| 14 | + m_Map->latitude = lat; |
| 15 | + m_Map->longitude = lng; |
| 16 | + m_Map->locationSet = true; |
| 17 | + m_Map->cancelDownloads(); |
| 18 | + m_Map->invalidate(); |
| 19 | + |
| 20 | + emit latitudeChanged(lat); |
| 21 | + emit longitudeChanged(lng); |
| 22 | +} |
| 23 | + |
| 24 | +void LightMaps::setCenter(int zoom, qreal lat, qreal lng) |
| 25 | +{ |
| 26 | + if ( m_Map->latitude == lat && m_Map->longitude == lng && m_Map->zoom == zoom ) return; |
| 27 | + |
| 28 | + if ( zoom < 0 ) zoom = 0; |
| 29 | + if ( zoom > 18 ) zoom = 18; |
| 30 | + |
| 31 | + |
| 32 | + m_Map->latitude = lat; |
| 33 | + m_Map->longitude = lng; |
| 34 | + m_Map->zoom = zoom; |
| 35 | + m_Map->locationSet = true; |
| 36 | + m_Map->cancelDownloads(); |
| 37 | + m_Map->invalidate(); |
| 38 | + |
| 39 | + emit latitudeChanged(lat); |
| 40 | + emit longitudeChanged(lng); |
| 41 | + emit zoomChanged(zoom); |
| 42 | +} |
| 43 | + |
| 44 | +void LightMaps::setLatitude( qreal lat) |
| 45 | +{ |
| 46 | + if ( m_Map->latitude == lat ) return; |
| 47 | + |
| 48 | + m_Map->latitude = lat; |
| 49 | + m_Map->locationSet = true; |
| 50 | + m_Map->cancelDownloads(); |
| 51 | + m_Map->invalidate(); |
| 52 | + |
| 53 | + emit latitudeChanged(lat); |
| 54 | +} |
| 55 | + |
| 56 | +void LightMaps::setLongitude( qreal lng ) |
| 57 | +{ |
| 58 | + if ( m_Map->longitude == lng ) return; |
| 59 | + |
| 60 | + m_Map->longitude = lng; |
| 61 | + m_Map->locationSet = true; |
| 62 | + m_Map->cancelDownloads(); |
| 63 | + m_Map->invalidate(); |
| 64 | + emit longitudeChanged(lng); |
| 65 | +} |
| 66 | + |
| 67 | +qreal LightMaps::latitude() const |
| 68 | +{ |
| 69 | + return m_Map->latitude; |
| 70 | +} |
| 71 | + |
| 72 | +qreal LightMaps::longitude() const |
| 73 | +{ |
| 74 | + return m_Map->longitude; |
| 75 | +} |
| 76 | + |
| 77 | +void LightMaps::updateMap(const QRect &r) |
| 78 | +{ |
| 79 | + update(r); |
| 80 | +} |
| 81 | +void LightMaps::resizeEvent(QResizeEvent *) |
| 82 | +{ |
| 83 | + m_Map->width = width(); |
| 84 | + m_Map->height = height(); |
| 85 | + m_Map->invalidate(); |
| 86 | +} |
| 87 | + |
| 88 | +void LightMaps::paintEvent(QPaintEvent *event) |
| 89 | +{ |
| 90 | + QPainter p; |
| 91 | + p.begin(this); |
| 92 | + m_Map->render(&p, event->rect()); |
| 93 | + |
| 94 | + QPen pen; |
| 95 | + pen.setColor(Qt::red); |
| 96 | + pen.setWidth(3); |
| 97 | + p.setPen(pen); |
| 98 | + |
| 99 | + // Draw graphics over the top. |
| 100 | + foreach ( const QRectF& line, m_Lines ) |
| 101 | + { |
| 102 | + double latitude1 = line.top(); |
| 103 | + double longitude1 = line.left(); |
| 104 | + double latitude2 = line.bottom(); |
| 105 | + double longitude2 = line.right(); |
| 106 | + |
| 107 | + QPoint p1,p2; |
| 108 | + |
| 109 | + bool p1Visible = geoToScreen(latitude1, longitude1, p1); |
| 110 | + bool p2Visible = geoToScreen(latitude2, longitude2, p2); |
| 111 | + |
| 112 | + if ( !p1Visible && !p2Visible ) |
| 113 | + { |
| 114 | + // just skip it. |
| 115 | + continue; |
| 116 | + } |
| 117 | + |
| 118 | + p.drawLine(p1,p2); |
| 119 | + |
| 120 | + } |
| 121 | + |
| 122 | + QPen pen_blue; |
| 123 | + pen_blue.setColor(Qt::blue); |
| 124 | + pen_blue.setWidth(3); |
| 125 | + p.setPen(pen_blue); |
| 126 | + |
| 127 | + foreach ( const QPointF& pf, m_Circles ) |
| 128 | + { |
| 129 | + QPoint p1; |
| 130 | + bool centerVisible = geoToScreen( pf.y(), pf.x(), p1); |
| 131 | + |
| 132 | + if ( !centerVisible ) |
| 133 | + { |
| 134 | + continue; |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + p.drawEllipse(p1, 6,6); |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | + |
| 144 | + p.setPen(Qt::black); |
| 145 | + |
| 146 | + p.drawText(rect(), Qt::AlignBottom | Qt::TextWordWrap, |
| 147 | + "Map data CCbySA 2009 OpenStreetMap.org contributors"); |
| 148 | + p.end(); |
| 149 | + |
| 150 | +} |
| 151 | + |
| 152 | +void LightMaps::timerEvent(QTimerEvent *) |
| 153 | +{ |
| 154 | + update(); |
| 155 | +} |
| 156 | + |
| 157 | +void LightMaps::mousePressEvent(QMouseEvent *event) |
| 158 | +{ |
| 159 | + if (event->buttons() != Qt::LeftButton) |
| 160 | + return; |
| 161 | + pressed = snapped = true; |
| 162 | + pressPos = dragPos = event->pos(); |
| 163 | +} |
| 164 | + |
| 165 | +void LightMaps::mouseMoveEvent(QMouseEvent *event) |
| 166 | +{ |
| 167 | + if (!event->buttons()) |
| 168 | + return; |
| 169 | + |
| 170 | + if (!pressed || !snapped) |
| 171 | + { |
| 172 | + QPoint delta = event->pos() - pressPos; |
| 173 | + pressPos = event->pos(); |
| 174 | + m_Map->pan(delta); |
| 175 | + emit dragBegin(); |
| 176 | + dragging = true; |
| 177 | + return; |
| 178 | + } |
| 179 | + else |
| 180 | + { |
| 181 | + const int threshold = 10; |
| 182 | + QPoint delta = event->pos() - pressPos; |
| 183 | + if (snapped) |
| 184 | + { |
| 185 | + snapped &= delta.x() < threshold; |
| 186 | + snapped &= delta.y() < threshold; |
| 187 | + snapped &= delta.x() > -threshold; |
| 188 | + snapped &= delta.y() > -threshold; |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | +} |
| 193 | + |
| 194 | +void LightMaps::mouseReleaseEvent(QMouseEvent *) |
| 195 | +{ |
| 196 | + if ( dragging ) { |
| 197 | + emit dragEnd(); |
| 198 | + dragging = false; |
| 199 | + } |
| 200 | + |
| 201 | + update(); |
| 202 | +} |
| 203 | + |
| 204 | +void LightMaps::keyPressEvent(QKeyEvent *event) |
| 205 | +{ |
| 206 | + |
| 207 | + if (event->key() == Qt::Key_Left) |
| 208 | + m_Map->pan(QPoint(20, 0)); |
| 209 | + if (event->key() == Qt::Key_Right) |
| 210 | + m_Map->pan(QPoint(-20, 0)); |
| 211 | + if (event->key() == Qt::Key_Up) |
| 212 | + m_Map->pan(QPoint(0, 20)); |
| 213 | + if (event->key() == Qt::Key_Down) |
| 214 | + m_Map->pan(QPoint(0, -20)); |
| 215 | + |
| 216 | +} |
| 217 | + |
| 218 | +void LightMaps::wheelEvent(QWheelEvent *event) |
| 219 | +{ |
| 220 | + if ( event->delta() < 0 ) |
| 221 | + { |
| 222 | + setZoom( zoom() - 1 ); |
| 223 | + } |
| 224 | + else |
| 225 | + { |
| 226 | + setZoom( zoom() + 1 ); |
| 227 | + } |
| 228 | + |
| 229 | +} |
| 230 | + |
| 231 | +void LightMaps::setZoom(int zoom) |
| 232 | +{ |
| 233 | + if ( zoom < 0 ) return; |
| 234 | + if ( zoom > 18 ) return; |
| 235 | + |
| 236 | + |
| 237 | + if ( m_Map->zoom == zoom ) return; |
| 238 | + m_Map->zoom = zoom; |
| 239 | + m_Map->cancelDownloads(); |
| 240 | + m_Map->invalidate(); |
| 241 | + emit zoomChanged(zoom); |
| 242 | +} |
| 243 | + |
| 244 | +int LightMaps::zoom() const |
| 245 | +{ |
| 246 | + return m_Map->zoom; |
| 247 | +} |
| 248 | + |
| 249 | +bool LightMaps::geoToScreen(qreal latitude, qreal longitude, QPoint &p) const |
| 250 | +{ |
| 251 | + return m_Map->geoToScreen(latitude, longitude, p); |
| 252 | +} |
| 253 | + |
| 254 | +QRectF LightMaps::geoBounds() |
| 255 | +{ |
| 256 | + return m_Map->geoBounds(); |
| 257 | +} |
| 258 | + |
| 259 | +void LightMaps::clearLines() |
| 260 | +{ |
| 261 | + m_Lines.clear(); |
| 262 | +} |
| 263 | + |
| 264 | +void LightMaps::addLine(qreal latitude1, qreal longitude1, qreal latitude2, qreal longitude2) |
| 265 | +{ |
| 266 | + QRectF line( QPointF(longitude1, latitude1),QPointF(longitude2, latitude2) ); |
| 267 | + m_Lines.append(line); |
| 268 | +} |
| 269 | + |
| 270 | +void LightMaps::clearCircles() |
| 271 | +{ |
| 272 | + m_Circles.clear(); |
| 273 | +} |
| 274 | + |
| 275 | +void LightMaps::addCircle(qreal latitude, qreal longitude) |
| 276 | +{ |
| 277 | + m_Circles.append( QPointF( longitude, latitude )); |
| 278 | +} |
| 279 | + |
| 280 | + |
| 281 | + |
0 commit comments