曲线编辑优化,支持拖拉选中框来修改曲线
This commit is contained in:
parent
deb9e026d6
commit
ffa3925d23
|
|
@ -144,7 +144,7 @@ void TransparentDraggableLine::onMousePress(QMouseEvent *event)
|
||||||
mPlot->m_SelectShiftLine = this;
|
mPlot->m_SelectShiftLine = this;
|
||||||
qcpItemLine->setPen(QPen(Qt::red));
|
qcpItemLine->setPen(QPen(Qt::red));
|
||||||
|
|
||||||
double delta = (m_lY1+lY2)/10.0;
|
double delta = (lY2-m_lY1)/10.0;
|
||||||
if(x < m_lY1+delta)
|
if(x < m_lY1+delta)
|
||||||
{
|
{
|
||||||
mDragMode = DragLeft;
|
mDragMode = DragLeft;
|
||||||
|
|
|
||||||
|
|
@ -82,6 +82,7 @@ SOURCES += \
|
||||||
qtprojectwidgets.cpp \
|
qtprojectwidgets.cpp \
|
||||||
totalTitleBar.cpp \
|
totalTitleBar.cpp \
|
||||||
transparentdraggableGuan.cpp \
|
transparentdraggableGuan.cpp \
|
||||||
|
transparentdraggableSelectRect.cpp \
|
||||||
transparentdraggableimage.cpp \
|
transparentdraggableimage.cpp \
|
||||||
variantfactory.cpp \
|
variantfactory.cpp \
|
||||||
variantmanager.cpp
|
variantmanager.cpp
|
||||||
|
|
@ -142,6 +143,7 @@ HEADERS += \
|
||||||
qtprojectwidgets.h \
|
qtprojectwidgets.h \
|
||||||
totalTitleBar.h \
|
totalTitleBar.h \
|
||||||
transparentdraggableGuan.h \
|
transparentdraggableGuan.h \
|
||||||
|
transparentdraggableSelectRect.h \
|
||||||
transparentdraggableimage.h \
|
transparentdraggableimage.h \
|
||||||
variantfactory.h \
|
variantfactory.h \
|
||||||
variantmanager.h
|
variantmanager.h
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@
|
||||||
#include "TransparentDraggableFac.h"
|
#include "TransparentDraggableFac.h"
|
||||||
#include "transparentdraggableGuan.h"
|
#include "transparentdraggableGuan.h"
|
||||||
#include "TransparentDraggableLine.h"
|
#include "TransparentDraggableLine.h"
|
||||||
|
#include "transparentdraggableSelectRect.h"
|
||||||
#include "qtcommonclass.h"
|
#include "qtcommonclass.h"
|
||||||
|
|
||||||
//是否隐藏刻度
|
//是否隐藏刻度
|
||||||
|
|
@ -396,6 +397,12 @@ void QMyCustomPlot::onDelAllShiftLine()
|
||||||
//右键--编辑曲线
|
//右键--编辑曲线
|
||||||
void QMyCustomPlot::onEditLine()
|
void QMyCustomPlot::onEditLine()
|
||||||
{
|
{
|
||||||
|
if(m_bEditRect)
|
||||||
|
{
|
||||||
|
QMessageBox::information(nullptr, "提示", "当前正在编辑曲线,请先完成编辑!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
this->setInteractions(QCP::iSelectAxes | QCP::iSelectLegend | QCP::iSelectPlottables | QCP::iMultiSelect); // 轴、图例、图表可以被选择,并且是多选的方式
|
this->setInteractions(QCP::iSelectAxes | QCP::iSelectLegend | QCP::iSelectPlottables | QCP::iMultiSelect); // 轴、图例、图表可以被选择,并且是多选的方式
|
||||||
this->setSelectionRectMode(QCP::srmCustom); // 鼠标框选
|
this->setSelectionRectMode(QCP::srmCustom); // 鼠标框选
|
||||||
|
|
||||||
|
|
@ -413,6 +420,7 @@ void QMyCustomPlot::onEditLine()
|
||||||
}
|
}
|
||||||
// 当选择完成时,获取矩形范围并放大
|
// 当选择完成时,获取矩形范围并放大
|
||||||
QRectF rect = this->selectionRect()->rect(); // 获取选择的矩形区域(像素坐标)
|
QRectF rect = this->selectionRect()->rect(); // 获取选择的矩形区域(像素坐标)
|
||||||
|
m_bEditRect=true;//当前是否正在编辑曲线。
|
||||||
|
|
||||||
// 转换为坐标轴范围
|
// 转换为坐标轴范围
|
||||||
double top = rect.top();
|
double top = rect.top();
|
||||||
|
|
@ -421,11 +429,51 @@ void QMyCustomPlot::onEditLine()
|
||||||
double right_Hight = this->xAxis->pixelToCoord(top);
|
double right_Hight = this->xAxis->pixelToCoord(top);
|
||||||
double left_Low = this->xAxis->pixelToCoord(bottom);
|
double left_Low = this->xAxis->pixelToCoord(bottom);
|
||||||
|
|
||||||
if(right_Hight-left_Low>5)
|
float rlev=0;
|
||||||
|
if(m_x.size()>=2)
|
||||||
{
|
{
|
||||||
|
rlev = abs(m_x[1] - m_x[0]);
|
||||||
|
}
|
||||||
|
//
|
||||||
|
int left_Low_Number=0;
|
||||||
|
int right_Hight_Number=0;
|
||||||
|
for(int i =0; i<m_x.size(); i++)
|
||||||
|
{
|
||||||
|
if(abs(m_x[i]-left_Low) <= rlev)
|
||||||
|
{
|
||||||
|
left_Low_Number = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(int i =0; i<m_x.size(); i++)
|
||||||
|
{
|
||||||
|
if(abs(m_x[i]-right_Hight) <= rlev)
|
||||||
|
{
|
||||||
|
right_Hight_Number = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(abs(right_Hight-left_Low) >= (3*rlev) )//至少选中5个点
|
||||||
|
{
|
||||||
|
// for (int j=right_Hight_Number; j<left_Low_Number-1; j++)
|
||||||
|
// {
|
||||||
|
// QCPItemLine *qcpItemLine = new QCPItemLine(this);
|
||||||
|
// qcpItemLine->setPen(QPen(Qt::blue));
|
||||||
|
// qcpItemLine->setLayer("overlay"); // 确保在最上层
|
||||||
|
// qcpItemLine->start->setCoords(this->m_x[j], this->m_y[j]);
|
||||||
|
// qcpItemLine->end->setCoords(this->m_x[j+1], this->m_y[j+1]);
|
||||||
|
// }
|
||||||
|
// this->replot();
|
||||||
|
// QPixmap p = this->grab(QRect(rect.x(), rect.y(), rect.width(), rect.height()));
|
||||||
|
// p.save("./Rectpicture.png", "png");
|
||||||
|
|
||||||
//添加图形
|
//添加图形
|
||||||
//emit CallManage::getInstance()->sig_addImageToPlot(customPlot, left_Low, right_Hight, ":/image/file.png");
|
this->addSelectRectToPlot(m_x[left_Low_Number], m_x[right_Hight_Number], left_Low_Number, right_Hight_Number);//left_Low, right_Hight
|
||||||
this->addImageToPlot(left_Low, right_Hight, ":/image/file.png");
|
|
||||||
|
//取消框选
|
||||||
|
this->setInteractions(QCP::iSelectLegend | QCP::iSelectPlottables);
|
||||||
|
this->setSelectionRectMode(QCP::srmNone);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -491,8 +539,28 @@ void QMyCustomPlot::addImageToPlot(double left_Low, double right_Hight, const QS
|
||||||
//dragRect->setTitle(strText);
|
//dragRect->setTitle(strText);
|
||||||
|
|
||||||
m_mapDraggable_Image[strUuid] = dragRect;
|
m_mapDraggable_Image[strUuid] = dragRect;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void QMyCustomPlot::addSelectRectToPlot(double left_Low, double right_Hight, int left_Low_Number, int right_Hight_Number)
|
||||||
|
{
|
||||||
|
QtCommonClass *qtCommon = new QtCommonClass(this);
|
||||||
|
QString strUuid = qtCommon->getUUid();
|
||||||
|
|
||||||
|
// 在初始化代码中
|
||||||
|
TransparentDraggableSelectRect *dragRect = new TransparentDraggableSelectRect(this, strUuid);
|
||||||
|
//图片,提前设值,后面setRange改变
|
||||||
|
dragRect->setNumber(left_Low_Number, right_Hight_Number);
|
||||||
|
// 设置初始范围
|
||||||
|
dragRect->setRange(left_Low, right_Hight);
|
||||||
|
// 可选:设置颜色
|
||||||
|
dragRect->setColor(QColor(255, 255, 255, 80)); // 半透明红色
|
||||||
|
//最小宽度
|
||||||
|
dragRect->setMinWidth(0.1);
|
||||||
|
//dragRect->setTitle(strText);
|
||||||
|
|
||||||
|
m_mapDraggable_Image[strUuid] = dragRect;
|
||||||
|
}
|
||||||
|
|
||||||
//校深线段
|
//校深线段
|
||||||
void QMyCustomPlot::s_AddShifLineToPlot(QString strUuid, QString strSlfName, QString strWellName, QString strTrackName, QString strLineName, double left_Low, double right_Hight)
|
void QMyCustomPlot::s_AddShifLineToPlot(QString strUuid, QString strSlfName, QString strWellName, QString strTrackName, QString strLineName, double left_Low, double right_Hight)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -69,6 +69,7 @@ public:
|
||||||
int m_iCurNum=0;
|
int m_iCurNum=0;
|
||||||
int m_iSplitNum=2000; //2000000; //2000;
|
int m_iSplitNum=2000; //2000000; //2000;
|
||||||
bool m_addRandomGraph=false;//当前是否绘制曲线 true:是。
|
bool m_addRandomGraph=false;//当前是否绘制曲线 true:是。
|
||||||
|
bool m_bEditRect=false; //当前是否正在编辑曲线。
|
||||||
|
|
||||||
QMap<QString, QObject*> m_mapDraggable_Rect;
|
QMap<QString, QObject*> m_mapDraggable_Rect;
|
||||||
QMap<QString, QObject*> m_mapDraggable_Result;
|
QMap<QString, QObject*> m_mapDraggable_Result;
|
||||||
|
|
@ -82,6 +83,7 @@ public:
|
||||||
QMap<QString, QObject*> m_mapDraggable_Guan;
|
QMap<QString, QObject*> m_mapDraggable_Guan;
|
||||||
QMap<QString, QObject*> m_mapDraggable_Jiegutext;
|
QMap<QString, QObject*> m_mapDraggable_Jiegutext;
|
||||||
QMap<QString, QObject*> m_mapDraggable_Line;
|
QMap<QString, QObject*> m_mapDraggable_Line;
|
||||||
|
QMap<QString, QObject*> m_mapDraggable_SelectRect;
|
||||||
|
|
||||||
QObject* m_SelectShiftLine=nullptr;//当前选中的分段线
|
QObject* m_SelectShiftLine=nullptr;//当前选中的分段线
|
||||||
|
|
||||||
|
|
@ -114,6 +116,9 @@ public:
|
||||||
|
|
||||||
void addLineToPlot(double left_Low, double right_Hight, const QString strText, QColor crColor=QColor(255, 255, 255, 80));
|
void addLineToPlot(double left_Low, double right_Hight, const QString strText, QColor crColor=QColor(255, 255, 255, 80));
|
||||||
|
|
||||||
|
//框选并编辑曲线
|
||||||
|
void addSelectRectToPlot(double left_Low, double right_Hight, int left_Low_Number, int right_Hight_Number);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void s_LineClicked(int index);
|
void s_LineClicked(int index);
|
||||||
void onResetZoom();
|
void onResetZoom();
|
||||||
|
|
|
||||||
543
logPlus/transparentdraggableSelectRect.cpp
Normal file
543
logPlus/transparentdraggableSelectRect.cpp
Normal file
|
|
@ -0,0 +1,543 @@
|
||||||
|
#include "transparentdraggableSelectRect.h"
|
||||||
|
|
||||||
|
|
||||||
|
extern double g_dPixelPerCm;//每厘米像素数
|
||||||
|
//static GeoIndicatorGenerator m_drawGeo;
|
||||||
|
|
||||||
|
TransparentDraggableSelectRect::TransparentDraggableSelectRect(QMyCustomPlot *parentPlot, QString strUuid, double minWidth, QString strTitle)
|
||||||
|
: QObject(parentPlot), mPlot(parentPlot), mstrTitle(strTitle), mMinWidth(minWidth)
|
||||||
|
{
|
||||||
|
m_strUuid = strUuid;
|
||||||
|
//
|
||||||
|
initRect();
|
||||||
|
}
|
||||||
|
|
||||||
|
TransparentDraggableSelectRect::~TransparentDraggableSelectRect()
|
||||||
|
{
|
||||||
|
if(mPlot) {
|
||||||
|
// mPlot->removeItem(mRect);
|
||||||
|
// mPlot->removeItem(mLeftHandle);
|
||||||
|
// mPlot->removeItem(mRightHandle);
|
||||||
|
mPlot->m_bEditRect = false;//当前是否正在编辑曲线。
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void TransparentDraggableSelectRect::DrawSVGNormal(QPainter* painter,QString svgFileName,QRectF borderRect,bool IsWellSectonHorizonLayout)
|
||||||
|
{
|
||||||
|
QString svg=svgFileName;
|
||||||
|
QRectF boundingRect = painter->transform().mapRect(borderRect);
|
||||||
|
painter->save();
|
||||||
|
QTransform transform;
|
||||||
|
transform.reset();
|
||||||
|
if (!IsWellSectonHorizonLayout)
|
||||||
|
{
|
||||||
|
painter->setWorldTransform(transform);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
}
|
||||||
|
QPixmap tiledmap(svg);
|
||||||
|
QRect border(boundingRect.left(),boundingRect.top(),boundingRect.width(),boundingRect.height());
|
||||||
|
painter->drawPixmap(border,tiledmap);
|
||||||
|
painter->restore();
|
||||||
|
}
|
||||||
|
//拉伸
|
||||||
|
void TransparentDraggableSelectRect::DrawSVGSteched(QPainter* painter,QString svgFileName,QRectF borderRect,bool IsWellSectonHorizonLayout)
|
||||||
|
{
|
||||||
|
QString svg=svgFileName;
|
||||||
|
QSvgRenderer m_SvgRenderer;
|
||||||
|
m_SvgRenderer.load(svg);
|
||||||
|
m_SvgRenderer.render(painter,borderRect);
|
||||||
|
}
|
||||||
|
//平铺
|
||||||
|
void TransparentDraggableSelectRect::DrawSVGTiled(QPainter* painter,QString svgFileName,QRectF borderRect,bool IsWellSectonHorizonLayout)
|
||||||
|
{
|
||||||
|
QString svg=svgFileName;
|
||||||
|
QRectF boundingRect = painter->transform().mapRect(borderRect);
|
||||||
|
painter->save();
|
||||||
|
QTransform transform;
|
||||||
|
transform.reset();
|
||||||
|
if (!IsWellSectonHorizonLayout)
|
||||||
|
{
|
||||||
|
painter->setWorldTransform(transform);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
}
|
||||||
|
QPixmap tiledmap(svg);
|
||||||
|
painter->drawTiledPixmap(boundingRect,tiledmap);
|
||||||
|
painter->restore();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//设置最小宽度
|
||||||
|
void TransparentDraggableSelectRect::setMinWidth(double minWidth)
|
||||||
|
{
|
||||||
|
mMinWidth = minWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
//设置标题
|
||||||
|
void TransparentDraggableSelectRect::setTitle(QString strTitle)
|
||||||
|
{
|
||||||
|
mstrTitle = strTitle;
|
||||||
|
mItemTitle->setText(mstrTitle);
|
||||||
|
//mPlot->replot();
|
||||||
|
}
|
||||||
|
|
||||||
|
void TransparentDraggableSelectRect::setNumber(int left_Low_Number, int right_Hight_Number)
|
||||||
|
{
|
||||||
|
m_left_Low_Number = left_Low_Number;
|
||||||
|
m_right_Hight_Number = right_Hight_Number;
|
||||||
|
|
||||||
|
for (int j=right_Hight_Number; j<left_Low_Number-1; j++)
|
||||||
|
{
|
||||||
|
QCPItemLine *qcpItemLine = new QCPItemLine(mPlot);
|
||||||
|
qcpItemLine->setPen(QPen(Qt::blue));
|
||||||
|
qcpItemLine->setLayer("overlay"); // 确保在最上层
|
||||||
|
qcpItemLine->start->setCoords(mPlot->m_x[j], mPlot->m_y[j]);
|
||||||
|
qcpItemLine->end->setCoords(mPlot->m_x[j+1], mPlot->m_y[j+1]);
|
||||||
|
|
||||||
|
//
|
||||||
|
m_qcpItemLine_List.append(qcpItemLine);
|
||||||
|
}
|
||||||
|
|
||||||
|
// //
|
||||||
|
// double lY1 = mPlot->yAxis->range().lower;//+10
|
||||||
|
// double lY2 = mPlot->yAxis->range().upper;
|
||||||
|
// double left_Low = mPlot->m_x[left_Low_Number];
|
||||||
|
// double right_Hight = mPlot->m_x[right_Hight_Number];
|
||||||
|
// //
|
||||||
|
// double x1 = mPlot->xAxis->coordToPixel(left_Low);
|
||||||
|
// double x2 = mPlot->xAxis->coordToPixel(right_Hight);
|
||||||
|
// double y1 = mPlot->yAxis->coordToPixel(lY1);
|
||||||
|
// double y2 = mPlot->yAxis->coordToPixel(lY2);
|
||||||
|
// //
|
||||||
|
// QPixmap p = this->grab(QRect(x2, x1, y2-y1, x1-x2));
|
||||||
|
// p.save("./Rectpicture.png","png");
|
||||||
|
}
|
||||||
|
|
||||||
|
//设置解释结论
|
||||||
|
void TransparentDraggableSelectRect::setResult(QString filePath)
|
||||||
|
{
|
||||||
|
m_Result = filePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TransparentDraggableSelectRect::drawResult(double left_Low, double right_Hight, double lY1, double lY2)
|
||||||
|
{
|
||||||
|
if(m_Result=="")
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
double x1 = mPlot->xAxis->coordToPixel(left_Low);
|
||||||
|
double x2 = mPlot->xAxis->coordToPixel(right_Hight);
|
||||||
|
double y1 = mPlot->yAxis->coordToPixel(lY1);
|
||||||
|
double y2 = mPlot->yAxis->coordToPixel(lY2);
|
||||||
|
|
||||||
|
//
|
||||||
|
QString filePath = m_Result;
|
||||||
|
//
|
||||||
|
QString strLast = filePath.right(4);
|
||||||
|
if(strLast.toLower()==".svg")
|
||||||
|
{
|
||||||
|
QString path,filename;
|
||||||
|
GetWellNameAndPath(filePath, filename, path);
|
||||||
|
QString basename = filename;
|
||||||
|
|
||||||
|
QString val=filePath;
|
||||||
|
QImage image(y2-y1, x1-x2,QImage::Format_RGB32);
|
||||||
|
QPainter painter(&image);
|
||||||
|
QRectF fillRect(0,0, y2-y1, x1-x2);
|
||||||
|
painter.fillRect(fillRect,Qt::white);
|
||||||
|
//拉伸
|
||||||
|
DrawSVGSteched(&painter,filePath,fillRect,0);
|
||||||
|
//平铺
|
||||||
|
//DrawSVGTiled(&painter,filePath,fillRect,0);
|
||||||
|
//正常
|
||||||
|
//DrawSVGNormal(&painter,filePath,fillRect,0);
|
||||||
|
|
||||||
|
val=GetImagePath()+"TempNew";
|
||||||
|
QDir ss;
|
||||||
|
if(!ss.exists(val)) {
|
||||||
|
ss.mkdir(val);
|
||||||
|
}
|
||||||
|
val+=QDir::separator();
|
||||||
|
val+=basename+".png";
|
||||||
|
image.save(val);
|
||||||
|
|
||||||
|
//
|
||||||
|
mPixmap->setPixmap(QPixmap(val)); // 设置图片
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
mPixmap->setPixmap(QPixmap(filePath)); // 设置图片
|
||||||
|
|
||||||
|
// QString path,filename;
|
||||||
|
// GetWellNameAndPath(filePath, filename, path);
|
||||||
|
// QString basename = filename;
|
||||||
|
|
||||||
|
// QString val=filePath;
|
||||||
|
// QImage image(y2-y1, x1-x2,QImage::Format_RGB32);
|
||||||
|
// QPainter painter(&image);
|
||||||
|
// QRectF fillRect(0,0, y2-y1, x1-x2);
|
||||||
|
// painter.fillRect(fillRect,Qt::white);
|
||||||
|
// //平铺
|
||||||
|
// DrawSVGNormal(&painter,filePath,fillRect,0);
|
||||||
|
|
||||||
|
// val=GetImagePath()+"TempNew";
|
||||||
|
// QDir ss;
|
||||||
|
// if(!ss.exists(val)) {
|
||||||
|
// ss.mkdir(val);
|
||||||
|
// }
|
||||||
|
// val+=QDir::separator();
|
||||||
|
// val+=basename+".png";
|
||||||
|
// image.save(val);
|
||||||
|
|
||||||
|
// //
|
||||||
|
// mPixmap->setPixmap(QPixmap(val)); // 设置图片
|
||||||
|
}
|
||||||
|
|
||||||
|
//mPlot->replot();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置矩形范围
|
||||||
|
void TransparentDraggableSelectRect::setRange(double left_Low, double right_Hight)
|
||||||
|
{
|
||||||
|
if(left_Low >= right_Hight) return;
|
||||||
|
|
||||||
|
double lY1 = mPlot->yAxis->range().lower;//+10
|
||||||
|
double lY2 = mPlot->yAxis->range().upper;
|
||||||
|
mRect->topLeft->setCoords(left_Low, lY1);
|
||||||
|
mRect->bottomRight->setCoords(right_Hight, lY2);
|
||||||
|
|
||||||
|
double delta = (right_Hight - left_Low) / (double)(m_left_Low_Number-m_right_Hight_Number-1);
|
||||||
|
|
||||||
|
int iLoop = 0;
|
||||||
|
for (int j=m_right_Hight_Number; j<m_left_Low_Number-1; j++)
|
||||||
|
{
|
||||||
|
QCPItemLine *qcpItemLine = m_qcpItemLine_List[iLoop];
|
||||||
|
qcpItemLine->start->setCoords(right_Hight-delta*iLoop, mPlot->m_y[j]);
|
||||||
|
qcpItemLine->end->setCoords(right_Hight-delta*(iLoop+1), mPlot->m_y[j+1]);
|
||||||
|
iLoop++;
|
||||||
|
}
|
||||||
|
|
||||||
|
//位置与rect不一样,否则图像反转
|
||||||
|
// mPixmap->topLeft->setCoords(right_Hight, lY1);
|
||||||
|
// mPixmap->bottomRight->setCoords(left_Low, lY2);
|
||||||
|
//drawResult(left_Low, right_Hight, lY1, lY2);
|
||||||
|
|
||||||
|
//mItemTitle->position->setCoords(0.5, 0.5);
|
||||||
|
// 设置父锚点,定位点
|
||||||
|
//mItemTitle->position->setParentAnchor(mRect->bottom);
|
||||||
|
// mItemTitle->position->setCoords((mRect->topLeft->coords().x() + mRect->bottomRight->coords().x())/2,
|
||||||
|
// (mRect->topLeft->coords().y() + mRect->bottomRight->coords().y())/2); // 设置文本在矩形中心位置
|
||||||
|
|
||||||
|
//mRect->topLeft->setCoords(left, mPlot->yAxis->range().upper);
|
||||||
|
//mRect->bottomRight->setCoords(right, mPlot->yAxis->range().lower);
|
||||||
|
|
||||||
|
updateHandles();
|
||||||
|
mPlot->replot();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取当前范围
|
||||||
|
QCPRange TransparentDraggableSelectRect::getRange()
|
||||||
|
{
|
||||||
|
return QCPRange(mRect->topLeft->coords().x(), mRect->bottomRight->coords().x());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置矩形颜色
|
||||||
|
void TransparentDraggableSelectRect::setColor(const QColor &color)
|
||||||
|
{
|
||||||
|
mRect->setBrush(QBrush(color));
|
||||||
|
mRect->setPen(QPen(color.darker()));
|
||||||
|
//mPlot->replot();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 删除框图
|
||||||
|
void TransparentDraggableSelectRect::deleteRect()
|
||||||
|
{
|
||||||
|
if(mPlot) {
|
||||||
|
|
||||||
|
// mRect->deleteLater();
|
||||||
|
// mLeftHandle->deleteLater();
|
||||||
|
// mRightHandle->deleteLater();
|
||||||
|
// mPixmap->deleteLater();
|
||||||
|
|
||||||
|
mPlot->m_mapDraggable_Image.remove(m_strUuid);
|
||||||
|
|
||||||
|
mPlot->removeItem(mRect);
|
||||||
|
mPlot->removeItem(mLeftHandle);
|
||||||
|
mPlot->removeItem(mRightHandle);
|
||||||
|
mPlot->removeItem(mPixmap);
|
||||||
|
mPlot->removeItem(mItemTitle);
|
||||||
|
|
||||||
|
for(int i=m_qcpItemLine_List.size()-1; i>-1; i--)
|
||||||
|
{
|
||||||
|
mPlot->removeItem(m_qcpItemLine_List[i]);
|
||||||
|
}
|
||||||
|
mPlot->m_bEditRect = false;//当前是否正在编辑曲线。
|
||||||
|
|
||||||
|
mPlot->replot();
|
||||||
|
this->deleteLater();
|
||||||
|
|
||||||
|
//
|
||||||
|
// //避免二次绘制框图
|
||||||
|
// mPlot->m_bDrawRect = false;
|
||||||
|
// mDragMode = DragNone;
|
||||||
|
// //取消选中框
|
||||||
|
// mPlot->selectionRect()->cancel();
|
||||||
|
// mPlot->replot();
|
||||||
|
// mPlot->selectionRect()->mActive=true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void TransparentDraggableSelectRect::initRect()
|
||||||
|
{
|
||||||
|
// 创建透明矩形
|
||||||
|
mRect = new QCPItemRect(mPlot);
|
||||||
|
mRect->setLayer("overlay"); // 确保在最上层
|
||||||
|
mRect->setBrush(QBrush(QColor(255, 255, 255, 50))); // 半透明蓝色
|
||||||
|
mRect->setPen(QPen(QColor(70, 70, 255, 200)));
|
||||||
|
|
||||||
|
// 创建左右边界控制点
|
||||||
|
mLeftHandle = new QCPItemRect(mPlot);
|
||||||
|
mLeftHandle->setLayer("overlay");
|
||||||
|
mLeftHandle->setBrush(QBrush(Qt::red));
|
||||||
|
mLeftHandle->setPen(QPen(Qt::darkRed));
|
||||||
|
|
||||||
|
mRightHandle = new QCPItemRect(mPlot);
|
||||||
|
mRightHandle->setLayer("overlay");
|
||||||
|
mRightHandle->setBrush(QBrush(Qt::red));
|
||||||
|
mRightHandle->setPen(QPen(Qt::darkRed));
|
||||||
|
|
||||||
|
// 设置初始位置
|
||||||
|
//double center = mPlot->xAxis->range().center();
|
||||||
|
// setRange(center - 10, center + 10);
|
||||||
|
|
||||||
|
// 连接鼠标事件
|
||||||
|
connect(mPlot, &QCustomPlot::mousePress, this, &TransparentDraggableSelectRect::onMousePress);
|
||||||
|
connect(mPlot, &QCustomPlot::mouseMove, this, &TransparentDraggableSelectRect::onMouseMove);
|
||||||
|
connect(mPlot, &QCustomPlot::mouseRelease, this, &TransparentDraggableSelectRect::onMouseRelease);
|
||||||
|
|
||||||
|
mPixmap = new QCPItemPixmap(mPlot);
|
||||||
|
//mPixmap->setPixmap(QPixmap(":/image/file.png")); // 设置图片
|
||||||
|
mPixmap->setScaled(true, Qt::IgnoreAspectRatio); // 设置缩放方式
|
||||||
|
mPixmap->setLayer("overlay"); // 确保在最上层
|
||||||
|
|
||||||
|
mItemTitle = new QCPItemText(mPlot);
|
||||||
|
mItemTitle->setText(mstrTitle);
|
||||||
|
//mItemTitle->setBrush(QBrush(Qt::red));
|
||||||
|
mItemTitle->setFont(QFont("Arial", 12, QFont::Bold));
|
||||||
|
mItemTitle->setColor(Qt::black);
|
||||||
|
mItemTitle->setPositionAlignment(Qt::AlignTop | Qt::AlignHCenter);
|
||||||
|
mItemTitle->position->setType(QCPItemPosition::ptPlotCoords);
|
||||||
|
//mItemTitle->position->setType(QCPItemPosition::ptAxisRectRatio);
|
||||||
|
mItemTitle->position->setCoords(0.5, 0);
|
||||||
|
mItemTitle->setLayer("overlay");
|
||||||
|
}
|
||||||
|
|
||||||
|
void TransparentDraggableSelectRect::updateHandles()
|
||||||
|
{
|
||||||
|
// 左边界矩形控制点
|
||||||
|
mLeftHandle->topLeft->setParentAnchor(mRect->topLeft);
|
||||||
|
mLeftHandle->bottomRight->setParentAnchor(mRect->topRight);//(mRect->bottomLeft);
|
||||||
|
mLeftHandle->topLeft->setCoords(-0.5, 0.5); // 矩形大小
|
||||||
|
mLeftHandle->bottomRight->setCoords(0.5, -0.5); // 矩形大小
|
||||||
|
|
||||||
|
// 右边界矩形控制点
|
||||||
|
mRightHandle->topLeft->setParentAnchor(mRect->bottomLeft);
|
||||||
|
mRightHandle->bottomRight->setParentAnchor(mRect->bottomRight);
|
||||||
|
mRightHandle->topLeft->setCoords(-0.5, 0.5); // 矩形大小
|
||||||
|
mRightHandle->bottomRight->setCoords(0.5, -0.5); // 矩形大小
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void TransparentDraggableSelectRect::onDelRect()
|
||||||
|
{
|
||||||
|
//mDragMode = DragNone;
|
||||||
|
//删除框图
|
||||||
|
deleteRect();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void TransparentDraggableSelectRect::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, &TransparentDraggableSelectRect::onDelRect);
|
||||||
|
|
||||||
|
// QAction* pItem = menu.exec(event->globalPos());
|
||||||
|
// if(pItem == delAction)
|
||||||
|
// {
|
||||||
|
// //event->accept();
|
||||||
|
|
||||||
|
// int ii=0;
|
||||||
|
// ii++;
|
||||||
|
// }
|
||||||
|
menu.exec(event->globalPos());
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
event->accept();
|
||||||
|
|
||||||
|
// 检查点击了哪个部分
|
||||||
|
//double x = mPlot->xAxis->pixelToCoord(event->pos().x());
|
||||||
|
//double y = mPlot->yAxis->pixelToCoord(event->pos().y());
|
||||||
|
|
||||||
|
double y = mPlot->xAxis->pixelToCoord(event->pos().y());//x轴展示深度
|
||||||
|
|
||||||
|
QCPRange currentRange = getRange();
|
||||||
|
|
||||||
|
if(mLeftHandle->selectTest(event->pos(), false) < 5) {
|
||||||
|
mDragMode = DragLeft;
|
||||||
|
}
|
||||||
|
else if(mRightHandle->selectTest(event->pos(), false) < 5) {
|
||||||
|
mDragMode = DragRight;
|
||||||
|
}
|
||||||
|
//else if(x >= currentRange.lower && x <= currentRange.upper) {
|
||||||
|
else if(y >= currentRange.lower && y <= currentRange.upper) {
|
||||||
|
mDragMode = DragRect;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
mDragMode = DragNone;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//mDragStartX = x;
|
||||||
|
mDragStartY = y;
|
||||||
|
mDragStartRange = currentRange;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void TransparentDraggableSelectRect::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;
|
||||||
|
|
||||||
|
QCPRange newRange = mDragStartRange;
|
||||||
|
|
||||||
|
switch(mDragMode) {
|
||||||
|
case DragLeft: {
|
||||||
|
//double proposedLeft = mDragStartRange.lower + dx;
|
||||||
|
double proposedLeft = mDragStartRange.lower + dy;
|
||||||
|
// 确保不超出轴范围且不使宽度小于最小值
|
||||||
|
newRange.lower = qBound(
|
||||||
|
mPlot->xAxis->range().lower,
|
||||||
|
proposedLeft,
|
||||||
|
mDragStartRange.upper - mMinWidth);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case DragRight: {
|
||||||
|
//double proposedRight = mDragStartRange.upper + dx;
|
||||||
|
double proposedRight = mDragStartRange.upper + dy;
|
||||||
|
// 确保不超出轴范围且不使宽度小于最小值
|
||||||
|
newRange.upper = qBound(
|
||||||
|
mDragStartRange.lower + mMinWidth,
|
||||||
|
proposedRight,
|
||||||
|
mPlot->xAxis->range().upper);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case DragRect: {
|
||||||
|
double width = mDragStartRange.size();
|
||||||
|
//double center = mDragStartRange.center() + dx;
|
||||||
|
double center = mDragStartRange.center() + dy;
|
||||||
|
newRange.lower = center - width/2;
|
||||||
|
newRange.upper = center + width/2;
|
||||||
|
|
||||||
|
// 检查是否超出轴范围
|
||||||
|
QCPRange axisRange = mPlot->xAxis->range();
|
||||||
|
if(newRange.lower < axisRange.lower) {
|
||||||
|
newRange.lower = axisRange.lower;
|
||||||
|
newRange.upper = newRange.lower + width;
|
||||||
|
}
|
||||||
|
else if(newRange.upper > axisRange.upper) {
|
||||||
|
newRange.upper = axisRange.upper;
|
||||||
|
newRange.lower = newRange.upper - width;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// //取整数(方便显示统计,左右边界整数显示。)
|
||||||
|
// newRange.lower = (int)newRange.lower;
|
||||||
|
// QCPRange rangeByFile = mPlot->xAxis->range();
|
||||||
|
// if (std::fabs(rangeByFile.upper - (int)newRange.upper) >= 1.0)
|
||||||
|
// {
|
||||||
|
// newRange.upper = (int)newRange.upper;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// 最终确保宽度不小于最小值(针对整体拖动的情况)
|
||||||
|
if(newRange.size() < mMinWidth) {
|
||||||
|
if(mDragMode == DragRect) {
|
||||||
|
// 如果是整体拖动,保持中心点不变
|
||||||
|
double center = newRange.center();
|
||||||
|
newRange.lower = center - mMinWidth/2;
|
||||||
|
newRange.upper = center + mMinWidth/2;
|
||||||
|
} else {
|
||||||
|
// 如果是边界拖动,强制设置最小宽度
|
||||||
|
if(mDragMode == DragLeft) {
|
||||||
|
newRange.lower = newRange.upper - mMinWidth;
|
||||||
|
} else if(mDragMode == DragRight) {
|
||||||
|
newRange.upper = newRange.lower + mMinWidth;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setRange(newRange.lower, newRange.upper);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void TransparentDraggableSelectRect::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;
|
||||||
|
}
|
||||||
|
}
|
||||||
89
logPlus/transparentdraggableSelectRect.h
Normal file
89
logPlus/transparentdraggableSelectRect.h
Normal file
|
|
@ -0,0 +1,89 @@
|
||||||
|
#ifndef TRANSPARENTDRAGGABLESELECTRECT_H
|
||||||
|
#define TRANSPARENTDRAGGABLESELECTRECT_H
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
#include "qmycustomplot.h"
|
||||||
|
#include <QString>
|
||||||
|
#include <QMenu>
|
||||||
|
#include "geometryutils.h"
|
||||||
|
#include <QSvgRenderer>
|
||||||
|
|
||||||
|
#pragma execution_character_set("utf-8") // 强制指定执行字符集为 UTF-8
|
||||||
|
|
||||||
|
class TransparentDraggableSelectRect : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
explicit TransparentDraggableSelectRect(QMyCustomPlot *parentPlot, QString strUuid="", double minWidth = 1.0, QString strTitle = "");
|
||||||
|
|
||||||
|
|
||||||
|
~TransparentDraggableSelectRect();
|
||||||
|
|
||||||
|
|
||||||
|
void DrawSVGNormal(QPainter* painter,QString svgFileName,QRectF borderRect,bool IsWellSectonHorizonLayout);
|
||||||
|
//拉伸
|
||||||
|
void DrawSVGSteched(QPainter* painter,QString svgFileName,QRectF borderRect,bool IsWellSectonHorizonLayout);
|
||||||
|
//平铺
|
||||||
|
void DrawSVGTiled(QPainter* painter,QString svgFileName,QRectF borderRect,bool IsWellSectonHorizonLayout);
|
||||||
|
//设置最小宽度
|
||||||
|
void setMinWidth(double minWidth);
|
||||||
|
//设置标题
|
||||||
|
void setTitle(QString strTitle);
|
||||||
|
|
||||||
|
void setNumber(int left_Low_Number, int right_Hight_Number);
|
||||||
|
|
||||||
|
//设置解释结论
|
||||||
|
void setResult(QString filePath);
|
||||||
|
void drawResult(double left_Low, double right_Hight, double lY1, double lY2);
|
||||||
|
|
||||||
|
// 设置矩形范围
|
||||||
|
void setRange(double left_Low, double right_Hight);
|
||||||
|
// 获取当前范围
|
||||||
|
QCPRange getRange();
|
||||||
|
|
||||||
|
// 设置矩形颜色
|
||||||
|
void setColor(const QColor &color);
|
||||||
|
|
||||||
|
// 删除框图
|
||||||
|
void deleteRect();
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void rangeChanged(QCPRange newRange);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void initRect();
|
||||||
|
void updateHandles() ;
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void onDelRect();
|
||||||
|
void onMousePress(QMouseEvent *event);
|
||||||
|
void onMouseMove(QMouseEvent *event);
|
||||||
|
void onMouseRelease(QMouseEvent *event);
|
||||||
|
|
||||||
|
private:
|
||||||
|
QMyCustomPlot *mPlot;
|
||||||
|
QCPItemRect *mRect;
|
||||||
|
QCPItemRect *mLeftHandle;
|
||||||
|
QCPItemRect *mRightHandle;
|
||||||
|
|
||||||
|
QCPItemPixmap *mPixmap;
|
||||||
|
QCPItemText *mItemTitle;
|
||||||
|
QString mstrTitle="";
|
||||||
|
QString m_strUuid = "";
|
||||||
|
QString m_Result;
|
||||||
|
//
|
||||||
|
QList<QCPItemLine*> m_qcpItemLine_List;
|
||||||
|
int m_left_Low_Number;
|
||||||
|
int m_right_Hight_Number;
|
||||||
|
|
||||||
|
enum DragMode { DragNone, DragLeft, DragRight, DragRect };
|
||||||
|
DragMode mDragMode = DragNone;
|
||||||
|
//double mDragStartX = 0;
|
||||||
|
double mDragStartY = 0;
|
||||||
|
QCPRange mDragStartRange;
|
||||||
|
|
||||||
|
// 添加最小宽度成员变量
|
||||||
|
double mMinWidth;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // TRANSPARENTDRAGGABLESELECTRECT_H
|
||||||
Loading…
Reference in New Issue
Block a user