8#include "LineGridNode.h"
12#include <QSGFlatColorMaterial>
14LineGridNode::LineGridNode()
18 m_geometry->setLineWidth(m_lineWidth);
21 m_material =
new QSGFlatColorMaterial{};
22 m_material->setColor(QColor(255, 0, 0, 255));
28LineGridNode::~LineGridNode()
32void LineGridNode::setVisible(
bool visible)
34 if (visible == m_visible) {
42void LineGridNode::setVertical(
bool vertical)
44 if (vertical == m_vertical) {
48 m_vertical = vertical;
51void LineGridNode::setRect(
const QRectF &rect)
60void LineGridNode::setColor(
const QColor &color)
62 if (color == m_material->color()) {
66 m_material->setColor(color);
70void LineGridNode::setSpacing(
float spacing)
72 if (qFuzzyCompare(spacing, m_spacing)) {
79void LineGridNode::setLineWidth(
float lineWidth)
81 if (qFuzzyCompare(lineWidth, m_lineWidth)) {
85 m_lineWidth = lineWidth;
86 m_geometry->setLineWidth(lineWidth);
90bool LineGridNode::isSubtreeBlocked()
const
95void LineGridNode::update()
97 if (!m_rect.isValid()) {
101 int totalVertices = 0;
103 totalVertices = std::floor(m_rect.width() / std::ceil(m_spacing)) * 2 + 4;
105 totalVertices = std::floor(m_rect.height() / std::ceil(m_spacing)) * 2 + 4;
108 if (totalVertices < 4) {
112 if (totalVertices != m_geometry->vertexCount()) {
113 m_geometry->allocate(totalVertices, totalVertices);
116 auto vertices = m_geometry->vertexDataAsPoint2D();
117 auto indices = m_geometry->indexDataAsUShort();
119 if (!vertices || !indices) {
125 line(vertices, indices, index, m_rect.left(), m_rect.top(), m_rect.right(), m_rect.top());
128 for (
auto i = 0; i < (totalVertices - 4) / 2; ++i) {
129 line(vertices, indices, index, m_rect.left(), y, m_rect.right(), y);
133 line(vertices, indices, index, m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
135 line(vertices, indices, index, m_rect.left(), m_rect.top(), m_rect.left(), m_rect.bottom());
138 for (
auto i = 0; i < (totalVertices - 4) / 2; ++i) {
139 line(vertices, indices, index, x, m_rect.top(), x, m_rect.bottom());
143 line(vertices, indices, index, m_rect.right(), m_rect.top(), m_rect.right(), m_rect.bottom());
146 m_geometry->markVertexDataDirty();
147 m_geometry->markIndexDataDirty();
151void LineGridNode::line(
QSGGeometry::Point2D *vertices, quint16 *indices,
int &index, qreal fromX, qreal fromY, qreal toX, qreal toY)
153 indices[index] = index;
154 vertices[index++].
set(fromX, fromY);
155 indices[index] = index;
156 vertices[index++].
set(toX, toY);
void setGeometry(QSGGeometry *geometry)
void set(float x, float y)
const AttributeSet & defaultAttributes_Point2D()
void setMaterial(QSGMaterial *material)
void markDirty(DirtyState bits)
void setFlags(Flags f, bool enabled)