fix(nmPlot): 统一节点和线段的鼠标命中范围

- 按逻辑像素统一节点 5 像素、线段 4 像素的命中容差
- 优先命中最近节点,缩放后保持点击范围一致
- 裂缝、断层和区域共用命中逻辑,覆盖悬停、拖动及双击加点
- 保留开放折线与区域闭合边规则,处理重复端点
feature/fix-result-persistence-20261118
lh 1 week ago
parent 5f2e24ecb8
commit cf92630199

@ -35,6 +35,13 @@ public:
protected:
// 裂缝、断层和区域共用的纯命中查询:pt 为场景坐标,点集读取当前对象。
// 在所属视图内按逻辑像素判断:节点半径 5,线段距离 4,节点优先且取最近项。
// closed 决定是否包含末点到首点的闭合边;命中线段时返回其起点下标。
// 只填写输出参数,不修改点集或选中状态;未命中时类型和下标均为 -1。
bool hitTestPolylineInPixels(const QPointF& pt, bool closed,
int& nOption, int& nSubIndex);
// 选中节点统一为 5 像素蓝色空心框;当前命中节点保留红色实心提示。
void paintSelectedNodeHandles(QPainter* painter, const QVector<QPointF>& points) const;
};

@ -1,6 +1,7 @@
#include <QPainter>
#include "nmObjBase.h"
#include "ZxRenderView.h"
#include <QtCore/qnumeric.h>
ZX_DEFINE_DYNAMIC(nObjBase, nmObjBase)
@ -75,3 +76,76 @@ void nmObjBase::paintSelectedNodeHandles(QPainter* painter, const QVector<QPoint
paintNodeHandle(painter, points[i], 5, nodeColor, active ? nodeColor : QColor());
}
}
bool nmObjBase::hitTestPolylineInPixels(const QPointF& pt, bool closed,
int& nOption, int& nSubIndex)
{
nOption = -1;
nSubIndex = -1;
// 保留框架对对象可交互状态的检查,几何命中范围由下面的像素距离统一决定。
if (!ZxObjBase::_runHitTest(pt, nOption, nSubIndex) ||
m_pAxisX == nullptr || m_pAxisY == nullptr) {
return false;
}
ZxRenderView* view = getView();
if (view == nullptr) {
return false;
}
// 使用对象所属地图的变换,不依赖全局活动视图;缩放、平移后直接查询最新位置。
const QPointF mousePos = view->mapFromScene(pt);
if (!qIsFinite(mousePos.x()) || !qIsFinite(mousePos.y())) {
return false;
}
QVector<QPointF> points = getPosOf(m_vecPoints);
const qreal nodeRadius = 5.0;
const qreal segmentRadius = 4.0;
qreal nearestDistanceSquared = nodeRadius * nodeRadius;
int nearestIndex = -1;
for (int i = 0; i < points.count(); ++i) {
points[i] = view->mapFromScene(points[i]);
const QPointF delta = mousePos - points[i];
const qreal distanceSquared = delta.x() * delta.x() + delta.y() * delta.y();
if (qIsFinite(distanceSquared) && distanceSquared <= nearestDistanceSquared &&
(nearestIndex < 0 || distanceSquared < nearestDistanceSquared)) {
nearestIndex = i;
nearestDistanceSquared = distanceSquared;
}
}
if (nearestIndex >= 0) {
nOption = OHO_Point;
nSubIndex = nearestIndex;
return true;
}
// 节点全部未命中后才判断线段;闭合区域也只命中边线,不把内部填充作为拖动区。
// 相等距离保留首项,靠近多个节点或交叉线段时不会随遍历覆盖而改变结果。
if (points.count() < 2) {
return false;
}
nearestDistanceSquared = segmentRadius * segmentRadius;
const int segmentCount = closed ? points.count() : points.count() - 1;
zxGmyBase geometry;
for (int i = 0; i < segmentCount; ++i) {
const QPointF& start = points[i];
const QPointF& end = points[(i + 1) % points.count()];
if (!qIsFinite(start.x()) || !qIsFinite(start.y()) ||
!qIsFinite(end.x()) || !qIsFinite(end.y()) || start == end) {
// 重复端点已参与上面的节点命中,零长度线段不再参与投影计算。
continue;
}
double ratio = 0.0;
const qreal distanceSquared = geometry.pointSegmentDistanceSquared2D(mousePos, start, end, ratio);
if (qIsFinite(distanceSquared) && distanceSquared <= nearestDistanceSquared &&
(nearestIndex < 0 || distanceSquared < nearestDistanceSquared)) {
nearestIndex = i;
nearestDistanceSquared = distanceSquared;
}
}
if (nearestIndex >= 0) {
nOption = OHO_Bound;
nSubIndex = nearestIndex;
return true;
}
return false;
}

