summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Qt3DStudio/Palettes/Inspector/Qt3DSDMInspectable.cpp
blob: eb6803b4792965af3bcfa24c39aa8308a35a6de2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/****************************************************************************
**
** Copyright (C) 2008 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "Qt3DSDMInspectable.h"
#include "Qt3DSDMInspectorGroup.h"
#include "Qt3DSDMInspectorRow.h"
#include "StudioApp.h"
#include "Core.h"
#include "Doc.h"
#include "Qt3DSDMStudioSystem.h"
#include "ClientDataModelBridge.h"
#include "Qt3DSDMSlides.h"
#include "IDocumentReader.h"

using namespace qt3dsdm;

Qt3DSDMInspectable::Qt3DSDMInspectable(qt3dsdm::Qt3DSDMInstanceHandle instance,
                                       qt3dsdm::Qt3DSDMInstanceHandle activeSlideInstance)
    : m_instance(instance)
    , m_activeSlideInstance(activeSlideInstance)
{
    QT3DS_ASSERT(getDoc()->GetDocumentReader().IsInstance(m_instance));

    if (m_activeSlideInstance) {
        // only active root scene or components set m_activeSlideInstance
        auto *bridge = getDoc()->GetStudioSystem()->GetClientDataModelBridge();
        QT3DS_ASSERT(bridge->IsSceneInstance(instance)
                     || bridge->IsComponentInstance(instance));
    }
}

Qt3DSDMInspectable::~Qt3DSDMInspectable()
{
    for (auto g : qAsConst(m_groups))
        delete g;
    m_groups.clear();
}

// Returns the name of this inspectable
Q3DStudio::CString Qt3DSDMInspectable::getName()
{
    auto *bridge = getDoc()->GetStudioSystem()->GetClientDataModelBridge();

    if (!m_activeSlideInstance)
        return bridge->GetName(m_instance, true);

    Q3DStudio::CString theName = bridge->GetName(m_instance, true);
    theName += " (";
    theName += bridge->GetName(m_activeSlideInstance, true);
    theName += ")";

    return theName;
}

// Returns the number of groups in this inspectable
long Qt3DSDMInspectable::getGroupCount() const
{
    IMetaData &theMetaData = *getDoc()->GetStudioSystem()->GetActionMetaData();
    // In addition to a background group, Scene has a basic properties group (hidden in
    // inspector) because it is derived from Asset. Until this is fixed properly, we force the
    // Scene groups count to 1 (else an empty group will appear in the inspector).
    long count = getObjectType() == OBJTYPE_SCENE ? 1
                                    : long(theMetaData.GetGroupCountForInstance(m_instance));

    if (m_activeSlideInstance)
        count += long(theMetaData.GetGroupCountForInstance(m_activeSlideInstance));

    return count;
}

// Return the property group for display
CInspectorGroup *Qt3DSDMInspectable::getGroup(long index)
{
    TMetaDataPropertyHandleList properties = GetGroupProperties(index);
    if (m_groups.contains(index)) {
        // Check if the group properties have changed. This can f.ex happen if property
        // filter status changed and it became visible.
        std::vector<Q3DStudio::Qt3DSDMInspectorRow *> rows
                = static_cast<const Qt3DSDMInspectorGroup *>(m_groups[index])->GetRows();
        TMetaDataPropertyHandleList rowProps;

        for (uint i = 0; i < rows.size(); ++i)
            rowProps.push_back(rows[i]->GetMetaDataProperty());

        // No need to sort; if ordering has changed for some reason, group also needs to be
        // reconstructed. (Assume that the default case is that row ordering is derived from
        // property ordering, so we can use direct == operator to see if properties match the
        // inspector rows.)
        if (properties == rowProps)
            return m_groups[index];

        delete m_groups[index];
    }

    Qt3DSDMInspectorGroup *group = new Qt3DSDMInspectorGroup(GetGroupName(index));
    m_groups[index] = group;

    for (auto &prop : properties)
        group->CreateRow(getDoc(), prop);

    return group;
}

// Return the property handles for display, given the group index
TMetaDataPropertyHandleList Qt3DSDMInspectable::GetGroupProperties(long index)
{
    long activeGroupIdx = activeGroupIndex(index);
    TMetaDataPropertyHandleList retval;
    IMetaData &theMetaData = *getDoc()->GetStudioSystem()->GetActionMetaData();
    theMetaData.GetMetaDataProperties(GetGroupInstance(index), retval);
    qt3dsdm::IPropertySystem &thePropertySystem(*getDoc()->GetStudioSystem()->GetPropertySystem());
    // get name of the current group for filtering
    Option<qt3dsdm::TCharStr> theGroupFilterName =
            theMetaData.GetGroupFilterNameForInstance(GetGroupInstance(index), activeGroupIdx);
    long theGroupCount = getGroupCount();

    // end is explicitly required
    for (size_t idx = 0; idx < retval.size(); ++idx) {
        if (theMetaData.GetMetaDataPropertyInfo(retval[idx])->m_IsHidden) {
            retval.erase(retval.begin() + idx);
            --idx;
        } else if (theGroupCount > 1 && theGroupFilterName.hasValue()
                   && theMetaData.GetMetaDataPropertyInfo(retval[idx])->m_GroupName
                   != theGroupFilterName) {
            retval.erase(retval.begin() + idx);
            --idx;
        } else {
            qt3ds::foundation::NVConstDataRef<SPropertyFilterInfo> theFilters(
                        theMetaData.GetMetaDataPropertyFilters(retval[idx]));
            if (theFilters.size()) {
                Option<bool> keepProperty;
                // The tests are done in an ambiguous way.  Really, show if equal should take
                // multiple conditions
                // as should hide if equal.  They do not, so we need to rigorously define exactly
                // how those two interact.
                for (QT3DSU32 propIdx = 0, propEnd = theFilters.size(); propIdx < propEnd;
                     ++propIdx) {
                    const SPropertyFilterInfo &theFilter(theFilters[propIdx]);

                    QT3DS_ASSERT(theFilter.m_FilterType == PropertyFilterTypes::ShowIfEqual
                                 || theFilter.m_FilterType == PropertyFilterTypes::HideIfEqual);

                    SValue theValue;
                    thePropertySystem.GetInstancePropertyValue(
                                GetGroupInstance(index), theFilter.m_FilterProperty, theValue);
                    bool resultIfTrue = theFilter.m_FilterType == PropertyFilterTypes::ShowIfEqual;
                    if (Equals(theValue.toOldSkool(), theFilter.m_Value.toOldSkool())) {
                        keepProperty = resultIfTrue;
                        break;
                    } else {
                        keepProperty = !resultIfTrue;
                    }
                }
                if (keepProperty.hasValue() && *keepProperty == false) {
                    retval.erase(retval.begin() + idx);
                    --idx;
                }
            }
        }
    }
    return retval;
}

// Return the Group Name, given the group index
QString Qt3DSDMInspectable::GetGroupName(long groupIndex)
{
    std::vector<TCharStr> theGroupNames;
    IMetaData &theMetaData = *getDoc()->GetStudioSystem()->GetActionMetaData();
    theMetaData.GetGroupNamesForInstance(GetGroupInstance(groupIndex), theGroupNames);

    long activeGroupIdx = activeGroupIndex(groupIndex);
    if (activeGroupIdx < theGroupNames.size())
        return QString::fromWCharArray(theGroupNames[activeGroupIdx].wide_str());

    return QObject::tr("Basic Properties");
}

// Return the Inspectable Instance Handle for the Group, given the group index
Qt3DSDMInstanceHandle Qt3DSDMInspectable::GetGroupInstance(long inGroupIndex)
{
    // if active root, return the slide instance at first index
    if (m_activeSlideInstance && inGroupIndex == 0)
        return m_activeSlideInstance;

    return m_instance;
}

EStudioObjectType Qt3DSDMInspectable::getObjectType() const
{
    return getDoc()->GetStudioSystem()->GetClientDataModelBridge()->GetObjectType(m_instance);
}

bool Qt3DSDMInspectable::isValid() const
{
    if (m_activeSlideInstance) {
        return getDoc()->GetStudioSystem()->IsInstance(m_instance)
                && getDoc()->GetStudioSystem()->IsInstance(m_activeSlideInstance);
    }
    return getDoc()->GetStudioSystem()->IsInstance(m_instance);
}

bool Qt3DSDMInspectable::isMaster() const
{
    ISlideSystem *slideSystem = getDoc()->GetStudioSystem()->GetSlideSystem();
    qt3dsdm::Qt3DSDMSlideHandle theSlideHandle = slideSystem->GetAssociatedSlide(m_instance);
    if (theSlideHandle.Valid())
        return slideSystem->IsMasterSlide(theSlideHandle);
    // Slide handle may not be valid if we are selecting the Scene or if we are inside Component and
    // we select the Component root.
    return false;
}

// Returns the group index taking into consideration that for active roots, first index is the slide
// group so need to decrement all index bigger than 1, by 1. For scene we decrement 1 more because
// the first group (Basic properties) is not in use.
long Qt3DSDMInspectable::activeGroupIndex(long groupIndex) const
{
    if (m_activeSlideInstance && groupIndex > 0 && getObjectType() != OBJTYPE_SCENE)
        return groupIndex - 1;

    return groupIndex;
}

CDoc *Qt3DSDMInspectable::getDoc() const
{
    return g_StudioApp.GetCore()->GetDoc();
}