integrate Quantities into PropertyView and activate preferences again

This commit is contained in:
jriegel 2013-09-25 21:46:08 +02:00
parent e55ccd05c7
commit 44c07da571
27 changed files with 303 additions and 63 deletions

View File

@ -1175,9 +1175,9 @@ void Application::initApplication(void)
Application::_pcSingleton = new Application(0,0,mConfig);
// set up Unit system default
//ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath
// ("User parameter:BaseApp/Preferences/Units");
//UnitsApi::setSchema((UnitSystem)hGrp->GetInt("UserSchema",0));
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath
("User parameter:BaseApp/Preferences/Units");
UnitsApi::setSchema((UnitSystem)hGrp->GetInt("UserSchema",0));
// starting the init script
Interpreter().runString(Base::ScriptFactory().ProduceScript("FreeCADInit"));

View File

@ -28,8 +28,10 @@
#include <Base/Console.h>
#include <Base/Exception.h>
#include <Base/Unit.h>
#include "FeatureTest.h"
#include "Material.h"
#include "Material.h"
#define new DEBUG_CLIENTBLOCK
using namespace App;
@ -94,7 +96,12 @@ FeatureTest::FeatureTest()
ADD_PROPERTY_TYPE(TypeAll ,(4711),group,(App::PropertyType) (Prop_Output|Prop_ReadOnly |Prop_Hidden ),
"An example property which has the types 'Output', 'ReadOnly' and 'Hidden'");
ADD_PROPERTY(Quantity,(1.0));
ADD_PROPERTY(QuantityLength,(1.0));
QuantityLength.setUnit(Base::Unit::Length);
ADD_PROPERTY(QuantityMass,(1.0));
QuantityMass.setUnit(Base::Unit::Mass);
ADD_PROPERTY(QuantityAngle,(1.0));
QuantityAngle.setUnit(Base::Unit::Angle);
}

View File

@ -93,7 +93,9 @@ public:
App::PropertyInteger TypeAll;
App::PropertyInteger TypeTransient;
App::PropertyQuantity Quantity;
App::PropertyQuantity QuantityLength;
App::PropertyQuantity QuantityMass;
App::PropertyQuantity QuantityAngle;
/** @name methods overide Feature */
//@{

View File

