Bring back the printer selection dialog for networked printers

This commit is contained in:
ChrisTerBeke 2019-10-25 12:25:41 +02:00
parent d905b8b8cc
commit 9a3ff527ac
4 changed files with 80 additions and 42 deletions

View file

@ -1,16 +1,17 @@
# Copyright (c) 2019 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.
import os
from typing import Optional, Dict, List, Callable, Any
from PyQt5.QtGui import QDesktopServices
from PyQt5.QtCore import pyqtSlot, QUrl, pyqtSignal, pyqtProperty
from PyQt5.QtCore import pyqtSlot, QUrl, pyqtSignal, pyqtProperty, QObject
from PyQt5.QtNetwork import QNetworkReply
from UM.FileHandler.FileHandler import FileHandler
from UM.i18n import i18nCatalog
from UM.Logger import Logger
from UM.Scene.SceneNode import SceneNode
from cura.CuraApplication import CuraApplication
from cura.PrinterOutput.NetworkedPrinterOutputDevice import AuthState
from cura.PrinterOutput.PrinterOutputDevice import ConnectionType
@ -42,6 +43,8 @@ class LocalClusterOutputDevice(UltimakerNetworkedPrinterOutputDevice):
)
self._cluster_api = None # type: Optional[ClusterApiClient]
self._active_exported_job = None # type: Optional[ExportFileJob]
self._printer_select_dialog = None # type: Optional[QObject]
# We don't have authentication over local networking, so we're always authenticated.
self.setAuthenticationState(AuthState.Authenticated)
@ -129,17 +132,49 @@ class LocalClusterOutputDevice(UltimakerNetworkedPrinterOutputDevice):
job.finished.connect(self._onPrintJobCreated)
job.start()
## Allows the user to choose a printer to print with from the printer selection dialogue.
# \param unique_name: The unique name of the printer to target.
@pyqtSlot(str, name="selectTargetPrinter")
def selectTargetPrinter(self, unique_name: str = "") -> None:
self._startPrintJobUpload(unique_name if unique_name != "" else None)
## Handler for when the print job was created locally.
# It can now be sent over the network.
def _onPrintJobCreated(self, job: ExportFileJob) -> None:
self._active_exported_job = job
# TODO: add preference to enable/disable this feature?
if self.clusterSize > 1:
self._showPrinterSelectionDialog() # self._startPrintJobUpload will be triggered from this dialog
return
self._startPrintJobUpload()
## Shows a dialog allowing the user to select which printer in a group to send a job to.
def _showPrinterSelectionDialog(self) -> None:
if not self._printer_select_dialog:
path = os.path.join(CuraApplication.getInstance().getPluginRegistry().getPluginPath("UM3NetworkPrinting"),
"resources", "qml", "PrintWindow.qml")
self._printer_select_dialog = CuraApplication.getInstance().createQmlComponent(path, {"OutputDevice": self})
self._printer_select_dialog.show()
## Upload the print job to the group.
def _startPrintJobUpload(self, unique_name: str = None) -> None:
if not self._active_exported_job:
Logger.log("e", "No active exported job to upload!")
return
self._progress.show()
parts = [
self._createFormPart("name=owner", bytes(self._getUserName(), "utf-8"), "text/plain"),
self._createFormPart("name=\"file\"; filename=\"%s\"" % job.getFileName(), job.getOutput())
self._createFormPart("name=\"file\"; filename=\"%s\"" % self._active_exported_job.getFileName(),
self._active_exported_job.getOutput())
]
# If a specific printer was selected we include the name in the request.
# FIXME: Connect should allow the printer UUID here instead of the 'unique_name'.
if unique_name is not None:
parts.append(self._createFormPart("name=require_printer_name", bytes(unique_name, "utf-8"), "text/plain"))
# FIXME: move form posting to API client
self.postFormWithParts("/cluster-api/v1/print_jobs/", parts, on_finished=self._onPrintUploadCompleted,
on_progress=self._onPrintJobUploadProgress)
self._active_exported_job = None
## Handler for print job upload progress.
def _onPrintJobUploadProgress(self, bytes_sent: int, bytes_total: int) -> None: