Commit d14f03f8 authored by Julius Künzel's avatar Julius Künzel 💬
Browse files

[Keyframes] Refactor code

This makes the code better readable by introducing a isAnimated()
function instead of checking all the different animatable types again and
again in the different places.

It also fixes a few minor things such as "copy keyfram at cursor
position" for rotoscoping
parent a5b757e7
Pipeline #243611 failed with stage
in 4 minutes and 39 seconds
......@@ -839,21 +839,6 @@ QString KeyframeModel::getAnimProperty() const
bool first = true;
std::shared_ptr<Mlt::Animation> anim(nullptr);
for (const auto &keyframe : m_keyframeList) {
if (first) {
switch (m_paramType) {
case ParamType::AnimatedRect:
mlt_prop.anim_set("key", keyframe.second.second.toString().toUtf8().constData(), keyframe.first.frames(pCore->getCurrentFps()));
break;
default:
mlt_prop.anim_set("key", keyframe.second.second.toDouble(), keyframe.first.frames(pCore->getCurrentFps()));
break;
}
anim.reset(mlt_prop.get_anim("key"));
anim->key_set_type(ix, convertToMltType(keyframe.second.first));
first = false;
ix++;
continue;
}
switch (m_paramType) {
case ParamType::AnimatedRect:
mlt_prop.anim_set("key", keyframe.second.second.toString().toUtf8().constData(), keyframe.first.frames(pCore->getCurrentFps()));
......@@ -862,6 +847,10 @@ QString KeyframeModel::getAnimProperty() const
mlt_prop.anim_set("key", keyframe.second.second.toDouble(), keyframe.first.frames(pCore->getCurrentFps()));
break;
}
if (first) {
anim.reset(mlt_prop.get_anim("key"));
first = false;
}
anim->key_set_type(ix, convertToMltType(keyframe.second.first));
ix++;
}
......@@ -1176,8 +1165,7 @@ void KeyframeModel::sendModification()
if (auto ptr = m_model.lock()) {
Q_ASSERT(m_index.isValid());
QString name = ptr->data(m_index, AssetParameterModel::NameRole).toString();
if (m_paramType == ParamType::KeyframeParam || m_paramType == ParamType::AnimatedRect || m_paramType == ParamType::Roto_spline ||
m_paramType == ParamType::ColorWheel) {
if (AssetParameterModel::isAnimated(m_paramType)) {
m_lastData = getAnimProperty();
ptr->setParameter(name, m_lastData, false, m_index);
} else {
......@@ -1228,10 +1216,10 @@ void KeyframeModel::refresh()
qDebug() << "// DATA WAS ALREADY PARSED, ABORTING REFRESH\n";
return;
}
if (m_paramType == ParamType::KeyframeParam || m_paramType == ParamType::AnimatedRect || m_paramType == ParamType::ColorWheel) {
parseAnimProperty(animData);
} else if (m_paramType == ParamType::Roto_spline) {
if (m_paramType == ParamType::Roto_spline) {
parseRotoProperty(animData);
} else if (AssetParameterModel::isAnimated(m_paramType)) {
parseAnimProperty(animData);
} else {
// first, try to convert to double
bool ok = false;
......@@ -1262,11 +1250,11 @@ void KeyframeModel::reset()
qDebug() << "// DATA WAS ALREADY PARSED, ABORTING\n_________________";
return;
}
if (m_paramType == ParamType::KeyframeParam || m_paramType == ParamType::AnimatedRect || m_paramType == ParamType::ColorWheel) {
if (m_paramType == ParamType::Roto_spline) {
// TODO: resetRotoProperty(animData);
} else if (AssetParameterModel::isAnimated(m_paramType)) {
qDebug() << "parsing keyframe" << animData;
resetAnimProperty(animData);
} else if (m_paramType == ParamType::Roto_spline) {
// TODO: resetRotoProperty(animData);
} else {
// first, try to convert to double
bool ok = false;
......
......@@ -121,7 +121,8 @@ AssetParameterModel::AssetParameterModel(std::unique_ptr<Mlt::Properties> asset,
val += out;
value = QString::number(val);
}
} else if (currentRow.type == ParamType::KeyframeParam || currentRow.type == ParamType::AnimatedRect || currentRow.type == ParamType::ColorWheel) {
} else if (isAnimated(currentRow.type) && currentRow.type != ParamType::Roto_spline) {
// Roto_spline keyframes are stored as JSON so do not apply this to roto
if (!value.contains(QLatin1Char('='))) {
value.prepend(QStringLiteral("%1=").arg(pCore->getItemIn(m_ownerId)));
}
......@@ -229,8 +230,7 @@ void AssetParameterModel::prepareKeyframes()
if (m_keyframes) return;
int ix = 0;
for (const auto &name : qAsConst(m_rows)) {
if (m_params.at(name).type == ParamType::KeyframeParam || m_params.at(name).type == ParamType::AnimatedRect ||
m_params.at(name).type == ParamType::Roto_spline || m_params.at(name).type == ParamType::ColorWheel) {
if (isAnimated(m_params.at(name).type)) {
addKeyframeParam(index(ix, 0));
}
ix++;
......@@ -246,8 +246,7 @@ QStringList AssetParameterModel::getKeyframableParameters() const
QStringList paramNames;
int ix = 0;
for (const auto &name : m_rows) {
if (m_params.at(name).type == ParamType::KeyframeParam || m_params.at(name).type == ParamType::AnimatedRect ||
m_params.at(name).type == ParamType::ColorWheel) {
if (isAnimated(m_params.at(name).type) && m_params.at(name).type != ParamType::Roto_spline) {
// addKeyframeParam(index(ix, 0));
paramNames << name;
}
......@@ -701,6 +700,12 @@ ParamType AssetParameterModel::paramTypeFromStr(const QString &type)
return ParamType::Double;
}
// static
bool AssetParameterModel::isAnimated(ParamType type)
{
return type == ParamType::KeyframeParam || type == ParamType::AnimatedRect || type == ParamType::ColorWheel || type == ParamType::Roto_spline;
}
// static
QString AssetParameterModel::getDefaultKeyframes(int start, const QString &defaultValue, bool linearOnly)
{
......@@ -944,8 +949,7 @@ QJsonDocument AssetParameterModel::toJson(bool includeFixed) const
QString x, y, w, h;
int rectIn = 0, rectOut = 0;
for (const auto &param : m_params) {
if (!includeFixed && param.second.type != ParamType::KeyframeParam && param.second.type != ParamType::AnimatedRect &&
param.second.type != ParamType::Roto_spline) {
if (!includeFixed && !isAnimated(param.second.type)) {
continue;
}
QJsonObject currentParam;
......@@ -1067,11 +1071,9 @@ QJsonDocument AssetParameterModel::valueAsJson(int pos, bool includeFixed) const
double x, y, w, h;
int count = 0;
for (const auto &param : m_params) {
if (!includeFixed && param.second.type != ParamType::KeyframeParam && param.second.type != ParamType::AnimatedRect &&
param.second.type != ParamType::ColorWheel) {
if (!includeFixed && !isAnimated(param.second.type)) {
continue;
}
QJsonObject currentParam;
QModelIndex ix = index(m_rows.indexOf(param.first), 0);
auto value = m_keyframes->getInterpolatedValue(pos, ix);
......@@ -1094,15 +1096,22 @@ QJsonDocument AssetParameterModel::valueAsJson(int pos, bool includeFixed) const
}
currentParam.insert(QLatin1String("name"), QJsonValue(param.first));
currentParam.insert(QLatin1String("value"), QJsonValue(QStringLiteral("0=%1").arg(
QString stringValue;
if (param.second.type == ParamType::Roto_spline) {
QJsonObject obj;
obj.insert(QStringLiteral("0"), value.toJsonArray());
stringValue = QString(QJsonDocument(obj).toJson());
} else {
stringValue = QStringLiteral("0=%1").arg(
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
value.type() == QVariant::Double
value.type() == QVariant::Double
#else
value.typeId() == QMetaType::Double
value.typeId() == QMetaType::Double
#endif
? QString::number(value.toDouble())
: value.toString())));
? QString::number(value.toDouble())
: value.toString());
}
int type = data(ix, AssetParameterModel::TypeRole).toInt();
double min = data(ix, AssetParameterModel::MinRole).toDouble();
......@@ -1112,6 +1121,7 @@ QJsonDocument AssetParameterModel::valueAsJson(int pos, bool includeFixed) const
min /= factor;
max /= factor;
}
currentParam.insert(QLatin1String("value"), QJsonValue(stringValue));
currentParam.insert(QLatin1String("type"), QJsonValue(type));
currentParam.insert(QLatin1String("min"), QJsonValue(min));
currentParam.insert(QLatin1String("max"), QJsonValue(max));
......
......@@ -134,6 +134,9 @@ public:
Enum16Role
};
/** @brief Returns true if @param type is animated */
static bool isAnimated(ParamType type);
/** @brief Returns the id of the asset represented by this object */
QString getAssetId() const;
const QString getAssetMltId();
......
......@@ -95,7 +95,7 @@ void AssetParameterView::setModel(const std::shared_ptr<AssetParameterModel> &mo
emit updateHeight();
});
m_lay->addWidget(w);
if (type == ParamType::KeyframeParam || type == ParamType::AnimatedRect || type == ParamType::Roto_spline || type == ParamType::ColorWheel) {
if (AssetParameterModel::isAnimated(type)) {
m_mainKeyframeWidget = static_cast<KeyframeWidget *>(w);
connect(this, &AssetParameterView::nextKeyframe, m_mainKeyframeWidget, &KeyframeWidget::goToNext);
connect(this, &AssetParameterView::previousKeyframe, m_mainKeyframeWidget, &KeyframeWidget::goToPrevious);
......@@ -123,7 +123,8 @@ QVector<QPair<QString, QVariant>> AssetParameterView::getDefaultValues() const
QString name = m_model->data(index, AssetParameterModel::NameRole).toString();
auto type = m_model->data(index, AssetParameterModel::TypeRole).value<ParamType>();
QVariant defaultValue = m_model->data(index, AssetParameterModel::DefaultRole);
if (type == ParamType::KeyframeParam || type == ParamType::AnimatedRect || type == ParamType::ColorWheel) {
if (AssetParameterModel::isAnimated(type) && type != ParamType::Roto_spline) {
// Roto_spline keyframes are stored as JSON so do not apply this to roto
QString val = defaultValue.toString();
if (!val.contains(QLatin1Char('='))) {
val.prepend(QStringLiteral("%1=").arg(m_model->data(index, AssetParameterModel::ParentInRole).toInt()));
......
......@@ -66,9 +66,12 @@ AbstractParamWidget *AbstractParamWidget::construct(const std::shared_ptr<AssetP
// We retrieve the parameter type
auto type = model->data(index, AssetParameterModel::TypeRole).value<ParamType>();
QString name = model->data(index, AssetParameterModel::NameRole).toString();
if (AssetParameterModel::isAnimated(type)) {
return new KeyframeWidget(model, index, frameSize, parent);
}
AbstractParamWidget *widget = nullptr;
QString name = model->data(index, AssetParameterModel::NameRole).toString();
switch (type) {
case ParamType::Double:
......@@ -86,12 +89,6 @@ AbstractParamWidget *AbstractParamWidget::construct(const std::shared_ptr<AssetP
case ParamType::Bool:
widget = new BoolParamWidget(model, index, parent);
break;
case ParamType::KeyframeParam:
case ParamType::AnimatedRect:
case ParamType::Roto_spline:
case ParamType::ColorWheel:
widget = new KeyframeWidget(model, index, frameSize, parent);
break;
case ParamType::Geometry:
widget = new GeometryEditWidget(model, index, frameSize, parent);
break;
......@@ -145,12 +142,8 @@ AbstractParamWidget *AbstractParamWidget::construct(const std::shared_ptr<AssetP
}
case ParamType::Animated:
case ParamType::RestrictedAnim:
// widget = new AnimationWidget(model, index, range, parent);
// break;
// case ParamType::KeyframeParam:
// widget = new KeyframeEdit(model, index, parent);
// break;
case ParamType::Addedgeometry:
default:
// not reimplemented
widget = new Unsupported(model, index, parent);
static_cast<Unsupported *>(widget)->setText(name);
......
......@@ -75,13 +75,7 @@ protected:
WidthOnly,
HeightOnly
};
enum {
ValueRole = Qt::UserRole,
TypeRole,
MinRole,
MaxRole,
OpacityRole
};
enum { ValueRole = Qt::UserRole, TypeRole, MinRole, MaxRole, OpacityRole };
mutable QReadWriteLock m_lock; // This is a lock that ensures safety in case of concurrent access
void resizeEvent(QResizeEvent *ev) override;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment