diff options
Diffstat (limited to 'cura/Machines/Models/DiscoveredPrintersModel.py')
-rw-r--r-- | cura/Machines/Models/DiscoveredPrintersModel.py | 52 |
1 files changed, 33 insertions, 19 deletions
diff --git a/cura/Machines/Models/DiscoveredPrintersModel.py b/cura/Machines/Models/DiscoveredPrintersModel.py index 67d9c19d7e..459ec4d795 100644 --- a/cura/Machines/Models/DiscoveredPrintersModel.py +++ b/cura/Machines/Models/DiscoveredPrintersModel.py @@ -72,8 +72,6 @@ class DiscoveredPrinter(QObject): # Human readable machine type string @pyqtProperty(str, notify = machineTypeChanged) def readableMachineType(self) -> str: - from cura.CuraApplication import CuraApplication - machine_manager = CuraApplication.getInstance().getMachineManager() # In NetworkOutputDevice, when it updates a printer information, it updates the machine type using the field # "machine_variant", and for some reason, it's not the machine type ID/codename/... but a human-readable string # like "Ultimaker 3". The code below handles this case. @@ -117,12 +115,11 @@ class DiscoveredPrinter(QObject): return catalog.i18nc("@label", "Available networked printers") -# -# Discovered printers are all the printers that were found on the network, which provide a more convenient way -# to add networked printers (Plugin finds a bunch of printers, user can select one from the list, plugin can then -# add that printer to Cura as the active one). -# class DiscoveredPrintersModel(QObject): + """Discovered printers are all the printers that were found on the network, which provide a more convenient way to + add networked printers (Plugin finds a bunch of printers, user can select one from the list, plugin can then add + that printer to Cura as the active one). + """ def __init__(self, application: "CuraApplication", parent: Optional["QObject"] = None) -> None: super().__init__(parent) @@ -131,6 +128,7 @@ class DiscoveredPrintersModel(QObject): self._discovered_printer_by_ip_dict = dict() # type: Dict[str, DiscoveredPrinter] self._plugin_for_manual_device = None # type: Optional[OutputDevicePlugin] + self._network_plugin_queue = [] # type: List[OutputDevicePlugin] self._manual_device_address = "" self._manual_device_request_timeout_in_seconds = 5 # timeout for adding a manual device in seconds @@ -155,20 +153,25 @@ class DiscoveredPrintersModel(QObject): all_plugins_dict = self._application.getOutputDeviceManager().getAllOutputDevicePlugins() - can_add_manual_plugins = [item for item in filter( + self._network_plugin_queue = [item for item in filter( lambda plugin_item: plugin_item.canAddManualDevice(address) in priority_order, all_plugins_dict.values())] - if not can_add_manual_plugins: + if not self._network_plugin_queue: Logger.log("d", "Could not find a plugin to accept adding %s manually via address.", address) return - plugin = max(can_add_manual_plugins, key = lambda p: priority_order.index(p.canAddManualDevice(address))) - self._plugin_for_manual_device = plugin - self._plugin_for_manual_device.addManualDevice(address, callback = self._onManualDeviceRequestFinished) - self._manual_device_address = address - self._manual_device_request_timer.start() - self.hasManualDeviceRequestInProgressChanged.emit() + self._attemptToAddManualDevice(address) + + def _attemptToAddManualDevice(self, address: str) -> None: + if self._network_plugin_queue: + self._plugin_for_manual_device = self._network_plugin_queue.pop() + Logger.log("d", "Network plugin %s: attempting to add manual device with address %s.", + self._plugin_for_manual_device.getId(), address) + self._plugin_for_manual_device.addManualDevice(address, callback=self._onManualDeviceRequestFinished) + self._manual_device_address = address + self._manual_device_request_timer.start() + self.hasManualDeviceRequestInProgressChanged.emit() @pyqtSlot() def cancelCurrentManualDeviceRequest(self) -> None: @@ -183,8 +186,11 @@ class DiscoveredPrintersModel(QObject): self.manualDeviceRequestFinished.emit(False) def _onManualRequestTimeout(self) -> None: - Logger.log("w", "Manual printer [%s] request timed out. Cancel the current request.", self._manual_device_address) + address = self._manual_device_address + Logger.log("w", "Manual printer [%s] request timed out. Cancel the current request.", address) self.cancelCurrentManualDeviceRequest() + if self._network_plugin_queue: + self._attemptToAddManualDevice(address) hasManualDeviceRequestInProgressChanged = pyqtSignal() @@ -200,11 +206,13 @@ class DiscoveredPrintersModel(QObject): self._manual_device_address = "" self.hasManualDeviceRequestInProgressChanged.emit() self.manualDeviceRequestFinished.emit(success) + if not success and self._network_plugin_queue: + self._attemptToAddManualDevice(address) @pyqtProperty("QVariantMap", notify = discoveredPrintersChanged) def discoveredPrintersByAddress(self) -> Dict[str, DiscoveredPrinter]: return self._discovered_printer_by_ip_dict - + @pyqtProperty("QVariantList", notify = discoveredPrintersChanged) def discoveredPrinters(self) -> List["DiscoveredPrinter"]: item_list = list( @@ -256,8 +264,14 @@ class DiscoveredPrintersModel(QObject): del self._discovered_printer_by_ip_dict[ip_address] self.discoveredPrintersChanged.emit() - # A convenience function for QML to create a machine (GlobalStack) out of the given discovered printer. - # This function invokes the given discovered printer's "create_callback" to do this. + @pyqtSlot("QVariant") def createMachineFromDiscoveredPrinter(self, discovered_printer: "DiscoveredPrinter") -> None: + """A convenience function for QML to create a machine (GlobalStack) out of the given discovered printer. + + This function invokes the given discovered printer's "create_callback" to do this + + :param discovered_printer: + """ + discovered_printer.create_callback(discovered_printer.getKey()) |