Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PerObjectSettingVisibilityHandler.py « PerObjectSettingsTool « plugins - github.com/Ultimaker/Cura.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9fca61832218c96edefc0f6f05f13f51122cbbfd (plain)
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
# Copyright (c) 2020 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.

from PyQt5.QtCore import pyqtProperty
from UM.FlameProfiler import pyqtSlot

from UM.Application import Application
from UM.PluginRegistry import PluginRegistry
from UM.Settings.ContainerRegistry import ContainerRegistry
from UM.Settings.SettingInstance import SettingInstance
from UM.Logger import Logger
import UM.Settings.Models.SettingVisibilityHandler

from cura.Settings.ExtruderManager import ExtruderManager #To get global-inherits-stack setting values from different extruders.
from cura.Settings.SettingOverrideDecorator import SettingOverrideDecorator


class PerObjectSettingVisibilityHandler(UM.Settings.Models.SettingVisibilityHandler.SettingVisibilityHandler):
    """The per object setting visibility handler ensures that only setting

    definitions that have a matching instance Container are returned as visible.
    """
    def __init__(self, parent = None, *args, **kwargs):
        super().__init__(parent = parent, *args, **kwargs)

        self._selected_object_id = None
        self._node = None
        self._stack = None

        PluginRegistry.getInstance().getPluginObject("PerObjectSettingsTool").visibility_handler = self

        # this is a set of settings that will be skipped if the user chooses to reset.
        self._skip_reset_setting_set = set()

    def setSelectedObjectId(self, id):
        if id != self._selected_object_id:
            self._selected_object_id = id

            self._node = Application.getInstance().getController().getScene().findObject(self._selected_object_id)
            if self._node:
                self._stack = self._node.callDecoration("getStack")

            self.visibilityChanged.emit()

    @pyqtProperty("quint64", fset = setSelectedObjectId)
    def selectedObjectId(self):
        return self._selected_object_id

    @pyqtSlot(str)
    def addSkipResetSetting(self, setting_name):
        self._skip_reset_setting_set.add(setting_name)

    def setVisible(self, visible):
        if not self._node:
            return

        if not self._stack:
            self._node.addDecorator(SettingOverrideDecorator())
            self._stack = self._node.callDecoration("getStack")

        settings = self._stack.getTop()
        all_instances = settings.findInstances()
        visibility_changed = False  # Flag to check if at the end the signal needs to be emitted

        # Remove all SettingInstances that are not in visibility list
        for instance in all_instances:
            # exceptionally skip setting
            if instance.definition.key in self._skip_reset_setting_set:
                continue
            if instance.definition.key not in visible:
                settings.removeInstance(instance.definition.key)
                visibility_changed = True

        # Add all SettingInstances that are not added, but are in visibility list
        for item in visible:
            if settings.getInstance(item) is not None:  # Setting was added already.
                continue
            definition = self._stack.getSettingDefinition(item)
            if not definition:
                Logger.log("w", f"Unable to add SettingInstance ({item}) to the per-object visibility because we couldn't find the matching SettingDefinition.")
                continue

            new_instance = SettingInstance(definition, settings)
            stack_nr = -1
            stack = None
            # Check from what ContainerStack we should copy the raw property of the setting from.
            if self._stack.getProperty("machine_extruder_count", "value") > 1:
                if definition.limit_to_extruder != "-1":
                    # A limit_to_extruder function was set and it's a multi extrusion machine. Check what stack we
                    # do need to use.
                    stack_nr = str(int(round(float(self._stack.getProperty(item, "limit_to_extruder")))))

                # Check if the found stack_number is in the extruder list of extruders.
                if stack_nr not in ExtruderManager.getInstance().extruderIds and self._stack.getProperty("extruder_nr", "value") is not None:
                    stack_nr = -1

                # Use the found stack_number to get the right ContainerStack to copy the value from.
                if stack_nr in ExtruderManager.getInstance().extruderIds:
                    stack = ContainerRegistry.getInstance().findContainerStacks(id = ExtruderManager.getInstance().extruderIds[stack_nr])[0]
            else:
                stack = self._stack

            # Use the raw property to set the value (so the inheritance doesn't break)
            if stack is not None:
                new_instance.setProperty("value", stack.getRawProperty(item, "value"))
            else:
                new_instance.setProperty("value", None)
            new_instance.resetState()  # Ensure that the state is not seen as a user state.
            settings.addInstance(new_instance)
            visibility_changed = True

        if visibility_changed:
            self.visibilityChanged.emit()

    def getVisible(self):
        visible_settings = set()
        if not self._node:
            return visible_settings

        if not self._stack:
            return visible_settings

        settings = self._stack.getTop()
        if not settings:
            return visible_settings

        visible_settings = set(map(lambda i: i.definition.key, settings.findInstances()))
        return visible_settings