class ColorImage(QQuickPaintedItem): def __init__(self, parent=None): super().__init__(parent) self._source = QUrl() self._color = QColor() self._svg_data = b"" self._renderer = None sourceChanged = pyqtSignal() colorChanged = pyqtSignal() def _updateSVG(self) -> None: if not self._source or self._source.toLocalFile() == "": return try: with open(self._source.toLocalFile(), "rb") as f: self._svg_data = f.read() except FileNotFoundError: Logger.log( "w", f"Unable to find image located at {self._source.toLocalFile()}" ) return self._svg_data = self._svg_data.replace( b"<svg ", b"<svg fill=\"%s\" " % self._color.name().encode("utf-8")) self._renderer = QSvgRenderer(self._svg_data) self.update() def setSource(self, source: QUrl) -> None: if self._source != source: self._source = source self.sourceChanged.emit() self._updateSVG() @pyqtProperty(QUrl, fset=setSource, notify=sourceChanged) def source(self) -> QUrl: return self._source def setColor(self, color: QColor) -> None: if self._color != color: self._color = color self.colorChanged.emit() self._updateSVG() @pyqtProperty(QColor, fset=setColor, notify=colorChanged) def color(self) -> QColor: return self._color def paint(self, painter: QPainter) -> None: pixel_ratio = Application.getInstance().getMainWindow( ).effectiveDevicePixelRatio() painter.scale(1 / pixel_ratio, 1 / pixel_ratio) if self._renderer: self._renderer.render(painter)
def _readLocalFile(self, file: QUrl, add_to_recent_files_hint: bool = True) -> None: from UM.FileHandler.ReadFileJob import ReadFileJob filename = file.toLocalFile() job = ReadFileJob(filename, handler=self, add_to_recent_files=add_to_recent_files_hint) job.finished.connect(self._readWorkspaceFinished) job.start()
def _readLocalFile(self, file: QUrl, add_to_recent_files_hint: bool = True): # We need to prevent circular dependency, so do some just in time importing. from UM.Mesh.ReadMeshJob import ReadMeshJob filename = file.toLocalFile() job = ReadMeshJob(filename, add_to_recent_files=add_to_recent_files_hint) job.finished.connect(self._readMeshFinished) job.start()