1/****************************************************************************
2**
3** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt3D module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qlevelofdetailswitch.h"
41#include "qlevelofdetailswitch_p.h"
42#include "qlevelofdetail_p.h"
43#include "qglobal.h"
44#include <Qt3DCore/QEntity>
45
46QT_BEGIN_NAMESPACE
47
48namespace Qt3DRender {
49
50QLevelOfDetailSwitchPrivate::QLevelOfDetailSwitchPrivate()
51 : QLevelOfDetailPrivate()
52{
53
54}
55
56void QLevelOfDetailSwitchPrivate::setCurrentIndex(int currentIndex)
57{
58 Q_Q(QLevelOfDetailSwitch);
59
60 bool changed = m_currentIndex != currentIndex;
61 QLevelOfDetailPrivate::setCurrentIndex(currentIndex);
62
63 if (!changed)
64 return;
65
66 int entityIndex = 0;
67 const auto entities = q->entities();
68 for (Qt3DCore::QEntity *entity : entities) {
69 const auto childNodes = entity->childNodes();
70 for (Qt3DCore::QNode *childNode : childNodes) {
71 Qt3DCore::QEntity *childEntity = qobject_cast<Qt3DCore::QEntity *>(object: childNode);
72 if (childEntity) {
73 childEntity->setEnabled(entityIndex == currentIndex);
74 entityIndex++;
75 }
76 }
77
78 break; // only work on the first entity, LOD should not be shared
79 }
80}
81
82/*!
83 \class Qt3DRender::QLevelOfDetailSwitch
84 \inmodule Qt3DRender
85 \inherits Qt3DRender::QLevelOfDetail
86 \since 5.9
87 \brief Provides a way of enabling child entities based on distance or screen size.
88
89 This component is assigned to an entity. When the entity changes distance relative
90 to the camera, the QLevelOfDetailSwitch will disable all the child entities except
91 the one matching index Qt3DRender::QLevelOfDetailSwitch::currentIndex.
92*/
93
94/*!
95 \qmltype LevelOfDetailSwitch
96 \instantiates Qt3DRender::QLevelOfDetailSwitch
97 \inherits Component3D
98 \inqmlmodule Qt3D.Render
99 \since 5.9
100 \brief Provides a way of enabling child entities based on distance or screen size.
101
102 This component is assigned to an entity. When the entity changes distance relative
103 to the camera, the LevelOfDetailSwitch will disable all the child entities except
104 the one matching index \l currentIndex.
105
106 \sa LevelOfDetail
107*/
108
109/*!
110 \qmlproperty int LevelOfDetailSwitch::currentIndex
111
112 The index of the presently selected child entity.
113*/
114
115/*! \fn Qt3DRender::QLevelOfDetailSwitch::QLevelOfDetailSwitch(Qt3DCore::QNode *parent)
116 Constructs a new QLevelOfDetailSwitch with the specified \a parent.
117 */
118QLevelOfDetailSwitch::QLevelOfDetailSwitch(QNode *parent)
119 : QLevelOfDetail(*new QLevelOfDetailSwitchPrivate(), parent)
120{
121 Q_D(QLevelOfDetailSwitch);
122 d->m_currentIndex = -1;
123}
124
125/*! \internal */
126QLevelOfDetailSwitch::~QLevelOfDetailSwitch()
127{
128}
129
130/*! \internal */
131QLevelOfDetailSwitch::QLevelOfDetailSwitch(QLevelOfDetailPrivate &dd, QNode *parent)
132 : QLevelOfDetail(dd, parent)
133{
134}
135
136// TODO Unused remove in Qt6
137void QLevelOfDetailSwitch::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &)
138{
139}
140
141} // namespace Qt3DRender
142
143QT_END_NAMESPACE
144

source code of qt3d/src/render/frontend/qlevelofdetailswitch.cpp