def restore_splitter(self, stream: QXmlStreamReader, testing: bool) -> Tuple[bool, Optional[QWidget]]: ''' Restores a splitter. Parameters ---------- stream : QXmlStreamReader created_widget : QWidget testing : bool Returns ------- value : bool widget : QWidget ''' orientation_str = stream.attributes().value("Orientation") if orientation_str.startswith("-"): orientation = Qt.Horizontal elif orientation_str.startswith("|"): orientation = Qt.Vertical else: return False, None widget_count = int(stream.attributes().value("Count")) if not widget_count: return False, None logger.debug('Restore NodeSplitter Orientation: %s WidgetCount: %s', orientation, widget_count) splitter = (None if testing else self.new_splitter(orientation)) visible = False sizes = [] while stream.readNextStartElement(): child_node = None if stream.name() == "Splitter": result, child_node = self.restore_splitter(stream, testing) if not result: return False, None elif stream.name() == "Area": result, child_node = self.restore_dock_area(stream, testing) if not result: return False, None elif stream.name() == "Sizes": s_sizes = stream.readElementText().strip() sizes = [int(sz) for sz in s_sizes.split(' ')] logger.debug('Sizes: %s (from s_sizes: %s)', sizes, s_sizes) else: stream.skipCurrentElement() if splitter is not None and child_node is not None: logger.debug('ChildNode isVisible %s isVisibleTo %s', child_node.isVisible(), child_node.isVisibleTo(splitter)) splitter.addWidget(child_node) visible |= child_node.isVisibleTo(splitter) if len(sizes) != widget_count: return False, None if testing: splitter = None else: if not splitter.count(): splitter.deleteLater() splitter = None else: splitter.setSizes(sizes) splitter.setVisible(visible) return True, splitter
def restore_state(self, stream: QXmlStreamReader, testing: bool = False) -> bool: ''' Restores the state from given stream. Parameters ---------- stream : QXmlStreamReader testing : bool If Testing is true, the function only parses the data from the given stream but does not restore anything. You can use this check for faulty files before you start restoring the state Returns ------- value : bool ''' is_floating = bool(int(stream.attributes().value("Floating"))) logger.debug('Restore DockContainerWidget Floating %s', is_floating) if not testing: self.d._visible_dock_area_count = -1 # invalidate the dock area count and clear the area cache self.d.dock_areas.clear() self.d.last_added_area_cache.clear() if is_floating: logger.debug('Restore floating widget') if not stream.readNextStartElement( ) or stream.name() != "Geometry": return False geometry_string = stream.readElementText( QXmlStreamReader.ErrorOnUnexpectedElement) geometry = QByteArray.fromHex(geometry_string) if geometry.isEmpty(): return False if not testing: floating_widget = self.floating_widget() floating_widget.restoreGeometry(geometry) res, new_root_splitter = self.d.restore_child_nodes(stream, testing) if not res: return False if testing: return True # If the root splitter is empty, rostoreChildNodes returns a 0 pointer # and we need to create a new empty root splitter if not new_root_splitter: new_root_splitter = self.d.new_splitter(Qt.Horizontal) self.d.layout.replaceWidget(self.d.root_splitter, new_root_splitter) old_root = self.d.root_splitter self.d.root_splitter = new_root_splitter old_root.deleteLater() return True