|
|
|
|
#include <QPainter>
|
|
|
|
|
#include <QPainterPath>
|
|
|
|
|
#include <QLineF>
|
|
|
|
|
#include <QtCore/qnumeric.h>
|
|
|
|
|
|
|
|
|
|
#include "ZxSerializer.h"
|
|
|
|
|
#include "IxPtyPano.h"
|
|
|
|
|
|
|
|
|
|
#include "ZxSubAxisX.h"
|
|
|
|
|
#include "ZxSubAxisY.h"
|
|
|
|
|
#include "ZxRenderView.h"
|
|
|
|
|
|
|
|
|
|
#include "nmObjRoundTool.h"
|
|
|
|
|
#include "nmObjRound.h"
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
// 校验圆心和圆的范围,避免无效坐标进入绘制或更新到数据层。
|
|
|
|
|
bool isValidCircle(const QPointF& center, qreal radius)
|
|
|
|
|
{
|
|
|
|
|
return radius > 0.0 && qIsFinite(radius) &&
|
|
|
|
|
qIsFinite(center.x() - radius) && qIsFinite(center.x() + radius) &&
|
|
|
|
|
qIsFinite(center.y() - radius) && qIsFinite(center.y() + radius);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 固定布局:圆心、半径占位、上/右/下/左控制点。
|
|
|
|
|
QVector<QPointF> circlePoints(const QPointF& center, qreal radius)
|
|
|
|
|
{
|
|
|
|
|
QVector<QPointF> points;
|
|
|
|
|
points << center << QPointF(radius, radius)
|
|
|
|
|
<< QPointF(center.x(), center.y() - radius)
|
|
|
|
|
<< QPointF(center.x() + radius, center.y())
|
|
|
|
|
<< QPointF(center.x(), center.y() + radius)
|
|
|
|
|
<< QPointF(center.x() - radius, center.y());
|
|
|
|
|
return points;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
ZX_DEFINE_DYNAMIC(nObjRound, nmObjRound)
|
|
|
|
|
|
|
|
|
|
nmObjRound::nmObjRound() {
|
|
|
|
|
m_sObjTag = "nObjRound";
|
|
|
|
|
nmObjRound::init("", NULL, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nmObjRound::nmObjRound(const QString& sName, \
|
|
|
|
|
ZxSubAxisX* pAxisX, \
|
|
|
|
|
ZxSubAxisY* pAxisY) {
|
|
|
|
|
m_sObjTag = "nObjRound";
|
|
|
|
|
nmObjRound::init(sName, pAxisX, pAxisY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nmObjRound::~nmObjRound() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化圆形的画笔和填充颜色。
|
|
|
|
|
void nmObjRound::init(const QString& sName, \
|
|
|
|
|
ZxSubAxisX* pAxisX, \
|
|
|
|
|
ZxSubAxisY* pAxisY) {
|
|
|
|
|
nmObjBase::init(sName, pAxisX, pAxisY);
|
|
|
|
|
|
|
|
|
|
nmObjRound::initFlags();
|
|
|
|
|
|
|
|
|
|
m_oPen = QPen(QBrush(QColor(0, 0, 0)), \
|
|
|
|
|
0.5f, Qt::SolidLine);
|
|
|
|
|
m_clrBackgrd = QColor(255, 170, 255, 100);
|
|
|
|
|
|
|
|
|
|
loadTempl();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化绘制圆形的工具
|
|
|
|
|
void nmObjRound::initTools() {
|
|
|
|
|
m_pTool = new nmObjRoundTool();
|
|
|
|
|
|
|
|
|
|
nmObjBase::initTools();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void nmObjRound::initFlags() {
|
|
|
|
|
setLockPos(false);
|
|
|
|
|
setLockSize(false);
|
|
|
|
|
setReadOnly(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QColor nmObjRound::getBackgrdColor() const {
|
|
|
|
|
//if (m_nLineIndex)
|
|
|
|
|
//return m_vecPresType[m_nLineIndex]
|
|
|
|
|
return m_clrBackgrd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void nmObjRound::setBackgrdColor(QColor color) {
|
|
|
|
|
if (m_clrBackgrd != color) {
|
|
|
|
|
//m_vecPresType[m_nLineIndex] =
|
|
|
|
|
m_clrBackgrd = color;
|
|
|
|
|
update();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool nmObjRound::hitTest(const QPointF& pt) {
|
|
|
|
|
return nmObjBase::hitTest(pt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool nmObjRound::_runHitTest(const QPointF& pt, int& nOption, int& nSubIndex) {
|
|
|
|
|
nOption = -1;
|
|
|
|
|
nSubIndex = -1;
|
|
|
|
|
if (!nmObjBase::_runHitTest(pt, nOption, nSubIndex)) return false;
|
|
|
|
|
|
|
|
|
|
ZxRenderView* view = getView();
|
|
|
|
|
QPointF center;
|
|
|
|
|
qreal radius = 0.0;
|
|
|
|
|
if (view == nullptr || !getSceneCircle(center, radius) ||
|
|
|
|
|
!qIsFinite(pt.x()) || !qIsFinite(pt.y())) return false;
|
|
|
|
|
|
|
|
|
|
const QPointF mousePos = view->mapFromScene(pt);
|
|
|
|
|
const QVector<QPointF> points = circlePoints(center, radius);
|
|
|
|
|
qreal nearestDistance = NodeHitRadiusPixels * NodeHitRadiusPixels;
|
|
|
|
|
int nearestIndex = -1;
|
|
|
|
|
for (int i = TopControlPoint; i <= LeftControlPoint; ++i) {
|
|
|
|
|
const QPointF delta = mousePos - view->mapFromScene(points[i]);
|
|
|
|
|
const qreal distance = delta.x() * delta.x() + delta.y() * delta.y();
|
|
|
|
|
if (qIsFinite(distance) && distance <= nearestDistance &&
|
|
|
|
|
(nearestIndex < 0 || distance < nearestDistance)) {
|
|
|
|
|
nearestDistance = distance;
|
|
|
|
|
nearestIndex = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (nearestIndex >= 0) {
|
|
|
|
|
nOption = OHO_Point;
|
|
|
|
|
nSubIndex = nearestIndex;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 视图可能在横纵方向使用不同的缩放比例,在视口对圆的轮廓扩展固定像素。
|
|
|
|
|
const QPointF extent(radius, radius);
|
|
|
|
|
const QRectF bounds = view->mapFromScene(QRectF(center - extent, center + extent)).normalized();
|
|
|
|
|
const qreal margin = OutlineHitRadiusPixels;
|
|
|
|
|
if (!bounds.isValid() || !bounds.adjusted(-margin, -margin, margin, margin).contains(mousePos)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
QPainterPath outline;
|
|
|
|
|
outline.addEllipse(bounds);
|
|
|
|
|
QPainterPathStroker stroker;
|
|
|
|
|
stroker.setWidth(2.0 * margin);
|
|
|
|
|
if (stroker.createStroke(outline).contains(mousePos)) {
|
|
|
|
|
nOption = OHO_Bound;
|
|
|
|
|
nSubIndex = 0;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (isSelected() && outline.contains(mousePos)) {
|
|
|
|
|
nOption = OHO_Inner;
|
|
|
|
|
nSubIndex = 0;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool nmObjRound::getSceneCircle(QPointF& center, qreal& radius) const {
|
|
|
|
|
if (m_pAxisX == nullptr || m_pAxisY == nullptr || m_vecPoints.count() <= LeftControlPoint ||
|
|
|
|
|
m_pAxisX->getRangeMin() == m_pAxisX->getRangeMax() ||
|
|
|
|
|
m_pAxisY->getRangeMin() == m_pAxisY->getRangeMax()) return false;
|
|
|
|
|
|
|
|
|
|
center = getPosOf(m_vecPoints[0]);
|
|
|
|
|
// 半径占位不是绝对坐标;从纵向控制点恢复半径,与 Binder 的保存口径一致。
|
|
|
|
|
radius = qAbs(center.y() - getPosOf(m_vecPoints[TopControlPoint]).y());
|
|
|
|
|
return isValidCircle(center, radius);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool nmObjRound::calculateCreationPoints(const QPointF& start, const QPointF& end,
|
|
|
|
|
QVector<QPointF>& points) {
|
|
|
|
|
const QPointF center = start * 0.5 + end * 0.5;
|
|
|
|
|
const qreal radius = QLineF(start, end).length() * 0.5;
|
|
|
|
|
if (!isValidCircle(center, radius)) return false;
|
|
|
|
|
|
|
|
|
|
points = circlePoints(center, radius);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool nmObjRound::calculateMovePoints(const QPointF& ptStart, const QPointF& ptEnd,
|
|
|
|
|
ObjHitOption oHitOption, int nHitIndex,
|
|
|
|
|
QVector<QPointF>& vecPos) const {
|
|
|
|
|
if (isReadOnly() || isLockPos()) return false;
|
|
|
|
|
|
|
|
|
|
QPointF center;
|
|
|
|
|
qreal radius = 0.0;
|
|
|
|
|
if (!getSceneCircle(center, radius)) return false;
|
|
|
|
|
const QPointF offset = ptEnd - ptStart;
|
|
|
|
|
if (!qIsFinite(offset.x()) || !qIsFinite(offset.y())) return false;
|
|
|
|
|
|
|
|
|
|
if (oHitOption == OHO_Point) {
|
|
|
|
|
if (isLockSize()) return false;
|
|
|
|
|
// 只取控制点所在方向的位移,按在节点框边缘时半径也不会跳变。
|
|
|
|
|
switch (nHitIndex) {
|
|
|
|
|
case TopControlPoint:
|
|
|
|
|
radius -= offset.y();
|
|
|
|
|
break;
|
|
|
|
|
case RightControlPoint:
|
|
|
|
|
radius += offset.x();
|
|
|
|
|
break;
|
|
|
|
|
case BottomControlPoint:
|
|
|
|
|
radius += offset.y();
|
|
|
|
|
break;
|
|
|
|
|
case LeftControlPoint:
|
|
|
|
|
radius -= offset.x();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else if (oHitOption == OHO_Inner) {
|
|
|
|
|
center += offset;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 拖过圆心时拒绝零半径或负半径,预览和提交遵守同一规则。
|
|
|
|
|
if (!isValidCircle(center, radius)) return false;
|
|
|
|
|
|
|
|
|
|
vecPos = circlePoints(center, radius);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool nmObjRound::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 commitPointPositions(points);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void nmObjRound::fillPtyPano(IxPtyPano* sheet) {
|
|
|
|
|
nmObjBase::fillPtyPano(sheet);
|
|
|
|
|
|
|
|
|
|
ZX_PROP("ObjPolygon.Pen", getPen, setPen);
|
|
|
|
|
ZX_PROP("ObjPolygon.BackgrdColor", getBackgrdColor, setBackgrdColor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void nmObjRound::paintBack(QPainter* painter, const ZxPaintParam& param) {
|
|
|
|
|
Q_UNUSED(param);
|
|
|
|
|
paintCircle(painter, QPen(QBrush(Qt::black), 0.0, Qt::DotLine));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void nmObjRound::paintCircle(QPainter* painter, const QPen& selectionPen) const {
|
|
|
|
|
QPointF center;
|
|
|
|
|
qreal radius = 0.0;
|
|
|
|
|
if (!getSceneCircle(center, radius)) return;
|
|
|
|
|
|
|
|
|
|
const QPointF extent(radius, radius);
|
|
|
|
|
const QRectF bounds(center - extent, center + extent);
|
|
|
|
|
QPainterPath outline;
|
|
|
|
|
outline.addEllipse(bounds);
|
|
|
|
|
|
|
|
|
|
painter->save();
|
|
|
|
|
painter->fillPath(outline, QBrush(m_clrBackgrd));
|
|
|
|
|
painter->strokePath(outline, m_oPen);
|
|
|
|
|
|
|
|
|
|
painter->setFont(QFont("Arial", 8, QFont::Normal));
|
|
|
|
|
painter->setPen(Qt::black);
|
|
|
|
|
ZxDrawHelper::drawText(painter, bounds, Qt::AlignCenter | Qt::AlignVCenter, m_sName);
|
|
|
|
|
|
|
|
|
|
if (isSelected()) {
|
|
|
|
|
painter->strokePath(outline, selectionPen);
|
|
|
|
|
paintSelectedNodeHandles(painter, circlePoints(center, radius), TopControlPoint);
|
|
|
|
|
}
|
|
|
|
|
painter->restore();
|
|
|
|
|
}
|