@ -55,6 +55,11 @@ using namespace std;
TYPESYSTEM_SOURCE(App::PropertyQuantity, App::PropertyFloat);
Base::Quantity PropertyQuantity::getQuantityValue(void) const
{
return Quantity( _dValue,_Unit);
}
const char* PropertyQuantity::getEditorName(void) const
{
@ -69,7 +74,30 @@ PyObject *PropertyQuantity::getPyObject(void)
void PropertyQuantity::setPyObject(PyObject *value)
{
setValue(UnitsApi::toDblWithUserPrefs(Length,value));
Base::Quantity quant;
if (PyString_Check(value))
quant = Quantity::parse(PyString_AsString(value));
else if (PyFloat_Check(value))
quant = Quantity(PyFloat_AsDouble(value),_Unit);
else if (PyInt_Check(value))
quant = Quantity((double)PyInt_AsLong(value),_Unit);
else if (PyObject_TypeCheck(value, &(QuantityPy::Type))) {
Base::QuantityPy *pcObject = static_cast<Base::QuantityPy*>(value);
quant = *(pcObject->getQuantityPtr());
}else
throw Base::Exception("Wrong type!");
Unit unit = quant.getUnit();
if(unit.isEmpty()){
setValue(quant.getValue());
return;
}
if (unit != _Unit)
throw Base::Exception("Not matching Unit!");
setValue(quant.getValue());
}
//**************************************************************************

View File

@ -33,6 +33,7 @@
#include <boost/filesystem/path.hpp>
#include <Base/Unit.h>
#include <Base/Quantity.h>
#include "PropertyStandard.h"
namespace Base {
@ -52,6 +53,9 @@ class AppExport PropertyQuantity : public PropertyFloat
public:
PropertyQuantity(void){}
virtual ~PropertyQuantity(){}
Base::Quantity getQuantityValue(void) const;
virtual const char* getEditorName(void) const;
virtual PyObject *getPyObject(void);

View File

@ -27,6 +27,7 @@
#include <cmath>
#include "Quantity.h"
#include "Exception.h"
#include "UnitsApi.h"
// suppress annoying warnings from generated source files
#ifdef _MSC_VER
@ -107,6 +108,11 @@ Quantity& Quantity::operator = (const Quantity &New)
return *this;
}
double Quantity::getUserPrefered(QString &unitString)const
{
return Base::UnitsApi::schemaPrefUnit(_Unit,unitString).getValue() * _Value;
}
// === Parser & Scanner stuff ===============================================
// include the Scanner and the Parser for the Quantitys

View File

@ -25,6 +25,7 @@
#define BASE_Quantity_H
#include "Unit.h"
#include <QString>
namespace Base {
@ -53,10 +54,16 @@ public:
Quantity pow(const Quantity&)const;
//@}
/// transfer to user prefered unit/potence
double getUserPrefered(QString &unitString = QString())const;
std::string getUserString(void)const;
static Quantity parse(const char* buffer);
const Unit & getUnit(void) const{return _Unit;}
void setUnit(const Unit &un){_Unit = un;}
double getValue(void) const{return _Value;}
void setValue(double val){_Value = val;}
protected:
double _Value;

View File

@ -26,24 +26,37 @@ Quantity(string) -- arbitrary mixture of numbers and chars defining a Quantity
</UserDocu>
<DeveloperDocu>Quantity</DeveloperDocu>
</Documentation>
<Methode Name="pow">
<Documentation>
<UserDocu>
sets the quantity to the power
</UserDocu>
</Documentation>
</Methode>
<Attribute Name="Value" ReadOnly="false">
<Methode Name="pow">
<Documentation>
<UserDocu>
sets the quantity to the power
</UserDocu>
</Documentation>
</Methode>
<Methode Name="getUserPrefered">
<Documentation>
<UserDocu>
returns a quantity with the translation factor and a string with the prevered unit
</UserDocu>
</Documentation>
</Methode>
<Attribute Name="Value" ReadOnly="false">
<Documentation>
<UserDocu>Numeric Value of the Quantity (in internal system mm,kg,s)</UserDocu>
</Documentation>
<Parameter Name="Value" Type="Float" />
</Attribute>
<Attribute Name="Unit" ReadOnly="false">
<Documentation>
<UserDocu>Unit of the Quantity</UserDocu>
</Documentation>
<Parameter Name="Unit" Type="Object" />
</Attribute>
</PythonExport>
<Attribute Name="Unit" ReadOnly="false">
<Documentation>
<UserDocu>Unit of the Quantity</UserDocu>
</Documentation>
<Parameter Name="Unit" Type="Object" />
</Attribute>
<Attribute Name="UserString" ReadOnly="true">
<Documentation>
<UserDocu>Unit of the Quantity</UserDocu>
</Documentation>
<Parameter Name="UserString" Type="String" />
</Attribute>
</PythonExport>
</GenerateModel>

View File

@ -6,6 +6,7 @@
// inclusion of the generated files (generated out of QuantityPy.xml)
#include "QuantityPy.h"
#include "UnitPy.h"
#include "QuantityPy.cpp"
using namespace Base;
@ -51,10 +52,8 @@ int QuantityPy::PyInit(PyObject* args, PyObject* kwd)
const char* string;
if (PyArg_ParseTuple(args,"s", &string)) {
return -1;
*self = Quantity::parse(string);
return 0;
}
PyErr_SetString(PyExc_TypeError, "Either three floats, tuple or Vector expected");
@ -68,6 +67,11 @@ PyObject* QuantityPy::pow(PyObject * args)
return 0;
}
PyObject* QuantityPy::getUserPrefered(PyObject *args)
{
PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented");
return 0;
}
PyObject* QuantityPy::number_add_handler(PyObject *self, PyObject *other)
{
@ -126,24 +130,34 @@ PyObject* QuantityPy::number_multiply_handler(PyObject *self, PyObject *other)
Py::Float QuantityPy::getValue(void) const
{
//return Py::Float();
throw Py::AttributeError("Not yet implemented");
return Py::Float(getQuantityPtr()->getValue());
}
void QuantityPy::setValue(Py::Float /*arg*/)
void QuantityPy::setValue(Py::Float arg)
{
throw Py::AttributeError("Not yet implemented");
getQuantityPtr()->setValue(arg);
}
Py::Object QuantityPy::getUnit(void) const
{
//return Py::Object();
throw Py::AttributeError("Not yet implemented");
return Py::Object(new UnitPy(new Unit(getQuantityPtr()->getUnit())));
}
void QuantityPy::setUnit(Py::Object /*arg*/)
void QuantityPy::setUnit(Py::Object arg)
{
throw Py::AttributeError("Not yet implemented");
union PyType_Object pyType = {&(Base::UnitPy::Type)};
Py::Type UnitType(pyType.o);
if(!arg.isType(UnitType))
throw Py::AttributeError("Not yet implemented");
getQuantityPtr()->setUnit(*static_cast<Base::UnitPy*>((*arg))->getUnitPtr());
}
Py::String QuantityPy::getUserString(void) const
{
//return Py::String();
throw Py::AttributeError("Not yet implemented");
}
PyObject *QuantityPy::getCustomAttributes(const char* /*attr*/) const

View File

@ -269,13 +269,21 @@ std::string Unit::getString(void) const
}
}
return ret.str();
}
return ret.str();
}
Unit Unit::Length(1);
Unit Unit::Area(2);
Unit Unit::Volume(3);
Unit Unit::Mass(0,1);
Unit Unit::Angle(0,0,0,0,0,0,0,1);
Unit Unit::TimeSpan(0,0,1);
Unit Unit::Velocity(1,0,-1);
Unit Unit::Acceleration(1,0,-2);
Unit Unit::Temperature(0,0,0,0,1);
Unit Unit::ElectricCurrent(0,0,0,1);
Unit Unit::AmountOfSubstance(0,0,0,0,0,1);
Unit Unit::LuminoseIntensity(0,0,0,0,0,0,1);

