XNSim/XNMonitor/XNCustomPlot.cpp

54 lines
1.4 KiB
C++
Raw Normal View History

2025-04-28 12:25:20 +08:00
#include "XNCustomPlot.h"
#include <QMouseEvent>
#include <QContextMenuEvent>
#include <QWheelEvent>
XNCustomPlot::XNCustomPlot(QWidget *parent)
: QCustomPlot(parent), m_yAxisUserScaled(false), m_isInternalChange(false)
{
// 使用旧式的信号槽连接语法
connect(yAxis, SIGNAL(rangeChanged(QCPRange)), this, SLOT(onYAxisRangeChanged(QCPRange)));
}
void XNCustomPlot::mousePressEvent(QMouseEvent *event)
{
// 首先处理基类的事件
QCustomPlot::mousePressEvent(event);
// 发出自定义信号
emit mousePressed(event);
// 处理右键菜单
if (event->button() == Qt::RightButton) {
emit customContextMenuRequested(event->pos());
}
}
void XNCustomPlot::mouseReleaseEvent(QMouseEvent *event)
{
QCustomPlot::mouseReleaseEvent(event);
// 如果用户通过鼠标调整了Y轴范围
if (yAxis->selectedParts().testFlag(QCPAxis::spAxis)) {
m_yAxisUserScaled = true;
}
}
void XNCustomPlot::wheelEvent(QWheelEvent *event)
{
// 处理鼠标滚轮事件
QCustomPlot::wheelEvent(event);
// 如果用户使用滚轮缩放了Y轴
if (yAxis->range() != yAxis->range()) { // 范围发生了变化
m_yAxisUserScaled = true;
}
}
void XNCustomPlot::onYAxisRangeChanged(const QCPRange &range)
{
// 如果Y轴范围发生了变化则设置Y轴被用户缩放
if (!m_isInternalChange) {
m_yAxisUserScaled = true;
}
}