Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/docks/keyframesdock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ KeyframesDock::KeyframesDock(QmlProducer *qmlProducer, QWidget *parent)
keyEaseInOutMenu->addAction(Actions["keyframesTypeEaseInOutBounAction"]);
m_keyTypeNextMenu->addMenu(keyEaseInOutMenu);
m_keyMenu->addMenu(m_keyTypeNextMenu);
m_keyMenu->addAction(Actions["keyframesMatchFirstAction"]);
m_keyMenu->addAction(Actions["keyframesRemoveAction"]);
Actions.loadFromMenu(m_keyMenu);

Expand Down Expand Up @@ -1076,6 +1077,12 @@ void KeyframesDock::setupActions()
});
Actions.add("keyframesRemoveAction", action);

action = new QAction(tr("Match First Keyframe Value"), this);
connect(action, &QAction::triggered, this, [this]() {
m_model.matchFirstValue(currentParameter());
});
Actions.add("keyframesMatchFirstAction", action);

action = new QAction(tr("Rebuild Audio Waveform"), this);
action->setEnabled(Settings.timelineShowWaveforms());
connect(action, &QAction::triggered, this, [&](bool checked) {
Expand Down Expand Up @@ -1319,6 +1326,9 @@ void KeyframesDock::onKeyframeRightClicked()
}
m_keyTypePrevMenu->setEnabled(!firstKey);
m_keyTypeNextMenu->setEnabled(!lastKey);
Actions["keyframesMatchFirstAction"]->setVisible(
lastKey && m_model.keyframeCount(currentParameter()) >= 2
&& m_model.data(m_model.index(currentParameter()), KeyframesModel::LoopRole).toBool());
m_keyMenu->popup(QCursor::pos());
}

Expand Down
139 changes: 128 additions & 11 deletions src/models/keyframesmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,22 @@

static const quintptr NO_PARENT_ID = quintptr(-1);

static void notifyPropertyChanged(Mlt::Properties &properties, const QString &name)
{
const QByteArray propertyName = name.toUtf8();
mlt_events_fire(properties.get_properties(),
"property-changed",
mlt_event_data_from_string(propertyName.constData()));
}

