Repository navigation
Expand file tree
/
Copy pathCrealityCloudOutputDevice.py
More file actions
126 lines (111 loc) · 5.58 KB
/
Copy pathCrealityCloudOutputDevice.py
File metadata and controls
126 lines (111 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
USE_QT5 = False
import os
from . CrealityCloudUtils import CrealityCloudUtils
try:
from PyQt6.QtCore import QObject
from PyQt6.QtGui import *
from PyQt6.QtNetwork import *
from PyQt6.QtQml import *
except ImportError:
from PyQt5.QtCore import QObject
from PyQt5.QtGui import *
from PyQt5.QtNetwork import *
from PyQt5.QtQml import *
USE_QT5 = True
from UM.Application import Application
from UM.Logger import Logger
from UM.Mesh.MeshWriter import MeshWriter
from UM.FileHandler.WriteFileJob import WriteFileJob
from UM.Message import Message
from UM.PluginRegistry import PluginRegistry
from UM.OutputDevice.OutputDevice import OutputDevice
from UM.OutputDevice import OutputDeviceError
from UM.i18n import i18nCatalog
catalog = i18nCatalog("uranium")
class CrealityCloudOutputDevice(OutputDevice):
def __init__(self, pluginId: str):
super().__init__("crealitycloud")
self._pluginId = pluginId
self.setName(catalog.i18nc("@item:inmenu", "Local File"))
self.setShortDescription(catalog.i18nc("@action:button Preceded by 'Ready to'.", "Upload to Creality Cloud"))
self.setDescription(catalog.i18nc(
"@action:button Preceded by 'Ready to'.", "Upload to Creality Cloud"))
self.setIconName("upload_gcode")
self.utils = CrealityCloudUtils.getInstance()
self.utils.saveGCodeStarted.connect(self.saveGCode)
self.utils.loginSuccess.connect(self.slotshowUploadGcodeDlg)
self.plugin_window = None
self._writing = False
self._nodes = None
self._stream = None
self._loginDlg = None
self._qml_folder = "qml" if not USE_QT5 else "qml_qt5"
def requestWrite(self, nodes, file_name = None, limit_mimetypes = None, file_handler = None, **kwargs) -> None:
if self._writing:
raise OutputDeviceError.DeviceBusyError()
self.utils.setDefaultFileName(file_name)
self._nodes = None
self._nodes = nodes
if self.plugin_window is not None:
self.plugin_window = None
self.writeStarted.emit(self)
if self.utils.getLogin():
self.plugin_window = self._createDialogue()
self.plugin_window.show()
else:
self._showLoginDlg()
def _createDialogue(self) -> QObject:
qml_file = os.path.join(PluginRegistry.getInstance().getPluginPath(self._pluginId), self._qml_folder, "UploadGcodeDlg.qml")
component = Application.getInstance().createQmlComponent(qml_file)
return component
def slotshowUploadGcodeDlg(self, type: int) -> None:
if type == 4:
if self.plugin_window is None:
self.plugin_window = self._createDialogue()
self.plugin_window.show()
def _showLoginDlg(self) -> None:
if not self._loginDlg:
self._loginDlg = self.utils.getLoginDlg()
if self._loginDlg:
self._loginDlg.setProperty("nextPage", 4)
self._loginDlg.show()
def saveGCode(self, file_name: str) -> None:
file_writer = Application.getInstance().getMeshFileHandler().getWriter("GCodeWriter")
Logger.log("d", "Writing GCode to %s", file_name)
self._stream = open(file_name, "wt", encoding = "utf-8")
try:
job = WriteFileJob(file_writer, self._stream, self._nodes, MeshWriter.OutputMode.TextMode)
job.setFileName(file_name)
job.progress.connect(self._onJobProgress)
job.finished.connect(self._onWriteJobFinished)
message = Message(catalog.i18nc("@info:progress Don't translate the XML tags <filename>!", "Saving to <filename>{0}</filename>").format(file_name),
0, False, -1, catalog.i18nc("@info:title", "Saving"))
message.show()
job.setMessage(message)
self._writing = True
job.start()
self.utils.updatedProgressTextSlot(catalog.i18nc("@info:status", "1/4 Output gcode file to local..."))
self.utils.updateStatus.emit("upload")
except PermissionError as e:
Logger.log("e", "Permission denied when trying to write to %s: %s", file_name, str(e))
raise OutputDeviceError.PermissionDeniedError(catalog.i18nc(
"@info:status Don't translate the XML tags <filename>!", "Permission denied when trying to save <filename>{0}</filename>").format(file_name)) from e
except OSError as e:
Logger.log("e", "Operating system would not let us write to %s: %s", file_name, str(e))
raise OutputDeviceError.WriteRequestFailedError(catalog.i18nc(
"@info:status Don't translate the XML tags <filename> or <message>!", "Could not save to <filename>{0}</filename>: <message>{1}</message>").format(file_name)) from e
def _onJobProgress(self, job: WriteFileJob, progress) -> None:
self.writeProgress.emit(self, progress)
def _onWriteJobFinished(self, job: WriteFileJob) -> None:
self._writing = False
self._stream.close()
self.writeFinished.emit(self)
if job.getResult():
self.writeSuccess.emit(self)
job.getStream().close()
self.utils.gzipFile()
else:
message = Message(catalog.i18nc("@info:status Don't translate the XML tags <filename> or <message>!", "Could not save to <filename>{0}</filename>: <message>{1}</message>").format(job.getFileName(), str(job.getError())), lifetime = 0, title = catalog.i18nc("@info:title", "Warning"))
message.show()
self.writeError.emit(self)
job.getStream().close()