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/nmObjRect.cpp

265 lines
8.1 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 <QPainterPath>
#include "ZxSerializer.h"
#include "IxPtyPano.h"
#include "ZxSubAxisX.h"
#include "ZxSubAxisY.h"
#include "nmObjRectTool.h"
#include "nmObjRect.h"
#include <QtCore/qnumeric.h>
ZX_DEFINE_DYNAMIC(nObjRect, nmObjRect)
nmObjRect::nmObjRect() {
m_sObjTag = "nObjRect";
nmObjRect::init("", NULL, NULL);
}
nmObjRect::nmObjRect(const QString& sName, \
ZxSubAxisX* pAxisX, \
ZxSubAxisY* pAxisY) {
m_sObjTag = "nObjRect";
nmObjRect::init(sName, pAxisX, pAxisY);
}
nmObjRect::~nmObjRect() {
}
// 初始化矩形对象对应的笔和填充颜色
void nmObjRect::init(const QString& sName, \
ZxSubAxisX* pAxisX, \
ZxSubAxisY* pAxisY) {
nmObjBase::init(sName, pAxisX, pAxisY);
nmObjRect::initFlags();
m_oPen = QPen(QBrush(QColor(0, 0, 0)), \
0.5f, Qt::SolidLine);
m_clrBackgrd = QColor(255, 170, 255, 100);
loadTempl();
}
void nmObjRect::initTools() {
m_pTool = new nmObjRectTool();
nmObjBase::initTools();
}
void nmObjRect::initFlags() {
setLockPos(false);
setLockSize(false);
setReadOnly(false);
}
bool nmObjRect::hitTest(const QPointF& pt) {
return nmObjBase::hitTest(pt);
}
bool nmObjRect::_runHitTest(const QPointF& pt, int& nOption, int& nSubIndex) {
nOption = -1;
nSubIndex = -1;
if (m_vecPoints.count() != 4) return false;
// 统一返回 OHO_Point/OHO_Bound/OHO_Inner,框架及公共工具使用同一份命中结果。
return hitTestPolylineInPixels(pt, true, nOption, nSubIndex, true);
}
bool nmObjRect::calculateCreationPoints(const QPointF& first, const QPointF& second,
QVector<QPointF>& points) {
points.clear();
if (!qIsFinite(first.x()) || !qIsFinite(first.y()) ||
!qIsFinite(second.x()) || !qIsFinite(second.y()) ||
first.x() == second.x() || first.y() == second.y()) return false;
points << first << QPointF(second.x(), first.y())
<< second << QPointF(first.x(), second.y());
return true;
}
RectBoundPos nmObjRect::rectHitPosition(int hitOption, int hitIndex) const {
if (hitOption == OHO_Inner) return RBP_Inner;
if (m_pAxisX == nullptr || m_pAxisY == nullptr || m_vecPoints.count() != 4 ||
hitIndex < 0 || hitIndex >= 4) return RBP_Unknown;
const QVector<QPointF> points = getAllPos();
const QPointF& point = points[hitIndex];
const QPointF& opposite = points[(hitIndex + 2) % 4];
if (hitOption == OHO_Point) {
if (point.x() == opposite.x() || point.y() == opposite.y()) return RBP_Unknown;
if (point.x() < opposite.x()) {
return point.y() < opposite.y() ? RBP_LeftTop : RBP_LeftBottom;
}
return point.y() < opposite.y() ? RBP_RightTop : RBP_RightBottom;
}
if (hitOption == OHO_Bound) {
const QPointF& next = points[(hitIndex + 1) % 4];
if (point.x() == next.x()) return point.x() < opposite.x() ? RBP_Left : RBP_Right;
if (point.y() == next.y()) return point.y() < opposite.y() ? RBP_Top : RBP_Bottom;
}
return RBP_Unknown;
}
bool nmObjRect::calculateMovePoints(const QPointF& first, const QPointF& second,
int hitOption, int hitIndex, QVector<QPointF>& points) const {
points.clear();
if (isReadOnly() || isLockPos() || m_pAxisX == nullptr || m_pAxisY == nullptr ||
m_vecPoints.count() != 4) return false;
const QVector<QPointF> original = getAllPos();
const QPointF delta = second - first;
if (!qIsFinite(delta.x()) || !qIsFinite(delta.y())) return false;
QVector<QPointF> moved = original;
if (hitOption == OHO_Inner) {
for (int i = 0; i < 4; ++i) moved[i] += delta;
} else if (hitOption == OHO_Point || hitOption == OHO_Bound) {
if (isLockSize() || hitIndex < 0 || hitIndex >= 4) return false;
const QPointF& anchor = original[hitIndex];
const QPointF& next = original[(hitIndex + 1) % 4];
const bool moveX = hitOption == OHO_Point || anchor.x() == next.x();
const bool moveY = hitOption == OHO_Point || anchor.y() == next.y();
if (!moveX && !moveY) return false;
// 按共同横/纵坐标找到同侧角,不依赖顺逆时针点序,也不把角跳到鼠标中心。
for (int i = 0; i < 4; ++i) {
if (moveX && original[i].x() == anchor.x()) moved[i].rx() += delta.x();
if (moveY && original[i].y() == anchor.y()) moved[i].ry() += delta.y();
}
} else {
return false;
}
for (int i = 0; i < 4; ++i) {
if (!qIsFinite(moved[i].x()) || !qIsFinite(moved[i].y())) return false;
}
// 对角线两个分量的方向必须保留,避免压扁或翻转后上下左右边的业务类型互换。
const QPointF oldSpan = original[2] - original[0];
const QPointF newSpan = moved[2] - moved[0];
if (!qIsFinite(oldSpan.x()) || !qIsFinite(oldSpan.y()) ||
!qIsFinite(newSpan.x()) || !qIsFinite(newSpan.y()) ||
oldSpan.x() == 0.0 || oldSpan.y() == 0.0 || newSpan.x() == 0.0 || newSpan.y() == 0.0 ||
(oldSpan.x() > 0.0) != (newSpan.x() > 0.0) ||
(oldSpan.y() > 0.0) != (newSpan.y() > 0.0)) return false;
points = moved;
return true;
}
bool nmObjRect::runMove(const QPointF& pt1, const QPointF& pt2) {
QVector<QPointF> points;
if (!calculateMovePoints(pt1, pt2, m_oHitOption, m_nHitIndex, points)) return false;
resetBrotherOffset();
if (m_oHitOption == OHO_Inner) {
m_dOffsetX = pt2.x() - pt1.x();
m_dOffsetY = pt2.y() - pt1.y();
}
// 只在松手时提交;矩形边界通过所属 Map 的 Binder 同步业务数据。
return moveToPos(points);
}
void nmObjRect::fillPtyPano(IxPtyPano* sheet) {
nmObjBase::fillPtyPano(sheet);
ZX_PROP("ObjRect.Pen", getPen, setPen);
ZX_PROP("ObjRect.BackgrdColor", getBackgrdColor, setBackgrdColor);
ZX_PROP("ObjRect.BackgrdColorAlpha", getBackgrdAlpha, setBackgrdAlpha);
}
void nmObjRect::onSerialize(ZxSerializer* ser) {
nmObjBase::onSerialize(ser);
ser->write("BackgrdColor", m_clrBackgrd);
}
void nmObjRect::onDeserialize(ZxSerializer* ser) {
nmObjBase::onDeserialize(ser);
ser->read("BackgrdColor", m_clrBackgrd);
}
void nmObjRect::onLoadTempl(ZxSerializer* ser) {
nmObjBase::onLoadTempl(ser);
ser->read("BackgrdColor", m_clrBackgrd);
}
void nmObjRect::onSaveTempl(ZxSerializer* ser) {
nmObjBase::onSaveTempl(ser);
ser->write("BackgrdColor", m_clrBackgrd);
}
void nmObjRect::paintBack(QPainter* painter, const ZxPaintParam& param) {
// 判断当前坐标轴是否有效
if(NULL == m_pAxisX || NULL == m_pAxisY || \
m_pAxisX->getRangeMin() == m_pAxisX->getRangeMax() || \
m_pAxisY->getRangeMin() == m_pAxisY->getRangeMax()) {
return;
}
painter->save();
QVector<QPointF> pts = getPosOf(m_vecPoints); // 获取矩形四个点的坐标
QRectF bound = getBounds(); // 获取图元基础边框(位置)
if(!pts.empty()) { //矩形的四个点存在
// 填充矩形路径
QPainterPath oPath;
oPath.moveTo(pts[0]);
for(int i = 1; i < pts.count(); i++) {
oPath.lineTo(pts[i]);
}
oPath.closeSubpath();
painter->fillPath(oPath, QBrush(m_clrBackgrd));
painter->setBrush(Qt::NoBrush); // 描边和节点不再重复填充。
// 边界
QPen pen = m_oPen;
painter->setPen(pen);
painter->drawPolygon(&pts[0], pts.count());
// 绘制名称文字
QFont ft = qApp->font();
ft.setPointSize(8);
painter->setFont(ft);
painter->setPen(Qt::black);
ZxDrawHelper::drawText(painter,
bound,
Qt::AlignCenter | Qt::AlignVCenter,
m_sName);
}
if (isSelected() && !pts.isEmpty()) {
QPen pen(QBrush(Qt::black), 0.0f, Qt::DotLine);
painter->setPen(pen);
painter->drawPolygon(pts.constData(), pts.count());
paintSelectedNodeHandles(painter, pts);
}
painter->restore();
}
QColor nmObjRect::getBackgrdColor() const {
return m_clrBackgrd;
}
void nmObjRect::setBackgrdColor(QColor clr) {
m_clrBackgrd.setRed(clr.red());
m_clrBackgrd.setGreen(clr.green());
m_clrBackgrd.setBlue(clr.blue());
update();
}
int nmObjRect::getBackgrdAlpha() const {
return m_clrBackgrd.alpha();
}
void nmObjRect::setBackgrdAlpha(int nAlpha) {
m_clrBackgrd.setAlpha(nAlpha);
update();
}