/*!
\qmltype KeyframesModel
\inqmlmodule org.shotcut.qml
\brief A two-level item model describing a filter's keyframeable parameters and their keyframes.

The read-only \c supportsLoop property indicates whether the build supports keyframe looping
(MLT 7.43.0 or newer).

\c KeyframesModel is available as the \c parameters context property in the Keyframes panel.
The top level contains one row per keyframeable parameter; each parameter row contains
one child row per keyframe.
Expand Down Expand Up @@ -78,6 +89,15 @@ KeyframesModel::KeyframesModel(QObject *parent)

KeyframesModel::~KeyframesModel() {}

bool KeyframesModel::supportsLoop() const
{
#if LIBMLT_VERSION_INT >= ((7 << 16) + (43 << 8))
return true;
#else
return false;
#endif
}

int KeyframesModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
Expand Down Expand Up @@ -219,11 +239,19 @@ QVariant KeyframesModel::data(const QModelIndex &index, int role) const
QString units = m_metadata->keyframes()
->parameter(m_metadataIndex[index.internalId()])
->units();
return QStringLiteral("%1 - %2\n%3%4")
.arg(QmlApplication::singleton().timeFromFrames(position))
.arg(type)
.arg(value)
.arg(units);
QString description = QStringLiteral("%1 - %2\n%3%4")
.arg(QmlApplication::singleton().timeFromFrames(
position))
.arg(type)
.arg(value)
.arg(units);
#if LIBMLT_VERSION_INT >= ((7 << 16) + (43 << 8))
if (animation.get_loop() && animation.key_count() >= 2
&& index.row() == animation.key_count() - 1)
description += tr(
"\nLoop endpoint (playback wraps to the first value)");
#endif
return description;
}
case FrameNumberRole:
return position;
Expand Down Expand Up @@ -338,6 +366,14 @@ QVariant KeyframesModel::data(const QModelIndex &index, int role) const
max = 0;
return max;
}
case LoopRole: {
#if LIBMLT_VERSION_INT >= ((7 << 16) + (43 << 8))
Mlt::Animation animation = m_filter->getAnimation(m_propertyNames[index.row()]);
return animation.is_valid() && animation.get_loop();
Comment on lines +369 to +372
#else
return false;
#endif
}
default:
break;
}
Expand Down Expand Up @@ -377,6 +413,7 @@ QHash<int, QByteArray> KeyframesModel::roleNames() const
roles[MaximumValueRole] = "maximum";
roles[LowestValueRole] = "lowest";
roles[HighestValueRole] = "highest";
roles[LoopRole] = "isLoop";
roles[FrameNumberRole] = "frame";
roles[KeyframeTypeRole] = "interpolation";
roles[PrevKeyframeTypeRole] = "prevInterpolation";
Expand Down Expand Up @@ -438,8 +475,7 @@ bool KeyframesModel::remove(int parameterIndex, int keyframeIndex)
m_filter->updateUndoCommand(key);
}
}
mlt_event_data eventData = mlt_event_data_from_string(name.toUtf8().constData());
mlt_events_fire(m_filter->service().get_properties(), "property-changed", eventData);
notifyPropertyChanged(m_filter->service(), name);
QModelIndex modelIndex;
if (keyframeIndex > 0) {
modelIndex = index(keyframeIndex - 1, 0, index(parameterIndex));
Expand Down Expand Up @@ -557,8 +593,7 @@ bool KeyframesModel::setInterpolation(int parameterIndex, int keyframeIndex, Int
m_filter->updateUndoCommand(key);
}
}
mlt_event_data eventData = mlt_event_data_from_string(name.toUtf8().constData());
mlt_events_fire(m_filter->service().get_properties(), "property-changed", eventData);
notifyPropertyChanged(m_filter->service(), name);
QModelIndex modelIndex = index(keyframeIndex, 0, index(parameterIndex));
emit dataChanged(modelIndex,
modelIndex,
Expand Down Expand Up @@ -635,8 +670,7 @@ void KeyframesModel::setKeyframePosition(int parameterIndex, int keyframeIndex,
m_filter->updateUndoCommand(key);
}
}
mlt_event_data eventData = mlt_event_data_from_string(name.toUtf8().constData());
mlt_events_fire(m_filter->service().get_properties(), "property-changed", eventData);
notifyPropertyChanged(m_filter->service(), name);
QModelIndex modelIndex = index(keyframeIndex, 0, index(parameterIndex));
emit dataChanged(modelIndex, modelIndex, QVector<int>() << FrameNumberRole << NameRole);
updateNeighborsMinMax(parameterIndex, keyframeIndex);
Expand Down Expand Up @@ -891,6 +925,51 @@ void KeyframesModel::setKeyframeValuePosition(int parameterIndex,
m_filter->endUndoCommand();
}

/*!
\qmlmethod void KeyframesModel::setLoop(int parameterIndex, bool loop)
\brief Sets whether the keyframes of parameter \a parameterIndex repeat.

The last keyframe is the interpolation target; its frame evaluates to the first value.
*/

void KeyframesModel::setLoop(int parameterIndex, bool loop)
{
#if LIBMLT_VERSION_INT >= ((7 << 16) + (43 << 8))
if (!m_filter || parameterIndex < 0 || parameterIndex >= m_propertyNames.count())
return;
QString name = m_propertyNames[parameterIndex];
auto parameter = m_metadata->keyframes()->parameter(m_metadataIndex[parameterIndex]);
QStringList names = QStringList() << name << parameter->gangedProperties()
<< parameter->gangedRectProperties();
m_filter->startUndoParameterCommand(loop ? tr("Enable keyframe loop")
: tr("Disable keyframe loop"));
for (auto &key : names) {
Mlt::Animation animation = m_filter->getAnimation(key);
if (animation.is_valid() && animation.get_loop() != loop) {
animation.set_loop(loop);
// Regenerate the property string so that the undo state includes the loop.
m_filter->service().get(key.toUtf8().constData());
m_filter->updateUndoCommand(key);
}
Comment thread
bmatherly marked this conversation as resolved.
Resolved
}
m_filter->endUndoCommand();
notifyPropertyChanged(m_filter->service(), name);
emit dataChanged(index(parameterIndex),
index(parameterIndex),
QVector<int>() << LoopRole << LowestValueRole << HighestValueRole);
if (m_keyframeCounts[parameterIndex] > 0)
emit dataChanged(index(0, 0, index(parameterIndex)),
index(m_keyframeCounts[parameterIndex] - 1, 0, index(parameterIndex)),
QVector<int>() << NumericValueRole << NameRole);
emit m_filter->changed(name);
emit m_filter->propertyChanged(name);
#else
Q_UNUSED(parameterIndex)
Q_UNUSED(loop)
LOG_ERROR() << "Keyframe looping requires MLT 7.43.0 or newer";
#endif
}

