logplus/logPlus/TransparentDraggableLine.cpp

222 lines
5.3 KiB
C++

#include "TransparentDraggableLine.h"
extern double g_dPixelPerCm;//每厘米像素数
//static GeoIndicatorGenerator m_drawGeo;
TransparentDraggableLine::TransparentDraggableLine(QMyCustomPlot *parentPlot, QString strUuid, double minWidth, QString strTitle)
: QObject(parentPlot), mPlot(parentPlot), mstrTitle(strTitle), mMinWidth(minWidth)
{
m_strUuid = strUuid;
//
initRect();
}
TransparentDraggableLine::~TransparentDraggableLine()
{
if(mPlot)
{
}
}
//设置最小宽度
void TransparentDraggableLine::setMinWidth(double minWidth)
{
mMinWidth = minWidth;
}
//设置标题
void TransparentDraggableLine::setTitle(QString strTitle)
{
mstrTitle = strTitle;
//mPlot->replot();
}
// 设置矩形范围
void TransparentDraggableLine::setRange(double left_Low, double 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, lY2);//圆心位置
//updateHandles();
mPlot->replot();
}
// 获取当前范围
void TransparentDraggableLine::getRange()
{
m_left_Low = qcpItemLine->start->coords().x();
m_right_Hight = qcpItemLine->end->coords().x();
}
// 设置矩形颜色
void TransparentDraggableLine::setColor(const QColor &color)
{
}
// 删除框图
void TransparentDraggableLine::deleteRect()
{
if(mPlot) {
mPlot->m_mapDraggable_Line.remove(m_strUuid);
mPlot->removeItem(qcpItemLine);
mPlot->replot();
this->deleteLater();
}
}
void TransparentDraggableLine::initRect()
{
// 连接鼠标事件
connect(mPlot, &QCustomPlot::mousePress, this, &TransparentDraggableLine::onMousePress);
connect(mPlot, &QCustomPlot::mouseMove, this, &TransparentDraggableLine::onMouseMove);
connect(mPlot, &QCustomPlot::mouseRelease, this, &TransparentDraggableLine::onMouseRelease);
qcpItemLine = new QCPItemLine(mPlot);
qcpItemLine->setPen(QPen(Qt::blue));
qcpItemLine->setLayer("overlay"); // 确保在最上层
//上下边界
updateHandles();
}
void TransparentDraggableLine::updateHandles()
{
}
void TransparentDraggableLine::onDelRect()
{
//mDragMode = DragNone;
//删除框图
deleteRect();
}
void TransparentDraggableLine::onMousePress(QMouseEvent *event)
{
if(event->button() != Qt::LeftButton)//右键
{
// QMenu menu(nullptr);
// QAction *delAction = menu.addAction("删除框图");
// connect(delAction, &QAction::triggered, this, &TransparentDraggableLine::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)
{
//之前的选中线段,恢复黑色
TransparentDraggableLine *tmpLine = (TransparentDraggableLine*)mPlot->m_SelectShiftLine;
tmpLine->qcpItemLine->setPen(QPen(Qt::blue));
}
//重新设置选中线段
mPlot->m_SelectShiftLine = this;
qcpItemLine->setPen(QPen(Qt::red));
double delta = (m_lY1+lY2)/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 TransparentDraggableLine::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 TransparentDraggableLine::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;
}
}