-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalg_with_custom_checkable_list.py
200 lines (160 loc) · 8.11 KB
/
alg_with_custom_checkable_list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"""
/***************************************************************************
A QGIS processing algorithm with a custom checkable list widget wrapper
begin : August 2023
copyright : (C) 2023 by Ben Wirf
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
from processing.gui.wrappers import WidgetWrapper
from qgis.PyQt.QtWidgets import (QWidget, QListWidget, QListWidgetItem,
QLabel, QLineEdit, QGridLayout, QMenu,
QAction)
from qgis.PyQt.QtCore import (QCoreApplication, QVariant, Qt, QEvent)
from qgis.PyQt.QtGui import QCursor
from qgis.core import (QgsProcessingAlgorithm, QgsProcessingParameterMatrix,
QgsMapLayerProxyModel)
from qgis.gui import (QgsMapLayerComboBox, QgsFieldComboBox)
import re
class CustomListWidget(QgsProcessingAlgorithm):
INPUT_PARAMS = 'INPUT_PARAMS'
def __init__(self):
super().__init__()
def name(self):
return "widgetwrapperexample"
def tr(self, text):
return QCoreApplication.translate("widgetwrapperexample", text)
def displayName(self):
return self.tr("Widget wrapper example")
def group(self):
return self.tr("Examples")
def groupId(self):
return "examples"
def shortHelpString(self):
return self.tr("Example script with custom widgets")
def helpUrl(self):
return "https://qgis.org"
def createInstance(self):
return type(self)()
def initAlgorithm(self, config=None):
param = QgsProcessingParameterMatrix(self.INPUT_PARAMS, 'Input parameters')
param.setMetadata({'widget_wrapper': {'class': CheckableListWidget}})
self.addParameter(param)
def processAlgorithm(self, parameters, context, feedback):
user_options = self.parameterAsMatrix(parameters, self.INPUT_PARAMS, context)
input_vector_layer = user_options[0] # QgsVectorLayer object
# Do something with input_vector_layer...
weight_field = user_options[1] # Name (string) of selected field
# Do something with weight_field...
field_values = user_options[2]# list of selected values
# if field_values:
# Do something with field_values...
# Just return the input parameter values for demo purposes
return {'INPUT_LAYER': input_vector_layer,
'INPUT_FIELD': weight_field,
'VALUE_LIST': field_values}
class CheckableListWidget(WidgetWrapper):
def createWidget(self):
self.custom_widget = customParametersWidget()
return self.custom_widget
def value(self):
source_layer = self.custom_widget.get_layer()
source_field = self.custom_widget.get_field()
checked_values = self.custom_widget.get_values()
# cast int/float items back to numeric values if possible
options = [int(v) if v.isdigit() else float(v) if re.match(r'^-?\d+(?:\.\d+)$', v) else str(v) for v in checked_values]
params = [source_layer, source_field, options]
return params
class customParametersWidget(QWidget):
def __init__(self):
super(customParametersWidget, self).__init__()
self.lyr_lbl = QLabel('Input vector layer:')
self.lyr_cb = QgsMapLayerComboBox(self)
self.lyr_cb.setFilters(QgsMapLayerProxyModel.VectorLayer)
self.lyr_cb.layerChanged.connect(self.layer_changed)
self.fld_lbl = QLabel('Select field:')
self.fld_cb = QgsFieldComboBox(self)
self.fld_cb.setLayer(self.lyr_cb.currentLayer())
self.fld_cb.fieldChanged.connect(self.populate_list_widget)
self.val_lbl = QLabel('Select values:')
self.filter_le = QLineEdit(self)
self.filter_le.setPlaceholderText('Type to filter...')
self.filter_le.textChanged.connect(self.filter_items)
self.value_list = QListWidget(self)
self.layout = QGridLayout()
self.layout.addWidget(self.lyr_lbl, 0, 0, 1, 1)
self.layout.addWidget(self.lyr_cb, 0, 1, 1, 3)
self.layout.addWidget(self.fld_lbl, 1, 0, 1, 1)
self.layout.addWidget(self.fld_cb, 1, 1, 1, 3)
self.layout.addWidget(self.val_lbl, 2, 0, 1, 1)
self.layout.addWidget(self.filter_le, 3, 0, 1, 4)
self.layout.addWidget(self.value_list, 4, 0, 1, 4)
self.setLayout(self.layout)
self.populate_list_widget()
self.context_menu = QMenu(self)
self.action_check_all = QAction('Select All', self)
self.action_check_all.triggered.connect(self.select_all)
self.action_uncheck_all = QAction('De-select All', self)
self.action_uncheck_all.triggered.connect(self.deselect_all)
self.context_menu.addAction(self.action_check_all)
self.context_menu.addAction(self.action_uncheck_all)
self.value_list.viewport().installEventFilter(self)
def layer_changed(self, lyr):
self.fld_cb.setLayer(lyr)
self.populate_list_widget()
def eventFilter(self, obj, event):
if event.type() == QEvent.MouseButtonPress and obj == self.value_list.viewport():
if event.button() == Qt.LeftButton:
clicked_item = self.value_list.itemAt(event.pos())
if clicked_item.checkState() == Qt.Checked:
clicked_item.setCheckState(Qt.Unchecked)
else:
clicked_item.setCheckState(Qt.Checked)
elif event.button() == Qt.RightButton:
self.context_menu.exec(QCursor.pos())
return True
return False
def select_all(self):
for i in range(self.value_list.count()):
item = self.value_list.item(i)
if not item.isHidden():
item.setCheckState(Qt.Checked)
def deselect_all(self):
for i in range(self.value_list.count()):
item = self.value_list.item(i)
if not item.isHidden():
item.setCheckState(Qt.Unchecked)
def filter_items(self, filter_txt):
for i in range(self.value_list.count()):
item = self.value_list.item(i)
filter = filter_txt.lower() not in item.text().lower()
self.value_list.setRowHidden(i, filter)
def populate_list_widget(self):
self.value_list.clear()
lyr = self.lyr_cb.currentLayer()
if not lyr:
return
fld_name = self.fld_cb.currentField()
fld_idx = lyr.fields().lookupField(fld_name)
list_items = lyr.uniqueValues(fld_idx)
if list_items:
for i in list_items:
li = QListWidgetItem(str(i))
li.setFlags(li.flags() | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
li.setCheckState(Qt.Unchecked)
self.value_list.addItem(li)
self.filter_le.clear()
def get_layer(self):
return self.lyr_cb.currentLayer()
def get_field(self):
return self.fld_cb.currentField()
def get_values(self):
return [self.value_list.item(i).data(0) for i in range(self.value_list.count()) if self.value_list.item(i).checkState() == Qt.Checked]