-
Notifications
You must be signed in to change notification settings - Fork 4
/
OctoPrintPlugin.py
105 lines (85 loc) · 3.68 KB
/
OctoPrintPlugin.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
import os.path
import json
from PyQt5.QtCore import QObject, QUrl, pyqtProperty, pyqtSignal, pyqtSlot
from PyQt5.QtQml import QQmlComponent, QQmlContext
from UM.Message import Message
from UM.Logger import Logger
from UM.Application import Application
from UM.Preferences import Preferences
from UM.Extension import Extension
from UM.PluginRegistry import PluginRegistry
from UM.OutputDevice.OutputDevicePlugin import OutputDevicePlugin
from . import OctoPrintOutputDevice
from UM.i18n import i18nCatalog
catalog = i18nCatalog("cura")
class OctoPrintExtension(QObject, Extension, OutputDevicePlugin):
def __init__(self, parent = None):
QObject.__init__(self, parent)
Extension.__init__(self)
OutputDevicePlugin.__init__(self)
self.addMenuItem(catalog.i18n("OctoPrint Servers"), self.showSettingsDialog)
self._dialogs = {}
self._dialogView = None
Preferences.getInstance().addPreference("octoprint/instances", json.dumps({}))
self._instances = json.loads(Preferences.getInstance().getValue("octoprint/instances"))
def start(self):
manager = self.getOutputDeviceManager()
for name, instance in self._instances.items():
manager.addOutputDevice(OctoPrintOutputDevice.OctoPrintOutputDevice(name, instance["url"], instance["apikey"]))
def stop(self):
manager = self.getOutputDeviceManager()
for name in self._instances.keys():
manager.removeOutputDevice(name)
def _createDialog(self, qml):
path = os.path.join(PluginRegistry.getInstance().getPluginPath(self.getPluginId()), qml)
dialog = Application.getInstance().createQmlComponent(path, {"manager": self})
return dialog
def _showDialog(self, qml):
if not qml in self._dialogs:
self._dialogs[qml] = self._createDialog(qml)
self._dialogs[qml].show()
def showSettingsDialog(self):
self._showDialog("OctoPrintPlugin.qml")
serverListChanged = pyqtSignal()
@pyqtProperty("QVariantList", notify = serverListChanged)
def serverList(self):
return list(self._instances.keys())
@pyqtSlot(str, result = str)
def instanceUrl(self, name):
if name in self._instances.keys():
return self._instances[name]["url"]
return None
@pyqtSlot(str, result = str)
def instanceApiKey(self, name):
if name in self._instances.keys():
return self._instances[name]["apikey"]
return None
@pyqtSlot(str, str, str, str)
def saveInstance(self, oldName, name, url, apiKey):
manager = self.getOutputDeviceManager()
if oldName and oldName != name:
manager.removeOutputDevice(oldName)
del self._instances[oldName]
self._instances[name] = {
"url": url,
"apikey": apiKey
}
manager.addOutputDevice(OctoPrintOutputDevice.OctoPrintOutputDevice(name, url, apiKey))
Preferences.getInstance().setValue("octoprint/instances", json.dumps(self._instances))
self.serverListChanged.emit()
@pyqtSlot(str)
def removeInstance(self, name):
self.getOutputDeviceManager().removeOutputDevice(name)
del self._instances[name]
Preferences.getInstance().setValue("octoprint/instances", json.dumps(self._instances))
self.serverListChanged.emit()
@pyqtSlot(str, str, result = bool)
def validName(self, oldName, newName):
# empty string isn't allowed
if not newName:
return False
# if name hasn't changed, not a duplicate, just no rename
if oldName == newName:
return True
# duplicates not allowed
return (not newName in self._instances.keys())