New Unit-Schema ImperialDecimal

This commit is contained in:
jriegel 2014-04-15 18:23:23 +02:00
parent 79b8e07338
commit aa5a2e1b3f
7 changed files with 87 additions and 21 deletions

View File

@ -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).

View File

@ -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 */
} ;

View File

@ -27,6 +27,7 @@
#endif
#include <QString>
#include <QLocale>
#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);
}

View File

@ -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

View File

@ -41,11 +41,16 @@
<string>MKS (m/kg/s/degree)</string>
</property>
</item>
<item>
<property name="text">
<string>US customary (in/lb)</string>
</property>
</item>
<item>
<property name="text">
<string>US customary (in/lb)</string>
</property>
</item>
<item>
<property name="text">
<string>Imperial decimal (in/lb)</string>
</property>
</item>
</widget>
</item>
</layout>

View File

@ -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()

View File

@ -49,8 +49,6 @@ public:
protected:
void changeEvent(QEvent *e);
void fillUpListBox(void);
public Q_SLOTS:
void on_comboBox_ViewSystem_currentIndexChanged(int index);