|
|
|
|
@ -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;
|
|
|
|
|
}
|
|
|
|
|
|