forked from mltframework/shotcut
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyframesmodel.cpp
More file actions
445 lines (415 loc) · 17.1 KB
/
keyframesmodel.cpp
File metadata and controls
445 lines (415 loc) · 17.1 KB
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
/*
* Copyright (c) 2018 Meltytech, LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "keyframesmodel.h"
#include "qmltypes/qmlmetadata.h"
#include "qmltypes/qmlfilter.h"
#include "mltcontroller.h"
#include <Logger.h>
static const quintptr NO_PARENT_ID = quintptr(-1);
KeyframesModel::KeyframesModel(QObject* parent)
: QAbstractItemModel(parent)
{
}
KeyframesModel::~KeyframesModel()
{
}
int KeyframesModel::rowCount(const QModelIndex& parent) const
{
if (parent.isValid()) {
// keyframes
if (parent.row() < m_keyframeCounts.count())
return m_keyframeCounts[parent.row()];
return 0;
}
// parameters
return m_propertyNames.count();
}
int KeyframesModel::columnCount(const QModelIndex& parent) const
{
Q_UNUSED(parent)
return 1;
}
QVariant KeyframesModel::data(const QModelIndex& index, int role) const
{
if (!m_metadata || !index.isValid())
return QVariant();
if (index.parent().isValid()) {
// LOG_DEBUG() << "keyframe" << index.internalId() << index.row() << role;
// keyframes
if (m_filter && index.internalId() < quintptr(m_propertyNames.count())) {
QString name = m_propertyNames[index.internalId()];
Mlt::Animation animation = m_filter->getAnimation(name);
if (animation.is_valid()) {
int position = const_cast<Mlt::Animation&>(animation).key_get_frame(index.row());
if (position >= 0) {
switch (role) {
case Qt::DisplayRole:
case NameRole: {
QString type = tr("Discrete");
switch (const_cast<Mlt::Animation&>(animation).key_get_type(index.row())) {
case mlt_keyframe_linear:
type = tr("Linear");
break;
case mlt_keyframe_smooth:
type = tr("Smooth");
break;
default:
break;
}
return QString("%1 - %2").arg(m_filter->timeFromFrames(position)).arg(type);
}
case FrameNumberRole:
return position;
case KeyframeTypeRole:
return const_cast<Mlt::Animation&>(animation).key_get_type(index.row());
case NumericValueRole:
return m_filter->getDouble(name, position);
case MinimumFrameRole: {
int result = (index.row() > 0 && position > 0) ? (animation.previous_key(position - 1) + 1) : 0;
// LOG_DEBUG() << "keyframeIndex" << index.row() << "minimumFrame" << result;
return result;
}
case MaximumFrameRole: {
int minimum = animation.previous_key(qMax(0, position - 1)) + 1;
int result = animation.next_key(position + 1) - 1;
result = (result < minimum) ? m_filter->duration() : result;
// LOG_DEBUG() << "keyframeIndex" << index.row() << "maximumFrame" << result;
return result - 1;
}
default:
break;
}
}
}
}
} else if (index.row() < m_metadata->keyframes()->parameterCount()) {
// LOG_DEBUG() << "parameter" << index.row() << role;
// parameters
switch (role) {
case Qt::DisplayRole:
case NameRole:
return m_metadata->keyframes()->parameter(m_metadataIndex[index.row()])->name();
case PropertyNameRole:
return m_metadata->keyframes()->parameter(m_metadataIndex[index.row()])->property();
default:
break;
}
}
return QVariant();
}
QModelIndex KeyframesModel::index(int row, int column, const QModelIndex& parent) const
{
if (column > 0)
return QModelIndex();
QModelIndex result;
if (parent.isValid()) {
// keyframes
result = createIndex(row, column, parent.row());
} else if (row < m_propertyNames.count()) {
result = createIndex(row, column, NO_PARENT_ID);
}
return result;
}
QModelIndex KeyframesModel::parent(const QModelIndex& index) const
{
if (!index.isValid() || index.internalId() == NO_PARENT_ID)
return QModelIndex();
else
return createIndex(index.internalId(), 0, NO_PARENT_ID);
}
QHash<int, QByteArray> KeyframesModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[NameRole] = "name";
roles[PropertyNameRole] = "property";
roles[FrameNumberRole] = "frame";
roles[KeyframeTypeRole] = "interpolation";
roles[NumericValueRole] = "value";
roles[MinimumFrameRole] = "minimumFrame";
roles[MaximumFrameRole] = "maximumFrame";
return roles;
}
void KeyframesModel::load(QmlFilter* filter, QmlMetadata* meta)
{
beginResetModel();
m_propertyNames.clear();
m_keyframeCounts.clear();
m_metadataIndex.clear();
m_filter = filter;
m_metadata = meta;
if (m_filter && m_metadata)
for (int i = 0; i < m_metadata->keyframes()->parameterCount(); i++) {
if (!m_metadata->keyframes()->parameter(i)->isSimple() || (m_filter->animateIn() <= 0 && m_filter->animateOut() <= 0)) {
if (m_filter->keyframeCount(m_metadata->keyframes()->parameter(i)->property()) > 0) {
m_propertyNames << m_metadata->keyframes()->parameter(i)->property();
m_keyframeCounts << keyframeCount(m_propertyNames.count() - 1);
m_metadataIndex << i;
// LOG_DEBUG() << m_propertyNames.last() << m_filter->get(m_propertyNames.last()) << keyframeCount(i);
}
}
}
endResetModel();
emit loaded();
}
bool KeyframesModel::remove(int parameterIndex, int keyframeIndex)
{
bool error = true;
if (m_filter && parameterIndex < m_propertyNames.count()) {
QString name = m_propertyNames[parameterIndex];
Mlt::Animation animation = m_filter->getAnimation(name);
if (animation.is_valid()) {
error = animation.remove(animation.key_get_frame(keyframeIndex));
if (!error) {
animation.interpolate();
beginRemoveRows(index(parameterIndex), keyframeIndex, keyframeIndex);
m_keyframeCounts[parameterIndex] -= 1;
endRemoveRows();
QModelIndex modelIndex;
if (keyframeIndex > 0) {
modelIndex = index(keyframeIndex - 1, 0, index(parameterIndex));
emit dataChanged(modelIndex, modelIndex, QVector<int>() << MaximumFrameRole);
}
if (keyframeIndex < keyframeCount(parameterIndex)) {
modelIndex = index(keyframeIndex, 0, index(parameterIndex));
emit dataChanged(modelIndex, modelIndex, QVector<int>() << MinimumFrameRole);
}
}
}
}
return error;
}
int KeyframesModel::previousKeyframePosition(int parameterIndex, int currentPosition)
{
int result = -1;
if (m_filter && parameterIndex < m_propertyNames.count()) {
QString name = m_propertyNames[parameterIndex];
Mlt::Animation animation = m_filter->getAnimation(name);
if (animation.is_valid()) {
currentPosition -= m_filter->in();
result = animation.previous_key(animation.is_key(currentPosition)? currentPosition - 1: currentPosition);
result += m_filter->in();
}
}
return result;
}
int KeyframesModel::nextKeyframePosition(int parameterIndex, int currentPosition)
{
int result = -1;
if (m_filter && parameterIndex < m_propertyNames.count()) {
QString name = m_propertyNames[parameterIndex];
Mlt::Animation animation = m_filter->getAnimation(name);
if (animation.is_valid()) {
currentPosition -= m_filter->in();
result = animation.next_key(animation.is_key(currentPosition)? currentPosition + 1: currentPosition);
result += m_filter->in();
}
}
return result;
}
int KeyframesModel::keyframeIndex(int parameterIndex, int currentPosition)
{
int result = -1;
if (m_filter && parameterIndex < m_propertyNames.count()) {
QString name = m_propertyNames[parameterIndex];
Mlt::Animation animation = m_filter->getAnimation(name);
if (animation.is_valid()) {
for (int i = 0; i < animation.key_count() && result == -1; i++) {
int frame = animation.key_get_frame(i);
if (frame == currentPosition)
result = i;
else if (frame > currentPosition)
break;
}
}
}
return result;
}
int KeyframesModel::parameterIndex(const QString& propertyName) const
{
return m_propertyNames.indexOf(propertyName);
}
bool KeyframesModel::setInterpolation(int parameterIndex, int keyframeIndex, InterpolationType type)
{
bool error = true;
if (m_filter && parameterIndex < m_propertyNames.count()) {
QString name = m_propertyNames[parameterIndex];
Mlt::Animation animation = m_filter->getAnimation(name);
if (animation.is_valid()) {
if (!animation.key_set_type(keyframeIndex, mlt_keyframe_type(type))) {
// LOG_DEBUG() << "keyframe index" << keyframeIndex << "keyframe type" << type;
QModelIndex modelIndex = index(keyframeIndex, 0, index(parameterIndex));
emit dataChanged(modelIndex, modelIndex, QVector<int>() << KeyframeTypeRole << NameRole);
error = false;
}
}
}
if (error)
LOG_ERROR() << "failed to set keyframe" << "at parameter index" << parameterIndex << "keyframeIndex" << keyframeIndex << "to type" << type;
return error;
}
bool KeyframesModel::setPosition(int parameterIndex, int keyframeIndex, int position)
{
bool error = true;
if (m_filter && parameterIndex < m_propertyNames.count()) {
QString name = m_propertyNames[parameterIndex];
Mlt::Animation animation = m_filter->getAnimation(name);
if (animation.is_valid()) {
if (!animation.key_set_frame(keyframeIndex, position)) {
// LOG_DEBUG() << "keyframe index" << keyframeIndex << "position" << position;
QModelIndex modelIndex = index(keyframeIndex, 0, index(parameterIndex));
emit dataChanged(modelIndex, modelIndex, QVector<int>() << FrameNumberRole << NameRole);
updateNeighborsMinMax(parameterIndex, keyframeIndex);
error = false;
emit m_filter->changed();
}
}
}
if (error)
LOG_ERROR() << "failed to set keyframe" << "at parameter index" << parameterIndex << "keyframeIndex" << keyframeIndex << "to position" << position;
return error;
}
void KeyframesModel::addKeyframe(int parameterIndex, double value, int position, KeyframesModel::InterpolationType type)
{
if (m_filter && parameterIndex < m_propertyNames.count()) {
QString name = m_propertyNames[parameterIndex];
m_filter->set(name, value, position, mlt_keyframe_type(type));
}
}
void KeyframesModel::setKeyframe(int parameterIndex, double value, int position, KeyframesModel::InterpolationType type)
{
if (m_filter && parameterIndex < m_propertyNames.count()) {
QString name = m_propertyNames[parameterIndex];
m_filter->filter().anim_set(name.toUtf8().constData(), value, position, m_filter->duration(), mlt_keyframe_type(type));
emit m_filter->changed();
QModelIndex modelIndex = index(keyframeIndex(parameterIndex, position), 0, index(parameterIndex));
emit dataChanged(modelIndex, modelIndex, QVector<int>() << NumericValueRole << NameRole);
updateNeighborsMinMax(parameterIndex, modelIndex.row());
}
}
void KeyframesModel::reload()
{
beginResetModel();
m_propertyNames.clear();
m_keyframeCounts.clear();
m_metadataIndex.clear();
if (m_filter)
for (int i = 0; i < m_metadata->keyframes()->parameterCount(); i++) {
if (!m_metadata->keyframes()->parameter(i)->isSimple() || (m_filter->animateIn() <= 0 && m_filter->animateOut() <= 0)) {
if (m_filter->keyframeCount(m_metadata->keyframes()->parameter(i)->property()) > 0) {
m_propertyNames << m_metadata->keyframes()->parameter(i)->property();
m_keyframeCounts << keyframeCount(m_propertyNames.count() - 1);
m_metadataIndex << i;
}
}
}
endResetModel();
}
void KeyframesModel::onFilterChanged(const QString& property)
{
// LOG_DEBUG() << property;
int i = m_propertyNames.indexOf(property);
if (i > -1) {
int count = m_keyframeCounts[i];
m_keyframeCounts[i] = keyframeCount(i);
if (count > 0) {
beginRemoveRows(index(i), 0, count - 1);
endRemoveRows();
}
if (m_keyframeCounts[i] > 0) {
// LOG_DEBUG() << property << m_filter->get(property) << m_keyframeCounts[i];
beginInsertRows(index(i), 0, m_keyframeCounts[i] - 1);
endInsertRows();
} else {
reload();
}
} else {
reload();
}
}
void KeyframesModel::onFilterInChanged(int delta)
{
for (int parameterIndex = 0; parameterIndex < m_propertyNames.count(); parameterIndex++) {
int count = m_keyframeCounts[parameterIndex];
if (count > 0) {
Mlt::Animation animation = m_filter->getAnimation(m_propertyNames[parameterIndex]);
if (animation.is_valid()) {
int filterDuration = m_filter->duration();
for (int keyframeIndex = 0; keyframeIndex < count;) {
int newFrame = animation.key_get_frame(keyframeIndex) - delta;
if (newFrame < 0 || newFrame >= filterDuration || animation.is_key(newFrame)) {
beginRemoveRows(index(parameterIndex), keyframeIndex, keyframeIndex);
animation.remove(animation.key_get_frame(keyframeIndex));
animation.interpolate();
m_keyframeCounts[parameterIndex] -= 1;
endRemoveRows();
--count;
} else {
animation.key_set_frame(keyframeIndex, newFrame);
++keyframeIndex;
}
}
QModelIndex parentIndex = index(parameterIndex);
emit dataChanged(index(0, 0, parentIndex), index(count - 1, 0, parentIndex), QVector<int>() << FrameNumberRole);
}
}
}
}
void KeyframesModel::onFilterOutChanged(int delta)
{
Q_UNUSED(delta)
for (int parameterIndex = 0; parameterIndex < m_propertyNames.count(); parameterIndex++) {
int count = m_keyframeCounts[parameterIndex];
if (count > 0) {
Mlt::Animation animation = m_filter->getAnimation(m_propertyNames[parameterIndex]);
if (animation.is_valid()) {
int filterDuration = m_filter->duration();
for (int keyframeIndex = 0; keyframeIndex < count;) {
int frame = animation.key_get_frame(keyframeIndex);
if (frame < 0 || frame >= filterDuration) {
beginRemoveRows(index(parameterIndex), keyframeIndex, keyframeIndex);
animation.remove(animation.key_get_frame(keyframeIndex));
animation.interpolate();
m_keyframeCounts[parameterIndex] -= 1;
endRemoveRows();
--count;
} else {
++keyframeIndex;
}
}
}
}
}
}
int KeyframesModel::keyframeCount(int index) const
{
if (index < m_propertyNames.count())
return qMax(const_cast<QmlFilter*>(m_filter)->keyframeCount(m_propertyNames[index]), 0);
else
return 0;
}
void KeyframesModel::updateNeighborsMinMax(int parameterIndex, int keyframeIndex)
{
QModelIndex modelIndex;
if (keyframeIndex > 0) {
modelIndex = index(keyframeIndex - 1, 0, index(parameterIndex));
emit dataChanged(modelIndex, modelIndex, QVector<int>() << MaximumFrameRole);
}
if (keyframeIndex < keyframeCount(parameterIndex) - 1) {
modelIndex = index(keyframeIndex + 1, 0, index(parameterIndex));
emit dataChanged(modelIndex, modelIndex, QVector<int>() << MinimumFrameRole);
}
}