Skip to content

Commit

Permalink
Correct errors related to issue FreeCAD#13019
Browse files Browse the repository at this point in the history
  • Loading branch information
davesrocketshop committed Apr 9, 2024
2 parents d1930fc + 9425d65 commit cba0530
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 11 deletions.
41 changes: 41 additions & 0 deletions src/Mod/Material/App/Materials.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,15 @@ void Material::setPhysicalValue(const QString& name, const std::shared_ptr<QList
}
}

void Material::setPhysicalValue(const QString& name, const QVariant& value)
{
setPhysicalEditState(name);

if (hasPhysicalProperty(name)) {
_physical[name]->setValue(value);
}
}

void Material::setAppearanceValue(const QString& name, const QString& value)
{
setAppearanceEditState(name);
Expand Down Expand Up @@ -890,6 +899,38 @@ void Material::setAppearanceValue(const QString& name,
}
}

void Material::setAppearanceValue(const QString& name, const QVariant& value)
{
setAppearanceEditState(name);

if (hasAppearanceProperty(name)) {
_appearance[name]->setValue(value);
}
}

void Material::setValue(const QString& name, const QString& value)
{
if (hasPhysicalProperty(name)) {
setPhysicalValue(name, value);
}
else if (hasAppearanceProperty(name)) {
setAppearanceValue(name, value);
}
else {
throw PropertyNotFound();
}
}

void Material::setValue(const QString& name, const QVariant& value)
{
if (hasPhysicalProperty(name)) {
setPhysicalValue(name, value);
}
else {
throw PropertyNotFound();
}
}

std::shared_ptr<MaterialProperty> Material::getPhysicalProperty(const QString& name)
{
try {
Expand Down
5 changes: 5 additions & 0 deletions src/Mod/Material/App/Materials.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,15 @@ class MaterialsExport Material: public Base::BaseClass
void setPhysicalValue(const QString& name, const Base::Quantity& value);
void setPhysicalValue(const QString& name, const std::shared_ptr<MaterialValue>& value);
void setPhysicalValue(const QString& name, const std::shared_ptr<QList<QVariant>>& value);
void setPhysicalValue(const QString& name, const QVariant& value);

void setAppearanceValue(const QString& name, const QString& value);
void setAppearanceValue(const QString& name, const std::shared_ptr<MaterialValue>& value);
void setAppearanceValue(const QString& name, const std::shared_ptr<QList<QVariant>>& value);
void setAppearanceValue(const QString& name, const QVariant& value);

void setValue(const QString& name, const QString& value);
void setValue(const QString& name, const QVariant& value);

std::shared_ptr<MaterialProperty> getPhysicalProperty(const QString& name);
std::shared_ptr<MaterialProperty> getPhysicalProperty(const QString& name) const;
Expand Down
13 changes: 9 additions & 4 deletions src/Mod/Material/Gui/BaseDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ void BaseDelegate::paintQuantity(QPainter* painter,
auto model = index.model();
painter->save();

// Base::Console().Log("paintQuantity()\n");
if (newRow(model, index)) {
painter->drawText(option.rect, 0, QString());
// Base::Console().Log("paintQuantity('A')\n");
}
else {
QVariant item = getValue(index);
Expand Down Expand Up @@ -331,8 +333,9 @@ void BaseDelegate::setEditorData(QWidget* editor, const QModelIndex& index) cons
return;
}
if (type == Materials::MaterialValue::Quantity) {
auto input = dynamic_cast<Gui::InputField*>(editor);
input->setQuantityString(item.toString());
auto input = dynamic_cast<Gui::QuantitySpinBox*>(editor);
// input->setQuantityString(item.toString());
input->setValue(item.value<Base::Quantity>());
return;
}
if (type == Materials::MaterialValue::List || type == Materials::MaterialValue::ImageList) {
Expand All @@ -354,10 +357,11 @@ void BaseDelegate::setModelData(QWidget* editor,
value = chooser->fileName();
}
else if (type == Materials::MaterialValue::Quantity) {
auto input = dynamic_cast<Gui::InputField*>(editor);
auto input = dynamic_cast<Gui::QuantitySpinBox*>(editor);
// value = input->text();
// return;
auto quantity = Base::Quantity::parse(input->text());
// auto quantity = Base::Quantity::parse(input->text());
auto quantity = input->value();
value = QVariant::fromValue(quantity);
}
else if (type == Materials::MaterialValue::Integer) {
Expand All @@ -383,6 +387,7 @@ void BaseDelegate::setModelData(QWidget* editor,
}

setValue(model, index, value);
// Q_EMIT model->dataChanged(index, index);
}

QWidget* BaseDelegate::createEditor(QWidget* parent,
Expand Down
2 changes: 1 addition & 1 deletion src/Mod/Material/Gui/BaseDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class BaseDelegate: public QStyledItemDelegate
const QStyleOptionViewItem& option,
const QModelIndex& index) const;

bool newRow(const QAbstractItemModel* model, const QModelIndex& index) const;
virtual bool newRow(const QAbstractItemModel* model, const QModelIndex& index) const;
QWidget* createWidget(QWidget* parent, const QVariant& item, const QModelIndex& index) const;
};

Expand Down
28 changes: 25 additions & 3 deletions src/Mod/Material/Gui/MaterialDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ MaterialDelegate::MaterialDelegate(QObject* parent)
: BaseDelegate(parent)
{}

bool MaterialDelegate::newRow(const QAbstractItemModel* model, const QModelIndex& index) const
{
// New rows are for lists and arrays
return false;
}

Materials::MaterialValue::ValueType MaterialDelegate::getType(const QModelIndex& index) const
{
auto treeModel = dynamic_cast<const QStandardItemModel*>(index.model());
Expand Down Expand Up @@ -113,6 +119,17 @@ QVariant MaterialDelegate::getValue(const QModelIndex& index) const
// auto propertyName = group->child(row, 0)->text();
auto propertyName = group->child(row, 0)->data().toString();
propertyValue = material->getProperty(propertyName)->getValue();

Base::Console().Log(
"getValue(%s, %s)\n",
propertyName.toStdString().c_str(),
material->getProperty(propertyName)->getDictionaryString().toStdString().c_str());
if (material->getProperty(propertyName)->getType() == Materials::MaterialValue::Quantity) {
Base::Console().Log(
"Quantity::getValue(%s, %s)\n",
propertyName.toStdString().c_str(),
propertyValue.value<Base::Quantity>().getUserString().toStdString().c_str());
}
}
return propertyValue;
}
Expand All @@ -137,6 +154,10 @@ void MaterialDelegate::setValue(QAbstractItemModel* model,
auto property = material->getProperty(propertyName);
property->setValue(value);
group->child(row, 1)->setText(property->getString());

Base::Console().Log("setValue(%s, %s)\n",
_name.c_str(),
property->getString().toStdString().c_str());
}

notifyChanged(model, index);
Expand All @@ -163,7 +184,7 @@ void MaterialDelegate::notifyChanged(const QAbstractItemModel* model,
Base::Console().Log("MaterialDelegate::notifyChanged() - marked altered\n");

Q_EMIT const_cast<MaterialDelegate*>(this)->propertyChange(propertyName,
propertyValue.toString());
propertyValue);
}
}

