Skip to content

Commit

Permalink
+ Proper reimplmentation of PropertyUnitItem
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Feb 13, 2014
1 parent 4d918d1 commit 989b6a0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 43 deletions.
63 changes: 21 additions & 42 deletions src/Gui/propertyeditor/PropertyItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#endif

#include <Base/Tools.h>
#include <Base/UnitsApi.h>
#include <App/Application.h>
#include <App/Document.h>
#include <App/DocumentObject.h>
Expand Down Expand Up @@ -647,80 +646,60 @@ 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<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();
//unit = QString::fromLatin1("'%1'").arg(value.getUserString());
unit = value.getUserString();
}

return QVariant(unit);
const Base::Quantity& unit = prop.value<Base::Quantity>();
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<const App::PropertyQuantity*>(prop)->getQuantityValue();
//QString unitString;
//return QVariant(value.getValue());
return QVariant(value.getUserString());
return QVariant::fromValue<Base::Quantity>(value);
}

void PropertyUnitItem::setValue(const QVariant& value)
{
if (!value.canConvert(QVariant::Double))
return;
double val = value.toDouble();

QString unit;
const std::vector<App::Property*>& prop = getPropertyData();
if (prop.empty())
if (!value.canConvert<Base::Quantity>())
return;
else if (prop.front()->getTypeId().isDerivedFrom(App::PropertyQuantity::getClassTypeId())) {
Base::Quantity value = static_cast<const App::PropertyQuantity*>(prop.front())->getQuantityValue();
//unit = value.getUserString();
unit = QString::fromLatin1("'%1 %2'").arg(val).arg(value.getUnit().getString());
}
const Base::Quantity& val = value.value<Base::Quantity>();

QString unit = QString::fromLatin1("'%1 %2'").arg(val.getValue()).arg(val.getUnit().getString());
setPropertyValue(unit);
}

QWidget* PropertyUnitItem::createEditor(QWidget* parent, const QObject* receiver, const char* method) const
{
QDoubleSpinBox *sb = new QDoubleSpinBox(parent);
sb->setFrame(false);
sb->setDecimals(decimals());
sb->setDecimals(std::max<int>(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<Base::Quantity>();

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;
//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<QDoubleSpinBox*>(editor);
return QVariant(sb->value());
Base::Quantity value = Base::Quantity::parse(sb->text());
return QVariant::fromValue<Base::Quantity>(value);
}

// --------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/Gui/propertyeditor/PropertyItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -235,7 +236,6 @@ class GuiExport PropertyUnitItem: public PropertyItem
virtual QVariant toString(const QVariant&) const;
virtual QVariant value(const App::Property*) const;
virtual void setValue(const QVariant&);
Base::Unit _Unit;

PropertyUnitItem();
};
Expand Down

0 comments on commit 989b6a0

Please sign in to comment.