Base: simplify UnitsSchemas management

Fixes: Maintaining schemas is difficult and error-prone

- Facilitate easy schemas add, remove, change, etc.
- Remove 14 files containing approx 2,190 lines of if/else code and data
- Place data in one file (UnitsSchemasData.h) using a normalized structure (including special functions)
- Isolate and simplify data operations (code)
- Remove schemas enum to keep data independent of code
- Separate responsibilities: Specifications, data, schemas, schema
- Add schema data 'isDefault'
- Add schema data name
- Prefer algorithms to raw loops
- Add schemas unit tests
- Tweak quantity unit tests
This commit is contained in:
bofdahof
2025-01-22 09:40:00 +10:00
committed by Ladislav Michl
parent ef6f60510a
commit 1155f0d752
39 changed files with 1983 additions and 2928 deletions

View File

@@ -33,6 +33,7 @@
#include <QWidget>
#endif
#include <algorithm>
#include "GeneralSettingsWidget.h"
#include <gsl/pointers>
#include <App/Application.h>
@@ -182,7 +183,7 @@ void GeneralSettingsWidget::onUnitSystemChanged(int index)
if (index < 0) {
return; // happens when clearing the combo box in retranslateUi()
}
Base::UnitsApi::setSchema(static_cast<Base::UnitSystem>(index));
Base::UnitsApi::setSchema(index);
ParameterGrp::handle hGrp =
App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Units");
hGrp->SetInt("UserSchema", index);
@@ -213,15 +214,16 @@ void GeneralSettingsWidget::retranslateUi()
_unitSystemLabel->setText(createLabelText(tr("Unit System")));
_unitSystemComboBox->clear();
ParameterGrp::handle hGrpUnits =
auto addItem = [&, index {0}](const std::string& item) mutable {
_unitSystemComboBox->addItem(QString::fromStdString(item), index++);
};
auto descriptions = Base::UnitsApi::getDescriptions();
std::for_each(descriptions.begin(), descriptions.end(), addItem);
const ParameterGrp::handle hGrpUnits =
App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Units");
auto userSchema = hGrpUnits->GetInt("UserSchema", 0);
int num = static_cast<int>(Base::UnitSystem::NumUnitSystemTypes);
for (int i = 0; i < num; i++) {
QString item = Base::UnitsApi::getDescription(static_cast<Base::UnitSystem>(i));
_unitSystemComboBox->addItem(item, i);
}
_unitSystemComboBox->setCurrentIndex(userSchema);
_unitSystemComboBox->setCurrentIndex(static_cast<int>(hGrpUnits->GetInt("UserSchema", 0)));
_navigationStyleLabel->setText(createLabelText(tr("Navigation Style")));
_navigationStyleComboBox->clear();