Commit 5c9d4ead authored by Jean-Baptiste Mardelle's avatar Jean-Baptiste Mardelle
Browse files

Fix grouping after copy / paster

parent a72fff76
Pipeline #3387 passed with stage
in 14 minutes and 20 seconds
...@@ -838,6 +838,37 @@ bool GroupsModel::fromJson(const QString &data) ...@@ -838,6 +838,37 @@ bool GroupsModel::fromJson(const QString &data)
return ok; return ok;
} }
void GroupsModel::adjustOffset(QJsonArray &updatedNodes, QJsonObject childObject, int offset, const QMap<int, int> &trackMap)
{
auto value = childObject.value(QLatin1String("children"));
auto children = value.toArray();
for (auto c : children) {
if (!c.isObject()) {
continue;
}
auto child = c.toObject();
auto type = groupTypeFromStr(child.value(QLatin1String("type")).toString());
if (child.contains(QLatin1String("data"))) {
if (auto ptr = m_parent.lock()) {
QString cur_data = child.value(QLatin1String("data")).toString();
int trackId = cur_data.section(":", 0, 0).toInt();
int pos = cur_data.section(":", 1, 1).toInt();
int trackPos = ptr->getTrackPosition(trackMap.value(trackId));
pos += offset;
child.insert(QLatin1String("data"), QJsonValue(QString("%1:%2").arg(trackPos).arg(pos)));
updatedNodes.append(QJsonValue(child));
}
} else if (type != GroupType::Leaf) {
QJsonObject currentGroup;
currentGroup.insert(QLatin1String("type"), QJsonValue(groupTypeToStr(type)));
QJsonArray array;
adjustOffset(array, child, offset, trackMap);
currentGroup.insert(QLatin1String("children"), array);
updatedNodes.append(QJsonValue(currentGroup));
}
}
}
bool GroupsModel::fromJsonWithOffset(const QString &data, const QMap<int, int> &trackMap, int offset, Fun &undo, Fun &redo) bool GroupsModel::fromJsonWithOffset(const QString &data, const QMap<int, int> &trackMap, int offset, Fun &undo, Fun &redo)
{ {
Fun local_undo = []() { return true; }; Fun local_undo = []() { return true; };
...@@ -848,6 +879,7 @@ bool GroupsModel::fromJsonWithOffset(const QString &data, const QMap<int, int> & ...@@ -848,6 +879,7 @@ bool GroupsModel::fromJsonWithOffset(const QString &data, const QMap<int, int> &
return false; return false;
} }
auto list = json.array(); auto list = json.array();
QJsonArray newGroups;
bool ok = true; bool ok = true;
for (auto elem : list) { for (auto elem : list) {
if (!elem.isObject()) { if (!elem.isObject()) {
...@@ -856,39 +888,31 @@ bool GroupsModel::fromJsonWithOffset(const QString &data, const QMap<int, int> & ...@@ -856,39 +888,31 @@ bool GroupsModel::fromJsonWithOffset(const QString &data, const QMap<int, int> &
return false; return false;
} }
QJsonObject obj = elem.toObject(); QJsonObject obj = elem.toObject();
QJsonArray updatedNodes;
auto type = groupTypeFromStr(obj.value(QLatin1String("type")).toString());
auto value = obj.value(QLatin1String("children")); auto value = obj.value(QLatin1String("children"));
if (!value.isArray()) { if (!value.isArray()) {
qDebug() << "Error : Expected json array of children while parsing groups"; qDebug() << "Error : Expected json array of children while parsing groups";
continue; continue;
} }
QJsonArray updatedNodes; // Adjust offset
auto children = value.toArray(); auto children = value.toArray();
std::unordered_set<int> ids; adjustOffset(updatedNodes, obj, offset, trackMap);
for (auto c : children) { QJsonObject currentGroup;
if (!c.isObject()) { currentGroup.insert(QLatin1String("children"), QJsonValue(updatedNodes));
continue; currentGroup.insert(QLatin1String("type"), QJsonValue(groupTypeToStr(type)));
} newGroups.append(QJsonValue(currentGroup));
QJsonObject child = c.toObject(); }
if (child.contains(QLatin1String("data"))) {
if (auto ptr = m_parent.lock()) { // Group
QString cur_data = child.value(QLatin1String("data")).toString(); for (const auto &elem : newGroups) {
int trackId = cur_data.section(":", 0, 0).toInt(); if (!elem.isObject()) {
int pos = cur_data.section(":", 1, 1).toInt(); qDebug() << "Error : Expected json object while parsing groups";
int trackPos = ptr->getTrackPosition(trackMap.value(trackId));
pos += offset;
child.insert(QLatin1String("data"), QJsonValue(QString("%1:%2").arg(trackPos).arg(pos)));
}
updatedNodes.append(QJsonValue(child));
}
}
qDebug() << "* ** * UPDATED JSON NODES: " << updatedNodes;
obj.insert(QLatin1String("children"), QJsonValue(updatedNodes));
qDebug() << "* ** * UPDATED JSON NODES: " << obj;
ok = ok && (fromJson(obj, local_undo, local_redo) > 0);
if (!ok) {
break; break;
} }
ok = ok && fromJson(elem.toObject(), local_undo, local_redo);
} }
if (ok) { if (ok) {
UPDATE_UNDO_REDO(local_redo, local_undo, undo, redo); UPDATE_UNDO_REDO(local_redo, local_undo, undo, redo);
} else { } else {
......
...@@ -219,6 +219,8 @@ protected: ...@@ -219,6 +219,8 @@ protected:
@param type: new type of the group @param type: new type of the group
*/ */
void setType(int gid, GroupType type); void setType(int gid, GroupType type);
void adjustOffset(QJsonArray &updatedNodes, QJsonObject childObject, int offset, const QMap<int, int> &trackMap);
private: private:
std::weak_ptr<TimelineItemModel> m_parent; std::weak_ptr<TimelineItemModel> m_parent;
......
Markdown is supported
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