From 6fa0382608a32c533a0ebbfe5478927148d8d0dc Mon Sep 17 00:00:00 2001 From: Neil Horne Date: Sun, 6 Sep 2020 18:59:37 +1000 Subject: [PATCH] Fix channel reorder affect on mixes (#7911) --- companion/src/firmwares/modeldata.cpp | 44 +++++++++++++++++++++++++++ companion/src/firmwares/modeldata.h | 1 + 2 files changed, 45 insertions(+) diff --git a/companion/src/firmwares/modeldata.cpp b/companion/src/firmwares/modeldata.cpp index d38069827..e38958249 100644 --- a/companion/src/firmwares/modeldata.cpp +++ b/companion/src/firmwares/modeldata.cpp @@ -659,6 +659,8 @@ int ModelData::updateReference() } } } + if (updRefInfo.type == REF_UPD_TYPE_CHANNEL) + sortMixes(); //s1.report("Mixes"); for (int i = 0; i < CPN_MAX_CHNOUT; i++) { @@ -1330,3 +1332,45 @@ void ModelData::removeMix(const int idx) memmove(&mixData[idx], &mixData[idx + 1], (CPN_MAX_MIXERS - (idx + 1)) * sizeof(MixData)); mixData[CPN_MAX_MIXERS - 1].clear(); } + +void ModelData::sortMixes() +{ + unsigned int lastchn = 0; + bool sortreq = false; + + for (int i = 0; i < CPN_MAX_MIXERS; i++) { + MixData *md = &mixData[i]; + if (!md->isEmpty()) { + if (md->destCh < lastchn) { + sortreq = true; + break; + } + else + lastchn = md->destCh; + } + } + + if (!sortreq) + return; + + // QMap automatically sorts based on key + QMap map; + for (int i = 0; i < CPN_MAX_MIXERS; i++) { + MixData *md = &mixData[i]; + if (!md->isEmpty()) { + // destCh may not be unique so build a compound sort key + map.insert(md->destCh * (CPN_MAX_MIXERS + 1) + i, i); + } + } + + MixData sortedMixData[CPN_MAX_MIXERS]; + int destidx = 0; + + QMap::const_iterator i; + for (i = map.constBegin(); i != map.constEnd(); ++i) { + memcpy(&sortedMixData[destidx], &mixData[i.value()], sizeof(MixData)); + destidx++; + } + + memcpy(&mixData[0], &sortedMixData[0], CPN_MAX_MIXERS * sizeof(MixData)); +} diff --git a/companion/src/firmwares/modeldata.h b/companion/src/firmwares/modeldata.h index 560206c72..558b848df 100644 --- a/companion/src/firmwares/modeldata.h +++ b/companion/src/firmwares/modeldata.h @@ -336,6 +336,7 @@ class ModelData { if (value != swtch.toValue()) value = swtch.toValue(); } + void sortMixes(); }; #endif // MODELDATA_H