118 lines
3.5 KiB
C++
118 lines
3.5 KiB
C++
#include "PlotRectItem.h"
|
||
#include <QPainter>
|
||
#include <QwtScaleMap>
|
||
PlotRectItem::PlotRectItem(const QString &title)
|
||
: QwtPlotItem(), m_isHovered(false), m_hasPeakData(false)
|
||
,m_selectionType(""), m_selectionIndex(-1)
|
||
{
|
||
setTitle(title);
|
||
setZ(1000);
|
||
// 初始化普通状态画笔:红色虚线,线宽2
|
||
m_normalPen = QPen(Qt::red, 2, Qt::DashLine);
|
||
// 初始化悬停状态画笔:红色实线,线宽2
|
||
m_hoverPen = QPen(Qt::red, 2, Qt::SolidLine);
|
||
|
||
m_brush = QBrush(QColor(255, 0, 0, 30));
|
||
}
|
||
|
||
void PlotRectItem::setRect(const QRectF &rect) {
|
||
m_rect = rect;
|
||
itemChanged(); // 触发重绘
|
||
}
|
||
|
||
void PlotRectItem::setPen(const QPen &pen) {
|
||
m_normalPen = pen;
|
||
// 同步悬停画笔的颜色和宽度,仅改变线型
|
||
m_hoverPen = pen;
|
||
m_hoverPen.setStyle(Qt::SolidLine);
|
||
itemChanged();
|
||
}
|
||
|
||
void PlotRectItem::setBrush(const QBrush &brush) {
|
||
m_brush = brush;
|
||
itemChanged();
|
||
}
|
||
|
||
//设置悬停状态
|
||
void PlotRectItem::setHovered(bool hovered) {
|
||
if (m_isHovered != hovered) {
|
||
m_isHovered = hovered;
|
||
itemChanged(); // 触发重绘
|
||
}
|
||
}
|
||
|
||
//获取悬停状态
|
||
bool PlotRectItem::isHovered() const {
|
||
return m_isHovered;
|
||
}
|
||
|
||
//获取矩形区域
|
||
QRectF PlotRectItem::rect() const {
|
||
return m_rect;
|
||
}
|
||
|
||
void PlotRectItem::setPeakData(double center, double fwhm, double area, double baseline)
|
||
{
|
||
m_hasPeakData = true;
|
||
m_peakCenter = center;
|
||
m_fwhm = fwhm;
|
||
m_peakArea = area;
|
||
m_baseline = baseline;
|
||
itemChanged(); // 触发重绘
|
||
}
|
||
|
||
void PlotRectItem::draw(QPainter *painter,
|
||
const QwtScaleMap &xMap, const QwtScaleMap &yMap,
|
||
const QRectF &) const {
|
||
const int x1 = xMap.transform(m_rect.left());
|
||
const int x2 = xMap.transform(m_rect.right());
|
||
const int y1 = yMap.transform(m_rect.top());
|
||
const int y2 = yMap.transform(m_rect.bottom());
|
||
const QRect rect(QPoint(x1, y1), QPoint(x2, y2));
|
||
|
||
painter->save();
|
||
// 根据悬停状态选择画笔
|
||
painter->setPen(m_isHovered ? m_hoverPen : m_normalPen);
|
||
painter->setBrush(m_brush);
|
||
painter->drawRect(rect);
|
||
painter->restore();
|
||
|
||
if (m_hasPeakData) {
|
||
painter->save();
|
||
painter->setRenderHint(QPainter::TextAntialiasing);
|
||
|
||
// 构建文本内容
|
||
QString text = QString("峰位: %1 keV\n"
|
||
"FWHM: %2\n"
|
||
"面积: %3\n"
|
||
"本底: %4")
|
||
.arg(m_peakCenter, 0, 'f', 2)
|
||
.arg(m_fwhm, 0, 'f', 2)
|
||
.arg(m_peakArea, 0, 'f', 2)
|
||
.arg(m_baseline, 0, 'f', 2);
|
||
|
||
// 计算文本位置:矩形右上角 + 偏移量
|
||
QPoint textPos(x2 + 10, y2);
|
||
|
||
// 绘制半透明背景框(增强可读性)
|
||
QFont font = painter->font();
|
||
font.setPointSize(9); // 稍微小一点
|
||
painter->setFont(font);
|
||
|
||
QFontMetrics fm(font);
|
||
QRect textRect = fm.boundingRect(QRect(0, 0, 1, 1), Qt::AlignLeft, text);
|
||
textRect.moveTopLeft(textPos);
|
||
textRect.adjust(-5, -3, 5, 3); // 增加边距
|
||
|
||
painter->fillRect(textRect, QColor(255, 255, 255, 220)); // 白底微透
|
||
painter->setPen(Qt::black);
|
||
painter->drawText(textRect, Qt::AlignLeft, text);
|
||
|
||
painter->restore();
|
||
}
|
||
}
|
||
|
||
QRectF PlotRectItem::boundingRect() const {
|
||
return m_rect;
|
||
}
|