fix(cpn): paste models crashing on Windows (#6892)

This commit is contained in:
Neil Horne
2025-12-23 07:13:08 +10:00
committed by GitHub
parent 41c08b86d1
commit 52a64cc813
2 changed files with 37 additions and 22 deletions
+17 -10
View File
@@ -789,7 +789,9 @@ bool MdiChild::insertModelRows(int atModelIdx, int count)
} }
} }
// add a placeholder model // add a placeholder model
radioData.models.insert(radioData.models.begin() + atModelIdx + i, ModelData()); ModelData *md = new ModelData();
radioData.models.insert(radioData.models.begin() + atModelIdx + i, *md);
delete md;
// adjust current model index if needed // adjust current model index if needed
if ((int)radioData.generalSettings.currModelIndex >= atModelIdx + i) if ((int)radioData.generalSettings.currModelIndex >= atModelIdx + i)
findNewDefaultModel(radioData.generalSettings.currModelIndex + 1); findNewDefaultModel(radioData.generalSettings.currModelIndex + 1);
@@ -920,8 +922,10 @@ unsigned MdiChild::countUsedModels()
void MdiChild::pasteModelData(const QMimeData * mimeData, const QModelIndex row, bool insert, bool move) void MdiChild::pasteModelData(const QMimeData * mimeData, const QModelIndex row, bool insert, bool move)
{ {
QVector<ModelData> modelsList; QVector<ModelData> *modelsList = new QVector<ModelData>;
if (!ModelsListModel::decodeMimeData(mimeData, &modelsList)) modelsList->clear();
if (!ModelsListModel::decodeMimeData(mimeData, modelsList))
return; return;
bool modified = false; bool modified = false;
@@ -936,14 +940,14 @@ void MdiChild::pasteModelData(const QMimeData * mimeData, const QModelIndex row,
//qDebug().nospace() << "row: " << row << "; ins: " << insert << "; mv: " << move << "; row modelIdx: " << modelIdx; //qDebug().nospace() << "row: " << row << "; ins: " << insert << "; mv: " << move << "; row modelIdx: " << modelIdx;
// Model data // Model data
for (int i=0; i < modelsList.size(); ++i) { for (int i=0; i < modelsList->size(); ++i) {
int origMdlIdx = hasOwnData ? modelsList.at(i).modelIndex : -1; // where is the model in *our* current array? int origMdlIdx = hasOwnData ? modelsList->at(i).modelIndex : -1; // where is the model in *our* current array?
bool doMove = (origMdlIdx > -1 && origMdlIdx < (int)radioData.models.size() && (move || cutModels.contains(origMdlIdx))); // DnD-moved or clipboard cut bool doMove = (origMdlIdx > -1 && origMdlIdx < (int)radioData.models.size() && (move || cutModels.contains(origMdlIdx))); // DnD-moved or clipboard cut
bool ok = true; bool ok = true;
if (modelIdx == -1 || (!insert && modelIdx >= (int)radioData.models.size())) { if (modelIdx == -1 || (!insert && modelIdx >= (int)radioData.models.size())) {
// This handles pasting past the end or when pasting multiple models. // This handles pasting past the end or when pasting multiple models.
modelIdx = modelAppend(modelsList[i]); modelIdx = modelAppend(modelsList->at(i));
if (modelIdx < 0) { if (modelIdx < 0) {
ok = false; ok = false;
showWarning(tr("Cannot paste model, out of available model slots.")); showWarning(tr("Cannot paste model, out of available model slots."));
@@ -952,14 +956,14 @@ void MdiChild::pasteModelData(const QMimeData * mimeData, const QModelIndex row,
else if (insert) { else if (insert) {
ok = insertModelRows(modelIdx, 1); ok = insertModelRows(modelIdx, 1);
if (ok) { if (ok) {
radioData.models[modelIdx] = modelsList[i]; radioData.models[modelIdx] = modelsList->at(i);
// ++inserts; // ++inserts;
} }
} }
else if (!deletesList.contains(modelIdx)) { else if (!deletesList.contains(modelIdx)) {
// pasting on top of a slot // pasting on top of a slot
if (radioData.models[modelIdx].isEmpty()) { if (radioData.models[modelIdx].isEmpty()) {
radioData.models[modelIdx] = modelsList[i]; radioData.models[modelIdx] = modelsList->at(i);
ok = true; ok = true;
} }
else { else {
@@ -974,13 +978,13 @@ void MdiChild::pasteModelData(const QMimeData * mimeData, const QModelIndex row,
msgBox.exec(); msgBox.exec();
if (msgBox.clickedButton() == overwriteButton) { if (msgBox.clickedButton() == overwriteButton) {
radioData.models[modelIdx] = modelsList[i]; radioData.models[modelIdx] = modelsList->at(i);
ok = true; ok = true;
} }
else if (msgBox.clickedButton() == insertButton) { else if (msgBox.clickedButton() == insertButton) {
ok = insertModelRows(modelIdx, 1); ok = insertModelRows(modelIdx, 1);
if (ok) { if (ok) {
radioData.models[modelIdx] = modelsList[i]; radioData.models[modelIdx] = modelsList->at(i);
// ++inserts; // ++inserts;
} }
} }
@@ -1012,9 +1016,12 @@ void MdiChild::pasteModelData(const QMimeData * mimeData, const QModelIndex row,
if (deletesList.size()) { if (deletesList.size()) {
deleteModels(deletesList); deleteModels(deletesList);
} }
if (modified) { if (modified) {
setModified(); setModified();
} }
delete modelsList;
} }
/* /*
+20 -12
View File
@@ -539,28 +539,36 @@ bool ModelsListModel::decodeMimeData(const QMimeData * mimeData, QVector<ModelDa
*hasGenSet = false; *hasGenSet = false;
if (models && mimeData->hasFormat("application/x-companion-modeldata")) { if (models && mimeData->hasFormat("application/x-companion-modeldata")) {
QByteArray data = mimeData->data("application/x-companion-modeldata"); QByteArray clipboard = mimeData->data("application/x-companion-modeldata");
QDataStream in(&data, QIODevice::ReadOnly); QDataStream in(&clipboard, QIODevice::ReadOnly);
Board::Type board; Board::Type board;
int mdlCnt; int mdlCnt;
in >> board >> mdlCnt; in >> board >> mdlCnt;
for (int i = 1; i <= mdlCnt; i++) { for (int i = 1; i <= mdlCnt; i++) {
QByteArray buffer; QByteArray *buffer = new QByteArray();
in >> buffer; in >> *buffer;
if (buffer.size() > 0) { if (buffer->size() > 0) {
ModelData model; ModelData *model = new ModelData();
if (loadModelFromYaml(model, buffer)) { if (loadModelFromYaml(*model, *buffer)) {
model.used = true; model->used = true;
QByteArray chklst; QByteArray *chklst = new QByteArray();
in >> chklst; in >> *chklst;
model.checklistData = chklst;
models->append(model); if (chklst->size() > 0) {
model->checklistData = *chklst;
models->append(*model);
}
delete chklst;
ret = true; ret = true;
} }
delete model;
delete buffer;
} }
} }
} }