File indexing completed on 2024-12-08 04:25:56
0001 /* 0002 SPDX-FileCopyrightText: 2017 Jean-Baptiste Mardelle 0003 SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0004 */ 0005 0006 #include "assetcommand.hpp" 0007 #include "assets/keyframes/model/keyframemodellist.hpp" 0008 #include "effects/effectsrepository.hpp" 0009 #include "transitions/transitionsrepository.hpp" 0010 #include <memory> 0011 #include <utility> 0012 AssetCommand::AssetCommand(const std::shared_ptr<AssetParameterModel> &model, const QModelIndex &index, QString value, QUndoCommand *parent) 0013 : QUndoCommand(parent) 0014 , m_model(model) 0015 , m_index(index) 0016 , m_value(std::move(value)) 0017 , m_updateView(false) 0018 , m_stamp(QTime::currentTime()) 0019 { 0020 m_name = m_model->data(index, AssetParameterModel::NameRole).toString(); 0021 const QString id = model->getAssetId(); 0022 if (EffectsRepository::get()->exists(id)) { 0023 setText(i18n("Edit %1", EffectsRepository::get()->getName(id))); 0024 } else if (TransitionsRepository::get()->exists(id)) { 0025 setText(i18n("Edit %1", TransitionsRepository::get()->getName(id))); 0026 } 0027 m_oldValue = m_model->data(index, AssetParameterModel::ValueRole).toString(); 0028 } 0029 0030 void AssetCommand::undo() 0031 { 0032 if (m_name.contains(QLatin1Char('\n'))) { 0033 // Check if it is a multi param 0034 auto type = m_model->data(m_index, AssetParameterModel::TypeRole).value<ParamType>(); 0035 if (type == ParamType::MultiSwitch) { 0036 QStringList names = m_name.split(QLatin1Char('\n')); 0037 QStringList oldValues = m_oldValue.split(QLatin1Char('\n')); 0038 if (names.count() == oldValues.count()) { 0039 for (int i = 0; i < names.count(); i++) { 0040 m_model->setParameter(names.at(i), oldValues.at(i), true, m_index); 0041 } 0042 return; 0043 } 0044 } 0045 } 0046 m_model->setParameter(m_name, m_oldValue, true, m_index); 0047 } 0048 0049 void AssetCommand::redo() 0050 { 0051 if (m_name.contains(QLatin1Char('\n'))) { 0052 // Check if it is a multi param 0053 auto type = m_model->data(m_index, AssetParameterModel::TypeRole).value<ParamType>(); 0054 if (type == ParamType::MultiSwitch) { 0055 QStringList names = m_name.split(QLatin1Char('\n')); 0056 QStringList values = m_value.split(QLatin1Char('\n')); 0057 if (names.count() == values.count()) { 0058 for (int i = 0; i < names.count(); i++) { 0059 m_model->setParameter(names.at(i), values.at(i), m_updateView, m_index); 0060 } 0061 m_updateView = true; 0062 return; 0063 } 0064 } 0065 } 0066 m_model->setParameter(m_name, m_value, m_updateView, m_index); 0067 m_updateView = true; 0068 } 0069 0070 int AssetCommand::id() const 0071 { 0072 return 1; 0073 } 0074 0075 bool AssetCommand::mergeWith(const QUndoCommand *other) 0076 { 0077 if (other->id() != id() || static_cast<const AssetCommand *>(other)->m_index != m_index || 0078 m_stamp.msecsTo(static_cast<const AssetCommand *>(other)->m_stamp) > 3000) { 0079 return false; 0080 } 0081 m_value = static_cast<const AssetCommand *>(other)->m_value; 0082 m_stamp = static_cast<const AssetCommand *>(other)->m_stamp; 0083 return true; 0084 } 0085 0086 AssetMultiCommand::AssetMultiCommand(const std::shared_ptr<AssetParameterModel> &model, const QList<QModelIndex> &indexes, const QStringList &values, 0087 QUndoCommand *parent) 0088 : QUndoCommand(parent) 0089 , m_model(model) 0090 , m_indexes(indexes) 0091 , m_values(values) 0092 , m_updateView(false) 0093 , m_stamp(QTime::currentTime()) 0094 { 0095 qDebug() << "CREATING MULTIPLE COMMAND!!!\nVALUES: " << m_values; 0096 m_name = m_model->data(m_indexes.first(), AssetParameterModel::NameRole).toString(); 0097 const QString id = model->getAssetId(); 0098 if (EffectsRepository::get()->exists(id)) { 0099 setText(i18n("Edit %1", EffectsRepository::get()->getName(id))); 0100 } else if (TransitionsRepository::get()->exists(id)) { 0101 setText(i18n("Edit %1", TransitionsRepository::get()->getName(id))); 0102 } 0103 for (QModelIndex ix : qAsConst(m_indexes)) { 0104 QVariant previousVal = m_model->data(ix, AssetParameterModel::ValueRole); 0105 m_oldValues << previousVal.toString(); 0106 } 0107 } 0108 0109 void AssetMultiCommand::undo() 0110 { 0111 int indx = 0; 0112 int max = m_indexes.size() - 1; 0113 for (const QModelIndex &ix : qAsConst(m_indexes)) { 0114 m_model->setParameter(m_model->data(ix, AssetParameterModel::NameRole).toString(), m_oldValues.at(indx), indx == max, ix); 0115 indx++; 0116 } 0117 } 0118 // virtual 0119 void AssetMultiCommand::redo() 0120 { 0121 int indx = 0; 0122 int max = m_indexes.size() - 1; 0123 for (const QModelIndex &ix : qAsConst(m_indexes)) { 0124 m_model->setParameter(m_model->data(ix, AssetParameterModel::NameRole).toString(), m_values.at(indx), m_updateView && indx == max, ix); 0125 indx++; 0126 } 0127 m_updateView = true; 0128 } 0129 0130 // virtual 0131 int AssetMultiCommand::id() const 0132 { 0133 return 1; 0134 } 0135 // virtual 0136 bool AssetMultiCommand::mergeWith(const QUndoCommand *other) 0137 { 0138 if (other->id() != id() || static_cast<const AssetMultiCommand *>(other)->m_indexes != m_indexes || 0139 m_stamp.msecsTo(static_cast<const AssetMultiCommand *>(other)->m_stamp) > 3000) { 0140 return false; 0141 } 0142 m_values = static_cast<const AssetMultiCommand *>(other)->m_values; 0143 m_stamp = static_cast<const AssetMultiCommand *>(other)->m_stamp; 0144 return true; 0145 } 0146 0147 AssetKeyframeCommand::AssetKeyframeCommand(const std::shared_ptr<AssetParameterModel> &model, const QModelIndex &index, QVariant value, GenTime pos, 0148 QUndoCommand *parent) 0149 : QUndoCommand(parent) 0150 , m_model(model) 0151 , m_index(index) 0152 , m_value(std::move(value)) 0153 , m_pos(pos) 0154 , m_updateView(false) 0155 , m_stamp(QTime::currentTime()) 0156 { 0157 const QString id = model->getAssetId(); 0158 if (EffectsRepository::get()->exists(id)) { 0159 setText(i18n("Edit %1 keyframe", EffectsRepository::get()->getName(id))); 0160 } else if (TransitionsRepository::get()->exists(id)) { 0161 setText(i18n("Edit %1 keyframe", TransitionsRepository::get()->getName(id))); 0162 } 0163 m_oldValue = m_model->getKeyframeModel()->getKeyModel(m_index)->getInterpolatedValue(m_pos); 0164 } 0165 0166 void AssetKeyframeCommand::undo() 0167 { 0168 m_model->getKeyframeModel()->getKeyModel(m_index)->directUpdateKeyframe(m_pos, m_oldValue); 0169 } 0170 // virtual 0171 void AssetKeyframeCommand::redo() 0172 { 0173 m_model->getKeyframeModel()->getKeyModel(m_index)->directUpdateKeyframe(m_pos, m_value); 0174 m_updateView = true; 0175 } 0176 0177 // virtual 0178 int AssetKeyframeCommand::id() const 0179 { 0180 return 2; 0181 } 0182 // virtual 0183 bool AssetKeyframeCommand::mergeWith(const QUndoCommand *other) 0184 { 0185 if (other->id() != id() || static_cast<const AssetKeyframeCommand *>(other)->m_index != m_index || 0186 m_stamp.msecsTo(static_cast<const AssetKeyframeCommand *>(other)->m_stamp) > 1000) { 0187 return false; 0188 } 0189 m_value = static_cast<const AssetKeyframeCommand *>(other)->m_value; 0190 m_stamp = static_cast<const AssetKeyframeCommand *>(other)->m_stamp; 0191 return true; 0192 } 0193 0194 AssetMultiKeyframeCommand::AssetMultiKeyframeCommand(const std::shared_ptr<AssetParameterModel> &model, const QList<QModelIndex> &indexes, 0195 const QStringList &sourceValues, const QStringList &values, GenTime pos, QUndoCommand *parent) 0196 : QUndoCommand(parent) 0197 , m_model(model) 0198 , m_indexes(indexes) 0199 , m_values(values) 0200 , m_oldValues(sourceValues) 0201 , m_pos(pos) 0202 , m_stamp(QTime::currentTime()) 0203 { 0204 const QString id = model->getAssetId(); 0205 if (EffectsRepository::get()->exists(id)) { 0206 setText(i18n("Edit %1 keyframe", EffectsRepository::get()->getName(id))); 0207 } else if (TransitionsRepository::get()->exists(id)) { 0208 setText(i18n("Edit %1 keyframe", TransitionsRepository::get()->getName(id))); 0209 } 0210 } 0211 0212 void AssetMultiKeyframeCommand::undo() 0213 { 0214 int indx = 0; 0215 for (const QModelIndex &ix : qAsConst(m_indexes)) { 0216 m_model->getKeyframeModel()->getKeyModel(ix)->directUpdateKeyframe(m_pos, m_oldValues.at(indx), false); 0217 m_model->getKeyframeModel()->getKeyModel(ix)->sendModification(); 0218 indx++; 0219 } 0220 Q_EMIT m_model->getKeyframeModel()->modelChanged(); 0221 } 0222 0223 // virtual 0224 void AssetMultiKeyframeCommand::redo() 0225 { 0226 int indx = 0; 0227 for (const QModelIndex &ix : qAsConst(m_indexes)) { 0228 m_model->getKeyframeModel()->getKeyModel(ix)->directUpdateKeyframe(m_pos, m_values.at(indx), false); 0229 m_model->getKeyframeModel()->getKeyModel(ix)->sendModification(); 0230 indx++; 0231 } 0232 Q_EMIT m_model->getKeyframeModel()->modelChanged(); 0233 } 0234 0235 // virtual 0236 int AssetMultiKeyframeCommand::id() const 0237 { 0238 return 4; 0239 } 0240 // virtual 0241 bool AssetMultiKeyframeCommand::mergeWith(const QUndoCommand *other) 0242 { 0243 if (other->id() != id()) { 0244 return false; 0245 } 0246 const AssetMultiKeyframeCommand *otherCommand = static_cast<const AssetMultiKeyframeCommand *>(other); 0247 if (!otherCommand || otherCommand->m_model != m_model || m_stamp.msecsTo(otherCommand->m_stamp) > 3000) { 0248 return false; 0249 } 0250 m_values = static_cast<const AssetMultiKeyframeCommand *>(other)->m_values; 0251 m_stamp = static_cast<const AssetMultiKeyframeCommand *>(other)->m_stamp; 0252 return true; 0253 } 0254 0255 AssetUpdateCommand::AssetUpdateCommand(const std::shared_ptr<AssetParameterModel> &model, QVector<QPair<QString, QVariant>> parameters, QUndoCommand *parent) 0256 : QUndoCommand(parent) 0257 , m_model(model) 0258 , m_value(std::move(parameters)) 0259 { 0260 const QString id = model->getAssetId(); 0261 if (EffectsRepository::get()->exists(id)) { 0262 setText(i18n("Update %1", EffectsRepository::get()->getName(id))); 0263 } else if (TransitionsRepository::get()->exists(id)) { 0264 setText(i18n("Update %1", TransitionsRepository::get()->getName(id))); 0265 } 0266 m_oldValue = m_model->getAllParameters(); 0267 } 0268 0269 void AssetUpdateCommand::undo() 0270 { 0271 m_model->setParameters(m_oldValue); 0272 } 0273 // virtual 0274 void AssetUpdateCommand::redo() 0275 { 0276 m_model->setParameters(m_value); 0277 } 0278 0279 // virtual 0280 int AssetUpdateCommand::id() const 0281 { 0282 return 3; 0283 }