Machine actions can now be triggered from QML

CURA-1385
This commit is contained in:
Jaime van Kessel 2016-06-17 17:22:49 +02:00
parent af3e4e3a15
commit 6f7affa2bf
3 changed files with 50 additions and 39 deletions

View file

@ -369,6 +369,7 @@ class CuraApplication(QtApplication):
qmlRegisterSingletonType(MachineManagerModel.MachineManagerModel, "Cura", 1, 0, "MachineManager", qmlRegisterSingletonType(MachineManagerModel.MachineManagerModel, "Cura", 1, 0, "MachineManager",
MachineManagerModel.createMachineManagerModel) MachineManagerModel.createMachineManagerModel)
qmlRegisterSingletonType(MachineActionManager.MachineActionManager, "Cura", 1, 0, "MachineActionManager", self.getMachineActionManager)
self.setMainQml(Resources.getPath(self.ResourceTypes.QmlFiles, "Cura.qml")) self.setMainQml(Resources.getPath(self.ResourceTypes.QmlFiles, "Cura.qml"))
self._qml_import_paths.append(Resources.getPath(self.ResourceTypes.QmlFiles)) self._qml_import_paths.append(Resources.getPath(self.ResourceTypes.QmlFiles))
self.initializeEngine() self.initializeEngine()
@ -385,6 +386,12 @@ class CuraApplication(QtApplication):
self.exec_() self.exec_()
## Get the machine action manager
# We ignore any **kwargs given to this, as we also register the machine manager as qml singleton.
# It wants to give this function an engine and script engine, but we don't care about that.
def getMachineActionManager(self, **kwargs):
return self._machine_action_manager
## Handle Qt events ## Handle Qt events
def event(self, event): def event(self, event):
if event.type() == QEvent.FileOpen: if event.type() == QEvent.FileOpen:

View file

@ -1,7 +1,7 @@
# Copyright (c) 2016 Ultimaker B.V. # Copyright (c) 2016 Ultimaker B.V.
# Cura is released under the terms of the AGPLv3 or higher. # Cura is released under the terms of the AGPLv3 or higher.
from PyQt5.QtCore import QObject from PyQt5.QtCore import QObject, pyqtSlot
from UM.PluginObject import PluginObject from UM.PluginObject import PluginObject
@ -16,3 +16,10 @@ class MachineAction(QObject, PluginObject):
def getLabel(self): def getLabel(self):
return self._label return self._label
@pyqtSlot()
def execute(self):
self._execute()
def _execute(self):
raise NotImplementedError("Execute() must be implemented")

View file

