geopro/src/app/panels/chart/ContourDrawTool.hpp

67 lines
2.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include <functional>
#include <vector>
#include <QObject>
#include <QPoint>
#include <QPointF>
#include <QPointer>
#include <Qt> // Qt::FocusPolicy
class QwtPlot;
namespace geopro::app {
class ContourDrawOverlay;
// I9 图上绘形工具:开启后在等值面图上用鼠标采集几何,实时预览,完成后回调数据坐标点。
// 复刻原版 contour overlay 绘制交互先弹窗选类型→再图上画→drawingComplete
// markType 1 点 / 4 文字:单击落点立即完成;
// markType 2 线逐点单击双击或回车结束≥2 点);
// markType 3 面逐点单击双击或回车闭合≥3 点)。
// Esc 取消。绘制时事件优先于 LivePanner/hover 消费(开启期间禁用平移)。
// 不拥有 plot外部持有QPointer 守护overlay 父=canvas 随之析构。
class ContourDrawTool : public QObject {
Q_OBJECT
public:
// xAxis/yAxis等值面所在轴GridDataChartView 为 xBottom/yLeft
ContourDrawTool(QwtPlot* plot, int xAxis, int yAxis, QObject* parent = nullptr);
// 完成回调:参数为数据坐标点序列(已按类型归一化:点/文字 1 点线≥2面≥3
void setOnComplete(std::function<void(const std::vector<QPointF>&)> cb) { onComplete_ = std::move(cb); }
// 取消回调Esc / 右键 / 外部 cancel调用方据此恢复 UI如重新开放工具条
void setOnCancel(std::function<void()> cb) { onCancel_ = std::move(cb); }
// 开始绘制指定标注类型("1".."4" 对应整数)。会重置已采集点并显示提示。
void begin(int markType);
// 外部强制取消(不触发 onCancel_
void cancel();
bool isActive() const { return active_; }
protected:
bool eventFilter(QObject* obj, QEvent* ev) override;
private:
QPointF toData(const QPoint& canvasPos) const; // 像素 → 数据坐标
void addVertex(const QPoint& canvasPos);
void finish(); // 校验最少点数 → onComplete_
void refreshOverlay(); // 把当前已落点(数据坐标)映射回像素喂给 overlay 预览
QPointer<QwtPlot> plot_;
int xAxis_;
int yAxis_;
std::function<void(const std::vector<QPointF>&)> onComplete_;
std::function<void()> onCancel_;
ContourDrawOverlay* overlay_ = nullptr; // 父=canvas
bool active_ = false;
int markType_ = 0;
std::vector<QPointF> dataPts_; // 已采集点(数据坐标)
QPoint lastCursor_; // 当前光标(橡皮筋预览到此)
Qt::FocusPolicy savedFocus_ = Qt::NoFocus; // begin 前 canvas 焦点策略(退出还原)
bool savedFocusValid_ = false;
void restoreCanvas(); // 退出绘制态:还原光标/焦点策略 + 隐藏 overlay
};
} // namespace geopro::app