1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "infinitegrid_p.h"
5#include <QtQuick3D/private/qquick3dviewport_p.h>
6
7QT_BEGIN_NAMESPACE
8
9/*!
10 \qmltype InfiniteGrid
11 \inqmlmodule QtQuick3D.Helpers
12 \since 6.5
13 \brief Shows an infinite grid.
14
15 This helper implements an infinite grid in the horizontal plane.
16 The grid fades out as the grid lines converge or at the far clip distance,
17 whichever comes first.
18
19 The grid needs to be a child of the \l{SceneEnvironment}.
20
21 \qml
22 View3D {
23 environment: SceneEnvironment {
24 backgroundMode: SceneEnvironment.SkyBox
25 lightProbe: Texture {
26 textureData: ProceduralSkyTextureData{}
27 }
28 InfiniteGrid {
29 gridInterval: 100
30 }
31 }
32 //...
33 }
34 \endqml
35*/
36
37/*!
38 \qmlproperty float InfiniteGrid::gridInterval
39
40 This property defines the distance between grid lines. The default value is \c 1.0.
41*/
42
43/*!
44 \qmlproperty bool InfiniteGrid::visible
45
46 This property determines whether the grid is shown. The default value is \c true.
47*/
48
49/*!
50 \qmlproperty bool InfiniteGrid::gridAxes
51
52 This property determines whether the X and Y axes are marked. If \c true,
53 the X-axis will be red and the Y-axis green. The default value is \c true.
54*/
55
56QQuick3DInfiniteGrid::QQuick3DInfiniteGrid()
57{
58}
59
60QQuick3DInfiniteGrid::~QQuick3DInfiniteGrid()
61{
62}
63
64bool QQuick3DInfiniteGrid::visible() const
65{
66 return m_visible;
67}
68
69void QQuick3DInfiniteGrid::setVisible(bool newVisible)
70{
71 if (m_visible == newVisible)
72 return;
73 m_visible = newVisible;
74 emit visibleChanged();
75 if (m_sceneEnv)
76 m_sceneEnv->setGridEnabled(m_visible);
77}
78
79float QQuick3DInfiniteGrid::gridInterval() const
80{
81 return m_gridInterval;
82}
83
84void QQuick3DInfiniteGrid::setGridInterval(float newGridInterval)
85{
86 if (qFuzzyCompare(p1: m_gridInterval, p2: newGridInterval))
87 return;
88 m_gridInterval = newGridInterval;
89 emit gridIntervalChanged();
90 if (m_sceneEnv && !qFuzzyIsNull(f: m_gridInterval))
91 m_sceneEnv->setGridScale(0.1 / m_gridInterval);
92}
93
94void QQuick3DInfiniteGrid::componentComplete()
95{
96 m_componentComplete = true;
97 auto *p = parent();
98 QQuick3DSceneEnvironment *sceneEnv = nullptr;
99 while (p && !sceneEnv) {
100 sceneEnv = qobject_cast<QQuick3DSceneEnvironment *>(object: p);
101 p = p->parent();
102 }
103 if (sceneEnv) {
104 m_sceneEnv = sceneEnv;
105 Q_ASSERT(m_sceneEnv);
106 m_sceneEnv->setGridEnabled(m_visible);
107 if (!qFuzzyIsNull(f: m_gridInterval))
108 m_sceneEnv->setGridScale(0.1 / m_gridInterval);
109 updateGridFlags();
110 } else {
111 qWarning(msg: "InfiniteGrid needs to be a child of SceneEnvironment.");
112 }
113}
114
115void QQuick3DInfiniteGrid::classBegin()
116{
117}
118
119bool QQuick3DInfiniteGrid::gridAxes() const
120{
121 return m_gridAxes;
122}
123
124void QQuick3DInfiniteGrid::setGridAxes(bool newGridAxes)
125{
126 if (m_gridAxes == newGridAxes)
127 return;
128 m_gridAxes = newGridAxes;
129 emit gridAxesChanged();
130 if (m_sceneEnv)
131 updateGridFlags();
132}
133
134void QQuick3DInfiniteGrid::updateGridFlags()
135{
136 enum GridFlags { NoFlag = 0, DrawAxis = 1 };
137 uint newFlags = m_gridAxes ? DrawAxis : NoFlag;
138 m_sceneEnv->setGridFlags(newFlags);
139}
140
141QT_END_NAMESPACE
142

source code of qtquick3d/src/helpers/infinitegrid.cpp