-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCustomJobPrefix.py
131 lines (100 loc) · 5.09 KB
/
CustomJobPrefix.py
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
127
128
129
130
131
# Copyright (c) 2022 Aldo Hoeben / fieldOfView
# CustomJobPrefix is released under the terms of the AGPLv3 or higher.
import os.path
from UM.Extension import Extension
from UM.Logger import Logger
from UM.Version import Version
from cura.CuraApplication import CuraApplication
try:
from cura.ApplicationMetadata import CuraSDKVersion
except ImportError: # Cura <= 3.6
CuraSDKVersion = "6.0.0"
USE_QT5 = False
if CuraSDKVersion >= "8.0.0":
from PyQt6.QtCore import QObject, pyqtProperty, pyqtSignal, pyqtSlot
else:
from PyQt5.QtCore import QObject, pyqtProperty, pyqtSignal, pyqtSlot
USE_QT5 = True
from . import PrintInformationPatches
from . import OutputDevicePatcher
from UM.i18n import i18nCatalog
catalog = i18nCatalog("cura")
from typing import Optional
class CustomJobPrefix(Extension, QObject,):
def __init__(self, parent = None) -> None:
QObject.__init__(self, parent)
Extension.__init__(self)
self._application = CuraApplication.getInstance()
self._qml_folder = "qml" if not USE_QT5 else "qml_qt5"
self.addMenuItem(catalog.i18nc("@item:inmenu", "Set name options"), self.showNameDialog)
self._prefix_dialog = None # type: Optional[QObject]
self._print_information_patches = None # type: Optional[PrintInformationPatches.PrintInformationPatches]
self._output_device_patcher = OutputDevicePatcher.OutputDevicePatcher()
self._application.engineCreatedSignal.connect(self._onEngineCreated)
self._application.globalContainerStackChanged.connect(self._onGlobalStackChanged)
def _onEngineCreated(self) -> None:
self._print_information_patches = PrintInformationPatches.PrintInformationPatches(self._application.getPrintInformation())
self._createAdditionalComponentsView()
def _createAdditionalComponentsView(self) -> None:
Logger.log("d", "Creating additional ui components for CustomJobPrefix")
try:
major_api_version = self._application.getAPIVersion().getMajor()
except AttributeError:
# UM.Application.getAPIVersion was added for API > 6 (Cura 4)
# Since this plugin version is only compatible with Cura 3.5 and newer, it is safe to assume API 5
major_api_version = 5
if not USE_QT5:
qml_file = "JobSpecsPatcher.qml"
elif major_api_version <= 5:
qml_file = "JobSpecsPatcher3x.qml"
else:
qml_file = "JobSpecsPatcher4x.qml"
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), self._qml_folder, qml_file)
self._additional_components = self._application.createQmlComponent(path, {"customJobPrefix": self})
if not self._additional_components:
Logger.log("w", "Could not create additional components for CustomJobPrefix")
return
self._application.addAdditionalComponent("jobSpecsButton", self._additional_components)
self._additional_components.patchParent()
def _onGlobalStackChanged(self) -> None:
self.jobAffixesChanged.emit()
@pyqtSlot()
def showNameDialog(self) -> None:
global_container_stack = self._application.getGlobalContainerStack()
if not global_container_stack:
return
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), self._qml_folder, "PrefixDialog.qml")
self._prefix_dialog = self._application.createQmlComponent(path, {"manager": self})
if self._prefix_dialog:
self._prefix_dialog.show()
jobAffixesChanged = pyqtSignal()
@pyqtSlot(str, str, str)
def setJobAffixes(self, prefix: str, postfix: str, path: str) -> None:
global_container_stack = self._application.getGlobalContainerStack()
if not global_container_stack:
return
global_container_stack.setMetaDataEntry("custom_job_prefix", prefix)
global_container_stack.setMetaDataEntry("custom_job_postfix", postfix)
global_container_stack.setMetaDataEntry("custom_job_path", path)
self.jobAffixesChanged.emit()
@pyqtProperty(str, notify=jobAffixesChanged)
def jobPrefix(self) -> str:
global_container_stack = self._application.getGlobalContainerStack()
if not global_container_stack:
return ""
return global_container_stack.getMetaDataEntry("custom_job_prefix", "{printer_type}")
@pyqtProperty(str, notify=jobAffixesChanged)
def jobPostfix(self) -> str:
global_container_stack = self._application.getGlobalContainerStack()
if not global_container_stack:
return ""
return global_container_stack.getMetaDataEntry("custom_job_postfix", "")
@pyqtProperty(str, notify=jobAffixesChanged)
def jobPath(self) -> str:
global_container_stack = self._application.getGlobalContainerStack()
if not global_container_stack:
return ""
return global_container_stack.getMetaDataEntry("custom_job_path", "")
@pyqtProperty(QObject, constant=True)
def printInformation(self) -> Optional[PrintInformationPatches.PrintInformationPatches]:
return self._print_information_patches