244 lines
6.0 KiB
C++
244 lines
6.0 KiB
C++
#include "TransparentDraggableCorePhysics.h"
|
|
|
|
extern double g_dPixelPerCm;//每厘米像素数
|
|
//static GeoIndicatorGenerator m_drawGeo;
|
|
|
|
TransparentDraggableCorePhysics::TransparentDraggableCorePhysics(QMyCustomPlot *parentPlot, QString strUuid, double minWidth, QString strTitle)
|
|
: QObject(parentPlot), mPlot(parentPlot), mstrTitle(strTitle), mMinWidth(minWidth)
|
|
{
|
|
m_strUuid = strUuid;
|
|
//
|
|
initRect();
|
|
}
|
|
|
|
TransparentDraggableCorePhysics::~TransparentDraggableCorePhysics()
|
|
{
|
|
if(mPlot)
|
|
{
|
|
}
|
|
}
|
|
|
|
//设置最小宽度
|
|
void TransparentDraggableCorePhysics::setMinWidth(double minWidth)
|
|
{
|
|
mMinWidth = minWidth;
|
|
}
|
|
|
|
//设置标题
|
|
void TransparentDraggableCorePhysics::setTitle(QString strTitle)
|
|
{
|
|
mstrTitle = strTitle;
|
|
//mPlot->replot();
|
|
}
|
|
|
|
// 设置矩形范围
|
|
void TransparentDraggableCorePhysics::setRange(double left_Low, double right_Hight, bool bReplot)
|
|
{
|
|
m_left_Low = left_Low;
|
|
m_right_Hight = right_Hight;
|
|
|
|
double lY1 = mPlot->yAxis->range().lower;//+10
|
|
double lY2 = mPlot->yAxis->range().upper;
|
|
|
|
for(int i =0; i<mPlot->m_x.size(); i++)
|
|
{
|
|
if(abs(mPlot->m_x[i]-left_Low) < 0.1)
|
|
{
|
|
lY1 = mPlot->m_y[i];
|
|
break;
|
|
}
|
|
}
|
|
m_lY1 = lY1;
|
|
|
|
qcpItemLine->start->setCoords(left_Low, lY1);//圆心位置
|
|
qcpItemLine->end->setCoords(right_Hight, 30);//圆心位置
|
|
|
|
//updateHandles();
|
|
//刷新,针对批量修改不在此处刷新,后面统一刷新
|
|
if(bReplot)
|
|
{
|
|
mPlot->replot();
|
|
}
|
|
}
|
|
|
|
// 获取当前范围
|
|
//void TransparentDraggableCorePhysics::getRange()
|
|
//{
|
|
// m_left_Low = qcpItemLine->start->coords().x();
|
|
// m_right_Hight = qcpItemLine->end->coords().x();
|
|
//}
|
|
|
|
QCPRange TransparentDraggableCorePhysics::getRange()
|
|
{
|
|
// 获取线段起点和终点的 X 坐标
|
|
double startX = qcpItemLine->start->coords().x();
|
|
double endX = qcpItemLine->end->coords().x();
|
|
|
|
// 自动处理左右顺序,确保最小值 <= 最大值
|
|
double lower = qMin(startX, endX);
|
|
double upper = qMax(startX, endX);
|
|
|
|
// 返回构造好的 QCPRange
|
|
return QCPRange(lower, upper);
|
|
}
|
|
|
|
// 设置矩形颜色
|
|
void TransparentDraggableCorePhysics::setColor(const QColor &color)
|
|
{
|
|
}
|
|
|
|
// 删除框图
|
|
void TransparentDraggableCorePhysics::deleteRect()
|
|
{
|
|
if(mPlot) {
|
|
mPlot->m_mapDraggable_CorePhysics.remove(m_strUuid);
|
|
|
|
mPlot->removeItem(qcpItemLine);
|
|
|
|
//mPlot->replot();
|
|
this->deleteLater();
|
|
}
|
|
}
|
|
|
|
|
|
void TransparentDraggableCorePhysics::initRect()
|
|
{
|
|
// 连接鼠标事件
|
|
connect(mPlot, &QCustomPlot::mousePress, this, &TransparentDraggableCorePhysics::onMousePress);
|
|
connect(mPlot, &QCustomPlot::mouseMove, this, &TransparentDraggableCorePhysics::onMouseMove);
|
|
connect(mPlot, &QCustomPlot::mouseRelease, this, &TransparentDraggableCorePhysics::onMouseRelease);
|
|
|
|
qcpItemLine = new QCPItemLine(mPlot);
|
|
qcpItemLine->setPen(QPen(Qt::black, 10));
|
|
qcpItemLine->setLayer("overlay"); // 确保在最上层
|
|
|
|
//上下边界
|
|
updateHandles();
|
|
}
|
|
|
|
void TransparentDraggableCorePhysics::updateHandles()
|
|
{
|
|
|
|
}
|
|
|
|
void TransparentDraggableCorePhysics::onDelRect()
|
|
{
|
|
//mDragMode = DragNone;
|
|
//删除框图
|
|
deleteRect();
|
|
mPlot->replot();
|
|
}
|
|
|
|
|
|
void TransparentDraggableCorePhysics::onMousePress(QMouseEvent *event)
|
|
{
|
|
if(event->button() != Qt::LeftButton)//右键
|
|
{
|
|
// QMenu menu(nullptr);
|
|
// QAction *delAction = menu.addAction("删除框图");
|
|
// connect(delAction, &QAction::triggered, this, &TransparentDraggableCorePhysics::onDelRect);
|
|
|
|
// menu.exec(event->globalPos());
|
|
// return;
|
|
}
|
|
|
|
event->accept();
|
|
|
|
// 检查点击了哪个部分
|
|
double y = mPlot->xAxis->pixelToCoord(event->pos().y());//x轴展示深度
|
|
double x = mPlot->yAxis->pixelToCoord(event->pos().x());
|
|
//double lY1 = mPlot->yAxis->range().lower;//+10
|
|
double lY2 = mPlot->yAxis->range().upper;
|
|
|
|
if(qcpItemLine->selectTest(event->pos(), false) < 5)
|
|
{
|
|
if(mPlot->m_SelectShiftLine)
|
|
{
|
|
//之前的选中线段,恢复黑色
|
|
TransparentDraggableCorePhysics *tmpLine = (TransparentDraggableCorePhysics*)mPlot->m_SelectShiftLine;
|
|
tmpLine->qcpItemLine->setPen(QPen(Qt::blue));
|
|
}
|
|
|
|
//重新设置选中线段
|
|
mPlot->m_SelectShiftLine = this;
|
|
qcpItemLine->setPen(QPen(Qt::red));
|
|
|
|
double delta = (lY2-m_lY1)/10.0;
|
|
if(x < m_lY1+delta)
|
|
{
|
|
mDragMode = DragLeft;
|
|
}
|
|
else if(x > lY2-delta)
|
|
{
|
|
mDragMode = DragRight;
|
|
}
|
|
else
|
|
{
|
|
mDragMode = DragRect;
|
|
}
|
|
}
|
|
else {
|
|
mDragMode = DragNone;
|
|
return;
|
|
}
|
|
|
|
getRange();
|
|
|
|
mDragStartY = y;
|
|
}
|
|
|
|
void TransparentDraggableCorePhysics::onMouseMove(QMouseEvent *event)
|
|
{
|
|
if(mDragMode == DragNone) return;
|
|
|
|
event->accept();
|
|
|
|
//double x = mPlot->xAxis->pixelToCoord(event->pos().x());
|
|
//double dx = x - mDragStartX;
|
|
|
|
double y = mPlot->xAxis->pixelToCoord(event->pos().y());
|
|
double dy = y - mDragStartY;
|
|
mDragStartY = y;
|
|
|
|
switch(mDragMode) {
|
|
case DragLeft: {
|
|
m_left_Low = m_left_Low + dy;
|
|
break;
|
|
}
|
|
case DragRight: {
|
|
m_right_Hight = m_right_Hight + dy;
|
|
break;
|
|
}
|
|
case DragRect: {
|
|
m_left_Low = m_left_Low + dy;
|
|
m_right_Hight = m_right_Hight + dy;
|
|
break;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
|
|
setRange(m_left_Low, m_right_Hight);
|
|
}
|
|
|
|
void TransparentDraggableCorePhysics::onMouseRelease(QMouseEvent *event)
|
|
{
|
|
if(event->button() == Qt::LeftButton && mDragMode != DragNone) {
|
|
event->accept();
|
|
//避免二次绘制框图
|
|
mPlot->m_bDrawRect = false;
|
|
//emit rangeChanged(getRange());
|
|
mDragMode = DragNone;
|
|
//取消选中状态
|
|
// QCPDataSelection emptySelection;
|
|
// mPlot->graph(0)->setSelection(emptySelection);
|
|
// mPlot->replot();
|
|
|
|
//取消选中框
|
|
mPlot->selectionRect()->cancel();
|
|
mPlot->replot();
|
|
mPlot->selectionRect()->mActive=true;
|
|
}
|
|
}
|
|
|