Merge pull request #4048 from Ultimaker/CURA-5408-material-profiles

CURA-5408 weight for materials + bug fixes
This commit is contained in:
Lipu Fei 2018-07-13 07:54:58 +02:00 committed by GitHub
commit a0c3767184
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 75 deletions

View File

@ -46,19 +46,21 @@ class ContainerManager(QObject):
self._quality_manager = self._application.getQualityManager() self._quality_manager = self._application.getQualityManager()
self._container_name_filters = {} # type: Dict[str, Dict[str, Any]] self._container_name_filters = {} # type: Dict[str, Dict[str, Any]]
@pyqtSlot(str, str, str, result=str) @pyqtSlot(str, str, result=str)
def getContainerMetaDataEntry(self, container_id, entry_name, sub_entry: Optional[str] = None): def getContainerMetaDataEntry(self, container_id: str, entry_names: str) -> str:
metadatas = self._container_registry.findContainersMetadata(id = container_id) metadatas = self._container_registry.findContainersMetadata(id = container_id)
if not metadatas: if not metadatas:
Logger.log("w", "Could not get metadata of container %s because it was not found.", container_id) Logger.log("w", "Could not get metadata of container %s because it was not found.", container_id)
return "" return ""
sub_data = metadatas[0].get(entry_name, "") entries = entry_names.split("/")
result = str(sub_data) result = metadatas[0]
if sub_entry: while entries:
result = str(sub_data.get(sub_entry, "")) entry = entries.pop(0)
result = result.get(entry, {})
return result if not result:
return ""
return str(result)
## Set a metadata entry of the specified container. ## Set a metadata entry of the specified container.
# #
@ -73,6 +75,7 @@ class ContainerManager(QObject):
# #
# \return True if successful, False if not. # \return True if successful, False if not.
# TODO: This is ONLY used by MaterialView for material containers. Maybe refactor this. # TODO: This is ONLY used by MaterialView for material containers. Maybe refactor this.
# Update: In order for QML to use objects and sub objects, those (sub) objects must all be QObject. Is that what we want?
@pyqtSlot("QVariant", str, str) @pyqtSlot("QVariant", str, str)
def setContainerMetaDataEntry(self, container_node, entry_name, entry_value): def setContainerMetaDataEntry(self, container_node, entry_name, entry_value):
root_material_id = container_node.metadata["base_file"] root_material_id = container_node.metadata["base_file"]
@ -107,63 +110,6 @@ class ContainerManager(QObject):
if sub_item_changed: #If it was only a sub-item that has changed then the setMetaDataEntry won't correctly notice that something changed, and we must manually signal that the metadata changed. if sub_item_changed: #If it was only a sub-item that has changed then the setMetaDataEntry won't correctly notice that something changed, and we must manually signal that the metadata changed.
container.metaDataChanged.emit(container) container.metaDataChanged.emit(container)
## Set a setting property of the specified container.
#
# This will set the specified property of the specified setting of the container
# and all containers that share the same base_file (if any). The latter only
# happens for material containers.
#
# \param container_id \type{str} The ID of the container to change.
# \param setting_key \type{str} The key of the setting.
# \param property_name \type{str} The name of the property, eg "value".
# \param property_value \type{str} The new value of the property.
#
# \return True if successful, False if not.
@pyqtSlot(str, str, str, str, result = bool)
def setContainerProperty(self, container_id, setting_key, property_name, property_value):
if self._container_registry.isReadOnly(container_id):
Logger.log("w", "Cannot set properties of read-only container %s.", container_id)
return False
containers = self._container_registry.findContainers(id = container_id)
if not containers:
Logger.log("w", "Could not set properties of container %s because it was not found.", container_id)
return False
container = containers[0]
container.setProperty(setting_key, property_name, property_value)
basefile = container.getMetaDataEntry("base_file", container_id)
for sibbling_container in self._container_registry.findInstanceContainers(base_file = basefile):
if sibbling_container != container:
sibbling_container.setProperty(setting_key, property_name, property_value)
return True
## Get a setting property of the specified container.
#
# This will get the specified property of the specified setting of the
# specified container.
#
# \param container_id The ID of the container to get the setting property
# of.
# \param setting_key The key of the setting to get the property of.
# \param property_name The property to obtain.
# \return The value of the specified property. The type of this property
# value depends on the type of the property. For instance, the "value"
# property of an integer setting will be a Python int, but the "value"
# property of an enum setting will be a Python str.
@pyqtSlot(str, str, str, result = QVariant)
def getContainerProperty(self, container_id: str, setting_key: str, property_name: str):
containers = self._container_registry.findContainers(id = container_id)
if not containers:
Logger.log("w", "Could not get properties of container %s because it was not found.", container_id)
return ""
container = containers[0]
return container.getProperty(setting_key, property_name)
@pyqtSlot(str, result = str) @pyqtSlot(str, result = str)
def makeUniqueName(self, original_name): def makeUniqueName(self, original_name):
return self._container_registry.uniqueName(original_name) return self._container_registry.uniqueName(original_name)

View File

