|
|
|
|
#include <QPainter>
|
|
|
|
|
#include <QPainterPath>
|
|
|
|
|
#include <QtCore/qnumeric.h>
|
|
|
|
|
|
|
|
|
|
#include "ZxRenderView.h"
|
|
|
|
|
#include "nmObjRect.h"
|
|
|
|
|
#include "nmObjRectTool.h"
|
|
|
|
|
|
|
|
|
|
nmObjRectTool::nmObjRectTool() : nmObjToolBase()
|
|
|
|
|
{
|
|
|
|
|
setText(tr("nObjRectTool"));
|
|
|
|
|
m_oNot = NMOT_Rect;
|
|
|
|
|
enableInteractionCursor();
|
|
|
|
|
enableEscapeCancellation();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool nmObjRectTool::onLeftDown(const QPointF& pt)
|
|
|
|
|
{
|
|
|
|
|
if (m_bCreationCancelled) return true;
|
|
|
|
|
if (m_pObj != nullptr) return nmObjToolBase::onLeftDown(pt);
|
|
|
|
|
|
|
|
|
|
beginCreationCursor();
|
|
|
|
|
setCreationHint(QString());
|
|
|
|
|
m_ptStart = pt;
|
|
|
|
|
m_ptMove = pt;
|
|
|
|
|
m_bTracking = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool nmObjRectTool::onMouseMove(const QPointF& pt)
|
|
|
|
|
{
|
|
|
|
|
if (m_bCreationCancelled) return true;
|
|
|
|
|
if (m_pObj != nullptr) return nmObjToolBase::onMouseMove(pt);
|
|
|
|
|
|
|
|
|
|
beginCreationCursor();
|
|
|
|
|
m_ptMove = pt;
|
|
|
|
|
if (m_bTracking) updateLastMoveArea(true);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool nmObjRectTool::onLeftUp(const QPointF& pt)
|
|
|
|
|
{
|
|
|
|
|
if (m_bCreationCancelled) return true;
|
|
|
|
|
if (m_pObj != nullptr) return nmObjToolBase::onLeftUp(pt);
|
|
|
|
|
if (!m_bTracking || getView() == nullptr) return true;
|
|
|
|
|
|
|
|
|
|
m_bTracking = false;
|
|
|
|
|
QVector<QPointF> points;
|
|
|
|
|
const bool validGeometry = nmObjRect::calculateCreationPoints(m_ptStart, pt, points);
|
|
|
|
|
// 无效坐标不进入场景到视口的转换,避免非有限数参与像素取整。
|
|
|
|
|
const QPointF pixelSpan = validGeometry
|
|
|
|
|
? QPointF(getView()->mapFromScene(pt) - getView()->mapFromScene(m_ptStart)) : QPointF();
|
|
|
|
|
// 两个方向分别检查,阻止水平/竖直拖动产生零尺寸;像素阈值不随地图缩放变化。
|
|
|
|
|
const qreal minimumExtent = 3.0;
|
|
|
|
|
if (!validGeometry ||
|
|
|
|
|
!qIsFinite(pixelSpan.x()) || !qIsFinite(pixelSpan.y()) ||
|
|
|
|
|
qAbs(pixelSpan.x()) < minimumExtent || qAbs(pixelSpan.y()) < minimumExtent) {
|
|
|
|
|
setCreationHint(tr("Drag diagonally to give the rectangle both width and height."));
|
|
|
|
|
getView()->setSnapshotMode(false);
|
|
|
|
|
updateScene();
|
|
|
|
|
return true; // 保留创建工具和十字光标,允许重新拖拽或按 Esc 退出。
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 完成信号可能同步移除工具,先清理预览与光标;发送后不再访问成员。
|
|
|
|
|
resetInteractionCursor();
|
|
|
|
|
emit sigPtsFinished(points);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool nmObjRectTool::onLeftDoubleClick(const QPointF& pt)
|
|
|
|
|
{
|
|
|
|
|
if (m_bCreationCancelled) return true;
|
|
|
|
|
if (m_pObj == nullptr) {
|
|
|
|
|
m_bTracking = false; // 双击后的松手不能从旧起点创建矩形。
|
|
|
|
|
if (getView() != nullptr) getView()->setSnapshotMode(false);
|
|
|
|
|
updateScene();
|
|
|
|
|
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);
|
|
|
|
|
if (getView() != nullptr) {
|
|
|
|
|
getView()->setSnapshotMode(false);
|
|
|
|
|
getView()->restoreCursor();
|
|
|
|
|
}
|
|
|
|
|
updateScene();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return nmObjToolBase::onLeftDoubleClick(pt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void nmObjRectTool::onPaint(QPainter* painter, const ZxPaintParam& param)
|
|
|
|
|
{
|
|
|
|
|
if (m_bCreationCancelled) return;
|
|
|
|
|
if (m_pObj == nullptr) paintTrackingVirtualObj(painter, param);
|
|
|
|
|
else paintTrackingRealObj(painter, param);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void nmObjRectTool::paintTrackingVirtualObj(QPainter* painter, const ZxPaintParam& param)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(param);
|
|
|
|
|
if (!m_bTracking) return;
|
|
|
|
|
QVector<QPointF> points;
|
|
|
|
|
if (!nmObjRect::calculateCreationPoints(m_ptStart, m_ptMove, points)) return;
|
|
|
|
|
|
|
|
|
|
painter->save();
|
|
|
|
|
painter->setPen(QPen(Qt::DashLine));
|
|
|
|
|
painter->setBrush(Qt::NoBrush);
|
|
|
|
|
painter->drawPolygon(points.constData(), points.count());
|
|
|
|
|
painter->restore();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void nmObjRectTool::paintTrackingRealObj(QPainter* painter, const ZxPaintParam& param)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(param);
|
|
|
|
|
if (!m_bTracking) return;
|
|
|
|
|
nmObjRect* pObj = dynamic_cast<nmObjRect*>(m_pObj);
|
|
|
|
|
if (pObj == nullptr) return;
|
|
|
|
|
|
|
|
|
|
QVector<QPointF> points;
|
|
|
|
|
if (!pObj->calculateMovePoints(m_ptStart, getCurrentPos(), 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 nmObjRectTool::interactionCursor(int nOption, int nSubIndex, bool dragging) const
|
|
|
|
|
{
|
|
|
|
|
nmObjRect* pObj = dynamic_cast<nmObjRect*>(m_pObj);
|
|
|
|
|
if (pObj == nullptr) return Qt::ArrowCursor;
|
|
|
|
|
if (nOption == OHO_Inner) {
|
|
|
|
|
// 公共整体移动规则允许锁定尺寸的图元平移,并统一检查只读和位置锁定。
|
|
|
|
|
return nmObjToolBase::interactionCursor(OHO_Bound, nSubIndex, dragging);
|
|
|
|
|
}
|
|
|
|
|
if (nmObjToolBase::interactionCursor(OHO_Point, nSubIndex, dragging) == Qt::ArrowCursor) {
|
|
|
|
|
return Qt::ArrowCursor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (pObj->rectHitPosition(nOption, nSubIndex)) {
|
|
|
|
|
case RBP_LeftTop:
|
|
|
|
|
case RBP_RightBottom:
|
|
|
|
|
return Qt::SizeFDiagCursor;
|
|
|
|
|
case RBP_RightTop:
|
|
|
|
|
case RBP_LeftBottom:
|
|
|
|
|
return Qt::SizeBDiagCursor;
|
|
|
|
|
case RBP_Top:
|
|
|
|
|
case RBP_Bottom:
|
|
|
|
|
return Qt::SizeVerCursor;
|
|
|
|
|
case RBP_Left:
|
|
|
|
|
case RBP_Right:
|
|
|
|
|
return Qt::SizeHorCursor;
|
|
|
|
|
default:
|
|
|
|
|
return Qt::ArrowCursor;
|
|
|
|
|
}
|
|
|
|
|
}
|