@ -74,55 +74,8 @@ bool nmObjLineCrack::hitTest(const QPointF& pt)
bool nmObjLineCrack::_runHitTest(const QPointF& pt, int& nOption, int& nSubIndex)
{
if(!nmObjBase::_runHitTest(pt, nOption, nSubIndex)) {
return false;
}
nOption = -1;
nSubIndex = 0;
if(NULL == m_pAxisX || NULL == m_pAxisY) {
return false;
}
QVector<QPointF> pts = getPosOf(m_vecPoints);
// 点
double r = 1.f;
for(int i = 0; i < pts.count(); i++) {
QPointF ptTopLeft = QPointF(pts[i].x() - r,
pts[i].y() - r);
QSizeF sz = QSizeF(r * 2.f, r * 2.f);
QRectF rt = QRectF(ptTopLeft, sz);
bool b = rt.contains(pt);
if(b) {
nOption = (int)OHO_Point;
nSubIndex = i;
return true;
}
}
// 裂缝是开放折线,只检测相邻节点间的可见线段,不连接末点与首点。
for(int i = 0; i + 1 < pts.count(); i++) {
int n1 = i;
int n2 = i + 1;
if(_isNearLine(pt, pts[n1], pts[n2], 1.f * 2)) {
// 命中检测只返回线段位置,加点由工具的双击事件处理。
nOption = (int)OHO_Bound;
nSubIndex = i;
return true;
}
}
nOption = (int)OHO_None;
nSubIndex = -1;
return false;
// 裂缝按开放折线命中,不连接首尾;悬停、按下和双击加点共用此结果。
return hitTestPolylineInPixels(pt, false, nOption, nSubIndex);
}
bool nmObjLineCrack::runMove(const QPointF& pt1,

@ -87,52 +87,10 @@ bool nmObjLineFault::hitTest(const QPointF& pt) {
return nmObjBase::hitTest(pt);
}
bool nmObjLineFault::_runHitTest(const QPointF& pt, int& nOption, int& nSubIndex) {
if(!nmObjBase::_runHitTest(pt, nOption, nSubIndex)) {
return false;
}
nOption = -1;
nSubIndex = 0;
if(NULL == m_pAxisX || NULL == m_pAxisY) {
return false;
}
QVector<QPointF> pts = getPosOf(m_vecPoints);
// 点
double r = 1.f;
for(int i = 0; i < pts.count(); i++) {
QPointF ptTopLeft = QPointF(pts[i].x() - r,
pts[i].y() - r);
QSizeF sz = QSizeF(r * 2.f, r * 2.f);
QRectF rt = QRectF(ptTopLeft, sz);
bool b = rt.contains(pt);
if(b) {
nOption = (int)OHO_Point;
nSubIndex = i;
return true;
}
}
// 断层是开放折线,只检测相邻节点间的可见线段,不连接末点与首点。
for(int i = 0; i + 1 < pts.count(); i++) {
int n1 = i;
int n2 = i + 1;
if(_isNearLine(pt, pts[n1], pts[n2], 1.f * 2)) {
// 命中检测只返回线段位置,加点由工具的双击事件处理。
nOption = (int)OHO_Bound;
nSubIndex = i;
return true;
}
}
return false;
bool nmObjLineFault::_runHitTest(const QPointF& pt, int& nOption, int& nSubIndex)
{
// 断层按开放折线命中,不连接首尾;悬停、按下和双击加点共用此结果。
return hitTestPolylineInPixels(pt, false, nOption, nSubIndex);
}
bool nmObjLineFault::runMove(const QPointF& pt1,

@ -80,55 +80,8 @@ bool nmObjRegion::hitTest(const QPointF& pt)
bool nmObjRegion::_runHitTest(const QPointF& pt, int& nOption, int& nSubIndex)
{
if(!nmObjBase::_runHitTest(pt, nOption, nSubIndex)) {
return false;
}
nOption = -1;
nSubIndex = 0;
if(NULL == m_pAxisX || NULL == m_pAxisY) {
return false;
}
QVector<QPointF> pts = getPosOf(m_vecPoints);
// 点
double r = 1.f;
for(int i = 0; i < pts.count(); i++) {
QPointF ptTopLeft = QPointF(pts[i].x() - r,
pts[i].y() - r);
QSizeF sz = QSizeF(r * 2.f, r * 2.f);
QRectF rt = QRectF(ptTopLeft, sz);
bool b = rt.contains(pt);
if(b) {
nOption = (int)OHO_Point;
nSubIndex = i;
return true;
}
}
// 线
for(int i = 0; i < pts.count(); i++) {
int n1 = i;
int n2 = (i == pts.count() - 1 ? 0 : i + 1);
if(_isNearLine(pt, pts[n1], pts[n2], 1.f * 2)) {
// 命中检测只返回线段位置,加点由工具的双击事件处理。
nOption = (int)OHO_Bound;
nSubIndex = i;
return true;
}
}
nOption = (int)OHO_None;
nSubIndex = -1;
return false;
// 区域包含末点到首点的闭合边,内部填充不参与节点或边线命中。
return hitTestPolylineInPixels(pt, true, nOption, nSubIndex);
}
bool nmObjRegion::runMove(const QPointF& pt1,

Loading…
Cancel
Save