def _add_wm_node(self, elem): #print "Adding {}".format(elem.id) parent_rel = elem.getRelation(pred=self._wmi.get_sub_properties('skiros:spatiallyRelated'), obj='-1') to_expand = True if not parent_rel: parent_rel = elem.getRelation(pred=self._wmi.get_sub_properties('skiros:skillProperty'), obj='-1') to_expand = False if not parent_rel: to_expand = False parent_rel = elem.getRelation(pred='skiros:hasSkill', obj='-1') if not parent_rel: log.warn("[add_wm_node]", "Skipping element without declared parent: {}".format(elem.id)) return True parent_id = '{}_skills'.format(parent_rel['src']) item = self.wm_tree_widget.findItems(parent_id, Qt.MatchRecursive | Qt.MatchFixedString, 1) if not item: # In case it is still not existing i create the "support" skill node item = self.wm_tree_widget.findItems(parent_rel['src'], Qt.MatchRecursive | Qt.MatchFixedString, 1)[0] item = QTreeWidgetItem(item, ['Skills', parent_id]) else: item = item[0] else: items = self.wm_tree_widget.findItems(parent_rel['src'], Qt.MatchRecursive | Qt.MatchFixedString, 1) if not items: log.warn("[add_wm_node]", "Parent {} of node {} is not in the known tree.".format(parent_rel['src'], elem.id)) return False item = items[0] name = utils.ontology_type2name(elem.id) if not elem.label else utils.ontology_type2name(elem.label) item = QTreeWidgetItem(item, [name, elem.id]) item.setExpanded(to_expand) return True
def _update_wm_node(self, elem, cur_item_id): # check if element is already in tree items = self.wm_tree_widget.findItems(elem.id, Qt.MatchRecursive | Qt.MatchFixedString, 1) if not items: return item = items[0] # check if updated item is selected if elem.id == cur_item_id: # update properties table if selected item has changed self.wm_properties_widget.blockSignals(True) self.fill_properties_table(elem) self.wm_properties_widget.blockSignals(False) # get parent node in tree parent = item.parent() if not parent: return # check if the old parent is still parent of the updated element parent_rel = elem.getRelation(pred=self._wmi.get_sub_properties('skiros:spatiallyRelated'), obj='-1') if not parent.text(1) in parent_rel['src']: # elem moved spatially item.parent().removeChild(item) parents = self.wm_tree_widget.findItems(parent_rel['src'], Qt.MatchRecursive | Qt.MatchFixedString, 1) if not parents: log.warn("[update_wm_node]", "No parent found for {}".format(elem.id)) return item.setText(0, utils.ontology_type2name(elem.id) if not elem.label else utils.ontology_type2name(elem.label)) item.setText(1, elem.id) parents[0].addChild(item)
def on_properties_table_item_changed(self, item, row): item_key = self.wm_properties_widget.item(row, 0) item_val = self.wm_properties_widget.item(row, 1) key = item_key.id value = item_val.text() elem = self._wmi.get_element(item.text(1)) if elem.hasProperty(key): prop = elem.getProperty(key) if value == '': value = None if prop.dataTypeIs(bool): value = item_val.checkState() == Qt.Checked try: elem.setProperty(prop.key, value, is_list=prop.isList(), force_convertion=value is not None) log.debug(self.__class__.__name__, '<{}> property {} to {}'.format(item.text(1), prop.key, value)) except ValueError: log.error(self.__class__.__name__, 'Changing <{}> property {} to {} failed'.format(item.text(1), prop.key, value)) item_val.setText(str(prop.value)) elif hasattr(elem, key.lower()): key = key.lower() attr = getattr(elem, key) setattr(elem, key, type(attr)(value)) log.debug(self.__class__.__name__, '<{}> attribute {} to {}'.format(item.text(1), key, value)) else: log.error(self.__class__.__name__, 'Changing <{}> property {} to {} failed'.format(item.text(1), key, value)) self._wmi.update_element_properties(elem) name = utils.ontology_type2name(elem.id) if not elem.label else utils.ontology_type2name(elem.label) item.setText(0, name)
def _create_wm_tree(self, item, scene, elem): name = utils.ontology_type2name(elem.id) if not elem.label else utils.ontology_type2name(elem.label) item = QTreeWidgetItem(item, [name, elem.id]) spatialRel = sorted(elem.getRelations(subj='-1', pred=self._wmi.get_sub_properties('skiros:spatiallyRelated')), key=lambda r: r['dst']) for rel in spatialRel: if rel['dst'] in scene: self._create_wm_tree(item, scene, scene[rel['dst']]) item.setExpanded(True) skillRel = sorted(elem.getRelations(subj='-1', pred='skiros:hasSkill'), key=lambda r: r['dst']) if skillRel: skillItem = QTreeWidgetItem(item, ['Skills', '{}_skills'.format(elem.id)]) for rel in skillRel: if rel['dst'] in scene: self._create_wm_tree(skillItem, scene, scene[rel['dst']]) skillItem.setExpanded(True) skillPropRel = sorted(elem.getRelations(subj='-1', pred=self._wmi.get_sub_properties('skiros:skillProperty')), key=lambda r: r['dst']) for rel in skillPropRel: if rel['dst'] in scene: self._create_wm_tree(item, scene, scene[rel['dst']])
def _add_relations_table_row(self, relation, editable_src=False, editable_dst=False): src = QTableWidgetItem(utils.ontology_type2name(relation['src'])) rel = QTableWidgetItem(utils.ontology_type2name(relation['type'])) dst = QTableWidgetItem(utils.ontology_type2name(relation['dst'])) src.id = relation['src'] rel.id = relation['type'] dst.id = relation['dst'] src.setTextAlignment(Qt.AlignRight) rel.setTextAlignment(Qt.AlignHCenter) dst.setTextAlignment(Qt.AlignLeft) if not editable_src: src.setFlags(src.flags() & ~Qt.ItemIsEditable) rel.setFlags(rel.flags() & ~Qt.ItemIsEditable) if not editable_dst: dst.setFlags(dst.flags() & ~Qt.ItemIsEditable) self.wm_relations_widget.insertRow(self.wm_relations_widget.rowCount()) # self.wm_relations_widget.setSpan(self.wm_relations_widget.rowCount()-1, 0, 1, 3) self.wm_relations_widget.setItem(self.wm_relations_widget.rowCount() - 1, 0, src) self.wm_relations_widget.setItem(self.wm_relations_widget.rowCount() - 1, 1, rel) self.wm_relations_widget.setItem(self.wm_relations_widget.rowCount() - 1, 2, dst)
def on_add_object_button_clicked(self): dialog = SkirosAddObjectDialog(self) ret = dialog.exec_() if not ret: return log.debug(self.__class__.__name__, 'Create element based on {}'.format(dialog.object)) parent = self.wm_tree_widget.currentItem() parent_id = parent.text(1) if not parent_id: return elem = self._wmi.get_template_element(dialog.object) elem.label = utils.ontology_type2name(dialog.object) elem_id = self._wmi.instanciate(elem, recursive=True, relations=[{'src': parent_id, 'type': 'skiros:contain', 'dst': '-1'}]) # parent = self.wm_tree_widget.currentItem() # parent_id = parent.text(1) log.debug(self.__class__.__name__, 'Added element {} to {}'.format(elem_id, parent_id))
def on_select_type(self, id, index): """Callback for change selection in dropdown lists. Adds and removes dropdown list to/from the dialog that are used to filter subtypes based on the current selection. Args: id (int): Number of the combobox that dispatched the callback index (int): Number of the selected item in the current combobox (id) """ # log.debug(self.__class__.__name__, 'Selected {}: {}'.format(id, index)) # clear filters after selected while id < len(self._comboBoxes) - 1: # log.debug(self.__class__.__name__, 'Delete {}'.format(id+1)) label = self.formLayout.labelForField(self._comboBoxes[id + 1]) label.deleteLater() self._comboBoxes[id + 1].deleteLater() del self._comboBoxes[id + 1] # get current selection if index > 0: # if not 'All' is selected selected = self._comboBoxes[id].itemData(self._comboBoxes[id].currentIndex()) elif id > 0: # if 'All' is selected and it is not the first combo box selected = self._comboBoxes[id - 1].itemData(self._comboBoxes[id - 1].currentIndex()) else: # if 'All' is selected and it is the first combo box selected = self.default_type # log.debug(self.__class__.__name__, 'Selected type {}'.format(selected)) # create new combo box if not 'All' is selected if index > 0: self.create_comboBox(selected) # log.debug(self.__class__.__name__, 'Created {}'.format(len(self._comboBoxes)-1)) # update list of individuals self.comboBox_individual.clear() if index > 0 or (id > 0 and index == 0): self.comboBox_individual.addItem('new ' + utils.ontology_type2name(selected), selected) [self.comboBox_individual.addItem(l, d) for l, d in self.get_individuals(selected).iteritems()] QTimer.singleShot(0, self.adjustSize)
def _add_properties_table_row(self, prop, editable_key=False, editable_value=True): key = QTableWidgetItem(utils.ontology_type2name(prop.key)) if not editable_key: key.setFlags(key.flags() & ~Qt.ItemIsEditable) value = prop.values if prop.isList() else prop.value if prop.dataTypeIs(float): val = QTableWidgetItem(format(value, '.4f') if value is not None else '') else: val = QTableWidgetItem(str(value) if value is not None else '') if not editable_value: val.setFlags(val.flags() & ~Qt.ItemIsEditable) if prop.dataTypeIs(bool): val.setText('') val.setFlags(val.flags() & ~Qt.ItemIsEditable) val.setCheckState(Qt.Checked if prop.value else Qt.Unchecked) # if isinstance(prop.dataType(), bool): # val.setCheckState(prop.value) key.id = val.id = prop.key self.wm_properties_widget.insertRow(self.wm_properties_widget.rowCount()) self.wm_properties_widget.setItem(self.wm_properties_widget.rowCount() - 1, 0, key) self.wm_properties_widget.setItem(self.wm_properties_widget.rowCount() - 1, 1, val)