Listen to changes on all extruders

Not just the active extruder. The non-active extruder may change when loading a project file which happened to have the same printer (so no new printer is created) and the same material and variant in the active extruder but not the same material or variant in another extruder.

Contributes to issue CURA-3803.
This commit is contained in:
Ghostkeeper 2017-05-22 12:50:53 +02:00
parent 6315947156
commit 5ae02fb4c6
No known key found for this signature in database
GPG Key ID: C5F96EE2BC0F7E75

View File

@ -1,12 +1,15 @@
# Copyright (c) 2016 Ultimaker B.V. # Copyright (c) 2017 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 Qt, pyqtSignal, pyqtProperty, QTimer from PyQt5.QtCore import Qt, pyqtSignal, pyqtProperty, QTimer
from typing import Iterable
import UM.Qt.ListModel import UM.Qt.ListModel
from UM.Application import Application from UM.Application import Application
import UM.FlameProfiler import UM.FlameProfiler
from cura.Settings.ExtruderManager import ExtruderManager from cura.Settings.ExtruderManager import ExtruderManager
from cura.Settings.ExtruderStack import ExtruderStack #To listen to changes on the extruders.
from cura.Settings.MachineManager import MachineManager #To listen to changes on the extruders of the currently active machine.
## Model that holds extruders. ## Model that holds extruders.
# #
@ -66,16 +69,12 @@ class ExtrudersModel(UM.Qt.ListModel.ListModel):
self._add_global = False self._add_global = False
self._simple_names = False self._simple_names = False
self._active_extruder_stack = None self._active_machine_extruders = [] # type: Iterable[ExtruderStack]
#Listen to changes. #Listen to changes.
Application.getInstance().globalContainerStackChanged.connect(self._updateExtruders) Application.getInstance().globalContainerStackChanged.connect(self._extrudersChanged) #When the machine is swapped we must update the active machine extruders.
manager = ExtruderManager.getInstance() ExtruderManager.getInstance().extrudersChanged.connect(self._extrudersChanged) #When the extruders change we must link to the stack-changed signal of the new extruder.
self._extrudersChanged() #Also calls _updateExtruders.
self._updateExtruders()
manager.activeExtruderChanged.connect(self._onActiveExtruderChanged)
self._onActiveExtruderChanged()
def setAddGlobal(self, add): def setAddGlobal(self, add):
if add != self._add_global: if add != self._add_global:
@ -104,17 +103,31 @@ class ExtrudersModel(UM.Qt.ListModel.ListModel):
def simpleNames(self): def simpleNames(self):
return self._simple_names return self._simple_names
def _onActiveExtruderChanged(self): ## Links to the stack-changed signal of the new extruders when an extruder
manager = ExtruderManager.getInstance() # is swapped out or added in the current machine.
active_extruder_stack = manager.getActiveExtruderStack() #
if self._active_extruder_stack != active_extruder_stack: # \param machine_id The machine for which the extruders changed. This is
if self._active_extruder_stack: # filled by the ExtruderManager.extrudersChanged signal when coming from
self._active_extruder_stack.containersChanged.disconnect(self._onExtruderStackContainersChanged) # that signal. Application.globalContainerStackChanged doesn't fill this
# signal; it's assumed to be the current printer in that case.
def _extrudersChanged(self, machine_id = None):
if machine_id is not None:
if Application.getInstance().getGlobalContainerStack() is None:
return #No machine, don't need to update the current machine's extruders.
if machine_id != Application.getInstance().getGlobalContainerStack().getId():
return #Not the current machine.
#Unlink from old extruders.
for extruder in self._active_machine_extruders:
extruder.containersChanged.disconnect(self._onExtruderStackContainersChanged)
if active_extruder_stack: #Link to new extruders.
# Update the model when the material container is changed self._active_machine_extruders = []
active_extruder_stack.containersChanged.connect(self._onExtruderStackContainersChanged) extruder_manager = ExtruderManager.getInstance()
self._active_extruder_stack = active_extruder_stack for extruder in extruder_manager.getExtruderStacks():
extruder.containersChanged.connect(self._onExtruderStackContainersChanged)
self._active_machine_extruders.append(extruder)
self._updateExtruders() #Since the new extruders may have different properties, update our own model.
def _onExtruderStackContainersChanged(self, container): def _onExtruderStackContainersChanged(self, container):
# Update when there is an empty container or material change # Update when there is an empty container or material change