fix(nmPlot): 防止误点或取消重测时清除已有结果

- 松手并通过长度校验后才替换旧测量线和面板数值
- 忽略误点和过短拖动,支持 Esc 取消本次测量
- 保留首次单击设定最近井拟合参考点的操作
- 关闭测量面板时清理预览和光标,恢复地图编辑
feature/fix-result-persistence-20261118
lh 1 week ago
parent 1cccc64efd
commit 93fb9b75aa

@ -52,6 +52,8 @@ class NM_PLOT_EXPORT nmGuiPlot : public iGuiPlot {
// 创建并添加图元,用 vec 设置图元的绘图坐标,完成创建通知并标记地图已修改。
// sName 为输入输出参数:普通图元会自动避开重名,边界允许使用空名。
nmObjBase* appendOneObj(NM_Obj_Type o, QString& sName, QVector<QPointF>& vec);
// 移除旧测量线及其绑定,保留共享测量数据和指定的新线段。
void removeMeasureGraphics(const ZxObjBase* keptGraphic = nullptr);
// 创建图元并注册信号和工具,同时设置边界置底、区域位于标记下方的顺序。
// 此函数不设置图元坐标,坐标由 appendOneObj 随后写入。
nmObjBase* _createOneObj(NM_Obj_Type o, QString& sName);
@ -130,8 +132,6 @@ class NM_PLOT_EXPORT nmGuiPlot : public iGuiPlot {
// 删除本 Map 的测量图元和数据,不访问全局当前分析。
void deleteMeasureObjects();
// 开始新一次测量时清空本 Map 的线段,保留数据对象供对话框继续监听。
void clearPreviousMeasure();
// 结束测量
void finishMeasure();
@ -151,8 +151,8 @@ class NM_PLOT_EXPORT nmGuiPlot : public iGuiPlot {
// 绘图对象数据点发生改变
void sigObjPtsChanged(ZxObjBase*);
// 开始一次测量
void sigMeasureStarted();
// 新测量已写入面板使用的数据对象。
void sigMeasureCommitted();
public:

@ -7,6 +7,8 @@
#include "nmDefines.h"
#include "nmObjToolBase.h"
class nmGuiPlot;
// 该类是数值试井绘图载体的样例nmGuiPlot的Cmd命令分发处理
class NM_PLOT_EXPORT nmGuiPlotCmdHelper
: public iGuiPlotCmdHelper
@ -29,7 +31,7 @@ public:
void insertOneWell();
signals:
void sigMeasureStarted();
void sigMeasureCommitted();
public slots:
@ -44,5 +46,8 @@ protected:
friend class nmGuiPlot;
private:
// 校验模型坐标,成功创建新线后再替换旧测量。
void commitMeasure(nmGuiPlot* plot, QVector<QPointF>& points);
QMap<NM_Obj_Type,nmObjToolBase*> m_activeTools;
};

@ -8,8 +8,14 @@ class NM_PLOT_EXPORT nmObjLineMeasureTool : public nmObjLineTool
public:
nmObjLineMeasureTool();
// 按下时记录本次起点,松手后才提交有效测量。
virtual bool onLeftDown(const QPointF& pt) override;
signals:
void sigMeasureStarted();
};
virtual bool onLeftUp(const QPointF& pt) override;
// Esc 只取消正在拖动的预览,保留上次结果和测量工具。
virtual bool cancelInteraction() override;
virtual void onPaint(QPainter* painter, const ZxPaintParam& param) override;
private:
// 结束本次手势并清除预览,供松手和 Esc 共用。
void clearMeasurePreview();
};

@ -43,7 +43,7 @@ public:
// 由视图在框架松手收尾后调用,只刷新当前悬停光标,不触发编辑。
void refreshHoverCursor(const QPointF& pt);
// 取消当前绘制或尚未提交的左键移动;没有可取消操作时返回 false。
bool cancelInteraction();
virtual bool cancelInteraction();
// Backspace 退回创建中的最后一个节点;返回 true 表示本工具已接管该按键。
// autoRepeat 为键盘长按连发标记,接管后不再删点,保证一次按压只退一个点。
virtual bool stepBackCreation(bool autoRepeat);

