From c9b8b0333c87ba5021e4d29a3cba120ea1f0ad1d Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Thu, 4 Jan 2018 14:56:42 +0100 Subject: [PATCH] Sending prints to a specific printer in the cluster is now possible again CL-541 --- .../ClusterUM3OutputDevice.py | 30 ++++++++++++--- plugins/UM3NetworkPrinting/PrintWindow.qml | 38 +++++++++++-------- 2 files changed, 46 insertions(+), 22 deletions(-) diff --git a/plugins/UM3NetworkPrinting/ClusterUM3OutputDevice.py b/plugins/UM3NetworkPrinting/ClusterUM3OutputDevice.py index 51e713904b..d13706cc4c 100644 --- a/plugins/UM3NetworkPrinting/ClusterUM3OutputDevice.py +++ b/plugins/UM3NetworkPrinting/ClusterUM3OutputDevice.py @@ -62,11 +62,15 @@ class ClusterUM3OutputDevice(NetworkedPrinterOutputDevice): self._active_printer = None # type: Optional[PrinterOutputModel] + self._printer_selection_dialog = None + self.setPriority(3) # Make sure the output device gets selected above local file output self.setName(self._id) self.setShortDescription(i18n_catalog.i18nc("@action:button Preceded by 'Ready to'.", "Print over network")) self.setDescription(i18n_catalog.i18nc("@properties:tooltip", "Print over network")) + self._printer_uuid_to_unique_name_mapping = {} + self._finished_jobs = [] def requestWrite(self, nodes, file_name=None, filter_by_machine=False, file_handler=None, **kwargs): @@ -79,11 +83,21 @@ class ClusterUM3OutputDevice(NetworkedPrinterOutputDevice): # Unable to find g-code. Nothing to send return - # TODO; DEBUG - self.sendPrintJob() + if len(self._printers) > 1: + self._spawnPrinterSelectionDialog() + else: + self.sendPrintJob() + + def _spawnPrinterSelectionDialog(self): + if self._printer_selection_dialog is None: + path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "PrintWindow.qml") + self._printer_selection_dialog = Application.getInstance().createQmlComponent(path, {"OutputDevice": self}) + if self._printer_selection_dialog is not None: + self._printer_selection_dialog.show() @pyqtSlot() - def sendPrintJob(self): + @pyqtSlot(str) + def sendPrintJob(self, target_printer = ""): Logger.log("i", "Sending print job to printer.") if self._sending_gcode: self._error_message = Message( @@ -108,9 +122,9 @@ class ClusterUM3OutputDevice(NetworkedPrinterOutputDevice): parts = [] # If a specific printer was selected, it should be printed with that machine. - require_printer_name = "" # Todo; actually needs to be set - if require_printer_name: - parts.append(self._createFormPart("name=require_printer_name", bytes(require_printer_name, "utf-8"), "text/plain")) + if target_printer: + target_printer = self._printer_uuid_to_unique_name_mapping[target_printer] + parts.append(self._createFormPart("name=require_printer_name", bytes(target_printer, "utf-8"), "text/plain")) # Add user name to the print_job parts.append(self._createFormPart("name=owner", bytes(self._getUserName(), "utf-8"), "text/plain")) @@ -324,6 +338,10 @@ class ClusterUM3OutputDevice(NetworkedPrinterOutputDevice): print_job.updateOwner(data["owner"]) def _updatePrinter(self, printer, data): + # For some unknown reason the cluster wants UUID for everything, except for sending a job directly to a printer. + # Then we suddenly need the unique name. So in order to not have to mess up all the other code, we save a mapping. + self._printer_uuid_to_unique_name_mapping[data["uuid"]] = data["unique_name"] + printer.updateName(data["friendly_name"]) printer.updateKey(data["uuid"]) printer.updateType(data["machine_variant"]) diff --git a/plugins/UM3NetworkPrinting/PrintWindow.qml b/plugins/UM3NetworkPrinting/PrintWindow.qml index 7afe174da2..13d087f930 100644 --- a/plugins/UM3NetworkPrinting/PrintWindow.qml +++ b/plugins/UM3NetworkPrinting/PrintWindow.qml @@ -20,8 +20,24 @@ UM.Dialog visible: true modality: Qt.ApplicationModal + onVisibleChanged: + { + if(visible) + { + resetPrintersModel() + } + } + title: catalog.i18nc("@title:window", "Print over network") - title: catalog.i18nc("@title:window","Print over network") + property var printersModel: ListModel{} + function resetPrintersModel() { + printersModel.append({ name: "Automatic", key: ""}) + + for (var index in OutputDevice.printers) + { + printersModel.append({name: OutputDevice.printers[index].name, key: OutputDevice.printers[index].key}) + } + } Column { @@ -31,8 +47,7 @@ UM.Dialog anchors.topMargin: UM.Theme.getSize("default_margin").height anchors.leftMargin: UM.Theme.getSize("default_margin").width anchors.rightMargin: UM.Theme.getSize("default_margin").width - height: 50 * screenScaleFactor - + height: 50 * screenScaleFactord Label { id: manualPrinterSelectionLabel @@ -42,7 +57,7 @@ UM.Dialog topMargin: UM.Theme.getSize("default_margin").height right: parent.right } - text: "Printer selection" + text: catalog.i18nc("@label", "Printer selection") wrapMode: Text.Wrap height: 20 * screenScaleFactor } @@ -50,18 +65,12 @@ UM.Dialog ComboBox { id: printerSelectionCombobox - model: OutputDevice.printers - textRole: "friendly_name" + model: base.printersModel + textRole: "name" width: parent.width height: 40 * screenScaleFactor Behavior on height { NumberAnimation { duration: 100 } } - - onActivated: - { - var printerData = OutputDevice.printers[index]; - OutputDevice.selectPrinter(printerData.unique_name, printerData.friendly_name); - } } SystemPalette @@ -79,8 +88,6 @@ UM.Dialog enabled: true onClicked: { base.visible = false; - // reset to defaults - OutputDevice.selectAutomaticPrinter() printerSelectionCombobox.currentIndex = 0 } } @@ -93,9 +100,8 @@ UM.Dialog enabled: true onClicked: { base.visible = false; - OutputDevice.sendPrintJob(); + OutputDevice.sendPrintJob(printerSelectionCombobox.model.get(printerSelectionCombobox.currentIndex).key) // reset to defaults - OutputDevice.selectAutomaticPrinter() printerSelectionCombobox.currentIndex = 0 } }