def fill_collection(self, key, collection, parent, plural, icons): """ Fill a collection of LADM_COL objects """ parent.insertChildren(parent.childCount(), 1, self.rootItem.columnCount()) collection_parent = parent.child(parent.childCount() - 1) collection_parent.setData( 0, "{} ({})".format(plural[key] if key in plural else key, len(collection))) collection_parent.setData(0, {"type": key}, Qt.UserRole) dict_table_package = LayerConfig.get_dict_table_package(self.names) res = collection_parent.setData( 0, QIcon(icons[dict_table_package[key]]) if key in dict_table_package else None, Qt.DecorationRole) for object in collection: # Fill LADM_COL object collection_parent.insertChildren(collection_parent.childCount(), 1, self.rootItem.columnCount()) object_parent = collection_parent.child( collection_parent.childCount() - 1) object_parent.setData(0, "t_id: {}".format(object['id'])) object_parent.setData(0, { "type": key, "id": object['id'], "value": object['id'] }, Qt.UserRole) object_parent.setData(0, key, Qt.ToolTipRole) font = QFont() font.setBold(True) object_parent.setData(0, font, Qt.FontRole) self.fill_model(object['attributes'], object_parent)
def fill_collection(self, key, collection, parent, plural, icons): """ Fill a collection of LADM-COL objects """ display_name = self._normalize_display(key, plural) key = self._normalize_key(key) collection_parent = self._create_new_item(parent) collection_parent.setData( 0, "{} ({})".format(display_name, len(collection))) collection_parent.setData( 0, { "type": key, "collapse": key not in [self.names.LC_PLOT_T, self.names.LC_PARCEL_T] }, Qt.UserRole) dict_table_package = LayerConfig.get_dict_table_package(self.names) res = collection_parent.setData( 0, QIcon(icons[dict_table_package[key]]) if key in dict_table_package else None, Qt.DecorationRole) for object in collection: # Fill LADM-COL object object_parent = self._create_new_item(collection_parent) object_parent.setData(0, "t_id: {}".format(object['id'])) object_parent.setData(0, { "type": key, "id": object['id'], "value": object['id'] }, Qt.UserRole) object_parent.setData(0, key, Qt.ToolTipRole) font = QFont() font.setBold(True) object_parent.setData(0, font, Qt.FontRole) self.fill_model(object['attributes'], object_parent)
def show_context_menu(self, point): tree_view = self.sender() index = tree_view.indexAt(point) context_menu = QMenu("Context menu") index_data = index.data(Qt.UserRole) if index_data is None: return if "value" in index_data: action_copy = QAction( QCoreApplication.translate("DockWidgetQueries", "Copy value")) action_copy.triggered.connect( partial(self.copy_value, index_data["value"])) context_menu.addAction(action_copy) context_menu.addSeparator() if "url" in index_data: action_open_url = QAction( QCoreApplication.translate("DockWidgetQueries", "Open URL")) action_open_url.triggered.connect( partial(self.open_url, index_data["url"])) context_menu.addAction(action_open_url) context_menu.addSeparator() # Configure actions for tables/layers if "type" in index_data and "id" in index_data: table_name = index_data["type"] table_package = LayerConfig.get_dict_table_package(self.names) t_id = index_data["id"] geometry_type = None if table_name in table_package and table_package[ table_name] == LADMNames.SPATIAL_UNIT_PACKAGE: # Layers in Spatial Unit package have double geometry, we need the polygon one geometry_type = QgsWkbTypes.PolygonGeometry if table_name == self.names.OP_PARCEL_T: if self._layers[ self.names.OP_PARCEL_T][LAYER] is None or self._layers[ self.names. OP_PLOT_T][LAYER] is None or self._layers[ self.names.COL_UE_BAUNIT_T][LAYER] is None: self.add_layers() layer = self._layers[self.names.OP_PARCEL_T][LAYER] self.iface.layerTreeView().setCurrentLayer(layer) else: layer = self.qgis_utils.get_layer(self._db, table_name, geometry_type, True) if layer is not None: if layer.isSpatial(): action_zoom_to_feature = QAction( QCoreApplication.translate( "DockWidgetQueries", "Zoom to {} with {}={}").format( table_name, self.names.T_ID_F, t_id)) action_zoom_to_feature.triggered.connect( partial(self.zoom_to_feature, layer, t_id)) context_menu.addAction(action_zoom_to_feature) if table_name == self.names.OP_PARCEL_T: # We show a handy option to zoom to related plots plot_ids = self.ladm_data.get_plots_related_to_parcels( self._db, [t_id], None, self._layers[self.names.OP_PLOT_T][LAYER], self._layers[self.names.COL_UE_BAUNIT_T][LAYER]) if plot_ids: action_zoom_to_plots = QAction( QCoreApplication.translate( "DockWidgetQueries", "Zoom to related plot(s)")) action_zoom_to_plots.triggered.connect( partial(self.zoom_to_plots, plot_ids)) context_menu.addAction(action_zoom_to_plots) action_open_feature_form = QAction( QCoreApplication.translate( "DockWidgetQueries", "Open form for {} with {}={}").format( table_name, self.names.T_ID_F, t_id)) action_open_feature_form.triggered.connect( partial(self.open_feature_form, layer, t_id)) context_menu.addAction(action_open_feature_form) if context_menu.actions(): context_menu.exec_(tree_view.mapToGlobal(point))
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)