summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Studio/Palettes/Timeline/Bindings/Qt3DSDMTimelineItemProperty.cpp
blob: a7fc4c53fa2edf055ff7757be8468bb8c7bbb660 (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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/****************************************************************************
**
** 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$
**
****************************************************************************/

//==============================================================================
//	Prefix
//==============================================================================
#include "stdafx.h"

//==============================================================================
//	Includes
//==============================================================================
#include "Qt3DSDMTimelineItemProperty.h"
#include "PropertyRow.h"
#include "TimelineTranslationManager.h"
#include "ITimelineItemBinding.h"
#include "PropertyTimebarRow.h"
#include "Qt3DSDMTimelineItemBinding.h"
#include "Qt3DSDMTimelineKeyframe.h"
#include "KeyframesManager.h"
#include "CmdDataModelChangeKeyframe.h"
#include "CmdDataModelRemoveKeyframe.h"
#include "StudioApp.h"
#include "Core.h"

// Link to data model
#include "TimeEditDlg.h"
#include "ClientDataModelBridge.h"
#include "Qt3DSDMSlides.h"
#include "Qt3DSDMStudioSystem.h"
#include "Qt3DSDMAnimation.h"
#include "Qt3DSDMMetaData.h"
#include "Qt3DSDMPropertyDefinition.h"
#include "Qt3DSDMDataCore.h"
#include "StudioFullSystem.h"
using namespace qt3dsdm;

bool SortKeyframeByTime(const Qt3DSDMTimelineKeyframe *inLHS, const Qt3DSDMTimelineKeyframe *inRHS)
{
    return inLHS->GetTime() < inRHS->GetTime();
}

// DataModel stores it from 0..1, UI expects 0..255
inline float DataModelToColor(float inValue)
{
    return inValue * 255;
}

Qt3DSDMTimelineItemProperty::Qt3DSDMTimelineItemProperty(CTimelineTranslationManager *inTransMgr,
                                                       Qt3DSDMPropertyHandle inPropertyHandle,
                                                       Qt3DSDMInstanceHandle inInstance)
    : m_Row(nullptr)
    , m_InstanceHandle(inInstance)
    , m_PropertyHandle(inPropertyHandle)
    , m_TransMgr(inTransMgr)
    , m_SetKeyframeValueCommand(nullptr)
{
    // Cache all the animation handles because we need them for any keyframes manipulation.
    // the assumption is that all associated handles are created all at once (i.e. we do not need to
    // add or delete from this list )
    CreateKeyframes();
    InitializeCachedVariables(inInstance);
    m_Signals.push_back(
        m_TransMgr->GetStudioSystem()->GetFullSystem()->GetSignalProvider()->ConnectPropertyLinked(
            std::bind(&Qt3DSDMTimelineItemProperty::OnPropertyLinkStatusChanged, this,
                      std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)));

    m_Signals.push_back(
        m_TransMgr->GetStudioSystem()
            ->GetFullSystem()
            ->GetSignalProvider()
            ->ConnectPropertyUnlinked(std::bind(
                &Qt3DSDMTimelineItemProperty::OnPropertyLinkStatusChanged, this,
                std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)));
}

Qt3DSDMTimelineItemProperty::~Qt3DSDMTimelineItemProperty()
{
    ReleaseKeyframes();
    Release();
}

void Qt3DSDMTimelineItemProperty::CreateKeyframes()
{
    // Cache all the animation handles because we need them for any keyframes manipulation.
    // the assumption is that all associated handles are created all at once (i.e. we do not need to
    // add or delete from this list )
    qt3dsdm::IPropertySystem *thePropertySystem = m_TransMgr->GetStudioSystem()->GetPropertySystem();
    DataModelDataType::Value theDataType = thePropertySystem->GetDataType(m_PropertyHandle);
    IStudioAnimationSystem *theAnimationSystem =
        m_TransMgr->GetStudioSystem()->GetAnimationSystem();
    std::tuple<bool, size_t> theArity = GetDatatypeAnimatableAndArity(theDataType);
    for (size_t i = 0; i < std::get<1>(theArity); ++i) {
        Qt3DSDMAnimationHandle theAnimationHandle =
            theAnimationSystem->GetControllingAnimation(m_InstanceHandle, m_PropertyHandle, i);
        if (theAnimationHandle.Valid())
            m_AnimationHandles.push_back(theAnimationHandle);
    }
    if (!m_AnimationHandles.empty()) { // update wrappers for keyframes
        IAnimationCore *theAnimationCore = m_TransMgr->GetStudioSystem()->GetAnimationCore();
        TKeyframeHandleList theKeyframes;
        // all channels have keyframes at the same time
        theAnimationCore->GetKeyframes(m_AnimationHandles[0], theKeyframes);
        for (size_t i = 0; i < theKeyframes.size(); ++i)
            CreateKeyframeIfNonExistent(theKeyframes[i], m_AnimationHandles[0]);
    }
}

void Qt3DSDMTimelineItemProperty::ReleaseKeyframes()
{
    // clear any selection from m_TransMgr
    TKeyframeList::iterator theIter = m_Keyframes.begin();
    for (; theIter != m_Keyframes.end(); ++theIter) {
        m_TransMgr->GetKeyframesManager()->SetKeyframeSelected(*theIter, false);

        SAFE_DELETE(*theIter);
    }
    m_Keyframes.clear();
    m_AnimationHandles.clear();
}

// Type doesn't change and due to the logic required to figure this out, cache it.
void Qt3DSDMTimelineItemProperty::InitializeCachedVariables(qt3dsdm::Qt3DSDMInstanceHandle inInstance)
{
    using namespace Q3DStudio;
    qt3dsdm::IPropertySystem *thePropertySystem = m_TransMgr->GetStudioSystem()->GetPropertySystem();

    m_Type.first = thePropertySystem->GetDataType(m_PropertyHandle);
    m_Type.second = thePropertySystem->GetAdditionalMetaDataType(inInstance, m_PropertyHandle);

    // Name doesn't change either.
    TCharStr theFormalName = thePropertySystem->GetFormalName(inInstance, m_PropertyHandle);

    if (theFormalName.empty()) // fallback on property name
        theFormalName = thePropertySystem->GetName(m_PropertyHandle);
    m_Name = theFormalName.c_str();
}

Q3DStudio::CString Qt3DSDMTimelineItemProperty::GetName() const
{
    return m_Name;
}

// Helper function to retrieve the parent binding class.
inline ITimelineItemBinding *GetParentBinding(CPropertyRow *inRow)
{
    ITimelineItemBinding *theParentBinding = nullptr;
    if (inRow) {
        CBaseStateRow *theParentRow = inRow->GetParentRow();
        if (theParentRow) {
            theParentBinding = theParentRow->GetTimelineItemBinding();
            ASSERT(theParentBinding); // TimelineItemBinding should be set properly during
                                      // CBaseStateRow::Initialize
        }
    }
    return theParentBinding;
}

bool Qt3DSDMTimelineItemProperty::IsMaster() const
{
    if (m_Row) {
        if (Qt3DSDMTimelineItemBinding *theParentBinding =
                dynamic_cast<Qt3DSDMTimelineItemBinding *>(GetParentBinding(m_Row)))
            return m_TransMgr->GetDoc()->GetDocumentReader().IsPropertyLinked(
                theParentBinding->GetInstanceHandle(), m_PropertyHandle);
    }
    return false;
}

qt3dsdm::TDataTypePair Qt3DSDMTimelineItemProperty::GetType() const
{
    return m_Type;
}

void CompareAndSet(const Qt3DSDMTimelineKeyframe *inKeyframe, float &outRetValue, bool inGreaterThan)
{
    float theValue = (inGreaterThan) ? inKeyframe->GetMaxValue() : inKeyframe->GetMinValue();
    if ((inGreaterThan && theValue > outRetValue) || (!inGreaterThan && theValue < outRetValue))
        outRetValue = theValue;
}

// return the max value of the current set of keyframes
float Qt3DSDMTimelineItemProperty::GetMaximumValue() const
{
    float theRetVal = FLT_MIN;
    do_all(m_Keyframes, std::bind(CompareAndSet, std::placeholders::_1, std::ref(theRetVal), true));
    if (m_Type.first == DataModelDataType::Float3 && m_Type.second == AdditionalMetaDataType::Color)
        theRetVal = DataModelToColor(theRetVal);
    return theRetVal;
}

// return the min value of the current set of keyframes
float Qt3DSDMTimelineItemProperty::GetMinimumValue() const
{
    float theRetVal = FLT_MAX;
    do_all(m_Keyframes, std::bind(CompareAndSet, std::placeholders::_1, std::ref(theRetVal), false));
    if (m_Type.first == DataModelDataType::Float3 && m_Type.second == AdditionalMetaDataType::Color)
        theRetVal = DataModelToColor(theRetVal);
    return theRetVal;
}

void Qt3DSDMTimelineItemProperty::Bind(CPropertyRow *inRow)
{
    ASSERT(!m_Row);

    m_Row = inRow;
}

void Qt3DSDMTimelineItemProperty::Release()
{
    m_Row = nullptr;
}

// Ensures the object that owns this property is selected.
void Qt3DSDMTimelineItemProperty::SetSelected()
{
    if (m_Row) {
        ITimelineItemBinding *theParentBinding = GetParentBinding(m_Row);
        if (theParentBinding)
            theParentBinding->SetSelected(false);
    }
}

void Qt3DSDMTimelineItemProperty::ClearKeySelection()
{
    m_TransMgr->ClearKeyframeSelection();
}

void Qt3DSDMTimelineItemProperty::DeleteAllKeys()
{
    if (m_Keyframes.empty())
        return;

    using namespace Q3DStudio;

    ScopedDocumentEditor editor(*m_TransMgr->GetDoc(), L"Delete All Keyframes", __FILE__, __LINE__);
    for (size_t idx = 0, end = m_AnimationHandles.size(); idx < end; ++idx)
        editor->DeleteAllKeyframes(m_AnimationHandles[idx]);
}

ITimelineKeyframesManager *Qt3DSDMTimelineItemProperty::GetKeyframesManager() const
{
    return m_TransMgr->GetKeyframesManager();
}

IKeyframe *Qt3DSDMTimelineItemProperty::GetKeyframeByTime(long inTime) const
{
    std::vector<long> theTest;
    TKeyframeList::const_iterator theIter = m_Keyframes.begin();
    for (; theIter != m_Keyframes.end(); ++theIter) {
        if ((*theIter)->GetTime() == inTime)
            return (*theIter);

        theTest.push_back((*theIter)->GetTime());
    }
    // if key had been deleted, this returns nullptr
    return nullptr;
}

IKeyframe *Qt3DSDMTimelineItemProperty::GetKeyframeByIndex(long inIndex) const
{
    if (inIndex >= 0 && inIndex < (long)m_Keyframes.size())
        return m_Keyframes[inIndex];

    ASSERT(0); // should not happen
    return nullptr;
}

long Qt3DSDMTimelineItemProperty::GetKeyframeCount() const
{
    // this list is updated in constructor and when keyframes are added or deleted.
    return (long)m_Keyframes.size();
}

long Qt3DSDMTimelineItemProperty::GetChannelCount() const
{
    return (long)m_AnimationHandles.size();
}

float Qt3DSDMTimelineItemProperty::GetChannelValueAtTime(long inChannelIndex, long inTime)
{
    // if no keyframes, get current property value.
    if (m_Keyframes.empty()) {
        Qt3DSDMTimelineItemBinding *theParentBinding =
            dynamic_cast<Qt3DSDMTimelineItemBinding *>(GetParentBinding(m_Row));
        if (theParentBinding) {

            SValue theValue;
            qt3dsdm::IPropertySystem *thePropertySystem =
                m_TransMgr->GetStudioSystem()->GetPropertySystem();
            thePropertySystem->GetInstancePropertyValue(theParentBinding->GetInstanceHandle(),
                                                        m_PropertyHandle, theValue);
            switch (m_Type.first) {
            case DataModelDataType::Float3: {
                if (m_Type.second == AdditionalMetaDataType::Color) {
                    SFloat3 theFloat3 = qt3dsdm::get<SFloat3>(theValue);
                    if (inChannelIndex >= 0 && inChannelIndex < 3)
                        return DataModelToColor(theFloat3[inChannelIndex]);
                } else {
                    SFloat3 theFloat3 = qt3dsdm::get<SFloat3>(theValue);
                    if (inChannelIndex >= 0 && inChannelIndex < 3)
                        return theFloat3[inChannelIndex];
                }
                break;
            }
            case DataModelDataType::Float2: {
                SFloat2 theFloat2 = qt3dsdm::get<SFloat2>(theValue);
                if (inChannelIndex >= 0 && inChannelIndex < 2)
                    return theFloat2[inChannelIndex];
                break;
            }
            case DataModelDataType::Float:
                return qt3dsdm::get<float>(theValue);
                break;
            default: // TODO: handle other types
                break;
            }
        }
    }
    IAnimationCore *theAnimationCore = m_TransMgr->GetStudioSystem()->GetAnimationCore();
    if (!m_AnimationHandles.empty() && inChannelIndex >= 0
        && inChannelIndex < (long)m_AnimationHandles.size()) {
        float theValue = theAnimationCore->EvaluateAnimation(
            m_AnimationHandles[inChannelIndex], Qt3DSDMTimelineKeyframe::GetTimeInSecs(inTime));
        if (m_Type.first == DataModelDataType::Float3
            && m_Type.second == AdditionalMetaDataType::Color)
            theValue = DataModelToColor(theValue);

        return theValue;
    }
    return 0.f;
}

void Qt3DSDMTimelineItemProperty::SetChannelValueAtTime(long inChannelIndex, long inTime,
                                                       float inValue)
{
    Qt3DSDMTimelineKeyframe *theKeyframeWrapper =
        dynamic_cast<Qt3DSDMTimelineKeyframe *>(GetKeyframeByTime(inTime));
    if (theKeyframeWrapper) {
        Qt3DSDMTimelineKeyframe::TKeyframeHandleList theKeyframes;
        theKeyframeWrapper->GetKeyframeHandles(theKeyframes);
        if (!theKeyframes.empty() && inChannelIndex < (long)theKeyframes.size()) {
            inValue /= 255;
            if (!m_SetKeyframeValueCommand)
                m_SetKeyframeValueCommand = new CCmdDataModelSetKeyframeValue(
                    g_StudioApp.GetCore()->GetDoc(), theKeyframes[inChannelIndex], inValue);
            m_SetKeyframeValueCommand->Update(inValue);
        }
    }
}

long Qt3DSDMTimelineItemProperty::OffsetSelectedKeyframes(long inOffset)
{
    long theRetVal = m_TransMgr->GetKeyframesManager()->OffsetSelectedKeyframes(inOffset);
    if (m_Row) // UI update, since the data model sends no event while the change isn't commited.
    {
        m_Row->Refresh();
    }
    return theRetVal;
}

void Qt3DSDMTimelineItemProperty::CommitChangedKeyframes()
{
    if (m_SetKeyframeValueCommand) { // if this is moving a keyframe value
        m_SetKeyframeValueCommand->Finalize(m_SetKeyframeValueCommand->GetValue());
        g_StudioApp.GetCore()->ExecuteCommand(m_SetKeyframeValueCommand, false);
        m_SetKeyframeValueCommand = nullptr;
    } else // otherwise its changing keyframe times
        m_TransMgr->GetKeyframesManager()->CommitChangedKeyframes();
}

void Qt3DSDMTimelineItemProperty::OnEditKeyframeTime(long inCurrentTime, long inObjectAssociation)
{
    (void)inObjectAssociation;
    CTimeEditDlg theTimeEditDlg;
    theTimeEditDlg.SetKeyframesManager(m_TransMgr->GetKeyframesManager());
    theTimeEditDlg.ShowDialog(inCurrentTime, 0, g_StudioApp.GetCore()->GetDoc(), ASSETKEYFRAME);
}

void Qt3DSDMTimelineItemProperty::SelectKeyframes(bool inSelected, long inTime /*= -1 */)
{
    Qt3DSDMTimelineItemBinding *theParent =
        dynamic_cast<Qt3DSDMTimelineItemBinding *>(GetParentBinding(m_Row));
    DoSelectKeyframes(inSelected, inTime, false, theParent);
}

CPropertyRow *Qt3DSDMTimelineItemProperty::GetRow()
{
    return m_Row;
}

bool Qt3DSDMTimelineItemProperty::IsDynamicAnimation()
{
    return m_Keyframes.size() > 0 && m_Keyframes[0]->IsDynamic();
}

//=============================================================================
/**
 * For updating the UI when keyframes are added/updated/deleted.
 */
bool Qt3DSDMTimelineItemProperty::RefreshKeyframe(qt3dsdm::Qt3DSDMKeyframeHandle inKeyframe,
                                                 ETimelineKeyframeTransaction inTransaction)
{
    bool theHandled = false;
    switch (inTransaction) {
    case ETimelineKeyframeTransaction_Delete: {
        TKeyframeList::iterator theIter = m_Keyframes.begin();
        for (; theIter != m_Keyframes.end(); ++theIter) {
            Qt3DSDMTimelineKeyframe *theKeyframe = *theIter;
            if (theKeyframe->HasKeyframeHandle(inKeyframe)) { // clear selection
                m_TransMgr->GetKeyframesManager()->SetKeyframeSelected(theKeyframe, false);
                m_Keyframes.erase(theIter);

                theHandled = true;
                break;
            }
        }
    } break;
    case ETimelineKeyframeTransaction_Add: {
        ASSERT(!m_AnimationHandles.empty());
        IAnimationCore *theAnimationCore = m_TransMgr->GetStudioSystem()->GetAnimationCore();
        Qt3DSDMAnimationHandle theAnimationHandle =
            theAnimationCore->GetAnimationForKeyframe(inKeyframe);
        // only create for the first animation handle.
        if (theAnimationHandle == m_AnimationHandles[0]) { // for undo/redo, the keyframes can be
                                                           // added in reverse, hence the need to
                                                           // sort
            if (CreateKeyframeIfNonExistent(inKeyframe, theAnimationHandle))
                std::stable_sort(m_Keyframes.begin(), m_Keyframes.end(), SortKeyframeByTime);
            theHandled = true;
        }
    } break;
    case ETimelineKeyframeTransaction_Update:
    case ETimelineKeyframeTransaction_DynamicChanged:
        theHandled = true;
        break;
    default:
        return false;
    }
    if (theHandled && m_Row)
        m_Row->Refresh();

    return theHandled;
}

IKeyframe *Qt3DSDMTimelineItemProperty::GetKeyframeByHandle(qt3dsdm::Qt3DSDMKeyframeHandle inKeyframe)
{
    TKeyframeList::iterator theIter = m_Keyframes.begin();
    for (; theIter != m_Keyframes.end(); ++theIter) {
        Qt3DSDMTimelineKeyframe *theKeyframe = *theIter;
        if (theKeyframe->HasKeyframeHandle(inKeyframe))
            return *theIter;
    }
    return nullptr;
}

// This is either triggered from this property's keyframe selection OR from a parent's keyframe
// selection.
void Qt3DSDMTimelineItemProperty::DoSelectKeyframes(bool inSelected, long inTime,
                                                   bool inParentTriggered,
                                                   Qt3DSDMTimelineItemBinding *inParent)
{
    // this is what it used to do before the refactor. selecting a keyframe always selects the
    // asset.
    if (inSelected)
        SetSelected();

    if (!inParent)
        return;

    if (inTime == -1) // all keyframes
    {
        TKeyframeList::iterator theIter = m_Keyframes.begin();
        for (; theIter != m_Keyframes.end(); ++theIter) {
            (*theIter)->SetSelected(inSelected);
            m_TransMgr->GetKeyframesManager()->SetKeyframeSelected(*theIter, inSelected, inParent);
        }
    } else {
        Qt3DSDMTimelineKeyframe *theKeyframe =
            dynamic_cast<Qt3DSDMTimelineKeyframe *>(GetKeyframeByTime(inTime));
        if (theKeyframe) {
            theKeyframe->SetSelected(inSelected);
            m_TransMgr->GetKeyframesManager()->SetKeyframeSelected(theKeyframe, inSelected,
                                                                   inParent);
        }
    }
    // Requires UI to be updated explicitly
    if (inParentTriggered && m_Row)
        m_Row->GetTimebar()->SelectKeysByTime(inTime, inSelected);

    // Support existing feature, selection by mouse-drag a rect, when all property keyframes are
    // selected, the asset keyframe is automatically selected as well.
    // and the mouse drags 'outside' a keyframe and de-selecting it, the asset keyframe has to be
    // deselected as well.
    if (!inParentTriggered && inTime != -1)
        inParent->OnPropertySelection(inTime);
}

//=============================================================================
/**
 * Create a wrapper for this keyframe if doesn't exists.
 * @return true if created, false if already exists.
 */
bool Qt3DSDMTimelineItemProperty::CreateKeyframeIfNonExistent(
    qt3dsdm::Qt3DSDMKeyframeHandle inKeyframeHandle, Qt3DSDMAnimationHandle inOwningAnimation)
{
    TKeyframeList::iterator theIter = m_Keyframes.begin();
    for (; theIter != m_Keyframes.end(); ++theIter) {
        Qt3DSDMTimelineKeyframe *theKeyframe = *theIter;
        if (theKeyframe->HasKeyframeHandle(inKeyframeHandle))
            return false;
    }
    // check for multiple channels => only create 1 Qt3DSDMTimelineKeyframe
    Qt3DSDMTimelineKeyframe *theNewKeyframe =
        new Qt3DSDMTimelineKeyframe(g_StudioApp.GetCore()->GetDoc());
    theNewKeyframe->AddKeyframeHandle(inKeyframeHandle);
    if (m_AnimationHandles.size()
        > 1) { // assert assumption that is only called for the first handle
        ASSERT(m_AnimationHandles[0] == inOwningAnimation);
        IAnimationCore *theAnimationCore = m_TransMgr->GetStudioSystem()->GetAnimationCore();
        float theKeyframeTime = KeyframeTime(theAnimationCore->GetKeyframeData(inKeyframeHandle));
        for (size_t i = 1; i < m_AnimationHandles.size(); ++i) {
            TKeyframeHandleList theKeyframes;
            theAnimationCore->GetKeyframes(m_AnimationHandles[i], theKeyframes);
            // the data model ensures that there is only 1 keyframe created for a given time
            for (size_t theKeyIndex = 0; theKeyIndex < theKeyframes.size(); ++theKeyIndex) {
                float theValue =
                    KeyframeTime(theAnimationCore->GetKeyframeData(theKeyframes[theKeyIndex]));
                if (theValue == theKeyframeTime) {
                    theNewKeyframe->AddKeyframeHandle(theKeyframes[theKeyIndex]);
                    break;
                }
            }
        }
    }
    m_Keyframes.push_back(theNewKeyframe);
    return true;
}

void Qt3DSDMTimelineItemProperty::OnPropertyLinkStatusChanged(qt3dsdm::Qt3DSDMSlideHandle inSlide,
                                                             qt3dsdm::Qt3DSDMInstanceHandle inInstance,
                                                             qt3dsdm::Qt3DSDMPropertyHandle inProperty)
{
    if (inInstance == m_InstanceHandle && inProperty == m_PropertyHandle) {
        // Re-bind to keyframes because the ones we should be pointing to will have changed.
        ReleaseKeyframes();
        CreateKeyframes();
    }
}

void Qt3DSDMTimelineItemProperty::RefreshKeyFrames(void)
{
    std::stable_sort(m_Keyframes.begin(), m_Keyframes.end(), SortKeyframeByTime);
}