You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nmWTAI-Platform/Src/nmNum/nmPlot/nmObjBase.cpp

168 lines
6.0 KiB
C++

#include <QPainter>
#include "nmObjBase.h"
#include "ZxRenderView.h"
#include <QtCore/qnumeric.h>
ZX_DEFINE_DYNAMIC(nObjBase, nmObjBase)
nmObjBase::nmObjBase()
{
}
nmObjBase::nmObjBase(const QString& sName, \
ZxSubAxisX* pAxisX, \
ZxSubAxisY* pAxisY)
{
}
nmObjBase::~nmObjBase()
{
}
void nmObjBase::paintBack(QPainter* painter, const ZxPaintParam& param)
{
ZxObjBase::paintBack(painter, param);
}
void nmObjBase::afterCreated()
{
}
void nmObjBase::removeData()
{
}
void nmObjBase::paintNodeHandle(QPainter* painter, const QPointF& pt,
int nPixelSize, const QColor& borderColor,
const QColor& fillColor)
{
if (painter == nullptr || nPixelSize < 2) {
return;
}
// 先转换节点中心,再临时取消地图变换;框的大小和线宽不随缩放变化。
// combinedTransform 不包含设备像素比,Qt 继续负责高 DPI 下的逻辑像素映射。
const QPointF screenPos = painter->combinedTransform().map(pt);
if (!qIsFinite(screenPos.x()) || !qIsFinite(screenPos.y())) {
return;
}
painter->save();
painter->resetTransform();
painter->setRenderHint(QPainter::Antialiasing, false);
QPen pen(borderColor);
pen.setWidthF(1.0);
painter->setPen(pen);
painter->setBrush(fillColor.isValid() ? QBrush(fillColor) : QBrush(Qt::NoBrush));
// 关闭抗锯齿时,Qt 已将 1 像素边线落到像素格内,只需对左上角取整。
// 不再额外加半像素,避免栅格化后节点框向右下偏离折线交点。
// 路径宽高扣除边线,使可见外框仍为 nPixelSize 像素。
const qreal left = qRound(screenPos.x() - nPixelSize * 0.5);
const qreal top = qRound(screenPos.y() - nPixelSize * 0.5);
painter->drawRect(QRectF(left, top, nPixelSize - 1, nPixelSize - 1));
painter->restore();
}
void nmObjBase::paintSelectedNodeHandles(QPainter* painter, const QVector<QPointF>& points) const
{
if (!isSelected()) {
return;
}
for (int i = 0; i < points.count(); ++i) {
const bool active = m_oHitOption == OHO_Point && m_nHitIndex == i;
const QColor nodeColor = active ? QColor(Qt::red) : QColor(0, 0, 128);
paintNodeHandle(painter, points[i], 5, nodeColor, active ? nodeColor : QColor());
}
}
bool nmObjBase::hitTestPolylineInPixels(const QPointF& pt, bool closed,
int& nOption, int& nSubIndex,
bool includeSelectedInterior)
{
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;
}
if (includeSelectedInterior && closed && isSelected() && points.count() >= 3) {
// 点和边优先;未选中时不抢占内部其他图元,保留边界的既有选择规则。
QPainterPath path;
for (int i = 0; i < points.count(); ++i) {
if (!qIsFinite(points[i].x()) || !qIsFinite(points[i].y())) return false;
if (i == 0) path.moveTo(points[i]);
else path.lineTo(points[i]);
}
path.closeSubpath();
if (path.contains(mousePos)) {
nOption = OHO_Inner;
nSubIndex = 0;
return true;
}
}
return false;
}