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

ExtruderConfigurationModel.py « Models « PrinterOutput « cura - github.com/Ultimaker/Cura.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4a1cf4916f780a291c0167f34b69f8197bffc279 (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
# Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.
from typing import Optional

from PyQt5.QtCore import pyqtProperty, QObject, pyqtSignal

from .MaterialOutputModel import MaterialOutputModel


class ExtruderConfigurationModel(QObject):

    extruderConfigurationChanged = pyqtSignal()

    def __init__(self, position: int = -1) -> None:
        super().__init__()
        self._position = position  # type: int
        self._material = None  # type: Optional[MaterialOutputModel]
        self._hotend_id = None  # type: Optional[str]

    def setPosition(self, position: int) -> None:
        self._position = position

    @pyqtProperty(int, fset = setPosition, notify = extruderConfigurationChanged)
    def position(self) -> int:
        return self._position

    def setMaterial(self, material: Optional[MaterialOutputModel]) -> None:
        if material is None or self._material == material:
            return
        self._material = material
        self.extruderConfigurationChanged.emit()

    @pyqtProperty(QObject, fset = setMaterial, notify = extruderConfigurationChanged)
    def activeMaterial(self) -> Optional[MaterialOutputModel]:
        return self._material

    @pyqtProperty(QObject, fset = setMaterial, notify = extruderConfigurationChanged)
    def material(self) -> Optional[MaterialOutputModel]:
        return self._material

    def setHotendID(self, hotend_id: Optional[str]) -> None:
        if self._hotend_id != hotend_id:
            self._hotend_id = hotend_id
            self.extruderConfigurationChanged.emit()

    @pyqtProperty(str, fset = setHotendID, notify = extruderConfigurationChanged)
    def hotendID(self) -> Optional[str]:
        return self._hotend_id

    ##  This method is intended to indicate whether the configuration is valid or not.
    #   The method checks if the mandatory fields are or not set
    #   At this moment is always valid since we allow to have empty material and variants.
    def isValid(self) -> bool:
        return True

    def __str__(self) -> str:
        message_chunks = []
        message_chunks.append("Position: " + str(self._position))
        message_chunks.append("-")
        message_chunks.append("Material: " + self.activeMaterial.type if self.activeMaterial else "empty")
        message_chunks.append("-")
        message_chunks.append("HotendID: " + self.hotendID if self.hotendID else "empty")
        return " ".join(message_chunks)

    def __eq__(self, other) -> bool:
        if not isinstance(other, ExtruderConfigurationModel):
            return False

        if self._position != other.position:
            return False
        # Empty materials should be ignored for comparison
        if self.activeMaterial is not None and other.activeMaterial is not None:
            if self.activeMaterial.guid != other.activeMaterial.guid:
                if self.activeMaterial.guid != "" and other.activeMaterial.guid != "":
                    return False
                else:
                    # At this point there is no material, so it doesn't matter what the hotend is.
                    return True

        if self.hotendID != other.hotendID:
            return False

        return True

    #   Calculating a hash function using the position of the extruder, the material GUID and the hotend id to check if is
    #   unique within a set
    def __hash__(self):
        return hash(self._position) ^ (hash(self._material.guid) if self._material is not None else hash(0)) ^ hash(self._hotend_id)