You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nmWTAI-Platform/Src/nmNum/nmPlot/nmObjToolBase.cpp

433 lines
15 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <QPainter>
#include <QtCore/qnumeric.h>
#include <QApplication>
#include <QLineF>
#include "ZxObjBase.h"
#include "nmObjBase.h"
#include "nmObjToolBase.h"
#include "nmPlotView.h"
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, 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, 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)) {
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;
}
Qt::CursorShape nmObjToolBase::interactionCursor(int nOption, bool dragging) const
{
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, 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) {
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, 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();
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)
{
// 缩放后旧的屏幕距离已失效,下一次鼠标事件重新计算候选和目标。
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;
}