|
|
|
|
|
#include <QPainter>
|
|
|
|
|
|
#include <QtCore/qnumeric.h>
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
|
|
#include <QLineF>
|
|
|
|
|
|
#include <limits>
|
|
|
|
|
|
#include "ZxObjBase.h"
|
|
|
|
|
|
#include "nmObjBase.h"
|
|
|
|
|
|
#include "nmObjToolBase.h"
|
|
|
|
|
|
#include "nmPlotView.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
|
|
// 只合并实际坐标完全相同的点,不用屏幕距离,避免缩放后误删短线段或不同吸附点。
|
|
|
|
|
|
bool sameCreationPosition(const QPointF& first, const QPointF& second)
|
|
|
|
|
|
{
|
|
|
|
|
|
return first.x() == second.x() && first.y() == second.y();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 以首点为原点、最远点为基线判断是否全共线;平移和缩放不改变判断尺度。
|
|
|
|
|
|
// 这里仅检查退化为一条线的情况,不改变点序,也不执行多边形自交修复。
|
|
|
|
|
|
bool hasNonCollinearCreationPoints(const QVector<QPointF>& points)
|
|
|
|
|
|
{
|
|
|
|
|
|
const QPointF origin = points.first();
|
|
|
|
|
|
QPointF direction;
|
|
|
|
|
|
qreal scale = 0.0;
|
|
|
|
|
|
for (int i = 1; i < points.count(); ++i) {
|
|
|
|
|
|
const QPointF delta = points[i] - origin;
|
|
|
|
|
|
const qreal extent = qMax(qAbs(delta.x()), qAbs(delta.y()));
|
|
|
|
|
|
if (extent > scale) {
|
|
|
|
|
|
scale = extent;
|
|
|
|
|
|
direction = delta;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!qIsFinite(scale) || scale <= 0.0) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
direction /= scale;
|
|
|
|
|
|
// 归一化后只容忍浮点舍入误差,不引入固定像素或模型长度的最小区域限制。
|
|
|
|
|
|
const qreal tolerance = 64.0 * std::numeric_limits<qreal>::epsilon();
|
|
|
|
|
|
for (int i = 1; i < points.count(); ++i) {
|
|
|
|
|
|
const QPointF delta = (points[i] - origin) / scale;
|
|
|
|
|
|
const qreal cross = direction.x() * delta.y() - direction.y() * delta.x();
|
|
|
|
|
|
if (qIsFinite(cross) && qAbs(cross) > tolerance) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
nmObjToolBase::nmObjToolBase() : ZxObjToolBase()
|
|
|
|
|
|
{
|
|
|
|
|
|
m_oNot = NMOT_UNKNWON;
|
|
|
|
|
|
m_bCreationCancelled = false;
|
|
|
|
|
|
m_bEscapeCancellationEnabled = false;
|
|
|
|
|
|
m_bConsumeCancelledRelease = false;
|
|
|
|
|
|
m_bInteractionCursorEnabled = false;
|
|
|
|
|
|
m_bCreationCursorActive = false;
|
|
|
|
|
|
m_bPointSnappingEnabled = false;
|
|
|
|
|
|
m_nSnapTargetIndex = -1;
|
|
|
|
|
|
m_bConsumeInsertPointRelease = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool nmObjToolBase::onLeftDown(const QPointF& pt)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 新按下表示新一轮操作,失焦丢失的松手不能影响后续编辑。
|
|
|
|
|
|
m_bConsumeCancelledRelease = false;
|
|
|
|
|
|
// 上一次双击若因窗口失焦未收到松手,不影响下一次正常按下。
|
|
|
|
|
|
m_bConsumeInsertPointRelease = false;
|
|
|
|
|
|
clearPointSnap();
|
|
|
|
|
|
const bool handled = ZxObjToolBase::onLeftDown(pt);
|
|
|
|
|
|
if (handled) {
|
|
|
|
|
|
registerCancellableInteraction();
|
|
|
|
|
|
}
|
|
|
|
|
|
if (handled && m_bInteractionCursorEnabled && m_pObj != nullptr) {
|
|
|
|
|
|
// 按下时还未开始拖动,保持悬停样式;实际移动后再切为握手。
|
|
|
|
|
|
changeCursor(interactionCursor(m_oHitOption, m_pObj->m_nHitIndex, false));
|
|
|
|
|
|
}
|
|
|
|
|
|
return handled;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool nmObjToolBase::onMouseMove(const QPointF& pt)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_bConsumeCancelledRelease) {
|
|
|
|
|
|
if (QApplication::mouseButtons().testFlag(Qt::LeftButton)) {
|
|
|
|
|
|
return true; // Esc 后仍按住左键移动时,不让框架重新进入拖动。
|
|
|
|
|
|
}
|
|
|
|
|
|
// 松手发生在窗口外时也可恢复悬停;取消时已清空命中,不会提交旧移动。
|
|
|
|
|
|
m_bConsumeCancelledRelease = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (m_bConsumeInsertPointRelease) {
|
|
|
|
|
|
if (QApplication::mouseButtons().testFlag(Qt::LeftButton)) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 窗口失焦可能丢失松手事件,左键已松开时立即恢复悬停命中。
|
|
|
|
|
|
m_bConsumeInsertPointRelease = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
const bool handled = ZxObjToolBase::onMouseMove(pt);
|
|
|
|
|
|
if (handled && m_bInteractionCursorEnabled && m_pObj != nullptr &&
|
|
|
|
|
|
m_bTracking && isLeftDown() && !isClick()) {
|
|
|
|
|
|
// 框架只在悬停分支调用 getCursorBy,拖动分支需显式更新光标。
|
|
|
|
|
|
changeCursor(interactionCursor(m_oHitOption, m_pObj->m_nHitIndex, true));
|
|
|
|
|
|
}
|
|
|
|
|
|
if (m_bPointSnappingEnabled) {
|
|
|
|
|
|
if (isPointSnapDrag()) {
|
|
|
|
|
|
updatePointSnap(pt);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
clearPointSnap();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return handled;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool nmObjToolBase::onLeftUp(const QPointF& pt)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_bConsumeCancelledRelease) {
|
|
|
|
|
|
m_bConsumeCancelledRelease = false;
|
|
|
|
|
|
m_bTracking = false;
|
|
|
|
|
|
m_oHitOption = OHO_None;
|
|
|
|
|
|
// 保留框架的松手收尾,但不进入移动提交和吸附分支,也不恢复旧命中光标。
|
|
|
|
|
|
ZxObjToolBase::onLeftUp(pt);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (m_bInteractionCursorEnabled && m_pObj != nullptr &&
|
|
|
|
|
|
(m_oHitOption != OHO_None || m_bConsumeInsertPointRelease)) {
|
|
|
|
|
|
// 单击未必被框架记为“最后活动工具”,由实际处理松手的工具请求刷新。
|
|
|
|
|
|
// 视图会等框架全部收尾后再回调,避免刚设置的悬停光标被重置。
|
|
|
|
|
|
if (nmPlotView* view = dynamic_cast<nmPlotView*>(getView())) {
|
|
|
|
|
|
view->requestHoverCursorRefresh(this);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 使用松手时的最新位置重新确认目标,与预览共用同一套距离规则。
|
|
|
|
|
|
const QPointF releasePos = isPointSnapDrag() ? updatePointSnap(pt) : pt;
|
|
|
|
|
|
const bool handled = ZxObjToolBase::onLeftUp(releasePos);
|
|
|
|
|
|
clearPointSnap();
|
|
|
|
|
|
return handled;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void nmObjToolBase::enableEscapeCancellation()
|
|
|
|
|
|
{
|
|
|
|
|
|
m_bEscapeCancellationEnabled = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void nmObjToolBase::registerCancellableInteraction()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_bEscapeCancellationEnabled) {
|
|
|
|
|
|
if (nmPlotView* view = dynamic_cast<nmPlotView*>(getView())) {
|
|
|
|
|
|
view->setCancellableTool(this);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void nmObjToolBase::clearCreationPoints()
|
|
|
|
|
|
{
|
|
|
|
|
|
// 其他工具默认没有需要清理的点集,点集绘制工具分别实现。
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool nmObjToolBase::cancelInteraction()
|
|
|
|
|
|
{
|
|
|
|
|
|
ZxRenderView* view = getView();
|
|
|
|
|
|
if (!m_bEscapeCancellationEnabled || view == nullptr || !isEnabled() ||
|
|
|
|
|
|
getCompoundTool() == nullptr || m_bCreationCancelled) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
const bool creating = m_pObj == nullptr;
|
|
|
|
|
|
if (creating) {
|
|
|
|
|
|
if (!m_bCreationCursorActive) {
|
|
|
|
|
|
return false; // 已完成的临时工具不能再次取消。
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if (!isLeftDown() || m_bConsumeInsertPointRelease ||
|
|
|
|
|
|
(m_oHitOption != OHO_Point && m_oHitOption != OHO_Bound &&
|
|
|
|
|
|
m_oHitOption != OHO_Inner)) {
|
|
|
|
|
|
return false; // 空闲选择和已经提交的双击加点不属于待取消的移动。
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
m_bConsumeCancelledRelease = isLeftDown();
|
|
|
|
|
|
m_bTracking = false;
|
|
|
|
|
|
m_oHitOption = OHO_None;
|
|
|
|
|
|
m_bHitTestMoving = false;
|
|
|
|
|
|
m_bConsumeInsertPointRelease = false;
|
|
|
|
|
|
if (creating) {
|
|
|
|
|
|
m_bCreationCancelled = true;
|
|
|
|
|
|
clearCreationPoints();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 移动尚未提交,清理预览状态即可;不写回点集,也不产生撤销记录。
|
|
|
|
|
|
m_pObj->m_oHitOption = OHO_None;
|
|
|
|
|
|
m_pObj->m_nHitIndex = -1;
|
|
|
|
|
|
m_pObj->resetBrotherOffset();
|
|
|
|
|
|
m_pObj->update();
|
|
|
|
|
|
}
|
|
|
|
|
|
clearPointSnap();
|
|
|
|
|
|
resetInteractionCursor();
|
|
|
|
|
|
view->setSnapshotMode(false);
|
|
|
|
|
|
view->restoreCursor();
|
|
|
|
|
|
updateScene();
|
|
|
|
|
|
|
|
|
|
|
|
if (creating) {
|
|
|
|
|
|
// removeTools 可能仍留有按下工具缓存,保留取消标记以消费随后的鼠标回调。
|
|
|
|
|
|
setEnabled(false);
|
|
|
|
|
|
emit sigCreationCancelled();
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void nmObjToolBase::setCreationHint(const QString& hint)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (nmPlotView* view = dynamic_cast<nmPlotView*>(getView())) {
|
|
|
|
|
|
view->setCreationHint(hint);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void nmObjToolBase::appendCreationPoint(QVector<QPointF>& points, const QPointF& pt)
|
|
|
|
|
|
{
|
|
|
|
|
|
beginCreationCursor();
|
|
|
|
|
|
m_ptMove = pt;
|
|
|
|
|
|
const QPointF position = updatePointSnap(pt);
|
|
|
|
|
|
if (!qIsFinite(position.x()) || !qIsFinite(position.y())) {
|
|
|
|
|
|
setCreationHint(tr("Invalid position. Please select another point."));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!points.isEmpty() && sameCreationPosition(points.last(), position)) {
|
|
|
|
|
|
setCreationHint(tr("This point coincides with the previous node. Please select another position."));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
points.push_back(position);
|
|
|
|
|
|
m_ptStart = position;
|
|
|
|
|
|
m_bTracking = true;
|
|
|
|
|
|
setCreationHint(QString());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool nmObjToolBase::stepBackCreation(bool autoRepeat)
|
|
|
|
|
|
{
|
|
|
|
|
|
Q_UNUSED(autoRepeat);
|
|
|
|
|
|
return false; // 只有显式支持退点的创建工具接管 Backspace。
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool nmObjToolBase::stepBackCreationPoints(QVector<QPointF>& points, bool autoRepeat)
|
|
|
|
|
|
{
|
|
|
|
|
|
ZxRenderView* view = getView();
|
|
|
|
|
|
if (m_pObj != nullptr || view == nullptr || !isEnabled() ||
|
|
|
|
|
|
getCompoundTool() == nullptr || !m_bCreationCursorActive || m_bCreationCancelled) {
|
|
|
|
|
|
return false; // 已创建、已完成或已取消的对象不能通过退格修改。
|
|
|
|
|
|
}
|
|
|
|
|
|
if (autoRepeat || QApplication::mouseButtons() != Qt::NoButton) {
|
|
|
|
|
|
return true; // 长按不连删;鼠标点击尚未结束时也不改变当前点集。
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
setCreationHint(QString());
|
|
|
|
|
|
if (points.isEmpty()) {
|
|
|
|
|
|
return true; // 没有节点时仍接管退格,保留当前绘制模式。
|
|
|
|
|
|
}
|
|
|
|
|
|
points.pop_back();
|
|
|
|
|
|
m_bTracking = !points.isEmpty();
|
|
|
|
|
|
m_ptStart = m_bTracking ? points.last() : QPointF();
|
|
|
|
|
|
|
|
|
|
|
|
// 点集改变后重新绘制整层,擦除已删除的边及区域闭合预览,不依赖鼠标移动。
|
|
|
|
|
|
view->setSnapshotMode(false);
|
|
|
|
|
|
if (m_bTracking) {
|
|
|
|
|
|
updatePointSnap(m_ptMove); // 鼠标位置保持不变,下一段从新的末节点开始预览。
|
|
|
|
|
|
} else {
|
|
|
|
|
|
clearPointSnap(); // 删空时清除残留标记,下一次移动或点击重新计算吸附。
|
|
|
|
|
|
}
|
|
|
|
|
|
updateScene();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void nmObjToolBase::finishPolylineCreation(QVector<QPointF>& points, bool closed)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 先检查副本,失败时保留全部已选节点,避免完成失败后预览起点发生跳变。
|
|
|
|
|
|
QVector<QPointF> finishedPoints = points;
|
|
|
|
|
|
if (closed && finishedPoints.count() > 1 &&
|
|
|
|
|
|
sameCreationPosition(finishedPoints.first(), finishedPoints.last())) {
|
|
|
|
|
|
finishedPoints.pop_back(); // 区域由图元自动闭合,不保存重复首点形成的零长边。
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 只需识别前两个/三个不同点,不让 A、B、A 这样的重复点击凑够区域点数。
|
|
|
|
|
|
const int minimumPointCount = closed ? 3 : 2;
|
|
|
|
|
|
QVector<QPointF> distinctPoints;
|
|
|
|
|
|
for (int i = 0; i < finishedPoints.count() &&
|
|
|
|
|
|
distinctPoints.count() < minimumPointCount; ++i) {
|
|
|
|
|
|
bool repeated = false;
|
|
|
|
|
|
for (int j = 0; j < distinctPoints.count(); ++j) {
|
|
|
|
|
|
if (sameCreationPosition(distinctPoints[j], finishedPoints[i])) {
|
|
|
|
|
|
repeated = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!repeated) {
|
|
|
|
|
|
distinctPoints.push_back(finishedPoints[i]);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (distinctPoints.count() < minimumPointCount) {
|
|
|
|
|
|
setCreationHint(closed
|
|
|
|
|
|
? tr("At least three distinct points are required. Please continue drawing.")
|
|
|
|
|
|
: tr("A fracture or fault needs at least two distinct points. Please continue drawing."));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (closed && !hasNonCollinearCreationPoints(finishedPoints)) {
|
|
|
|
|
|
setCreationHint(tr("The points cannot all lie on one straight line. Please continue drawing."));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 完成信号可能同步移除本工具;先结束预览并清理状态,发送后不再访问成员。
|
|
|
|
|
|
points.clear();
|
|
|
|
|
|
m_bTracking = false;
|
|
|
|
|
|
resetInteractionCursor();
|
|
|
|
|
|
clearPointSnap();
|
|
|
|
|
|
emit sigPtsFinished(finishedPoints);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void nmObjToolBase::enableInteractionCursor()
|
|
|
|
|
|
{
|
|
|
|
|
|
m_bInteractionCursorEnabled = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Qt::CursorShape nmObjToolBase::interactionCursor(int nOption, int nSubIndex, bool dragging) const
|
|
|
|
|
|
{
|
|
|
|
|
|
Q_UNUSED(nSubIndex); // 普通点集只区分点、边;矩形会按具体角和边选择方向。
|
|
|
|
|
|
if (m_pObj == nullptr || !m_pObj->isSelected() || !m_pObj->isVisible() ||
|
|
|
|
|
|
m_pObj->isReadOnly() || m_pObj->isLockPos()) {
|
|
|
|
|
|
return Qt::ArrowCursor;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (nOption == OHO_Point) {
|
|
|
|
|
|
return m_pObj->isLockSize() ? Qt::ArrowCursor : Qt::CrossCursor;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (nOption == OHO_Bound) {
|
|
|
|
|
|
// 锁定尺寸不妨碍整体平移;是否允许移动由位置锁定决定。
|
|
|
|
|
|
return dragging ? Qt::ClosedHandCursor : Qt::OpenHandCursor;
|
|
|
|
|
|
}
|
|
|
|
|
|
return Qt::ArrowCursor;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Qt::CursorShape nmObjToolBase::getCursorBy(const int& nOption, const int& nSubIndex)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_bInteractionCursorEnabled) {
|
|
|
|
|
|
return ZxObjToolBase::getCursorBy(nOption, nSubIndex);
|
|
|
|
|
|
}
|
|
|
|
|
|
return interactionCursor(nOption, nSubIndex, false);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void nmObjToolBase::beginCreationCursor()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_bInteractionCursorEnabled || m_pObj != nullptr || getView() == nullptr) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
registerCancellableInteraction();
|
|
|
|
|
|
m_bCreationCursorActive = true;
|
|
|
|
|
|
getView()->setCursorKeeping(true);
|
|
|
|
|
|
changeCursor(Qt::CrossCursor);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void nmObjToolBase::resetInteractionCursor()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_bInteractionCursorEnabled) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
ZxRenderView* view = getView();
|
|
|
|
|
|
const bool hadCreationCursor = m_bCreationCursorActive;
|
|
|
|
|
|
m_bCreationCursorActive = false;
|
|
|
|
|
|
if (view != nullptr) {
|
|
|
|
|
|
// 只释放本工具开启的保持状态,避免影响其他创建工具。
|
|
|
|
|
|
if (hadCreationCursor) {
|
|
|
|
|
|
setCreationHint(QString());
|
|
|
|
|
|
view->setCursorKeeping(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (hadCreationCursor || getLastActivateTool() == this) {
|
|
|
|
|
|
view->restoreCursor();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void nmObjToolBase::refreshHoverCursor(const QPointF& pt)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_bInteractionCursorEnabled || m_pObj == nullptr || getView() == nullptr ||
|
|
|
|
|
|
!isEnabled() || getCompoundTool() == nullptr) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
int option = -1;
|
|
|
|
|
|
int index = -1;
|
|
|
|
|
|
// 重新查询提交后的几何位置,不复用拖动前的命中,也不再次分发鼠标事件。
|
|
|
|
|
|
if (m_pObj->isVisible() && m_pObj->_runHitTest(pt, option, index)) {
|
|
|
|
|
|
changeCursor(interactionCursor(option, index, false));
|
|
|
|
|
|
} else {
|
|
|
|
|
|
getView()->restoreCursor();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void nmObjToolBase::enablePointSnapping()
|
|
|
|
|
|
{
|
|
|
|
|
|
m_bPointSnappingEnabled = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QVector<QPointF> nmObjToolBase::pointSnapCandidates() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return QVector<QPointF>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool nmObjToolBase::isPointSnapDrag()
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_bPointSnappingEnabled && !m_bConsumeInsertPointRelease &&
|
|
|
|
|
|
m_pObj != nullptr && m_bTracking && m_oHitOption == OHO_Point &&
|
|
|
|
|
|
m_pObj->isSelected() && !m_pObj->isReadOnly() &&
|
|
|
|
|
|
!m_pObj->isLockPos() && !m_pObj->isLockSize() && !isClick();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QPointF nmObjToolBase::updatePointSnap(const QPointF& pt)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_bPointSnappingEnabled || getView() == nullptr) {
|
|
|
|
|
|
return pt;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
m_vecSnapCandidates = pointSnapCandidates();
|
|
|
|
|
|
m_nSnapTargetIndex = -1;
|
|
|
|
|
|
const QPointF mousePos = getView()->mapFromScene(pt);
|
|
|
|
|
|
// 统一使用视口逻辑像素;缩放地图不改变用户需要靠近目标的屏幕距离。
|
|
|
|
|
|
const qreal snapRadius = 8.0;
|
|
|
|
|
|
qreal nearestDistanceSquared = snapRadius * snapRadius;
|
|
|
|
|
|
for (int i = 0; i < m_vecSnapCandidates.count(); ++i) {
|
|
|
|
|
|
const QPointF delta = getView()->mapFromScene(m_vecSnapCandidates[i]) - mousePos;
|
|
|
|
|
|
const qreal distanceSquared = delta.x() * delta.x() + delta.y() * delta.y();
|
|
|
|
|
|
if (!qIsFinite(distanceSquared) || distanceSquared > nearestDistanceSquared) {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 距离相等时保留首个目标,避免同坐标候选反复覆盖;始终只有一个高亮。
|
|
|
|
|
|
if (m_nSnapTargetIndex < 0 || distanceSquared < nearestDistanceSquared) {
|
|
|
|
|
|
m_nSnapTargetIndex = i;
|
|
|
|
|
|
nearestDistanceSquared = distanceSquared;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 候选点可能位于图元外,整层刷新同时擦除上一次的吸附标记。
|
|
|
|
|
|
updateScene();
|
|
|
|
|
|
return pointSnapPosition(pt);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QPointF nmObjToolBase::pointSnapPosition(const QPointF& pt) const
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_nSnapTargetIndex >= 0 && m_nSnapTargetIndex < m_vecSnapCandidates.count()) {
|
|
|
|
|
|
return m_vecSnapCandidates[m_nSnapTargetIndex];
|
|
|
|
|
|
}
|
|
|
|
|
|
return pt;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void nmObjToolBase::paintPointSnapTargets(QPainter* painter) const
|
|
|
|
|
|
{
|
|
|
|
|
|
if (getView() == nullptr || m_nSnapTargetIndex < 0 ||
|
|
|
|
|
|
m_nSnapTargetIndex >= m_vecSnapCandidates.count() ||
|
|
|
|
|
|
(m_pObj != nullptr && !m_pObj->isSelected())) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 吸附目标比普通节点大一圈,绿色填充配深色细边,便于在不同背景上辨认。
|
|
|
|
|
|
nmObjBase::paintNodeHandle(painter, m_vecSnapCandidates[m_nSnapTargetIndex],
|
|
|
|
|
|
8, QColor(40, 40, 40), QColor(0, 200, 0));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void nmObjToolBase::clearPointSnap()
|
|
|
|
|
|
{
|
|
|
|
|
|
const bool hadCandidates = !m_vecSnapCandidates.isEmpty();
|
|
|
|
|
|
m_vecSnapCandidates.clear();
|
|
|
|
|
|
m_nSnapTargetIndex = -1;
|
|
|
|
|
|
if (hadCandidates && getView() != nullptr) {
|
|
|
|
|
|
updateScene();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void nmObjToolBase::onDeactivated()
|
|
|
|
|
|
{
|
|
|
|
|
|
resetInteractionCursor();
|
|
|
|
|
|
clearPointSnap();
|
|
|
|
|
|
ZxObjToolBase::onDeactivated();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool nmObjToolBase::insertPointOnDoubleClick(const QPointF& pt, bool bClosed)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_bConsumeCancelledRelease) {
|
|
|
|
|
|
return true; // 取消后的同一次按压不能转成双击加点。
|
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
|
clearPointSnap();
|
|
|
|
|
|
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();
|
|
|
|
|
|
nmObjBase* object = qobject_cast<nmObjBase*>(m_pObj);
|
|
|
|
|
|
if (object != nullptr && object->commitPointPositions(vecPos, nInsertIndex)) {
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 缩放后旧的屏幕距离已失效,下一次鼠标事件重新计算候选和目标。
|
|
|
|
|
|
clearPointSnap();
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|