DSCAnalysisTool/src/ui/draglinehandler.cpp

138 lines
4.1 KiB
C++
Raw Normal View History

2025-03-20 09:28:22 +00:00
#include "draglinehandler.h"
DragLineHandler::DragLineHandler(QCustomPlot *plot, QCPItemStraightLine *line1,
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);
}
DragLineHandler::~DragLineHandler()
{
2025-03-20 09:28:22 +00:00
}
bool DragLineHandler::eventFilter(QObject *obj, QEvent *event)
{
if(!_enableFlag){
2025-03-26 09:30:02 +00:00
// qDebug()<<"_enableFlag 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-03-26 09:30:02 +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-03-26 09:30:02 +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
if (nearLine1 || nearLine2)
{
2025-03-24 09:30:42 +00:00
_plot->setCursor(Qt::SplitHCursor);
2025-03-20 09:28:22 +00:00
2025-03-24 09:30:42 +00:00
// qDebug()<<"mIconLabel visiable.";
2025-03-20 09:28:22 +00:00
}
else
{
2025-03-24 09:30:42 +00:00
// qDebug()<<"mIconLabel not visiable.";
2025-03-20 09:28:22 +00:00
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-03-27 09:31:19 +00:00
// qDebug()<<"mouse press...";
2025-03-21 09:28:36 +00:00
2025-03-20 09:28:22 +00:00
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
if (mouseEvent->button() == Qt::LeftButton)
{
QPoint mousePos = mouseEvent->pos();
2025-03-24 09:30:42 +00:00
if (isNearLine(_line1, mousePos))
2025-03-20 09:28:22 +00:00
{
2025-03-24 09:30:42 +00:00
_draggingLine = _line1;
_line1->setSelected(true);
2025-03-20 09:28:22 +00:00
}
2025-03-24 09:30:42 +00:00
else if (isNearLine(_line2, mousePos))
2025-03-20 09:28:22 +00:00
{
2025-03-24 09:30:42 +00:00
_draggingLine = _line2;
_line2->setSelected(true);
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
}
}
}
return QObject::eventFilter(obj, event);
}
bool DragLineHandler::isNearLine(QCPItemStraightLine *line, const QPoint &pos)
{
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;
}
void DragLineHandler::updateSelectedRegion()
{
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-03-27 09:31:19 +00:00
// qDebug() << "Selected region: [" << xData[index1] << ", " << xData[index2] << "]";
2025-03-20 09:28:22 +00:00
}
int DragLineHandler::findClosestIndex(const QVector<double> &data, double target)
{
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);
}