diff --git a/cura/Machines/Models/DiscoveredPrintersModel.py b/cura/Machines/Models/DiscoveredPrintersModel.py index 0bacdc804b..c36af1afe0 100644 --- a/cura/Machines/Models/DiscoveredPrintersModel.py +++ b/cura/Machines/Models/DiscoveredPrintersModel.py @@ -1,3 +1,5 @@ + + from typing import Callable, List, Optional, TYPE_CHECKING from PyQt5.QtCore import pyqtSlot, pyqtProperty, pyqtSignal, QObject @@ -13,6 +15,7 @@ class DiscoveredPrinter(QObject): def __init__(self, ip_address: str, key: str, name: str, create_callback: Callable[[str], None], machine_type: str, device, parent = None) -> None: super().__init__(parent) + self._ip_address = ip_address self._key = key self._name = name @@ -40,10 +43,11 @@ class DiscoveredPrinter(QObject): def machine_type(self) -> str: return self._machine_type - def setMachineType(self, machine_type: str) -> None: - if self._machine_type != machine_type: - self._machine_type = machine_type - self.machineTypeChanged.emit() + # Machine type string with underscores "_" replaced with spaces " " + @pyqtProperty(str, notify = machineTypeChanged) + def machine_type_with_spaces(self) -> str: + from cura.CuraApplication import CuraApplication + return CuraApplication.getInstance().getMachineManager().getMachineTypeNameFromId(self._machine_type) @pyqtProperty(QObject, constant = True) def device(self): diff --git a/cura/Settings/MachineManager.py b/cura/Settings/MachineManager.py index aac18799f6..c910b67d0c 100755 --- a/cura/Settings/MachineManager.py +++ b/cura/Settings/MachineManager.py @@ -1657,3 +1657,47 @@ class MachineManager(QObject): abbr_machine += stripped_word return abbr_machine + + def getMachineTypeNameFromId(self, machine_type_id: str) -> str: + machine_type_name = "" + results = self._container_registry.findDefinitionContainersMetadata(id = machine_type_id) + if results: + machine_type_name = results[0]["name"] + return machine_type_name + + @pyqtSlot(QObject) + def associateActiveMachineWithPrinterDevice(self, printer_device: Optional["PrinterOutputDevice"]) -> None: + if not printer_device: + return + + Logger.log("d", "Attempting to set the network key of the active machine to %s", printer_device.key) + + global_stack = self._global_container_stack + if not global_stack: + return + + meta_data = global_stack.getMetaData() + + if "um_network_key" in meta_data: # Global stack already had a connection, but it's changed. + old_network_key = meta_data["um_network_key"] + # Since we might have a bunch of hidden stacks, we also need to change it there. + metadata_filter = {"um_network_key": old_network_key} + containers = self._container_registry.findContainerStacks(type = "machine", **metadata_filter) + + for container in containers: + container.setMetaDataEntry("um_network_key", printer_device.key) + + # Delete old authentication data. + Logger.log("d", "Removing old authentication id %s for device %s", + global_stack.getMetaDataEntry("network_authentication_id", None), + printer_device.key) + + container.removeMetaDataEntry("network_authentication_id") + container.removeMetaDataEntry("network_authentication_key") + + # Ensure that these containers do know that they are configured for network connection + container.addConfiguredConnectionType(printer_device.connectionType.value) + + else: # Global stack didn't have a connection yet, configure it. + global_stack.setMetaDataEntry("um_network_key", printer_device.key) + global_stack.addConfiguredConnectionType(printer_device.connectionType.value) diff --git a/plugins/UM3NetworkPrinting/src/UM3OutputDevicePlugin.py b/plugins/UM3NetworkPrinting/src/UM3OutputDevicePlugin.py index ea2142d033..2729b5466e 100644 --- a/plugins/UM3NetworkPrinting/src/UM3OutputDevicePlugin.py +++ b/plugins/UM3NetworkPrinting/src/UM3OutputDevicePlugin.py @@ -218,7 +218,22 @@ class UM3OutputDevicePlugin(OutputDevicePlugin): def _createMachineFromDiscoveredPrinter(self, key: str) -> None: # TODO: This needs to be implemented. It's supposed to create a machine given a unique key as already discovered # by this plugin. - pass + discovered_device = self._discovered_devices.get(key) + if discovered_device is None: + Logger.log("e", "Could not find discovered device with key [%s]", key) + return + + group_name = discovered_device.getProperty("name") + machine_type_id = discovered_device.getProperty("printer_type") + + Logger.log("i", "Creating machine from network device with key = [%s], group name = [%s], printer type = [%s]", + key, group_name, machine_type_id) + + self._application.getMachineManager().addMachine(machine_type_id, group_name) + # connect the new machine to that network printer + self._application.getMachineManager().associateActiveMachineWithPrinterDevice(discovered_device) + # ensure that the connection states are refreshed. + self.reCheckConnections() def _checkManualDevice(self, address): # Check if a UM3 family device exists at this address. diff --git a/resources/qml/PrinterSelector/MachineSelectorButton.qml b/resources/qml/PrinterSelector/MachineSelectorButton.qml index 285ab4b599..3c421262e5 100644 --- a/resources/qml/PrinterSelector/MachineSelectorButton.qml +++ b/resources/qml/PrinterSelector/MachineSelectorButton.qml @@ -24,6 +24,8 @@ Button property var outputDevice: null property var printerTypesList: [] + property var updatePrinterTypesFunction: updatePrinterTypesList + function updatePrinterTypesList() { printerTypesList = (outputDevice != null) ? outputDevice.uniquePrinterTypes : [] @@ -87,14 +89,14 @@ Button Connections { target: outputDevice - onUniqueConfigurationsChanged: updatePrinterTypesList() + onUniqueConfigurationsChanged: updatePrinterTypesFunction() } Connections { target: Cura.MachineManager - onOutputDevicesChanged: updatePrinterTypesList() + onOutputDevicesChanged: updatePrinterTypesFunction() } - Component.onCompleted: updatePrinterTypesList() + Component.onCompleted: updatePrinterTypesFunction() } diff --git a/resources/qml/WelcomePages/AddNetworkPrinterScrollView.qml b/resources/qml/WelcomePages/AddNetworkPrinterScrollView.qml index 79e1cafe2e..fa3b87c07f 100644 --- a/resources/qml/WelcomePages/AddNetworkPrinterScrollView.qml +++ b/resources/qml/WelcomePages/AddNetworkPrinterScrollView.qml @@ -57,6 +57,13 @@ Item anchors.rightMargin: 10 outputDevice: modelData.device + updatePrinterTypesFunction: updateMachineTypes + + function updateMachineTypes() + { + printerTypesList = [ modelData.machine_type_with_spaces ] + } + checkable: false selected: ListView.view.currentIndex == model.index onClicked: