From aa5a2e1b3fc3882b4bb03a6343e3b8245a1e18e3 Mon Sep 17 00:00:00 2001 From: jriegel Date: Tue, 15 Apr 2014 18:23:23 +0200 Subject: [PATCH] New Unit-Schema ImperialDecimal --- src/Base/UnitsApi.cpp | 1 + src/Base/UnitsSchema.h | 3 +- src/Base/UnitsSchemaImperial1.cpp | 61 +++++++++++++++++++++++++++++++ src/Base/UnitsSchemaImperial1.h | 13 +++++++ src/Gui/DlgSettingsUnits.ui | 15 +++++--- src/Gui/DlgSettingsUnitsImp.cpp | 13 ------- src/Gui/DlgSettingsUnitsImp.h | 2 - 7 files changed, 87 insertions(+), 21 deletions(-) diff --git a/src/Base/UnitsApi.cpp b/src/Base/UnitsApi.cpp index 7b95925aa..20e460b66 100644 --- a/src/Base/UnitsApi.cpp +++ b/src/Base/UnitsApi.cpp @@ -88,6 +88,7 @@ void UnitsApi::setSchema(UnitSystem s) case SI1 : UserPrefSystem = new UnitsSchemaInternal(); break; case SI2 : UserPrefSystem = new UnitsSchemaMKS(); break; case Imperial1: UserPrefSystem = new UnitsSchemaImperial1(); break; + case ImperialDecimal: UserPrefSystem = new UnitsSchemaImperialDecimal(); break; } actSystem = s; UserPrefSystem->setSchemaUnits(); // if necesarry a unit schema can change the constants in Quantity (e.g. mi=1.8km rather then 1.6km). diff --git a/src/Base/UnitsSchema.h b/src/Base/UnitsSchema.h index 26ecc5ca7..012cd0917 100644 --- a/src/Base/UnitsSchema.h +++ b/src/Base/UnitsSchema.h @@ -38,7 +38,8 @@ namespace Base { enum UnitSystem { SI1 = 0 , /** internal (mm,kg,s) SI system (http://en.wikipedia.org/wiki/International_System_of_Units) */ SI2 = 1 , /** MKS (m,kg,s) SI system */ - Imperial1 = 2 /** the Imperial system (http://en.wikipedia.org/wiki/Imperial_units) */ + Imperial1 = 2, /** the Imperial system (http://en.wikipedia.org/wiki/Imperial_units) */ + ImperialDecimal = 3 /** Imperial with length in inch only */ } ; diff --git a/src/Base/UnitsSchemaImperial1.cpp b/src/Base/UnitsSchemaImperial1.cpp index d3a810500..44d6b50ee 100644 --- a/src/Base/UnitsSchemaImperial1.cpp +++ b/src/Base/UnitsSchemaImperial1.cpp @@ -27,6 +27,7 @@ #endif #include +#include #include "Exception.h" #include "UnitsApi.h" #include "UnitsSchemaImperial1.h" @@ -118,3 +119,63 @@ QString UnitsSchemaImperial1::schemaTranslate(Base::Quantity quant,double &facto } return QString::fromLatin1("%L1 %2").arg(quant.getValue() / factor).arg(unitString); } + + + + +QString UnitsSchemaImperialDecimal::schemaTranslate(Base::Quantity quant,double &factor,QString &unitString) +{ + double UnitValue = std::abs(quant.getValue()); + Unit unit = quant.getUnit(); + // for imperial user/programmer mind; UnitValue is in internal system, that means + // mm/kg/s. And all combined units have to be calculated from there! + + // now do special treatment on all cases seems necessary: + if(unit == Unit::Length){ // Length handling ============================ + if(UnitValue < 0.00000254){// smaller then 0.001 thou -> inch and scientific notation + unitString = QString::fromLatin1("in"); + factor = 25.4; + //}else if(UnitValue < 2.54){ // smaller then 0.1 inch -> Thou (mil) + // unitString = QString::fromLatin1("thou"); + // factor = 0.0254; + }else{ // bigger then 1000 mi -> scientific notation + unitString = QString::fromLatin1("in"); + factor = 25.4; + } + }else if (unit == Unit::Area){ + // TODO Cascade for the Areas + // default action for all cases without special treatment: + unitString = QString::fromLatin1("in^2"); + factor = 645.16; + }else if (unit == Unit::Volume){ + // TODO Cascade for the Volume + // default action for all cases without special treatment: + unitString = QString::fromLatin1("in^3"); + factor = 16387.064; + }else if (unit == Unit::Mass){ + // TODO Cascade for the wights + // default action for all cases without special treatment: + unitString = QString::fromLatin1("lb"); + factor = 0.45359237; + }else if (unit == Unit::Pressure){ + if(UnitValue < 145.038){// psi is the smallest + unitString = QString::fromLatin1("psi"); + factor = 0.145038; + //}else if(UnitValue < 145038){ + // unitString = QString::fromLatin1("ksi"); + // factor = 145.038; + }else{ // bigger then 1000 ksi -> psi + scientific notation + unitString = QString::fromLatin1("psi"); + factor = 0.145038; + } + }else{ + // default action for all cases without special treatment: + unitString = quant.getUnit().getString(); + factor = 1.0; + } + QLocale Lc = QLocale::system(); + Lc.setNumberOptions(Lc.OmitGroupSeparator | Lc.RejectGroupSeparator); + QString Ln = Lc.toString(quant.getValue() / factor); + return QString::fromLatin1("%1 %2").arg(Ln).arg(unitString); + //return QString::fromLatin1("%L1 %2").arg(quant.getValue() / factor).arg(unitString); +} diff --git a/src/Base/UnitsSchemaImperial1.h b/src/Base/UnitsSchemaImperial1.h index a88557ebd..67496ec79 100644 --- a/src/Base/UnitsSchemaImperial1.h +++ b/src/Base/UnitsSchemaImperial1.h @@ -45,6 +45,19 @@ public: }; +/** The schema class for the imperial unit system + * Here are the definiton for the imperial unit system. + * It also defines how the value/units get printed. + */ +class UnitsSchemaImperialDecimal: public UnitsSchema +{ +public: + //virtual void setSchemaUnits(void); + //virtual void resetSchemaUnits(void); + virtual QString schemaTranslate(Base::Quantity quant,double &factor,QString &unitString); + +}; + } // namespace Base diff --git a/src/Gui/DlgSettingsUnits.ui b/src/Gui/DlgSettingsUnits.ui index 0d3e3e3d5..b3267f7c1 100644 --- a/src/Gui/DlgSettingsUnits.ui +++ b/src/Gui/DlgSettingsUnits.ui @@ -41,11 +41,16 @@ MKS (m/kg/s/degree) - - - US customary (in/lb) - - + + + US customary (in/lb) + + + + + Imperial decimal (in/lb) + + diff --git a/src/Gui/DlgSettingsUnitsImp.cpp b/src/Gui/DlgSettingsUnitsImp.cpp index 16b51dd9a..f7cbe0095 100644 --- a/src/Gui/DlgSettingsUnitsImp.cpp +++ b/src/Gui/DlgSettingsUnitsImp.cpp @@ -63,18 +63,6 @@ DlgSettingsUnitsImp::~DlgSettingsUnitsImp() delete ui; } -void DlgSettingsUnitsImp::fillUpListBox() -{ - //tableWidget->setRowCount(10); - //for (int i = 0 ; i<9;i++) { - // QTableWidgetItem *newItem = new QTableWidgetItem(UnitsApi::getQuantityName((Base::QuantityType)i)); - // tableWidget->setItem(i, 0, newItem); - // - // newItem = new QTableWidgetItem(UnitsApi::getPrefUnitOf((Base::QuantityType)i)); - // tableWidget->setItem(i, 1, newItem); - //} -} - void DlgSettingsUnitsImp::on_comboBox_ViewSystem_currentIndexChanged(int index) { if (index < 0) @@ -82,7 +70,6 @@ void DlgSettingsUnitsImp::on_comboBox_ViewSystem_currentIndexChanged(int index) UnitsApi::setSchema((UnitSystem)index); - fillUpListBox(); } void DlgSettingsUnitsImp::saveSettings() diff --git a/src/Gui/DlgSettingsUnitsImp.h b/src/Gui/DlgSettingsUnitsImp.h index 52a3c640d..ff7f0330c 100644 --- a/src/Gui/DlgSettingsUnitsImp.h +++ b/src/Gui/DlgSettingsUnitsImp.h @@ -49,8 +49,6 @@ public: protected: void changeEvent(QEvent *e); - void fillUpListBox(void); - public Q_SLOTS: void on_comboBox_ViewSystem_currentIndexChanged(int index);