Root/
| 1 | /* |
| 2 | * Copyright 2010-2011 Niels Kummerfeldt <niels.kummerfeldt@tu-harburg.de> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include "mapwidget.h" |
| 21 | |
| 22 | #include "projection.h" |
| 23 | |
| 24 | #include <cmath> |
| 25 | |
| 26 | #include <QtCore/QDebug> |
| 27 | #include <QtCore/QDir> |
| 28 | #include <QtCore/QFile> |
| 29 | #include <QtCore/QFileInfo> |
| 30 | #include <QtCore/QSettings> |
| 31 | #include <QtCore/QTimer> |
| 32 | #include <QtGui/QApplication> |
| 33 | #ifdef Q_WS_QWS |
| 34 | #include <QtGui/QWSServer> |
| 35 | #endif |
| 36 | #include <QtGui/QPainter> |
| 37 | #include <QtGui/QPaintEvent> |
| 38 | #include <QtNetwork/QNetworkReply> |
| 39 | #include <QtNetwork/QNetworkRequest> |
| 40 | |
| 41 | MapWidget::MapWidget(QWidget *parent) |
| 42 | : QWidget(parent), |
| 43 | m_usage(false), |
| 44 | m_ui(true), |
| 45 | m_zoomable(false), |
| 46 | m_takeScreenshot(false), |
| 47 | m_screenshotNumber(0), |
| 48 | m_baseName(), |
| 49 | m_xPadding(0), |
| 50 | m_yPadding(0), |
| 51 | m_pos(0, 0), |
| 52 | m_startPos(0, 0), |
| 53 | m_isMoving(false), |
| 54 | m_pixWidth(256), |
| 55 | m_pixHeight(256), |
| 56 | m_pix(), |
| 57 | m_cols(2), |
| 58 | m_rows(2), |
| 59 | m_indexX(0), |
| 60 | m_indexY(0), |
| 61 | m_minIndexX(0), |
| 62 | m_minIndexY(0), |
| 63 | m_maxIndexX(0), |
| 64 | m_maxIndexY(0), |
| 65 | m_minIndexXList(), |
| 66 | m_minIndexYList(), |
| 67 | m_maxIndexXList(), |
| 68 | m_maxIndexYList(), |
| 69 | m_level(0), |
| 70 | m_zoomLevel(), |
| 71 | m_manager(new QNetworkAccessManager(this)), |
| 72 | m_networkMode(false), |
| 73 | m_copyright(), |
| 74 | m_layer(), |
| 75 | m_layerMenu(new QMenu(this)) |
| 76 | { |
| 77 | for (int x = 0; x < 100; ++x) { |
| 78 | for (int y = 0; y < 100; ++y) { |
| 79 | m_pix[x][y] = 0; |
| 80 | } |
| 81 | } |
| 82 | QString fileName; |
| 83 | if (QApplication::arguments().count() > 1) { |
| 84 | fileName = QApplication::arguments().at(1); |
| 85 | } |
| 86 | if (fileName.endsWith(".map")) { |
| 87 | loadMapFile(fileName); |
| 88 | |
| 89 | m_zoomable = m_zoomLevel.count() > 1 && |
| 90 | m_zoomLevel.count() == m_minIndexXList.count() && |
| 91 | m_zoomLevel.count() == m_maxIndexXList.count() && |
| 92 | m_zoomLevel.count() == m_minIndexYList.count() && |
| 93 | m_zoomLevel.count() == m_maxIndexYList.count(); |
| 94 | |
| 95 | m_indexX = (m_minIndexX + m_maxIndexX) / 2; |
| 96 | m_indexY = (m_minIndexY + m_maxIndexY) / 2; |
| 97 | |
| 98 | m_cols = ceil(320.0 / (qreal) m_pixWidth) + 1; |
| 99 | m_rows = ceil(240.0 / (qreal) m_pixHeight) + 1; |
| 100 | |
| 101 | for (int x = 0; x < m_cols; ++x) { |
| 102 | for (int y = 0; y < m_rows; ++y) { |
| 103 | m_pix[x][y] = loadPixmap(m_indexX+x, m_indexY+y); |
| 104 | } |
| 105 | } |
| 106 | } else { |
| 107 | m_networkMode = true; |
| 108 | m_zoomable = true; |
| 109 | for (int i = 0; i < 19; ++i) { |
| 110 | m_zoomLevel << QString::number(i); |
| 111 | m_minIndexXList << 0; |
| 112 | m_maxIndexXList << (1 << i) - 1; |
| 113 | m_minIndexYList << 0; |
| 114 | m_maxIndexYList << (1 << i) - 1; |
| 115 | } |
| 116 | m_baseName = QDir::homePath()+"/Maps/%p/%z/%x/%y.png"; |
| 117 | QTimer::singleShot(100, this, SLOT(loadConfig())); |
| 118 | } |
| 119 | |
| 120 | connect(m_manager, SIGNAL(finished(QNetworkReply*)), |
| 121 | this, SLOT(replyFinished(QNetworkReply*))); |
| 122 | |
| 123 | #ifdef Q_WS_QWS |
| 124 | QWSServer::setCursorVisible(false); |
| 125 | #endif |
| 126 | setFocusPolicy(Qt::StrongFocus); |
| 127 | resize(320, 240); |
| 128 | } |
| 129 | |
| 130 | MapWidget::~MapWidget() |
| 131 | { |
| 132 | if (m_networkMode) { |
| 133 | saveConfig(); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void MapWidget::addLayer(AbstractLayer *layer, int z, const QString &name) |
| 138 | { |
| 139 | QAction *action = new QAction(name, m_layerMenu); |
| 140 | action->setCheckable(true); |
| 141 | action->setChecked(layer->isVisible()); |
| 142 | connect(action, SIGNAL(triggered(bool)), layer, SLOT(setVisible(bool))); |
| 143 | connect(layer, SIGNAL(visibilityChanged(bool)), action, SLOT(setChecked(bool))); |
| 144 | m_layerMenu->addAction(action); |
| 145 | m_layer.insertMulti(z, layer); |
| 146 | layer->zoom(m_level); |
| 147 | } |
| 148 | |
| 149 | void MapWidget::resizeEvent(QResizeEvent *event) |
| 150 | { |
| 151 | qreal width = event->size().width(); |
| 152 | qreal height = event->size().height(); |
| 153 | |
| 154 | for (int x = 0; x < m_cols; ++x) { |
| 155 | for (int y = 0; y < m_rows; ++y) { |
| 156 | delete m_pix[x][y]; |
| 157 | m_pix[x][y] = 0; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | m_cols = ceil(width / (qreal) m_pixWidth) + 1; |
| 162 | m_rows = ceil(height / (qreal) m_pixHeight) + 1; |
| 163 | |
| 164 | for (int x = 0; x < m_cols; ++x) { |
| 165 | for (int y = 0; y < m_rows; ++y) { |
| 166 | m_pix[x][y] = loadPixmap(m_indexX+x, m_indexY+y); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | void MapWidget::mouseMoveEvent(QMouseEvent *event) |
| 172 | { |
| 173 | event->accept(); |
| 174 | |
| 175 | if (m_isMoving) { |
| 176 | foreach (AbstractLayer *l, m_layer) { |
| 177 | l->pan((event->pos() - m_startPos) - m_pos); |
| 178 | } |
| 179 | m_pos = event->pos() - m_startPos; |
| 180 | updatePos(); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | void MapWidget::mousePressEvent(QMouseEvent *event) |
| 185 | { |
| 186 | event->accept(); |
| 187 | |
| 188 | if (m_ui && QRect(9, 14, 13, 13).contains(event->pos())) { |
| 189 | changeZoomLevel(1); |
| 190 | reloadPixmaps(); |
| 191 | updatePos(); |
| 192 | m_isMoving = false; |
| 193 | } else if (m_ui && QRect(9, 214, 13, 13).contains(event->pos())) { |
| 194 | changeZoomLevel(-1); |
| 195 | reloadPixmaps(); |
| 196 | updatePos(); |
| 197 | m_isMoving = false; |
| 198 | } else if (!m_ui || !QRect(5, 10, 20, 220).contains(event->pos())) { |
| 199 | m_startPos = event->pos() - m_pos; |
| 200 | m_isMoving = true; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | void MapWidget::mouseReleaseEvent(QMouseEvent *event) |
| 205 | { |
| 206 | event->accept(); |
| 207 | |
| 208 | m_isMoving = false; |
| 209 | } |
| 210 | |
| 211 | void MapWidget::wheelEvent(QWheelEvent *event) |
| 212 | { |
| 213 | event->accept(); |
| 214 | |
| 215 | if (event->delta() < 0) { |
| 216 | changeZoomLevel(-1); |
| 217 | reloadPixmaps(); |
| 218 | } else { |
| 219 | changeZoomLevel(1); |
| 220 | reloadPixmaps(); |
| 221 | } |
| 222 | updatePos(); |
| 223 | } |
| 224 | |
| 225 | void MapWidget::keyPressEvent(QKeyEvent *event) |
| 226 | { |
| 227 | event->accept(); |
| 228 | |
| 229 | QPoint move; |
| 230 | int width = 10; |
| 231 | if (event->modifiers() == Qt::AltModifier) { |
| 232 | width = 100; |
| 233 | } else if (event->modifiers() == Qt::ShiftModifier) { |
| 234 | width = 1; |
| 235 | } |
| 236 | switch (event->key()) { |
| 237 | case Qt::Key_Tab: |
| 238 | { |
| 239 | if (event->modifiers() == Qt::NoModifier) { |
| 240 | emit showMarkerList(); |
| 241 | } |
| 242 | break; |
| 243 | } |
| 244 | case Qt::Key_D: |
| 245 | { |
| 246 | if (event->modifiers() == Qt::ControlModifier) { |
| 247 | emit downloadArea(m_level, geoRect()); |
| 248 | } |
| 249 | break; |
| 250 | } |
| 251 | case Qt::Key_Up: |
| 252 | { |
| 253 | move = QPoint(0, width); |
| 254 | break; |
| 255 | } |
| 256 | case Qt::Key_Down: |
| 257 | { |
| 258 | move = QPoint(0, -width); |
| 259 | break; |
| 260 | } |
| 261 | case Qt::Key_Left: |
| 262 | { |
| 263 | move = QPoint(width, 0); |
| 264 | break; |
| 265 | } |
| 266 | case Qt::Key_Right: |
| 267 | { |
| 268 | move = QPoint(-width, 0); |
| 269 | break; |
| 270 | } |
| 271 | case Qt::Key_O: |
| 272 | { |
| 273 | if (event->modifiers() == Qt::NoModifier) { |
| 274 | changeZoomLevel(-1); |
| 275 | reloadPixmaps(); |
| 276 | } else if (event->modifiers() == Qt::ControlModifier) { |
| 277 | emit loadFile(); |
| 278 | } |
| 279 | break; |
| 280 | } |
| 281 | case Qt::Key_I: |
| 282 | { |
| 283 | if (event->modifiers() == Qt::NoModifier) { |
| 284 | changeZoomLevel(1); |
| 285 | reloadPixmaps(); |
| 286 | } |
| 287 | break; |
| 288 | } |
| 289 | case Qt::Key_U: |
| 290 | { |
| 291 | if (event->modifiers() == Qt::NoModifier) { |
| 292 | m_ui = !m_ui; |
| 293 | } |
| 294 | break; |
| 295 | } |
| 296 | case Qt::Key_H: |
| 297 | { |
| 298 | if (event->modifiers() == Qt::NoModifier) { |
| 299 | m_usage = !m_usage; |
| 300 | } |
| 301 | break; |
| 302 | } |
| 303 | case Qt::Key_F: |
| 304 | { |
| 305 | if (event->modifiers() == Qt::ControlModifier) { |
| 306 | emit search(); |
| 307 | } |
| 308 | break; |
| 309 | } |
| 310 | case Qt::Key_S: |
| 311 | { |
| 312 | if (event->modifiers() == Qt::AltModifier) { |
| 313 | m_takeScreenshot = true; |
| 314 | } |
| 315 | break; |
| 316 | } |
| 317 | case Qt::Key_L: |
| 318 | { |
| 319 | if (event->modifiers() == Qt::NoModifier) { |
| 320 | m_layerMenu->popup(mapToGlobal(QPoint(0, 0))); |
| 321 | } |
| 322 | break; |
| 323 | } |
| 324 | case Qt::Key_Q: |
| 325 | case Qt::Key_Escape: |
| 326 | { |
| 327 | if (event->modifiers() == Qt::NoModifier) { |
| 328 | emit close(); |
| 329 | } |
| 330 | break; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | foreach (AbstractLayer *l, m_layer) { |
| 335 | l->keyPressed(event); |
| 336 | } |
| 337 | |
| 338 | m_pos += move; |
| 339 | foreach (AbstractLayer *l, m_layer) { |
| 340 | l->pan(move); |
| 341 | } |
| 342 | updatePos(); |
| 343 | } |
| 344 | |
| 345 | void MapWidget::paintEvent(QPaintEvent *event) |
| 346 | { |
| 347 | event->accept(); |
| 348 | |
| 349 | QImage screenshot(width(), height(), QImage::Format_ARGB32); |
| 350 | |
| 351 | QPainter painter; |
| 352 | if (m_takeScreenshot) { |
| 353 | painter.begin(&screenshot); |
| 354 | } else { |
| 355 | painter.begin(this); |
| 356 | } |
| 357 | |
| 358 | painter.setPen(Qt::black); |
| 359 | for (int x = 0; x < m_cols; ++x) { |
| 360 | for (int y = 0; y < m_rows; ++y) { |
| 361 | QPixmap *pix = m_pix[x][y]; |
| 362 | if (pix) { |
| 363 | QRect rect(m_pos+QPoint(m_pixWidth*x, m_pixHeight*y), pix->size()); |
| 364 | painter.drawPixmap(rect, *pix); |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | QMapIterator<int, AbstractLayer *> i(m_layer); |
| 370 | while (i.hasNext()) { |
| 371 | i.next(); |
| 372 | i.value()->paintLayer(&painter); |
| 373 | } |
| 374 | |
| 375 | if (m_ui) { |
| 376 | painter.setBrush(QBrush(QColor(255, 255, 255, 210))); |
| 377 | if (m_networkMode) { |
| 378 | painter.drawRoundedRect(30, height() - 17, width() - 145, 16, 5, 5); |
| 379 | painter.drawText(35, height() - 15, width() - 155, 14, Qt::AlignCenter, |
| 380 | Projection::geo2string(geoPos())); |
| 381 | } |
| 382 | if (m_zoomable) { |
| 383 | painter.drawRoundedRect(5, 10, 20, 220, 10, 10); |
| 384 | painter.setBrush(QBrush(QColor(0, 0, 255, 210))); |
| 385 | int step = 180 / (m_zoomLevel.count() - 1); |
| 386 | for (int i = 0; i < m_zoomLevel.count(); ++i) { |
| 387 | painter.drawLine(5, 211-(i * step), 25, 211-(i * step)); |
| 388 | } |
| 389 | painter.drawRect(9, 208-(m_level * step), 12, 5); |
| 390 | painter.setPen(QPen(Qt::black, 3, Qt::SolidLine, Qt::FlatCap)); |
| 391 | painter.drawLine(9, 20, 22, 20); |
| 392 | painter.drawLine(15, 14, 15, 27); |
| 393 | painter.drawLine(9, 220, 22, 220); |
| 394 | painter.setPen(QPen()); |
| 395 | } |
| 396 | int midX = width() / 2; |
| 397 | int midY = height() / 2; |
| 398 | painter.drawLine(midX - 5, midY, midX + 5, midY); |
| 399 | painter.drawLine(midX, midY - 5, midX, midY + 5); |
| 400 | } |
| 401 | |
| 402 | if (m_usage) { |
| 403 | painter.setBrush(QBrush(QColor(255, 255, 255, 210))); |
| 404 | painter.drawRoundedRect(20, 5, 280, 215, 10, 10); |
| 405 | |
| 406 | QStringList usage; |
| 407 | usage << "Esc: Quit application"; |
| 408 | usage << "h: Show/hide this message"; |
| 409 | usage << "Arrows: Move the map"; |
| 410 | if (m_zoomable) { |
| 411 | usage << "i: Zoom in"; |
| 412 | usage << "o: Zoom out"; |
| 413 | } |
| 414 | usage << "u: Show/hide user interface"; |
| 415 | usage << "m: Add a marker"; |
| 416 | usage << "l: Show/hide individual layers"; |
| 417 | usage << "tab: Show/hide marker list"; |
| 418 | if (m_networkMode) { |
| 419 | usage << "Crtl+d: Download map data for visible area"; |
| 420 | painter.drawText(30, 200, 260, 20, Qt::AlignCenter, "Map data: (C) OpenStreetMap.org"); |
| 421 | } else if (!m_copyright.isEmpty()) { |
| 422 | painter.drawText(30, 200, 260, 20, Qt::AlignCenter, "Map data: (C) "+m_copyright); |
| 423 | } |
| 424 | usage << "Ctrl+o: Open POI / Track file"; |
| 425 | painter.drawText(30, 10, 260, 20, Qt::AlignCenter, "NanoMap - Usage"); |
| 426 | painter.drawLine(70, 27, 250, 27); |
| 427 | painter.drawText(30, 30, 260, 200, Qt::AlignLeft, usage.join("\n")); |
| 428 | } |
| 429 | |
| 430 | painter.end(); |
| 431 | if (m_takeScreenshot) { |
| 432 | screenshot.save(QString("NanoMap-%1.png").arg(m_screenshotNumber)); |
| 433 | m_takeScreenshot = false; |
| 434 | ++m_screenshotNumber; |
| 435 | update(); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | void MapWidget::replyFinished(QNetworkReply *reply) |
| 440 | { |
| 441 | if (reply->error() == QNetworkReply::NoError) { |
| 442 | QString path = reply->url().path(); |
| 443 | int level = path.section('/', 1, 1).toInt(); |
| 444 | int x = path.section('/', 2, 2).toInt(); |
| 445 | QString name = path.section('/', 3, 3); |
| 446 | int y = name.section('.', 0, 0).toInt(); |
| 447 | |
| 448 | QDir base(QDir::homePath()+"/Maps/OSM"); |
| 449 | base.mkpath(QString("%1/%2").arg(level).arg(x)); |
| 450 | |
| 451 | QByteArray data = reply->readAll(); |
| 452 | if (!data.isEmpty()) { |
| 453 | QFile file(QDir::homePath()+"/Maps/OSM"+path); |
| 454 | if (file.open(QFile::WriteOnly)) { |
| 455 | file.write(data); |
| 456 | if (level == m_level) { |
| 457 | if (m_pix[x-m_indexX][y-m_indexY]) { |
| 458 | m_pix[x-m_indexX][y-m_indexY]->loadFromData(data); |
| 459 | } else { |
| 460 | m_pix[x-m_indexX][y-m_indexY] = new QPixmap(file.fileName()); |
| 461 | } |
| 462 | update(); |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | reply->deleteLater(); |
| 468 | } |
| 469 | |
| 470 | void MapWidget::updatePos() |
| 471 | { |
| 472 | if (m_pos.x() < -m_pixWidth) { |
| 473 | if (m_indexX < m_maxIndexX) { |
| 474 | m_pos.setX(m_pos.x() + m_pixWidth); |
| 475 | m_startPos.setX(m_startPos.x() - m_pixWidth); |
| 476 | ++m_indexX; |
| 477 | |
| 478 | for (int y = 0; y < m_rows; ++y) { |
| 479 | for (int x = 0; x < m_cols; ++x) { |
| 480 | if (x == 0) { |
| 481 | delete m_pix[x][y]; |
| 482 | m_pix[x][y] = 0; |
| 483 | } |
| 484 | if (x < m_cols-1) { |
| 485 | m_pix[x][y] = m_pix[x+1][y]; |
| 486 | } else { |
| 487 | m_pix[x][y] = loadPixmap(m_indexX+x, m_indexY+y); |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | } else if (m_pos.x() > 0) { |
| 493 | if (m_indexX > m_minIndexX) { |
| 494 | m_pos.setX(m_pos.x() - m_pixWidth); |
| 495 | m_startPos.setX(m_startPos.x() + m_pixWidth); |
| 496 | --m_indexX; |
| 497 | |
| 498 | for (int y = 0; y < m_rows; ++y) { |
| 499 | for (int x = m_cols-1; x >= 0; --x) { |
| 500 | if (x == m_cols-1) { |
| 501 | delete m_pix[x][y]; |
| 502 | m_pix[x][y] = 0; |
| 503 | } |
| 504 | if (x > 0) { |
| 505 | m_pix[x][y] = m_pix[x-1][y]; |
| 506 | } else { |
| 507 | m_pix[x][y] = loadPixmap(m_indexX, m_indexY+y); |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | if (m_pos.y() < -m_pixHeight) { |
| 514 | if (m_indexY < m_maxIndexY) { |
| 515 | m_pos.setY(m_pos.y() + m_pixHeight); |
| 516 | m_startPos.setY(m_startPos.y() - m_pixHeight); |
| 517 | ++m_indexY; |
| 518 | |
| 519 | for (int x = 0; x < m_cols; ++x) { |
| 520 | for (int y = 0; y < m_rows; ++y) { |
| 521 | if (y == 0) { |
| 522 | delete m_pix[x][y]; |
| 523 | m_pix[x][y] = 0; |
| 524 | } |
| 525 | if (y < m_rows-1) { |
| 526 | m_pix[x][y] = m_pix[x][y+1]; |
| 527 | } else { |
| 528 | m_pix[x][y] = loadPixmap(m_indexX+x, m_indexY+y); |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | } else if (m_pos.y() > 0) { |
| 534 | if (m_indexY > m_minIndexY) { |
| 535 | m_pos.setY(m_pos.y() - m_pixHeight); |
| 536 | m_startPos.setY(m_startPos.y() + m_pixHeight); |
| 537 | --m_indexY; |
| 538 | |
| 539 | for (int x = 0; x < m_cols; ++x) { |
| 540 | for (int y = m_rows-1; y >= 0; --y) { |
| 541 | if (y == m_rows-1) { |
| 542 | delete m_pix[x][y]; |
| 543 | m_pix[x][y] = 0; |
| 544 | } |
| 545 | if (y > 0) { |
| 546 | m_pix[x][y] = m_pix[x][y-1]; |
| 547 | } else { |
| 548 | m_pix[x][y] = loadPixmap(m_indexX+x, m_indexY); |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | update(); |
| 555 | } |
| 556 | |
| 557 | void MapWidget::reloadPixmaps() |
| 558 | { |
| 559 | for (int x = 0; x < m_cols; ++x) { |
| 560 | for (int y = 0; y < m_rows; ++y) { |
| 561 | delete m_pix[x][y]; |
| 562 | m_pix[x][y] = 0; |
| 563 | m_pix[x][y] = loadPixmap(m_indexX+x, m_indexY+y); |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | QString MapWidget::filename(int x, int y) |
| 569 | { |
| 570 | QString result; |
| 571 | if (x >= m_minIndexX && x <= m_maxIndexX && |
| 572 | y >= m_minIndexY && y <= m_maxIndexY) { |
| 573 | QString level = QString::number(m_level); |
| 574 | QString sx = QString::number(x); |
| 575 | sx.prepend(QString(m_xPadding-sx.length(), '0')); |
| 576 | QString sy = QString::number(y); |
| 577 | sy.prepend(QString(m_yPadding-sy.length(), '0')); |
| 578 | result = m_baseName; |
| 579 | result.replace("%z", level).replace("%x", sx).replace("%y", sy); |
| 580 | if (result.contains("%p")) { |
| 581 | QStringList dirs = QDir(QDir::homePath()+"/Maps/").entryList(QDir::AllDirs|QDir::NoDotAndDotDot); |
| 582 | foreach (const QString &dir, dirs) { |
| 583 | QString tmp = result; |
| 584 | tmp.replace("%p", dir); |
| 585 | if (QFile::exists(tmp)) { |
| 586 | return tmp; |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | } |
| 591 | return result; |
| 592 | } |
| 593 | |
| 594 | QPixmap* MapWidget::loadPixmap(int x, int y) |
| 595 | { |
| 596 | QPixmap *pix = 0; |
| 597 | |
| 598 | if (x >= m_minIndexX && x <= m_maxIndexX && |
| 599 | y >= m_minIndexY && y <= m_maxIndexY) { |
| 600 | pix = new QPixmap(filename(x, y)); |
| 601 | if (m_networkMode && pix->isNull()) { |
| 602 | downloadTile(x, y, m_level); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | return pix; |
| 607 | } |
| 608 | |
| 609 | void MapWidget::loadMapFile(const QString &filename) |
| 610 | { |
| 611 | QFile file(filename); |
| 612 | if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { |
| 613 | QFileInfo info(filename); |
| 614 | m_baseName = info.absolutePath()+QDir::separator(); |
| 615 | |
| 616 | QTextStream in(&file); |
| 617 | QString line; |
| 618 | while (!in.atEnd()) { |
| 619 | line = in.readLine().trimmed(); |
| 620 | if (!line.startsWith('#') && !line.isEmpty()) { |
| 621 | if (line.startsWith("BaseName=")) { |
| 622 | m_baseName.append(line.section('=', 1)); |
| 623 | } else if (line.startsWith("ZeroPadding=")) { |
| 624 | QString tmp = line.section('=', 1); |
| 625 | m_xPadding = tmp.section(',', 0, 0).toInt(); |
| 626 | m_yPadding = tmp.section(',', 1, 1).toInt(); |
| 627 | } else if (line.startsWith("TileWidth=")) { |
| 628 | QString tmp = line.section('=', 1); |
| 629 | m_pixWidth = tmp.toInt(); |
| 630 | } else if (line.startsWith("TileHeight=")) { |
| 631 | QString tmp = line.section('=', 1); |
| 632 | m_pixHeight = tmp.toInt(); |
| 633 | } else if (line.startsWith("ZoomLevel=")) { |
| 634 | QString tmp = line.section('=', 1); |
| 635 | m_zoomLevel << tmp.split(','); |
| 636 | } else if (line.startsWith("MinX=")) { |
| 637 | QString tmp = line.section('=', 1); |
| 638 | QStringList list = tmp.split(','); |
| 639 | foreach (const QString &s, list) { |
| 640 | m_minIndexXList << s.toInt(); |
| 641 | } |
| 642 | m_minIndexX = m_minIndexXList.first(); |
| 643 | } else if (line.startsWith("MaxX=")) { |
| 644 | QString tmp = line.section('=', 1); |
| 645 | QStringList list = tmp.split(','); |
| 646 | foreach (const QString &s, list) { |
| 647 | m_maxIndexXList << s.toInt(); |
| 648 | } |
| 649 | m_maxIndexX = m_maxIndexXList.first(); |
| 650 | } else if (line.startsWith("MinY=")) { |
| 651 | QString tmp = line.section('=', 1); |
| 652 | QStringList list = tmp.split(','); |
| 653 | foreach (const QString &s, list) { |
| 654 | m_minIndexYList << s.toInt(); |
| 655 | } |
| 656 | m_minIndexY = m_minIndexYList.first(); |
| 657 | } else if (line.startsWith("MaxY=")) { |
| 658 | QString tmp = line.section('=', 1); |
| 659 | QStringList list = tmp.split(','); |
| 660 | foreach (const QString &s, list) { |
| 661 | m_maxIndexYList << s.toInt(); |
| 662 | } |
| 663 | m_maxIndexY = m_maxIndexYList.first(); |
| 664 | } else if (line.startsWith("CopyRight=")) { |
| 665 | m_copyright = line.section('=', 1); |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | void MapWidget::loadConfig() |
| 673 | { |
| 674 | QSettings set(QDir::homePath()+"/Maps/nanomap.conf", QSettings::NativeFormat); |
| 675 | |
| 676 | set.beginGroup("map"); |
| 677 | qreal lon = set.value("lon", 0).toReal(); |
| 678 | qreal lat = set.value("lat", 0).toReal(); |
| 679 | int level = set.value("level", 0).toInt(); |
| 680 | m_usage = set.value("usage", true).toBool(); |
| 681 | changeZoomLevel(level - m_level); |
| 682 | centerOnGeoPos(lon, lat); |
| 683 | set.endGroup(); |
| 684 | } |
| 685 | |
| 686 | void MapWidget::saveConfig() |
| 687 | { |
| 688 | QSettings set(QDir::homePath()+"/Maps/nanomap.conf", QSettings::NativeFormat); |
| 689 | |
| 690 | set.beginGroup("map"); |
| 691 | QPointF pos = geoPos(); |
| 692 | set.setValue("lon", pos.x()); |
| 693 | set.setValue("lat", pos.y()); |
| 694 | set.setValue("level", m_level); |
| 695 | set.setValue("usage", m_usage); |
| 696 | set.endGroup(); |
| 697 | } |
| 698 | |
| 699 | void MapWidget::downloadTile(int x, int y, int level) |
| 700 | { |
| 701 | QUrl url(QString("http://tile.openstreetmap.org/%1/%2/%3.png").arg(level).arg(x).arg(y)); |
| 702 | m_manager->get(QNetworkRequest(url)); |
| 703 | } |
| 704 | |
| 705 | void MapWidget::changeZoomLevel(int diff) |
| 706 | { |
| 707 | qreal w = width() / 2; |
| 708 | qreal h = height() / 2; |
| 709 | qreal px = (qreal) ((m_indexX - m_minIndexX)*m_pixWidth + w - m_pos.x()) / |
| 710 | (qreal) ((1 + m_maxIndexX - m_minIndexX) * m_pixWidth); |
| 711 | qreal py = (qreal) ((m_indexY - m_minIndexY)*m_pixHeight + h - m_pos.y()) / |
| 712 | (qreal) ((1 + m_maxIndexY - m_minIndexY) * m_pixHeight); |
| 713 | |
| 714 | m_level += diff; |
| 715 | |
| 716 | if (m_level < 0 || m_level >= m_zoomLevel.count()) { |
| 717 | m_level -= diff; |
| 718 | return; |
| 719 | } |
| 720 | |
| 721 | m_minIndexX = m_minIndexXList.at(m_level); |
| 722 | m_maxIndexX = m_maxIndexXList.at(m_level); |
| 723 | m_minIndexY = m_minIndexYList.at(m_level); |
| 724 | m_maxIndexY = m_maxIndexYList.at(m_level); |
| 725 | |
| 726 | qreal x = (px * (1 + m_maxIndexX - m_minIndexX)) + m_minIndexX; |
| 727 | qreal y = (py * (1 + m_maxIndexY - m_minIndexY)) + m_minIndexY; |
| 728 | |
| 729 | m_indexX = (int) floor(x); |
| 730 | m_indexY = (int) floor(y); |
| 731 | m_pos.setX(((m_indexX-x) * m_pixWidth) + w); |
| 732 | m_pos.setY(((m_indexY-y) * m_pixHeight) + h); |
| 733 | |
| 734 | foreach (AbstractLayer *l, m_layer) { |
| 735 | l->zoom(m_level); |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | void MapWidget::centerOnGeoPos(qreal lon, qreal lat) |
| 740 | { |
| 741 | QPoint oldPos = QPoint(m_pixWidth * m_indexX, m_pixHeight * m_indexY) - m_pos; |
| 742 | |
| 743 | qreal w = width() / 2.0; |
| 744 | qreal h = height() / 2.0; |
| 745 | |
| 746 | qreal x = Projection::lon2tilex(lon, m_level); |
| 747 | qreal y = Projection::lat2tiley(lat, m_level); |
| 748 | |
| 749 | m_indexX = (int) floor(x); |
| 750 | m_indexY = (int) floor(y); |
| 751 | m_pos.setX(((m_indexX-x) * m_pixWidth) + w); |
| 752 | m_pos.setY(((m_indexY-y) * m_pixHeight) + h); |
| 753 | |
| 754 | QPoint newPos = QPoint(m_pixWidth * m_indexX, m_pixHeight * m_indexY) - m_pos; |
| 755 | |
| 756 | reloadPixmaps(); |
| 757 | |
| 758 | foreach (AbstractLayer *l, m_layer) { |
| 759 | l->pan(oldPos - newPos); |
| 760 | } |
| 761 | |
| 762 | updatePos(); |
| 763 | } |
| 764 | |
| 765 | QRectF MapWidget::geoRect() const |
| 766 | { |
| 767 | qreal partX = (-m_pos.x()) / 256.0; |
| 768 | qreal partY = (height() - m_pos.y()) / 256.0; |
| 769 | qreal minLon = Projection::tilex2lon(m_indexX + partX, m_level); |
| 770 | qreal minLat = Projection::tiley2lat(m_indexY + partY, m_level); |
| 771 | |
| 772 | partX = (width() - m_pos.x()) / 256.0; |
| 773 | partY = (-m_pos.y()) / 256.0; |
| 774 | qreal maxLon = Projection::tilex2lon(m_indexX + partX, m_level); |
| 775 | qreal maxLat = Projection::tiley2lat(m_indexY + partY, m_level); |
| 776 | |
| 777 | return QRectF(QPointF(minLon, minLat), QPointF(maxLon, maxLat)); |
| 778 | } |
| 779 | |
| 780 | QPointF MapWidget::geoPos() const |
| 781 | { |
| 782 | qreal w = width() / 2.0; |
| 783 | qreal h = height() / 2.0; |
| 784 | qreal partX = (w - m_pos.x()) / 256.0; |
| 785 | qreal partY = (h - m_pos.y()) / 256.0; |
| 786 | qreal lon = Projection::tilex2lon(m_indexX + partX, m_level); |
| 787 | qreal lat = Projection::tiley2lat(m_indexY + partY, m_level); |
| 788 | |
| 789 | return QPointF(lon, lat); |
| 790 | } |
| 791 | |
| 792 | QPoint MapWidget::geo2screen(qreal lon, qreal lat) const |
| 793 | { |
| 794 | qreal tx = Projection::lon2tilex(lon, m_level); |
| 795 | qreal ty = Projection::lat2tiley(lat, m_level); |
| 796 | |
| 797 | int x = (tx * m_pixWidth) - ((m_indexX * m_pixWidth) - m_pos.x()); |
| 798 | int y = (ty * m_pixHeight) - ((m_indexY * m_pixHeight) - m_pos.y()); |
| 799 | |
| 800 | return QPoint(x, y); |
| 801 | } |
| 802 | |
| 803 | QPoint MapWidget::raw2screen(qreal x, qreal y, int scale) const |
| 804 | { |
| 805 | qreal tx = x * scale; |
| 806 | qreal ty = y * scale; |
| 807 | |
| 808 | int sx = (tx * m_pixWidth) - ((m_indexX * m_pixWidth) - m_pos.x()); |
| 809 | int sy = (ty * m_pixHeight) - ((m_indexY * m_pixHeight) - m_pos.y()); |
| 810 | |
| 811 | return QPoint(sx, sy); |
| 812 | } |
| 813 | |
| 814 |
Branches:
master