/*!
\qmlmethod bool KeyframesModel::isKeyframe(int parameterIndex, int position)
\brief Returns \c true if a keyframe exists at time \a position for \a parameterIndex.
Expand Down Expand Up @@ -1162,3 +1241,41 @@ QStringList KeyframesModel::gangedProperties(int parameterIndex) const
{
return m_metadata->keyframes()->parameter(m_metadataIndex[parameterIndex])->gangedProperties();
}

void KeyframesModel::matchFirstValue(int parameterIndex)
{
if (!m_filter || parameterIndex < 0 || parameterIndex >= m_propertyNames.count()) {
LOG_ERROR() << "Invalid parameter index" << parameterIndex;
return;
}
const QString name = m_propertyNames[parameterIndex];
auto parameter = m_metadata->keyframes()->parameter(m_metadataIndex[parameterIndex]);
QStringList names = QStringList() << name << parameter->gangedProperties()
<< parameter->gangedRectProperties();
names.removeDuplicates();
m_filter->startUndoParameterCommand(tr("Match First Keyframe Value"));
for (const auto &key : names) {
Mlt::Animation animation = m_filter->getAnimation(key);
if (!animation.is_valid() || animation.key_count() < 2)
continue;
const int lastIndex = animation.key_count() - 1;
const int lastFrame = animation.key_get_frame(lastIndex);
const auto lastType = animation.key_get_type(lastIndex);
const QString value = m_filter->get(key, animation.key_get_frame(0));
if (m_filter->service().anim_set(qUtf8Printable(key),
qUtf8Printable(value),
lastFrame,
m_filter->duration())) {
LOG_ERROR() << "Failed to match keyframe value" << key;
} else if (animation.key_set_type(lastIndex, lastType)) {
LOG_ERROR() << "Failed to restore keyframe type" << key;
} else {
animation.interpolate();
m_filter->service().get(qUtf8Printable(key));
m_filter->updateUndoCommand(key);
}
Comment on lines +1258 to +1276
}
m_filter->endUndoCommand();
emit m_filter->changed(name);
emit m_filter->propertyChanged(name);
}
7 changes: 6 additions & 1 deletion src/models/keyframesmodel.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2023 Meltytech, LLC
* Copyright (c) 2018-2026 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
Expand Down Expand Up @@ -29,6 +29,7 @@ class QmlFilter;
class KeyframesModel : public QAbstractItemModel
{
Q_OBJECT
Q_PROPERTY(bool supportsLoop READ supportsLoop CONSTANT)

public:
enum InterpolationType {
Expand Down Expand Up @@ -79,6 +80,7 @@ class KeyframesModel : public QAbstractItemModel
MaximumValueRole, /// parameter only
LowestValueRole, /// parameter only
HighestValueRole, /// parameter only
LoopRole, /// parameter only
FrameNumberRole, /// keyframe only
KeyframeTypeRole, /// keyframe only
PrevKeyframeTypeRole, /// keyframe only
Expand All @@ -89,6 +91,7 @@ class KeyframesModel : public QAbstractItemModel

explicit KeyframesModel(QObject *parent = 0);
virtual ~KeyframesModel();
bool supportsLoop() const;

int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
Expand All @@ -114,6 +117,8 @@ class KeyframesModel : public QAbstractItemModel
int keyframeIndex,
double value,
int position);
Q_INVOKABLE void setLoop(int parameterIndex, bool loop);
void matchFirstValue(int parameterIndex);
Q_INVOKABLE bool isKeyframe(int parameterIndex, int position);
Q_INVOKABLE bool advancedKeyframesInUse();
Q_INVOKABLE void removeAdvancedKeyframes();
Expand Down
Loading
Loading