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