feat(color): New label based Model Selector (#1374)
* Model Labels Selector * Portrait Layout * Receiver ID Check * Fix Labels .yml format * Companion Initial Commit commit 967ddf3979b4954402fe62c858faba4b7a416bba Author: dlktdr <cblackburn320@gmail.com> Date: Mon Aug 29 01:49:15 2022 -0700 Fix Labels .yml format commit 534221cad9e01146c47a9ac1080a655e04f1f6c8 Author: dlktdr <cblackburn320@gmail.com> Date: Mon Aug 29 01:20:57 2022 -0700 Receiver ID Check commit f32c0939d44e9ae60bc0705a9176354c23fec198 Author: dlktdr <cblackburn320@gmail.com> Date: Sun Aug 28 23:13:25 2022 -0700 Portrait Layout commit 961181217c7841ff3f21299ac6aa419b810f80e9 Author: dlktdr <cblackburn320@gmail.com> Date: Sat Dec 11 16:56:13 2021 -0800 Model Labels Selector commitdc99dd40d2Author: David PHAM-VAN <1387855+DavBfr@users.noreply.github.com> Date: Fri Aug 26 03:04:34 2022 -0300 feat: Allow Lua widget to override built-in widget with same name (#2208) * Allow CPP Widgets Override with LUA * Reset a widget option it the type differs commit4b747caff9Author: eshifri <46511024+eshifri@users.noreply.github.com> Date: Thu Aug 25 19:47:53 2022 -0700 fix: Write model name into models.yml when duplicating model (#2224) * Update model name in the models.yml file (#2214) * Fix the length of the name. commite97f23a305Author: eshifri <46511024+eshifri@users.noreply.github.com> Date: Wed Aug 24 21:19:52 2022 -0700 fix(cpn): Fix full path to model notes for Simulator (#2223) * Correct Highlighted Column * labels .yml indentation * Update da.h New defines translated to Danish * Update dataconstants.h Omit warnings due to difference of LEN_MODEL_FILENAME and LEN_MODEL_NAME in modellist.cpp * Update modelslist.cpp Wonder if this are the right file in PR #1374 (?), seems a lot are changed to the better in #1374 * Update dataconstants.h Restore #define for LEN_MODEL_FILENAME to 16 * Rebase, Correct Issues Delete now deletes instead of select New model stores correct information Duplicate save all information in cell Rework items to use cell over button Reword function variables * CMPN: Add context menu Fix output not functional. Removed Yaml::null at some point :) Fix some compiler warnings * CMPN: Compiler warning, replace ,->_ in list * FW: Various Changes Change view to a grid Don't allow comma in label name Add scroll indicators Save sort order Co-authored-by: HThuren <99370924+HThuren@users.noreply.github.com>
This commit is contained in:
@@ -89,39 +89,23 @@ static void writeYamlToByteArray(const YAML::Node& node, QByteArray& data, bool
|
||||
qDebug() << data.toStdString().c_str();
|
||||
}
|
||||
|
||||
bool loadModelsListFromYaml(std::vector<CategoryData>& categories,
|
||||
bool loadLabelsListFromYaml(QStringList& labels,
|
||||
EtxModelfiles& modelFiles,
|
||||
const QByteArray& data)
|
||||
{
|
||||
labels.clear();
|
||||
if (data.size() == 0)
|
||||
return true;
|
||||
|
||||
YAML::Node node = loadYamlFromByteArray(data);
|
||||
if (!node.IsSequence()) return false;
|
||||
|
||||
int modelIdx = 0;
|
||||
for (const auto& cat : node) {
|
||||
if (!cat.IsMap()) continue;
|
||||
|
||||
for (const auto& cat_map : cat) {
|
||||
categories.push_back(cat_map.first.Scalar().c_str());
|
||||
|
||||
const auto& models = cat_map.second;
|
||||
if (!models.IsSequence()) continue;
|
||||
|
||||
for (const auto& model : models) {
|
||||
std::string filename, name;
|
||||
model["filename"] >> filename;
|
||||
model["name"] >> name;
|
||||
modelFiles.push_back(
|
||||
{filename, name, (int)categories.size() - 1, modelIdx++});
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!node.IsMap()) return false; // Root Map (Labels, Models)
|
||||
for (const auto& lbl : node["Labels"]) {
|
||||
labels.append(QString::fromStdString(lbl.first.as<std::string>()));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool loadModelFromYaml(ModelData& model, const QByteArray& data)
|
||||
{
|
||||
YAML::Node node = loadYamlFromByteArray(data);
|
||||
@@ -168,46 +152,12 @@ bool loadRadioSettingsFromYaml(GeneralSettings& settings, const QByteArray& data
|
||||
return true;
|
||||
}
|
||||
|
||||
// TODO:
|
||||
// 'modelFiles' should be ordered by Category index to avoid
|
||||
// turning the sequence into a map.
|
||||
//
|
||||
bool writeModelsListToYaml(const std::vector<CategoryData>& categories,
|
||||
const EtxModelfiles& modelFiles,
|
||||
QByteArray& data)
|
||||
bool writeLabelsListToYaml(const RadioData &radioData, QByteArray& data)
|
||||
{
|
||||
YAML::Node node;
|
||||
std::vector<CategoryData> cats = categories;
|
||||
std::vector<EtxModelMetadata> files = { modelFiles.begin(), modelFiles.end() };
|
||||
|
||||
std::stable_sort(files.begin(), files.end(),
|
||||
[](const EtxModelMetadata &a, const EtxModelMetadata &b) {
|
||||
return a.category < b.category;
|
||||
});
|
||||
|
||||
int catIdx = 0;
|
||||
for (const auto& cat : cats) {
|
||||
node[catIdx++][cat.name] = YAML::Node();
|
||||
foreach(QString label, radioData.labels) {
|
||||
node["Labels"][label.toStdString()] = YAML::Null;
|
||||
}
|
||||
|
||||
for (const auto& modelFile: modelFiles) {
|
||||
|
||||
YAML::Node cat_attrs;
|
||||
cat_attrs["filename"] = modelFile.filename;
|
||||
cat_attrs["name"] = modelFile.name;
|
||||
|
||||
catIdx = modelFile.category;
|
||||
if (catIdx >= (int)cats.size()) {
|
||||
catIdx = 0;
|
||||
if (cats.size() == 0) {
|
||||
cats.push_back("Models");
|
||||
}
|
||||
}
|
||||
|
||||
const std::string cat_name = cats[catIdx].name;
|
||||
node[catIdx][cat_name].push_back(cat_attrs);
|
||||
}
|
||||
|
||||
writeYamlToByteArray(node, data);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -32,22 +32,19 @@ constexpr unsigned int CPN_CURRENT_SETTINGS_VERSION = { 221 };
|
||||
struct EtxModelMetadata {
|
||||
std::string filename;
|
||||
std::string name;
|
||||
int category;
|
||||
int modelIdx;
|
||||
};
|
||||
|
||||
typedef std::list<EtxModelMetadata> EtxModelfiles;
|
||||
|
||||
bool loadModelsListFromYaml(std::vector<CategoryData>& categories,
|
||||
bool loadLabelsListFromYaml(QStringList& labels,
|
||||
EtxModelfiles& modelFiles,
|
||||
const QByteArray& data);
|
||||
|
||||
bool loadModelFromYaml(ModelData& model, const QByteArray& data);
|
||||
bool loadRadioSettingsFromYaml(GeneralSettings& settings, const QByteArray& data);
|
||||
|
||||
bool writeModelsListToYaml(const std::vector<CategoryData>& categories,
|
||||
const EtxModelfiles& modelFiles,
|
||||
QByteArray& data);
|
||||
bool writeLabelsListToYaml(const RadioData &radioData, QByteArray& data);
|
||||
|
||||
bool writeModelToYaml(const ModelData& model, QByteArray& data);
|
||||
bool writeRadioSettingsToYaml(const GeneralSettings& settings, QByteArray& data);
|
||||
|
||||
@@ -743,6 +743,7 @@ Node convert<ModelData>::encode(const ModelData& rhs)
|
||||
Node header;
|
||||
header["name"] = rhs.name;
|
||||
header["bitmap"] = rhs.bitmap;
|
||||
header["labels"] = rhs.labels;
|
||||
|
||||
for (int i=0; i<CPN_MAX_MODULES; i++) {
|
||||
if (rhs.moduleData[i].protocol != PULSES_OFF) {
|
||||
@@ -986,6 +987,7 @@ bool convert<ModelData>::decode(const Node& node, ModelData& rhs)
|
||||
if (header.IsMap()) {
|
||||
header["name"] >> rhs.name;
|
||||
header["bitmap"] >> rhs.bitmap;
|
||||
header["labels"] >> rhs.labels;
|
||||
header["modelId"] >> modelIds;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,7 +153,7 @@ enum Capability {
|
||||
HasMixerExpo,
|
||||
HasBatMeterRange,
|
||||
DangerousFunctions,
|
||||
HasModelCategories,
|
||||
HasModelLabels,
|
||||
HasSwitchableJack,
|
||||
HasSportConnector,
|
||||
PwrButtonPress,
|
||||
|
||||
@@ -111,9 +111,9 @@ class ModelData {
|
||||
|
||||
char semver[8 + 1];
|
||||
bool used;
|
||||
int category;
|
||||
char name[15+1];
|
||||
char filename[16+1];
|
||||
char labels[100];
|
||||
int modelIndex; // Companion only, temporary index position managed by data model.
|
||||
|
||||
TimerData timers[CPN_MAX_TIMERS];
|
||||
|
||||
@@ -742,7 +742,7 @@ int OpenTxFirmware::getCapability(::Capability capability)
|
||||
return (IS_HORUS_OR_TARANIS(board) ? true : id.contains("battgraph"));
|
||||
case DangerousFunctions:
|
||||
return id.contains("danger") ? 1 : 0;
|
||||
case HasModelCategories:
|
||||
case HasModelLabels:
|
||||
return IS_FAMILY_HORUS_OR_T16(board);
|
||||
case HasSwitchableJack:
|
||||
return IS_TARANIS_XLITES(board);
|
||||
|
||||
@@ -94,13 +94,6 @@ void RadioData::convert(RadioDataConversionState & cstate)
|
||||
models[i].convert(cstate.withModelIndex(i));
|
||||
}
|
||||
|
||||
if (categories.size() == 0) {
|
||||
categories.push_back(CategoryData(qPrintable(tr("Models"))));
|
||||
for (unsigned i=0; i<models.size(); i++) {
|
||||
models[i].category = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (IS_FAMILY_HORUS_OR_T16(cstate.toType)) {
|
||||
fixModelFilenames();
|
||||
}
|
||||
@@ -110,3 +103,104 @@ void RadioData::convert(RadioDataConversionState & cstate)
|
||||
models.resize(getCurrentFirmware()->getCapability(Models));
|
||||
}
|
||||
}
|
||||
|
||||
void RadioData::addLabel(QString label)
|
||||
{
|
||||
if(!labels.contains(label))
|
||||
labels.append(label);
|
||||
}
|
||||
|
||||
bool RadioData::deleteLabel(QString label)
|
||||
{
|
||||
bool deleted = false;
|
||||
// Remove labels in the models
|
||||
for(auto& model : models) {
|
||||
QStringList modelLabels = QString(model.labels).split(',',QString::SkipEmptyParts);
|
||||
if(modelLabels.contains(label))
|
||||
deleted = true;
|
||||
modelLabels.removeAll(label);
|
||||
strcpy(model.labels, QString(modelLabels.join(',')).toLocal8Bit().data());
|
||||
}
|
||||
|
||||
// Remove the label from the global list
|
||||
labels.removeAll(label);
|
||||
|
||||
// If no labels remain, add a Favorites one
|
||||
if(!labels.size()) {
|
||||
addLabel(tr("Favorites"));
|
||||
}
|
||||
return deleted;
|
||||
}
|
||||
|
||||
bool RadioData::deleteLabel(int index)
|
||||
{
|
||||
if(index >= labels.size()) return false;
|
||||
QString modelLabel = labels.at(index);
|
||||
return deleteLabel(modelLabel);
|
||||
}
|
||||
|
||||
bool RadioData::renameLabel(QString from, QString to)
|
||||
{
|
||||
bool success = true;
|
||||
int lengthdiff = to.size() - from.size();
|
||||
|
||||
// Check that rename is possible, not too long
|
||||
for(auto& model : models) {
|
||||
if((int)strlen(model.labels) + lengthdiff > (int)sizeof(model.labels) - 1) {
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
if(success) {
|
||||
for(auto& model : models) {
|
||||
QStringList modelLabels = QString(model.labels).split(',',QString::SkipEmptyParts);
|
||||
int ind = modelLabels.indexOf(from); // TOOD: Make sure it will fit
|
||||
if(ind != -1) {
|
||||
modelLabels.replace(ind, to);
|
||||
strcpy(model.labels, QString(modelLabels.join(',')).toLocal8Bit().data());
|
||||
}
|
||||
}
|
||||
int ind = labels.indexOf(from);
|
||||
if(ind != -1) {
|
||||
labels.replace(ind, to);
|
||||
}
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
bool RadioData::renameLabel(int index, QString to)
|
||||
{
|
||||
if(index >= labels.size()) return false;
|
||||
QString from = labels.at(index);
|
||||
return renameLabel(from, to);
|
||||
}
|
||||
|
||||
bool RadioData::addLabelToModel(int index, QString label)
|
||||
{
|
||||
if(index >= models.size()) return false;
|
||||
|
||||
char *modelLabelCsv = models[index].labels;
|
||||
// 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)) {
|
||||
modelLabels.append(label);
|
||||
strcpy(models[index].labels, QString(modelLabels.join(',')).toLocal8Bit().data());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool RadioData::removeLabelFromModel(int index, QString label)
|
||||
{
|
||||
if(index >= models.size()) return false;
|
||||
|
||||
char *modelLabelCsv = models[index].labels;
|
||||
QStringList modelLabels = QString(modelLabelCsv).split(',',QString::SkipEmptyParts);
|
||||
if(modelLabels.contains(label)) {
|
||||
modelLabels.removeAll(label);
|
||||
strcpy(models[index].labels, QString(modelLabels.join(',')).toLocal8Bit().data());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "generalsettings.h"
|
||||
#include "modeldata.h"
|
||||
#include "labels.h"
|
||||
|
||||
#include "datahelpers.h" // required for getElementName
|
||||
|
||||
@@ -9,15 +10,6 @@
|
||||
|
||||
class RadioDataConversionState;
|
||||
|
||||
class CategoryData {
|
||||
public:
|
||||
CategoryData(const char * name) {
|
||||
memset(this->name, 0, sizeof(CategoryData::name));
|
||||
strncpy(this->name, name, sizeof(CategoryData::name)-1);
|
||||
}
|
||||
char name[15+1];
|
||||
};
|
||||
|
||||
class RadioData {
|
||||
Q_DECLARE_TR_FUNCTIONS(RadioData)
|
||||
|
||||
@@ -25,11 +17,19 @@ class RadioData {
|
||||
RadioData();
|
||||
|
||||
GeneralSettings generalSettings;
|
||||
std::vector<CategoryData> categories;
|
||||
QStringList labels;
|
||||
std::vector<ModelData> models;
|
||||
|
||||
void convert(RadioDataConversionState & cstate);
|
||||
|
||||
void addLabel(QString label);
|
||||
bool deleteLabel(QString label);
|
||||
bool deleteLabel(int index);
|
||||
bool renameLabel(QString from, QString to);
|
||||
bool renameLabel(int index, QString to);
|
||||
bool addLabelToModel(int index, QString label);
|
||||
bool removeLabelFromModel(int index, QString label);
|
||||
|
||||
void setCurrentModel(unsigned int index);
|
||||
void fixModelFilenames();
|
||||
QString getNextModelFilename();
|
||||
|
||||
Reference in New Issue
Block a user