View File

@ -75,6 +75,26 @@ public:
char getLengthDimension(void){return Sig.Length;}
std::string getString(void) const;
/** Predefined Unit types. */
//@{
/// Length unit
static Unit Length;
/// Mass unit
static Unit Mass;
/// Angle
static Unit Angle;
static Unit Area;
static Unit Volume;
static Unit TimeSpan;
static Unit Velocity;
static Unit Acceleration;
static Unit Temperature;
static Unit ElectricCurrent;
static Unit AmountOfSubstance;
static Unit LuminoseIntensity;
protected:
UnitSignature Sig;
};

View File

@ -130,6 +130,16 @@ double UnitsApi::translateUnit(const QString & str)
// === static translation methodes ==========================================
QString UnitsApi::schemaTranslate(Base::Quantity quant)
{
return UserPrefSystem->schemaTranslate(quant);
}
Base::Quantity UnitsApi::schemaPrefUnit(const Base::Unit &unit,QString &outUnitString)
{
return UserPrefSystem->schemaPrefUnit(unit,outUnitString);
}
QString UnitsApi::toStrWithUserPrefs(QuantityType t,double Value)
{
return UserPrefSystem->toStrWithUserPrefs(t,Value);

View File

@ -65,6 +65,8 @@ public:
static double translateUnit(const char*);
static double translateUnit(const QString &);
static QString schemaTranslate(Base::Quantity quant);
static Base::Quantity schemaPrefUnit(const Base::Unit &unit,QString &outUnitString);
/** @name Translation from internal to user prefs */
//@{

View File

@ -27,6 +27,8 @@
#include <string>
#include <QString>
#include "Quantity.h"
//#include "UnitsApi.h"
@ -69,6 +71,10 @@ public:
* units parser.
*/
virtual QString toStrWithUserPrefs(QuantityType t,double Value)=0;
virtual QString schemaTranslate(Base::Quantity quant)=0;
// returns the prefered unit as string and the quantity to translate
virtual Base::Quantity schemaPrefUnit(const Base::Unit &unit,QString &outUnitString)=0;
};

