Manual-Labeling-Tool/Manual-Labeling-Client/Manual-Label-Tool-Widget/windLayerTreeViewMenuProvid...

132 lines
4.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "windLayerTreeViewMenuProvider.h"
#include "qgslayertreeviewdefaultactions.h"
#include <QMenu>
#include <QAction>
#include "qgslayertreenode.h"
#include "qgslayertree.h"
#include "qgsvectorlayer.h"
#include "qgsmapcanvas.h"
#include "qgsrasterlayerproperties.h"
#include "qgsgui.h"
#include "qgsproviderguiregistry.h"
#include "qgsmeshlayerproperties.h"
windLayerTreeViewMenuProvider::windLayerTreeViewMenuProvider(QgsLayerTreeView* view, QgsMapCanvas* canvas, QObject* parent)
: QObject(parent)
, mLayerTreeView(view)
, mMapCanvas(canvas)
{
}
QMenu* windLayerTreeViewMenuProvider::createContextMenu()
{
// 创建空的菜单
QMenu* menu = new QMenu();
// 获取图层树视图的默认操作集合,它包含了许多常用的菜单项实现
QgsLayerTreeViewDefaultActions* actions = mLayerTreeView->defaultActions();
// 获取当前鼠标位置对应的模型索引
QModelIndex index = mLayerTreeView->currentIndex();
// 情况1在空白处右键未选中任何节点
if (!index.isValid()) {
menu->addAction(actions->actionAddGroup(menu));
menu->addAction(tr("&Expand All"), mLayerTreeView, &QgsLayerTreeView::expandAll);
menu->addAction(tr("&Collapse All"), mLayerTreeView, &QgsLayerTreeView::collapseAll);
return menu;
}
// 将索引转换为图层树节点
QgsLayerTreeNode* node = mLayerTreeView->index2node(index);
if (!node) {
return menu; // 如果转换失败,返回空菜单
}
// 情况2右键点击的是图层组
if (QgsLayerTree::isGroup(node)) {
menu->addAction(actions->actionZoomToGroup(mMapCanvas, menu));
menu->addAction(actions->actionRemoveGroupOrLayer(menu));
menu->addAction(actions->actionRenameGroupOrLayer(menu));
}
else if (QgsLayerTree::isLayer(node)) {
menu->addAction(actions->actionZoomToLayers(mMapCanvas, menu));
menu->addAction(actions->actionZoomToSelection(mMapCanvas, menu));
menu->addAction(actions->actionRenameGroupOrLayer(menu));
menu->addAction(actions->actionShowInOverview(menu));
menu->addAction(actions->actionRemoveGroupOrLayer(menu));
QgsMapLayer* mapLayer = QgsLayerTree::toLayer(node)->layer();
if (mapLayer) {
// 添加"缩放到图层"动作
menu->addAction(actions->actionZoomToLayers(mMapCanvas, menu));
// 添加"删除图层"和"重命名图层"动作
menu->addAction(actions->actionRemoveGroupOrLayer(menu));
menu->addAction(actions->actionRenameGroupOrLayer(menu));
menu->addSeparator();
if (mapLayer->type() == Qgis::LayerType::Raster) {
QAction* rasterLayerPropertyAction = menu->addAction(u8"属性");
QObject::connect(rasterLayerPropertyAction, &QAction::triggered, this, &windLayerTreeViewMenuProvider::showlayerpropertyDialog);
}
else if (mapLayer->type() == Qgis::LayerType::Mesh) {
QAction* meshLayerPropertyAction = menu->addAction(u8"属性");
QObject::connect(meshLayerPropertyAction, &QAction::triggered, this, &windLayerTreeViewMenuProvider::showlayerpropertyDialog);
}
}
}
return menu; // 返回构建好的菜单
}
void windLayerTreeViewMenuProvider::showlayerpropertyDialog(bool flag)
{
QgsMapLayer* mapLayer = mLayerTreeView->currentLayer();
if (!mapLayer) {
return;
}
if (mapLayer->type()==Qgis::LayerType::Raster) {
// collect factories from registered data providers
QList<const QgsMapLayerConfigWidgetFactory*> providerFactories = QgsGui::providerGuiRegistry()->mapLayerConfigWidgetFactories(mapLayer);
QgsRasterLayerProperties* rasterLayerPropertiesDialog = new QgsRasterLayerProperties(mapLayer, mMapCanvas, mMapCanvas);
for (const QgsMapLayerConfigWidgetFactory* factory : std::as_const(providerFactories))
{
rasterLayerPropertiesDialog->addPropertiesPageFactory(factory);
}
rasterLayerPropertiesDialog->restoreLastPage();
rasterLayerPropertiesDialog->setModal(true);
rasterLayerPropertiesDialog->show();
connect(rasterLayerPropertiesDialog, &QgsRasterLayerProperties::accepted, [rasterLayerPropertiesDialog] {
rasterLayerPropertiesDialog->deleteLater();
});
connect(rasterLayerPropertiesDialog, &QgsRasterLayerProperties::rejected, [rasterLayerPropertiesDialog] {
rasterLayerPropertiesDialog->deleteLater();
});
}
else if (mapLayer->type() == Qgis::LayerType::Mesh) {
QList<const QgsMapLayerConfigWidgetFactory*> providerFactories = QgsGui::providerGuiRegistry()->mapLayerConfigWidgetFactories(mapLayer);
QgsMeshLayerProperties meshLayerPropertiesDialog(mapLayer, mMapCanvas, mMapCanvas);
for (const QgsMapLayerConfigWidgetFactory* factory : std::as_const(providerFactories))
{
meshLayerPropertiesDialog.addPropertiesPageFactory(factory);
}
meshLayerPropertiesDialog.restoreLastPage();
meshLayerPropertiesDialog.exec();
}
}