|
| 1 | +from UM.i18n import i18nCatalog |
| 2 | +from UM.Logger import Logger |
| 3 | +from UM.Settings.DefinitionContainer import DefinitionContainer |
| 4 | +from UM.Application import Application |
| 5 | + |
| 6 | +from UM.Settings.ContainerRegistry import ContainerRegistry |
| 7 | +from cura.MachineAction import MachineAction |
| 8 | +from UM.PluginRegistry import PluginRegistry |
| 9 | +from cura.CuraApplication import CuraApplication |
| 10 | + |
| 11 | +from PyQt5.QtCore import pyqtSignal, pyqtProperty, pyqtSlot, QUrl, QObject |
| 12 | +from PyQt5.QtQml import QQmlComponent, QQmlContext |
| 13 | +from PyQt5.QtGui import QDesktopServices |
| 14 | +from PyQt5.QtNetwork import QNetworkRequest, QNetworkAccessManager |
| 15 | + |
| 16 | +import os.path |
| 17 | +import json |
| 18 | +import base64 |
| 19 | +import time |
| 20 | + |
| 21 | +from PyQt5.QtCore import QTimer |
| 22 | + |
| 23 | +catalog = i18nCatalog("cura") |
| 24 | + |
| 25 | +class QidiMachineConfig(MachineAction): |
| 26 | + printersChanged = pyqtSignal() |
| 27 | + printersTryToConnect = pyqtSignal() |
| 28 | + |
| 29 | + def __init__(self): |
| 30 | + super().__init__("QidiMachineConfig", "QidiPrint") |
| 31 | + self._qml_url = "qml//QidiMachineConfig.qml" |
| 32 | + ContainerRegistry.getInstance().containerAdded.connect(self._onContainerAdded) |
| 33 | + |
| 34 | + self._application = CuraApplication.getInstance() |
| 35 | + self._network_plugin = None |
| 36 | + |
| 37 | + self.__additional_components_context = None |
| 38 | + self.__additional_component = None |
| 39 | + self.__additional_components_view = None |
| 40 | + |
| 41 | + @pyqtSlot() |
| 42 | + def runDiscovery(self): |
| 43 | + if not self._network_plugin: |
| 44 | + Logger.log("d", "Starting printer discovery.") |
| 45 | + self._network_plugin = self._application.getOutputDeviceManager().getOutputDevicePlugin(self._plugin_id) |
| 46 | + if not self._network_plugin: |
| 47 | + return |
| 48 | + self._network_plugin.printerListChanged.connect(self._onPrinterDiscoveryChanged) |
| 49 | + self.printersChanged.emit() |
| 50 | + self._network_plugin.startDiscovery() |
| 51 | + |
| 52 | + # Re-filters the list of printers. |
| 53 | + @pyqtSlot() |
| 54 | + def reset(self): |
| 55 | + Logger.log("d", "Reset the list of found printers.") |
| 56 | + self.printersChanged.emit() |
| 57 | + |
| 58 | + @pyqtSlot(str) |
| 59 | + def removePrinter(self, key): |
| 60 | + if self._network_plugin: |
| 61 | + self._network_plugin.removePrinter(key) |
| 62 | + |
| 63 | + @pyqtSlot(str, str, str) |
| 64 | + def setManualPrinter(self, oldName, name, address): |
| 65 | + if oldName != "": |
| 66 | + # This manual printer replaces a current manual printer |
| 67 | + self._network_plugin.removePrinter(oldName) |
| 68 | + if address != "": |
| 69 | + self._network_plugin.addPrinter(name, address) |
| 70 | + |
| 71 | + @pyqtSlot(str, str, result = bool) |
| 72 | + def validName(self, oldName, newName): |
| 73 | + if not newName: |
| 74 | + # empty string isn't allowed |
| 75 | + return False |
| 76 | + if oldName == newName: |
| 77 | + # if name hasn't changed, it is not a duplicate |
| 78 | + return True |
| 79 | + # duplicates not allowed |
| 80 | + return (not newName in self._network_plugin._instances.keys()) |
| 81 | + |
| 82 | + def _onPrinterDiscoveryChanged(self, *args): |
| 83 | + self.printersChanged.emit() |
| 84 | + |
| 85 | + @pyqtProperty("QVariantList", notify=printersChanged) |
| 86 | + def foundDevices(self): |
| 87 | + if self._network_plugin: |
| 88 | + printers = list(self._network_plugin.getPrinters().values()) |
| 89 | + printers.sort(key=lambda k: k.name) |
| 90 | + return printers |
| 91 | + else: |
| 92 | + return [] |
| 93 | + |
| 94 | + @pyqtSlot() |
| 95 | + def changestage(self): |
| 96 | + CuraApplication.getInstance().getController().setActiveStage("MonitorStage") |
| 97 | + |
| 98 | + @pyqtSlot(str) |
| 99 | + def disconnect(self, key): |
| 100 | + global_container_stack = self._application.getGlobalContainerStack() |
| 101 | + if global_container_stack: |
| 102 | + meta_data = global_container_stack.getMetaData() |
| 103 | + if "qidi_active_printer" in meta_data: |
| 104 | + global_container_stack.setMetaDataEntry("qidi_active_printer", None) |
| 105 | + Logger.log("d", "disconnecting '{}'", key) |
| 106 | + if self._network_plugin: |
| 107 | + self._network_plugin.disconnect(key) |
| 108 | + |
| 109 | + @pyqtSlot(str) |
| 110 | + def setKey(self, key): |
| 111 | + Logger.log("d", "QidiPrint Plugin the network key of the active machine to %s", key) |
| 112 | + global_container_stack = self._application.getGlobalContainerStack() |
| 113 | + if global_container_stack: |
| 114 | + meta_data = global_container_stack.getMetaData() |
| 115 | + if "qidi_active_printer" in meta_data: |
| 116 | + global_container_stack.setMetaDataEntry("qidi_active_printer", key) |
| 117 | + else: |
| 118 | + Logger.log("d", "QidiPrint Plugin add dataEntry") |
| 119 | + global_container_stack.setMetaDataEntry("qidi_active_printer", key) |
| 120 | + |
| 121 | + if self._network_plugin: |
| 122 | + # Ensure that the connection states are refreshed. |
| 123 | + Application.getInstance().globalContainerStackChanged.emit() |
| 124 | + |
| 125 | + @pyqtSlot(result=str) |
| 126 | + def getStoredKey(self): |
| 127 | + global_container_stack = self._application.getGlobalContainerStack() |
| 128 | + if global_container_stack: |
| 129 | + meta_data = global_container_stack.getMetaData() |
| 130 | + if "qidi_active_printer" in meta_data: |
| 131 | + return global_container_stack.getMetaDataEntry("qidi_active_printer") |
| 132 | + return "" |
| 133 | + |
| 134 | + def _onContainerAdded(self, container): |
| 135 | + # Add this action as a supported action to all machine definitions |
| 136 | + if isinstance(container, DefinitionContainer) and container.getMetaDataEntry("type") == "machine" and container.getMetaDataEntry("manufacturer") == 'Qidi': |
| 137 | + self._application.getMachineActionManager().addSupportedAction(container.getId(), self.getKey()) |
| 138 | + |
0 commit comments