View File

@ -62,3 +62,26 @@ QString UnitsSchemaImperial1::toStrWithUserPrefs(QuantityType t,double Value)
double UnitValue = Value/UnitsApi::getPrefFactorOf(t);
return QString::fromAscii("%1 %2").arg(UnitValue).arg(UnitsApi::getPrefUnitOf(t));
}
QString UnitsSchemaImperial1::schemaTranslate(Base::Quantity quant)
{
double UnitValue = quant.getValue();
Unit unit = quant.getUnit();
return QString::fromAscii("%1 %2").arg(UnitValue).arg(QString::fromAscii(unit.getString().c_str()));
}
Base::Quantity UnitsSchemaImperial1::schemaPrefUnit(const Base::Unit &unit,QString &outUnitString)
{
if(unit == Unit::Length){
outUnitString = QString::fromAscii("\"");
return Base::Quantity(1/25.40,Unit::Length);
}else if(unit == Unit::Mass){
outUnitString = QString::fromAscii("lb");
return Base::Quantity(1/0.45359237,Unit::Length);
}else{
outUnitString = QString::fromAscii(unit.getString().c_str());
return Base::Quantity(1,unit);
}
}

View File

@ -42,6 +42,9 @@ public:
void setSchemaUnits(void);
void toStrWithUserPrefs(QuantityType t,double Value,QString &outValue,QString &outUnit);
QString toStrWithUserPrefs(QuantityType t,double Value);
virtual QString schemaTranslate(Base::Quantity quant);
Base::Quantity schemaPrefUnit(const Base::Unit &unit,QString &outUnitString);
};

View File

@ -62,4 +62,22 @@ QString UnitsSchemaInternal::toStrWithUserPrefs(QuantityType t,double Value)
return QString::fromAscii("%1 %2").arg(UnitValue).arg(UnitsApi::getPrefUnitOf(t));
}
QString UnitsSchemaInternal::schemaTranslate(Base::Quantity quant)
{
double UnitValue = quant.getValue();
Unit unit = quant.getUnit();
return QString::fromAscii("%1 %2").arg(UnitValue).arg(QString::fromAscii(unit.getString().c_str()));
}
Base::Quantity UnitsSchemaInternal::schemaPrefUnit(const Base::Unit &unit,QString &outUnitString)
{
if(unit == Unit::Length){
outUnitString = QString::fromAscii("mm");
return Base::Quantity(1.0,Unit::Length);
}else{
outUnitString = QString::fromAscii(unit.getString().c_str());
return Base::Quantity(1,unit);
}
}

View File

@ -43,6 +43,9 @@ public:
void setSchemaUnits(void);
void toStrWithUserPrefs(QuantityType t,double Value,QString &outValue,QString &outUnit);
QString toStrWithUserPrefs(QuantityType t,double Value);
virtual QString schemaTranslate(Base::Quantity quant);
Base::Quantity schemaPrefUnit(const Base::Unit &unit,QString &outUnitString);
};

View File

