def get_src_children(self, obj): """ Get children items for a source item. Note: See class description for more details about argument and return type. Arguments: obj (object): Source data object. Returns: list[object]: Child items. """ children = [] node_type = get_node_type(obj) if isinstance(obj, Model): children = [obj.root] elif node_type == NodeType.History: children = [self.category_model.case] elif node_type == NodeType.Case: children = obj.child_nodes elif node_type == NodeType.Stage: children = self.category_model.get_stage_children(obj) elif node_type & NodeType.NoChildrenItems: children = [] # it is confirmed that command has no # children in the categories representation else: children = obj.child_nodes return children
def get_node(self, tree_item, node_type=None): """ Find appopriate node from tree item. Arguments: tree_item (QTreeWidgetItem): Tree widget item. node_type (Optional[str, Node, Category]): Requested node type or typename. Returns: Node: Data model node (*None* if not found). """ node = None type_id = get_type(tree_item) needed_type_id = get_node_type(node_type) if node_type else type_id if type_id > needed_type_id: # search among parents node = self.get_node(tree_item.parent(), node_type) elif type_id == needed_type_id: node_id = get_id(tree_item) if type_id == NodeType.History: node = self.history elif type_id == NodeType.Category: node = self._categories[node_id] elif node_id: node = self.history.get_node(node_id) return node
def update_item(self, obj, tree_item): # pragma pylint: disable=no-self-use """ Update destination item from source data item. Note: See class description for more details about argument types. Arguments: obj (object): Source data object. tree_item (QTreeWidgetItem): Tree widget item. """ node_type = get_node_type(obj) if node_type in (NodeType.Command, NodeType.Category, NodeType.Stage, NodeType.Case, NodeType.Variable): update_validity_role(obj, tree_item) if node_type == NodeType.Command: update_font(tree_item, obj.type is None) # !!! Validity update must be before data set tree_item.setText(0, get_object_name(obj)) tree_item.setText(1, get_object_type(obj)) tree_item.setData(0, Q.Qt.ToolTipRole, get_object_info(obj)) tree_item.setData(0, Role.ExpandedRole, tree_item.isExpanded()) icon = get_icon(obj) if icon is not None: tree_item.setIcon(0, icon)
def stage(self): """ Get stage being managed by the panel. Returns: Stage: Stage being managed. """ node_type = get_node_type(self.node) return self.node if node_type == NodeType.Stage else self.node.stage
def get_object_info(obj, **kwargs): """ Get object info to be displayed in tooltip. Arguments: obj (Node): Data model node. **kwargs: Arbitrary keyword arguments: Returns: str: Object's info. """ node_type = get_node_type(obj) info = NodeType.value2str(node_type) if node_type == NodeType.Command: info += ": " if obj.type is None: name = translate("AsterStudy", "[noname]") else: name = obj.name info += bold(name) cata = obj.title title = translate_command(cata) tip = " ({title} / {name})" if title != cata else " ({name})" info += tip.format(title=italic(title), name=cata) if kwargs.get('with_parent_stage', False): info += "<br>" st_name = bold(obj.stage.name) info += translate("AsterStudy", "From stage: {}").format(st_name) elif node_type == NodeType.Comment: info += ":<br>" content = obj.content.split("\n") content = [" # " + i for i in content] info += italic("\n".join(content)) elif node_type == NodeType.Variable: info += ": " info += bold(obj.name) info += " ({})".format(italic(obj.expression)) elif node_type == NodeType.Case: info += ": " info += bold(obj.name) if obj.description: info += "\n\n" info += obj.description elif node_type != NodeType.History: info += ": " info += bold(obj.name) if node_type in [ NodeType.Case, NodeType.Stage, NodeType.Category, NodeType.Command ]: validity = Validity.value2str(obj.check()) if validity: info += "<br>" info += font("Invalid:", color="#ff0000") info += ", ".join([bold(i.strip()) for i in validity.split(",")]) info = preformat(info) return info
def create_item(self, obj): """ Create a destination item for given source item. Note: See class description for more details about argument and return type. Arguments: obj (object): Source data object. Returns: TreeWidgetItem: Tree widget item. """ is_current_case = self.category_model.case == \ self.category_model.history.current_case node_type = get_node_type(obj) tree_item = Q.QTreeWidgetItem() # first column: name, icon tree_item.setText(0, get_object_name(obj)) icon = get_icon(obj) if icon is not None: tree_item.setIcon(0, icon) tree_item.setData(0, Q.Qt.ToolTipRole, get_object_info(obj)) tree_item.setData(0, Role.TypeRole, node_type) if isinstance(obj, History): node_id = root_node_type() else: node_id = obj.uid tree_item.setData(0, Role.IdRole, node_id) # second column: catalogue name (Command only) tree_item.setText(1, get_object_type(obj)) # third column (debug mode only): object id tree_item.setText(2, str(node_id)) # item flags if node_type & NodeType.ValidityItems: update_validity_role(obj, tree_item) if node_type == NodeType.Stage: flags = tree_item.flags() if is_current_case: flags = flags | Q.Qt.ItemIsEditable tree_item.setFlags(flags) elif node_type in (NodeType.Command, NodeType.Variable): update_font(tree_item, obj.type is None) flags = tree_item.flags() if obj.type is not None and is_current_case: flags = flags | Q.Qt.ItemIsEditable tree_item.setFlags(flags) self.category_model.register_item(tree_item) return tree_item
def mode(self): """ Get current working mode of panel. Returns: int: Working mode: *UnitPanel.CreateMode* (0) for adding new file, *UnitPanel.EditMode* (1) for editing file. """ node_type = get_node_type(self.node) return UnitPanel.EditMode if node_type == NodeType.Unit \ else UnitPanel.CreateMode
def info(node): """ Get pretty formatted preview information on the data model node. Arguments: node (Node): Data model object. Returns: str: Object's description. """ node_type = get_node_type(node) return Visitor.dump(node) \ if node_type in (NodeType.Command, NodeType.Variable) else ""
def _doubleClicked(self, index): """ Called when item is double clicked in the view. Emits `itemDoubleClicked(entity)` signal. Arguments: index (QModelIndex): Model index being activated. """ obj = self._modelObject(index) if obj is not None: entity = Entity(obj.uid, get_node_type(obj)) self.itemDoubleClicked.emit(entity)
def selection(self): """ Get currently selected objects. Returns: list[Entity]: List of selected entities. """ selection = [] for index in self._view.selectionModel().selectedIndexes(): obj = self._modelObject(index) if obj is not None: selection.append(Entity(obj.uid, get_node_type(obj))) return selection
def _indexChanged(self, index, value): """ Called when editable item is changed by the user. Emits `itemChanged(entity, text)` signal. Arguments: index (QModelIndex): Model index being changed. value (str): New value of model index. """ obj = self._modelObject(index) if obj is not None: entity = Entity(obj.uid, get_node_type(obj)) self.itemChanged.emit(entity, value)
def get_object_type(obj): """ Get object catalogue name to be displayed in data view. Arguments: obj (Node): Data model node. Returns: str: Object's catalogue name. """ name = "" node_type = get_node_type(obj) if node_type == NodeType.Command: name = translate_command(obj.title) return name
def update(self): """Update model.""" stages = [] if self.case is None: if get_node_type(self.root) == NodeType.Stage: stages.append(self.root) else: self._stage_children = {} self._categories = [] stages = self.case.stages for stage in stages: if stage.is_graphical_mode(): # not be necessary if command.check was called before update stage.reorder() commands = stage.sorted_commands categories = [] withnext = [] for command in commands: if command.title == "_CONVERT_COMMENT": if behavior().show_comments: withnext.append(command) continue category = CATA.get_command_category(command.title) category = translate_category(category) if not categories or categories[-1].name != category: uid = len(self._categories) + 1 new_category = Category(-uid, category, stage.uid, self._history_proxy) self._categories.append(new_category) categories.append(new_category) for i in withnext: categories[-1].add_child(i) categories[-1].add_child(command) withnext = [] # purge the buffer if categories: # only comments => ignored! for i in withnext: categories[-1].add_child(i) self._stage_children[stage] = categories else: self._stage_children[stage] = []
def updateControls(self): """ Called when current index of filename combobox is changed. Updates state of 'exists' and 'embedded' controls. """ exists = isvalid = isreference = isforced = False combo_index = self.file_combo.currentIndex() index = self.file_combo.model().index(combo_index, 0) if index.isValid(): filename = index.data(Role.CustomRole) isvalid = index.data(Role.ValidityRole) isreference = index.data(Role.ReferenceRole) if isreference: exists = external_file(filename) is not None else: exists = isvalid and os.path.isfile(filename) isforced = get_node_type(self.node) in (NodeType.Unit,) \ and self.node.is_forced_attr self.exists_check.setChecked(exists) # undefined file can not be embedded self.embedded_check.setDisabled(isreference or not isvalid or isforced)
def get_object_name(obj): """ Get object name to be displayed in data view. Arguments: obj (Node): Data model node. Returns: str: Object's name. """ node_type = get_node_type(obj) if node_type == NodeType.History: return translate("AsterStudy", "History") elif node_type == NodeType.Command: if obj.gettype(ConversionLevel.NoFail) is not None: return obj.name if behavior().show_catalogue_name: return translate("AsterStudy", "[noname]") else: return translate_command(obj.title) elif node_type == NodeType.Comment: return obj.content.split("\n")[0] return obj.name