#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);
}
zhangiser
- 粉丝: 172
- 资源: 12
最新资源
- C语言-leetcode题解之26-remove-duplicates-from-sorted-array.c
- C语言-leetcode题解之24-swap-nodes-in-pairs.c
- C语言-leetcode题解之22-generate-parentheses.c
- C语言-leetcode题解之21-merge-two-sorted-lists.c
- java-leetcode题解之Online Stock Span.java
- java-leetcode题解之Online Majority Element In Subarray.java
- java-leetcode题解之Odd Even Jump.java
- 计算机毕业设计:python+爬虫+cnki网站爬
- nyakumi-lewd-snack-3-4k_720p.7z.002
- 现在微信小程序能用的mqtt.min.js
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