@ -62,4 +62,22 @@ QString UnitsSchemaMKS::toStrWithUserPrefs(QuantityType t,double Value)
return QString::fromAscii("%1 %2").arg(UnitValue).arg(UnitsApi::getPrefUnitOf(t));
}
QString UnitsSchemaMKS::schemaTranslate(Base::Quantity quant)
{
double UnitValue = quant.getValue();
Unit unit = quant.getUnit();
return QString::fromAscii("%1 %2").arg(UnitValue).arg(QString::fromAscii(unit.getString().c_str()));
}
Base::Quantity UnitsSchemaMKS::schemaPrefUnit(const Base::Unit &unit,QString &outUnitString)
{
if(unit == Unit::Length){
outUnitString = QString::fromAscii("m");
return Base::Quantity(1/1000.0,Unit::Length);
}else{
outUnitString = QString::fromAscii(unit.getString().c_str());
return Base::Quantity(1,unit);
}
}

View File

@ -41,6 +41,9 @@ public:
void setSchemaUnits(void);
void toStrWithUserPrefs(QuantityType t,double Value,QString &outValue,QString &outUnit);
QString toStrWithUserPrefs(QuantityType t,double Value);
virtual QString schemaTranslate(Base::Quantity quant);
Base::Quantity schemaPrefUnit(const Base::Unit &unit,QString &outUnitString);
};

View File

