#include #include #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"; init("", NULL, NULL); } nmObjPolygon::nmObjPolygon(const QString& sName, \ ZxSubAxisX* pAxisX, \ ZxSubAxisY* pAxisY) { m_sObjTag = "nObjPolygon"; init(sName, pAxisX, pAxisY); } nmObjPolygon::~nmObjPolygon() { } void nmObjPolygon::init(const QString& sName, \ ZxSubAxisX* pAxisX, \ ZxSubAxisY* pAxisY) { nmObjBase::init(sName, pAxisX, pAxisY); initFlags(); m_oPen = QPen(QBrush(QColor(128, 128, 128)), \ 0.3f, 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) { if (!nmObjBase::_runHitTest(pt, nOption, nSubIndex)) { return false; } nOption = -1; nSubIndex = 0; if (NULL == m_pAxisX || NULL == m_pAxisY) { return false; } QVector pts = getPosOf(m_vecPoints); // 点 float r = 1.f; for (int i = 0; i < pts.count(); i++) { QPointF ptTopLeft = QPointF(pts[i].x() - r, pts[i].y() - r); QSizeF sz = QSizeF(r * 2.f, r * 2.f); QRectF rt = QRectF(ptTopLeft, sz); bool b = rt.contains(pt); if (b) { nOption = (int)OHO_Point; nSubIndex = i; return true; } } // 线 for (int i = 0; i < pts.count()/* - 1*/; i++) { int n1 = i; int n2 = (i == pts.count() - 1 ? 0 : i + 1); if (_isNearLine(pt, pts[n1], pts[n2], 1.f * 2)) { nOption = (int)OHO_Bound; nSubIndex = i; return true; } } // 内部 QPainterPath oPath; oPath.moveTo(pts[0]); for (int i = 1; i < pts.count(); i++) { oPath.lineTo(pts[i]); } oPath.closeSubpath(); if (oPath.contains(pt)) { nOption = (int)OHO_Inner; nSubIndex = 0; return true; } return false; } bool nmObjPolygon::runMove(const QPointF& pt1, const QPointF& pt2) { if (isLockPos()) { return false; } if (NULL == m_pAxisX || NULL == m_pAxisY) { return false; } int nCount = m_vecPoints.count(); QVector vecPts = getPosOf(m_vecPoints); //屏幕值 if (m_oHitOption == OHO_Point && m_nHitIndex >= 0) { vecPts[m_nHitIndex] = offsetPoint(vecPts[m_nHitIndex], pt1, pt2); return moveToPos(vecPts); } else if (m_oHitOption == OHO_Bound && m_nHitIndex >= 0) { vecPts[m_nHitIndex] = offsetPoint(vecPts[m_nHitIndex], pt1, pt2); vecPts[m_nHitIndex + 1] = offsetPoint(vecPts[m_nHitIndex + 1], pt1, pt2); return moveToPos(vecPts); } else if (m_oHitOption == OHO_Inner) { for (int i = 0; i < nCount; i++) { vecPts[i] = offsetPoint(vecPts[i], pt1, pt2); } m_fOffsetX = pt2.x() - pt1.x(); m_fOffsetY = pt2.y() - pt1.y(); return moveToPos(vecPts); } else { return false; } return true; } void nmObjPolygon::fillPtyPano(IxPtyPano* sheet) { nmObjBase::fillPtyPano(sheet); ZX_PROP("ObjPolygon.Pen", getPen, setPen); ZX_PROP("ObjPolygon.BackgrdColor", getBackgrdColor, setBackgrdColor); } void nmObjPolygon::onSerialize(ZxSerializer* ser) { nmObjBase::onSerialize(ser); 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 color) { if (m_clrBackgrd != color) { m_clrBackgrd = color; update(); } } void nmObjPolygon::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 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)); // 边界 QPen pen = m_oPen; painter->setPen(pen); painter->drawPolygon(&pts[0], pts.count()); // 绘制名称文字 painter->setFont(QFont("Arial", 8, QFont::Normal)); painter->setPen(Qt::black); ZxDrawHelper::drawText(painter, bound, Qt::AlignCenter|Qt::AlignVCenter, m_sName); } if (isSelected()) //选中状态下 { QPen pen(QBrush(Qt::black), 0.0f, Qt::DotLine); painter->setPen(pen); painter->drawPolygon(&pts[0], pts.count()); for (int i = 0; i < pts.count(); i++) { float w = 2.2f; QPointF pt = pts[i]; QRectF rect(pt.x() - w * 0.5f, pt.y() - w * 0.5f, w, w); if (m_nHitIndex == i && m_oHitOption == OHO_Point) { QBrush br(Qt::red); painter->fillRect(rect, br); } else { painter->setPen(QColor(0, 0, 128)); painter->drawRect(rect); } } } painter->restore(); }