Skip to content

Commit dbf6107

Browse files
committedNov 13, 2016
[processing] Add button for projection selection dialog to
Crs parameters in algorithm settings in modeler This makes it easier to pick static CRSes for the parameter, and also makes it obvious to users that they can use a fixed CRS parameter in their model (as opposed to one taken from a layer or input)
1 parent f24cda4 commit dbf6107

File tree

1 file changed

+47
-21
lines changed

1 file changed

+47
-21
lines changed
 

‎python/plugins/processing/gui/wrappers.py

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
import os
3535
from functools import cmp_to_key
3636

37-
from qgis.core import QgsCoordinateReferenceSystem, QgsVectorLayer
38-
from qgis.PyQt.QtWidgets import QCheckBox, QComboBox, QLineEdit, QPlainTextEdit
39-
from qgis.gui import QgsFieldExpressionWidget, QgsExpressionLineEdit, QgsProjectionSelectionWidget
37+
from qgis.core import QgsCoordinateReferenceSystem, QgsVectorLayer, QgsApplication
38+
from qgis.PyQt.QtWidgets import QCheckBox, QComboBox, QLineEdit, QPlainTextEdit, QWidget, QHBoxLayout, QToolButton
39+
from qgis.gui import QgsFieldExpressionWidget, QgsExpressionLineEdit, QgsProjectionSelectionWidget, QgsGenericProjectionSelector
4040
from qgis.PyQt.QtCore import pyqtSignal, QObject, QVariant
4141

4242
from processing.gui.NumberInputPanel import NumberInputPanel
@@ -107,36 +107,40 @@ def __init__(self, param, dialog, row=0, col=0):
107107
if param.default is not None:
108108
self.setValue(param.default)
109109

110-
def comboValue(self, validator=None):
111-
idx = self.widget.findText(self.widget.currentText())
110+
def comboValue(self, validator=None, combobox=None):
111+
if not combobox:
112+
combobox = self.widget
113+
idx = combobox.findText(combobox.currentText())
112114
if idx < 0:
113-
v = self.widget.currentText().strip()
115+
v = combobox.currentText().strip()
114116
if validator is not None and not validator(v):
115117
raise InvalidParameterValue()
116118
return v
117-
return self.widget.itemData(self.widget.currentIndex())
119+
return combobox.itemData(combobox.currentIndex())
118120

119121
def createWidget(self):
120122
pass
121123

122124
def setValue(self, value):
123125
pass
124126

125-
def setComboValue(self, value):
127+
def setComboValue(self, value, combobox=None):
128+
if not combobox:
129+
combobox = self.widget
126130
if isinstance(value, list):
127131
value = value[0]
128-
values = [self.widget.itemData(i) for i in range(self.widget.count())]
132+
values = [combobox.itemData(i) for i in range(combobox.count())]
129133
try:
130134
idx = values.index(value)
131-
self.widget.setCurrentIndex(idx)
135+
combobox.setCurrentIndex(idx)
132136
return
133137
except ValueError:
134138
pass
135-
if self.widget.isEditable():
139+
if combobox.isEditable():
136140
if value is not None:
137-
self.widget.setEditText(str(value))
141+
combobox.setEditText(str(value))
138142
else:
139-
self.widget.setCurrentIndex(0)
143+
combobox.setCurrentIndex(0)
140144

141145
def value(self):
142146
pass
@@ -206,19 +210,32 @@ class CrsWidgetWrapper(WidgetWrapper):
206210

207211
def createWidget(self):
208212
if self.dialogType == DIALOG_MODELER:
209-
widget = QComboBox()
210-
widget.setEditable(True)
213+
self.combo = QComboBox()
214+
widget = QWidget()
215+
layout = QHBoxLayout()
216+
layout.setMargin(0)
217+
layout.setContentsMargins(0, 0, 0, 0)
218+
layout.setSpacing(1)
219+
layout.addWidget(self.combo)
220+
btn = QToolButton()
221+
btn.setIcon(QgsApplication.getThemeIcon("mActionSetProjection.svg"))
222+
btn.setToolTip(self.tr("Select CRS"))
223+
btn.clicked.connect(self.selectProjection)
224+
layout.addWidget(btn)
225+
226+
widget.setLayout(layout)
227+
self.combo.setEditable(True)
211228
crss = self.dialog.getAvailableValuesOfType(ParameterCrs)
212229
for crs in crss:
213-
widget.addItem(self.dialog.resolveValueDescription(crs), crs)
230+
self.combo.addItem(self.dialog.resolveValueDescription(crs), crs)
214231
raster = self.dialog.getAvailableValuesOfType(ParameterRaster, OutputRaster)
215232
vector = self.dialog.getAvailableValuesOfType(ParameterVector, OutputVector)
216233
for r in raster:
217-
widget.addItem("Crs of layer " + self.dialog.resolveValueDescription(r), r)
234+
self.combo.addItem("Crs of layer " + self.dialog.resolveValueDescription(r), r)
218235
for v in vector:
219-
widget.addItem("Crs of layer " + self.dialog.resolveValueDescription(v), v)
236+
self.combo.addItem("Crs of layer " + self.dialog.resolveValueDescription(v), v)
220237
if not self.param.default:
221-
widget.setEditText(self.param.default)
238+
self.combo.setEditText(self.param.default)
222239
return widget
223240
else:
224241

@@ -232,15 +249,24 @@ def createWidget(self):
232249

233250
return widget
234251

252+
def selectProjection(self):
253+
dialog = QgsGenericProjectionSelector(self.widget)
254+
current_crs = QgsCoordinateReferenceSystem(self.combo.currentText())
255+
if current_crs.isValid():
256+
dialog.setSelectedCrsId(current_crs.srsid())
257+
258+
if dialog.exec_():
259+
self.setValue(dialog.selectedAuthId())
260+
235261
def setValue(self, value):
236262
if self.dialogType == DIALOG_MODELER:
237-
self.setComboValue(value)
263+
self.setComboValue(value, self.combo)
238264
else:
239265
self.widget.setCrs(QgsCoordinateReferenceSystem(value))
240266

241267
def value(self):
242268
if self.dialogType == DIALOG_MODELER:
243-
return self.comboValue()
269+
return self.comboValue(combobox=self.combo)
244270
else:
245271
crs = self.widget.crs()
246272
if crs.isValid():

0 commit comments

Comments
 (0)
Please sign in to comment.