Proposition : model number handling (#4308)
* Try something for discussions * Better alternative * Typo * Improve * Change delimiter * Move delimiter to space * * Refactoring of CategorizedStorageFormat::load() and CategorizedStorageFormat::save() to support unused model slots * Fix all unsafe usages of QString::toStdString().c_str() * Fixed radio data import [Horus] (broken in0f90ff0b65) (cherry picked from commitcc12601a01) * Added Q_OBJECT to TreeModel and some cosmetics (cherry picked from commitf40e64c2aa) * Cleanup
This commit is contained in:
@@ -147,6 +147,7 @@ enum Capability {
|
|||||||
MixersMonitor,
|
MixersMonitor,
|
||||||
HasBatMeterRange,
|
HasBatMeterRange,
|
||||||
DangerousFunctions,
|
DangerousFunctions,
|
||||||
|
HasModelCategories
|
||||||
};
|
};
|
||||||
|
|
||||||
class SimulatorInterface;
|
class SimulatorInterface;
|
||||||
|
|||||||
@@ -763,6 +763,8 @@ int OpenTxFirmware::getCapability(Capability capability)
|
|||||||
return (IS_HORUS_OR_TARANIS(board) ? true : id.contains("battgraph"));
|
return (IS_HORUS_OR_TARANIS(board) ? true : id.contains("battgraph"));
|
||||||
case DangerousFunctions:
|
case DangerousFunctions:
|
||||||
return id.contains("danger") ? 1 : 0;
|
return id.contains("danger") ? 1 : 0;
|
||||||
|
case HasModelCategories:
|
||||||
|
return IS_HORUS(board);
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -422,7 +422,7 @@ void MdiChild::modelAdd()
|
|||||||
model.category = categoryIndex;
|
model.category = categoryIndex;
|
||||||
model.used = true;
|
model.used = true;
|
||||||
sprintf(model.filename, "model%lu.bin", (long unsigned)radioData.models.size()+1);
|
sprintf(model.filename, "model%lu.bin", (long unsigned)radioData.models.size()+1);
|
||||||
strcpy(model.name, tr("New model").toStdString().c_str());
|
strcpy(model.name, qPrintable(tr("New model")));
|
||||||
radioData.models.push_back(model);
|
radioData.models.push_back(model);
|
||||||
radioData.setCurrentModel(radioData.models.size() - 1);
|
radioData.setCurrentModel(radioData.models.size() - 1);
|
||||||
setModified();
|
setModified();
|
||||||
|
|||||||
@@ -473,7 +473,7 @@ void ModelsListWidget::duplicate()
|
|||||||
while (i<getCurrentFirmware()->getCapability(Models)) {
|
while (i<getCurrentFirmware()->getCapability(Models)) {
|
||||||
if (radioData->models[i].isEmpty()) {
|
if (radioData->models[i].isEmpty()) {
|
||||||
radioData->models[i] = *model;
|
radioData->models[i] = *model;
|
||||||
strcpy(radioData->models[i].filename, radioData->getNextModelFilename().toStdString().c_str());
|
strcpy(radioData->models[i].filename, qPrintable(radioData->getNextModelFilename()));
|
||||||
((MdiChild *)parent())->setModified();
|
((MdiChild *)parent())->setModified();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -96,9 +96,8 @@ void RadioData::convert(Board::Type before, Board::Type after)
|
|||||||
for (unsigned i=0; i<models.size(); i++) {
|
for (unsigned i=0; i<models.size(); i++) {
|
||||||
models[i].convert(before, after);
|
models[i].convert(before, after);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (categories.size() == 0) {
|
if (categories.size() == 0) {
|
||||||
categories.push_back(CategoryData(QObject::tr("Models").toStdString().c_str()));
|
categories.push_back(CategoryData(qPrintable(QObject::tr("Models"))));
|
||||||
for (unsigned i=0; i<models.size(); i++) {
|
for (unsigned i=0; i<models.size(); i++) {
|
||||||
models[i].category = 0;
|
models[i].category = 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,38 +45,70 @@ bool CategorizedStorageFormat::load(RadioData & radioData)
|
|||||||
QList<QByteArray> lines = modelsListBuffer.split('\n');
|
QList<QByteArray> lines = modelsListBuffer.split('\n');
|
||||||
int modelIndex = 0;
|
int modelIndex = 0;
|
||||||
int categoryIndex = -1;
|
int categoryIndex = -1;
|
||||||
foreach (const QByteArray & line, lines) {
|
foreach (const QByteArray & lineArray, lines) {
|
||||||
if (!line.isEmpty()) {
|
QString line = QString(lineArray).trimmed();
|
||||||
if (line.startsWith('[') && line.endsWith(']')) {
|
if (line.isEmpty()) continue;
|
||||||
|
// qDebug() << "parsing line" << line;
|
||||||
|
|
||||||
|
if (line.startsWith('[') && line.endsWith(']')) {
|
||||||
|
// ignore categories for boards that do not support them
|
||||||
|
if (getCurrentFirmware()->getCapability(HasModelCategories)) {
|
||||||
QString name = line.mid(1, line.size() - 2);
|
QString name = line.mid(1, line.size() - 2);
|
||||||
CategoryData category(name.toStdString().c_str());
|
CategoryData category(qPrintable(name));
|
||||||
radioData.categories.push_back(category);
|
radioData.categories.push_back(category);
|
||||||
categoryIndex++;
|
categoryIndex++;
|
||||||
|
qDebug() << "added category" << name;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// determine if we have a model number
|
||||||
|
QStringList parts = line.split(QRegExp("\\s+"), QString::SkipEmptyParts);
|
||||||
|
if (parts.size() == 2) {
|
||||||
|
// parse model number
|
||||||
|
int modelNumber = parts[0].toInt();
|
||||||
|
if (modelNumber > 0 && modelNumber > modelIndex && modelNumber < getCurrentFirmware()->getCapability(Models)) {
|
||||||
|
modelIndex = modelNumber;
|
||||||
|
qDebug() << "advanced model number to" << modelIndex;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
qDebug() << "Loading" << line;
|
if (modelNumber != modelIndex) qDebug() << "Invalid model number" << parts[0];
|
||||||
QByteArray modelBuffer;
|
|
||||||
if (!loadFile(modelBuffer, QString("MODELS/%1").arg(QString(line)))) {
|
|
||||||
setError(QObject::tr("Can't extract %1").arg(QString(line)));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if ((int)radioData.models.size() <= modelIndex) {
|
|
||||||
radioData.models.resize(modelIndex + 1);
|
|
||||||
}
|
|
||||||
if (!loadModelFromByteArray(radioData.models[modelIndex], modelBuffer)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
strncpy(radioData.models[modelIndex].filename, line.data(), sizeof(radioData.models[modelIndex].filename));
|
|
||||||
if (strcmp(radioData.generalSettings.currModelFilename, line.data()) == 0) {
|
|
||||||
radioData.generalSettings.currModelIndex = modelIndex;
|
|
||||||
}
|
|
||||||
radioData.models[modelIndex].category = categoryIndex;
|
|
||||||
radioData.models[modelIndex].used = true;
|
|
||||||
modelIndex++;
|
|
||||||
}
|
}
|
||||||
|
parts.removeFirst();
|
||||||
|
}
|
||||||
|
if (parts.size() == 1) {
|
||||||
|
// parse model file name and load
|
||||||
|
QString fileName = parts[0];
|
||||||
|
qDebug() << "Loading model from file" << fileName << "into slot" << modelIndex;
|
||||||
|
QByteArray modelBuffer;
|
||||||
|
if (!loadFile(modelBuffer, QString("MODELS/%1").arg(fileName))) {
|
||||||
|
setError(QObject::tr("Can't extract %1").arg(fileName));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if ((int)radioData.models.size() <= modelIndex) {
|
||||||
|
radioData.models.resize(modelIndex + 1);
|
||||||
|
}
|
||||||
|
if (!loadModelFromByteArray(radioData.models[modelIndex], modelBuffer)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
strncpy(radioData.models[modelIndex].filename, qPrintable(fileName), sizeof(radioData.models[modelIndex].filename));
|
||||||
|
if (IS_HORUS(board) && !strcmp(radioData.generalSettings.currModelFilename, qPrintable(fileName))) {
|
||||||
|
radioData.generalSettings.currModelIndex = modelIndex;
|
||||||
|
qDebug() << "currModelIndex =" << modelIndex;
|
||||||
|
}
|
||||||
|
if (getCurrentFirmware()->getCapability(HasModelCategories)) {
|
||||||
|
radioData.models[modelIndex].category = categoryIndex;
|
||||||
|
}
|
||||||
|
radioData.models[modelIndex].used = true;
|
||||||
|
modelIndex++;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
// invalid line
|
||||||
|
// TODO add to parsing report
|
||||||
|
qDebug() << "Invalid line" <<line;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,19 +128,28 @@ bool CategorizedStorageFormat::write(const RadioData & radioData)
|
|||||||
// all models
|
// all models
|
||||||
for (unsigned int i=0; i<radioData.models.size(); i++) {
|
for (unsigned int i=0; i<radioData.models.size(); i++) {
|
||||||
const ModelData & model = radioData.models[i];
|
const ModelData & model = radioData.models[i];
|
||||||
if (!model.isEmpty()) {
|
if (model.isEmpty()) continue;
|
||||||
QString modelFilename = QString("MODELS/%1").arg(model.filename);
|
|
||||||
QByteArray modelData;
|
QString modelFilename = QString("MODELS/%1").arg(model.filename);
|
||||||
writeModelToByteArray(model, modelData);
|
QByteArray modelData;
|
||||||
if (!writeFile(modelData, modelFilename)) {
|
writeModelToByteArray(model, modelData);
|
||||||
return false;
|
if (!writeFile(modelData, modelFilename)) {
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
|
if (getCurrentFirmware()->getCapability(HasModelCategories)) {
|
||||||
int categoryIndex = model.category;
|
int categoryIndex = model.category;
|
||||||
if (currentCategoryIndex != categoryIndex) {
|
if (currentCategoryIndex != categoryIndex) {
|
||||||
modelsList.append(QString().sprintf("[%s]\n", radioData.categories[model.category].name));
|
modelsList.append(QString().sprintf("[%s]\n", radioData.categories[model.category].name));
|
||||||
currentCategoryIndex = categoryIndex;
|
currentCategoryIndex = categoryIndex;
|
||||||
}
|
}
|
||||||
modelsList.append(QString(model.filename) + "\n");
|
}
|
||||||
|
if (IS_HORUS(getCurrentBoard())) {
|
||||||
|
modelsList.append(QString("%1\n").arg(model.filename));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// use format with model number and file name
|
||||||
|
// this is needed, because this kind of radios can have unused model slots
|
||||||
|
modelsList.append(QString("%1 %2\n").arg(i).arg(model.filename));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ bool OtxFormat::write(const RadioData & radioData)
|
|||||||
bool OtxFormat::loadFile(QByteArray & filedata, const QString & filename)
|
bool OtxFormat::loadFile(QByteArray & filedata, const QString & filename)
|
||||||
{
|
{
|
||||||
size_t size;
|
size_t size;
|
||||||
void * data = mz_zip_reader_extract_file_to_heap(&zip_archive, filename.toStdString().c_str(), &size, 0);
|
void * data = mz_zip_reader_extract_file_to_heap(&zip_archive, qPrintable(filename), &size, 0);
|
||||||
if (!data) {
|
if (!data) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user