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

152 lines
5.3 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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)
{
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;
}