Beispiel #1
0
    def restore_state_from_xml(self, state: QByteArray, version: int,
                               testing: bool) -> bool:
        '''
        Restores the state

        Parameters
        ----------
        state : QByteArray
        version : int
        testing : bool

        Returns
        -------
        value : bool
        '''
        if state.isEmpty():
            return False

        stream = QXmlStreamReader(state)
        stream.readNextStartElement()
        if stream.name() != "QtAdvancedDockingSystem":
            return False

        v = stream.attributes().value("Version")
        if int(v) != version:
            return False

        result = True
        dock_containers = stream.attributes().value("Containers")
        logger.debug('dock_containers %s', dock_containers)
        dock_container_count = 0
        while stream.readNextStartElement():
            if stream.name() == "Container":
                result = self.restore_container(dock_container_count,
                                                stream,
                                                testing=testing)
                if not result:
                    break
                dock_container_count += 1

        if testing or not dock_container_count:
            return result

        # Delete remaining empty floating widgets
        floating_widget_index = dock_container_count - 1
        delete_count = len(self.floating_widgets) - floating_widget_index

        for i in range(delete_count):
            to_remove = self.floating_widgets[floating_widget_index + i]
            self.public.remove_dock_container(to_remove.dock_container())
            to_remove.deleteLater()

        return result
Beispiel #2
0
def colorize_svg(
	src: Union[str, Path, IO[bytes]],
	dst: Union[None, str, Path, BinaryIO]=None,
	*,
	selected: bool=False,
	app: Optional[QApplication]=None,
) -> bytes:
	"""Inject colors into a breeze-style SVG.
	
	:param src: A file object or path to read SVG data from.
	:param dst: A file object or path to write SVG data to.
	:param selected: Use selection colors?
	:param app: Currently running QApplication. Uses QApplication.instance() by default.
	:return: Returns the SVG as binary data.
	"""
	
	if app is None:
		app = QApplication.instance()
	
	sheet = get_sheet(selected, app)
	
	if hasattr(src, 'read'):
		raw = src.read()
	else:
		path = Path(src)
		with gzip.open(str(path)) if path.suffix == '.svgz' else path.open('rb') as f:
			raw = f.read()
	
	processed = QByteArray()
	reader = QXmlStreamReader(raw)
	writer = QXmlStreamWriter(processed)
	while not reader.atEnd():
		if (
			reader.readNext() == QXmlStreamReader.StartElement and
			reader.qualifiedName() == 'style' and
			reader.attributes().value('id') == 'current-color-scheme'
		):
			writer.writeStartElement('style')
			writer.writeAttributes(reader.attributes())
			writer.writeCharacters(sheet)
			writer.writeEndElement()
			while reader.tokenType() != QXmlStreamReader.EndElement:
				reader.readNext()
		elif reader.tokenType() != QXmlStreamReader.Invalid:
			writer.writeCurrentToken(reader)
	
	processed = bytes(processed)
	if hasattr(dst, 'write'):
		dst.write(processed)
	elif dst is not None:
		with Path(dst).open('wb'):
			dst.write(processed)
	return processed