wordpress 導(dǎo)入幻燈片優(yōu)化網(wǎng)站建設(shè)
前文鏈接:QGraphicsView實(shí)現(xiàn)簡易地圖3『局部加載-地圖縮放』
當(dāng)鼠標(biāo)拖動地圖移動時(shí),需要實(shí)時(shí)增補(bǔ)和刪減瓦片地圖,大致思路是計(jì)算地圖從各方向移動時(shí)進(jìn)出視口的瓦片坐標(biāo)值,根據(jù)變化后的瓦片坐標(biāo)值來增減地圖瓦片,以下將提供實(shí)現(xiàn)此需求的核心代碼。
1、動態(tài)演示效果
2、靜態(tài)展示圖片

核心代碼
void MapView::moveScene()
{QString appPath = QApplication::applicationDirPath();QString dirPath = QString("%1/MapData/GaoDeMap/Map/MapPng/L0%2").arg(appPath).arg(m_curLevel + 1);// 視口寬度和高度int w = viewport()->width();int h = viewport()->height();// 計(jì)算呈現(xiàn)的瓦片地圖左上角的場景坐標(biāo)和視口坐標(biāo)、呈現(xiàn)的瓦片地圖右下角的場景坐標(biāo)和視口坐標(biāo)QPoint topLeftScenePos(m_topLeftTileCoord.x * PIXMAP_SIZE, m_topLeftTileCoord.y * PIXMAP_SIZE);QPointF topLeftViewPos = mapFromScene(topLeftScenePos);QPoint bottomRightScenePos(m_bottomRightTileCoord.x * PIXMAP_SIZE, m_bottomRightTileCoord.y * PIXMAP_SIZE);QPointF bottomRightViewPos = mapFromScene(bottomRightScenePos);// 1、水平瓦片坐標(biāo)控制:判斷最左側(cè)瓦片是否完全進(jìn)入視口、最右側(cè)瓦片是否完全離開視口if (topLeftViewPos.x() > 0){int count = qCeil(topLeftViewPos.x() / PIXMAP_SIZE); // 左側(cè)進(jìn)入視口瓦片數(shù)量int oldLeftTileCoordX = m_topLeftTileCoord.x; // 保存原左側(cè)瓦片坐標(biāo)Xm_topLeftTileCoord.x -= count; // 更新現(xiàn)左側(cè)瓦片坐標(biāo)X// 增加從左側(cè)進(jìn)入視口的圖片for (int row = m_topLeftTileCoord.y; row <= m_bottomRightTileCoord.y; ++row){for (int col = m_topLeftTileCoord.x; col < oldLeftTileCoordX; ++col){QString fileName = QString("%1/Map_%2-%3.png").arg(dirPath).arg(QString::number(row + 1).rightJustified(2, '0')).arg(QString::number(col + 1).rightJustified(2, '0'));QPixmap pixmap(fileName);QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);item->setPos(PIXMAP_SIZE * col, PIXMAP_SIZE * row);m_scene->addItem(item);m_mapItems[row][col] = item;}}}if (bottomRightViewPos.x() > w){int count = qFloor((bottomRightViewPos.x() - w) / PIXMAP_SIZE) + 1; // 右側(cè)離開視口瓦片數(shù)量int oldRightTileCoordX = m_bottomRightTileCoord.x; // 保存原右側(cè)瓦片坐標(biāo)Xm_bottomRightTileCoord.x -= count; // 更新現(xiàn)右側(cè)瓦片坐標(biāo)X// 刪除從右側(cè)離開視口的圖片for (int row = m_topLeftTileCoord.y; row <= m_bottomRightTileCoord.y; ++row){for (int col = oldRightTileCoordX; col > m_bottomRightTileCoord.x; --col){QGraphicsPixmapItem *item = m_mapItems[row][col];m_scene->removeItem(item);m_mapItems[row].remove(col);delete item;}}}// 2、水平瓦片坐標(biāo)控制:判斷最右側(cè)瓦片是否完全進(jìn)入視口、最左側(cè)瓦片是否完全離開視口if (bottomRightViewPos.x() + 255 < w){int count = qCeil((w - (bottomRightViewPos.x() + 255)) / PIXMAP_SIZE); // 右側(cè)進(jìn)入視口瓦片數(shù)量int oldRightTileCoordX = m_bottomRightTileCoord.x; // 保存原右側(cè)瓦片坐標(biāo)Xm_bottomRightTileCoord.x += count; // 保存現(xiàn)右側(cè)瓦片坐標(biāo)X// 增加從右側(cè)進(jìn)入視口的圖片for (int row = m_topLeftTileCoord.y; row <= m_bottomRightTileCoord.y; ++row){for (int col = m_bottomRightTileCoord.x; col > oldRightTileCoordX; --col){QString fileName = QString("%1/Map_%2-%3.png").arg(dirPath).arg(QString::number(row + 1).rightJustified(2, '0')).arg(QString::number(col + 1).rightJustified(2, '0'));QPixmap pixmap(fileName);QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);item->setPos(PIXMAP_SIZE * col, PIXMAP_SIZE * row);m_scene->addItem(item);m_mapItems[row][col] = item;}}}if (topLeftViewPos.x() + 255 < 0){int count = qFloor(fabs(topLeftViewPos.x()) / PIXMAP_SIZE); // 左側(cè)離開視口瓦片數(shù)量int oldLeftTileCoordX = m_topLeftTileCoord.x; // 保存原左側(cè)瓦片坐標(biāo)Xm_topLeftTileCoord.x += count; // 保存現(xiàn)左側(cè)瓦片坐標(biāo)X// 刪除從左側(cè)離開視口的圖片for (int row = m_topLeftTileCoord.y; row <= m_bottomRightTileCoord.y; ++row){for (int col = oldLeftTileCoordX; col < m_topLeftTileCoord.x; ++col){QGraphicsPixmapItem *item = m_mapItems[row][col];m_scene->removeItem(item);m_mapItems[row].remove(col);delete item;}}}// 3、垂直瓦片坐標(biāo)控制:判斷最上側(cè)瓦片是否完全進(jìn)入視口,最下側(cè)瓦片是否完全離開視口if (topLeftViewPos.y() > 0){int count = qCeil(topLeftViewPos.y() / PIXMAP_SIZE); // 上側(cè)進(jìn)入視口瓦片數(shù)量int oldTopTileCoordY = m_topLeftTileCoord.y; // 保存原上側(cè)瓦片坐標(biāo)Ym_topLeftTileCoord.y -= count; // 保存現(xiàn)上側(cè)瓦片坐標(biāo)Y// 增加從上側(cè)進(jìn)入視口的圖片for (int row = m_topLeftTileCoord.y; row < oldTopTileCoordY; ++row){for (int col = m_topLeftTileCoord.x; col <= m_bottomRightTileCoord.x; ++col){QString fileName = QString("%1/Map_%2-%3.png").arg(dirPath).arg(QString::number(row + 1).rightJustified(2, '0')).arg(QString::number(col + 1).rightJustified(2, '0'));QPixmap pixmap(fileName);QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);item->setPos(PIXMAP_SIZE * col, PIXMAP_SIZE * row);m_scene->addItem(item);m_mapItems[row][col] = item;}}}if (bottomRightViewPos.y() > h){int count = qFloor((bottomRightViewPos.y() - h) / PIXMAP_SIZE) + 1; // 下側(cè)離開視口瓦片數(shù)量int oldBottomTileCoordY = m_bottomRightTileCoord.y; // 保存原下側(cè)瓦片坐標(biāo)Ym_bottomRightTileCoord.y -= count; // 保存現(xiàn)下側(cè)瓦片坐標(biāo)Y// 刪除從下側(cè)離開視口的圖片for (int row = oldBottomTileCoordY; row > m_bottomRightTileCoord.y; --row){for (int col = m_topLeftTileCoord.x; col <= m_bottomRightTileCoord.x; ++col){QGraphicsPixmapItem *item = m_mapItems[row][col];m_scene->removeItem(item);m_mapItems[row].remove(col);delete item;}}}// 4、垂直瓦片坐標(biāo)控制:判斷最下側(cè)瓦片是否完全進(jìn)入視口,最上側(cè)瓦片是否完全離開視口if (bottomRightViewPos.y() + 255 < h){int count = qCeil((h - (bottomRightViewPos.y() + 255)) / PIXMAP_SIZE); // 下側(cè)進(jìn)入視口瓦片數(shù)量int oldBottomTileCoordY = m_bottomRightTileCoord.y; // 保存原下側(cè)瓦片坐標(biāo)Ym_bottomRightTileCoord.y += count; // 保存現(xiàn)下側(cè)瓦片坐標(biāo)Y// 增加從下側(cè)進(jìn)入視口的圖片for (int row = m_bottomRightTileCoord.y; row > oldBottomTileCoordY; --row){for (int col = m_topLeftTileCoord.x; col <= m_bottomRightTileCoord.x; ++col){QString fileName = QString("%1/Map_%2-%3.png").arg(dirPath).arg(QString::number(row + 1).rightJustified(2, '0')).arg(QString::number(col + 1).rightJustified(2, '0'));QPixmap pixmap(fileName);QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);item->setPos(PIXMAP_SIZE * col, PIXMAP_SIZE * row);m_scene->addItem(item);m_mapItems[row][col] = item;}}}if (topLeftViewPos.y() + 255 < 0){int count = qFloor(fabs(topLeftViewPos.y()) / PIXMAP_SIZE); // 上側(cè)離開視口瓦片數(shù)量int oldTopTileCoordY = m_topLeftTileCoord.y; // 保存原上側(cè)瓦片坐標(biāo)Ym_topLeftTileCoord.y += count; // 保存現(xiàn)上側(cè)瓦片坐標(biāo)Y// 刪除從上側(cè)離開視口的圖片for (int row = oldTopTileCoordY; row < m_topLeftTileCoord.y; ++row){for (int col = m_topLeftTileCoord.x; col <= m_bottomRightTileCoord.x; ++col){QGraphicsPixmapItem *item = m_mapItems[row][col];m_scene->removeItem(item);m_mapItems[row].remove(col);delete item;}}}
}