@ -55,6 +55,8 @@ DlgSettingsUnitsImp::DlgSettingsUnitsImp(QWidget* parent)
QObject::connect(comboBox_ViewSystem, SIGNAL(currentIndexChanged(int)), this, SLOT(currentIndexChanged(int)));
tableWidget->setVisible(false);
}
/**

View File

@ -647,45 +647,85 @@ PropertyUnitItem::PropertyUnitItem()
{
}
QVariant PropertyUnitItem::toString(const QVariant& Value) const
{
double val = Value.toDouble();
QString unit;
const std::vector<App::Property*>& prop = getPropertyData();
if (!prop.empty() && prop.front()->getTypeId().isDerivedFrom(App::PropertyQuantity::getClassTypeId())) {
Base::Quantity value = static_cast<const App::PropertyQuantity*>(prop.front())->getQuantityValue();
value.getUserPrefered(unit);
unit.prepend(QLatin1String(" "));
}
QString data = QString::fromAscii("%1 %2").arg(val,0,'f',decimals()).arg(unit);
return QVariant(data);
}
QVariant PropertyUnitItem::value(const App::Property* prop) const
{
assert(prop && prop->getTypeId().isDerivedFrom(App::PropertyLength::getClassTypeId()));
//UnitType = Base::Length;
assert(prop && prop->getTypeId().isDerivedFrom(App::PropertyQuantity::getClassTypeId()));
double value = static_cast<const App::PropertyLength*>(prop)->getValue();
QString nbr;
nbr = Base::UnitsApi::toStrWithUserPrefs(Base::Length,value);
Base::Quantity value = static_cast<const App::PropertyQuantity*>(prop)->getQuantityValue();
return QVariant(value.getUserPrefered());
return QVariant(nbr);
}
void PropertyUnitItem::setValue(const QVariant& value)
{
if (!value.canConvert(QVariant::String))
if (!value.canConvert(QVariant::Double))
return;
QString val = value.toString();
QString data = QString::fromAscii("\"%1\"").arg(val);
double val = value.toDouble();
QString unit;
const std::vector<App::Property*>& prop = getPropertyData();
if (prop.empty())
return;
else if (prop.front()->getTypeId().isDerivedFrom(App::PropertyQuantity::getClassTypeId())) {
Base::Quantity value = static_cast<const App::PropertyQuantity*>(prop.front())->getQuantityValue();
value.getUserPrefered(unit);
unit.prepend(QLatin1String(" "));
}
QString data = QString::fromAscii("'%1%2'").arg(val,0,'f',decimals()).arg(unit);
setPropertyValue(data);
}
QWidget* PropertyUnitItem::createEditor(QWidget* parent, const QObject* receiver, const char* method) const
{
QLineEdit *le = new QLineEdit(parent);
le->setFrame(false);
QObject::connect(le, SIGNAL(textChanged(const QString&)), receiver, method);
return le;
QDoubleSpinBox *sb = new QDoubleSpinBox(parent);
sb->setFrame(false);
sb->setDecimals(decimals());
QObject::connect(sb, SIGNAL(valueChanged(double)), receiver, method);
return sb;
}
void PropertyUnitItem::setEditorData(QWidget *editor, const QVariant& data) const
{
QLineEdit *le = qobject_cast<QLineEdit*>(editor);
le->setText(data.toString());
QDoubleSpinBox *sb = qobject_cast<QDoubleSpinBox*>(editor);
sb->setRange((double)INT_MIN, (double)INT_MAX);
sb->setValue(data.toDouble());
const std::vector<App::Property*>& prop = getPropertyData();
if (prop.empty())
return;
else if (prop.front()->getTypeId().isDerivedFrom(App::PropertyQuantity::getClassTypeId())) {
Base::Quantity value = static_cast<const App::PropertyQuantity*>(prop.front())->getQuantityValue();
QString unitString;
value.getUserPrefered(unitString);
unitString.prepend(QLatin1String(" "));
sb->setSuffix(unitString);
}
}
QVariant PropertyUnitItem::editorData(QWidget *editor) const
{
QLineEdit *le = qobject_cast<QLineEdit*>(editor);
return QVariant(le->text());
QDoubleSpinBox *sb = qobject_cast<QDoubleSpinBox*>(editor);
return QVariant(sb->value());
}
// --------------------------------------------------------------------

View File

@ -232,10 +232,10 @@ class GuiExport PropertyUnitItem: public PropertyItem
virtual QVariant editorData(QWidget *editor) const;
protected:
//virtual QVariant toString(const QVariant&) const;
virtual QVariant toString(const QVariant&) const;
virtual QVariant value(const App::Property*) const;
virtual void setValue(const QVariant&);
Base::QuantityType UnitType;
Base::Unit _Unit;
PropertyUnitItem();
};

View File

@ -65,7 +65,7 @@ WidgetFactorySupplier::WidgetFactorySupplier()
new PrefPageProducer<DlgSettingsEditorImp> ( QT_TRANSLATE_NOOP("QObject","General") );
new PrefPageProducer<DlgReportViewImp> ( QT_TRANSLATE_NOOP("QObject","General") );
new PrefPageProducer<DlgSettingsMacroImp> ( QT_TRANSLATE_NOOP("QObject","General") );
//new PrefPageProducer<DlgSettingsUnitsImp> ( QT_TRANSLATE_NOOP("QObject","General") );
new PrefPageProducer<DlgSettingsUnitsImp> ( QT_TRANSLATE_NOOP("QObject","General") );
new PrefPageProducer<DlgSettings3DViewImp> ( QT_TRANSLATE_NOOP("QObject","Display") );
new PrefPageProducer<DlgSettingsViewColor> ( QT_TRANSLATE_NOOP("QObject","Display") );

View File

@ -8,6 +8,7 @@ include_directories(
${Boost_INCLUDE_DIRS}
${ZLIB_INCLUDE_DIR}
${XERCESC_INCLUDE_DIR}
${QT_INCLUDE_DIR}
)
set(Image_LIBS

View File

@ -18,6 +18,7 @@ include_directories(
${XERCESC_INCLUDE_DIR}
${ZLIB_INCLUDE_DIR}
${FREETYPE_INCLUDE_DIRS}
${QT_INCLUDE_DIR}
)
link_directories(${OCC_LIBRARY_DIR})

View File

@ -11,6 +11,7 @@ include_directories(
${ZLIB_INCLUDE_DIR}
${PYTHON_INCLUDE_PATH}
${XERCESC_INCLUDE_DIR}
${QT_INCLUDE_DIR}
)
link_directories(${OCC_LIBRARY_DIR})