diff --git a/Include/nmNum/nmPlot/nmGuiPlotCmdHelper.h b/Include/nmNum/nmPlot/nmGuiPlotCmdHelper.h index 8c026d9a..56144e8f 100644 --- a/Include/nmNum/nmPlot/nmGuiPlotCmdHelper.h +++ b/Include/nmNum/nmPlot/nmGuiPlotCmdHelper.h @@ -34,6 +34,8 @@ signals: public slots: void slotObjPtsFinished(QVector& vec); + // 只移除发出取消信号的创建工具,复位按钮;不经过对象创建和数据绑定流程。 + void slotObjCreationCancelled(); void finishMeasure(); // void slotObjFinished(); diff --git a/Include/nmNum/nmPlot/nmObjLineCrackTool.h b/Include/nmNum/nmPlot/nmObjLineCrackTool.h index 4ab24acc..0347d447 100644 --- a/Include/nmNum/nmPlot/nmObjLineCrackTool.h +++ b/Include/nmNum/nmPlot/nmObjLineCrackTool.h @@ -34,6 +34,8 @@ protected: // 由当前 Map 的 Binder 提供候选,按绑定的数据对象排除自身节点。 virtual QVector pointSnapCandidates() const override; + // Esc 退出绘制时丢弃未完成的点集,避免残留到后续鼠标事件。 + virtual void clearCreationPoints() override; protected: diff --git a/Include/nmNum/nmPlot/nmObjLineFaultTool.h b/Include/nmNum/nmPlot/nmObjLineFaultTool.h index 6aa04f8b..5b6d5264 100644 --- a/Include/nmNum/nmPlot/nmObjLineFaultTool.h +++ b/Include/nmNum/nmPlot/nmObjLineFaultTool.h @@ -36,6 +36,8 @@ protected: // 由当前 Map 的 Binder 提供候选,按绑定的数据对象排除自身节点。 virtual QVector pointSnapCandidates() const override; + // Esc 退出绘制时丢弃未完成的点集,避免残留到后续鼠标事件。 + virtual void clearCreationPoints() override; protected: diff --git a/Include/nmNum/nmPlot/nmObjRegionTool.h b/Include/nmNum/nmPlot/nmObjRegionTool.h index ac1c1300..7fb954f8 100644 --- a/Include/nmNum/nmPlot/nmObjRegionTool.h +++ b/Include/nmNum/nmPlot/nmObjRegionTool.h @@ -36,6 +36,8 @@ protected: // 由当前 Map 的 Binder 提供候选,按绑定的数据对象排除自身节点。 virtual QVector pointSnapCandidates() const override; + // Esc 退出绘制时丢弃未完成的点集,避免残留到后续鼠标事件。 + virtual void clearCreationPoints() override; protected: diff --git a/Include/nmNum/nmPlot/nmObjToolBase.h b/Include/nmNum/nmPlot/nmObjToolBase.h index 94b8b830..ae3d5d45 100644 --- a/Include/nmNum/nmPlot/nmObjToolBase.h +++ b/Include/nmNum/nmPlot/nmObjToolBase.h @@ -29,6 +29,8 @@ signals: // 鼠标交互完成(双击) void sigPtsFinished(QVector& vec); + // 仅取消临时绘制工具;命令层负责移除工具及复位工具栏,不创建业务对象。 + void sigCreationCancelled(); public: @@ -40,6 +42,8 @@ public: void resetInteractionCursor(); // 由视图在框架松手收尾后调用,只刷新当前悬停光标,不触发编辑。 void refreshHoverCursor(const QPointF& pt); + // 取消当前绘制或尚未提交的左键移动;没有可取消操作时返回 false。 + bool cancelInteraction(); protected: @@ -48,6 +52,11 @@ protected: // 悬停命中统一映射为箭头、十字或张开的手形。 virtual Qt::CursorShape getCursorBy(const int& nOption, const int& nSubIndex) override; + // 裂缝、断层、区域显式开启 Esc 取消,与光标和吸附开关各自独立。 + void enableEscapeCancellation(); + // 创建取消时由子类丢弃自己的临时点集,不发送绘制完成信号。 + virtual void clearCreationPoints(); + // 由支持节点编辑的工具显式开启;其他图元沿用原有交互。 void enablePointSnapping(); // 子类提供当前 Map 中可连接的场景坐标,并按数据对象身份排除自身。 @@ -69,6 +78,7 @@ protected: bool insertPointOnDoubleClick(const QPointF& pt, bool bClosed); NM_Obj_Type m_oNot; //内部类型标识,不涉及序列化 + bool m_bCreationCancelled; // 临时工具已取消;阻止框架缓存的鼠标回调重新开始绘制。 private: @@ -78,6 +88,11 @@ private: // 根据选择、锁定和命中位置决定光标;整体拖动时使用握紧的手形。 Qt::CursorShape interactionCursor(int nOption, bool dragging) const; + // 当前操作开始后登记到所属视图,使按住鼠标时也能接收 Esc。 + void registerCancellableInteraction(); + + bool m_bEscapeCancellationEnabled; // 仅三个点集编辑工具启用取消,不接管其他工具的 Esc。 + bool m_bConsumeCancelledRelease; // 取消后等待左键松开,期间不再建立拖动或提交坐标。 bool m_bInteractionCursorEnabled; // 仅显式启用的三个工具使用统一光标。 bool m_bCreationCursorActive; // 记录本工具是否开启了视图的光标保持,便于成对释放。 bool m_bPointSnappingEnabled; // 仅裂缝、断层和区域启用公共节点吸附。 diff --git a/Include/nmNum/nmPlot/nmPlotView.h b/Include/nmNum/nmPlot/nmPlotView.h index c76a177e..81c8503c 100644 --- a/Include/nmNum/nmPlot/nmPlotView.h +++ b/Include/nmNum/nmPlot/nmPlotView.h @@ -20,6 +20,8 @@ public: // 工具仅登记本次松手后的光标刷新,由视图在框架收尾后统一执行。 void requestHoverCursorRefresh(nmObjToolBase* tool); + // 登记当前可取消的工具;仅由主动支持 Esc 的绘制、编辑操作调用。 + void setCancellableTool(nmObjToolBase* tool); public: @@ -42,12 +44,15 @@ protected: void mouseMoveEvent(QMouseEvent* e); // 框架完成松手收尾后,恢复本次编辑工具在当前位置的悬停光标。 void mouseReleaseEvent(QMouseEvent* e) override; + // 在框架过滤“鼠标按住时的键盘事件”之前处理 Esc,其余按键沿用框架。 + void keyPressEvent(QKeyEvent* e) override; void focusOutEvent(QFocusEvent *); void leaveEvent(QEvent *); private: + QPointer m_cancellableTool; // 最近开始的可取消操作;工具销毁后自动置空。 QPointer m_pendingCursorTool; // 本次松手请求刷新的工具;销毁后自动置空。 }; diff --git a/Src/nmNum/nmPlot/nmGuiPlotCmdHelper.cpp b/Src/nmNum/nmPlot/nmGuiPlotCmdHelper.cpp index 4d4156b8..a1c29f1d 100644 --- a/Src/nmNum/nmPlot/nmGuiPlotCmdHelper.cpp +++ b/Src/nmNum/nmPlot/nmGuiPlotCmdHelper.cpp @@ -225,6 +225,8 @@ bool nmGuiPlotCmdHelper::runAction(QString sAction) { pObjTool->beginCreationCursor(); connect(pObjTool, SIGNAL(sigPtsFinished(QVector&)), this, SLOT(slotObjPtsFinished(QVector&))); + connect(pObjTool, SIGNAL(sigCreationCancelled()), + this, SLOT(slotObjCreationCancelled())); m_activeTools.insert(NMOT_Line_Fracture, pObjTool); return true; } else if (_isSame(sAction, "nmInsertObjLineFault")) { @@ -235,6 +237,8 @@ bool nmGuiPlotCmdHelper::runAction(QString sAction) { pObjTool->beginCreationCursor(); connect(pObjTool, SIGNAL(sigPtsFinished(QVector&)), this, SLOT(slotObjPtsFinished(QVector&))); + connect(pObjTool, SIGNAL(sigCreationCancelled()), + this, SLOT(slotObjCreationCancelled())); m_activeTools.insert(NMOT_Line_Fault, pObjTool); return true; } else if (_isSame(sAction, "nmInsertObjRegion")) { @@ -245,6 +249,8 @@ bool nmGuiPlotCmdHelper::runAction(QString sAction) { pObjTool->beginCreationCursor(); connect(pObjTool, SIGNAL(sigPtsFinished(QVector&)), this, SLOT(slotObjPtsFinished(QVector&))); + connect(pObjTool, SIGNAL(sigCreationCancelled()), + this, SLOT(slotObjCreationCancelled())); m_activeTools.insert(NMOT_Region, pObjTool); return true; } else if (_isSame(sAction, "nmInsertObjLineMeasuringScale")) { @@ -319,6 +325,19 @@ void nmGuiPlotCmdHelper::cancelActiveTools() finalWithAcm(true, true); } +void nmGuiPlotCmdHelper::slotObjCreationCancelled() +{ + nmObjToolBase* tool = qobject_cast(sender()); + if (tool == nullptr || m_pGuiPlot == nullptr || m_pGuiPlot->m_pPlot == nullptr || + m_activeTools.value(tool->getNOT(), nullptr) != tool) { + return; + } + // 先撤销登记,避免取消旧工具时误清掉后来切换的新工具。 + m_activeTools.remove(tool->getNOT()); + m_pGuiPlot->m_pPlot->removeTools(tool); + finalWithAcm(true, true); +} + void nmGuiPlotCmdHelper::slotObjPtsFinished(QVector& vec) { nmObjToolBase* pTool = dynamic_cast(sender()); if (NULL == pTool) { diff --git a/Src/nmNum/nmPlot/nmObjLineCrackTool.cpp b/Src/nmNum/nmPlot/nmObjLineCrackTool.cpp index 1c460008..9da24656 100644 --- a/Src/nmNum/nmPlot/nmObjLineCrackTool.cpp +++ b/Src/nmNum/nmPlot/nmObjLineCrackTool.cpp @@ -23,11 +23,15 @@ nmObjLineCrackTool::nmObjLineCrackTool() m_vecPoints.clear(); enablePointSnapping(); enableInteractionCursor(); + enableEscapeCancellation(); } bool nmObjLineCrackTool::onLeftDown(const QPointF& pt) { + if (m_bCreationCancelled) { + return true; // 取消的临时工具不再接收新点。 + } if (m_pObj != nullptr) { return nmObjToolBase::onLeftDown(pt); } @@ -43,6 +47,9 @@ bool nmObjLineCrackTool::onLeftDown(const QPointF& pt) bool nmObjLineCrackTool::onMouseMove(const QPointF& pt) { + if (m_bCreationCancelled) { + return true; // 框架仍可能回调旧工具,禁止恢复预览和十字光标。 + } if (m_pObj != nullptr) { return nmObjToolBase::onMouseMove(pt); } @@ -59,6 +66,9 @@ bool nmObjLineCrackTool::onMouseMove(const QPointF& pt) bool nmObjLineCrackTool::onLeftUp(const QPointF& pt) { + if (m_bCreationCancelled) { + return true; // 消费取消后仍在队列中的松手事件。 + } if (m_pObj != nullptr) { // 公共工具区分单节点拖动、整体移动以及双击后的松手。 return nmObjToolBase::onLeftUp(pt); @@ -71,6 +81,9 @@ bool nmObjLineCrackTool::onLeftUp(const QPointF& pt) bool nmObjLineCrackTool::onLeftDoubleClick(const QPointF& pt) { + if (m_bCreationCancelled) { + return true; // 取消不能转成双击完成。 + } if (NULL == m_pObj) // 绘图对象尚未创建(鼠标选点ing) { // 点数不足时吞掉双击,避免误结束创建流程。 @@ -194,3 +207,8 @@ QVector nmObjLineCrackTool::pointSnapCandidates() const { return nmPlotGraphicBinder::pointSnapCandidates(getView(), m_pObj); } + +void nmObjLineCrackTool::clearCreationPoints() +{ + m_vecPoints.clear(); +} diff --git a/Src/nmNum/nmPlot/nmObjLineFaultTool.cpp b/Src/nmNum/nmPlot/nmObjLineFaultTool.cpp index 3d795fae..cd083b6a 100644 --- a/Src/nmNum/nmPlot/nmObjLineFaultTool.cpp +++ b/Src/nmNum/nmPlot/nmObjLineFaultTool.cpp @@ -20,6 +20,7 @@ nmObjLineFaultTool::nmObjLineFaultTool() m_vecPoints.clear(); enablePointSnapping(); enableInteractionCursor(); + enableEscapeCancellation(); } nmObjLineFaultTool::~nmObjLineFaultTool() @@ -28,6 +29,9 @@ nmObjLineFaultTool::~nmObjLineFaultTool() bool nmObjLineFaultTool::onLeftDown(const QPointF& pt) { + if (m_bCreationCancelled) { + return true; // 取消的临时工具不再接收新点。 + } if (m_pObj != nullptr) { return nmObjToolBase::onLeftDown(pt); } @@ -43,6 +47,9 @@ bool nmObjLineFaultTool::onLeftDown(const QPointF& pt) bool nmObjLineFaultTool::onMouseMove(const QPointF& pt) { + if (m_bCreationCancelled) { + return true; // 框架仍可能回调旧工具,禁止恢复预览和十字光标。 + } if (m_pObj != nullptr) { return nmObjToolBase::onMouseMove(pt); } @@ -59,6 +66,9 @@ bool nmObjLineFaultTool::onMouseMove(const QPointF& pt) bool nmObjLineFaultTool::onLeftUp(const QPointF& pt) { + if (m_bCreationCancelled) { + return true; // 消费取消后仍在队列中的松手事件。 + } if (m_pObj != nullptr) { // 公共工具区分单节点拖动、整体移动以及双击后的松手。 return nmObjToolBase::onLeftUp(pt); @@ -71,6 +81,9 @@ bool nmObjLineFaultTool::onLeftUp(const QPointF& pt) bool nmObjLineFaultTool::onLeftDoubleClick(const QPointF& pt) { + if (m_bCreationCancelled) { + return true; // 取消不能转成双击完成。 + } nmObjLineFault* pObj = dynamic_cast(m_pObj); if(NULL == pObj) { // 绘图对象尚未创建(鼠标选点ing) // 点数不足时吞掉双击,避免误结束创建流程。 @@ -181,3 +194,8 @@ QVector nmObjLineFaultTool::pointSnapCandidates() const { return nmPlotGraphicBinder::pointSnapCandidates(getView(), m_pObj); } + +void nmObjLineFaultTool::clearCreationPoints() +{ + m_vecPoints.clear(); +} diff --git a/Src/nmNum/nmPlot/nmObjRegionTool.cpp b/Src/nmNum/nmPlot/nmObjRegionTool.cpp index 9ba58148..89ab0aeb 100644 --- a/Src/nmNum/nmPlot/nmObjRegionTool.cpp +++ b/Src/nmNum/nmPlot/nmObjRegionTool.cpp @@ -16,10 +16,14 @@ nmObjRegionTool::nmObjRegionTool() : nmObjToolBase() m_oNot = NMOT_Region; enablePointSnapping(); enableInteractionCursor(); + enableEscapeCancellation(); } bool nmObjRegionTool::onLeftDown(const QPointF& pt) { + if (m_bCreationCancelled) { + return true; // 取消的临时工具不再接收新点。 + } if (m_pObj != nullptr) { return nmObjToolBase::onLeftDown(pt); } @@ -35,6 +39,9 @@ bool nmObjRegionTool::onLeftDown(const QPointF& pt) bool nmObjRegionTool::onMouseMove(const QPointF& pt) { + if (m_bCreationCancelled) { + return true; // 框架仍可能回调旧工具,禁止恢复预览和十字光标。 + } if (m_pObj != nullptr) { return nmObjToolBase::onMouseMove(pt); } @@ -51,6 +58,9 @@ bool nmObjRegionTool::onMouseMove(const QPointF& pt) bool nmObjRegionTool::onLeftUp(const QPointF& pt) { + if (m_bCreationCancelled) { + return true; // 消费取消后仍在队列中的松手事件。 + } if (m_pObj != nullptr) { // 公共工具区分单节点拖动、整体移动以及双击后的松手。 return nmObjToolBase::onLeftUp(pt); @@ -63,6 +73,9 @@ bool nmObjRegionTool::onLeftUp(const QPointF& pt) bool nmObjRegionTool::onLeftDoubleClick(const QPointF& pt) { + if (m_bCreationCancelled) { + return true; // 取消不能转成双击完成。 + } if(NULL == m_pObj) { // 区域至少需要 3 个点才能闭合成有效面,点数不足时吞掉双击。 if (m_vecPoints.count() < 3) @@ -183,3 +196,8 @@ QVector nmObjRegionTool::pointSnapCandidates() const { return nmPlotGraphicBinder::pointSnapCandidates(getView(), m_pObj); } + +void nmObjRegionTool::clearCreationPoints() +{ + m_vecPoints.clear(); +} diff --git a/Src/nmNum/nmPlot/nmObjToolBase.cpp b/Src/nmNum/nmPlot/nmObjToolBase.cpp index ca76b99c..e210e070 100644 --- a/Src/nmNum/nmPlot/nmObjToolBase.cpp +++ b/Src/nmNum/nmPlot/nmObjToolBase.cpp @@ -10,6 +10,9 @@ 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; @@ -19,10 +22,15 @@ nmObjToolBase::nmObjToolBase() : ZxObjToolBase() 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, false)); @@ -32,6 +40,13 @@ bool nmObjToolBase::onLeftDown(const QPointF& pt) 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; @@ -57,6 +72,14 @@ bool nmObjToolBase::onMouseMove(const QPointF& pt) 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)) { // 单击未必被框架记为“最后活动工具”,由实际处理松手的工具请求刷新。 @@ -84,6 +107,71 @@ bool nmObjToolBase::onLeftUp(const QPointF& pt) return handled; } +void nmObjToolBase::enableEscapeCancellation() +{ + m_bEscapeCancellationEnabled = true; +} + +void nmObjToolBase::registerCancellableInteraction() +{ + if (m_bEscapeCancellationEnabled) { + if (nmPlotView* view = dynamic_cast(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)) { + 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::enableInteractionCursor() { m_bInteractionCursorEnabled = true; @@ -118,6 +206,7 @@ void nmObjToolBase::beginCreationCursor() if (!m_bInteractionCursorEnabled || m_pObj != nullptr || getView() == nullptr) { return; } + registerCancellableInteraction(); m_bCreationCursorActive = true; getView()->setCursorKeeping(true); changeCursor(Qt::CrossCursor); @@ -246,6 +335,9 @@ void nmObjToolBase::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()) { diff --git a/Src/nmNum/nmPlot/nmPlotView.cpp b/Src/nmNum/nmPlot/nmPlotView.cpp index edd41737..1e775e1a 100644 --- a/Src/nmNum/nmPlot/nmPlotView.cpp +++ b/Src/nmNum/nmPlot/nmPlotView.cpp @@ -12,6 +12,7 @@ #include "nmPlotView.h" #include "nmObjToolBase.h" #include +#include ZX_DEFINE_DYNAMIC(mPlotView, nmPlotView) @@ -164,6 +165,26 @@ void nmPlotView::mouseMoveEvent(QMouseEvent* e) } } +void nmPlotView::setCancellableTool(nmObjToolBase* tool) +{ + m_cancellableTool = tool; +} + +void nmPlotView::keyPressEvent(QKeyEvent* e) +{ + if (e->key() == Qt::Key_Escape && m_cancellableTool && + m_cancellableTool->getView() == this && + m_cancellableTool->getRootTool() == getActiveTool() && + m_cancellableTool->cancelInteraction()) { + // 创建取消可能同步移除工具;调用后只清理视图的弱引用,不再访问该工具。 + m_cancellableTool = nullptr; + m_pendingCursorTool = nullptr; + e->accept(); + return; + } + ZxRenderView::keyPressEvent(e); +} + void nmPlotView::requestHoverCursorRefresh(nmObjToolBase* tool) { m_pendingCursorTool = tool;