Skip to content

Commit

Permalink
New Unit-Schema ImperialDecimal
Browse files Browse the repository at this point in the history
  • Loading branch information
jriegel committed Apr 15, 2014
1 parent 79b8e07 commit aa5a2e1
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/Base/UnitsApi.cpp
Expand Up @@ -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).
Expand Down
3 changes: 2 additions & 1 deletion src/Base/UnitsSchema.h
Expand Up @@ -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 */
} ;


Expand Down
61 changes: 61 additions & 0 deletions src/Base/UnitsSchemaImperial1.cpp
Expand Up @@ -27,6 +27,7 @@
#endif

#include <QString>
#include <QLocale>
#include "Exception.h"
#include "UnitsApi.h"
#include "UnitsSchemaImperial1.h"
Expand Down Expand Up @@ -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);
}
13 changes: 13 additions & 0 deletions src/Base/UnitsSchemaImperial1.h
Expand Up @@ -45,6 +45,19 @@ class UnitsSchemaImperial1: public UnitsSchema

};

/** 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

Expand Down
15 changes: 10 additions & 5 deletions src/Gui/DlgSettingsUnits.ui
Expand Up @@ -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>
Expand Down
13 changes: 0 additions & 13 deletions src/Gui/DlgSettingsUnitsImp.cpp
Expand Up @@ -63,26 +63,13 @@ 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)
return; // happens when clearing the combo box in retranslateUi()

UnitsApi::setSchema((UnitSystem)index);

fillUpListBox();
}

void DlgSettingsUnitsImp::saveSettings()
Expand Down
2 changes: 0 additions & 2 deletions src/Gui/DlgSettingsUnitsImp.h
Expand Up @@ -49,8 +49,6 @@ class DlgSettingsUnitsImp : public PreferencePage
protected:
void changeEvent(QEvent *e);

void fillUpListBox(void);

public Q_SLOTS:
void on_comboBox_ViewSystem_currentIndexChanged(int index);

Expand Down

0 comments on commit aa5a2e1

Please sign in to comment.