@ -103,7 +103,6 @@ TabView
onYes: onYes:
{ {
Cura.ContainerManager.setContainerProperty(base.containerId, "material_diameter", "value", new_diameter_value);
base.setMetaDataEntry("approximate_diameter", old_approximate_diameter_value, getApproximateDiameter(new_diameter_value).toString()); base.setMetaDataEntry("approximate_diameter", old_approximate_diameter_value, getApproximateDiameter(new_diameter_value).toString());
base.setMetaDataEntry("properties/diameter", properties.diameter, new_diameter_value); base.setMetaDataEntry("properties/diameter", properties.diameter, new_diameter_value);
} }
@ -230,7 +229,7 @@ TabView
{ {
// This does not use a SettingPropertyProvider, because we need to make the change to all containers // This does not use a SettingPropertyProvider, because we need to make the change to all containers
// which derive from the same base_file // which derive from the same base_file
var old_diameter = Cura.ContainerManager.getContainerProperty(base.containerId, "material_diameter", "value").toString(); var old_diameter = Cura.ContainerManager.getContainerMetaDataEntry(base.containerId, "properties/diameter");
var old_approximate_diameter = Cura.ContainerManager.getContainerMetaDataEntry(base.containerId, "approximate_diameter"); var old_approximate_diameter = Cura.ContainerManager.getContainerMetaDataEntry(base.containerId, "approximate_diameter");
var new_approximate_diameter = getApproximateDiameter(value); var new_approximate_diameter = getApproximateDiameter(value);
if (new_approximate_diameter != Cura.ExtruderManager.getActiveExtruderStack().approximateMaterialDiameter) if (new_approximate_diameter != Cura.ExtruderManager.getActiveExtruderStack().approximateMaterialDiameter)
@ -242,7 +241,6 @@ TabView
confirmDiameterChangeDialog.open() confirmDiameterChangeDialog.open()
} }
else { else {
Cura.ContainerManager.setContainerProperty(base.containerId, "material_diameter", "value", value);
base.setMetaDataEntry("approximate_diameter", old_approximate_diameter, getApproximateDiameter(value).toString()); base.setMetaDataEntry("approximate_diameter", old_approximate_diameter, getApproximateDiameter(value).toString());
base.setMetaDataEntry("properties/diameter", properties.diameter, value); base.setMetaDataEntry("properties/diameter", properties.diameter, value);
} }
@ -271,7 +269,7 @@ TabView
{ {
id: spoolWeightSpinBox id: spoolWeightSpinBox
width: scrollView.columnWidth width: scrollView.columnWidth
value: base.getMaterialPreferenceValue(properties.guid, "spool_weight") value: base.getMaterialPreferenceValue(properties.guid, "spool_weight", Cura.ContainerManager.getContainerMetaDataEntry(properties.container_id, "properties/weight"))
suffix: " g" suffix: " g"
stepSize: 100 stepSize: 100
decimals: 0 decimals: 0
@ -468,7 +466,7 @@ TabView
} }
if(!spoolWeight) if(!spoolWeight)
{ {
spoolWeight = base.getMaterialPreferenceValue(properties.guid, "spool_weight"); spoolWeight = base.getMaterialPreferenceValue(properties.guid, "spool_weight", Cura.ContainerManager.getContainerMetaDataEntry(properties.container_id, "properties/weight"));
} }
if (diameter == 0 || density == 0 || spoolWeight == 0) if (diameter == 0 || density == 0 || spoolWeight == 0)
@ -517,21 +515,34 @@ TabView
// value has not changed // value has not changed
return; return;
} }
materialPreferenceValues[material_guid][entry_name] = new_value; if (entry_name in materialPreferenceValues[material_guid] && new_value.toString() == 0)
{
// no need to store a 0, that's the default, so remove it
materialPreferenceValues[material_guid].delete(entry_name);
if (!(materialPreferenceValues[material_guid]))
{
// remove empty map
materialPreferenceValues.delete(material_guid);
}
}
if (new_value.toString() != 0)
{
// store new value
materialPreferenceValues[material_guid][entry_name] = new_value;
}
// store preference // store preference
UM.Preferences.setValue("cura/material_settings", JSON.stringify(materialPreferenceValues)); UM.Preferences.setValue("cura/material_settings", JSON.stringify(materialPreferenceValues));
} }
function getMaterialPreferenceValue(material_guid, entry_name) function getMaterialPreferenceValue(material_guid, entry_name, default_value)
{ {
if(material_guid in materialPreferenceValues && entry_name in materialPreferenceValues[material_guid]) if(material_guid in materialPreferenceValues && entry_name in materialPreferenceValues[material_guid])
{ {
return materialPreferenceValues[material_guid][entry_name]; return materialPreferenceValues[material_guid][entry_name];
} }
default_value = default_value | 0;
var material_weight = Cura.ContainerManager.getContainerMetaDataEntry(base.containerId, "properties", "weight"); return default_value;
return material_weight || 0;
} }
// update the display name of the material // update the display name of the material

View File

@ -486,6 +486,7 @@ Item
materialProperties.name = currentItem.name ? currentItem.name : "Unknown"; materialProperties.name = currentItem.name ? currentItem.name : "Unknown";
materialProperties.guid = currentItem.guid; materialProperties.guid = currentItem.guid;
materialProperties.container_id = currentItem.container_id;
materialProperties.brand = currentItem.brand ? currentItem.brand : "Unknown"; materialProperties.brand = currentItem.brand ? currentItem.brand : "Unknown";
materialProperties.material = currentItem.material ? currentItem.material : "Unknown"; materialProperties.material = currentItem.material ? currentItem.material : "Unknown";
@ -543,6 +544,7 @@ Item
id: materialProperties id: materialProperties
property string guid: "00000000-0000-0000-0000-000000000000" property string guid: "00000000-0000-0000-0000-000000000000"
property string container_id: "Unknown";
property string name: "Unknown"; property string name: "Unknown";
property string profile_type: "Unknown"; property string profile_type: "Unknown";
property string brand: "Unknown"; property string brand: "Unknown";