@ -32,7 +32,6 @@
#include "nmPlotDialogContextProvider.h"
#include "nmDataLogFile.h"
#include "nmDataMeasure.h"
#include "nmDataMeasuringScale.h"
#include "nmSingalCenter.h"
@ -136,7 +135,7 @@ nmGuiPlot::nmGuiPlot(bool bUseBtns, QWidget *parent) :
m_pCmdHelper = new nmGuiPlotCmdHelper(this);
Q_ASSERT(NULL != m_pCmdHelper);
connect(m_pCmdHelper, SIGNAL(sigMeasureStarted()), this, SIGNAL(sigMeasureStarted()));
connect(m_pCmdHelper, SIGNAL(sigMeasureCommitted()), this, SIGNAL(sigMeasureCommitted()));
m_sChartCmdType = "NmDemo2DType";
@ -1375,17 +1374,7 @@ void nmGuiPlot::deleteMeasureObjects()
return;
}
QVector<ZxObjBase*> measureObjs = getObjsByTag("nObjLineMeasure");
for(int i = 0; i < measureObjs.count(); i++) {
if(measureObjs[i] != nullptr) {
nmObjLineMeasure* pMeasure = dynamic_cast<nmObjLineMeasure*>(measureObjs[i]);
if(pMeasure) {
pMeasure->removeData();
}
m_pPlot->removeObjByName(measureObjs[i]->getName());
}
}
removeMeasureGraphics();
// 先解除本 Map 的图元绑定,再删除所属分析的测量数据。
// 非模态对话框关闭时,当前页签可能已切换,不能使用全局当前分析。
@ -1397,35 +1386,19 @@ void nmGuiPlot::deleteMeasureObjects()
setAllPlotObjectsEditable(true);
}
void nmGuiPlot::clearPreviousMeasure()
void nmGuiPlot::removeMeasureGraphics(const ZxObjBase* keptGraphic)
{
if(m_pPlot == nullptr) {
return;
}
QVector<ZxObjBase*> measureObjs = getObjsByTag("nObjLineMeasure");
for(int i = 0; i < measureObjs.count(); i++) {
if(measureObjs[i] != nullptr) {
nmObjLineMeasure* pMeasure = dynamic_cast<nmObjLineMeasure*>(measureObjs[i]);
if(pMeasure) {
pMeasure->removeData();
}
m_pPlot->removeObjByName(measureObjs[i]->getName());
for (int i = m_pPlot->getObjCount() - 1; i >= 0; --i) {
nmObjLineMeasure* measure = dynamic_cast<nmObjLineMeasure*>(m_pPlot->getObjByIndex(i));
if (measure != nullptr && measure != keptGraphic) {
measure->removeData();
m_pPlot->removeObjByIndex(i);
}
}
// 连续测量复用同一数据对象,保证对话框的刷新连接仍然有效。
nmDataMeasure* data = m_pDataManager != nullptr ? m_pDataManager->getMeasureData() : nullptr;
if(data) {
data->setStartPoint(QPointF(0, 0));
data->setEndPoint(QPointF(0, 0));
data->setCurrentPoint(QPointF(0, 0));
nmDataAttribute lenAttr = data->getLength();
lenAttr.setValue(0.0);
data->setLength(lenAttr);
}
}
void nmGuiPlot::setAllPlotObjectsEditable(bool editable)

@ -1,6 +1,8 @@
#include "ZxBaseUtil.h"
#include "zxLogInstance.h"
#include <QLineF>
#include <QWidget>
#include <QtCore/qnumeric.h>
#include "ZxPlot.h"
#include "ZxSubAxisX.h"
@ -283,11 +285,8 @@ bool nmGuiPlotCmdHelper::runAction(QString sAction) {
pPlot->appendTools(pObjTool);
connect(pObjTool, SIGNAL(sigPtsFinished(QVector<QPointF>&)), \
this, SLOT(slotObjPtsFinished(QVector<QPointF>&)));
connect(pMeasureTool, SIGNAL(sigMeasureStarted()), \
pNmPlot, SLOT(clearPreviousMeasure()));
connect(pMeasureTool, SIGNAL(sigMeasureStarted()), \
this, SIGNAL(sigMeasureStarted()));
m_activeTools.insert(NMOT_Line_Measure, pObjTool);
pMeasureTool->beginCreationCursor();
return true;
} else if (_isSame(sAction, "nmDeleteOneObj")) {
nmObjToolBase* pObjTool = new nmObjDeleteTool(pPlot);
@ -359,13 +358,17 @@ void nmGuiPlotCmdHelper::slotObjPtsFinished(QVector<QPointF>& vec) {
ZxPlot* pPlot = pWxPlot->m_pPlot;
Q_ASSERT (NULL != pPlot);
// 如果不为测量工具则移除
if (pTool->getNOT() != NMOT_Line_Measure) {
pTool->resetInteractionCursor();
pPlot->removeTools(pTool);
// 从活跃 map 里清除它
m_activeTools.remove(pTool->getNOT());
// 连续测量保留工具。已退出的工具不能通过延迟到达的松手事件提交。
const NM_Obj_Type type = pTool->getNOT();
if (type == NMOT_Line_Measure) {
if (m_activeTools.value(type, nullptr) == pTool) {
commitMeasure(pWxPlot, vec);
}
return;
}
pTool->resetInteractionCursor();
pPlot->removeTools(pTool);
m_activeTools.remove(type);
QString sName = tr("nmObj"); // 默认名称
@ -386,8 +389,6 @@ void nmGuiPlotCmdHelper::slotObjPtsFinished(QVector<QPointF>& vec) {
sName = tr("Fault");
} else if (pTool->getNOT() == NMOT_Line_MeasuringScale) {
sName = tr("MeasuringScale");
} else if (pTool->getNOT() == NMOT_Line_Measure) {
sName = tr("Measure");
} else if (pTool->getNOT() == NMOT_Rect) {
sName = tr("Rectangle");
} else if (pTool->getNOT() == NMOT_RectOutline) {
@ -448,14 +449,6 @@ void nmGuiPlotCmdHelper::slotObjPtsFinished(QVector<QPointF>& vec) {
binder->createOutlineForGraphic(pPolygon);
}
}
} else if (pTool->getNOT() == NMOT_Line_Measure) {
// 先执行 afterCreated(设置锁定、只读等属性)
pObj->afterCreated();
// 再通过 Binder 创建数据并建立绑定
nmObjLineMeasure* pMeasure = dynamic_cast<nmObjLineMeasure*>(pObj);
if (pMeasure && pWxPlot->graphicBinder()) {
pWxPlot->graphicBinder()->createMeasureForGraphic(pMeasure);
}
} else if (pTool->getNOT() == NMOT_RegionMark) {
// 先执行 afterCreated(图元内部初始化)
pObj->afterCreated();
@ -484,6 +477,44 @@ void nmGuiPlotCmdHelper::slotObjPtsFinished(QVector<QPointF>& vec) {
}
}
void nmGuiPlotCmdHelper::commitMeasure(nmGuiPlot* plot, QVector<QPointF>& points)
{
ZxPlot* chart = plot->m_pPlot;
nmPlotGraphicBinder* binder = plot->graphicBinder();
if (chart == nullptr || binder == nullptr || plot->boundDataManager() == nullptr ||
points.count() != 2) {
return;
}
const ZxSubAxisX* axisX = chart->getMainAxisX();
const ZxSubAxisY* axisY = chart->getMainAxisY();
if (axisX == nullptr || axisY == nullptr ||
axisX->getRangeMin() == axisX->getRangeMax() ||
axisY->getRangeMin() == axisY->getRangeMax()) {
return;
}
// 首次单击仍可选取拟合参考点。已有测量时,只允许有效线段替换。
const bool singlePoint = points[0] == points[1];
if (singlePoint && !plot->getObjsByTag("nObjLineMeasure").isEmpty()) return;
const QVector<QPointF> values = chart->getValueForPos(points);
if (values.count() != 2) return;
const qreal length = QLineF(values[0], values[1]).length();
if (!qIsFinite(length) || (!singlePoint && length <= 0.0)) return;
QString name = tr("Measure");
nmObjLineMeasure* measure = static_cast<nmObjLineMeasure*>(
plot->appendOneObj(NMOT_Line_Measure, name, points));
if (measure == nullptr) return;
// 新线已创建,先断开旧线绑定,再写入供面板监听的同一个数据对象。
plot->removeMeasureGraphics(measure);
measure->afterCreated();
binder->createMeasureForGraphic(measure);
emit sigMeasureCommitted();
}
void nmGuiPlotCmdHelper::finishMeasure()
{
nmGuiPlot* pWxPlot = dynamic_cast<nmGuiPlot*>(m_pGuiPlot);
@ -493,8 +524,10 @@ void nmGuiPlotCmdHelper::finishMeasure()
nmObjToolBase* pMeasureTool = m_activeTools.value(NMOT_Line_Measure, nullptr);
if(pMeasureTool) {
pWxPlot->m_pPlot->removeTools(pMeasureTool);
m_activeTools.remove(NMOT_Line_Measure);
pMeasureTool->cancelInteraction();
pMeasureTool->resetInteractionCursor();
pWxPlot->m_pPlot->removeTools(pMeasureTool);
}
pWxPlot->deleteMeasureObjects();

@ -1,6 +1,9 @@
#include <QPainter>
#include "nmObjLine.h"
#include <QLineF>
#include <QtCore/qnumeric.h>
#include "ZxRenderView.h"
#include "nmObjLineMeasureTool.h"
#include "nmDataLogFile.h"
@ -8,18 +11,75 @@ nmObjLineMeasureTool::nmObjLineMeasureTool()
{
setText(tr("nObjLineMeasureTool"));
m_oNot = NMOT_Line_Measure;
enableInteractionCursor();
enableEscapeCancellation();
nmDataLogFile::getInstance()->writeLog(" nmObjLineMeasureTool ++++++++ " + QString::number(m_oNot));
}
bool nmObjLineMeasureTool::onLeftDown(const QPointF& pt)
{
if (NULL == m_pObj)
{
emit sigMeasureStarted();
m_ptStart = pt;
m_bTracking = true;
if (m_pObj == nullptr) {
beginCreationCursor(); // 每次按下重新登记,可连续取消多次测量。
}
return nmObjLineTool::onLeftDown(pt);
}
bool nmObjLineMeasureTool::onLeftUp(const QPointF& pt)
{
if (m_pObj != nullptr) return nmObjLineTool::onLeftUp(pt);
ZxRenderView* view = getView();
if (!m_bTracking || view == nullptr) return true;
const QPointF start = m_ptStart;
clearMeasurePreview();
if (!qIsFinite(start.x()) || !qIsFinite(start.y()) ||
!qIsFinite(pt.x()) || !qIsFinite(pt.y())) {
return true;
}
return nmObjLineTool::onLeftDown(pt);
// 真正的单击可为首次拟合井位提供参考点,提交时再检查是否已有结果。
if (start != pt) {
const qreal pixelLength = QLineF(view->mapFromScene(start), view->mapFromScene(pt)).length();
const qreal minimumPixels = 3.0;
if (!qIsFinite(pixelLength) || pixelLength < minimumPixels) return true;
}
QVector<QPointF> points;
points << start << pt;
emit sigPtsFinished(points);
return true;
}
bool nmObjLineMeasureTool::cancelInteraction()
{
if (m_pObj != nullptr) return nmObjToolBase::cancelInteraction();
if (!m_bTracking || getView() == nullptr || getCompoundTool() == nullptr || !isEnabled()) {
return false;
}
clearMeasurePreview();
return true;
}
void nmObjLineMeasureTool::clearMeasurePreview()
{
m_bTracking = false; // 取消后收到的松手事件不会再提交。
if (ZxRenderView* view = getView()) {
view->setSnapshotMode(false);
updateScene();
}
}
void nmObjLineMeasureTool::onPaint(QPainter* painter, const ZxPaintParam& param)
{
if (m_pObj != nullptr) {
nmObjLineTool::onPaint(painter, param);
return;
}
if (!m_bTracking) return;
painter->save();
painter->setPen(QPen(Qt::DashLine));
painter->drawLine(m_ptStart, getCurrentPos());
painter->restore();
}

@ -1643,8 +1643,8 @@ void nmSubWndMain::bindMeasureDialog(QWidget* measureDlg)
return;
}
disconnect(plot, SIGNAL(sigMeasureStarted()), measureDlg, SLOT(resetButtons()));
connect(plot, SIGNAL(sigMeasureStarted()), measureDlg, SLOT(resetButtons()));
disconnect(plot, SIGNAL(sigMeasureCommitted()), measureDlg, SLOT(resetButtons()));
connect(plot, SIGNAL(sigMeasureCommitted()), measureDlg, SLOT(resetButtons()));
disconnect(measureDlg, SIGNAL(needDeleteMeasureObj()), plot, SLOT(finishMeasure()));
connect(measureDlg, SIGNAL(needDeleteMeasureObj()), plot, SLOT(finishMeasure()));

Loading…
Cancel
Save