def fill_table(self, search_criterion_supplies, search_criterion_collected): """ Shouldn't handle 'inverse' mode as we won't switch table columns at runtime. :param search_criterion_supplies: key-value pair to build an expression to search data in the supplies source :param search_criterion_collected: key-value pair to build an expression to search data in the collected source :return: """ plural = LayerConfig.get_dict_plural(self.utils._db.names) dict_collected_parcels = self.utils.ladm_data.get_parcel_data_to_compare_changes(self.utils._db, search_criterion_collected) # Custom layer modifiers layer_modifiers = { LayerConfig.PREFIX_LAYER_MODIFIERS: LayerConfig.SUPPLIES_DB_PREFIX, LayerConfig.SUFFIX_LAYER_MODIFIERS: LayerConfig.SUPPLIES_DB_SUFFIX, LayerConfig.STYLE_GROUP_LAYER_MODIFIERS: self.symbology.get_supplies_style_group(self.utils._supplies_db.names) } dict_supplies_parcels = self.utils.ladm_data.get_parcel_data_to_compare_changes_supplies(self.utils._supplies_db, search_criterion_supplies, layer_modifiers=layer_modifiers) # Before filling the table we make sure we get one and only one parcel attrs dict collected_attrs = dict() if dict_collected_parcels: collected_parcel_number = list(dict_collected_parcels.keys())[0] collected_attrs = dict_collected_parcels[collected_parcel_number][0] del collected_attrs[self.utils._db.names.T_ID_F] # Remove this line if self.utils._db.names.T_ID_F is somehow needed supplies_attrs = dict() if dict_supplies_parcels: supplies_parcel_number = list(dict_supplies_parcels.keys())[0] supplies_attrs = dict_supplies_parcels[supplies_parcel_number][0] del supplies_attrs[self.utils._supplies_db.names.T_ID_F] # Remove this line if self.utils._supplies_db.names,T_ID_F is somehow needed number_of_rows = len(collected_attrs) or len(supplies_attrs) self.tbl_changes_per_parcel.setRowCount(number_of_rows) # t_id shouldn't be counted self.tbl_changes_per_parcel.setSortingEnabled(False) field_names = list(collected_attrs.keys()) if collected_attrs else list(supplies_attrs.keys()) if PLOT_GEOMETRY_KEY in field_names: field_names.remove(PLOT_GEOMETRY_KEY) # We'll handle plot geometry separately for row, field_name in enumerate(field_names): supplies_value = supplies_attrs[field_name] if field_name in supplies_attrs else NULL collected_value = collected_attrs[field_name] if field_name in collected_attrs else NULL field_alias = DICT_ALIAS_KEYS_CHANGE_DETECTION[field_name] if field_name in DICT_ALIAS_KEYS_CHANGE_DETECTION else field_name self.fill_row(field_alias, supplies_value, collected_value, row, plural) if number_of_rows: # At least one row in the table? self.fill_geometry_row(PLOT_GEOMETRY_KEY, supplies_attrs[PLOT_GEOMETRY_KEY] if PLOT_GEOMETRY_KEY in supplies_attrs else QgsGeometry(), collected_attrs[PLOT_GEOMETRY_KEY] if PLOT_GEOMETRY_KEY in collected_attrs else QgsGeometry(), number_of_rows - 1) self.tbl_changes_per_parcel.setSortingEnabled(True)
def fill_model(self, record, parent): """ Fill data in the treeview depending on the structure. It expects JSON data. The JSON data may contain LADM-COL object collections in the form: "ladm_col_table_name" : [{"id": 5, "attributes":{k,v pairs}}, {"id": 8, "attributes":{k,v pairs}}, ...] """ plural = LayerConfig.get_dict_plural(self.names) icons = LayerConfig.get_dict_package_icon() dict_table_package = LayerConfig.get_dict_table_package(self.names) for key, values in record.items(): # either tuple or dict if type(values) is list: if not len(values): # Empty object kv_item = self._create_new_item(parent) kv_item.setData( 0, "{} (0)".format(plural[key] if key in plural else key)) kv_item.setData(0, QBrush(Qt.lightGray), Qt.ForegroundRole) kv_item.setData(0, {"type": key}, Qt.UserRole) kv_item.setData( 0, QIcon(icons[dict_table_package[key]]) if key in dict_table_package else None, Qt.DecorationRole) continue for value in values: if type(value) is dict: if len( value ) == 2 and 'id' in value and 'attributes' in value: # We have a list of LADM-COL model objects, we deal differently with them... self.fill_collection(key, values, parent, plural, icons) break elif type(values) is dict: if key == 'attributes': # Dict of key-value pairs, reuse the function self.fill_model(values, parent) else: # Non-LADM object (e.g., external boundaries) kv_item = self._create_new_item(parent) kv_item.setData(0, "{}:".format(key)) self.fill_model(values, kv_item) else: # Simple key-value pair kv_item = self._create_new_item(parent) kv_text = "{}: {}".format(key, values) kv_item.setData(0, kv_text) value_user_role = {"value": values} if key.startswith("Archivo fuente"): value_user_role.update({'url': values}) kv_item.setData(0, value_user_role, Qt.UserRole) if values is None: kv_item.setData(0, QBrush(Qt.lightGray), Qt.ForegroundRole) else: kv_item.setData(0, kv_text, Qt.ToolTipRole) # Additional item for a file preview if key.startswith("Archivo fuente"): if values: if values.startswith(DEFAULT_ENDPOINT_SOURCE_SERVICE ): # We want the thumbnail kv_subitem = self._create_new_item(kv_item) kv_subitem.setData(0, { 'type': 'img', 'url': values }, Qt.UserRole)