From b37fa6f7184e75c373dea734918bd71baa56e821 Mon Sep 17 00:00:00 2001 From: Ghostkeeper Date: Thu, 22 Jun 2017 12:01:42 +0200 Subject: [PATCH] Implement warning message when you change material diameter The undo functionality currently gives an error though. Investigating... Contributes to issue CURA-2822. --- cura/CuraApplication.py | 11 ++++- cura/Settings/MaterialManager.py | 56 ++++++++++++++++++++++ resources/qml/Preferences/MaterialView.qml | 9 +++- 3 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 cura/Settings/MaterialManager.py diff --git a/cura/CuraApplication.py b/cura/CuraApplication.py index 7995c77fce..332b103502 100755 --- a/cura/CuraApplication.py +++ b/cura/CuraApplication.py @@ -1,5 +1,6 @@ -# Copyright (c) 2015 Ultimaker B.V. +# Copyright (c) 2017 Ultimaker B.V. # Cura is released under the terms of the AGPLv3 or higher. + from PyQt5.QtNetwork import QLocalServer from PyQt5.QtNetwork import QLocalSocket @@ -62,6 +63,7 @@ from . import CameraImageProvider from . import MachineActionManager from cura.Settings.MachineManager import MachineManager +from cura.Settings.MaterialManager import MaterialManager from cura.Settings.ExtruderManager import ExtruderManager from cura.Settings.UserChangesModel import UserChangesModel from cura.Settings.ExtrudersModel import ExtrudersModel @@ -189,6 +191,7 @@ class CuraApplication(QtApplication): self._machine_action_manager = MachineActionManager.MachineActionManager() self._machine_manager = None # This is initialized on demand. + self._material_manager = None self._setting_inheritance_manager = None self._additional_components = {} # Components to add to certain areas in the interface @@ -640,6 +643,7 @@ class CuraApplication(QtApplication): # Initialise extruder so as to listen to global container stack changes before the first global container stack is set. ExtruderManager.getInstance() qmlRegisterSingletonType(MachineManager, "Cura", 1, 0, "MachineManager", self.getMachineManager) + qmlRegisterSingletonType(MaterialManager, "Cura", 1, 0, "MaterialManager", self.getMaterialManager) qmlRegisterSingletonType(SettingInheritanceManager, "Cura", 1, 0, "SettingInheritanceManager", self.getSettingInheritanceManager) @@ -665,6 +669,11 @@ class CuraApplication(QtApplication): self._machine_manager = MachineManager.createMachineManager() return self._machine_manager + def getMaterialManager(self, *args): + if self._material_manager is None: + self._material_manager = MaterialManager.createMaterialManager() + return self._material_manager + def getSettingInheritanceManager(self, *args): if self._setting_inheritance_manager is None: self._setting_inheritance_manager = SettingInheritanceManager.createSettingInheritanceManager() diff --git a/cura/Settings/MaterialManager.py b/cura/Settings/MaterialManager.py new file mode 100644 index 0000000000..88baf9419b --- /dev/null +++ b/cura/Settings/MaterialManager.py @@ -0,0 +1,56 @@ +# Copyright (c) 2017 Ultimaker B.V. +# Cura is released under the terms of the AGPLv3 or higher. + +from PyQt5.QtCore import QObject, pyqtSlot #To expose data to QML. + +from UM.Logger import Logger +from UM.Message import Message #To create a warning message about material diameter. +from UM.i18n import i18nCatalog #Translated strings. +from UM.Settings.ContainerRegistry import ContainerRegistry #To find the material containers we need. + +catalog = i18nCatalog("cura") + +## Handles material-related data, processing requests to change them and +# providing data for the GUI. +# +# TODO: Move material-related managing over from the machine manager to here. +class MaterialManager(QObject): + ## Creates the global values for the material manager to use. + def __init__(self, parent = None): + super().__init__(parent) + + #Material diameter changed warning message. + self._material_diameter_warning_message = Message(catalog.i18nc("@info:status Has a cancel button next to it.", + "The selected material diameter causes the material to become incompatible with the current printer.")) + self._material_diameter_warning_message.addAction("Undo", catalog.i18nc("@action:button", "Undo"), None, catalog.i18nc("@action", "Undo changing the material diameter.")) + self._material_diameter_warning_message.actionTriggered = self._materialWarningMessageAction + + ## Creates an instance of the MaterialManager. + # + # This should only be called by PyQt to create the singleton instance of + # this class. + @staticmethod + def createMaterialManager(engine = None, script_engine = None): + return MaterialManager() + + @pyqtSlot(str, str) + def showMaterialWarningMessage(self, material_id, previous_diameter): + self._material_diameter_warning_message.previous_diameter = previous_diameter #Make sure that the undo button can properly undo the action. + self._material_diameter_warning_message.material_id = material_id + self._material_diameter_warning_message.show() + + ## Called when clicking "undo" on the warning dialogue for disappeared + # materials. + # + # This executes the undo action, restoring the material diameter. + # + # \param button The identifier of the button that was pressed. + def _materialWarningMessageAction(self, button): + if button == "Undo": + container_registry = ContainerRegistry.getInstance() + matches = container_registry.findInstanceContainers(type = "material", id = self._material_diameter_warning_message.material_id) + if matches: + matches[0].setMetaDataEntry("diameter", self._material_diameter_warning_message.previous_diameter) + #No matches? Then the material has been deleted in the meanwhile. + else: + Logger.log("w", "Unknown button action for material diameter warning message: {action}".format(action = button)) \ No newline at end of file diff --git a/resources/qml/Preferences/MaterialView.qml b/resources/qml/Preferences/MaterialView.qml index 30edb063c1..8942549265 100644 --- a/resources/qml/Preferences/MaterialView.qml +++ b/resources/qml/Preferences/MaterialView.qml @@ -1,5 +1,5 @@ -// Copyright (c) 2016 Ultimaker B.V. -// Uranium is released under the terms of the AGPLv3 or higher. +// Copyright (c) 2017 Ultimaker B.V. +// Cura is released under the terms of the AGPLv3 or higher. import QtQuick 2.1 import QtQuick.Controls 1.3 @@ -168,8 +168,13 @@ TabView { // This does not use a SettingPropertyProvider, because we need to make the change to all containers // which derive from the same base_file + var old_diameter = Cura.ContainerManager.getContainerMetaDataEntry(base.containerId, "properties/diameter"); base.setMetaDataEntry("approximate_diameter", properties.approximate_diameter, Math.round(value).toString()); base.setMetaDataEntry("properties/diameter", properties.diameter, value); + if (Cura.MachineManager.filterMaterialsByMachine && properties.approximate_diameter != Math.round(Cura.MachineManager.activeMachine.approximateMaterialDiameter).toString()) + { + Cura.MaterialManager.showMaterialWarningMessage(base.containerId, old_diameter); + } Cura.ContainerManager.setContainerProperty(base.containerId, "material_diameter", "value", value); } onValueChanged: updateCostPerMeter()