|
|
|
|
|
#include <QPainter>
|
|
|
|
|
|
#include <QLineF>
|
|
|
|
|
|
#include <QPainterPath>
|
|
|
|
|
|
|
|
|
|
|
|
#include "ZxRenderView.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include "nmObjPolygon.h"
|
|
|
|
|
|
#include "nmObjPolygonTool.h"
|
|
|
|
|
|
#include "nmPlotGraphicBinder.h"
|
|
|
|
|
|
|
|
|
|
|
|
nmObjPolygonTool::nmObjPolygonTool() : nmObjToolBase()
|
|
|
|
|
|
{
|
|
|
|
|
|
setText(tr("nObjPolygonTool"));
|
|
|
|
|
|
m_oNot = NMOT_Polygon;
|
|
|
|
|
|
enablePointSnapping();
|
|
|
|
|
|
enableInteractionCursor();
|
|
|
|
|
|
enableEscapeCancellation();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool nmObjPolygonTool::onLeftDown(const QPointF& pt)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_bCreationCancelled) return true;
|
|
|
|
|
|
if (m_pObj != nullptr) return nmObjToolBase::onLeftDown(pt);
|
|
|
|
|
|
|
|
|
|
|
|
// 保留点击起点闭合;使用固定视口像素,避免缩放后闭合范围忽大忽小。
|
|
|
|
|
|
if (getView() != nullptr && m_vecPoints.count() >= 3 &&
|
|
|
|
|
|
QLineF(getView()->mapFromScene(pt),
|
|
|
|
|
|
getView()->mapFromScene(m_vecPoints.first())).length() <= 5.0) {
|
|
|
|
|
|
finishPolylineCreation(m_vecPoints, true);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
appendCreationPoint(m_vecPoints, pt);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool nmObjPolygonTool::onMouseMove(const QPointF& pt)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_bCreationCancelled) return true;
|
|
|
|
|
|
if (m_pObj != nullptr) return nmObjToolBase::onMouseMove(pt);
|
|
|
|
|
|
|
|
|
|
|
|
m_ptMove = pt;
|
|
|
|
|
|
beginCreationCursor();
|
|
|
|
|
|
updatePointSnap(pt);
|
|
|
|
|
|
if (m_bTracking) updateLastMoveArea(true);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool nmObjPolygonTool::onLeftUp(const QPointF& pt)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_bCreationCancelled) return true;
|
|
|
|
|
|
if (m_pObj != nullptr) return nmObjToolBase::onLeftUp(pt);
|
|
|
|
|
|
|
|
|
|
|
|
if (!m_vecPoints.isEmpty()) updateLastMoveArea(true);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool nmObjPolygonTool::onLeftDoubleClick(const QPointF& pt)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_bCreationCancelled) return true;
|
|
|
|
|
|
if (m_pObj == nullptr) {
|
|
|
|
|
|
finishPolylineCreation(m_vecPoints, true);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (insertPointOnDoubleClick(pt, true)) return true;
|
|
|
|
|
|
|
|
|
|
|
|
// 重新查询双击位置,不复用上次悬停的边或节点下标。
|
|
|
|
|
|
int option = -1;
|
|
|
|
|
|
int index = -1;
|
|
|
|
|
|
if (m_pObj->_runHitTest(pt, option, index) && option == OHO_Inner) {
|
|
|
|
|
|
m_bTracking = false;
|
|
|
|
|
|
m_oHitOption = OHO_None;
|
|
|
|
|
|
m_pObj->m_oHitOption = OHO_None;
|
|
|
|
|
|
m_pObj->m_nHitIndex = -1;
|
|
|
|
|
|
m_pObj->setSelected(false);
|
|
|
|
|
|
clearPointSnap();
|
|
|
|
|
|
if (getView() != nullptr) {
|
|
|
|
|
|
getView()->setSnapshotMode(false);
|
|
|
|
|
|
getView()->restoreCursor();
|
|
|
|
|
|
}
|
|
|
|
|
|
updateScene();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
return nmObjToolBase::onLeftDoubleClick(pt);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool nmObjPolygonTool::onMouseWheel(const QPointF& pt, int delta)
|
|
|
|
|
|
{
|
|
|
|
|
|
return nmObjToolBase::onMouseWheel(pt, delta);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void nmObjPolygonTool::onPaint(QPainter* painter, const ZxPaintParam& param)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_bCreationCancelled) return;
|
|
|
|
|
|
if (m_pObj == nullptr) {
|
|
|
|
|
|
paintTrackingVirtualObj(painter, param);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
paintTrackingRealObj(painter, param);
|
|
|
|
|
|
}
|
|
|
|
|
|
// 吸附目标最后绘制,避免被边界预览的填充覆盖。
|
|
|
|
|
|
paintPointSnapTargets(painter);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void nmObjPolygonTool::paintTrackingVirtualObj(QPainter* painter, const ZxPaintParam& param)
|
|
|
|
|
|
{
|
|
|
|
|
|
Q_UNUSED(param);
|
|
|
|
|
|
painter->save();
|
|
|
|
|
|
painter->setPen(QPen(Qt::SolidLine));
|
|
|
|
|
|
for (int i = 0; i + 1 < m_vecPoints.count(); ++i) {
|
|
|
|
|
|
painter->drawLine(m_vecPoints[i], m_vecPoints[i + 1]);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!m_vecPoints.isEmpty()) {
|
|
|
|
|
|
const QPointF previewPos = pointSnapPosition(m_ptMove);
|
|
|
|
|
|
painter->setPen(QPen(Qt::DashLine));
|
|
|
|
|
|
painter->drawLine(m_vecPoints.last(), previewPos);
|
|
|
|
|
|
if (m_vecPoints.count() >= 2) {
|
|
|
|
|
|
painter->drawLine(previewPos, m_vecPoints.first());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
painter->restore();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void nmObjPolygonTool::paintTrackingRealObj(QPainter* painter, const ZxPaintParam& param)
|
|
|
|
|
|
{
|
|
|
|
|
|
Q_UNUSED(param);
|
|
|
|
|
|
if (!m_bTracking) return;
|
|
|
|
|
|
nmObjPolygon* pObj = dynamic_cast<nmObjPolygon*>(m_pObj);
|
|
|
|
|
|
if (pObj == nullptr) return;
|
|
|
|
|
|
|
|
|
|
|
|
// 只有单节点移动吸附;拖边仍移动相邻两点,内部拖动保持整体形状。
|
|
|
|
|
|
const QPointF ptNew = m_oHitOption == OHO_Point
|
|
|
|
|
|
? pointSnapPosition(getCurrentPos()) : getCurrentPos();
|
|
|
|
|
|
QVector<QPointF> points;
|
|
|
|
|
|
if (!pObj->calculateMovePoints(m_ptStart, ptNew, m_oHitOption,
|
|
|
|
|
|
pObj->m_nHitIndex, points)) return;
|
|
|
|
|
|
|
|
|
|
|
|
QPainterPath path;
|
|
|
|
|
|
path.moveTo(points.first());
|
|
|
|
|
|
for (int i = 1; i < points.count(); ++i) path.lineTo(points[i]);
|
|
|
|
|
|
path.closeSubpath();
|
|
|
|
|
|
|
|
|
|
|
|
painter->save();
|
|
|
|
|
|
QColor color = pObj->getBackgrdColor();
|
|
|
|
|
|
color.setAlpha(48);
|
|
|
|
|
|
painter->fillPath(path, QBrush(color));
|
|
|
|
|
|
painter->strokePath(path, QPen(Qt::DashLine));
|
|
|
|
|
|
painter->restore();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Qt::CursorShape nmObjPolygonTool::interactionCursor(int nOption, bool dragging) const
|
|
|
|
|
|
{
|
|
|
|
|
|
if (nOption == OHO_Bound) {
|
|
|
|
|
|
// 移动一条边会改变形状,与节点编辑使用相同的十字光标和尺寸锁检查。
|
|
|
|
|
|
return nmObjToolBase::interactionCursor(OHO_Point, dragging);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (nOption == OHO_Inner) {
|
|
|
|
|
|
return nmObjToolBase::interactionCursor(OHO_Bound, dragging);
|
|
|
|
|
|
}
|
|
|
|
|
|
return nmObjToolBase::interactionCursor(nOption, dragging);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QVector<QPointF> nmObjPolygonTool::pointSnapCandidates() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return nmPlotGraphicBinder::pointSnapCandidates(getView(), m_pObj);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool nmObjPolygonTool::stepBackCreation(bool autoRepeat)
|
|
|
|
|
|
{
|
|
|
|
|
|
return stepBackCreationPoints(m_vecPoints, autoRepeat);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void nmObjPolygonTool::clearCreationPoints()
|
|
|
|
|
|
{
|
|
|
|
|
|
m_vecPoints.clear();
|
|
|
|
|
|
}
|