Skip to content

Commit

Permalink
Modify validation functions and when called
Browse files Browse the repository at this point in the history
  • Loading branch information
elecpower committed Jan 23, 2025
1 parent 1c0202e commit 575d379
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 24 deletions.
28 changes: 10 additions & 18 deletions companion/src/firmwares/modeldata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1927,36 +1927,29 @@ int ModelData::getCustomScreensCount() const
return cnt;
}

bool ModelData::isValid(QWidget * parent)
void ModelData::validate()
{
int inputerrors = 0;
modelErrors = false;

for (int i = 0; i < CPN_MAX_INPUTS; i++) {
if (!expoData[i].isEmpty() && expoData[i].srcRaw == SOURCE_TYPE_NONE)
inputerrors++;
modelErrors = true;
return;
}

int mixerrors = 0;
for (int i = 0; i < CPN_MAX_MIXERS; i++) {
if (!mixData[i].isEmpty() && mixData[i].srcRaw == SOURCE_TYPE_NONE)
mixerrors++;
}

if (inputerrors > 0 || mixerrors > 0) {
QString msg = tr("Model %1 has the following errors:").arg(name) % "\n\n";
if (inputerrors > 0)
msg.append(tr("- %1 inputs with no source").arg(inputerrors) % "\n");
if (mixerrors > 0)
msg.append(tr("- %1 mixes with no source").arg(mixerrors));

QMessageBox::information(parent, CPN_STR_APP_NAME, msg);
return false;
modelErrors = true;
return;
}

return true;
}

void ModelData::fixErrors()
{
if (!modelErrors)
return;

for (int i = 0; i < CPN_MAX_INPUTS; i++) {
if (!expoData[i].isEmpty() && expoData[i].srcRaw == SOURCE_TYPE_NONE) {
// delete input line and fix up anything else then update refs
Expand All @@ -1969,5 +1962,4 @@ void ModelData::fixErrors()
// delete mixer line and fix up anything like numbering??
}
}

}
5 changes: 4 additions & 1 deletion companion/src/firmwares/modeldata.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class ModelData {
char labels[100];
int modelIndex; // Companion only, temporary index position managed by data model.
bool modelUpdated; // Companion only, used to highlight if changed in models list
bool modelErrors; // Companion only, used to highlight if data errors in models list

TimerData timers[CPN_MAX_TIMERS];
bool noGlobalFunctions;
Expand Down Expand Up @@ -355,8 +356,10 @@ class ModelData {
static AbstractStaticItemModel * funcSwitchStartItemModel();

int getCustomScreensCount() const;
bool isValid(QWidget * parent = nullptr);
void fixErrors();
bool hasErrors() { return modelErrors; }
bool isValid() { return !hasErrors(); }
void validate();

protected:
void removeGlobalVar(int & var);
Expand Down
10 changes: 8 additions & 2 deletions companion/src/firmwares/radiodata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,12 +344,18 @@ AbstractStaticItemModel * RadioData::modelSortOrderItemModel()
return mdl;
}

int RadioData::invalidModels(QWidget * parent)
void RadioData::validateModels()
{
for(auto &model: models)
model.validate();
}

int RadioData::invalidModels()
{
int cnt = 0;

for(auto &model: models)
cnt += model.isValid(parent) ? 0 : 1;
cnt += model.isValid() ? 0 : 1;

return cnt;
}
Expand Down
3 changes: 2 additions & 1 deletion companion/src/firmwares/radiodata.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ class RadioData {
void setCurrentModel(unsigned int index);
void fixModelFilenames();
QString getNextModelFilename();
int invalidModels(QWidget * parent = nullptr);
void validateModels();
int invalidModels();
void fixInvalidModels();

static QString modelSortOrderToString(int value);
Expand Down
9 changes: 7 additions & 2 deletions companion/src/mdichild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,8 @@ bool MdiChild::loadFile(const QString & filename, bool resetCurrentFile)
refresh();
}

radioData.validateModels();

return true;
}

Expand Down Expand Up @@ -1331,7 +1333,8 @@ bool MdiChild::saveAs(bool isNew)

bool MdiChild::saveFile(const QString & filename, bool setCurrent)
{
int cnt = radioData.invalidModels(this);
// do not check for etx files as we accept errors
int cnt = radioData.invalidModels();
if (cnt) {
if (askQuestion(tr("%1 models have errors. Repair?").arg(cnt), QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel) == QMessageBox::Yes) {
radioData.fixInvalidModels();
Expand All @@ -1353,8 +1356,10 @@ bool MdiChild::saveFile(const QString & filename, bool setCurrent)
g.eepromDir(QFileInfo(filename).dir().absolutePath());

for (int i = 0; i < (int)radioData.models.size(); i++) {
if (!radioData.models[i].isEmpty())
if (!radioData.models[i].isEmpty()) {
radioData.models[i].modelUpdated = false;
radioData.models[i].validate();
}
}

refresh();
Expand Down
6 changes: 6 additions & 0 deletions companion/src/modelslist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ QVariant ModelsListModel::data(const QModelIndex & index, int role) const
}

if (role == Qt::ForegroundRole && item->isModel()) {
if (index.column() == (hasLabels ? 0 : 1) && radioData->models[item->getModelIndex()].modelErrors) {
QBrush brush;
brush.setColor(Qt::red);
return brush;
}

int col = item->columnCount() - 1;
if(hasLabels)
col --;
Expand Down

0 comments on commit 575d379

Please sign in to comment.