def find_in_nodes(node: QTreeWidgetItem, last_name: str = ''): """Find the word in all nodes.""" last_name += node.text(0) if node.childCount(): last_name += '->' code = int(node.text(2)) doc = self.data[code] pattern = re.compile(text, flags) for m in pattern.finditer(doc): add_find_result(code, last_name, *m.span()) for i in range(node.childCount()): find_in_nodes(node.child(i), last_name)
def find_in_nodes(node: QTreeWidgetItem, last_name: str = ''): """Find the word in all nodes.""" last_name += node.text(0) if node.childCount(): last_name += '->' code = int(node.text(2)) doc = self.data[code] pattern = re.compile(text.encode('utf-8'), flags) for m in pattern.finditer(doc.encode('utf-8')): start, end = m.span() item = QListWidgetItem(last_name) item.setToolTip(f"{code}:{start}:{end}") self.find_list_node[code] = node self.find_list.addItem(item) for i in range(node.childCount()): find_in_nodes(node.child(i), last_name)
def new_pointer(node: QTreeWidgetItem): """Give a new pointer code for node.""" code = self.data.new_num() self.data[code] = self.data[int(node.text(2))] node.setText(2, str(code)) for i in range(node.childCount()): new_pointer(node.child(i))
def __delete_node_data(self, node: QTreeWidgetItem): """Delete data from data structure.""" name = node.text(0) if name.startswith('@'): for action in self.macros_toolbar.actions(): if action.text() == name[1:]: self.macros_toolbar.removeAction(action) del self.data[int(node.text(2))] for i in range(node.childCount()): self.__delete_node_data(node.child(i))
def add_node(node: QTreeWidgetItem) -> NodeDict: code_int = int(node.text(2)) node_dict: NodeDict = { 'code': code_int, 'name': node.text(0), 'path': node.text(1), 'sub': [], } if file_suffix(node.text(1)) not in _SUPPORTED_FILE_SUFFIX: my_codes.append(code_int) if QFileInfo(QDir(node_getpath(node.parent())).filePath(node.text(1))).isFile(): # Files do not need to make a copy. return node_dict for j in range(node.childCount()): node_dict['sub'].append(add_node(node.child(j))) return node_dict
def save_file(node: QTreeWidgetItem, data: DataDict) -> Tuple[str, bool]: """Recursive to all the contents of nodes.""" text_data = [] all_saved = data.is_saved(int(node.text(2))) for i in range(node.childCount()): doc, saved = save_file(node.child(i), data) text_data.append(doc) all_saved &= saved my_content = data[int(node.text(2))].splitlines() for i in range(len(my_content)): content_text = my_content[i] if content_text.endswith("@others"): preffix = content_text[:-len("@others")] my_content[i] = '\n\n'.join(preffix + t for t in text_data) my_content = '\n'.join(my_content) path_text = QFileInfo(node.text(1)).fileName() if path_text and not all_saved: suffix_text = QFileInfo(path_text).suffix() if suffix_text == 'kmol': # Save project. _write_tree(node.text(1), node, data) else: # File path. file_path = QDir(QFileInfo(node_getpath(node)).absolutePath()) if not file_path.exists(): file_path.mkpath('.') print("Create Folder: {}".format(file_path.absolutePath())) file_name = file_path.filePath(path_text) if suffix_text in _SUPPORTED_FILE_SUFFIX: # Add end new line. if my_content and (my_content[-1] != '\n'): my_content += '\n' try: with open(file_name, 'w', encoding='utf-8') as f: f.write(my_content) except UnicodeError: print(f"Unicode Error in: {file_name}") else: print(f"Saved: {file_name}") elif suffix_text: print(f"Ignore file: {file_name}") return my_content, all_saved
def _write_tree(proj_name: str, root_node: QTreeWidgetItem, data: DataDict): """Write to YAML file.""" yml_data: YMLData = {} my_codes: List[int] = [] def add_node(node: QTreeWidgetItem) -> NodeDict: code_int = int(node.text(2)) node_dict: NodeDict = { 'code': code_int, 'name': node.text(0), 'path': node.text(1), 'sub': [], } if file_suffix(node.text(1)) not in _SUPPORTED_FILE_SUFFIX: my_codes.append(code_int) if QFileInfo(QDir(node_getpath(node.parent())).filePath(node.text(1))).isFile(): # Files do not need to make a copy. return node_dict for j in range(node.childCount()): node_dict['sub'].append(add_node(node.child(j))) return node_dict root_code = int(root_node.text(2)) yml_data['description'] = root_code yml_data['node'] = [] for i in range(root_node.childCount()): yml_data['node'].append(add_node(root_node.child(i))) yml_data['data'] = {root_code: _LiteralDoc(data[root_code]) or ''} for code in my_codes: yml_data['data'][code] = _LiteralDoc(data[code]) or '' data.save_all() yml_str = ( f"# Generated by Kmol editor {__version__}\n\n" + yaml.dump(yml_data, default_flow_style=False) ) with open(proj_name, 'w', encoding='utf-8') as f: f.write(yml_str) print("Saved: {}".format(proj_name))
def expand(node: QTreeWidgetItem, level: int): not_target = level != target_level node.setExpanded(not_target) if not_target: for i in range(node.childCount()): expand(node.child(i), level + 1)
def _expand_recursive(node: QTreeWidgetItem): """Expand node and its children.""" node.setExpanded(True) for i in range(node.childCount()): _expand_recursive(node.child(i))