diff --git a/src/Gui/propertyeditor/PropertyItem.cpp b/src/Gui/propertyeditor/PropertyItem.cpp index f31b202a9..f8cefb6ca 100644 --- a/src/Gui/propertyeditor/PropertyItem.cpp +++ b/src/Gui/propertyeditor/PropertyItem.cpp @@ -35,7 +35,6 @@ #endif #include -#include #include #include #include @@ -647,46 +646,27 @@ PropertyUnitItem::PropertyUnitItem() { } -QVariant PropertyUnitItem::toString(const QVariant& Value) const +QVariant PropertyUnitItem::toString(const QVariant& prop) const { - double val = Value.toDouble(); - - QString unit; - const std::vector& prop = getPropertyData(); - if (!prop.empty() && prop.front()->getTypeId().isDerivedFrom(App::PropertyQuantity::getClassTypeId())) { - Base::Quantity value = static_cast(prop.front())->getQuantityValue(); - //unit = QString::fromLatin1("'%1'").arg(value.getUserString()); - unit = value.getUserString(); - } - - return QVariant(unit); + const Base::Quantity& unit = prop.value(); + return QVariant(unit.getUserString()); } QVariant PropertyUnitItem::value(const App::Property* prop) const { assert(prop && prop->getTypeId().isDerivedFrom(App::PropertyQuantity::getClassTypeId())); + Base::Quantity value = static_cast(prop)->getQuantityValue(); - //QString unitString; - //return QVariant(value.getValue()); - return QVariant(value.getUserString()); + return QVariant::fromValue(value); } void PropertyUnitItem::setValue(const QVariant& value) { - if (!value.canConvert(QVariant::Double)) + if (!value.canConvert()) return; - double val = value.toDouble(); - - QString unit; - const std::vector& prop = getPropertyData(); - if (prop.empty()) - return; - else if (prop.front()->getTypeId().isDerivedFrom(App::PropertyQuantity::getClassTypeId())) { - Base::Quantity value = static_cast(prop.front())->getQuantityValue(); - //unit = value.getUserString(); - unit = QString::fromLatin1("'%1 %2'").arg(val).arg(value.getUnit().getString()); - } + const Base::Quantity& val = value.value(); + QString unit = QString::fromLatin1("'%1 %2'").arg(val.getValue()).arg(val.getUnit().getString()); setPropertyValue(unit); } @@ -694,33 +674,32 @@ QWidget* PropertyUnitItem::createEditor(QWidget* parent, const QObject* receiver { QDoubleSpinBox *sb = new QDoubleSpinBox(parent); sb->setFrame(false); - sb->setDecimals(decimals()); + sb->setDecimals(std::max(5,decimals())); QObject::connect(sb, SIGNAL(valueChanged(double)), receiver, method); return sb; } void PropertyUnitItem::setEditorData(QWidget *editor, const QVariant& data) const { + const Base::Quantity& value = data.value(); + QDoubleSpinBox *sb = qobject_cast(editor); sb->setRange((double)INT_MIN, (double)INT_MAX); - sb->setValue(data.toDouble()); - const std::vector& prop = getPropertyData(); - if (prop.empty()) - return; - else if (prop.front()->getTypeId().isDerivedFrom(App::PropertyQuantity::getClassTypeId())) { - Base::Quantity value = static_cast(prop.front())->getQuantityValue(); - QString unitString; - //double factor; - sb->setValue(value.getValue()); - //unitString.prepend(QLatin1String(" ")); - sb->setSuffix(value.getUnit().getString()); - } + //sb->setValue(value.getValue()); + //sb->setSuffix(value.getUnit().getString()); + double factor; + QString unitStr; + value.getUserString(factor, unitStr); + double unitValue = value.getValue() / factor; + sb->setValue(unitValue); + sb->setSuffix(unitStr); } QVariant PropertyUnitItem::editorData(QWidget *editor) const { QDoubleSpinBox *sb = qobject_cast(editor); - return QVariant(sb->value()); + Base::Quantity value = Base::Quantity::parse(sb->text()); + return QVariant::fromValue(value); } // -------------------------------------------------------------------- diff --git a/src/Gui/propertyeditor/PropertyItem.h b/src/Gui/propertyeditor/PropertyItem.h index f271debcd..e4a551127 100644 --- a/src/Gui/propertyeditor/PropertyItem.h +++ b/src/Gui/propertyeditor/PropertyItem.h @@ -41,6 +41,7 @@ Q_DECLARE_METATYPE(Base::Vector3f) Q_DECLARE_METATYPE(Base::Vector3d) Q_DECLARE_METATYPE(Base::Matrix4D) Q_DECLARE_METATYPE(Base::Placement) +Q_DECLARE_METATYPE(Base::Quantity) namespace Gui { namespace Dialog { class TaskPlacement; } @@ -235,7 +236,6 @@ protected: virtual QVariant toString(const QVariant&) const; virtual QVariant value(const App::Property*) const; virtual void setValue(const QVariant&); - Base::Unit _Unit; PropertyUnitItem(); };