@ -1,9 +1,9 @@
# Copyright (c) 2016 Ultimaker B.V. # Copyright (c) 2016 Ultimaker B.V.
# Cura is released under the terms of the AGPLv3 or higher. # Cura is released under the terms of the AGPLv3 or higher.
from UM.Logger import Logger from UM.Logger import Logger
from UM.PluginRegistry import PluginRegistry # So MachineAction can be added as plugin type from UM.PluginRegistry import PluginRegistry # So MachineAction can be added as plugin type
from PyQt5.QtCore import QObject, pyqtSlot
## Raised when trying to add an unknown machine action as a required action ## Raised when trying to add an unknown machine action as a required action
class UnknownMachineAction(Exception): class UnknownMachineAction(Exception):
@ -15,55 +15,51 @@ class NotUniqueMachineAction(Exception):
pass pass
class MachineActionManager: class MachineActionManager(QObject):
def __init__(self): def __init__(self, parent = None):
## Dict of all known machine actions super().__init__(parent)
self._machine_actions = {}
## Dict of all required actions by machine reference. self._machine_actions = {} # Dict of all known machine actions
self._required_actions = {} self._required_actions = {} # Dict of all required actions by machine reference.
self._supported_actions = {} # Dict of all supported actions by machine reference
## Dict of all supported actions by machine reference self._first_start_actions = {} # Dict of all actions that need to be done when first added by machine reference
self._supported_actions = {}
## Dict of all actions that need to be done when first added by machine reference.
self._first_start_actions = {}
# Add machine_action as plugin type
PluginRegistry.addType("machine_action", self.addMachineAction) PluginRegistry.addType("machine_action", self.addMachineAction)
## Add a required action to a machine ## Add a required action to a machine
# Raises an exception when the action is not recognised. # Raises an exception when the action is not recognised.
def addRequiredAction(self, machine, action_key): def addRequiredAction(self, machine_id, action_key):
if action_key in self._machine_actions: if action_key in self._machine_actions:
if machine in self._required_actions: if machine_id in self._required_actions:
self._required_actions[machine] |= {self._machine_actions[action_key]} self._required_actions[machine_id] |= {self._machine_actions[action_key]}
else: else:
self._required_actions[machine] = {self._machine_actions[action_key]} self._required_actions[machine_id] = {self._machine_actions[action_key]}
else: else:
raise UnknownMachineAction("Action %s, which is required for %s is not known." % (action_key, machine.getKey())) raise UnknownMachineAction("Action %s, which is required for %s is not known." % (action_key, machine_id.getKey()))
## Add a supported action to a machine. ## Add a supported action to a machine.
def addSupportedAction(self, machine, action_key): def addSupportedAction(self, machine_id, action_key):
if action_key in self._machine_actions: if action_key in self._machine_actions:
if machine in self._supported_actions: if machine_id in self._supported_actions:
self._supported_actions[machine] |= {self._machine_actions[action_key]} self._supported_actions[machine_id] |= {self._machine_actions[action_key]}
else: else:
self._supported_actions[machine] = {self._machine_actions[action_key]} self._supported_actions[machine_id] = {self._machine_actions[action_key]}
else: else:
Logger.log("W", "Unable to add %s to %s, as the action is not recognised", action_key, machine.getKey()) Logger.log("W", "Unable to add %s to %s, as the action is not recognised", action_key, machine_id.getKey())
## Add an action to the first start list of a machine. ## Add an action to the first start list of a machine.
def addFirstStartAction(self, machine, action_key, index = None): def addFirstStartAction(self, machine_id, action_key, index = None):
if action_key in self._machine_actions: if action_key in self._machine_actions:
if machine in self._first_start_actions: if machine_id in self._first_start_actions:
if index is not None: if index is not None:
self._first_start_actions[machine].insert(index, self._machine_actions[action_key]) self._first_start_actions[machine_id].insert(index, self._machine_actions[action_key])
else: else:
self._first_start_actions[machine].append(self._machine_actions[action_key]) self._first_start_actions[machine_id].append(self._machine_actions[action_key])
else: else:
self._first_start_actions[machine] = [self._machine_actions[action_key]] self._first_start_actions[machine_id] = [self._machine_actions[action_key]]
else: else:
Logger.log("W", "Unable to add %s to %s, as the action is not recognised", action_key, machine.getKey()) Logger.log("W", "Unable to add %s to %s, as the action is not recognised", action_key, machine_id.getKey())
## Add a (unique) MachineAction ## Add a (unique) MachineAction
# if the Key of the action is not unique, an exception is raised. # if the Key of the action is not unique, an exception is raised.
@ -76,18 +72,19 @@ class MachineActionManager:
## Get all actions supported by given machine ## Get all actions supported by given machine
# \param machine The machine you want the supported actions of # \param machine The machine you want the supported actions of
# \returns set of supported actions. # \returns set of supported actions.
def getSupportedActions(self, machine): @pyqtSlot(str, result = "QVariantList")
if machine in self._supported_actions: def getSupportedActions(self, machine_id):
return self._supported_actions[machine] if machine_id in self._supported_actions:
return self._supported_actions[machine_id]
else: else:
return set() return set()
## Get all actions required by given machine ## Get all actions required by given machine
# \param machine The machine you want the required actions of # \param machine The machine you want the required actions of
# \returns set of required actions. # \returns set of required actions.
def getRequiredActions(self, machine): def getRequiredActions(self, machine_id):
if machine in self._required_actions: if machine_id in self._required_actions:
return self._required_actions[machine] return self._required_actions[machine_id]
else: else:
return set() return set()
@ -96,9 +93,9 @@ class MachineActionManager:
# action multiple times). # action multiple times).
# \param machine The machine you want the first start actions of # \param machine The machine you want the first start actions of
# \returns List of actions. # \returns List of actions.
def getFirstStartActions(self, machine): def getFirstStartActions(self, machine_id):
if machine in self._first_start_actions: if machine_id in self._first_start_actions:
return self._first_start_actions[machine] return self._first_start_actions[machine_id]
else: else:
return [] return []