def searchModel(self, columnIndex, columnValue): ''' Searches for 'columnValue' in the column whose index is specified by 'columnIndex' in all rows contained in the model. ''' if isinstance(columnValue, QVariant): columnValue = str(columnValue.toString()) if not isinstance(columnValue, str): columnValue = str(columnValue) columnValue = columnValue.strip() proxy = QSortFilterProxyModel(self) proxy.setSourceModel(self._tableModel) proxy.setFilterKeyColumn(columnIndex) proxy.setFilterFixedString(columnValue) # Will return model index containing the primary key. matchingIndex = proxy.mapToSource(proxy.index(0, 0)) return matchingIndex
def refresh_model(self, source_model, db_connector=None, silent=False): filtered_source_model = QSortFilterProxyModel() filtered_source_model.setSourceModel(source_model) filtered_source_model.setFilterRole(int(SourceModel.Roles.TYPE)) self.print_info.emit(self.tr("Refresh available models:")) self.clear() previously_checked_models = self._checked_models self._checked_models = {} # models from db db_modelnames = self._db_modelnames(db_connector) # models from the repos filtered_source_model.setFilterFixedString("model") for r in range(0, filtered_source_model.rowCount()): filtered_source_model_index = filtered_source_model.index( r, SourceModel.Columns.SOURCE) modelname = filtered_source_model_index.data( int(SourceModel.Roles.NAME)) if modelname: enabled = modelname not in db_modelnames self.add_source( modelname, filtered_source_model_index.data( int(SourceModel.Roles.TYPE)), filtered_source_model_index.data( int(SourceModel.Roles.PATH)), filtered_source_model_index.data( int(SourceModel.Roles.ORIGIN_INFO)), previously_checked_models.get( ( modelname, filtered_source_model_index.data( int(SourceModel.Roles.PATH)), ), Qt.Checked, ) if enabled and modelname not in self.checked_models() else Qt.Unchecked, enabled, ) if not silent: self.print_info.emit( self.tr("- Append (repository) model {}{}").format( modelname, " (inactive because it already exists in the database)" if not enabled else "", )) # models from the files filtered_source_model.setFilterFixedString("ili") for r in range(0, filtered_source_model.rowCount()): filtered_source_model_index = filtered_source_model.index( r, SourceModel.Columns.SOURCE) ili_file_path = filtered_source_model_index.data( int(SourceModel.Roles.PATH)) self.ilicache = IliCache(None, ili_file_path) models = self.ilicache.process_ili_file(ili_file_path) for model in models: if model["name"]: enabled = model["name"] not in db_modelnames self.add_source( model["name"], filtered_source_model_index.data( int(SourceModel.Roles.TYPE)), filtered_source_model_index.data( int(SourceModel.Roles.PATH)), filtered_source_model_index.data( int(SourceModel.Roles.ORIGIN_INFO)), previously_checked_models.get( ( model["name"], filtered_source_model_index.data( int(SourceModel.Roles.PATH)), ), Qt.Checked if model is models[-1] and enabled and model["name"] not in self.checked_models() else Qt.Unchecked, ), enabled, ) if not silent: self.print_info.emit( self.tr("- Append (file) model {}{} from {}"). format( model["name"], " (inactive because it already exists in the database)" if not enabled else "", ili_file_path, )) # models from the transfer files filtered_source_model.setFilterRegExp("|".join(TransferExtensions)) for r in range(0, filtered_source_model.rowCount()): filtered_source_model_index = filtered_source_model.index( r, SourceModel.Columns.SOURCE) xtf_file_path = filtered_source_model_index.data( int(SourceModel.Roles.PATH)) models = self._transfer_file_models(xtf_file_path) for model in models: if model["name"]: enabled = model["name"] not in db_modelnames self.add_source( model["name"], filtered_source_model_index.data( int(SourceModel.Roles.TYPE)), filtered_source_model_index.data( int(SourceModel.Roles.PATH)), filtered_source_model_index.data( int(SourceModel.Roles.ORIGIN_INFO)), previously_checked_models.get( ( model["name"], filtered_source_model_index.data( int(SourceModel.Roles.PATH)), ), Qt.Checked if enabled and model["name"] not in self.checked_models() else Qt.Unchecked, ), enabled, ) if not silent: self.print_info.emit( self.tr("- Append (xtf file) model {}{} from {}"). format( model["name"], " (inactive because it already exists in the database)" if not enabled else "", xtf_file_path, )) return self.rowCount()