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:
@@ -63,7 +63,8 @@
|
||||
using namespace Gui;
|
||||
using namespace Gui::Dialog;
|
||||
namespace fs = std::filesystem;
|
||||
using namespace Base;
|
||||
using Base::UnitsApi;
|
||||
using Base::QuantityFormat;
|
||||
|
||||
/* TRANSLATOR Gui::Dialog::DlgSettingsGeneral */
|
||||
|
||||
@@ -117,14 +118,14 @@ DlgSettingsGeneral::DlgSettingsGeneral( QWidget* parent )
|
||||
connect(ui->comboBox_UnitSystem, qOverload<int>(&QComboBox::currentIndexChanged), this, &DlgSettingsGeneral::onUnitSystemIndexChanged);
|
||||
ui->spinBoxDecimals->setMaximum(std::numeric_limits<double>::digits10 + 1);
|
||||
|
||||
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));
|
||||
ui->comboBox_UnitSystem->addItem(item, i);
|
||||
}
|
||||
auto addItem = [&, index {0}](const std::string& item) mutable {
|
||||
ui->comboBox_UnitSystem->addItem(QString::fromStdString(item), index++);
|
||||
};
|
||||
auto descriptions = UnitsApi::getDescriptions();
|
||||
std::for_each(descriptions.begin(), descriptions.end(), addItem);
|
||||
|
||||
// Enable/disable the fractional inch option depending on system
|
||||
const auto visible = (UnitsApi::getSchema() == UnitSystem::ImperialBuilding);
|
||||
const auto visible = UnitsApi::isMultiUnitLength();
|
||||
ui->comboBox_FracInch->setVisible(visible);
|
||||
ui->fractionalInchLabel->setVisible(visible);
|
||||
}
|
||||
@@ -204,7 +205,7 @@ void DlgSettingsGeneral::saveUnitSystemSettings()
|
||||
hGrpu->SetBool("IgnoreProjectSchema", ui->checkBox_projectUnitSystemIgnore->isChecked());
|
||||
|
||||
// Set actual value
|
||||
Base::UnitsApi::setDecimals(ui->spinBoxDecimals->value());
|
||||
UnitsApi::setDecimals(ui->spinBoxDecimals->value());
|
||||
|
||||
// Convert the combobox index to the its integer denominator. Currently
|
||||
// with 1/2, 1/4, through 1/128, this little equation directly computes the
|
||||
@@ -218,21 +219,21 @@ void DlgSettingsGeneral::saveUnitSystemSettings()
|
||||
hGrpu->SetInt("FracInch", FracInch);
|
||||
|
||||
// Set the actual format value
|
||||
Base::QuantityFormat::setDefaultDenominator(FracInch);
|
||||
QuantityFormat::setDefaultDenominator(FracInch);
|
||||
|
||||
// Set and save the Unit System
|
||||
if (ui->checkBox_projectUnitSystemIgnore->isChecked()) {
|
||||
// currently selected View System (unit system)
|
||||
int viewSystemIndex = ui->comboBox_UnitSystem->currentIndex();
|
||||
UnitsApi::setSchema(static_cast<UnitSystem>(viewSystemIndex));
|
||||
UnitsApi::setSchema(viewSystemIndex);
|
||||
}
|
||||
else if (App::Document* doc = App::GetApplication().getActiveDocument()) {
|
||||
UnitsApi::setSchema(static_cast<UnitSystem>(doc->UnitSystem.getValue()));
|
||||
UnitsApi::setSchema(doc->UnitSystem.getValue());
|
||||
}
|
||||
else {
|
||||
// if there is no existing document then the unit must still be set
|
||||
int viewSystemIndex = ui->comboBox_UnitSystem->currentIndex();
|
||||
UnitsApi::setSchema(static_cast<UnitSystem>(viewSystemIndex));
|
||||
UnitsApi::setSchema(viewSystemIndex);
|
||||
}
|
||||
|
||||
ui->SubstituteDecimal->onSave();
|
||||
@@ -287,11 +288,11 @@ void DlgSettingsGeneral::loadSettings()
|
||||
ParameterGrp::handle hGrpu = App::GetApplication().GetParameterGroupByPath
|
||||
("User parameter:BaseApp/Preferences/Units");
|
||||
ui->comboBox_UnitSystem->setCurrentIndex(hGrpu->GetInt("UserSchema", 0));
|
||||
ui->spinBoxDecimals->setValue(hGrpu->GetInt("Decimals", Base::UnitsApi::getDecimals()));
|
||||
ui->spinBoxDecimals->setValue(hGrpu->GetInt("Decimals", UnitsApi::getDecimals()));
|
||||
ui->checkBox_projectUnitSystemIgnore->setChecked(hGrpu->GetBool("IgnoreProjectSchema", false));
|
||||
|
||||
// Get the current user setting for the minimum fractional inch
|
||||
FracInch = hGrpu->GetInt("FracInch", Base::QuantityFormat::getDefaultDenominator());
|
||||
FracInch = hGrpu->GetInt("FracInch", QuantityFormat::getDefaultDenominator());
|
||||
|
||||
// Convert fractional inch to the corresponding combobox index using this
|
||||
// handy little equation.
|
||||
@@ -529,11 +530,11 @@ void DlgSettingsGeneral::translateIconSizes()
|
||||
|
||||
void DlgSettingsGeneral::retranslateUnits()
|
||||
{
|
||||
int num = ui->comboBox_UnitSystem->count();
|
||||
for (int i = 0; i < num; i++) {
|
||||
QString item = Base::UnitsApi::getDescription(static_cast<Base::UnitSystem>(i));
|
||||
ui->comboBox_UnitSystem->setItemText(i, item);
|
||||
}
|
||||
auto setItem = [&, index {0}](const std::string& item) mutable {
|
||||
ui->comboBox_UnitSystem->setItemText(index++, QString::fromStdString(item));
|
||||
};
|
||||
const auto descriptions = UnitsApi::getDescriptions();
|
||||
std::for_each(descriptions.begin(), descriptions.end(), setItem);
|
||||
}
|
||||
|
||||
void DlgSettingsGeneral::changeEvent(QEvent *event)
|
||||
@@ -761,13 +762,14 @@ void DlgSettingsGeneral::onLoadPreferencePackClicked(const std::string& packName
|
||||
}
|
||||
}
|
||||
|
||||
void DlgSettingsGeneral::onUnitSystemIndexChanged(int index)
|
||||
void DlgSettingsGeneral::onUnitSystemIndexChanged(const int index)
|
||||
{
|
||||
if (index < 0)
|
||||
return; // happens when clearing the combo box in retranslateUi()
|
||||
if (index < 0) {
|
||||
return; // happens when clearing the combo box in retranslateUi()
|
||||
}
|
||||
|
||||
// Enable/disable the fractional inch option depending on system
|
||||
const auto visible = (static_cast<UnitSystem>(index) == UnitSystem::ImperialBuilding);
|
||||
const auto visible = UnitsApi::isMultiUnitLength();
|
||||
ui->comboBox_FracInch->setVisible(visible);
|
||||
ui->fractionalInchLabel->setVisible(visible);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user