fix(cpn): Preserve sort order and selected labels (#3580)

* fix(cpn): read/write models Sort Order (based on work by dlktdr)

* fix(cpn): read and write labels and selected
This commit is contained in:
Neil Horne
2023-05-23 13:41:41 +10:00
committed by GitHub
parent 48e5d141aa
commit ad5d8e190e
6 changed files with 96 additions and 34 deletions
@@ -89,18 +89,37 @@ static void writeYamlToByteArray(const YAML::Node& node, QByteArray& data, bool
qDebug() << data.toStdString().c_str();
}
bool loadLabelsListFromYaml(QStringList& labels,
bool loadLabelsListFromYaml(RadioData::ModelLabels& labels,
int& sortOrder,
EtxModelfiles& modelFiles,
const QByteArray& data)
{
sortOrder = 0;
labels.clear();
if (data.size() == 0)
return true;
YAML::Node node = loadYamlFromByteArray(data);
if (!node.IsMap()) return false; // Root Map (Labels, Models)
for (const auto& lbl : node["Labels"]) {
labels.append(QString::fromStdString(lbl.first.as<std::string>()));
}
if (!node.IsMap()) return false; // Root Map (Labels, Sort, Models)
if (node["Sort"].IsScalar()) {
bool ok = false;
sortOrder = QString::fromStdString(node["Sort"].Scalar()).toUInt(&ok);
if (!ok)
sortOrder = 0;
}
YAML::Node lbls = node["Labels"];
if (lbls.IsMap()) {
for (YAML::const_iterator it=lbls.begin(); it!=lbls.end(); ++it) {
std::string lbl = it->first.as<std::string>();
RadioData::LabelData ld;
ld.name = QString::fromStdString(lbl);
if (lbls[lbl]["selected"])
ld.selected = lbls[lbl]["selected"].as<bool>();
else
ld.selected = false;
labels.append(ld);
}
}
return true;
}
@@ -155,9 +174,12 @@ bool loadRadioSettingsFromYaml(GeneralSettings& settings, const QByteArray& data
bool writeLabelsListToYaml(const RadioData &radioData, QByteArray& data)
{
YAML::Node node;
foreach(QString label, radioData.labels) {
node["Labels"][label.toStdString()] = YAML::Null;
foreach(RadioData::LabelData label, radioData.labels) {
node["Labels"][label.name.toStdString()] = YAML::Null;
if (label.selected)
node["Labels"][label.name.toStdString()]["selected"] = true;
}
node["Sort"] = radioData.sortOrder;
writeYamlToByteArray(node, data);
return true;
}
@@ -37,7 +37,8 @@ struct EtxModelMetadata {
typedef std::list<EtxModelMetadata> EtxModelfiles;
bool loadLabelsListFromYaml(QStringList& labels,
bool loadLabelsListFromYaml(RadioData::ModelLabels& labels,
int& sortOrder,
EtxModelfiles& modelFiles,
const QByteArray& data);
+29 -9
View File
@@ -22,7 +22,8 @@
#include "radiodataconversionstate.h"
#include "eeprominterface.h"
RadioData::RadioData()
RadioData::RadioData() :
sortOrder(0)
{
models.resize(getCurrentFirmware()->getCapability(Models));
}
@@ -120,8 +121,11 @@ void RadioData::addLabel(QString label)
output.truncate(truncateAt);
}
label = QString(output);
if (labels.indexOf(label) == -1)
labels.append(label);
if (indexOfLabel(label) < 0) {
LabelData ld = { label, false };
labels.append(ld);
}
}
bool RadioData::deleteLabel(QString label)
@@ -139,7 +143,9 @@ bool RadioData::deleteLabel(QString label)
}
// Remove the label from the global list
labels.removeAll(label);
int index = indexOfLabel(label);
if (index > -1)
labels.remove(index);
// If no labels remain, add a Favorites one
if (!labels.size()) {
@@ -151,7 +157,7 @@ bool RadioData::deleteLabel(QString label)
bool RadioData::deleteLabel(int index)
{
if (index >= labels.size()) return false;
QString modelLabel = labels.at(index);
QString modelLabel = labels.at(index).name;
return deleteLabel(modelLabel);
}
@@ -188,9 +194,9 @@ bool RadioData::renameLabel(QString from, QString to)
}
}
}
int ind = labels.indexOf(from);
int ind = indexOfLabel(from);
if (ind != -1) {
labels.replace(ind, to);
labels[ind].name = to;
}
}
return success;
@@ -199,7 +205,7 @@ bool RadioData::renameLabel(QString from, QString to)
bool RadioData::renameLabel(int index, QString to)
{
if (index >= labels.size()) return false;
QString from = labels.at(index);
QString from = labels.at(index).name;
return renameLabel(from, to);
}
@@ -211,7 +217,7 @@ void RadioData::swapLabel(int indFrom, int indTo)
indFrom < 0 ||
indTo < 0)
return;
QString tmplbl = labels.at(indFrom);
LabelData tmplbl = labels.at(indFrom);
labels.replace(indFrom, labels.at(indTo));
labels.replace(indTo, tmplbl);
}
@@ -258,6 +264,20 @@ void RadioData::addLabelsFromModels()
}
}
int RadioData::indexOfLabel(QString & label) const
{
int index = -1;
for (int i = 0; i < labels.size(); i++) {
if (labels.at(i).name == label) {
index = i;
break;
}
}
return index;
}
QStringList RadioData::fromCSV(const QString &csv)
{
QStringList lbls = QString(csv).split(',',QString::SkipEmptyParts);
+12 -1
View File
@@ -17,7 +17,17 @@ class RadioData {
RadioData();
GeneralSettings generalSettings;
QStringList labels;
struct LabelData {
QString name;
bool selected;
};
typedef QVector<LabelData> ModelLabels;
ModelLabels labels;
int sortOrder;
std::vector<ModelData> models;
void convert(RadioDataConversionState & cstate);
@@ -31,6 +41,7 @@ class RadioData {
bool addLabelToModel(int index, QString label);
bool removeLabelFromModel(int index, QString label);
void addLabelsFromModels();
int indexOfLabel(QString & label) const;
static QStringList fromCSV(const QString &csv);
static QString toCSV(QStringList lbls);