DSCAnalysisTool/src/ui/eventhandler.cpp

258 lines
8.0 KiB
C++
Raw Normal View History

2025-04-10 07:20:57 +00:00
#include "eventhandler.h"
2025-04-09 09:30:32 +00:00
#include "logger.h"
2025-04-10 09:33:38 +00:00
#include "specificheatcomparisonmethodform.h"
2025-03-20 09:28:22 +00:00
2025-04-10 07:20:57 +00:00
EventHandler::EventHandler(QCustomPlot *plot,
2025-04-09 09:30:32 +00:00
QCPItemStraightLine *line1,
QCPItemStraightLine *line2,
QObject *parent)
: QObject(parent), _plot(plot), _line1(line1), _line2(line2)
,_enableFlag(false),_mode(Mode::SelectTwoLines),_menu(new QMenu(_plot))
{
_plot->setInteractions(QCP::iSelectItems);
initMenu();
}
#if 0
2025-04-10 07:20:57 +00:00
EventHandler::EventHandler(QCustomPlot *plot, QCPItemStraightLine *line1,
2025-03-20 09:28:22 +00:00
QCPItemStraightLine *line2, QCPGraph*graph,
QObject *parent)
2025-03-24 09:30:42 +00:00
: QObject(parent), _plot(plot), _line1(line1), _line2(line2),_graph(graph)
2025-03-20 09:28:22 +00:00
,_enableFlag(false)
{
2025-03-24 09:30:42 +00:00
_plot->setInteractions(QCP::iSelectItems);
}
2025-04-09 09:30:32 +00:00
#endif
2025-03-24 09:30:42 +00:00
2025-04-10 07:20:57 +00:00
EventHandler::~EventHandler()
2025-03-24 09:30:42 +00:00
{
2025-03-20 09:28:22 +00:00
}
2025-04-10 07:20:57 +00:00
bool EventHandler::eventFilter(QObject *obj, QEvent *event)
2025-03-20 09:28:22 +00:00
{
if(!_enableFlag){
2025-04-03 09:24:29 +00:00
// qDebug()<<"_enableFlag false.";
2025-04-09 09:30:32 +00:00
logde<<"eventFilter false.";
2025-03-24 09:30:42 +00:00
#if 0
if(mPlot){
mPlot->setCursor(Qt::ArrowCursor);
}
#endif
return QObject::eventFilter(obj, event);
2025-03-20 09:28:22 +00:00
}
2025-03-21 09:28:36 +00:00
2025-03-24 09:30:42 +00:00
if (obj == _plot)
2025-03-20 09:28:22 +00:00
{
if (event->type() == QEvent::MouseMove)
{
2025-04-03 09:24:29 +00:00
// qDebug()<<"mouse move...";
2025-03-21 09:28:36 +00:00
2025-03-20 09:28:22 +00:00
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
QPoint mousePos = mouseEvent->pos();
2025-04-03 09:24:29 +00:00
// qDebug()<<"x:"<<mousePos.x();
2025-03-20 09:28:22 +00:00
2025-03-24 09:30:42 +00:00
bool nearLine1 = isNearLine(_line1, mousePos);
bool nearLine2 = isNearLine(_line2, mousePos);
2025-03-20 09:28:22 +00:00
2025-04-03 09:24:29 +00:00
if ((nearLine1 && _line1->visible()) ||
(nearLine2 && _line2->visible()))
2025-03-20 09:28:22 +00:00
{
2025-03-24 09:30:42 +00:00
_plot->setCursor(Qt::SplitHCursor);
2025-03-20 09:28:22 +00:00
}
else
{
2025-03-24 09:30:42 +00:00
_plot->setCursor(Qt::ArrowCursor);
2025-03-20 09:28:22 +00:00
}
2025-03-24 09:30:42 +00:00
if (_draggingLine)
2025-03-20 09:28:22 +00:00
{
2025-03-24 09:30:42 +00:00
double x = _plot->xAxis->pixelToCoord(mousePos.x());
if(_draggingLine == _line1){
2025-03-21 09:28:36 +00:00
emit sigSendLineXCoord(1,x);
}else{
emit sigSendLineXCoord(2,x);
}
2025-03-24 09:30:42 +00:00
_draggingLine->point1->setCoords(x, _plot->yAxis->range().lower);
_draggingLine->point2->setCoords(x, _plot->yAxis->range().upper);
_plot->replot();
2025-03-20 09:28:22 +00:00
updateSelectedRegion();
}
}
else if (event->type() == QEvent::MouseButtonPress)
{
2025-04-03 09:24:29 +00:00
// qDebug()<<"mouse press...";
2025-03-21 09:28:36 +00:00
2025-04-09 09:30:32 +00:00
if(Mode::SelectTwoLines == _mode){
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
if (mouseEvent->button() == Qt::LeftButton)
2025-03-20 09:28:22 +00:00
{
2025-04-09 09:30:32 +00:00
QPoint mousePos = mouseEvent->pos();
if (isNearLine(_line1, mousePos))
{
_draggingLine = _line1;
_line1->setSelected(true);
}
else if (isNearLine(_line2, mousePos))
{
_draggingLine = _line2;
_line2->setSelected(true);
}
2025-03-20 09:28:22 +00:00
}
2025-04-09 09:30:32 +00:00
}else{
#if 0
// 检查是否有选中的曲线
if (event->button() == Qt::LeftButton && selectedPlottables().size() > 0)
2025-03-20 09:28:22 +00:00
{
2025-04-09 09:30:32 +00:00
// 获取选中的曲线
QCPCurve *selectedCurve = qobject_cast<QCPCurve*>(selectedPlottables().first());
if (selectedCurve)
{
// 改变曲线的颜色
selectedCurve->setPen(QPen(Qt::green)); // 设置线条颜色为绿色
replot(); // 重新绘制
}
2025-03-20 09:28:22 +00:00
}
2025-04-09 09:30:32 +00:00
#endif
2025-03-20 09:28:22 +00:00
}
}
else if (event->type() == QEvent::MouseButtonRelease)
{
2025-03-24 09:30:42 +00:00
if (_draggingLine)
2025-03-20 09:28:22 +00:00
{
2025-03-24 09:30:42 +00:00
_draggingLine->setSelected(false);
_draggingLine = nullptr;
2025-03-20 09:28:22 +00:00
}
2025-04-09 09:30:32 +00:00
}else if (event->type() == QEvent::ContextMenu) {
logde<<"context menu...";
#if 0
for (QCPAbstractPlottable *plottable : _plot->plottables()) {
if (plottable->selected()) {
std::cout << "选中的曲线: " << plottable->name().toStdString() << std::endl;
}
}
#endif
2025-04-10 07:20:57 +00:00
if(_plot->selectedPlottables().size() > 0){
logde<<"selected...";
QCPCurve *curve = qobject_cast<QCPCurve *>(_plot->selectedPlottables().front());
if(!curve){
return QObject::eventFilter(obj, event);
}
2025-04-09 09:30:32 +00:00
QContextMenuEvent *contextMenuEvent = static_cast<QContextMenuEvent *>(event);
_menu->move(contextMenuEvent->globalPos());
_menu->show();
2025-04-10 07:20:57 +00:00
2025-04-09 09:30:32 +00:00
}else{
logde<<"not selected...";
}
2025-03-20 09:28:22 +00:00
}
}
return QObject::eventFilter(obj, event);
}
2025-04-10 07:20:57 +00:00
void EventHandler::initMenu()
2025-04-09 09:30:32 +00:00
{
_specificHeatItemAction = new QAction("比热项",_plot);
_baseLineAction = new QAction("基线",_plot);
_standardSampleAction = new QAction("标样",_plot);
_sampleAction = new QAction("样品",_plot);
_menu->addAction(_specificHeatItemAction);
QMenu *specificHeatItemMenu = new QMenu(_plot);
specificHeatItemMenu->addAction(_baseLineAction);
specificHeatItemMenu->addAction(_standardSampleAction);
specificHeatItemMenu->addAction(_sampleAction);
_specificHeatItemAction->setMenu(specificHeatItemMenu);
2025-04-10 07:20:57 +00:00
connect(_baseLineAction,&QAction::triggered,
this,&EventHandler::slotBaseLine);
connect(_standardSampleAction,&QAction::triggered,
this,&EventHandler::slotStandardSample);
connect(_sampleAction,&QAction::triggered,
this,&EventHandler::slotSample);
2025-04-09 09:30:32 +00:00
}
2025-04-10 07:20:57 +00:00
bool EventHandler::isNearLine(QCPItemStraightLine *line, const QPoint &pos)
2025-03-20 09:28:22 +00:00
{
double lineX = line->point1->coords().x();
2025-03-24 09:30:42 +00:00
int linePixelX = _plot->xAxis->coordToPixel(lineX);
2025-03-20 09:28:22 +00:00
return qAbs(pos.x() - linePixelX) < 5;
}
2025-04-10 07:20:57 +00:00
void EventHandler::slotBaseLine()
{
logde<<"slotBaseLine...";
QCPCurve *curve = qobject_cast<QCPCurve *>(_plot->selectedPlottables().front());
if(!curve){
return;
}
curve->setPen(QPen(Qt::red));
_plot->replot();
2025-04-10 09:33:38 +00:00
emit sigSetCurve(SpecificHeatComparisonMethodForm::CurveType::BaseLine,curve);
2025-04-10 07:20:57 +00:00
}
void EventHandler::slotStandardSample()
{
QCPCurve *curve = qobject_cast<QCPCurve *>(_plot->selectedPlottables().front());
if(!curve){
return;
}
curve->setPen(QPen(Qt::darkGreen));
_plot->replot();
2025-04-10 09:33:38 +00:00
emit sigSetCurve(SpecificHeatComparisonMethodForm::CurveType::StandardSample,curve);
2025-04-10 07:20:57 +00:00
}
void EventHandler::slotSample()
{
#if 1
QCPCurve *curve = qobject_cast<QCPCurve *>(_plot->selectedPlottables().front());
if(!curve){
return;
}
curve->setPen(QPen(Qt::black));
_plot->replot();
#endif
2025-04-10 09:33:38 +00:00
emit sigSetCurve(SpecificHeatComparisonMethodForm::CurveType::Sample,curve);
2025-04-10 07:20:57 +00:00
}
void EventHandler::updateSelectedRegion()
2025-03-20 09:28:22 +00:00
{
2025-03-24 09:30:42 +00:00
double x1 = _line1->point1->coords().x();
double x2 = _line1->point1->coords().x();
2025-03-20 09:28:22 +00:00
if (x1 > x2)
std::swap(x1, x2);
// 获取曲线数据
QVector<double> xData, yData;
// mGraph->data()->data(xData, yData);
2025-03-24 09:30:42 +00:00
auto dataContainer = _graph->data();
2025-03-20 09:28:22 +00:00
for (auto it = dataContainer->begin(); it != dataContainer->end(); ++it)
{
xData.append(it->key);
yData.append(it->value);
}
// 查找对应x位置
int index1 = findClosestIndex(xData, x1);
int index2 = findClosestIndex(xData, x2);
2025-04-03 09:24:29 +00:00
// qDebug() << "Selected region: [" << xData[index1] << ", " << xData[index2] << "]";
2025-03-20 09:28:22 +00:00
}
2025-04-10 07:20:57 +00:00
int EventHandler::findClosestIndex(const QVector<double> &data, double target)
2025-03-20 09:28:22 +00:00
{
auto it = std::min_element(data.begin(), data.end(), [target](double a, double b) {
return qAbs(a - target) < qAbs(b - target);
});
return std::distance(data.begin(), it);
}