def loadColumns(self, table): #Load textual and spatial (if available) columns #Get spatial columns first spColumns = table_column_names(table, True, creation_order=True) self.cboSpatialCols_2.clear() self.cboSpatialCols_2.addItems(spColumns) #Textual Columns self.lstSrcCols_2.clear() self.allCols = table_column_names(table, creation_order=True) for sc in spColumns: colIndex = getIndex(self.allCols, sc) if colIndex != -1: self.allCols.remove(sc) for col in self.allCols: if ' ' in col: col = u'"{}"'.format(col) tabItem = QListWidgetItem(col, self.lstSrcCols_2) tabItem.setCheckState(Qt.Unchecked) tabItem.setIcon(QIcon(":/plugins/stdm/images/icons/column.png")) self.lstSrcCols_2.addItem(tabItem) if len(spColumns) > 0: self.cboSpatialCols_2.setEnabled(True)
def loadColumns(self,table): #Load textual and spatial (if available) columns #Get spatial columns first spColumns = table_column_names(table,True, creation_order=True) self.cboSpatialCols_2.clear() self.cboSpatialCols_2.addItems(spColumns) #Textual Columns self.lstSrcCols_2.clear() self.allCols = table_column_names(table, creation_order=True) for sc in spColumns: colIndex = getIndex(self.allCols,sc) if colIndex != -1: self.allCols.remove(sc) for col in self.allCols: if ' ' in col: col = u'"{}"'.format(col) tabItem = QListWidgetItem(col, self.lstSrcCols_2) tabItem.setCheckState(Qt.Unchecked) tabItem.setIcon(QIcon(":/plugins/stdm/images/icons/column.png")) self.lstSrcCols_2.addItem(tabItem) if len(spColumns) > 0: self.cboSpatialCols_2.setEnabled(True)
def old_new_columns(self, old_view, new_view): """ Create a dictionary out of the old and new view columns. :param old_view: Old view name :type old_view: String :param new_view: New View name :type new_view: Strong :return: Dictionary of old and new view columns :rtype: Dictionary """ if 'new_' in old_view: old_view = old_view.lstrip('new_') old_columns = table_column_names( old_view, False, True ) new_columns = table_column_names( new_view, False, True ) old_new_cols = dict( zip(old_columns, new_columns) ) old_new_cols.update( self.supporting_doc_columns ) return old_new_cols
def old_new_columns(self, old_view, new_view): """ Create a dictionary out of the old and new view columns. :param old_view: Old view name :type old_view: String :param new_view: New View name :type new_view: Strong :return: Dictionary of old and new view columns :rtype: Dictionary """ if 'new_' in old_view: old_view = old_view.lstrip('new_') old_columns = table_column_names( old_view, False, True ) new_columns = table_column_names( new_view, False, True ) old_new_cols = dict( zip(old_columns, new_columns) ) old_new_cols.update( self.supporting_doc_columns ) return old_new_cols
def assignCols(self): #Load source and target columns respectively srcCols = self._source_columns() for c in srcCols: srcItem = QListWidgetItem(c,self.lstSrcFields) srcItem.setCheckState(Qt.Unchecked) srcItem.setIcon(QIcon(":/plugins/stdm/images/icons/column.png")) self.lstSrcFields.addItem(srcItem) #Destination Columns tabIndex = int(self.field("tabIndex")) self.targetTab = self.destCheckedItem.text() targetCols = table_column_names(self.targetTab, False, True) #Remove geometry columns in the target columns list for gc in self.geomcols: colIndex = getIndex(targetCols,gc) if colIndex != -1: targetCols.remove(gc) #Remove 'id' column if there id_idx = getIndex(targetCols, 'id') if id_idx != -1: targetCols.remove('id') self._add_target_table_columns(targetCols)
def __init__(self, search_config): self._config = search_config self._is_valid = self._validate_data_source() self._geom_columns = [] self._valid_cols = OrderedDict() self._valid_filter_cols = OrderedDict() self._vector_layer = None self._column_types = {} # Set attributes if data source is valid if self._is_valid: self._geom_columns = table_column_names(self._config.data_source, True) self._valid_cols = self._validate_columns() self._valid_filter_cols = self._validate_filter_columns() # Use the first geometry column in the list. Hence, it is # important to ensure that each data has not more than one # geometry column. geom_col = self._geom_columns[0] if len( self._geom_columns) > 0 else '' self._vector_layer = vector_layer( self._config.data_source, layer_name=self._config.display_name, geom_column=geom_col)
def assignCols(self): #Load source and target columns respectively srcCols = self._source_columns() for c in srcCols: srcItem = QListWidgetItem(c,self.lstSrcFields) srcItem.setCheckState(Qt.Unchecked) srcItem.setIcon(QIcon(":/plugins/stdm/images/icons/column.png")) self.lstSrcFields.addItem(srcItem) #Destination Columns tabIndex = int(self.field("tabIndex")) self.targetTab = self.destCheckedItem.text() targetCols = table_column_names(self.targetTab, False, True) #Remove geometry columns in the target columns list for gc in self.geomcols: colIndex = getIndex(targetCols,gc) if colIndex != -1: targetCols.remove(gc) #Remove 'id' column if there id_idx = getIndex(targetCols, 'id') if id_idx != -1: targetCols.remove('id') self._add_target_table_columns(targetCols)
def loadGeomCols(self, table): # Load geometry columns based on the selected table self.geom_cols = table_column_names(table, True, True) self.geomClm.clear() self.geomClm.addItems(self.geom_cols) if self.restored_config.get('geom_column'): self.geomClm.setCurrentIndex(self.geomClm.findText(self.restored_config['geom_column']))
def _table_columns(self, table_name): """ Return the column names for a given table_name name. :param table_name: Table name :type table_name: str :return: Collection of column names. If not found then an empty list is returned. :rtype: list """ return table_column_names(table_name)
def _table_columns(self, table_name): """ Return the column names for a given table_name name. :param table_name: Table name :type table_name: str :return: Collection of column names. If not found then an empty list is returned. :rtype: list """ return table_column_names(table_name)
def _populate_layers(self): self.stdm_layers_combo.clear() if self._curr_profile is None: return self.spatial_unit = self._curr_profile. \ social_tenure.spatial_unit # Get entities containing geometry # columns based on the config info config_entities = self._curr_profile.entities self.geom_entities = [ ge for ge in config_entities.values() if ge.TYPE_INFO == 'ENTITY' and ge.has_geometry_column() ] self._profile_spatial_layers = [] self.sp_tables = spatial_tables() for e in self.geom_entities: table_name = e.name if table_name in self.sp_tables: for gc in e.geometry_columns(): column_name = gc.name display_name = gc.layer_display() self._add_geometry_column_to_combo(table_name, column_name, display_name, gc) # Add geometry entity to the collection self._profile_spatial_layers.append(table_name) # Append the corresponding(profile) # view to the list of entity names str_view = self._curr_profile.social_tenure.view_name if str_view in self.sp_tables: self.str_view_geom_columns = table_column_names(str_view, True) if len(self.str_view_geom_columns) > 0: # Pick the first column # geom_col = geom_columns[0] for i, geom_col in enumerate(self.str_view_geom_columns): if i > 0: view_layer_name = self._curr_profile. \ social_tenure.layer_display() view_layer_name = '{}.{}'.format( view_layer_name, geom_col) else: view_layer_name = self._curr_profile. \ social_tenure.layer_display() self._add_geometry_column_to_combo( str_view, geom_col, view_layer_name, self._curr_profile.social_tenure) # Append view to the list of spatial layers self._profile_spatial_layers.append(view_layer_name)
def _check_column_exists(column, table): table_columns = table_column_names(table) if column not in table_columns: LOGGER.debug( '%s column does not exist in % table. Foreign key will ' 'not be created', column, table) return False return True
def _load_source_table_fields(self, sel): self.cbo_referencing_col.clear() if not sel: return columns_names = table_column_names(sel) self.cbo_referencing_col.clear() self.cbo_referencing_col.addItem("") self.cbo_referencing_col.addItems(columns_names)
def _loadFields(self): """ Load labeling fields/columns. """ if not self._dsName: self.cboLabelField.clear() return cols = table_column_names(self._dsName) spatialCols = table_column_names(self._dsName,True) spSet = set(spatialCols) nonSpatialCols = [c for c in cols if c not in spSet] if len(nonSpatialCols) == 0: return self.cboLabelField.clear() self.cboLabelField.addItem("") self.cboLabelField.addItems(nonSpatialCols)
def _loadFields(self): """ Load labeling fields/columns. """ if not self._dsName: self.cboLabelField.clear() return cols = table_column_names(self._dsName) spatialCols = table_column_names(self._dsName,True) spSet = set(spatialCols) nonSpatialCols = [c for c in cols if c not in spSet] if len(nonSpatialCols) == 0: return self.cboLabelField.clear() self.cboLabelField.addItem("") self.cboLabelField.addItems(nonSpatialCols)
def layer_entity_children(self, sel_lyr_name): layer_lists = [ layer_list for layer_list in self.same_entity_layers() if sel_lyr_name in layer_list ] str_view = self._curr_profile.social_tenure.view_name if len(layer_lists) < 1: geom_columns = table_column_names(str_view, True) layer_lists = [geom_columns] return layer_lists
def _load_source_table_fields(self, sel): self.cbo_referencing_col.clear() data_source_index = self.cbo_source_field.currentIndex() self.on_data_source_changed( self.cbo_source_field.itemData(data_source_index)) if not sel: return columns_names = table_column_names(sel) self.cbo_referencing_col.clear() self.cbo_referencing_col.addItem("") self.cbo_referencing_col.addItems(columns_names)
def _validate_columns(self): # Validates if the columns in the config exist in the data source. ds_set = set(table_column_names(self._config.data_source)) conf_set = set(self._config.columns.keys()) valid_cols_set = ds_set.intersection(conf_set) v_col_mapping = OrderedDict() for vc in valid_cols_set: v_col_mapping[vc] = self._config.columns.get(vc) # Set column type col_type = columnType(self._config.data_source, vc) if col_type: self._column_types[vc] = col_type return v_col_mapping
def on_table_name_changed(self, table): """ Slot raised when table name changes so that the corresponding column names can be updated. :param table: Name of the current table :type table: str """ self.cbo_value_field.clear() self.cbo_x_field.clear() numeric_cols = numeric_columns(table) x_cols = table_column_names(table) self.cbo_value_field.addItems(numeric_cols) self.cbo_x_field.addItems(x_cols)
def on_table_name_changed(self, table): """ Slot raised when table name changes so that the corresponding column names can be updated. :param table: Name of the current table :type table: str """ self.cbo_value_field.clear() self.cbo_x_field.clear() numeric_cols = numeric_columns(table) x_cols = table_column_names(table) self.cbo_value_field.addItems(numeric_cols) self.cbo_x_field.addItems(x_cols)
def _load_source_table_fields(self, sel): self.cbo_referencing_col.clear() data_source_index = self.cbo_source_field.currentIndex() self.on_data_source_changed( self.cbo_source_field.itemData(data_source_index) ) if not sel: return columns_names = table_column_names(sel) self.cbo_referencing_col.clear() self.cbo_referencing_col.addItem("") self.cbo_referencing_col.addItems(columns_names)
def _validate_custom_attr_dummy_column(self, custom_entity): # Check if the dummy column has been added to the custom tenure entity # Insert dummy column so that the table is not flagged as a m2m dummy_col = custom_entity.column(self.CUSTOM_TENURE_DUMMY_COLUMN) if dummy_col is None: dummy_col = VarCharColumn( self.CUSTOM_TENURE_DUMMY_COLUMN, custom_entity, maximum=1 ) custom_entity.add_column(dummy_col) if pg_table_exists(custom_entity.name): custom_ent_cols = table_column_names(custom_entity.name) if dummy_col.name not in custom_ent_cols: custom_table = alchemy_table(custom_entity.name) varchar_updater(dummy_col, custom_table, custom_ent_cols)
def _loadFields(self, dataSourceName): """ Load fields/columns of the given data source. """ if dataSourceName == "": self.cboDataField.clear() return columnsNames = table_column_names(dataSourceName) if len(columnsNames) == 0: return self.cboDataField.clear() self.cboDataField.addItem("") self.cboDataField.addItems(columnsNames)
def _loadFields(self, dataSourceName): """ Load fields/columns of the given data source. """ if dataSourceName == "": self.cboDataField.clear() return columnsNames = table_column_names(dataSourceName) if len(columnsNames) == 0: return self.cboDataField.clear() self.cboDataField.addItem("") self.cboDataField.addItems(columnsNames)
def _on_source_table_changed(self, index): source_table = self.cbo_source_tables.currentText() self.cbo_output_column.clear() if source_table: ref_table_cols = table_column_names(source_table) self.tb_source_trans_cols.set_combo_selection( [self._source_cols, ref_table_cols]) #self.cbo_output_column.addItem("") self.cbo_output_column.addItems(ref_table_cols) else: self.tb_source_trans_cols.clear_view()
def _on_source_table_changed(self, index): source_table = self.cbo_source_tables.currentText() self.cbo_output_column.clear() if source_table: ref_table_cols = table_column_names(source_table) self.tb_source_trans_cols.set_combo_selection([self._source_cols, ref_table_cols]) #self.cbo_output_column.addItem("") self.cbo_output_column.addItems(ref_table_cols) else: self.tb_source_trans_cols.clear_view()
def layer_entity_children(self, sel_lyr_name): layer_lists = [ layer_list for layer_list in self.same_entity_layers() if sel_lyr_name in layer_list ] str_view = self._curr_profile.social_tenure.view_name if len(layer_lists) < 1: geom_columns = table_column_names( str_view, True ) layer_lists = [geom_columns] return layer_lists
def load_data_source_fields(self, data_source_name): """ Load fields/columns of the given data source. """ if data_source_name == "": self.clear() return columns_names = table_column_names(data_source_name) if len(columns_names) == 0: return self.cbo_source_field.clear() self.cbo_source_field.addItem("") self.cbo_source_field.addItems(columns_names)
def load_data_source_fields(self, data_source_name): """ Load fields/columns of the given data source. """ if data_source_name == "": self.clear() return columns_names = table_column_names(data_source_name) if len(columns_names) == 0: return self.cbo_source_field.clear() self.cbo_source_field.addItem("") self.cbo_source_field.addItems(columns_names)
def _load_fields(self): """ Load spatial fields/columns of the given data source. """ if not self._ds_name: self.cboSpatialFields.clear() return spatialColumns = table_column_names(self._ds_name, True) if len(spatialColumns) == 0: return self.cboSpatialFields.clear() self.cboSpatialFields.addItem("") self.cboSpatialFields.addItems(spatialColumns)
def _load_fields(self): """ Load spatial fields/columns of the given data source. """ if not self._ds_name: self.cboSpatialFields.clear() return spatialColumns = table_column_names(self._ds_name, True) if len(spatialColumns) == 0: return self.cboSpatialFields.clear() self.cboSpatialFields.addItem("") self.cboSpatialFields.addItems(spatialColumns)
def register_column_widgets(self): """ Registers the column widgets. """ # Append column labels and widgets table_name = self._entity.name columns = table_column_names(table_name) self.scroll_widget_contents = QWidget() self.scroll_widget_contents.setObjectName('scrollAreaWidgetContents') for c in self._entity.columns.values(): if c.name in self.exclude_columns: continue if not c.name in columns and not isinstance(c, VirtualColumn): continue # Get widget factory column_widget = ColumnWidgetRegistry.create( c, self.scroll_widget_contents, host=self) self.column_widgets[c] = column_widget
def assignCols(self): # Load source and target columns respectively source_columns = self._source_columns() # Destination Columns self.targetTab = self.selected_destination_table() target_columns = table_column_names(self.targetTab, False, True) # Remove geometry columns and 'id' column in the target columns list target_columns = [c for c in target_columns if c not in self.geom_cols and c != 'id'] # now synchronize the lists, as much as possible # this consists of moving columns with matching names in the source and target lists to the same # placement at the top of the lists, and filtering out lists of remaining unmatched columns matched_source_columns = [] unmatched_source_columns = source_columns[:] matched_target_columns = [] unmatched_target_columns = target_columns[:] for source in source_columns: for target in unmatched_target_columns: if ImportData.names_are_matching(source, target): matched_source_columns.append(source) unmatched_source_columns = [c for c in unmatched_source_columns if c != source] matched_target_columns.append(target) unmatched_target_columns = [c for c in unmatched_target_columns if c != target] break # any matching columns get added to the start of the lists, and unmatched get added # to the end of the list for c in matched_source_columns + unmatched_source_columns: src_item = QListWidgetItem(c, self.lstSrcFields) # automatically check any columns we could match src_item.setCheckState(Qt.Checked if c in matched_source_columns else Qt.Unchecked) src_item.setIcon(GuiUtils.get_icon("column.png")) self.lstSrcFields.addItem(src_item) self._add_target_table_columns(matched_target_columns + unmatched_target_columns)
def register_column_widgets(self): """ Registers the column widgets. """ # Append column labels and widgets table_name = self._entity.name columns = table_column_names(table_name) self.scroll_widget_contents = QWidget() self.scroll_widget_contents.setObjectName( 'scrollAreaWidgetContents' ) for c in self._entity.columns.values(): if c.name in self.exclude_columns: continue if not c.name in columns and not isinstance(c, VirtualColumn): continue # Get widget factory column_widget = ColumnWidgetRegistry.create( c, self.scroll_widget_contents, host=self ) self.column_widgets[c] = column_widget
def update_str_table(self): """ Updates the database to the next version. """ for profile in self.config.profiles.values(): social_tenure = profile.social_tenure if not pg_table_exists(social_tenure.name, False): return parties = social_tenure.parties if len(parties) < 1: return party = parties[0].short_name.lower() party_table = parties[0].name old_column = 'party_id' if not old_column in table_column_names(social_tenure.name): return new_column = '{}_id'.format(party) if old_column != new_column: copy_from_column_to_another( str(social_tenure.name), old_column, new_column ) add_constraint(str(social_tenure.name), new_column, party_table)
def _on_enum_table_changed(self, index): """ Slot raised when an enumeration table is selected. :param index: Index of currently selected item. :type index: int """ enum_table = self.cbo_enum_table.currentText() self.cbo_primary_col.clear() self.cbo_other_col.clear() self._enum_col_names = [] if enum_table: self._enum_col_names = table_column_names(enum_table) #Remove id column name id_col = "id" id_idx = getIndex(self._enum_col_names, id_col) if id_idx != -1: self._enum_col_names.remove(id_col) self.cbo_primary_col.addItem("") self.cbo_primary_col.addItems(self._enum_col_names)
def _on_enum_table_changed(self, index): """ Slot raised when an enumeration table is selected. :param index: Index of currently selected item. :type index: int """ enum_table = self.cbo_enum_table.currentText() self.cbo_primary_col.clear() self.cbo_other_col.clear() self._enum_col_names = [] if enum_table: self._enum_col_names = table_column_names(enum_table) #Remove id column name id_col = "id" id_idx = getIndex(self._enum_col_names, id_col) if id_idx != -1: self._enum_col_names.remove(id_col) self.cbo_primary_col.addItem("") self.cbo_primary_col.addItems(self._enum_col_names)
def update_str_table(self): """ Updates the database to the next version. """ for profile in self.config.profiles.values(): social_tenure = profile.social_tenure if not pg_table_exists(social_tenure.name, False): return parties = social_tenure.parties if len(parties) < 1: return party = parties[0].short_name.lower() party_table = parties[0].name old_column = 'party_id' if not old_column in table_column_names(social_tenure.name): return new_column = '{}_id'.format(party) if old_column != new_column: copy_from_column_to_another(str(social_tenure.name), old_column, new_column) add_constraint(str(social_tenure.name), new_column, party_table)
def _init_entity_columns(self): """ Asserts if the entity columns actually do exist in the database. The method also initializes the table headers, entity column and cell formatters. """ self._headers[:] = [] table_name = self._entity.name columns = table_column_names(table_name) missing_columns = [] header_idx = 0 #Iterate entity column and assert if they exist for c in self._entity.columns.values(): # Exclude geometry columns if isinstance(c, GeometryColumn): continue #Do not include virtual columns in list of missing columns if not c.name in columns and not isinstance(c, VirtualColumn): missing_columns.append(c.name) else: header = c.ui_display() self._headers.append(header) col_name = c.name ''' If it is a virtual column then use column name as the header but fully qualified column name (created by SQLAlchemy relationship) as the entity attribute name. ''' if isinstance(c, MultipleSelectColumn): col_name = c.model_attribute_name self._entity_attrs.append(col_name) # Get widget factory so that we can use the value formatter w_factory = ColumnWidgetRegistry.factory(c.TYPE_INFO) if not w_factory is None: formatter = w_factory(c) self._cell_formatters[col_name] = formatter #Set searchable columns if c.searchable: self._searchable_columns[c.ui_display()] = { 'name': c.name, 'header_index': header_idx } header_idx += 1 if len(missing_columns) > 0: msg = QApplication.translate( 'EntityBrowser', u'The following columns have been defined in the ' u'configuration but are missing in corresponding ' u'database table, please re-run the configuration wizard ' u'to create them.\n{0}'.format('\n'.join(missing_columns))) QMessageBox.warning( self, QApplication.translate('EntityBrowser', 'Entity Browser'), msg)
def _setup_columns_content_area(self): # Only use this if entity supports documents # self.entity_tab_widget = None self.doc_widget = None self.entity_scroll_area = QScrollArea(self) self.entity_scroll_area.setFrameShape(QFrame.NoFrame) self.entity_scroll_area.setWidgetResizable(True) self.entity_scroll_area.setObjectName('scrollArea') # Grid layout for controls self.gl = QGridLayout(self.scroll_widget_contents) self.gl.setObjectName('gl_widget_contents') # Append column labels and widgets table_name = self._entity.name columns = table_column_names(table_name) # Iterate entity column and assert if they exist row_id = 0 for c, column_widget in self.column_widgets.iteritems(): if c.name in self.exclude_columns: continue if isinstance(c, MultipleSelectColumn): continue if not c.name in columns and not isinstance(c, VirtualColumn): continue if column_widget is not None: header = c.ui_display() self.c_label = QLabel(self.scroll_widget_contents) self.c_label.setText(header) self.gl.addWidget(self.c_label, row_id, 0, 1, 1) if c.TYPE_INFO == 'AUTO_GENERATED': column_widget.setReadOnly(False) column_widget.btn_load.hide() self.gl.addWidget(column_widget, row_id, 1, 1, 1) col_name = c.name # Add widget to MapperMixin collection self.addMapping( col_name, column_widget, c.mandatory, pseudoname=c.ui_display() ) # Bump up row_id row_id += 1 self.entity_scroll_area.setWidget(self.scroll_widget_contents) if self.entity_tab_widget is None: self.entity_tab_widget = QTabWidget(self) # Check if there are children and add foreign key browsers # Add primary tab if necessary self._add_primary_attr_widget() # self.entity_tab_widget.setTabEnabled(0, False) # enable/disable the tab # set the style sheet self.setStyleSheet( "QTabBar::tab::selected {width: 0; height: 0; margin: 0; " "padding: 0; border: none;} ") # Return the correct widget if self.entity_tab_widget is not None: return self.entity_tab_widget return self.entity_scroll_area
def _populate_layers(self): self.stdm_layers_combo.clear() if self._curr_profile is None: return self.spatial_units = self._curr_profile.social_tenure.spatial_units # Get entities containing geometry # columns based on the config info config_entities = self._curr_profile.entities self.geom_entities = [ ge for ge in config_entities.values() if ge.TYPE_INFO == 'ENTITY' and ge.has_geometry_column() ] self._profile_spatial_layers = [] self.sp_tables = spatial_tables() for e in self.geom_entities: table_name = e.name if table_name in self.sp_tables: for i, gc in enumerate(e.geometry_columns()): column_name = gc.name display_name = gc.layer_display() if i > 0: display_name = u'{}.{}'.format(display_name, gc.name) self._add_geometry_column_to_combo( table_name, column_name, display_name, gc ) # Add geometry entity to the collection self._profile_spatial_layers.append( table_name ) # Append the corresponding(profile) view to the list of entity names str_views = self._curr_profile.social_tenure.views.keys() for str_view in str_views: if str_view in self.sp_tables: self.str_view_geom_columns = table_column_names( str_view, True ) if len(self.str_view_geom_columns) > 0: # Pick the first column for i, geom_col in enumerate(self.str_view_geom_columns): view_layer_name = str_view if i > 0: view_layer_name = '{}.{}'.format( view_layer_name, geom_col ) self._add_geometry_column_to_combo( str_view, geom_col, view_layer_name, self._curr_profile.social_tenure ) # Append view to the list of spatial layers self._profile_spatial_layers.append( str_view ) # add old config views and custom views. for sp_table in self.sp_tables: if sp_table in pg_views() and sp_table not in str_views and \ sp_table in profile_and_user_views( self._curr_profile): view_geom_columns = table_column_names( sp_table, True ) for geom_col in view_geom_columns: view_layer_name = '{}.{}'.format( sp_table, geom_col ) self._add_geometry_column_to_combo( sp_table, geom_col, view_layer_name, geom_col )
def _populate_layers(self): self.stdm_layers_combo.clear() if self._curr_profile is None: return self.spatial_units = self._curr_profile.social_tenure.spatial_units # Get entities containing geometry # columns based on the config info config_entities = self._curr_profile.entities self.geom_entities = [ ge for ge in config_entities.values() if ge.TYPE_INFO == 'ENTITY' and ge.has_geometry_column() ] self._profile_spatial_layers = [] self.sp_tables = spatial_tables() for e in self.geom_entities: table_name = e.name if table_name in self.sp_tables: for i, gc in enumerate(e.geometry_columns()): column_name = gc.name display_name = gc.layer_display() if i > 0: display_name = '{}.{}'.format(display_name, gc.name) self._add_geometry_column_to_combo(table_name, column_name, display_name, gc) # Add geometry entity to the collection self._profile_spatial_layers.append(table_name) # Append the corresponding(profile) view to the list of entity names str_views = list(self._curr_profile.social_tenure.views.keys()) for str_view in str_views: if str_view in self.sp_tables: self.str_view_geom_columns = table_column_names(str_view, True) if len(self.str_view_geom_columns) > 0: # Pick the first column for i, geom_col in enumerate(self.str_view_geom_columns): view_layer_name = str_view if i > 0: view_layer_name = '{}.{}'.format( view_layer_name, geom_col) self._add_geometry_column_to_combo( str_view, geom_col, view_layer_name, self._curr_profile.social_tenure) # Append view to the list of spatial layers self._profile_spatial_layers.append(str_view) # add old config views and custom views. for sp_table in self.sp_tables: if sp_table in pg_views() and sp_table not in str_views and \ sp_table in profile_and_user_views( self._curr_profile): view_geom_columns = table_column_names(sp_table, True) for geom_col in view_geom_columns: view_layer_name = '{}.{}'.format(sp_table, geom_col) self._add_geometry_column_to_combo(sp_table, geom_col, view_layer_name, geom_col)
def _setup_columns_content_area(self): # Only use this if entity supports documents # self.entity_tab_widget = None self.doc_widget = None self.entity_scroll_area = QScrollArea(self) self.entity_scroll_area.setFrameShape(QFrame.NoFrame) self.entity_scroll_area.setWidgetResizable(True) self.entity_scroll_area.setObjectName('scrollArea') # Grid layout for controls self.gl = QGridLayout(self.scroll_widget_contents) self.gl.setObjectName('gl_widget_contents') # Append column labels and widgets table_name = self._entity.name columns = table_column_names(table_name) # Iterate entity column and assert if they exist row_id = 0 for column_name, column_widget in self.column_widgets.items(): c = self.columns[column_name] if c.name in self.exclude_columns: continue if isinstance(c, MultipleSelectColumn): continue if c.name not in columns and not isinstance(c, VirtualColumn): continue if column_widget is not None: header = c.ui_display() self.c_label = QLabel(self.scroll_widget_contents) self.c_label.setText(header) self.gl.addWidget(self.c_label, row_id, 0, 1, 1) if c.TYPE_INFO == 'AUTO_GENERATED': column_widget.setReadOnly(False) column_widget.btn_load.hide() self.gl.addWidget(column_widget, row_id, 1, 1, 1) col_name = c.name # Add widget to MapperMixin collection self.addMapping( col_name, column_widget, c.mandatory, pseudoname=c.ui_display() ) # Bump up row_id row_id += 1 self.entity_scroll_area.setWidget(self.scroll_widget_contents) if self.entity_tab_widget is None: self.entity_tab_widget = QTabWidget(self) # Check if there are children and add foreign key browsers # Add primary tab if necessary self._add_primary_attr_widget() # self.entity_tab_widget.setTabEnabled(0, False) # enable/disable the tab # set the style sheet self.setStyleSheet( "QTabBar::tab::selected {width: 0; height: 0; margin: 0; " "padding: 0; border: none;} ") # Return the correct widget if self.entity_tab_widget is not None: return self.entity_tab_widget return self.entity_scroll_area
def _init_fk_columns(self): """ Asserts if the entity columns actually do exist in the database. The method also initializes the table headers, entity column and cell formatters. """ self._headers[:] = [] self._entity_attrs[:] = [] if self._dbmodel is None: msg = QApplication.translate( 'ForeignKeyMapper', 'The data model for ' 'the entity could ' 'not be loaded, ' 'please contact ' 'your database ' 'administrator.' ) QMessageBox.critical( self, QApplication.translate( 'EntityBrowser', 'Entity Browser' ), msg ) return table_name = self._entity.name columns = table_column_names(table_name) missing_columns = [] header_idx = 0 #Iterate entity column and assert if they exist for c in self._entity.columns.values(): #Do not include virtual columns in list of missing columns if not c.name in columns and not isinstance(c, VirtualColumn): missing_columns.append(c.name) else: header = c.header() self._headers.append(header) ''' If it is a virtual column then use column name as the header but fully qualified column name (created by SQLAlchemy relationship) as the entity attribute name. ''' col_name = c.name if isinstance(c, MultipleSelectColumn): col_name = c.model_attribute_name self._entity_attrs.append(col_name) #Get widget factory so that we can use the value formatter w_factory = ColumnWidgetRegistry.factory(c.TYPE_INFO) if not w_factory is None: try: formatter = w_factory(c) self._cell_formatters[col_name] = formatter except WidgetException as we: msg = QApplication.translate( 'ForeignKeyMapper', 'Error in creating column:' ) msg = '{0} {1}:{2}\n{3}'.format( msg, self._entity.name, c.name, unicode(we) ) QMessageBox.critical( self, QApplication.translate( 'ForeignKeyMapper', 'Widget Creation Error' ), msg ) #Set searchable columns if c.searchable: self._searchable_columns[header] = { 'name': c.name, 'header_index': header_idx } header_idx += 1 if len(missing_columns) > 0: msg = QApplication.translate( 'ForeignKeyMapper', u'The following columns have been defined in the ' u'configuration but are missing in corresponding ' u'database table, please re-run the configuration wizard ' u'to create them.\n{0}'.format( '\n'.join(missing_columns) ) ) QMessageBox.warning( self, QApplication.translate('ForeignKeyMapper','Entity Browser'), msg ) self._tableModel = BaseSTDMTableModel([], self._headers, self) self._tbFKEntity.setModel(self._tableModel) self._tbFKEntity.resizeColumnsToContents() #First (id) column will always be hidden self._tbFKEntity.hideColumn(0) self._tbFKEntity.horizontalHeader().setResizeMode( QHeaderView.Interactive ) self._tbFKEntity.verticalHeader().setVisible(True)
def _init_entity_columns(self): """ Asserts if the entity columns actually do exist in the database. The method also initializes the table headers, entity column and cell formatters. """ self._headers[:] = [] table_name = self._entity.name columns = table_column_names(table_name) missing_columns = [] header_idx = 0 #Iterate entity column and assert if they exist for c in self._entity.columns.values(): # Exclude geometry columns if isinstance(c, GeometryColumn): continue # Do not include virtual columns in list of missing columns if not c.name in columns and not isinstance(c, VirtualColumn): missing_columns.append(c.name) else: header = c.ui_display() self._headers.append(header) col_name = c.name ''' If it is a virtual column then use column name as the header but fully qualified column name (created by SQLAlchemy relationship) as the entity attribute name. ''' if isinstance(c, MultipleSelectColumn): col_name = c.model_attribute_name self._entity_attrs.append(col_name) # Get widget factory so that we can use the value formatter w_factory = ColumnWidgetRegistry.factory(c.TYPE_INFO) if not w_factory is None: formatter = w_factory(c) self._cell_formatters[col_name] = formatter # Set searchable columns if c.searchable: self._searchable_columns[c.ui_display()] = { 'name': c.name, 'header_index': header_idx } header_idx += 1 if len(missing_columns) > 0: msg = QApplication.translate( 'EntityBrowser', u'The following columns have been defined in the ' u'configuration but are missing in corresponding ' u'database table, please re-run the configuration wizard ' u'to create them.\n{0}'.format( '\n'.join(missing_columns) ) ) QMessageBox.warning( self, QApplication.translate('EntityBrowser','Entity Browser'), msg )
def _table_column_names(table): # Returns both spatial and non-spatial column names in the given table. sp_cols = table_column_names(table, True) textual_cols = table_column_names(table) return sp_cols + textual_cols
def _setup_columns_content_area(self): # Only use this if entity supports documents # self.entity_tab_widget = None self.doc_widget = None self.entity_scroll_area = QScrollArea(self) self.entity_scroll_area.setFrameShape(QFrame.NoFrame) self.entity_scroll_area.setWidgetResizable(True) self.entity_scroll_area.setObjectName('scrollArea') # Grid layout for controls self.gl = QGridLayout(self.scroll_widget_contents) self.gl.setObjectName('gl_widget_contents') # Append column labels and widgets table_name = self._entity.name columns = table_column_names(table_name) # Iterate entity column and assert if they exist row_id = 0 for c, column_widget in self.column_widgets.iteritems(): if c.name in self.exclude_columns: continue if not c.name in columns and not isinstance(c, VirtualColumn): continue if column_widget is not None: header = c.ui_display() self.c_label = QLabel(self.scroll_widget_contents) # Format label text if it is a mandatory field if c.mandatory: header = u'{0} *'.format(c.ui_display()) #Highlight asterisk header = self._highlight_asterisk(header) self.c_label.setText(header) self.gl.addWidget(self.c_label, row_id, 0, 1, 1) self.column_widget = column_widget self.gl.addWidget(self.column_widget, row_id, 1, 1, 1) #Add user tip if specified for the column configuration if c.user_tip: self.tip_lbl = UserTipLabel(user_tip=c.user_tip) self.gl.addWidget(self.tip_lbl, row_id, 2, 1, 1) if c.mandatory and not self.has_mandatory: self.has_mandatory = True col_name = c.name #Replace name accordingly based on column type if isinstance(c, MultipleSelectColumn): col_name = c.model_attribute_name # Add widget to MapperMixin collection self.addMapping( col_name, self.column_widget, c.mandatory, pseudoname=c.ui_display() ) # Bump up row_id row_id += 1 self.entity_scroll_area.setWidget(self.scroll_widget_contents) if self.entity_tab_widget is None: self.entity_tab_widget = QTabWidget(self) # Check if there are children and add foreign key browsers # Add primary tab if necessary self._add_primary_attr_widget() if not self._disable_collections: ch_entities = self.children_entities() for col, ch in ch_entities.iteritems(): if hasattr(col.entity_relation, 'show_in_parent'): if col.entity_relation.show_in_parent != '0': self._add_fk_browser(ch, col) else: self._add_fk_browser(ch, col) #Add tab widget if entity supports documents if self._entity.supports_documents: self.doc_widget = SupportingDocumentsWidget( self._entity.supporting_doc, self._ent_document_model, self ) # Map the source document manager object self.addMapping( 'documents', self.doc_widget.source_document_manager ) # # # Add attribute tab # self._add_primary_attr_widget() # Add supporting documents tab self.entity_tab_widget.addTab( self.doc_widget, self.tr('Supporting Documents') ) # Return the correct widget if not self.entity_tab_widget is None: return self.entity_tab_widget return self.entity_scroll_area
def loadGeomCols(self, table): #Load geometry columns based on the selected table self.geomcols = table_column_names(table, True, True) self.geomClm.clear() self.geomClm.addItems(self.geomcols)
def _setup_columns_content_area(self): #Only use this if entity supports documents self.entity_tab_widget = None self.doc_widget = None self.entity_scroll_area = QScrollArea(self) self.entity_scroll_area.setFrameShape(QFrame.NoFrame) self.entity_scroll_area.setWidgetResizable(True) self.entity_scroll_area.setObjectName('scrollArea') self.scroll_widget_contents = QWidget() self.scroll_widget_contents.setObjectName( 'scrollAreaWidgetContents' ) #Grid layout for controls self.gl = QGridLayout(self.scroll_widget_contents) self.gl.setObjectName('gl_widget_contents') #Append column labels and widgets table_name = self._entity.name columns = table_column_names(table_name) #Iterate entity column and assert if they exist row_id = 0 for c in self._entity.columns.values(): if not c.name in columns and not isinstance(c, VirtualColumn): continue #Get widget factory column_widget = ColumnWidgetRegistry.create( c, self.scroll_widget_contents ) if not column_widget is None: header = c.header() self.c_label = QLabel(self.scroll_widget_contents) #Format label text if it is a mandatory field if c.mandatory: header = '{0} *'.format(c.header()) #Highlight asterisk header = self._highlight_asterisk(header) self.c_label.setText(header) self.gl.addWidget(self.c_label, row_id, 0, 1, 1) self.column_widget = column_widget self.gl.addWidget(self.column_widget, row_id, 1, 1, 1) #Add user tip if specified for the column configuration if c.user_tip: self.tip_lbl = UserTipLabel(user_tip=c.user_tip) self.gl.addWidget(self.tip_lbl, row_id, 2, 1, 1) if c.mandatory and not self.has_mandatory: self.has_mandatory = True col_name = c.name #Replace name accordingly based on column type if isinstance(c, MultipleSelectColumn): col_name = c.model_attribute_name #Add widget to MapperMixin collection self.addMapping( col_name, self.column_widget, c.mandatory, pseudoname=c.header() ) #Bump up row_id row_id += 1 self.entity_scroll_area.setWidget( self.scroll_widget_contents ) #Check if there are children and add foreign key browsers ch_entities = self.children_entities() if len(ch_entities) > 0: if self.entity_tab_widget is None: self.entity_tab_widget = QTabWidget(self) #Add primary tab if necessary self._add_primary_attr_widget() for ch in ch_entities: self._add_fk_browser(ch) #Add tab widget if entity supports documents if self._entity.supports_documents: self.doc_widget = SupportingDocumentsWidget( self._entity.supporting_doc, self._ent_document_model, self ) #Map the source document manager object self.addMapping( 'documents', self.doc_widget.source_document_manager ) if self.entity_tab_widget is None: self.entity_tab_widget = QTabWidget(self) #Add attribute tab self._add_primary_attr_widget() #Add supporting documents tab self.entity_tab_widget.addTab( self.doc_widget, self.tr('Supporting Documents') ) #Return the correct widget if not self.entity_tab_widget is None: return self.entity_tab_widget return self.entity_scroll_area
def loadGeomCols(self, table): #Load geometry columns based on the selected table self.geomcols = table_column_names(table, True, True) self.geomClm.clear() self.geomClm.addItems(self.geomcols)
def _init_fk_columns(self): """ Asserts if the entity columns actually do exist in the database. The method also initializes the table headers, entity column and cell formatters. """ self._headers[:] = [] self._entity_attrs[:] = [] if self._dbmodel is None: msg = QApplication.translate( 'ForeignKeyMapper', 'The data model for ' 'the entity could ' 'not be loaded, ' 'please contact ' 'your database ' 'administrator.') QMessageBox.critical( self, QApplication.translate('EntityBrowser', 'Entity Browser'), msg) return table_name = self._entity.name columns = table_column_names(table_name) missing_columns = [] header_idx = 0 # Iterate entity column and assert if they exist for c in self._entity.columns.values(): # Do not include virtual columns in list of missing columns if not c.name in columns and not isinstance(c, VirtualColumn): missing_columns.append(c.name) else: header = c.header() self._headers.append(header) ''' If it is a virtual column then use column name as the header but fully qualified column name (created by SQLAlchemy relationship) as the entity attribute name. ''' col_name = c.name if isinstance(c, MultipleSelectColumn): col_name = c.model_attribute_name self._entity_attrs.append(col_name) # Get widget factory so that we can use the value formatter w_factory = ColumnWidgetRegistry.factory(c.TYPE_INFO) if not w_factory is None: try: formatter = w_factory(c) self._cell_formatters[col_name] = formatter except WidgetException as we: msg = QApplication.translate( 'ForeignKeyMapper', 'Error in creating column:') msg = '{0} {1}:{2}\n{3}'.format( msg, self._entity.name, c.name, str(we)) QMessageBox.critical( self, QApplication.translate('ForeignKeyMapper', 'Widget Creation Error'), msg) # Set searchable columns if c.searchable: self._searchable_columns[header] = { 'name': c.name, 'header_index': header_idx } header_idx += 1 if len(missing_columns) > 0: msg = QApplication.translate( 'ForeignKeyMapper', 'The following columns have been defined in the ' 'configuration but are missing in corresponding ' 'database table, please re-run the configuration wizard ' 'to create them.\n{0}'.format('\n'.join(missing_columns))) QMessageBox.warning( self, QApplication.translate('ForeignKeyMapper', 'Entity Browser'), msg) self._tableModel = BaseSTDMTableModel([], self._headers, self) self._tbFKEntity.setModel(self._tableModel) # First (id) column will always be hidden self._tbFKEntity.hideColumn(0) self._tbFKEntity.horizontalHeader().setSectionResizeMode( QHeaderView.Interactive) self._tbFKEntity.horizontalHeader().setStretchLastSection(True) self._tbFKEntity.verticalHeader().setVisible(True)
def _table_column_names(table): # Returns both spatial and non-spatial column names in the given table. sp_cols = table_column_names(table, True) textual_cols = table_column_names(table) return sp_cols + textual_cols