优化解释结论道绘图,支持同一个结论的上下rect框联动,上方拖动会带动下方变化

This commit is contained in:
jiayulong 2025-12-08 17:25:33 +08:00
parent d512c3cac9
commit fcce4f565a
4 changed files with 235 additions and 59 deletions

View File

@ -14,12 +14,19 @@ class TransparentDraggableResult : public QObject
{
Q_OBJECT
public:
explicit TransparentDraggableResult(QMyCustomPlot *parentPlot, QString strUuid="", double minWidth = 1.0, QString strTitle = "")
explicit TransparentDraggableResult(QMyCustomPlot *parentPlot, TransparentDraggableResult *upDraggableResult=nullptr, QString strUuid="", double minWidth = 1.0, QString strTitle = "")
: QObject(parentPlot), mPlot(parentPlot), mstrTitle(strTitle), mMinWidth(minWidth)
{
m_upDraggableResult = upDraggableResult;
m_strUuid = strUuid;
//
initRect();
//将自己设置为上方rect的下方
if(m_upDraggableResult)
{
m_upDraggableResult->m_downDraggableResult = this;
}
}
~TransparentDraggableResult() {
@ -91,7 +98,8 @@ public:
//mRect->bottomRight->setCoords(right, mPlot->yAxis->range().lower);
updateHandles();
mPlot->replot();
//后面统一刷新上方和下方rect
//mPlot->replot();
}
// 获取当前范围
@ -198,6 +206,21 @@ private:
mRightHandle->topLeft->setCoords(-5, 5); // 矩形大小
mRightHandle->bottomRight->setCoords(5, -5); // 矩形大小
// if(m_upDraggableResult)
// {
// // 右边界矩形控制点
// m_upDraggableResult->mLeftHandle->topLeft->setParentAnchor(mRect->bottomLeft);
// m_upDraggableResult->mLeftHandle->bottomRight->setParentAnchor(mRect->bottomRight);
// m_upDraggableResult->mLeftHandle->topLeft->setCoords(-5, 5); // 矩形大小
// m_upDraggableResult->mLeftHandle->bottomRight->setCoords(5, -5); // 矩形大小
// // 右边界矩形控制点
// //mRect->topLeft->setParentAnchor(m_upDraggableResult->mRect->bottomLeft);
// //m_upDraggableResult->mRect->topRight->setParentAnchor(mRect->bottomRight);
// // mRect->topLeft->setParentAnchor(m_upDraggableResult->mRect->bottomLeft);
// // mRect->bottomRight->setParentAnchor(m_upDraggableResult->mRect->bottomRight);
// }
}
private slots:
@ -229,44 +252,40 @@ private slots:
// }
void onMousePress(QMouseEvent *event) {
if(event->button() != Qt::LeftButton)//右键
{
double y = mPlot->xAxis->pixelToCoord(event->pos().y());//x轴展示深度
QCPRange currentRange = getRange();
if(mLeftHandle->selectTest(event->pos(), false) < 5) {
mDragMode = DragNone;
}
else if(mRightHandle->selectTest(event->pos(), false) < 5) {
mDragMode = DragNone;
}
//else if(x >= currentRange.lower && x <= currentRange.upper) {
else if(y >= currentRange.lower && y <= currentRange.upper) {
mDragMode = DragNone;
}
else {
mDragMode = DragNone;
return;
}
//event->accept();
QMenu menu(nullptr);
QAction *delAction = menu.addAction("删除框图");
//delAction->installEventFilter(this);
connect(delAction, &QAction::triggered, this, &TransparentDraggableResult::onDelRect);
// //右键
// if(event->button() != Qt::LeftButton)
// {
// double y = mPlot->xAxis->pixelToCoord(event->pos().y());//x轴展示深度
// QCPRange currentRange = getRange();
// if(mLeftHandle->selectTest(event->pos(), false) < 5) {
// mDragMode = DragNone;
// }
// else if(mRightHandle->selectTest(event->pos(), false) < 5) {
// mDragMode = DragNone;
// }
// //else if(x >= currentRange.lower && x <= currentRange.upper) {
// else if(y >= currentRange.lower && y <= currentRange.upper) {
// mDragMode = DragNone;
// }
// else {
// mDragMode = DragNone;
// return;
// }
// //event->accept();
// QMenu menu(nullptr);
// QAction *delAction = menu.addAction("删除框图");
// //delAction->installEventFilter(this);
// connect(delAction, &QAction::triggered, this, &TransparentDraggableResult::onDelRect);
// QAction* pItem = menu.exec(event->globalPos());
// if(pItem == delAction)
// {
// //event->accept();
// int ii=0;
// ii++;
// }
menu.exec(event->globalPos());
return;
}
// menu.exec(event->globalPos());
// return;
// }
event->accept();
@ -297,13 +316,30 @@ private slots:
mDragStartY = y;
mDragStartRange = currentRange;
//上方rect同步动
if(m_upDraggableResult)
{
if(mDragMode==DragRight || mDragMode==DragRect)
{
m_upDraggableResult->mDragMode = DragLeft;
m_upDraggableResult->mDragStartY = y;
m_upDraggableResult->mDragStartRange = m_upDraggableResult->getRange();
}
}
void onMouseMove(QMouseEvent *event) {
if(mDragMode == DragNone) return;
event->accept();
//下方rect同步动
if(m_downDraggableResult)
{
if(mDragMode==DragLeft || mDragMode==DragRect)
{
m_downDraggableResult->mDragMode = DragRight;
m_downDraggableResult->mDragStartY = y;
m_downDraggableResult->mDragStartRange = m_downDraggableResult->getRange();
}
}
}
void onMouseMove_in(QMouseEvent *event) {
//double x = mPlot->xAxis->pixelToCoord(event->pos().x());
//double dx = x - mDragStartX;
@ -393,10 +429,81 @@ private slots:
}
setRange(newRange.lower, newRange.upper);
}
void onMouseRelease(QMouseEvent *event) {
void onMouseMove_in_DragLeft(QMouseEvent *event) {
double y = mPlot->xAxis->pixelToCoord(event->pos().y());
double dy = y - mDragStartY;
QCPRange newRange = mDragStartRange;
double proposedLeft = mDragStartRange.lower + dy;
// 确保不超出轴范围且不使宽度小于最小值
newRange.lower = qBound(
getMyLower(),
proposedLeft,
mDragStartRange.upper - mMinWidth);
// 最终确保宽度不小于最小值(针对整体拖动的情况)
if(newRange.size() < mMinWidth) {
newRange.lower = newRange.upper - mMinWidth;
}
setRange(newRange.lower, newRange.upper);
}
void onMouseMove_in_DragRight(QMouseEvent *event) {
double y = mPlot->xAxis->pixelToCoord(event->pos().y());
double dy = y - mDragStartY;
QCPRange newRange = mDragStartRange;
double proposedRight = mDragStartRange.upper + dy;
// 确保不超出轴范围且不使宽度小于最小值
newRange.upper = qBound(
mDragStartRange.lower + mMinWidth,
proposedRight,
getMyUpper());
// 最终确保宽度不小于最小值(针对整体拖动的情况)
if(newRange.size() < mMinWidth) {
newRange.upper = newRange.lower + mMinWidth;
}
setRange(newRange.lower, newRange.upper);
}
void onMouseMove(QMouseEvent *event) {
if(mDragMode == DragNone) return;
event->accept();
onMouseMove_in(event);
//上方rect同步动
if(m_upDraggableResult)
{
if(mDragMode==DragRight || mDragMode==DragRect)
{
//m_upDraggableResult->mDragMode = DragLeft;
m_upDraggableResult->onMouseMove_in_DragLeft(event);
}
}
//下方rect同步动
if(m_downDraggableResult)
{
if(mDragMode==DragLeft || mDragMode==DragRect)
{
//m_downDraggableResult->mDragMode = DragRight;
m_downDraggableResult->onMouseMove_in_DragRight(event);
}
}
//后面统一刷新上方和下方rect
mPlot->replot();
}
void onMouseRelease_in(QMouseEvent *event) {
if(event->button() == Qt::LeftButton && mDragMode != DragNone) {
event->accept();
//避免二次绘制框图
@ -408,13 +515,38 @@ private slots:
// mPlot->graph(0)->setSelection(emptySelection);
// mPlot->replot();
//取消选中框
mPlot->selectionRect()->cancel();
mPlot->replot();
mPlot->selectionRect()->mActive=true;
// //取消选中框
// mPlot->selectionRect()->cancel();
// mPlot->replot();
// mPlot->selectionRect()->mActive=true;
}
}
void onMouseRelease(QMouseEvent *event) {
if(event->button() == Qt::LeftButton && mDragMode != DragNone) {
//上方rect同步动
if(m_upDraggableResult)
{
if(mDragMode==DragRight || mDragMode==DragRect)
{
m_upDraggableResult->onMouseRelease_in(event);
}
}
//下方rect同步动
if(m_downDraggableResult)
{
if(mDragMode==DragLeft || mDragMode==DragRect)
{
m_downDraggableResult->onMouseRelease_in(event);
}
}
}
onMouseRelease_in(event);
}
double getMyLower()
{
double dLower = mPlot->xAxis->range().lower;
@ -484,6 +616,8 @@ private:
QString mstrTitle="";
QString m_strUuid = "";
TransparentDraggableResult *m_upDraggableResult=nullptr;
TransparentDraggableResult *m_downDraggableResult=nullptr;
QString m_Result;
enum DragMode { DragNone, DragLeft, DragRight, DragRect };

View File

@ -1704,17 +1704,51 @@ bool FormDraw::LoadFromSLF_Result(QMyCustomPlot *widget, QString strSlfName, QSt
}
//
QString strUuid = "";
if(result != "" && m_Result->MDepth1!=0) {
widget->addResultToPlot(-m_Result->MDepth1, -m_Result->StartDepth, result);
widget->addResultToPlot(-m_Result->MDepth1, -m_Result->StartDepth, result, strUuid);
}
if(result1 != "" && m_Result->MDepth2!=0) {
widget->addResultToPlot(-m_Result->MDepth2, -m_Result->MDepth1, result1);
widget->addResultToPlot(-m_Result->MDepth2, -m_Result->MDepth1, result1, strUuid);
}
if(result2 != "" && m_Result->MDepth3!=0) {
widget->addResultToPlot(-m_Result->MDepth3, -m_Result->MDepth2, result2);
widget->addResultToPlot(-m_Result->MDepth3, -m_Result->MDepth2, result2, strUuid);
}
if(result3 != "" && m_Result->MDepth4!=0) {
widget->addResultToPlot(-m_Result->MDepth4, -m_Result->MDepth3, result3, strUuid);
}
if(result4 != "" && m_Result->MDepth5!=0) {
widget->addResultToPlot(-m_Result->MDepth5, -m_Result->MDepth4, result4, strUuid);
}
if(result5 != "" && m_Result->MDepth6!=0) {
widget->addResultToPlot(-m_Result->MDepth6, -m_Result->MDepth5, result5, strUuid);
}
if(result6 != "" && m_Result->MDepth6!=0) {
widget->addResultToPlot(-m_Result->MDepth7, -m_Result->MDepth6, result6, strUuid);
}
if(result7 != "" && m_Result->MDepth8!=0) {
widget->addResultToPlot(-m_Result->MDepth8, -m_Result->MDepth7, result7, strUuid);
}
if(result8 != "" && m_Result->MDepth9!=0) {
widget->addResultToPlot(-m_Result->MDepth9, -m_Result->MDepth8, result8, strUuid);
}
if(result9 != "" && m_Result->MDepth10!=0) {
widget->addResultToPlot(-m_Result->MDepth10, -m_Result->MDepth9, result9, strUuid);
}
if(result10 != "" && m_Result->MDepth10!=0) {
widget->addResultToPlot(-m_Result->EndDepth, -m_Result->MDepth10, result10, strUuid);
}
}
logio->CloseTable(iIndex);
delete pstr;

View File

@ -265,13 +265,21 @@ void QMyCustomPlot::addTextToPlot(double left_Low, double right_Hight, const QSt
m_mapDraggableRect[strUuid] = dragRect;
}
void QMyCustomPlot::addResultToPlot(double left_Low, double right_Hight, QString myResult)
void QMyCustomPlot::addResultToPlot(double left_Low, double right_Hight, QString myResult, QString &strUuid)
{
//获取上方Rect
TransparentDraggableResult *upDragRect = nullptr;
if(strUuid!="")
{
upDragRect = (TransparentDraggableResult *)m_mapDraggable_Result[strUuid];
}
//
QtCommonClass *qtCommon = new QtCommonClass(this);
QString strUuid = qtCommon->getUUid();
strUuid = qtCommon->getUUid();
// 在初始化代码中
TransparentDraggableResult *dragRect = new TransparentDraggableResult(this, strUuid);
TransparentDraggableResult *dragRect = new TransparentDraggableResult(this, upDragRect, strUuid);
// 设置初始范围
dragRect->setRange(left_Low, right_Hight);
// 可选:设置颜色

View File

@ -73,7 +73,7 @@ public:
void addTextToPlot(double left_Low, double right_Hight, const QString strText);
void addResultToPlot(double left_Low, double right_Hight, QString myResult);
void addResultToPlot(double left_Low, double right_Hight, QString myResult, QString &strUuid);
public slots:
void s_LineClicked(int index);