def __init__(self, settings, parent=None): super().__init__(parent) self.setMinimumWidth(100) for status, statusTitle in Statuses.items(): if settings[status + '_status_used']: self.addItem(statusIcon(status), statusTitle, status)
def setCurrentValue(self, value): self.setText(Statuses[value]) self.home(False) for act in self.actions(): self.removeAction(act) icon = statusIcon(value) self.action = self.addAction(icon, QLineEdit.LeadingPosition) self.data = value self.currentIndexChanged.emit(-1)
def prepareMenu(self): self.listWidget = QListWidget(self) filters = self.filters.copy() appliedValues = [] columnFilters = None revert = False if self.fieldid in filters.keys(): columnFilters = filters.pop(self.fieldid) for filter_ in columnFilters.filters(): if filter_.isRevert(): revert = True appliedValues.append(filter_.value) hasBlanks = False columnType = self.model.columnType(self.fieldid) if self.model.columnName(self.fieldid) == 'year': filtersSql = self.filtersToSql(filters.values()) if filtersSql: filtersSql = 'WHERE ' + filtersSql sql = "SELECT DISTINCT %s FROM coins %s" % (self.columnName, filtersSql) query = QSqlQuery(sql, self.db) while query.next(): icon = None if query.record().isNull(0): data = None else: orig_data = query.record().value(0) data = str(orig_data) label = data try: year = int(orig_data) if year < 0: label = "%d BC" % -year except ValueError: pass if not data: hasBlanks = True continue item = CustomSortListWidgetItem() item.setData(Qt.DisplayRole, label) item.setData(Qt.UserRole, data) item.setData(Qt.UserRole + 1, orig_data) if data in appliedValues: if revert: item.setCheckState(Qt.Checked) else: item.setCheckState(Qt.Unchecked) else: if revert: item.setCheckState(Qt.Unchecked) else: item.setCheckState(Qt.Checked) self.listWidget.addItem(item) self.listWidget.sortItems() elif columnType == Type.Text or columnType in Type.ImageTypes: dataFilter = BlankFilter(self.columnName).toSql() blanksFilter = DataFilter(self.columnName).toSql() filtersSql = self.filtersToSql(filters.values()) sql = "SELECT 1 FROM coins WHERE " + filtersSql if filtersSql: sql += ' AND ' # Get blank row count blank_sql = sql + blanksFilter + " LIMIT 1" query = QSqlQuery(blank_sql, self.db) if query.first(): hasBlanks = True # Get not blank row count not_blank_sql = sql + dataFilter + " LIMIT 1" query = QSqlQuery(not_blank_sql, self.db) if query.first(): if columnType in Type.ImageTypes: label = self.tr("(Images)") elif columnType == Type.Text: label = self.tr("(Text)") else: label = self.tr("(Data)") item = QListWidgetItem(label, type=FilterMenuButton.DataType) item.setData(Qt.UserRole, label) item.setCheckState(Qt.Checked) if columnFilters and columnFilters.hasData(): item.setCheckState(Qt.Unchecked) self.listWidget.addItem(item) elif columnType == Type.Status: filtersSql = self.filtersToSql(filters.values()) if filtersSql: filtersSql = 'WHERE ' + filtersSql sql = "SELECT DISTINCT %s FROM coins %s ORDER BY %s ASC" % ( self.columnName, filtersSql, self.columnName) query = QSqlQuery(sql, self.db) while query.next(): value = query.record().value(0) label = Statuses[value] item = StatusSortListWidgetItem(label) item.setData(Qt.UserRole, value) if self.settings['show_filter_icons']: icon = statusIcon(value) item.setIcon(icon) if value in appliedValues: if revert: item.setCheckState(Qt.Checked) else: item.setCheckState(Qt.Unchecked) else: if revert: item.setCheckState(Qt.Unchecked) else: item.setCheckState(Qt.Checked) self.listWidget.addItem(item) self.listWidget.sortItems() elif columnType == Type.Denomination: filtersSql = self.filtersToSql(filters.values()) if filtersSql: filtersSql = 'WHERE ' + filtersSql sql = "SELECT DISTINCT %s FROM coins %s" % (self.columnName, filtersSql) query = QSqlQuery(sql, self.db) while query.next(): icon = None if query.record().isNull(0): data = None else: orig_data = query.record().value(0) data = str(orig_data) label, _ = numberWithFraction(data, self.settings['convert_fraction']) if not data: hasBlanks = True continue item = CustomSortListWidgetItem() item.setData(Qt.DisplayRole, label) item.setData(Qt.UserRole, data) item.setData(Qt.UserRole + 1, orig_data) if data in appliedValues: if revert: item.setCheckState(Qt.Checked) else: item.setCheckState(Qt.Unchecked) else: if revert: item.setCheckState(Qt.Unchecked) else: item.setCheckState(Qt.Checked) self.listWidget.addItem(item) self.listWidget.sortItems() else: filtersSql = self.filtersToSql(filters.values()) if filtersSql: filtersSql = 'WHERE ' + filtersSql sql = "SELECT DISTINCT %s FROM coins %s" % (self.columnName, filtersSql) query = QSqlQuery(sql, self.db) while query.next(): icon = None if query.record().isNull(0): data = None else: orig_data = query.record().value(0) data = str(orig_data) if self.settings['show_filter_icons']: icon = self.reference.getIcon(self.columnName, data) if not data: hasBlanks = True continue item = QListWidgetItem() item.setData(Qt.DisplayRole, orig_data) item.setData(Qt.UserRole, data) if icon: item.setIcon(icon) if data in appliedValues: if revert: item.setCheckState(Qt.Checked) else: item.setCheckState(Qt.Unchecked) else: if revert: item.setCheckState(Qt.Unchecked) else: item.setCheckState(Qt.Checked) self.listWidget.addItem(item) self.listWidget.sortItems() item = QListWidgetItem(self.tr("(Select all)"), type=FilterMenuButton.SelectAllType) item.setData(Qt.UserRole, self.tr("(Select all)")) item.setCheckState(Qt.Checked) self.listWidget.insertItem(0, item) if hasBlanks: item = QListWidgetItem(self.tr("(Blanks)"), type=FilterMenuButton.BlanksType) item.setData(Qt.UserRole, self.tr("(Blanks)")) item.setCheckState(Qt.Checked) if revert: if columnFilters and not columnFilters.hasBlank(): item.setCheckState(Qt.Unchecked) else: if columnFilters and columnFilters.hasBlank(): item.setCheckState(Qt.Unchecked) self.listWidget.addItem(item) self.listWidget.itemChanged.connect(self.itemChanged) self.searchBox = QLineEdit(self) self.searchBox.setPlaceholderText(self.tr("Filter")) self.searchBox.textChanged.connect(self.applySearch) self.buttonBox = QDialogButtonBox(Qt.Horizontal) self.buttonBox.addButton(QDialogButtonBox.Ok) self.buttonBox.addButton(QDialogButtonBox.Cancel) self.buttonBox.accepted.connect(self.apply) self.buttonBox.rejected.connect(self.menu().hide) layout = QVBoxLayout() layout.addWidget(self.searchBox) layout.addWidget(self.listWidget) layout.addWidget(self.buttonBox) widget = QWidget(self) widget.setLayout(layout) widgetAction = QWidgetAction(self) widgetAction.setDefaultWidget(widget) self.menu().clear() self.menu().addAction(widgetAction) # Fill items if self.listWidget.count() > 1: self.itemChanged(self.listWidget.item(1))