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.
233 lines
6.0 KiB
C++
233 lines
6.0 KiB
C++
#include <QPainter>
|
|
|
|
#include "ZxSerializer.h"
|
|
#include "IxPtyPano.h"
|
|
|
|
#include "ZxSubAxisX.h"
|
|
#include "ZxSubAxisY.h"
|
|
|
|
#include "nmObjPolygonTool.h"
|
|
#include "nmObjPolygon.h"
|
|
|
|
ZX_DEFINE_DYNAMIC(nObjPolygon, nmObjPolygon)
|
|
|
|
nmObjPolygon::nmObjPolygon()
|
|
{
|
|
m_sObjTag = "nObjPolygon";
|
|
nmObjPolygon::init("", nullptr, nullptr);
|
|
}
|
|
|
|
nmObjPolygon::nmObjPolygon(const QString& sName, \
|
|
ZxSubAxisX* pAxisX, \
|
|
ZxSubAxisY* pAxisY)
|
|
{
|
|
m_sObjTag = "nObjPolygon";
|
|
nmObjPolygon::init(sName, pAxisX, pAxisY);
|
|
}
|
|
|
|
nmObjPolygon::~nmObjPolygon()
|
|
{
|
|
}
|
|
|
|
void nmObjPolygon::init(const QString& sName, \
|
|
ZxSubAxisX* pAxisX, \
|
|
ZxSubAxisY* pAxisY)
|
|
{
|
|
nmObjBase::init(sName, pAxisX, pAxisY);
|
|
|
|
nmObjPolygon::initFlags();
|
|
|
|
m_oPen = QPen(QBrush(QColor(128, 128, 128)), \
|
|
0.3, Qt::SolidLine);
|
|
m_clrBackgrd = QColor(255, 170, 255, 100);
|
|
|
|
loadTempl();
|
|
}
|
|
|
|
void nmObjPolygon::initTools()
|
|
{
|
|
m_pTool = new nmObjPolygonTool();
|
|
|
|
nmObjBase::initTools();
|
|
}
|
|
|
|
void nmObjPolygon::initFlags()
|
|
{
|
|
setLockPos(false);
|
|
setLockSize(false);
|
|
setReadOnly(false);
|
|
}
|
|
|
|
bool nmObjPolygon::hitTest(const QPointF& pt)
|
|
{
|
|
return nmObjBase::hitTest(pt);
|
|
}
|
|
|
|
bool nmObjPolygon::_runHitTest(const QPointF& pt, int& nOption, int& nSubIndex)
|
|
{
|
|
// 复用固定像素的点/边命中,并保留选中多边形内部的整体移动入口。
|
|
return hitTestPolylineInPixels(pt, true, nOption, nSubIndex, true);
|
|
}
|
|
|
|
bool nmObjPolygon::calculateMovePoints(const QPointF& pt1, const QPointF& pt2,
|
|
int hitOption, int hitIndex,
|
|
QVector<QPointF>& points)
|
|
{
|
|
points.clear();
|
|
if (isReadOnly() || isLockPos() || m_pAxisX == nullptr || m_pAxisY == nullptr) {
|
|
return false;
|
|
}
|
|
points = getAllPos();
|
|
if (points.count() < 3) return false;
|
|
if (hitOption == OHO_Point || hitOption == OHO_Bound) {
|
|
if (isLockSize() || hitIndex < 0 || hitIndex >= points.count()) return false;
|
|
if (hitOption == OHO_Point) {
|
|
points[hitIndex] = pt2;
|
|
} else {
|
|
const int nextIndex = (hitIndex + 1) % points.count();
|
|
points[hitIndex] = offsetPoint(points[hitIndex], pt1, pt2);
|
|
points[nextIndex] = offsetPoint(points[nextIndex], pt1, pt2);
|
|
}
|
|
} else if (hitOption == OHO_Inner) {
|
|
for (int i = 0; i < points.count(); ++i) {
|
|
points[i] = offsetPoint(points[i], pt1, pt2);
|
|
}
|
|
} else {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool nmObjPolygon::runMove(const QPointF& pt1, const QPointF& pt2)
|
|
{
|
|
QVector<QPointF> points;
|
|
if (!calculateMovePoints(pt1, pt2, m_oHitOption, m_nHitIndex, points)) return false;
|
|
|
|
// 节点/边编辑不沿用上次整体移动的关联偏移;业务数据由边界的 Binder 同步。
|
|
resetBrotherOffset();
|
|
if (m_oHitOption == OHO_Inner) {
|
|
m_dOffsetX = pt2.x() - pt1.x();
|
|
m_dOffsetY = pt2.y() - pt1.y();
|
|
}
|
|
return commitPointPositions(points);
|
|
}
|
|
|
|
void nmObjPolygon::fillPtyPano(IxPtyPano* sheet)
|
|
{
|
|
nmObjBase::fillPtyPano(sheet);
|
|
|
|
ZX_PROP("ObjPolygon.Pen", getPen, setPen);
|
|
ZX_PROP("ObjPolygon.BackgrdColor", getBackgrdColor, setBackgrdColor);
|
|
ZX_PROP("ObjPolygon.BackgrdColorAlpha", getBackgrdAlpha, setBackgrdAlpha);
|
|
}
|
|
|
|
void nmObjPolygon::onSerialize(ZxSerializer* ser)
|
|
{
|
|
nmObjBase::onSerialize(ser);
|
|
|
|
ser->write("BackgrdColor", m_clrBackgrd);
|
|
|
|
// for ()
|
|
// {
|
|
// ser->write("BackgrdColor", m_clrBackgrd);
|
|
// }
|
|
}
|
|
|
|
void nmObjPolygon::onDeserialize(ZxSerializer* ser)
|
|
{
|
|
nmObjBase::onDeserialize(ser);
|
|
|
|
ser->read("BackgrdColor", m_clrBackgrd);
|
|
}
|
|
|
|
void nmObjPolygon::onSaveTempl(ZxSerializer* ser)
|
|
{
|
|
nmObjBase::onSaveTempl(ser);
|
|
|
|
ser->write("BackgrdColor", m_clrBackgrd);
|
|
}
|
|
|
|
void nmObjPolygon::onLoadTempl(ZxSerializer* ser)
|
|
{
|
|
nmObjBase::onLoadTempl(ser);
|
|
|
|
ser->read("BackgrdColor", m_clrBackgrd);
|
|
}
|
|
|
|
|
|
QColor nmObjPolygon::getBackgrdColor() const
|
|
{
|
|
return m_clrBackgrd;
|
|
}
|
|
void nmObjPolygon::setBackgrdColor(QColor clr)
|
|
{
|
|
m_clrBackgrd.setRed(clr.red());
|
|
m_clrBackgrd.setGreen(clr.green());
|
|
m_clrBackgrd.setBlue(clr.blue());
|
|
update();
|
|
}
|
|
|
|
int nmObjPolygon::getBackgrdAlpha() const
|
|
{
|
|
return m_clrBackgrd.alpha();
|
|
}
|
|
void nmObjPolygon::setBackgrdAlpha(int nAlpha)
|
|
{
|
|
m_clrBackgrd.setAlpha(nAlpha);
|
|
update();
|
|
}
|
|
|
|
void nmObjPolygon::paintBack(QPainter* painter, const ZxPaintParam& param)
|
|
{
|
|
if (nullptr == m_pAxisX || nullptr == 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();
|
|
// m_clrBackgrd.setAlpha(100);
|
|
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();
|
|
}
|