def refresh_node_widgets(self): """ refresh displayed node item widgets and mark current list item of filtered current node data. """ assert not self.refreshing_widgets self.refreshing_widgets = True try: flow_id = self.flow_id self.vpo(f"LiszApp.refresh_node_widgets: flow='{flow_id}' path={self.flow_path}") self.filtered_indexes = list() lf_ds = self.menu_bar_ids.listFilterSelected.state == 'normal' lf_ns = self.menu_bar_ids.listFilterUnselected.state == 'normal' nic = self.items_container nic.clear_widgets() h = 0 for item_idx, nid in enumerate(self.current_node_items): if item_idx != self.dragging_item_idx: widgets = self.create_item_widgets(item_idx, nid) # has to be created for to re-calc sel for nodes sel_state = nid.get('sel') if lf_ds and sel_state or lf_ns and sel_state != 1.0: for niw in widgets: nic.add_widget(niw) h += niw.height self.filtered_indexes.append(item_idx) nic.height = h # ensure that current leaf/node is visible - if still exists in current list if flow_action(flow_id) == 'focus': niw = self.widget_by_id(flow_key(flow_id)) if niw: nic.parent.scroll_to(niw, animate=False) finally: assert self.refreshing_widgets self.refreshing_widgets = False
def widget_by_flow_id(self, flow_id: str) -> Optional[Any]: """ determine the widget referenced by the passed flow_id. :param flow_id: flow id referencing the focused widget. :return: widget that has the focus when the passed flow id is set. """ return self.widget_by_id(flow_key(flow_id)) or super().widget_by_flow_id(flow_id)
def on_item_edit(self, _item_id: str, _event_kwargs: Dict[str, Any]) -> bool: """ edit list item """ self.vpo(f"LiszApp.on_item_edit({_item_id} {_event_kwargs})") item_id = flow_key(self.flow_id) niw = self.widget_by_id(item_id) nic = self.items_container svw = nic.parent svw.scroll_to(niw, animate=False) border = Popup.border.defaultvalue # (bottom, right, top, left) pos = niw.to_window(*niw.pos) pwx = max(-border[3] / 2.01, pos[0] - border[3]) pwy = min(max(0, pos[1] - border[0]), svw.height - niw.height - border[0]) self.prevent_keyboard_covering(pwy) height = niw.height * 1.8 / 1.5 + border[0] + border[2] pu = Factory.ItemEditor(title=niw.item_data['id'], pos_hint=dict(x=pwx / Window.width, y=pwy / Window.height), size_hint=(None, None), size=(svw.width + border[1] + border[3], height), background_color=(0.9, 0.6, 0.6, 0.6), border=border, separator_height=0, title_size=0, auto_width_minimum=Window.width, auto_width_window_padding=0, ) # calc popup height: Popup widget in style.kv is adding dp(12) as padding and dp(16) to title font size # .. therefore remove title label widget from popup instance (on some devices the height was too small) pu.children[0].remove_widget(pu.children[0].children[-1]) pu.open() # popup will call edit_item_finished() on dismiss/close return True
def on_clipboard_key_x(self): """ cut focused item or the currently displayed node items to the OS clipboard. """ lit = self.current_item_or_node_literal() self.vpo(f"LiszApp.on_clipboard_key_x: cutting {lit}") Clipboard.copy(lit) if lit.startswith('['): for item in self.current_node_items: self.delete_item(item['id']) elif lit.startswith('{'): self.delete_item(flow_key(self.flow_id)) else: self.delete_item(lit) self.refresh_all()
def add_extract_options(node_flow_path: List[str], pre: str = ""): """ add extract options for the node specified by node_flow_path. :param node_flow_path: flow path of node for to add extract options to the child_maps list. :param pre: info button text prefix (for to mark the focused item). """ node = self.flow_path_node(node_flow_path, strict=True) if not node or node_flow_path in added_nodes: return # skip empty and already added nodes added_nodes.append(node_flow_path) parent_node_id = flow_key(self.flow_path[-1]) if self.flow_path else FLOW_PATH_ROOT_ID if child_maps: child_maps.append(dict( # add separator widget cls='Widget', kwargs=dict(size_hint_y=None, height=self.font_size / 3))) copy_icon = id_of_flow('copy', 'node') image_size = (self.font_size * 1.29, self.font_size * 1.29) node_info = self.node_info(node, what=() if self.verbose else ('selected_leaf_count', 'unselected_leaf_count')) if parent_node_id: node_info['name'] = parent_node_id info_text = (f"{self.flow_path_text(node_flow_path, display_root=True)}" f" {node_info[('un' if sel_status is False else '') + 'selected_leaf_count']}" f"/{node_info['selected_leaf_count'] + node_info['unselected_leaf_count']}") child_maps.append(dict( kwargs=dict( text=pre + info_text, tap_flow_id=id_of_flow('open', 'node_info', repr(node_flow_path)), tap_kwargs=dict(popup_kwargs=dict(node_info=node_info)), image_size=image_size, circle_fill_color=self.flow_path_ink, circle_fill_size=image_size, square_fill_color=self.flow_id_ink[:3] + (0.39, )))) attr_tpl = dict(tap_flow_id=id_of_flow('extract', 'node', repr(node_flow_path)), tap_kwargs=dict(popups_to_close=('replace_with_data_map_container', ), extract_type=''), icon_name=id_of_flow('export', 'node'), image_size=image_size) attrs = deepcopy(attr_tpl) attrs.update(text=get_txt("copy all node items"), icon_name=copy_icon) attrs['tap_kwargs']['extract_type'] = 'copy' child_maps.append(dict(kwargs=attrs)) attrs = deepcopy(attr_tpl) attrs.update(text=get_txt("export all node items")) attrs['tap_kwargs']['extract_type'] = 'export' child_maps.append(dict(kwargs=attrs)) if self.debug: attrs = deepcopy(attr_tpl) attrs.update(text=get_txt("cut all node items"), icon_name=id_of_flow('cut', 'node')) attrs['tap_kwargs']['extract_type'] = 'cut' child_maps.append(dict(kwargs=attrs)) attrs = deepcopy(attr_tpl) attrs.update(text=get_txt("delete all node items"), icon_name=id_of_flow('delete', 'item')) attrs['tap_kwargs']['extract_type'] = 'delete' child_maps.append(dict(kwargs=attrs)) if sel_status is not None: extract_filter = 'sel' if sel_status else 'unsel' ink = self.selected_item_ink if sel_status else self.unselected_item_ink attrs = deepcopy(attr_tpl) attrs.update(text=get_txt("copy {'' if sel_status else 'un'}selected node items"), icon_name=copy_icon, circle_fill_color=ink, circle_fill_size=image_size) attrs['tap_kwargs']['extract_type'] = 'copy_' + extract_filter child_maps.append(dict(kwargs=attrs)) attrs = deepcopy(attr_tpl) attrs.update(text=get_txt("export {'' if sel_status else 'un'}selected node items"), circle_fill_color=ink, circle_fill_size=image_size) attrs['tap_kwargs']['extract_type'] = 'export_' + extract_filter child_maps.append(dict(kwargs=attrs))
def show_extract_options(self, widget: Widget, flow_path_text: str = '', item: Optional[LiszItem] = None, sel_status: Optional[bool] = None) -> bool: """ open context menu with the available extract actions of the current context/flow/app-status (focus, debug). :param widget: widget to show extract options for. :param flow_path_text: flow path text for to identify the node to extract (def=self.flow_path). :param item: data dict of the item represented by :paramref:`~show_extract_options.widget`. :param sel_status: pass True / False to add option for to export selected / unselected items. :return: True if extract options are available and got displayed else False. """ def add_extract_options(node_flow_path: List[str], pre: str = ""): """ add extract options for the node specified by node_flow_path. :param node_flow_path: flow path of node for to add extract options to the child_maps list. :param pre: info button text prefix (for to mark the focused item). """ node = self.flow_path_node(node_flow_path, strict=True) if not node or node_flow_path in added_nodes: return # skip empty and already added nodes added_nodes.append(node_flow_path) parent_node_id = flow_key(self.flow_path[-1]) if self.flow_path else FLOW_PATH_ROOT_ID if child_maps: child_maps.append(dict( # add separator widget cls='Widget', kwargs=dict(size_hint_y=None, height=self.font_size / 3))) copy_icon = id_of_flow('copy', 'node') image_size = (self.font_size * 1.29, self.font_size * 1.29) node_info = self.node_info(node, what=() if self.verbose else ('selected_leaf_count', 'unselected_leaf_count')) if parent_node_id: node_info['name'] = parent_node_id info_text = (f"{self.flow_path_text(node_flow_path, display_root=True)}" f" {node_info[('un' if sel_status is False else '') + 'selected_leaf_count']}" f"/{node_info['selected_leaf_count'] + node_info['unselected_leaf_count']}") child_maps.append(dict( kwargs=dict( text=pre + info_text, tap_flow_id=id_of_flow('open', 'node_info', repr(node_flow_path)), tap_kwargs=dict(popup_kwargs=dict(node_info=node_info)), image_size=image_size, circle_fill_color=self.flow_path_ink, circle_fill_size=image_size, square_fill_color=self.flow_id_ink[:3] + (0.39, )))) attr_tpl = dict(tap_flow_id=id_of_flow('extract', 'node', repr(node_flow_path)), tap_kwargs=dict(popups_to_close=('replace_with_data_map_container', ), extract_type=''), icon_name=id_of_flow('export', 'node'), image_size=image_size) attrs = deepcopy(attr_tpl) attrs.update(text=get_txt("copy all node items"), icon_name=copy_icon) attrs['tap_kwargs']['extract_type'] = 'copy' child_maps.append(dict(kwargs=attrs)) attrs = deepcopy(attr_tpl) attrs.update(text=get_txt("export all node items")) attrs['tap_kwargs']['extract_type'] = 'export' child_maps.append(dict(kwargs=attrs)) if self.debug: attrs = deepcopy(attr_tpl) attrs.update(text=get_txt("cut all node items"), icon_name=id_of_flow('cut', 'node')) attrs['tap_kwargs']['extract_type'] = 'cut' child_maps.append(dict(kwargs=attrs)) attrs = deepcopy(attr_tpl) attrs.update(text=get_txt("delete all node items"), icon_name=id_of_flow('delete', 'item')) attrs['tap_kwargs']['extract_type'] = 'delete' child_maps.append(dict(kwargs=attrs)) if sel_status is not None: extract_filter = 'sel' if sel_status else 'unsel' ink = self.selected_item_ink if sel_status else self.unselected_item_ink attrs = deepcopy(attr_tpl) attrs.update(text=get_txt("copy {'' if sel_status else 'un'}selected node items"), icon_name=copy_icon, circle_fill_color=ink, circle_fill_size=image_size) attrs['tap_kwargs']['extract_type'] = 'copy_' + extract_filter child_maps.append(dict(kwargs=attrs)) attrs = deepcopy(attr_tpl) attrs.update(text=get_txt("export {'' if sel_status else 'un'}selected node items"), circle_fill_color=ink, circle_fill_size=image_size) attrs['tap_kwargs']['extract_type'] = 'export_' + extract_filter child_maps.append(dict(kwargs=attrs)) self.vpo(f"LiszApp.show_extract_options({widget}, {flow_path_text}, {item}, {sel_status})") flow_path = self.flow_path_from_text(flow_path_text) if flow_path_text else self.flow_path has_focus = flow_action(self.flow_id) == 'focus' flo_key = flow_key(self.flow_id) child_maps = list() added_nodes = list() node_id = item['id'] if item else '' if node_id and 'node' in item: add_extract_options(flow_path + [id_of_flow('enter', 'item', node_id)], pre=FOCUS_FLOW_PREFIX if has_focus and node_id == flo_key else "") add_extract_options(flow_path) if has_focus and flo_key != node_id: item = self.item_by_id(flo_key) if 'node' in item: add_extract_options(flow_path + [id_of_flow('enter', 'item', flo_key)], pre=FOCUS_FLOW_PREFIX) if not child_maps: self.show_message(get_txt("no extract options available for this item/node")) return False popup_kwargs = dict(parent=widget, child_data_maps=child_maps, auto_width_child_padding=self.font_size * 3) return self.change_flow(id_of_flow('open', 'extract'), popup_kwargs=popup_kwargs)
def show_add_options(self, widget: Widget) -> bool: """ open context menu with all available actions for to add/import new node/item(s). :param widget: FlowButton to open this context menu. :return: True if any add options are available and got displayed else False. """ def add_import_options(info_text: str): """ add menu options for the current node. """ info = self.node_info(node, what=() if self.verbose else ('selected_leaf_count', 'unselected_leaf_count')) if len(node) == 1: info['content'] = node[0]['id'] info_text = str(info['selected_leaf_count'] + info['unselected_leaf_count']) + " items from " + info_text if node_id: info['name'] = node_id info_text = get_txt("node '{node_id}' with ") + info_text info_text += " to" child_maps.append(dict(kwargs=dict( text=info_text, tap_flow_id=id_of_flow('open', 'node_info'), tap_kwargs=dict(popup_kwargs=dict(node_info=info)), image_size=image_size, circle_fill_color=self.flow_path_ink, circle_fill_size=image_size, square_fill_color=self.flow_id_ink[:3] + (0.39,)))) args_tpl: Dict[str, Any] = dict( tap_flow_id=id_of_flow('import', 'node'), tap_kwargs=dict(node_to_import=node, popups_to_close=('replace_with_data_map_container',)), image_size=image_size) if self.flow_path: kwargs = deepcopy(args_tpl) kwargs['text'] = get_txt("current list begin") if node_id: kwargs['tap_kwargs']['add_node_id'] = node_id child_maps.append(dict(kwargs=kwargs)) item_index = len(self.current_node_items) if item_index: kwargs = deepcopy(args_tpl) kwargs['text'] = get_txt("current list end") kwargs['tap_kwargs']['import_items_index'] = item_index if node_id: kwargs['tap_kwargs']['add_node_id'] = node_id child_maps.append(dict(kwargs=kwargs)) kwargs = deepcopy(args_tpl) kwargs['text'] = get_txt("{FLOW_PATH_ROOT_ID} list begin") kwargs['tap_kwargs']['import_items_node'] = self.root_node if node_id: kwargs['tap_kwargs']['add_node_id'] = node_id child_maps.append(dict(kwargs=kwargs)) item_index = len(self.root_node) if item_index: kwargs = deepcopy(args_tpl) kwargs['text'] = get_txt("{FLOW_PATH_ROOT_ID} list end") kwargs['tap_kwargs']['import_items_node'] = self.root_node kwargs['tap_kwargs']['import_items_index'] = item_index if node_id: kwargs['tap_kwargs']['add_node_id'] = node_id child_maps.append(dict(kwargs=kwargs)) if focused_id: item_index = self.find_item_index(focused_id) kwargs = deepcopy(args_tpl) kwargs['text'] = get_txt("before focused item '{focused_id}'") kwargs['tap_kwargs']['import_items_index'] = item_index if node_id: kwargs['tap_kwargs']['add_node_id'] = node_id child_maps.append(dict(kwargs=kwargs)) focused_item = self.item_by_id(focused_id) if 'node' in focused_item: kwargs = deepcopy(args_tpl) kwargs['text'] = get_txt("into focused sub-list '{focused_id}'") kwargs['tap_kwargs']['import_items_node'] = focused_item['node'] if node_id: kwargs['tap_kwargs']['add_node_id'] = node_id child_maps.append(dict(kwargs=kwargs)) kwargs = deepcopy(args_tpl) kwargs['text'] = get_txt("after focused '{focused_id}'") kwargs['tap_kwargs']['import_items_index'] = item_index + 1 if node_id: kwargs['tap_kwargs']['add_node_id'] = node_id child_maps.append(dict(kwargs=kwargs)) self.vpo(f"LiszApp.show_add_options({widget})") image_size = (self.font_size * 1.35, self.font_size * 1.35) focused_id = flow_key(self.flow_id) if flow_action(self.flow_id) == 'focus' else '' child_maps = list() node = node_from_literal(Clipboard.paste()) if node: node_id = '' add_import_options(get_txt("clipboard")) node_files = self.importable_node_files(folder_path=self.documents_root_path) for node_file_info in node_files: if child_maps: # add separator widget child_maps.append(dict(cls='Widget', kwargs=dict(size_hint_y=None, height=self.font_size / 3))) node_id, node, file_path, err_msg = node_file_info if not err_msg: add_import_options(get_txt("export file")) child_maps.append(dict(cls='Widget', kwargs=dict(size_hint_y=None, height=self.font_size / 3))) node_id = '' add_import_options(get_txt("export file")) elif self.verbose: child_maps.append(dict(kwargs=dict( cls='ImageLabel', text=node_id + " error: " + err_msg, square_fill_color=(1, 0, 0, 0.39,)))) if not child_maps: self.show_message(get_txt("neither clipboard nor '{self.documents_root_path}' contains items to import"), title=get_txt("import error(s)", count=1)) return False popup_kwargs = dict(parent=widget, child_data_maps=child_maps, auto_width_child_padding=self.font_size * 3) return self.change_flow(id_of_flow('open', 'alt_add'), popup_kwargs=popup_kwargs)