def fill_table_prices(self, qtable, table_view, new_camp, set_edit_triggers=QTableView.NoEditTriggers): """ Set a model with selected filter and attach it to selected table @setEditStrategy: 0: OnFieldChange, 1: OnRowChange, 2: OnManualSubmit """ if self.schema_name not in table_view: table_view = self.schema_name + "." + table_view # Set model model = QSqlTableModel() model.setTable(table_view) model.setEditStrategy(QSqlTableModel.OnFieldChange) model.setSort(2, 0) model.select() qtable.setEditTriggers(set_edit_triggers) # Check for errors if model.lastError().isValid(): self.controller.show_warning(model.lastError().text()) return # Attach model to table view expr = f"campaign_id = '{new_camp}'" qtable.setModel(model) qtable.model().setFilter(expr)
def fill_table(self, widget, table_name, set_edit_strategy=QSqlTableModel.OnManualSubmit, expr_filter=None): """ Set a model with selected filter. Attach that model to selected table """ if self.schema_name not in table_name: table_name = self.schema_name + "." + table_name # Set model self.model = QSqlTableModel() self.model.setTable(table_name) self.model.setEditStrategy(set_edit_strategy) self.model.setSort(0, 0) self.model.select() # Check for errors if self.model.lastError().isValid(): self.controller.show_warning(self.model.lastError().text()) # Attach model to table view widget.setModel(self.model) if expr_filter: widget.model().setFilter(expr_filter)
def fill_table_relations(self, widget, table_name): """ Set a model with selected filter. Attach that model to selected table """ if self.schema_name not in table_name: table_name = self.schema_name + "." + table_name filter_ = "" for row in self.selected_list: filter_ += f"arc_id = '{row}' OR " filter_ = filter_[:-3] + "" filter_ += " AND arc_state = '1' " # Set model model = QSqlTableModel() model.setTable(table_name) model.setEditStrategy(QSqlTableModel.OnManualSubmit) model.setFilter(filter_) model.select() # Check for errors if model.lastError().isValid(): self.controller.show_warning(model.lastError().text()) # Attach model to table view widget.setModel(model) widget.show()
def fill_table(qtable, table_name, expr_filter=None, edit_strategy=QSqlTableModel.OnManualSubmit, sort_order=Qt.AscendingOrder): """ Set a model with selected filter. Attach that model to selected table :param qtable: tableview where set the model (QTableView) :param table_name: database table name or view name (String) :param expr_filter: expression to filter the model (String) :param edit_strategy: (QSqlTableModel.OnFieldChange, QSqlTableModel.OnManualSubmit, QSqlTableModel.OnRowChange) :param sort_order: can be 0 or 1 (Qt.AscendingOrder or Qt.AscendingOrder) :return: """ if global_vars.schema_name and global_vars.schema_name not in table_name: table_name = f"{global_vars.schema_name}.{table_name}" # Set model model = QSqlTableModel(db=global_vars.qgis_db_credentials) model.setTable(table_name) model.setEditStrategy(edit_strategy) model.setSort(0, sort_order) if expr_filter is not None: model.setFilter(expr_filter) model.select() # Check for errors if model.lastError().isValid(): tools_log.log_warning(f"fill_table: {model.lastError().text()}") # Attach model to tableview qtable.setModel(model)
def workcat_fill_table(self, widget, table_name, set_edit_triggers=QTableView.NoEditTriggers, expr=None): """ Fill table @widget filtering query by @workcat_id Set a model with selected filter. Attach that model to selected table @setEditStrategy: 0: OnFieldChange 1: OnRowChange 2: OnManualSubmit """ if self.schema_name not in table_name: table_name = self.schema_name + "." + table_name # Set model model = QSqlTableModel() model.setTable(table_name) model.setEditStrategy(QSqlTableModel.OnFieldChange) model.setSort(0, 0) model.select() widget.setEditTriggers(set_edit_triggers) # Check for errors if model.lastError().isValid(): self.controller.show_warning(model.lastError().text()) # Attach model to table view if expr: widget.setModel(model) widget.model().setFilter(expr) else: widget.setModel(model)
def connect_run_info(self): self.run_info_model = QSqlTableModel(db=self.dd.db) self.run_info_model.setTable('hsrr.run_info') self.run_info_model.setSort(self.run_info_model.fieldIndex("run"), Qt.AscendingOrder) self.run_info_model.setEditStrategy(QSqlTableModel.OnFieldChange) self.run_info_view.setModel(self.run_info_model)
def fill_table_planned_month(self, qtable, txt_filter, tableright, expression=None, set_edit_triggers=QTableView.NoEditTriggers): """ Set a model with selected filter and attach it to selected table @setEditStrategy: 0: OnFieldChange, 1: OnRowChange, 2: OnManualSubmit """ if self.schema_name not in tableright: tableright = f"{self.schema_name}.{tableright}" # Set model model = QSqlTableModel() model.setTable(tableright) model.setEditStrategy(QSqlTableModel.OnManualSubmit) model.setSort(2, 0) model.select() qtable.setEditTriggers(set_edit_triggers) # Check for errors if model.lastError().isValid(): self.controller.show_warning(model.lastError().text()) # Create expresion expr = (f" mu_name ILIKE '%{txt_filter.text()}%' " f" AND campaign_id = '{self.planned_camp_id}' ") if expression is not None: expr += expression qtable.setModel(model) qtable.model().setFilter(expr)
def set_table_model(self, dialog, table_object, geom_type, expr_filter): """ Sets a TableModel to @widget_name attached to @table_name and filter @expr_filter """ expr = None if expr_filter: # Check expression (is_valid, expr) = self.check_expression(expr_filter) # @UnusedVariable if not is_valid: return expr # Set a model with selected filter expression table_name = f"v_edit_{geom_type}" if self.schema_name not in table_name: table_name = self.schema_name + "." + table_name # Set the model model = QSqlTableModel(db=self.controller.db) model.setTable(table_name) model.setEditStrategy(QSqlTableModel.OnManualSubmit) model.select() if model.lastError().isValid(): self.controller.show_warning(model.lastError().text()) return expr # Attach model to selected widget if type(table_object) is str: # self.controller.log_debug(f"set_table_model (str): {table_object}") widget = utils_giswater.getWidget(dialog, table_object) if not widget: message = "Widget not found" self.controller.log_info(message, parameter=table_object) return expr elif type(table_object) is QTableView: # self.controller.log_debug(f"set_table_model: {table_object.objectName()}") widget = table_object else: msg = "Table_object is not a table name or QTableView" self.controller.log_info(msg) return expr if expr_filter: widget.setModel(model) widget.model().setFilter(expr_filter) widget.model().select() else: widget.setModel(None) return expr
def fill_table(self, qtable, table_name, set_edit_triggers=QTableView.NoEditTriggers, expr_filter=None): """ Fill table @widget filtering query by @workcat_id Set a model with selected filter. Attach that model to selected table @setEditStrategy: 0: OnFieldChange 1: OnRowChange 2: OnManualSubmit """ expr = None if expr_filter: # Check expression (is_valid, expr) = self.check_expression(expr_filter) # @UnusedVariable if not is_valid: return expr # Set a model with selected filter expression if self.schema_name not in table_name: table_name = self.schema_name + "." + table_name # Set model model = QSqlTableModel() model.setTable(table_name) model.setEditStrategy(QSqlTableModel.OnManualSubmit) model.setSort(0, 0) model.select() # When change some field we need to refresh Qtableview and filter by psector_id qtable.setEditTriggers(set_edit_triggers) # Check for errors if model.lastError().isValid(): self.controller.show_warning(model.lastError().text()) # Attach model to table view if expr: qtable.setModel(model) qtable.model().setFilter(expr_filter) else: qtable.setModel(model) return expr
def set_table_model(dialog, table_object, table_name, expr_filter): """ Sets a TableModel to @widget_name attached to @table_name and filter @expr_filter """ expr = None if expr_filter: # Check expression (is_valid, expr) = check_expression_filter(expr_filter) if not is_valid: return expr if global_vars.schema_name and global_vars.schema_name not in table_name: table_name = f"{global_vars.schema_name}.{table_name}" # Set a model with selected filter expression model = QSqlTableModel(db=global_vars.qgis_db_credentials) model.setTable(table_name) model.setEditStrategy(QSqlTableModel.OnManualSubmit) model.select() if model.lastError().isValid(): tools_qgis.show_warning(model.lastError().text()) return expr # Attach model to selected widget if type(table_object) is str: widget = get_widget(dialog, table_object) if not widget: message = "Widget not found" tools_log.log_info(message, parameter=table_object) return expr elif type(table_object) is QTableView: widget = table_object else: msg = "Table_object is not a table name or QTableView" tools_log.log_info(msg) return expr if expr_filter: widget.setModel(model) widget.model().setFilter(expr_filter) widget.model().select() else: widget.setModel(None) return expr
def fill_main_table(self, dialog, table_name, set_edit_triggers=QTableView.NoEditTriggers, refresh_model=True): """ Set a model with selected filter and attach it to selected table @setEditStrategy: 0: OnFieldChange, 1: OnRowChange, 2: OnManualSubmit """ if self.schema_name not in table_name: table_name = self.schema_name + "." + table_name # Set model model = QSqlTableModel() model.setTable(table_name) model.setEditStrategy(QSqlTableModel.OnFieldChange) dialog.all_rows.setEditTriggers(set_edit_triggers) # Check for errors if model.lastError().isValid(): self.controller.show_warning(model.lastError().text()) return # Get all ids from Qtable selected_rows id_all_selected_rows = self.select_all_rows(dialog.selected_rows, 'mu_id') # Convert id_all_selected_rows to string ids = "0, " for x in range(0, len(id_all_selected_rows)): ids += str(id_all_selected_rows[x]) + ", " ids = ids[:-2] + "" # Build expression expr = (f" mu_name ILIKE '%{dialog.txt_search.text()}%'" f" AND mu_id NOT IN ({ids})" f" AND campaign_id::text = '{self.campaign_id}'" f" OR campaign_id IS null") self.controller.log_info(expr) # (is_valid, expr) = self.check_expression(expr) # @UnusedVariable # # if not is_valid: # # return model.setFilter(expr) # Refresh model? if refresh_model: model.select() # Attach model to table view dialog.all_rows.setModel(model)
def fill_table_mincut_management(self, widget, table_name): """ Set a model with selected filter. Attach that model to selected table """ if self.schema_name not in table_name: table_name = self.schema_name + "." + table_name # Set model model = QSqlTableModel() model.setTable(table_name) model.setEditStrategy(QSqlTableModel.OnManualSubmit) model.sort(0, 1) model.select() # Check for errors if model.lastError().isValid(): self.controller.show_warning(model.lastError().text()) # Attach model to table view widget.setModel(model)
def fill_table_by_expr(self, qtable, table_name, expr): """ :param qtable: QTableView to show :param expr: expression to set model """ if self.schema_name not in table_name: table_name = self.schema_name + "." + table_name model = QSqlTableModel() model.setTable(table_name) model.setFilter(expr) model.setEditStrategy(QSqlTableModel.OnFieldChange) qtable.setEditTriggers(QTableView.DoubleClicked) model.select() qtable.setModel(model) qtable.show() # Check for errors if model.lastError().isValid(): self.controller.show_warning(model.lastError().text())
def set_table_model(self, qtable, geom_type, expr_filter): """ Sets a TableModel to @widget_name attached to @table_name and filter @expr_filter """ expr = None if expr_filter: # Check expression (is_valid, expr) = self.check_expression(expr_filter) # @UnusedVariable if not is_valid: return expr # Set a model with selected filter expression table_name = "v_edit_" + geom_type if self.schema_name not in table_name: table_name = self.schema_name + "." + table_name # Set the model model = QSqlTableModel() model.setTable(table_name) model.setEditStrategy(QSqlTableModel.OnManualSubmit) model.select() if model.lastError().isValid(): self.controller.show_warning(model.lastError().text()) return expr # Attach model to selected widget if type(qtable) is QTableView: widget = qtable else: message = "Table_object is not a table name or QTableView" self.controller.log_info(message) return expr if expr_filter: widget.setModel(model) widget.model().setFilter(expr_filter) widget.model().select() else: widget.setModel(None) return expr
def fill_table_object(widget, table_name, expr_filter=None): """ Set a model with selected filter. Attach that model to selected table """ if global_vars.schema_name not in table_name: table_name = global_vars.schema_name + "." + table_name # Set model model = QSqlTableModel() model.setTable(table_name) model.setEditStrategy(QSqlTableModel.OnManualSubmit) model.sort(0, 1) if expr_filter: model.setFilter(expr_filter) model.select() # Check for errors if model.lastError().isValid(): global_vars.controller.show_warning(model.lastError().text()) # Attach model to table view widget.setModel(model)
def fill_table(self, dialog, table_view, set_edit_triggers=QTableView.NoEditTriggers, update=False): """ Set a model with selected filter and attach it to selected table @setEditStrategy: 0: OnFieldChange, 1: OnRowChange, 2: OnManualSubmit """ if self.schema_name not in table_view: table_view = self.schema_name + "." + table_view # Set model model = QSqlTableModel() model.setTable(table_view) model.setEditStrategy(QSqlTableModel.OnManualSubmit) model.setSort(2, 0) model.select() dialog.selected_rows.setEditTriggers(set_edit_triggers) # Check for errors if model.lastError().isValid(): self.controller.show_warning(model.lastError().text()) # Create expresion expr = f" mu_name ILIKE '%{dialog.txt_selected_filter.text()}%'" if self.selected_camp is not None: expr += f" AND campaign_id = '{self.campaign_id}'" if update: expr += f" OR campaign_id = '{self.selected_camp}'" # Attach model to table or view dialog.selected_rows.setModel(model) dialog.selected_rows.model().setFilter(expr) # Set year to plan to all rows in list for x in range(0, model.rowCount()): i = int(dialog.selected_rows.model().fieldIndex('campaign_id')) index = dialog.selected_rows.model().index(x, i) model.setData(index, self.campaign_id) self.calculate_total_price(dialog, self.campaign_id)
def set_model_to_table(self, widget, table_name, expr_filter): """ Set a model with selected filter. Attach that model to selected table """ if self.schema_name not in table_name: table_name = self.schema_name + "." + table_name # Set model model = QSqlTableModel(); model.setTable(table_name) model.setEditStrategy(QSqlTableModel.OnManualSubmit) model.setFilter(expr_filter) model.select() # Check for errors if model.lastError().isValid(): self.controller.show_warning(model.lastError().text()) # Attach model to table view if widget: widget.setModel(model) else: self.controller.log_info("set_model_to_table: widget not found")