|
|
|
|
|
#include <QPainter>
|
|
|
|
|
|
#include <QtCore/qnumeric.h>
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
|
|
#include <QLineF>
|
|
|
|
|
|
#include "ZxObjBase.h"
|
|
|
|
|
|
#include "nmObjToolBase.h"
|
|
|
|
|
|
|
|
|
|
|
|
nmObjToolBase::nmObjToolBase() : ZxObjToolBase()
|
|
|
|
|
|
{
|
|
|
|
|
|
m_oNot = NMOT_UNKNWON;
|
|
|
|
|
|
m_bConsumeInsertPointRelease = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool nmObjToolBase::onLeftDown(const QPointF& pt)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 上一次双击若因窗口失焦未收到松手,不影响下一次正常按下。
|
|
|
|
|
|
m_bConsumeInsertPointRelease = false;
|
|
|
|
|
|
return ZxObjToolBase::onLeftDown(pt);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool nmObjToolBase::onMouseMove(const QPointF& pt)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_bConsumeInsertPointRelease) {
|
|
|
|
|
|
if (QApplication::mouseButtons().testFlag(Qt::LeftButton)) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 窗口失焦可能丢失松手事件,左键已松开时立即恢复悬停命中。
|
|
|
|
|
|
m_bConsumeInsertPointRelease = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
return ZxObjToolBase::onMouseMove(pt);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool nmObjToolBase::onLeftUp(const QPointF& pt)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_bConsumeInsertPointRelease) {
|
|
|
|
|
|
m_bConsumeInsertPointRelease = false;
|
|
|
|
|
|
m_bTracking = false;
|
|
|
|
|
|
// 保留框架的松手收尾,但不再提交拖动;清理临时节点命中状态。
|
|
|
|
|
|
ZxObjToolBase::onLeftUp(pt);
|
|
|
|
|
|
if (m_pObj != nullptr) {
|
|
|
|
|
|
m_pObj->m_oHitOption = OHO_None;
|
|
|
|
|
|
m_pObj->m_nHitIndex = -1;
|
|
|
|
|
|
m_pObj->update();
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
return ZxObjToolBase::onLeftUp(pt);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool nmObjToolBase::insertPointOnDoubleClick(const QPointF& pt, bool bClosed)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_pObj == nullptr || getView() == nullptr || !m_pObj->isSelected() ||
|
|
|
|
|
|
!m_pObj->isVisible() || m_pObj->isReadOnly() ||
|
|
|
|
|
|
m_pObj->isLockPos() || m_pObj->isLockSize()) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int nOption = -1;
|
|
|
|
|
|
int nSegmentIndex = -1;
|
|
|
|
|
|
if (!m_pObj->_runHitTest(pt, nOption, nSegmentIndex) ||
|
|
|
|
|
|
(nOption != OHO_Point && nOption != OHO_Bound)) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 双击是一次独立编辑,终止第二次按下可能建立的拖动状态。
|
|
|
|
|
|
// 随后的松手也不能再把新节点吸附到附近其他图元上。
|
|
|
|
|
|
m_bTracking = false;
|
|
|
|
|
|
m_oHitOption = OHO_None;
|
|
|
|
|
|
m_bConsumeInsertPointRelease = true;
|
|
|
|
|
|
getView()->setSnapshotMode(false);
|
|
|
|
|
|
|
|
|
|
|
|
if (nOption == OHO_Point) {
|
|
|
|
|
|
return true; // 双击已有节点不重复加点。
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QVector<QPointF> vecPos = m_pObj->getAllPos();
|
|
|
|
|
|
const int nPointCount = vecPos.count();
|
|
|
|
|
|
const int nSegmentCount = bClosed ? nPointCount : nPointCount - 1;
|
|
|
|
|
|
if (nPointCount < (bClosed ? 3 : 2) ||
|
|
|
|
|
|
nSegmentIndex < 0 || nSegmentIndex >= nSegmentCount) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const QPointF& start = vecPos[nSegmentIndex];
|
|
|
|
|
|
const QPointF& end = vecPos[(nSegmentIndex + 1) % nPointCount];
|
|
|
|
|
|
const QPointF segment = end - start;
|
|
|
|
|
|
const qreal lengthSquared = segment.x() * segment.x() + segment.y() * segment.y();
|
|
|
|
|
|
if (!qIsFinite(lengthSquared) || lengthSquared <= 0.0) {
|
|
|
|
|
|
return true; // 重复端点形成的零长度线段不能继续插点。
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const QPointF fromStart = pt - start;
|
|
|
|
|
|
const qreal projection = (fromStart.x() * segment.x() + fromStart.y() * segment.y())
|
|
|
|
|
|
/ lengthSquared;
|
|
|
|
|
|
if (!qIsFinite(projection)) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
const qreal ratio = qBound(qreal(0.0), projection, qreal(1.0));
|
|
|
|
|
|
const QPointF insertPos = start + segment * ratio;
|
|
|
|
|
|
|
|
|
|
|
|
// 按视口逻辑像素判断重复距离,缩放后仍避免在已有控制点附近生成密集节点。
|
|
|
|
|
|
const QPointF insertViewportPos = getView()->mapFromScene(insertPos);
|
|
|
|
|
|
const qreal minPointDistance = 6.0;
|
|
|
|
|
|
for (int i = 0; i < nPointCount; ++i) {
|
|
|
|
|
|
if (QLineF(getView()->mapFromScene(vecPos[i]), insertViewportPos).length() <= minPointDistance) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const int nInsertIndex = nSegmentIndex + 1;
|
|
|
|
|
|
vecPos.insert(nInsertIndex, insertPos);
|
|
|
|
|
|
// 走框架的整组点提交入口,保留加点前后的点集供撤销、重做;不移动关联图元。
|
|
|
|
|
|
m_pObj->resetBrotherOffset();
|
|
|
|
|
|
if (m_pObj->moveToPos(vecPos)) {
|
|
|
|
|
|
m_pObj->m_oHitOption = OHO_Point;
|
|
|
|
|
|
m_pObj->m_nHitIndex = nInsertIndex;
|
|
|
|
|
|
m_pObj->update();
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool nmObjToolBase::onLeftDoubleClick(const QPointF& pt)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ZxObjToolBase::onLeftDoubleClick(pt);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool nmObjToolBase::onMouseWheel(const QPointF& pt, int delta)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ZxObjToolBase::onMouseWheel(pt, delta);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void nmObjToolBase::onPaint(QPainter* painter, const ZxPaintParam& param)
|
|
|
|
|
|
{
|
|
|
|
|
|
ZxObjToolBase::onPaint(painter, param);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NM_Obj_Type nmObjToolBase::getNOT()
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_oNot;
|
|
|
|
|
|
}
|