518 lines
16 KiB
C++
518 lines
16 KiB
C++
#include "transparentdraggableSelectRect.h"
|
|
#include "CallManage.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);
|
|
}
|
|
}
|
|
|
|
// 设置矩形范围
|
|
void TransparentDraggableSelectRect::setRange(double left_Low, double right_Hight)
|
|
{
|
|
if(left_Low >= right_Hight) return;
|
|
|
|
m_left_Low_Current = left_Low;
|
|
m_right_Hight_Current = right_Hight;
|
|
//
|
|
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++;
|
|
}
|
|
|
|
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_SelectRect.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::slotSaveChange()
|
|
{
|
|
//
|
|
QList<double> left_Low_List;
|
|
QList<double> right_Hight_List;
|
|
left_Low_List.append(0 - mPlot->m_x[m_left_Low_Number]);
|
|
right_Hight_List.append(0 - mPlot->m_x[m_right_Hight_Number]);
|
|
//
|
|
left_Low_List.append(0 - m_left_Low_Current);
|
|
right_Hight_List.append(0 - m_right_Hight_Current);
|
|
|
|
QString strSlfName = mPlot->m_strSlfName;
|
|
QString strLineName = mPlot->m_strLineName;
|
|
int count = 2;
|
|
|
|
//执行校正
|
|
emit CallManage::getInstance()->sig_EShiftDepth(strSlfName, strLineName, count, left_Low_List, right_Hight_List);
|
|
|
|
|
|
//删除框图
|
|
deleteRect();
|
|
//emit sig_DelRect();
|
|
}
|
|
|
|
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;
|
|
|
|
QMenu menu(mPlot);
|
|
menu.addAction(QIcon(::GetImagePath() + "icon/savecurve.png"), "保存当前数据修改", this, &TransparentDraggableSelectRect::slotSaveChange);
|
|
menu.addAction(QIcon(::GetImagePath() + "icon/ClearSel.png"), "放弃当前数据修改", this, &TransparentDraggableSelectRect::onDelRect);
|
|
menu.exec(event->globalPos());
|
|
}
|
|
else {
|
|
mDragMode = DragNone;
|
|
return;
|
|
}
|
|
|
|
//event->accept();
|
|
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,
|
|
getMyLower(),
|
|
proposedLeft,
|
|
mDragStartRange.upper - mMinWidth);
|
|
break;
|
|
}
|
|
case DragRight: {
|
|
//double proposedRight = mDragStartRange.upper + dx;
|
|
double proposedRight = mDragStartRange.upper + dy;
|
|
// 确保不超出轴范围且不使宽度小于最小值
|
|
newRange.upper = qBound(
|
|
mDragStartRange.lower + mMinWidth,
|
|
proposedRight,
|
|
getMyUpper());
|
|
//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;
|
|
|
|
// 检查是否超出轴范围
|
|
if(newRange.lower < getMyLower()) {
|
|
newRange.lower = getMyLower();
|
|
newRange.upper = newRange.lower + width;
|
|
}
|
|
else if(newRange.upper > getMyUpper()) {
|
|
newRange.upper = getMyUpper();
|
|
newRange.lower = newRange.upper - width;
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
}
|
|
|
|
double TransparentDraggableSelectRect::getMyLower()
|
|
{
|
|
double dLower = mPlot->xAxis->range().lower;
|
|
double proposedLeft = mDragStartRange.lower;
|
|
|
|
TransparentDraggableSelectRect *pDraggableRect =NULL;
|
|
{
|
|
QMap<QString,QObject *>::Iterator it = mPlot->m_mapDraggable_SelectRect.begin();
|
|
while( it != mPlot->m_mapDraggable_SelectRect.end() )
|
|
{
|
|
if(it.key() == m_strUuid)
|
|
{
|
|
it++;
|
|
continue;
|
|
}
|
|
pDraggableRect = (TransparentDraggableSelectRect*)it.value();
|
|
//
|
|
QCPRange tmpRange = pDraggableRect->getRange();
|
|
if(tmpRange.upper >= dLower && tmpRange.upper <= proposedLeft)
|
|
{
|
|
dLower = tmpRange.upper;
|
|
}
|
|
it++;
|
|
}
|
|
}
|
|
|
|
return dLower;
|
|
}
|
|
|
|
double TransparentDraggableSelectRect::getMyUpper()
|
|
{
|
|
double dUpper = mPlot->xAxis->range().upper;
|
|
double proposedRight = mDragStartRange.upper;
|
|
|
|
TransparentDraggableSelectRect *pDraggableRect =NULL;
|
|
{
|
|
QMap<QString,QObject *>::Iterator it = mPlot->m_mapDraggable_SelectRect.begin();
|
|
while( it != mPlot->m_mapDraggable_SelectRect.end() )
|
|
{
|
|
if(it.key() == m_strUuid)
|
|
{
|
|
it++;
|
|
continue;
|
|
}
|
|
pDraggableRect = (TransparentDraggableSelectRect*)it.value();
|
|
//
|
|
QCPRange tmpRange = pDraggableRect->getRange();
|
|
if(tmpRange.lower <= dUpper && tmpRange.lower >= proposedRight)
|
|
{
|
|
dUpper = tmpRange.lower;
|
|
}
|
|
it++;
|
|
}
|
|
}
|
|
|
|
return dUpper;
|
|
}
|