diff --git a/companion/src/firmwares/radiodata.cpp b/companion/src/firmwares/radiodata.cpp index a36c43595..3dc3b5b85 100644 --- a/companion/src/firmwares/radiodata.cpp +++ b/companion/src/firmwares/radiodata.cpp @@ -106,7 +106,7 @@ void RadioData::convert(RadioDataConversionState & cstate) void RadioData::addLabel(QString label) { - if(!labels.contains(label)) + if(labels.indexOf(label) == -1) labels.append(label); } @@ -116,7 +116,7 @@ bool RadioData::deleteLabel(QString label) // Remove labels in the models for(auto& model : models) { QStringList modelLabels = QString(model.labels).split(',',QString::SkipEmptyParts); - if(modelLabels.contains(label)) + if(modelLabels.indexOf(label) >= 0) deleted = true; modelLabels.removeAll(label); strcpy(model.labels, QString(modelLabels.join(',')).toLocal8Bit().data()); @@ -182,7 +182,7 @@ bool RadioData::addLabelToModel(int index, QString label) // Make sure it will fit if(strlen(modelLabelCsv) + label.size() + 1 < sizeof(models[index].labels)-1) { QStringList modelLabels = QString(modelLabelCsv).split(',',QString::SkipEmptyParts); - if(!modelLabels.contains(label)) { + if(modelLabels.indexOf(label) == -1) { modelLabels.append(label); strcpy(models[index].labels, QString(modelLabels.join(',')).toLocal8Bit().data()); return true; @@ -197,10 +197,20 @@ bool RadioData::removeLabelFromModel(int index, QString label) char *modelLabelCsv = models[index].labels; QStringList modelLabels = QString(modelLabelCsv).split(',',QString::SkipEmptyParts); - if(modelLabels.contains(label)) { + if(modelLabels.indexOf(label) >= 0) { modelLabels.removeAll(label); strcpy(models[index].labels, QString(modelLabels.join(',')).toLocal8Bit().data()); return true; } return false; } + +void RadioData::addLabelsFromModels() +{ + for(const auto &model: models) { + QStringList labels = QString(model.labels).split(',',QString::SkipEmptyParts); + foreach(QString label, labels) { + addLabel(label); + } + } +} diff --git a/companion/src/firmwares/radiodata.h b/companion/src/firmwares/radiodata.h index 987154be7..ff672d1f4 100644 --- a/companion/src/firmwares/radiodata.h +++ b/companion/src/firmwares/radiodata.h @@ -29,6 +29,7 @@ class RadioData { bool renameLabel(int index, QString to); bool addLabelToModel(int index, QString label); bool removeLabelFromModel(int index, QString label); + void addLabelsFromModels(); void setCurrentModel(unsigned int index); void fixModelFilenames(); diff --git a/companion/src/labels.cpp b/companion/src/labels.cpp index 20423fcc8..4486871b3 100644 --- a/companion/src/labels.cpp +++ b/companion/src/labels.cpp @@ -36,12 +36,11 @@ Qt::ItemFlags LabelsModel::flags(const QModelIndex &index) const bool LabelsModel::setData(const QModelIndex &index, const QVariant &value, int role) { if(!index.isValid() || index.column() != 0 || - index.row() >= radioData->labels.size() || - selectedModel == -1) + index.row() >= radioData->labels.size()) return false; if(role == Qt::CheckStateRole) { - if(value==Qt::Unchecked) { + if(value==Qt::Unchecked && selectedModel != -1) { if(radioData->removeLabelFromModel(selectedModel, radioData->labels.at(index.row()))) emit modelChanged(selectedModel); } else { @@ -52,12 +51,15 @@ bool LabelsModel::setData(const QModelIndex &index, const QVariant &value, int r this->index(index.row(), 0)); return true; } else if(role == Qt::EditRole) { - // Don't allow comma, replace with _ - if(radioData->renameLabel(index.row(),value.toString().replace(',','_'))) { - labels[index.row()].label = value.toString().replace(',','_'); + QString replFrom = labels[index.row()].label; + QString replTo = value.toString().replace(',','_'); // Don't allow comma, replace with _ + if(radioData->labels.indexOf(replTo) == -1) { // Don't allow duplicates + bool modelsChanged = radioData->renameLabel(replFrom,replTo); + labels[index.row()].label = replTo; emit dataChanged(this->index(index.row(), 0), this->index(index.row(), 0)); - emit modelChanged(selectedModel); + if(selectedModel != -1 && modelsChanged) + emit modelChanged(selectedModel); } return true; } @@ -66,7 +68,6 @@ bool LabelsModel::setData(const QModelIndex &index, const QVariant &value, int r QVariant LabelsModel::data(const QModelIndex &index, int role) const { - // if(index.row() >= radioData->labels.size() || !index.isValid() ) if(index.row() >= labels.size() || !index.isValid() ) return QVariant(); @@ -78,11 +79,12 @@ QVariant LabelsModel::data(const QModelIndex &index, int role) const //return label; } } else if (role == Qt::CheckStateRole) { - if(index.column() == 0 && selectedModel >= 0 && - selectedModel < (int)radioData->models.size()) { - return QString(radioData->models.at(selectedModel).labels).contains(label)?Qt::Checked:Qt::Unchecked; - } + if(index.column() == 0 && selectedModel >= 0 && + selectedModel < (int)radioData->models.size()) { + QStringList modelLabels = QString(radioData->models.at(selectedModel).labels).split(',',QString::SkipEmptyParts); + return modelLabels.indexOf(label)==-1?Qt::Unchecked:Qt::Checked; } + } return QVariant(); } @@ -103,8 +105,7 @@ QModelIndex LabelsModel::parent(const QModelIndex &index) const int LabelsModel::rowCount(const QModelIndex &parent) const { - Q_UNUSED(parent); - //return radioData->labels.size(); + Q_UNUSED(parent); return labels.size(); } @@ -218,7 +219,7 @@ bool LabelsModel::insertRows(int row, int count, const QModelIndex &parent) if(newno == 0) newstr = QString(tr("New")); newno++; - } while(radioData->labels.contains(newstr)); + } while(radioData->labels.indexOf(newstr) >= 0); // Add it to radioData radioData->labels.insert(row+i, newstr); } diff --git a/companion/src/mdichild.cpp b/companion/src/mdichild.cpp index 46f9871db..c3e23927f 100644 --- a/companion/src/mdichild.cpp +++ b/companion/src/mdichild.cpp @@ -1063,6 +1063,7 @@ void MdiChild::pasteModelData(const QMimeData * mimeData, const QModelIndex row, deletesList.append(origMdlIdx); removeModelFromCutList(origMdlIdx); } + radioData.addLabelsFromModels(); } //qDebug().nospace() << "i: " << i << "; modelIdx:" << modelIdx << "; origMdlIdx: " << origMdlIdx << "; doMove: " << doMove << "; inserts:" << inserts << "; deletes: " << deletesList; diff --git a/companion/src/storage/labeled.cpp b/companion/src/storage/labeled.cpp index 8cb2ed177..4175ef297 100644 --- a/companion/src/storage/labeled.cpp +++ b/companion/src/storage/labeled.cpp @@ -283,20 +283,17 @@ bool LabelsStorageFormat::loadYaml(RadioData & radioData) radioData.generalSettings.currModelIndex = modelIdx; } - if (hasLabels) { - QStringList labels = QString(model.labels).split(',',QString::SkipEmptyParts); - foreach(QString label, labels) { - radioData.addLabel(label); - } - } - model.used = true; modelIdx++; } - // If no labels, add a Favorites - if(hasLabels && !radioData.labels.size()) { - radioData.labels.append(tr("Favorites")); + // Add the labels in the models + if (hasLabels) { + radioData.addLabelsFromModels(); + + // If no labels, add a Favorites + if(!radioData.labels.size()) + radioData.labels.append(tr("Favorites")); } return true;