/usr/lib/python3/dist-packages/UM/Controller.py is in python3-uranium 3.1.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | # Copyright (c) 2015 Ultimaker B.V.
# Uranium is released under the terms of the LGPLv3 or higher.
from UM.Scene.Scene import Scene
from UM.Event import Event, MouseEvent, ToolEvent, ViewEvent
from UM.Signal import Signal, signalemitter
from UM.Logger import Logger
from UM.PluginRegistry import PluginRegistry
# Type hinting imports
from UM.View.View import View
from UM.InputDevice import InputDevice
from typing import Optional, Dict
from UM.Math.Vector import Vector
## Glue class that holds the scene, (active) view(s), (active) tool(s) and possible user inputs.
#
# The different types of views / tools / inputs are defined by plugins.
# \sa View
# \sa Tool
# \sa Scene
@signalemitter
class Controller:
def __init__(self, application):
super().__init__() # Call super to make multiple inheritance work.
self._active_tool = None
self._tool_operation_active = False
self._tools = {}
self._input_devices = {}
self._active_view = None
self._views = {}
self._scene = Scene()
self._application = application
self._camera_tool = None
self._selection_tool = None
self._tools_enabled = True
self._is_model_rendering_enabled = True
PluginRegistry.addType("view", self.addView)
PluginRegistry.addType("tool", self.addTool)
PluginRegistry.addType("input_device", self.addInputDevice)
## Get the application.
# \returns Application \type {Application}
def getApplication(self):
return self._application
## Add a view by name if it"s not already added.
# \param name \type{string} Unique identifier of view (usually the plugin name)
# \param view \type{View} The view to be added
def addView(self, view: View):
name = view.getPluginId()
if name not in self._views:
self._views[name] = view
view.setRenderer(self._application.getRenderer())
self.viewsChanged.emit()
else:
Logger.log("w", "%s was already added to view list. Unable to add it again.", name)
## Request view by name. Returns None if no view is found.
# \param name \type{string} Unique identifier of view (usually the plugin name)
# \return View \type{View} if name was found, none otherwise.
def getView(self, name: str) -> Optional[View]:
try:
return self._views[name]
except KeyError: # No such view
Logger.log("e", "Unable to find %s in view list", name)
return None
## Return all views.
# \return views \type{dict}
def getAllViews(self) -> Dict[str, View]:
return self._views
## Request active view. Returns None if there is no active view
# \return view \type{View} if an view is active, None otherwise.
def getActiveView(self) -> Optional[View]:
return self._active_view
## Set the currently active view.
# \param name \type{string} The name of the view to set as active
def setActiveView(self, name: str):
Logger.log("d", "Setting active view to %s", name)
try:
if self._active_view:
self._active_view.event(ViewEvent(Event.ViewDeactivateEvent))
self._active_view = self._views[name]
if self._active_view:
self._active_view.event(ViewEvent(Event.ViewActivateEvent))
self.activeViewChanged.emit()
except KeyError:
Logger.log("e", "No view named %s found", name)
except Exception as e:
Logger.logException("e", "An exception occurred while switching views: %s", str(e))
def enableModelRendering(self):
self._is_model_rendering_enabled = True
def disableModelRendering(self):
self._is_model_rendering_enabled = False
def isModelRenderingEnabled(self):
return self._is_model_rendering_enabled
## Emitted when the list of views changes.
viewsChanged = Signal()
## Emitted when the active view changes.
activeViewChanged = Signal()
## Add an input device (e.g. mouse, keyboard, etc) if it's not already added.
# \param device The input device to be added
def addInputDevice(self, device: InputDevice):
name = device.getPluginId()
if name not in self._input_devices:
self._input_devices[name] = device
device.event.connect(self.event)
else:
Logger.log("w", "%s was already added to input device list. Unable to add it again." % name)
## Request input device by name. Returns None if no device is found.
# \param name \type{string} Unique identifier of input device (usually the plugin name)
# \return input \type{InputDevice} device if name was found, none otherwise.
def getInputDevice(self, name: str) -> Optional[InputDevice]:
try:
return self._input_devices[name]
except KeyError: # No such device
Logger.log("e", "Unable to find %s in input devices", name)
return None
## Remove an input device from the list of input devices.
# Does nothing if the input device is not in the list.
# \param name \type{string} The name of the device to remove.
def removeInputDevice(self, name: str):
if name in self._input_devices:
self._input_devices[name].event.disconnect(self.event)
del self._input_devices[name]
## Request tool by name. Returns None if no view is found.
# \param name \type{string} Unique identifier of tool (usually the plugin name)
# \return tool \type{Tool} if name was found, none otherwise.
def getTool(self, name: str):
try:
return self._tools[name]
except KeyError: # No such tool
Logger.log("e", "Unable to find %s in tools", name)
return None
## Get all tools
# \return tools \type{dict}
def getAllTools(self):
return self._tools
## Add a Tool (transform object, translate object) if its not already added.
# \param tool \type{Tool} Tool to be added
def addTool(self, tool):
name = tool.getPluginId()
if name not in self._tools:
self._tools[name] = tool
tool.operationStarted.connect(self._onToolOperationStarted)
tool.operationStopped.connect(self._onToolOperationStopped)
self.toolsChanged.emit()
else:
Logger.log("w", "%s was already added to tool list. Unable to add it again.", name)
def _onToolOperationStarted(self, tool):
if not self._tool_operation_active:
self._tool_operation_active = True
self.toolOperationStarted.emit(tool)
def _onToolOperationStopped(self, tool):
if self._tool_operation_active:
self._tool_operation_active = False
self.toolOperationStopped.emit(tool)
## Gets whether a tool is currently in use
# \return \type{bool} true if a tool current being used.
def isToolOperationActive(self) -> bool:
return self._tool_operation_active
## Request active tool. Returns None if there is no active tool
# \return Tool \type{Tool} if an tool is active, None otherwise.
def getActiveTool(self):
return self._active_tool
## Set the current active tool.
# The tool can be set by name of the tool or directly passing the tool object.
# \param tool \type{Tool} or \type{string}
def setActiveTool(self, tool):
from UM.Tool import Tool
if self._active_tool:
self._active_tool.event(ToolEvent(ToolEvent.ToolDeactivateEvent))
if isinstance(tool, Tool) or tool is None:
new_tool = tool
else:
new_tool = self.getTool(tool)
tool_changed = False
if self._active_tool is not new_tool:
self._active_tool = new_tool
tool_changed = True
if self._active_tool:
self._active_tool.event(ToolEvent(ToolEvent.ToolActivateEvent))
from UM.Scene.Selection import Selection # Imported here to prevent a circular dependency.
if not self._active_tool and Selection.getCount() > 0: # If something is selected, a tool must always be active.
if "TranslateTool" in self._tools:
self._active_tool = self._tools["TranslateTool"] # Then default to the translation tool.
self._active_tool.event(ToolEvent(ToolEvent.ToolActivateEvent))
tool_changed = True
else:
Logger.log("w", "Controller does not have an active tool and could not default to Translate tool.")
if tool_changed:
self.activeToolChanged.emit()
## Emitted when the list of tools changes.
toolsChanged = Signal()
## Emitted when a tool changes its enabled state.
toolEnabledChanged = Signal()
## Emitted when the active tool changes.
activeToolChanged = Signal()
## Emitted whenever a tool starts a longer operation.
#
# \param tool The tool that started the operation.
# \sa Tool::startOperation
toolOperationStarted = Signal()
## Emitted whenever a tool stops a longer operation.
#
# \param tool The tool that stopped the operation.
# \sa Tool::stopOperation
toolOperationStopped = Signal()
## Get the scene
# \return scene \type{Scene}
def getScene(self) -> Scene:
return self._scene
## Process an event
# \param event \type{Event} event to be handle.
# The event is first passed to the camera tool, then active tool and finally selection tool.
# If none of these events handle it (when they return something that does not evaluate to true)
# a context menu signal is emitted.
def event(self, event: Event):
# First, try to perform camera control
if self._camera_tool and self._camera_tool.event(event):
return
if self._tools and event.type == Event.KeyPressEvent:
from UM.Scene.Selection import Selection # Imported here to prevent a circular dependency.
if Selection.hasSelection():
for key, tool in self._tools.items():
if tool.getShortcutKey() is not None and event.key == tool.getShortcutKey():
self.setActiveTool(tool)
if self._selection_tool and self._selection_tool.event(event):
return
# If we are not doing camera control, pass the event to the active tool.
if self._active_tool and self._active_tool.event(event):
return
if self._active_view:
self._active_view.event(event)
if event.type == Event.MouseReleaseEvent and MouseEvent.RightButton in event.buttons:
self.contextMenuRequested.emit(event.x, event.y)
contextMenuRequested = Signal()
## Set the tool used for handling camera controls.
# Camera tool is the first tool to receive events.
# The tool can be set by name of the tool or directly passing the tool object.
# \param tool \type{Tool} or \type{string}
# \sa setSelectionTool
# \sa setActiveTool
def setCameraTool(self, tool):
from UM.Tool import Tool
if isinstance(tool, Tool) or tool is None:
self._camera_tool = tool
else:
self._camera_tool = self.getTool(tool)
## Get the camera tool (if any)
# \returns camera tool (or none)
def getCameraTool(self):
return self._camera_tool
## Set the tool used for performing selections.
# Selection tool receives its events after camera tool and active tool.
# The tool can be set by name of the tool or directly passing the tool object.
# \param tool \type{Tool} or \type{string}
# \sa setCameraTool
# \sa setActiveTool
def setSelectionTool(self, tool):
from UM.Tool import Tool
if isinstance(tool, Tool) or tool is None:
self._selection_tool = tool
else:
self._selection_tool = self.getTool(tool)
def getToolsEnabled(self):
return self._tools_enabled
def setToolsEnabled(self, enabled):
self._tools_enabled = enabled
# Rotate camer view according defined angle
last_rotation_angle_x = 0
last_rotation_angle_y = 0
def rotateView(self, coordinate ="x", angle = 0):
camera = self._scene.getActiveCamera()
camera_tool = self.getTool("CameraTool")
camera_tool.setOrigin(Vector(0, 100, 0))
if coordinate == "home":
camera.setPosition(Vector(0, 300, 700))
camera.setPerspective(True)
camera.lookAt(Vector(0, 100, 100))
camera_tool.rotateCam(0, 0)
elif coordinate == "3d":
camera.setPosition(Vector(-180, 250, 1000))
camera.setPerspective(True)
camera.lookAt(Vector(0, 100, 100))
camera_tool.rotateCam(0, 0)
else:
# for comparison is == used, because might not store them at the same location
# https://stackoverflow.com/questions/1504717/why-does-comparing-strings-in-python-using-either-or-is-sometimes-produce
camera.setPosition(Vector(0, 0, 700))
camera.setPerspective(True)
camera.lookAt(Vector(0, 100, 0))
if coordinate == "x":
camera_tool.rotateCam(angle, 0)
elif coordinate == "y":
camera_tool.rotateCam(0, angle)
|