159 lines
4.2 KiB
C++
159 lines
4.2 KiB
C++
#include "mygraphicsgriditem.h"
|
|
#include <cmath>
|
|
|
|
MyGraphicsGridItem::MyGraphicsGridItem(double gridSpacing,QRectF rect)
|
|
: m_gridSpacing(gridSpacing), m_rect(rect)
|
|
{
|
|
m_gridSpacing = m_gridSpacing / 8.0;
|
|
m_boundingRect = m_rect;
|
|
double left = m_rect.left();
|
|
double right = m_rect.right();
|
|
double top = m_rect.top();
|
|
double bottom = m_rect.bottom();
|
|
// 水平网格线 Y
|
|
for (double y = top; y <= bottom; y += m_gridSpacing)
|
|
{
|
|
m_listY.append(y);
|
|
}
|
|
|
|
// 垂直网格线
|
|
for (double x = left; x <= right; x += m_gridSpacing)
|
|
{
|
|
m_listX.append(x);
|
|
}
|
|
|
|
}
|
|
|
|
MyGraphicsGridItem::~MyGraphicsGridItem()
|
|
{
|
|
}
|
|
|
|
void MyGraphicsGridItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
{
|
|
return QGraphicsItem::mousePressEvent(event);
|
|
}
|
|
|
|
void MyGraphicsGridItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
|
{
|
|
return QGraphicsItem::mouseMoveEvent(event);
|
|
}
|
|
|
|
void MyGraphicsGridItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
|
{
|
|
return QGraphicsItem::mouseReleaseEvent(event);
|
|
}
|
|
|
|
QRectF MyGraphicsGridItem::boundingRect() const
|
|
{
|
|
return m_boundingRect;
|
|
}
|
|
|
|
void MyGraphicsGridItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
{
|
|
Q_UNUSED(option);
|
|
Q_UNUSED(widget);
|
|
|
|
// 获取当前的变换矩阵
|
|
QTransform transform = painter->worldTransform();
|
|
double scaleFactor = sqrt(transform.m11() * transform.m11() +
|
|
transform.m22() * transform.m22());
|
|
|
|
QPen pen;
|
|
pen.setWidth(1);
|
|
double width = pen.width()/scaleFactor;
|
|
pen.setWidthF(width); // 线段保持原来的线宽
|
|
painter->setPen(pen);
|
|
|
|
// 计算网格线的数量
|
|
double left = m_rect.left();
|
|
double right = m_rect.right();
|
|
double top = m_rect.top();
|
|
double bottom = m_rect.bottom();
|
|
int num = ((right - left) / m_gridSpacing) / 2 ;
|
|
double middle = num * m_gridSpacing;
|
|
const double epsilon = 1e-9;
|
|
|
|
// 水平网格线
|
|
for (int i = 0; i < m_listY.size(); i++)
|
|
{
|
|
double y = m_listY.at(i);
|
|
int indexInt = qRound((y - top) / m_gridSpacing);
|
|
|
|
if(fabs(y - middle) < epsilon)
|
|
{
|
|
pen.setColor(Qt::red);
|
|
pen.setStyle(Qt::SolidLine);
|
|
}
|
|
else if(indexInt % 8 == 6)
|
|
{
|
|
pen.setColor(Qt::black);
|
|
pen.setStyle(Qt::SolidLine);
|
|
}
|
|
else
|
|
{
|
|
pen.setColor(Qt::transparent);//透明色
|
|
}
|
|
painter->setPen(pen);
|
|
painter->drawLine(QPointF(left, y), QPointF(right, y));//加1为笔宽
|
|
}
|
|
|
|
// 垂直网格线
|
|
for (int i = 0; i < m_listX.size(); i++)
|
|
{
|
|
double x = m_listX.at(i);
|
|
int indexInt = qRound((x - top) / m_gridSpacing);
|
|
if(fabs(x - middle) < epsilon)
|
|
{
|
|
pen.setColor(Qt::red);
|
|
pen.setStyle(Qt::SolidLine);
|
|
}
|
|
else if(indexInt % 8 == 6)
|
|
{
|
|
pen.setColor(Qt::black);
|
|
pen.setStyle(Qt::SolidLine);
|
|
}
|
|
else
|
|
{
|
|
pen.setColor(Qt::transparent);//透明色
|
|
}
|
|
painter->setPen(pen);
|
|
painter->drawLine(QPointF(x, top), QPointF(x, bottom));
|
|
// 添加数字标注
|
|
// QGraphicsTextItem *textItem = m_scene->addText(QString::number(x), font);
|
|
// textItem->setPos(x, top-gridSpacing);
|
|
// textItem->setDefaultTextColor(Qt::black);
|
|
}
|
|
}
|
|
|
|
QPointF MyGraphicsGridItem::getClosestFocus(QPointF point)
|
|
{
|
|
if (m_listX.isEmpty() || m_listY.isEmpty()) {
|
|
return QPointF(0,0);
|
|
}
|
|
|
|
double minDiffX = std::numeric_limits<double>::max();
|
|
double minDiffY = std::numeric_limits<double>::max();
|
|
double closestX = 0.0;
|
|
double closestY = 0.0;
|
|
|
|
// 找离 point.x() 最近的 X 值
|
|
for (double x : m_listX) {
|
|
double diff = std::fabs(point.x() - x);
|
|
if (diff < minDiffX) {
|
|
minDiffX = diff;
|
|
closestX = x;
|
|
}
|
|
}
|
|
|
|
// 找离 point.y() 最近的 Y 值
|
|
for (double y : m_listY) {
|
|
double diff = std::fabs(point.y() - y);
|
|
if (diff < minDiffY) {
|
|
minDiffY = diff;
|
|
closestY = y;
|
|
}
|
|
}
|
|
|
|
return QPointF(closestX, closestY);
|
|
}
|