268 lines
7.3 KiB
C++
268 lines
7.3 KiB
C++
#include "MapLayerStore.h"
|
||
#include "MapLayer.h"
|
||
#include "RasterImageLayer.h"
|
||
#include <QFileInfo>
|
||
#include <QWidget>
|
||
|
||
MapLayerStore::MapLayerStore(QObject* parent)
|
||
: QObject(parent)
|
||
{
|
||
}
|
||
|
||
void MapLayerStore::addLayer(MapLayer* layer)
|
||
{
|
||
if (!layer) return;
|
||
|
||
int index = (int)m_layers.size();
|
||
LayerEntry entry;
|
||
entry.mapLayer.reset(layer);
|
||
|
||
m_layers.push_back(std::move(entry));
|
||
m_mapLayerIndex[layer] = index;
|
||
|
||
emit layerAdded(layer);
|
||
}
|
||
|
||
void MapLayerStore::addImageLayer(MapLayer* mapLayer, RasterImageLayer* imageLayer, QWidget* widget)
|
||
{
|
||
if (!mapLayer || !imageLayer) return;
|
||
|
||
LayerEntry* entry = findLayerEntry(mapLayer);
|
||
if (!entry) return;
|
||
|
||
entry->imageLayers.push_back({std::unique_ptr<RasterImageLayer>(imageLayer), widget});
|
||
emit imageLayerAdded(imageLayer);
|
||
}
|
||
|
||
void MapLayerStore::removeLayer(MapLayer* layer)
|
||
{
|
||
if (!layer) return;
|
||
|
||
auto it = m_mapLayerIndex.find(layer);
|
||
if (it == m_mapLayerIndex.end()) return;
|
||
|
||
int index = it->second;
|
||
if (index < 0 || index >= (int)m_layers.size()) return;
|
||
|
||
LayerEntry& entry = m_layers[index];
|
||
|
||
// Delete all associated widgets
|
||
for (auto& imgEntry : entry.imageLayers) {
|
||
if (imgEntry.widget) {
|
||
delete imgEntry.widget;
|
||
}
|
||
}
|
||
entry.imageLayers.clear();
|
||
|
||
emit layerAboutToBeRemoved(layer);
|
||
|
||
m_layers.erase(m_layers.begin() + index);//?????????????????????
|
||
m_mapLayerIndex.erase(it);
|
||
|
||
// Rebuild index for layers after removed one
|
||
for (int i = index; i < (int)m_layers.size(); ++i) {
|
||
m_mapLayerIndex[m_layers[i].mapLayer.get()] = i;
|
||
}
|
||
}
|
||
|
||
void MapLayerStore::removeImageLayer(RasterImageLayer* imageLayer)//可以优化:RasterImageLayer包含一个指向所属MapLayer的指针,这样就不需要遍历所有图层来找到它了
|
||
{
|
||
if (!imageLayer) return;
|
||
|
||
for (auto& entry : m_layers) {
|
||
for (auto it = entry.imageLayers.begin(); it != entry.imageLayers.end(); ++it) {
|
||
if (it->imageLayer.get() == imageLayer) {
|
||
emit imageLayerAboutToBeRemoved(imageLayer);
|
||
if (it->widget) {
|
||
//delete it->widget;//在调用此函数的地方已经删除了widget了,这里不需要再删除一次了
|
||
}
|
||
entry.imageLayers.erase(it);
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
void MapLayerStore::removeLayerByName(const QString& name)
|
||
{
|
||
for (auto& entry : m_layers) {
|
||
if (entry.mapLayer->name() == name) {
|
||
removeLayer(entry.mapLayer.get());
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
|
||
bool MapLayerStore::containsLayer(const QString& url, bool isAbsolutePath) const
|
||
{
|
||
QFileInfo fi(url);
|
||
QString fileName = fi.completeBaseName();
|
||
|
||
if (!isAbsolutePath) {
|
||
return getLayer(fileName) != nullptr;
|
||
}
|
||
|
||
for (const auto& entry : m_layers) {
|
||
if (entry.mapLayer->dataPath() == url)
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
MapLayer* MapLayerStore::getLayer(const QString& name) const
|
||
{
|
||
for (const auto& entry : m_layers) {
|
||
if (entry.mapLayer->name() == name)
|
||
return entry.mapLayer.get();
|
||
}
|
||
return nullptr;
|
||
}
|
||
|
||
MapLayer* MapLayerStore::getLayerAt(int index) const
|
||
{
|
||
if (index < 0 || index >= (int)m_layers.size())
|
||
return nullptr;
|
||
return m_layers[index].mapLayer.get();
|
||
}
|
||
|
||
MapLayer* MapLayerStore::getLayerByAbsolutePath(const QString& absolutePath) const
|
||
{
|
||
for (const auto& entry : m_layers) {
|
||
if (entry.mapLayer->dataPath() == absolutePath)
|
||
return entry.mapLayer.get();
|
||
}
|
||
return nullptr;
|
||
}
|
||
|
||
int MapLayerStore::layerCount() const
|
||
{
|
||
return (int)m_layers.size();
|
||
}
|
||
|
||
RasterImageLayer* MapLayerStore::getImageLayer(MapLayer* mapLayer, int index) const
|
||
{
|
||
const LayerEntry* entry = findLayerEntry(mapLayer);
|
||
if (!entry) return nullptr;
|
||
|
||
if (index < 0 || index >= (int)entry->imageLayers.size())
|
||
return nullptr;
|
||
|
||
return entry->imageLayers[index].imageLayer.get();
|
||
}
|
||
|
||
int MapLayerStore::imageLayerCount(MapLayer* mapLayer) const
|
||
{
|
||
const LayerEntry* entry = findLayerEntry(mapLayer);
|
||
if (!entry) return 0;
|
||
return (int)entry->imageLayers.size();
|
||
}
|
||
|
||
std::vector<RasterImageLayer*> MapLayerStore::getAllImageLayers() const
|
||
{
|
||
std::vector<RasterImageLayer*> result;
|
||
for (const auto& entry : m_layers) {
|
||
for (const auto& imgEntry : entry.imageLayers) {
|
||
if (imgEntry.imageLayer)
|
||
result.push_back(imgEntry.imageLayer.get());
|
||
}
|
||
}
|
||
return result;
|
||
}
|
||
|
||
QWidget* MapLayerStore::widgetForImageLayer(RasterImageLayer* imageLayer) const
|
||
{
|
||
for (const auto& entry : m_layers) {
|
||
for (const auto& imgEntry : entry.imageLayers) {
|
||
if (imgEntry.imageLayer.get() == imageLayer)
|
||
return imgEntry.widget;
|
||
}
|
||
}
|
||
return nullptr;
|
||
}
|
||
|
||
QWidget* MapLayerStore::widgetForLayer(MapLayer* layer) const//需要修改??????????????????????????????????????????????
|
||
{
|
||
const LayerEntry* entry = findLayerEntry(layer);
|
||
if (!entry || entry->imageLayers.empty())
|
||
return nullptr;
|
||
return entry->imageLayers.front().widget;
|
||
}
|
||
|
||
QWidget* MapLayerStore::widgetForLayer(const QString& absolutePath) const
|
||
{
|
||
for (const auto& entry : m_layers) {
|
||
if (entry.mapLayer->dataPath() == absolutePath) {
|
||
if (!entry.imageLayers.empty())
|
||
return entry.imageLayers.front().widget;
|
||
return nullptr;
|
||
}
|
||
}
|
||
return nullptr;
|
||
}
|
||
|
||
std::vector<QWidget*> MapLayerStore::widgetsForMapLayer(MapLayer* mapLayer) const
|
||
{
|
||
std::vector<QWidget*> result;
|
||
const LayerEntry* entry = findLayerEntry(mapLayer);
|
||
if (!entry) return result;
|
||
|
||
for (const auto& imgEntry : entry->imageLayers) {
|
||
if (imgEntry.widget)
|
||
result.push_back(imgEntry.widget);
|
||
}
|
||
return result;
|
||
}
|
||
|
||
MapLayer* MapLayerStore::mapLayerForImageLayer(RasterImageLayer* imageLayer) const
|
||
{
|
||
for (const auto& entry : m_layers) {
|
||
for (const auto& imgEntry : entry.imageLayers) {
|
||
if (imgEntry.imageLayer.get() == imageLayer)
|
||
return entry.mapLayer.get();
|
||
}
|
||
}
|
||
return nullptr;
|
||
}
|
||
|
||
MapLayer* MapLayerStore::mapLayerForWidget(QWidget* widget) const
|
||
{
|
||
if (!widget) return nullptr;
|
||
for (const auto& entry : m_layers) {
|
||
for (const auto& imgEntry : entry.imageLayers) {
|
||
if (imgEntry.widget == widget)
|
||
return entry.mapLayer.get();
|
||
}
|
||
}
|
||
return nullptr;
|
||
}
|
||
|
||
RasterImageLayer* MapLayerStore::imageLayerForWidget(QWidget* widget) const
|
||
{
|
||
if (!widget) return nullptr;
|
||
for (const auto& entry : m_layers) {
|
||
for (const auto& imgEntry : entry.imageLayers) {
|
||
if (imgEntry.widget == widget)
|
||
return imgEntry.imageLayer.get();
|
||
}
|
||
}
|
||
return nullptr;
|
||
}
|
||
|
||
MapLayerStore::LayerEntry* MapLayerStore::findLayerEntry(MapLayer* mapLayer)
|
||
{
|
||
auto it = m_mapLayerIndex.find(mapLayer);
|
||
if (it == m_mapLayerIndex.end()) return nullptr;
|
||
int index = it->second;
|
||
if (index < 0 || index >= (int)m_layers.size()) return nullptr;
|
||
return &m_layers[index];
|
||
}
|
||
|
||
const MapLayerStore::LayerEntry* MapLayerStore::findLayerEntry(MapLayer* mapLayer) const
|
||
{
|
||
auto it = m_mapLayerIndex.find(mapLayer);
|
||
if (it == m_mapLayerIndex.end()) return nullptr;
|
||
int index = it->second;
|
||
if (index < 0 || index >= (int)m_layers.size()) return nullptr;
|
||
return &m_layers[index];
|
||
}
|