2026-03-09 16:50:21 +08:00
|
|
|
|
#include "CustomQwtPlot.h"
|
|
|
|
|
|
#include <QwtPlotCurve>
|
|
|
|
|
|
#include <QwtLegend>
|
|
|
|
|
|
#include <QwtPlotCanvas>
|
2026-03-12 20:23:55 +08:00
|
|
|
|
#include <QwtText>
|
|
|
|
|
|
#include <QwtPlotMarker>
|
2026-03-09 16:50:21 +08:00
|
|
|
|
#include <QPen>
|
|
|
|
|
|
|
|
|
|
|
|
CustomQwtPlot::CustomQwtPlot(QWidget *parent)
|
|
|
|
|
|
: QwtPlot(parent)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-12 20:23:55 +08:00
|
|
|
|
QwtPlotCurve *CustomQwtPlot::GetCurve(const QString &curve_name)
|
2026-03-09 16:50:21 +08:00
|
|
|
|
{
|
2026-03-12 20:23:55 +08:00
|
|
|
|
return _curves.value(curve_name, nullptr);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QList<QwtPlotCurve *> CustomQwtPlot::GetCurveList() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return _curves.values();
|
2026-03-09 16:50:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CustomQwtPlot::AddCurve(QwtPlotCurve *curve)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (curve) {
|
2026-03-12 20:23:55 +08:00
|
|
|
|
curve->setPen(QPen(getDistinctColorForManyCurves(_curves.count()), 1));
|
2026-03-09 16:50:21 +08:00
|
|
|
|
curve->attach(this);
|
2026-03-12 20:23:55 +08:00
|
|
|
|
_curves[curve->title().text()] = curve;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QwtPlotMarker *CustomQwtPlot::GetMarker(const QString &marker_name, const QString &postion)
|
|
|
|
|
|
{
|
|
|
|
|
|
return _markers.value(marker_name).value(postion);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QList<QwtPlotMarker *> CustomQwtPlot::GetMarkerList() const
|
|
|
|
|
|
{
|
|
|
|
|
|
QList<QwtPlotMarker *> markers;
|
|
|
|
|
|
for (auto tmp_markers : this->_markers) {
|
|
|
|
|
|
markers.append(tmp_markers.values());
|
|
|
|
|
|
}
|
|
|
|
|
|
return markers;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CustomQwtPlot::AddMarker(QwtPlotMarker *marker, const QString &marker_name, const QString &postion)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (marker) {
|
|
|
|
|
|
QwtPlotCurve* curve = GetCurve(marker_name);
|
|
|
|
|
|
if (curve) {
|
|
|
|
|
|
QPen pen = curve->pen();
|
|
|
|
|
|
pen.setWidth(2);
|
|
|
|
|
|
marker->setLinePen(pen);
|
|
|
|
|
|
marker->attach(this);
|
|
|
|
|
|
_markers[marker_name][postion] = marker;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CustomQwtPlot::RemoveMarker(const QString &marker_name, const QString &postion)
|
|
|
|
|
|
{
|
|
|
|
|
|
QwtPlotMarker* marker = GetMarker(marker_name, postion);
|
|
|
|
|
|
if (marker) {
|
|
|
|
|
|
marker->detach();
|
|
|
|
|
|
delete marker;
|
|
|
|
|
|
}
|
|
|
|
|
|
_markers[marker_name].remove(postion);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CustomQwtPlot::CleanMarkers()
|
|
|
|
|
|
{
|
|
|
|
|
|
QList<QwtPlotMarker *> markers = GetMarkerList();
|
|
|
|
|
|
for (auto marker : markers) {
|
|
|
|
|
|
if (marker) {
|
|
|
|
|
|
marker->detach();
|
|
|
|
|
|
delete marker;
|
|
|
|
|
|
}
|
2026-03-09 16:50:21 +08:00
|
|
|
|
}
|
2026-03-12 20:23:55 +08:00
|
|
|
|
this->_markers.clear();
|
2026-03-09 16:50:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QColor getDistinctColorForManyCurves(int curve_index)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 1. 定义基础色相(覆盖不同主色系,0-360度)
|
|
|
|
|
|
const QList<int> base_hues = {
|
|
|
|
|
|
0, // 红色
|
|
|
|
|
|
30, // 橙色
|
|
|
|
|
|
60, // 黄色
|
|
|
|
|
|
90, // 黄绿色
|
|
|
|
|
|
120, // 绿色
|
|
|
|
|
|
150, // 青绿色
|
|
|
|
|
|
180, // 青色
|
|
|
|
|
|
210, // 天蓝色
|
|
|
|
|
|
240, // 蓝色
|
|
|
|
|
|
270, // 紫色
|
|
|
|
|
|
300, // 洋红色
|
|
|
|
|
|
330 // 玫红色
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 2. 定义不同的饱和度/明度组合(避免颜色太暗/太灰)
|
|
|
|
|
|
const QList<QPair<int, int>> sv_combinations = {
|
|
|
|
|
|
{85, 90}, // 高饱和、高明度
|
|
|
|
|
|
{70, 85}, // 中高饱和、中高明度
|
|
|
|
|
|
{85, 75}, // 高饱和、中明度
|
|
|
|
|
|
{60, 80}, // 中饱和、中高明度
|
|
|
|
|
|
{75, 70}, // 中高饱和、中明度
|
|
|
|
|
|
{90, 80} // 极高饱和、中高明度
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 3. 计算当前曲线对应的色相和饱和度/明度
|
|
|
|
|
|
int hue_index = curve_index % base_hues.size(); // 循环使用基础色相
|
|
|
|
|
|
int sv_index = (curve_index / base_hues.size()) % sv_combinations.size(); // 循环使用饱和度/明度组合
|
|
|
|
|
|
|
|
|
|
|
|
// 4. 获取HSV参数(色相0-360,饱和度0-255,明度0-255)
|
|
|
|
|
|
int hue = base_hues[hue_index];
|
|
|
|
|
|
int saturation = sv_combinations[sv_index].first * 255 / 100; // 转换为0-255范围
|
|
|
|
|
|
int value = sv_combinations[sv_index].second * 255 / 100; // 转换为0-255范围
|
|
|
|
|
|
|
|
|
|
|
|
// 5. 生成并返回颜色(HSV转RGB)
|
|
|
|
|
|
QColor color;
|
|
|
|
|
|
color.setHsv(hue, saturation, value);
|
|
|
|
|
|
|
|
|
|
|
|
// 额外优化:避免极浅/极暗的颜色(保证曲线可见)
|
|
|
|
|
|
if (value < 50 * 255 / 100) {
|
|
|
|
|
|
value = 50 * 255 / 100;
|
|
|
|
|
|
color.setHsv(hue, saturation, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return color;
|
|
|
|
|
|
}
|
|
|
|
|
|
|