#include "QgsMapToolIdentifyBox.h"
#include <qgsvertexmarker.h>
#include <qgssymbollayer.h>
#include "qgsapplication.h"
#include "QgsMessageLog.h"
QgsMapToolIdentifyAllFeaturesEX::QgsMapToolIdentifyAllFeaturesEX(QgsMapCanvas* canvas)
: QgsMapToolIdentify(canvas)
{
mToolName = tr("Identify All Features");
// set cursor
mCursor = QCursor(Qt::CrossCursor);
}
void QgsMapToolIdentifyAllFeaturesEX::canvasReleaseEvent(QgsMapMouseEvent* e)
{
QPoint point = e->pos() - mInitDragPos;
//点
if (!mSelectionActive || (point.manhattanLength() < QApplication::startDragDistance()))
{
mSelectionActive = false;
mSelectGeometry = QgsGeometry::fromPointXY(toMapCoordinates(e->pos()));
identifyFromGeometry();
}
//矩形
if (mpRubberBand && mSelectionActive)
{
mSelectGeometry = mpRubberBand->asGeometry();
mpRubberBand.reset();
identifyFromGeometry();
}
mSelectionActive = false;
}
void QgsMapToolIdentifyAllFeaturesEX::identifyFromGeometry()
{
if (mCanvas)
{
mCanvas->setSelectionColor(Qt::red);//设置颜色
QList< QgsMapLayer* > layers = mCanvas->layers();
foreach(QgsMapLayer * l, layers)
{
QgsVectorLayer* l1 = qobject_cast<QgsVectorLayer*>(l);
l1->removeSelection();
}
}
//返回选中的结果
QDateTime start = QDateTime::currentDateTime();
QList<IdentifyResult> results = QgsMapToolIdentify::identify(mSelectGeometry, m_mod, LayerType::enum_type::AllLayers);
QDateTime end = QDateTime::currentDateTime();
//QgsMessageLog::logMessage("耗时:"+QString::number((end - start).seconds()), "Messages");
//QList<IdentifyResult> results = QgsMapToolIdentify::identify(mSelectGeometry, m_mod, LayerType::enum_type::AllLayers);
//选择的Features集合
QMap<QString, QList<QgsFeature>> maplist;
//用于显示
QMap<QString,QgsFeatureIds> ids;
for (IdentifyResult var : results)
{
QgsFeature _Feature = var.mFeature;
if (ids.contains(var.mLayer->id())) {
QgsFeatureIds ids1 = ids.value(var.mLayer->id());
ids1.insert(_Feature.id());
ids.insert(var.mLayer->id(), ids1);
}
else {
QgsFeatureIds ids1;
ids1.insert(_Feature.id());
ids.insert(var.mLayer->id(), ids1);
}
}
for (int i = 0; i < results.count(); ++i)
{
QgsVectorLayer* layer = qobject_cast<QgsVectorLayer*>(results.at(i).mLayer);
//只针对开启编辑图层
/*if (!layer->isEditable()) {
continue;
}*/
if (ids.count() > 0)
layer->selectByIds(ids.value(layer->id()));
QgsFeature feature = results.at(i).mFeature;
if (maplist.contains(layer->id())) {
QList<QgsFeature> sfeatures = maplist.value(layer->id());
sfeatures.append(feature);
maplist.insert(layer->id(), sfeatures);
}
else {
QList<QgsFeature> selectFeatures;
selectFeatures.append(feature);
maplist.insert(layer->id(), selectFeatures);
}
}
//发出选中的Feature信息信号
emit AllfeatureIdentified(maplist);
}
void QgsMapToolIdentifyAllFeaturesEX::clearRubberBand()
{
}
bool QgsMapToolIdentifyAllFeaturesEX::init()
{
open();
return true;
}
void QgsMapToolIdentifyAllFeaturesEX::setMode(IdentifyMode mode)
{
m_mod = mode;
}
void QgsMapToolIdentifyAllFeaturesEX::keyPressEvent(QKeyEvent* e)
{
if (e->key() == Qt::Key_Escape)
{
close();
mCanvas->unsetMapTool(this);
clearRubberBand();
}
emit keyPressEventEx(e);
}
void QgsMapToolIdentifyAllFeaturesEX::keyReleaseEvent(QKeyEvent* e)
{
emit keyReleaseEventEX(e);
}
void QgsMapToolIdentifyAllFeaturesEX::canvasPressEvent(QgsMapMouseEvent* e)
{
if (!mpRubberBand) {
mpRubberBand.reset(new QgsRubberBand(mCanvas, Qgis::GeometryType::Polygon));
/*mpRubberBand->setFillColor(mFillColor);
mpRubberBand->setStrokeColor(mStrokeColor);*/
}
mInitDragPos = e->pos();
}
void QgsMapToolIdentifyAllFeaturesEX::canvasMoveEvent(QgsMapMouseEvent* e)
{
if (e->buttons() != Qt::LeftButton)
return;
QRect rect;
if (!mSelectionActive)
{
mSelectionActive = true;
rect = QRect(e->pos(), e->pos());
}
else
{
rect = QRect(e->pos(), mInitDragPos);
}
if (mpRubberBand)
mpRubberBand->setToCanvasRectangle(rect);
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
QgisDrawTool.rar (8个子文件)
QgisDrawTool
SVInterface
ITool.h 347B
ICommand.h 211B
ITool.cpp 20B
ICommand.cpp 77B
CommonTool.cpp 3KB
QgsMapToolIdentifyBox.h 1KB
CommonTool.h 1KB
QgsMapToolIdentifyBox.cpp 4KB
共 8 条
- 1
资源评论
zhangiser
- 粉丝: 173
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功