Expand Down Expand Up @@ -447,11 +468,12 @@ QWidget* MaterialDelegate::createWidget(QWidget* parent,
widget = combo;
}
else if (type == Materials::MaterialValue::Quantity) {
auto input = new Gui::InputField(parent);
// auto input = new Gui::InputField(parent);
auto input = new Gui::QuantitySpinBox(parent);
input->setMinimum(std::numeric_limits<double>::min());
input->setMaximum(std::numeric_limits<double>::max());
input->setUnitText(getUnits(index));
input->setPrecision(6);
// input->setPrecision(6);
input->setValue(item.value<Base::Quantity>());

widget = input;
Expand Down
3 changes: 2 additions & 1 deletion src/Mod/Material/Gui/MaterialDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ class MaterialDelegate: public BaseDelegate
const QModelIndex& index,
const QVariant& value) const override;
void notifyChanged(const QAbstractItemModel* model, const QModelIndex& index) const override;
bool newRow(const QAbstractItemModel* model, const QModelIndex& index) const override;

Q_SIGNALS:
/** Emits this signal when a property has changed */
void propertyChange(const QString& property, const QString value);
void propertyChange(const QString& property, const QVariant& value);

private:
QWidget* createWidget(QWidget* parent, const QVariant& item, const QModelIndex& index) const;
Expand Down
2 changes: 1 addition & 1 deletion src/Mod/Material/Gui/MaterialsEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ void MaterialsEditor::onDescription()
_material->setDescription(ui->editDescription->toPlainText());
}

void MaterialsEditor::propertyChange(const QString& property, const QString value)
void MaterialsEditor::propertyChange(const QString& property, const QVariant& value)
{
if (_material->hasPhysicalProperty(property)) {
_material->setPhysicalValue(property, value);
Expand Down
2 changes: 1 addition & 1 deletion src/Mod/Material/Gui/MaterialsEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class MaterialsEditor: public QDialog
void onSourceReference(const QString& text);
void onDescription();

void propertyChange(const QString& property, const QString value);
void propertyChange(const QString& property, const QVariant& value);
void onInheritNewMaterial(bool checked);
void onNewMaterial(bool checked);
void onFavourite(bool checked);
Expand Down

0 comments on commit cba0530

Please sign in to comment.