diff --git a/src/configmanager/editorwidgets/listwidget.py b/src/configmanager/editorwidgets/listwidget.py index ac4dacee9..b5ff64607 100644 --- a/src/configmanager/editorwidgets/listwidget.py +++ b/src/configmanager/editorwidgets/listwidget.py @@ -1,5 +1,4 @@ from functools import partial - from qgis.PyQt.QtCore import Qt from qgis.PyQt.QtWidgets import QWidget from qgis.core import QgsMapLayer @@ -35,6 +34,7 @@ def __init__(self, parent=None): self.layerCombo.setModel(self.layermodel) self.keyCombo.setModel(self.fieldmodel) self.valueCombo.setModel(self.fieldmodel) + self.sortCombo.setModel(self.fieldmodel) self.filterButton.pressed.connect(self.define_filter) self.fieldmodel.setLayerFilter(self.layerCombo.view().selectionModel()) @@ -61,6 +61,7 @@ def reset(self): self.layerCombo.setCurrentIndex(-1) self.keyCombo.setCurrentIndex(-1) self.valueCombo.setCurrentIndex(-1) + self.sortCombo.setCurrentIndex(-1) def widgetchanged(self): self.widgetdirty.emit(self.getconfig()) @@ -89,6 +90,18 @@ def list(self): def filter(self): return self.filterText.toPlainText() + @property + def sortAsNumber(self): + return self.sortByAsNumberCheck.isChecked() + + @property + def sort_by(self): + if self.sortByValue.isChecked(): + index_key = self.fieldmodel.index(self.sortCombo.currentIndex(), 0) + fieldname_key = self.fieldmodel.data(index_key, QgsFieldModel.FieldNameRole) + return fieldname_key + return "default" + @property def layer(self): return self.layerCombo.currentText() @@ -115,6 +128,8 @@ def getconfig(self): subconfig['key'] = self.key subconfig['value'] = self.value subconfig['filter'] = self.filter + subconfig['sort_by'] = self.sort_by + subconfig['sort_by_as_number'] = self.sortAsNumber config['layer'] = subconfig else: config['list'] = {} @@ -136,6 +151,7 @@ def setconfig(self, config): self.listText.setPlainText('') self.keyCombo.clear() self.valueCombo.clear() + self.sortCombo.clear() self.filterText.clear() self.layermodel.refresh() @@ -153,6 +169,8 @@ def setconfig(self, config): subconfig = config.get('layer', {}) layer = subconfig.get('layer', '') or '' key = subconfig.get('key', '') or '' + sortBy = subconfig.get('sort_by', 'default') or 'default' + sortByAsNumber = subconfig.get('sort_by_as_number', False) or False value = subconfig.get('value', '') or '' filter = subconfig.get('filter', None) index = self.layerCombo.findData(layer, Qt.DisplayRole) @@ -170,6 +188,16 @@ def setconfig(self, config): if valueindex > -1: self.valueCombo.setCurrentIndex(valueindex) + self.sortByAsNumberCheck.setChecked(sortByAsNumber) + + if sortBy == "default": + self.sortDefault.setChecked(True) + else: + self.sortByValue.setChecked(True) + sortIndex = self.sortCombo.findData(sortBy.lower(), QgsFieldModel.FieldNameRole) + if sortIndex > -1: + self.sortCombo.setCurrentIndex(sortIndex) + self.filterText.setPlainText(filter) self.allownullCheck.setChecked(self.allownull) diff --git a/src/configmanager/editorwidgets/uifiles/ui_listwidget_config.ui b/src/configmanager/editorwidgets/uifiles/ui_listwidget_config.ui index 6ba653fea..7cb9f597c 100644 --- a/src/configmanager/editorwidgets/uifiles/ui_listwidget_config.ui +++ b/src/configmanager/editorwidgets/uifiles/ui_listwidget_config.ui @@ -6,17 +6,67 @@ 0 0 - 439 - 382 + 471 + 424 Form - + 0 + + 0 + + + 0 + + + 0 + + + + + + + From layer + + + true + + + + + + + From pre-defined list + + + + + + + + + true + + + Allow Null Value + + + + + + + false + + + Order By Value + + + @@ -78,14 +128,52 @@ - + + + + 0 + + + + + Sort by data column + + + + + + + Sort By column + + + + + + + QComboBox::AdjustToContents + + + 0 + + + + + + + As Number + + + + + + Filter - + @@ -106,7 +194,7 @@ ... - + :/icons/Expression:/icons/Expression @@ -122,7 +210,7 @@ - + @@ -153,47 +241,6 @@ - - - - - - From layer - - - true - - - - - - - From pre-defined list - - - - - - - - - true - - - Allow Null Value - - - - - - - false - - - Order By Value - - - diff --git a/src/roam/editorwidgets/listwidget.py b/src/roam/editorwidgets/listwidget.py index cc3e2b4d8..2cde35d3f 100644 --- a/src/roam/editorwidgets/listwidget.py +++ b/src/roam/editorwidgets/listwidget.py @@ -2,7 +2,6 @@ from qgis.PyQt.QtCore import QSize, Qt, QEvent from qgis.PyQt.QtGui import QStandardItem, QStandardItemModel, QIcon from qgis.PyQt.QtWidgets import QComboBox -from qgis.core import QgsProject, QgsExpression, QgsFeatureRequest import roam.utils from roam.api import RoamEvents, utils @@ -154,8 +153,24 @@ def _buildfromlayer(self, widget, layerconfig): return features = roam.api.utils.search_layer(layer, filterexp, fields, with_geometry=False) - # Sort the fields based on value field - features = sorted(features, key=lambda f: f[valuefield]) + # Sort the fields based on config + sortBy = layerconfig.get('sort_by', 'default') + sortAsNumber = layerconfig.get('sort_by_as_number', False) + if sortBy == "default": + sortBy = valuefield + + def custom_sort(feature): + value = feature[sortBy] + if sortAsNumber: + try: + value = float(str(value)) + return False, value + except ValueError: + return True, + return False, value + + features = sorted(features, key=custom_sort) + for feature in features: keyvalue = nullconvert(feature[keyfieldindex]) valuvalue = nullconvert(feature[valuefield])