tab切换,改变LayerTreeModel的setCurrentIndex

This commit is contained in:
tangchao0503
2026-03-16 15:06:30 +08:00
parent 7473a45f41
commit 30fa211a22
3 changed files with 47 additions and 0 deletions

View File

@ -1334,6 +1334,41 @@ void HPPA::onTabWidgetCurrentChanged(int index)//代码新建一个tab会调
{
canvas->setMapTool(activeTool);
}
// Sync layer tree view selection with the current tab
if (m_MapLayerStore && m_layerTreeView && m_LayerTreeModel && m_RasterGroup)
{
MapLayer* layer = m_MapLayerStore->layerForWidget(currentWidget);
if (layer)
{
// Find the LayerTreeLayer node in m_RasterGroup that matches this layer
for (int i = 0; i < m_RasterGroup->childCount(); ++i)
{
LayerTreeNode* child = m_RasterGroup->childAt(i);
if (child && child->type() == LayerTreeNode::Type::Layer)
{
auto* layerNode = static_cast<LayerTreeLayer*>(child);
if (layerNode->mapLayer() == layer)
{
QModelIndex nodeIndex = m_LayerTreeModel->index(i, 0,
m_LayerTreeModel->index(m_RasterGroup->rowInParent(), 0));
// Block signals to avoid infinite loop with onLayerTreeSelectionChanged
m_layerTreeView->selectionModel()->blockSignals(true);
m_layerTreeView->setCurrentIndex(nodeIndex);
m_layerTreeView->selectionModel()->blockSignals(false);
// Manually update ImageControl since we blocked the signal
RasterLayer* rl = qobject_cast<RasterLayer*>(layer);
if (rl)
{
m_ic->setActiveLayer(rl);
}
break;
}
}
}
}
}
}
void HPPA::onActionOpenDirectory()

View File

@ -81,3 +81,12 @@ QWidget* MapLayerStore::widgetForLayer(const QString& absolutePath) const
}
return nullptr;
}
MapLayer* MapLayerStore::layerForWidget(QWidget* widget) const
{
if (!widget) return nullptr;
for (const auto& kv : m_layerWidgets) {
if (kv.second == widget) return kv.first;
}
return nullptr;
}

View File

@ -35,6 +35,9 @@ public slots:
// Get associated widget by layer absolute data path
QWidget* widgetForLayer(const QString& absolutePath) const;
// Reverse lookup: find the MapLayer associated with a given widget (or nullptr)
MapLayer* layerForWidget(QWidget* widget) const;
signals:
void layerAdded(MapLayer* layer);
// Emitted just before the layer is destroyed/removed from store