(cpn) Labels - Various fixes

Don't allow duplicate
Partial labels of others also selected
Paste doesn't update labels list
This commit is contained in:
dlktdr
2022-09-06 21:04:10 +10:00
committed by Peter Feerick
parent 32a6f792b9
commit d462e24862
5 changed files with 39 additions and 29 deletions
+14 -4
View File
@@ -106,7 +106,7 @@ void RadioData::convert(RadioDataConversionState & cstate)
void RadioData::addLabel(QString label) void RadioData::addLabel(QString label)
{ {
if(!labels.contains(label)) if(labels.indexOf(label) == -1)
labels.append(label); labels.append(label);
} }
@@ -116,7 +116,7 @@ bool RadioData::deleteLabel(QString label)
// Remove labels in the models // Remove labels in the models
for(auto& model : models) { for(auto& model : models) {
QStringList modelLabels = QString(model.labels).split(',',QString::SkipEmptyParts); QStringList modelLabels = QString(model.labels).split(',',QString::SkipEmptyParts);
if(modelLabels.contains(label)) if(modelLabels.indexOf(label) >= 0)
deleted = true; deleted = true;
modelLabels.removeAll(label); modelLabels.removeAll(label);
strcpy(model.labels, QString(modelLabels.join(',')).toLocal8Bit().data()); strcpy(model.labels, QString(modelLabels.join(',')).toLocal8Bit().data());
@@ -182,7 +182,7 @@ bool RadioData::addLabelToModel(int index, QString label)
// Make sure it will fit // Make sure it will fit
if(strlen(modelLabelCsv) + label.size() + 1 < sizeof(models[index].labels)-1) { if(strlen(modelLabelCsv) + label.size() + 1 < sizeof(models[index].labels)-1) {
QStringList modelLabels = QString(modelLabelCsv).split(',',QString::SkipEmptyParts); QStringList modelLabels = QString(modelLabelCsv).split(',',QString::SkipEmptyParts);
if(!modelLabels.contains(label)) { if(modelLabels.indexOf(label) == -1) {
modelLabels.append(label); modelLabels.append(label);
strcpy(models[index].labels, QString(modelLabels.join(',')).toLocal8Bit().data()); strcpy(models[index].labels, QString(modelLabels.join(',')).toLocal8Bit().data());
return true; return true;
@@ -197,10 +197,20 @@ bool RadioData::removeLabelFromModel(int index, QString label)
char *modelLabelCsv = models[index].labels; char *modelLabelCsv = models[index].labels;
QStringList modelLabels = QString(modelLabelCsv).split(',',QString::SkipEmptyParts); QStringList modelLabels = QString(modelLabelCsv).split(',',QString::SkipEmptyParts);
if(modelLabels.contains(label)) { if(modelLabels.indexOf(label) >= 0) {
modelLabels.removeAll(label); modelLabels.removeAll(label);
strcpy(models[index].labels, QString(modelLabels.join(',')).toLocal8Bit().data()); strcpy(models[index].labels, QString(modelLabels.join(',')).toLocal8Bit().data());
return true; return true;
} }
return false; return false;
} }
void RadioData::addLabelsFromModels()
{
for(const auto &model: models) {
QStringList labels = QString(model.labels).split(',',QString::SkipEmptyParts);
foreach(QString label, labels) {
addLabel(label);
}
}
}
+1
View File
@@ -29,6 +29,7 @@ class RadioData {
bool renameLabel(int index, QString to); bool renameLabel(int index, QString to);
bool addLabelToModel(int index, QString label); bool addLabelToModel(int index, QString label);
bool removeLabelFromModel(int index, QString label); bool removeLabelFromModel(int index, QString label);
void addLabelsFromModels();
void setCurrentModel(unsigned int index); void setCurrentModel(unsigned int index);
void fixModelFilenames(); void fixModelFilenames();
+15 -14
View File
@@ -36,12 +36,11 @@ Qt::ItemFlags LabelsModel::flags(const QModelIndex &index) const
bool LabelsModel::setData(const QModelIndex &index, const QVariant &value, int role) bool LabelsModel::setData(const QModelIndex &index, const QVariant &value, int role)
{ {
if(!index.isValid() || index.column() != 0 || if(!index.isValid() || index.column() != 0 ||
index.row() >= radioData->labels.size() || index.row() >= radioData->labels.size())
selectedModel == -1)
return false; return false;
if(role == Qt::CheckStateRole) { if(role == Qt::CheckStateRole) {
if(value==Qt::Unchecked) { if(value==Qt::Unchecked && selectedModel != -1) {
if(radioData->removeLabelFromModel(selectedModel, radioData->labels.at(index.row()))) if(radioData->removeLabelFromModel(selectedModel, radioData->labels.at(index.row())))
emit modelChanged(selectedModel); emit modelChanged(selectedModel);
} else { } else {
@@ -52,12 +51,15 @@ bool LabelsModel::setData(const QModelIndex &index, const QVariant &value, int r
this->index(index.row(), 0)); this->index(index.row(), 0));
return true; return true;
} else if(role == Qt::EditRole) { } else if(role == Qt::EditRole) {
// Don't allow comma, replace with _ QString replFrom = labels[index.row()].label;
if(radioData->renameLabel(index.row(),value.toString().replace(',','_'))) { QString replTo = value.toString().replace(',','_'); // Don't allow comma, replace with _
labels[index.row()].label = value.toString().replace(',','_'); 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), emit dataChanged(this->index(index.row(), 0),
this->index(index.row(), 0)); this->index(index.row(), 0));
emit modelChanged(selectedModel); if(selectedModel != -1 && modelsChanged)
emit modelChanged(selectedModel);
} }
return true; 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 QVariant LabelsModel::data(const QModelIndex &index, int role) const
{ {
// if(index.row() >= radioData->labels.size() || !index.isValid() )
if(index.row() >= labels.size() || !index.isValid() ) if(index.row() >= labels.size() || !index.isValid() )
return QVariant(); return QVariant();
@@ -78,11 +79,12 @@ QVariant LabelsModel::data(const QModelIndex &index, int role) const
//return label; //return label;
} }
} else if (role == Qt::CheckStateRole) { } else if (role == Qt::CheckStateRole) {
if(index.column() == 0 && selectedModel >= 0 && if(index.column() == 0 && selectedModel >= 0 &&
selectedModel < (int)radioData->models.size()) { selectedModel < (int)radioData->models.size()) {
return QString(radioData->models.at(selectedModel).labels).contains(label)?Qt::Checked:Qt::Unchecked; QStringList modelLabels = QString(radioData->models.at(selectedModel).labels).split(',',QString::SkipEmptyParts);
} return modelLabels.indexOf(label)==-1?Qt::Unchecked:Qt::Checked;
} }
}
return QVariant(); return QVariant();
} }
@@ -104,7 +106,6 @@ QModelIndex LabelsModel::parent(const QModelIndex &index) const
int LabelsModel::rowCount(const QModelIndex &parent) const int LabelsModel::rowCount(const QModelIndex &parent) const
{ {
Q_UNUSED(parent); Q_UNUSED(parent);
//return radioData->labels.size();
return labels.size(); return labels.size();
} }
@@ -218,7 +219,7 @@ bool LabelsModel::insertRows(int row, int count, const QModelIndex &parent)
if(newno == 0) if(newno == 0)
newstr = QString(tr("New")); newstr = QString(tr("New"));
newno++; newno++;
} while(radioData->labels.contains(newstr)); } while(radioData->labels.indexOf(newstr) >= 0);
// Add it to radioData // Add it to radioData
radioData->labels.insert(row+i, newstr); radioData->labels.insert(row+i, newstr);
} }
+1
View File
@@ -1063,6 +1063,7 @@ void MdiChild::pasteModelData(const QMimeData * mimeData, const QModelIndex row,
deletesList.append(origMdlIdx); deletesList.append(origMdlIdx);
removeModelFromCutList(origMdlIdx); removeModelFromCutList(origMdlIdx);
} }
radioData.addLabelsFromModels();
} }
//qDebug().nospace() << "i: " << i << "; modelIdx:" << modelIdx << "; origMdlIdx: " << origMdlIdx << "; doMove: " << doMove << "; inserts:" << inserts << "; deletes: " << deletesList; //qDebug().nospace() << "i: " << i << "; modelIdx:" << modelIdx << "; origMdlIdx: " << origMdlIdx << "; doMove: " << doMove << "; inserts:" << inserts << "; deletes: " << deletesList;
+7 -10
View File
@@ -283,20 +283,17 @@ bool LabelsStorageFormat::loadYaml(RadioData & radioData)
radioData.generalSettings.currModelIndex = modelIdx; radioData.generalSettings.currModelIndex = modelIdx;
} }
if (hasLabels) {
QStringList labels = QString(model.labels).split(',',QString::SkipEmptyParts);
foreach(QString label, labels) {
radioData.addLabel(label);
}
}
model.used = true; model.used = true;
modelIdx++; modelIdx++;
} }
// If no labels, add a Favorites // Add the labels in the models
if(hasLabels && !radioData.labels.size()) { if (hasLabels) {
radioData.labels.append(tr("Favorites")); radioData.addLabelsFromModels();
// If no labels, add a Favorites
if(!radioData.labels.size())
radioData.labels.append(tr("Favorites"));
} }
return true; return true;