summaryrefslogtreecommitdiffstats
path: root/src/dm/systems/cores/SimpleSlideCore.h
blob: effa3bdf0af0ae10185de5a4233b42850c97641b (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/****************************************************************************
**
** Copyright (C) 1993-2009 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$
**
****************************************************************************/
#pragma once
#ifndef SLIDECOREH
#define SLIDECOREH
#include "HandleSystemBase.h"
#include "Qt3DSDMSlideCore.h"
#include "SimpleDataCore.h"
#include "Qt3DSDMStringTable.h"
#include <unordered_map>
#include <QtCore/qdebug.h>

namespace std {

template<> struct hash<std::pair<int,int> >
{
    typedef std::pair<int,int> argument_type;
    typedef std::size_t result_type;
    result_type operator()(std::pair<int,int> const& pa) const
    {
        result_type const h1 ( std::hash<int>{}(pa.first) );
        result_type const h2 ( std::hash<int>{}(pa.second) );
        return h1 ^ (h2 << 1);
    }
};

}

namespace qt3dsdm {

// The first revision of this
typedef std::pair<int, int> TSlideInstancePropertyPair;
typedef std::unordered_map<TSlideInstancePropertyPair, SInternValue > TSlideEntryHash;

using std::make_pair;

// Abstract access to these objects a little bit because in the future we are going to
// reorganize the data such that getting a defined set of properties for a single instance is
// very fast.
struct SSlide : public CHandleObject
{
    SSlide()
        : m_Instance(0)
        , m_Parent(0)
    {
    }
    SSlide(int inHandle, int inInstance)
        : CHandleObject(inHandle)
        , m_Instance(inInstance)
        , m_Parent(0)
        , m_Time(0)
    {
    }
    int m_Instance;
    int m_Parent;
    TIntList m_Children;
    TSlideEntryHash m_Properties;
    long m_Time;

    // Returns true if it was inserted, false if the property value was set.
    bool SetInstancePropertyValue(Qt3DSDMInstanceHandle inInstance, Qt3DSDMPropertyHandle inProperty,
                                  const SInternValue &inValue)
    {
        TSlideInstancePropertyPair theKey(inInstance.GetHandleValue(), inProperty.GetHandleValue());
        std::pair<TSlideEntryHash::iterator, bool> insertResult =
            m_Properties.insert(std::make_pair(theKey, inValue));

        if (insertResult.second == false)
            insertResult.first->second = inValue;
        return insertResult.second;
    }
    // Returns true if the property was deleted
    bool RemoveInstancePropertyValue(Qt3DSDMInstanceHandle inInstance,
                                     Qt3DSDMPropertyHandle inProperty)
    {
        TSlideInstancePropertyPair theKey(inInstance.GetHandleValue(), inProperty.GetHandleValue());
        TSlideEntryHash::iterator find(m_Properties.find(theKey));
        if (find != m_Properties.end()) {
            m_Properties.erase(find);
            return true;
        }
        return false;
    }
    // Return a pointer to out property value.  This allows quicker checks for isset and such
    SInternValue *GetInstancePropertyValue(Qt3DSDMInstanceHandle inInstance,
                                           Qt3DSDMPropertyHandle inProperty) const
    {
        TSlideInstancePropertyPair theKey(inInstance.GetHandleValue(), inProperty.GetHandleValue());
        TSlideEntryHash::const_iterator find(m_Properties.find(theKey));
        if (find != m_Properties.end())
            return const_cast<SInternValue *>(&find->second);
        return NULL;
    }

    void GetSpecificInstancePropertyValues(Qt3DSDMInstanceHandle inInstance,
                                           TPropertyHandleValuePairList &outProperties) const
    {
        for (TSlideEntryHash::const_iterator theIter = m_Properties.begin(),
                                             theEnd = m_Properties.end();
             theIter != theEnd; ++theIter) {
            if (theIter->first.first == inInstance)
                outProperties.push_back(
                    make_pair(theIter->first.second, theIter->second.GetValue()));
        }
    }

    bool HasProperty(std::function<bool(const TSlideEntry &)> inPredicate) const
    {
        for (TSlideEntryHash::const_iterator theIter = m_Properties.begin(),
                                             theEnd = m_Properties.end();
             theIter != theEnd; ++theIter) {
            if (inPredicate(TSlideEntry(theIter->first.first, theIter->first.second,
                                        theIter->second.GetValue())))
                return true;
        }
        return false;
    }

    void DeleteSlideEntries(TSlideEntryList &outList,
                            std::function<bool(const TSlideEntry &)> inPredicate)
    {
        for (TSlideEntryHash::const_iterator theIter = m_Properties.begin(),
                                             theEnd = m_Properties.end();
             theIter != theEnd; ++theIter) {
            if (inPredicate(TSlideEntry(theIter->first.first, theIter->first.second,
                                        theIter->second.GetValue()))) {
                outList.push_back(TSlideEntry(theIter->first.first, theIter->first.second,
                                              theIter->second.GetValue()));
            }
        }
        DeleteEntriesFromList(outList);
    }

    void DeleteEntriesFromList(const TSlideEntryList &inList)
    {
        for (size_t idx = 0, end = inList.size(); idx < end; ++idx)
            m_Properties.erase(
                std::pair<int, int>(std::get<0>(inList[idx]), std::get<1>(inList[idx])));
    }

    void InsertSlideEntries(const TSlideEntryList &inList, IStringTable &inStringTable)
    {
        for (size_t idx = 0, end = inList.size(); idx < end; ++idx) {
            TSlideInstancePropertyPair theKey(std::get<0>(inList[idx]), std::get<1>(inList[idx]));

            if (m_Properties.find(theKey) != m_Properties.end()) {
                // The only known case when this condition happens is when DnD a sub-presentation to
                // the scene as a texture rect then undoing.
                qWarning() << __FUNCTION__ << ": Instance/Property Pair" << theKey
                                           << "already exists, erasing it.";
                m_Properties.erase(theKey);
            }

            m_Properties.insert(
                std::make_pair(theKey, SInternValue(std::get<2>(inList[idx]), inStringTable)));
        }
    }

    // Convert to the older, straight forward representation of the
    // data in this slide.
    void ToSlideEntryList(TSlideEntryList &outList) const
    {
        for (TSlideEntryHash::const_iterator theIter = m_Properties.begin(),
                                             theEnd = m_Properties.end();
             theIter != theEnd; ++theIter)
            outList.push_back(TSlideEntry(theIter->first.first, theIter->first.second,
                                          theIter->second.GetValue()));
    }

    void FromSlideEntryList(const TSlideEntryList &inList, IStringTable &inStringTable)
    {
        using namespace std;
        m_Properties.clear();
        for (TSlideEntryList::const_iterator theIter = inList.begin(), theEnd = inList.end();
             theIter != theEnd; ++theIter)
            SetInstancePropertyValue(get<0>(*theIter), get<1>(*theIter),
                                     SInternValue(get<2>(*theIter), inStringTable));
    }

    // result is the instance, property, myvalue, othervalue
    void IntersectProperties(const SSlide &inOther,
                             std::function<void(Qt3DSDMInstanceHandle, Qt3DSDMPropertyHandle,
                                                  SInternValue, SInternValue)>
                                 inResult) const
    {
        for (TSlideEntryHash::const_iterator theIter = m_Properties.begin(),
                                             theEnd = m_Properties.end();
             theIter != theEnd; ++theIter) {
            SInternValue *otherValue =
                inOther.GetInstancePropertyValue(theIter->first.first, theIter->first.second);
            if (otherValue)
                inResult(theIter->first.first, theIter->first.second, theIter->second, *otherValue);
        }
    }

    // Call the predicate, if it returns true set the property for all properties.
    // This allows a third party to manipulate the property values during the process.
    void SetPropertyValuesIf(
        IStringTable &inStringTable,
        std::function<bool(Qt3DSDMInstanceHandle, Qt3DSDMPropertyHandle, SValue &)> inPredIntercept)
    {
        for (TSlideEntryHash::iterator theIter = m_Properties.begin(), theEnd = m_Properties.end();
             theIter != theEnd; ++theIter) {
            SValue theValue(theIter->second.GetValue());
            if (inPredIntercept(theIter->first.first, theIter->first.second, theValue))
                theIter->second = SInternValue(theValue, inStringTable);
        }
    }

    void
    ClearPropertiesIf(std::function<bool(Qt3DSDMInstanceHandle, Qt3DSDMPropertyHandle)> inPredicate)
    {
        bool foundOne;
        do {
            foundOne = false;
            for (TSlideEntryHash::iterator theIter = m_Properties.begin(),
                                           theEnd = m_Properties.end();
                 theIter != theEnd; ++theIter) {
                if (inPredicate(theIter->first.first, theIter->first.second)) {
                    foundOne = true;
                    m_Properties.erase(theIter->first);
                    break;
                }
            }

        } while (foundOne);
    }

    static const EHandleObjectType s_Type = CHandleObject::EHandleObjectTypeSSlide;
    EHandleObjectType GetType() override { return s_Type; }
};

class CSimpleSlideCore : public CHandleBase, public ISlideCore
{
    TStringTablePtr m_StringTable;

public: // use
    CSimpleSlideCore(TStringTablePtr inStrTable)
        : m_StringTable(inStrTable)
    {
    }

    TStringTablePtr GetStringTablePtr() const override { return m_StringTable; }
    IStringTable &GetStringTable() const override { return *m_StringTable.get(); }
    Qt3DSDMSlideHandle CreateSlide(Qt3DSDMInstanceHandle inInstance) override;
    Qt3DSDMInstanceHandle GetSlideInstance(Qt3DSDMSlideHandle inSlide) const override;
    Qt3DSDMSlideHandle GetSlideByInstance(Qt3DSDMInstanceHandle inInstance) const override;
    void DeleteSlide(Qt3DSDMSlideHandle inSlide, TInstanceHandleList &outInstances) override;
    void GetSlides(TSlideHandleList &outSlides) const override;

    long GetSlideTime(Qt3DSDMSlideHandle inSlide) const override;
    void SetSlideTime(Qt3DSDMSlideHandle inSlide, long inNewTime) override;

    void DeriveSlide(Qt3DSDMSlideHandle inSlide, Qt3DSDMSlideHandle inParent, int inIndex = -1) override;
    Qt3DSDMSlideHandle GetParentSlide(Qt3DSDMSlideHandle inSlide) const override;
    void GetChildSlides(Qt3DSDMSlideHandle inSlide, TSlideHandleList &outChildren) const override;
    int GetChildIndex(Qt3DSDMSlideHandle inParent, Qt3DSDMSlideHandle inChild) const override;

    bool GetInstancePropertyValue(Qt3DSDMSlideHandle inSlide, Qt3DSDMInstanceHandle inHandle,
                                  Qt3DSDMPropertyHandle inProperty, SValue &outValue) const override;
    void SetInstancePropertyValue(Qt3DSDMSlideHandle inSlide, Qt3DSDMInstanceHandle inHandle,
                                  Qt3DSDMPropertyHandle inProperty, const SValue &inValue) override;

    // Return the slide this property should be set on, along with the previous value if any.
    // Set the value on the slide.
    std::pair<SSlide *, SInternValue *>
    ResolveSetInstancePropertyValue(Qt3DSDMSlideHandle inSlide, Qt3DSDMInstanceHandle inHandle,
                                    Qt3DSDMPropertyHandle inProperty);
    void ForceSetInstancePropertyValue(Qt3DSDMSlideHandle inSlide, Qt3DSDMInstanceHandle inHandle,
                                       Qt3DSDMPropertyHandle inProperty, const SValue &inValue) override;
    void forceSetInstancePropertyValueOnAllSlides(Qt3DSDMInstanceHandle inInstance,
                                                  Qt3DSDMPropertyHandle inProperty,
                                                  const SValue &inValue) override;

    bool GetSpecificInstancePropertyValue(Qt3DSDMSlideHandle inSlide,
                                          Qt3DSDMInstanceHandle inInstance,
                                          Qt3DSDMPropertyHandle inProperty, SValue &outValue) const override;

    void GetSpecificInstancePropertyValues(Qt3DSDMSlideHandle inSlide,
                                           Qt3DSDMInstanceHandle inInstance,
                                           TPropertyHandleValuePairList &outValues) override;

    void GetSlidePropertyEntries(Qt3DSDMSlideHandle inSlide, TSlideEntryList &outEntries) const override;

    void PushPropertyValueToChildren(Qt3DSDMSlideHandle inParent, Qt3DSDMInstanceHandle inHandle,
                                     Qt3DSDMPropertyHandle inProperty, const SValue &inValue) override;

    void GetIntersectingProperties(Qt3DSDMSlideHandle inSlide1, Qt3DSDMSlideHandle inSlide2,
                                   TSlideEntryList &outEntries) const override;
    void PushIntersectingProperties(Qt3DSDMSlideHandle inSlide1, Qt3DSDMSlideHandle inSlide2,
                                    Qt3DSDMSlideHandle inDestination) override;

    void ClearChildrenPropertyValues(Qt3DSDMSlideHandle inParent, Qt3DSDMInstanceHandle inHandle,
                                     Qt3DSDMPropertyHandle inProperty) override;

    void DeleteAllInstanceEntries(Qt3DSDMInstanceHandle inHandle) override;
    void DeleteAllPropertyEntries(Qt3DSDMPropertyHandle inHandle) override;
    void DeleteAllInstancePropertyEntries(const TInstanceHandleList &inInstances,
                                          const TPropertyHandleList &inProperties) override;

    bool ContainsProperty(Qt3DSDMSlideHandle inSlide, Qt3DSDMInstanceHandle inHandle,
                          Qt3DSDMPropertyHandle inProperty) const override;
    bool HandleValid(int inHandle) const override { return CHandleBase::HandleValid(inHandle); }

    Qt3DSDMSlideHandle CreateSlideWithHandle(int inHandle, Qt3DSDMInstanceHandle inInstance);
    void GetSlideProperties(Qt3DSDMSlideHandle inSlide, TSlideEntryList &outProperties) const;

    bool IsSlide(Qt3DSDMSlideHandle inSlide) const override;

    // Possibly alter every slide in the database
    void ForEachSlide(std::function<void(SSlide *)> inFunction);
    void ForEachChild(Qt3DSDMSlideHandle inSlide, std::function<void(SSlide *)> inFunction);

    // Only implemented at the producer level, not at this lower level.
    void CopyProperties(Qt3DSDMSlideHandle /*inSourceSlide*/,
                        Qt3DSDMInstanceHandle /*inSourceInstance*/,
                        Qt3DSDMSlideHandle /*inDestSlide*/, Qt3DSDMInstanceHandle /*inDestInstance*/) override
    {
        throw SlideNotFound(L"");
    }

    static SSlide *GetSlideNF(int inHandle, THandleObjectMap &inObjects)
    {
        return const_cast<SSlide *>(
            GetSlideNF(inHandle, static_cast<const THandleObjectMap &>(inObjects)));
    }

    static const SSlide *GetSlideNF(int inHandle, const THandleObjectMap &inObjects)
    {
        const SSlide *theSlide = GetHandleObject<SSlide>(inHandle, inObjects);
        if (theSlide)
            return theSlide;
        throw SlideNotFound(L"");
    }

    static inline bool PropertyFound(int inInstance, int inProperty, const TSlideEntry &inEntry)
    {
        if (inInstance == std::get<0>(inEntry) && inProperty == std::get<1>(inEntry))
            return true;
        return false;
    }

    static void ForceSetPropertyValue(IStringTable &inStringTable, THandleObjectMap &inObjects,
                                      Qt3DSDMSlideHandle inSlide, Qt3DSDMInstanceHandle inHandle,
                                      Qt3DSDMPropertyHandle inProperty, const SValue &inValue);

    static void ClearPropertyValue(THandleObjectMap &inObjects, Qt3DSDMSlideHandle inSlide,
                                   Qt3DSDMInstanceHandle inInstance,
                                   Qt3DSDMPropertyHandle inProperty);

    static inline bool SlideEntryInstanceMatches(const TSlideEntry &inEntry,
                                                 Qt3DSDMInstanceHandle inHandle)
    {
        using namespace std;
        if (inHandle.GetHandleValue() == get<0>(inEntry))
            return true;
        return false;
    }

    static inline bool SlideEntryPropertyMatches(const TSlideEntry &inEntry,
                                                 Qt3DSDMPropertyHandle inProperty)
    {
        using namespace std;
        return inProperty.GetHandleValue() == get<1>(inEntry);
    }

    static inline bool SlideEntryInstancePropertyMatches(const TSlideEntry &inEntry,
                                                         const TInstanceHandleList &inInstances,
                                                         const TPropertyHandleList &inProperties)
    {
        using namespace std;
        return exists(inInstances, std::bind(equal_to<int>(), get<0>(inEntry),
                                             std::placeholders::_1))
            && exists(inProperties, std::bind(equal_to<int>(), get<1>(inEntry),
                                              std::placeholders::_1));
    }
};

typedef std::shared_ptr<CSimpleSlideCore> TSimpleSlideCorePtr;
}

#endif