Skip to content

Commit 58d40d1

Browse files
committedFeb 29, 2016
[processing] add ParameterPoint for selecting point on canvas (fix #5733)
1 parent 143f3d6 commit 58d40d1

File tree

6 files changed

+199
-4
lines changed

6 files changed

+199
-4
lines changed
 

‎python/plugins/processing/core/parameters.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,37 @@ def getAsScriptCode(self):
215215
return '##' + self.name + '=extent'
216216

217217

218+
class ParameterPoint(Parameter):
219+
220+
def __init__(self, name='', description='', default=None, optional=False):
221+
Parameter.__init__(self, name, description, default, optional)
222+
# The value is a string in the form "x, y"
223+
224+
def setValue(self, text):
225+
if text is None:
226+
if not self.optional:
227+
return False
228+
self.value = None
229+
return True
230+
231+
tokens = unicode(text).split(',')
232+
if len(tokens) != 2:
233+
return False
234+
try:
235+
float(tokens[0])
236+
float(tokens[1])
237+
self.value = text
238+
return True
239+
except:
240+
return False
241+
242+
def getValueAsCommandLineParameter(self):
243+
return '"' + unicode(self.value) + '"'
244+
245+
def getAsScriptCode(self):
246+
return '##' + self.name + '=point'
247+
248+
218249
class ParameterFile(Parameter):
219250

220251
def __init__(self, name='', description='', isFolder=False, optional=True, ext=None):

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,14 @@
2626
__revision__ = '$Format:%H$'
2727

2828
from PyQt4.QtCore import Qt
29-
from PyQt4.QtGui import QMessageBox, QApplication, QCursor, QColor, QPalette, QPushButton, QWidget,\
30-
QVBoxLayout
29+
from PyQt4.QtGui import (QMessageBox,
30+
QApplication,
31+
QCursor,
32+
QColor,
33+
QPalette,
34+
QPushButton,
35+
QWidget,
36+
QVBoxLayout)
3137

3238
from qgis.core import *
3339

@@ -54,6 +60,7 @@
5460
from processing.core.parameters import ParameterNumber
5561
from processing.core.parameters import ParameterFile
5662
from processing.core.parameters import ParameterCrs
63+
from processing.core.parameters import ParameterPoint
5764
from processing.core.parameters import ParameterGeometryPredicate
5865

5966
from processing.core.outputs import OutputRaster
@@ -152,7 +159,7 @@ def setParamValue(self, param, widget, alg=None):
152159
options = dataobjects.getVectorLayers([param.datatype], sorting=False)
153160
return param.setValue([options[i] for i in widget.selectedoptions])
154161
elif isinstance(param, (ParameterNumber, ParameterFile, ParameterCrs,
155-
ParameterExtent)):
162+
ParameterExtent, ParameterPoint)):
156163
return param.setValue(widget.getValue())
157164
elif isinstance(param, ParameterString):
158165
if param.multiline:

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from processing.gui.CrsSelectionPanel import CrsSelectionPanel
3939
from processing.gui.ExtentSelectionPanel import ExtentSelectionPanel
4040
from processing.gui.FixedTablePanel import FixedTablePanel
41+
from processing.gui.PointSelectionPanel import PointSelectionPanel
4142
from processing.gui.BatchInputSelectionPanel import BatchInputSelectionPanel
4243
from processing.gui.BatchOutputSelectionPanel import BatchOutputSelectionPanel
4344
from processing.gui.GeometryPredicateSelectionPanel import GeometryPredicateSelectionPanel
@@ -48,6 +49,7 @@
4849
from processing.core.parameters import ParameterVector
4950
from processing.core.parameters import ParameterExtent
5051
from processing.core.parameters import ParameterCrs
52+
from processing.core.parameters import ParameterPoint
5153
from processing.core.parameters import ParameterBoolean
5254
from processing.core.parameters import ParameterSelection
5355
from processing.core.parameters import ParameterFixedTable
@@ -155,6 +157,8 @@ def getWidgetFromParameter(self, param, row, col):
155157
item = FixedTablePanel(param)
156158
elif isinstance(param, ParameterExtent):
157159
item = ExtentSelectionPanel(self.parent, self.alg, param.default)
160+
elif isinstance(param, ParameterPoint):
161+
item = PointSelectionPanel(self.parent, param.default)
158162
elif isinstance(param, ParameterCrs):
159163
item = CrsSelectionPanel(param.default)
160164
elif isinstance(param, ParameterFile):

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,17 @@
3636

3737
from PyQt4 import uic
3838
from PyQt4.QtCore import QCoreApplication, QVariant
39-
from PyQt4.QtGui import QWidget, QLayout, QVBoxLayout, QHBoxLayout, QToolButton, QIcon, QLabel, QCheckBox, QComboBox, QLineEdit, QPlainTextEdit
39+
from PyQt4.QtGui import (QWidget,
40+
QLayout,
41+
QVBoxLayout,
42+
QHBoxLayout,
43+
QToolButton,
44+
QIcon,
45+
QLabel,
46+
QCheckBox,
47+
QComboBox,
48+
QLineEdit,
49+
QPlainTextEdit)
4050

4151
from processing.core.ProcessingConfig import ProcessingConfig
4252

@@ -49,6 +59,7 @@
4959
from processing.gui.ExtentSelectionPanel import ExtentSelectionPanel
5060
from processing.gui.FileSelectionPanel import FileSelectionPanel
5161
from processing.gui.CrsSelectionPanel import CrsSelectionPanel
62+
from processing.gui.PointSelectionPanel import PointSelectionPanel
5263
from processing.gui.GeometryPredicateSelectionPanel import \
5364
GeometryPredicateSelectionPanel
5465

@@ -66,6 +77,7 @@
6677
from processing.core.parameters import ParameterFile
6778
from processing.core.parameters import ParameterCrs
6879
from processing.core.parameters import ParameterString
80+
from processing.core.parameters import ParameterPoint
6981
from processing.core.parameters import ParameterGeometryPredicate
7082

7183
from processing.core.outputs import OutputRaster
@@ -184,6 +196,8 @@ def initWidgets(self):
184196
desc = param.description
185197
if isinstance(param, ParameterExtent):
186198
desc += self.tr(' (xmin, xmax, ymin, ymax)')
199+
if isinstance(param, ParameterPoint):
200+
desc += self.tr(' (x, y)')
187201
try:
188202
if param.optional:
189203
desc += self.tr(' [optional]')
@@ -376,6 +390,8 @@ def getWidgetFromParameter(self, param):
376390
param.isInteger)
377391
elif isinstance(param, ParameterExtent):
378392
item = ExtentSelectionPanel(self.parent, self.alg, param.default)
393+
elif isinstance(param, ParameterPoint):
394+
item = PointSelectionPanel(self.parent, param.default)
379395
elif isinstance(param, ParameterCrs):
380396
item = CrsSelectionPanel(param.default)
381397
elif isinstance(param, ParameterString):
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
PointMapTool.py
6+
---------------------
7+
Date : February 2016
8+
Copyright : (C) 2016 by Alexander Bruy
9+
Email : alexander dot bruy at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Alexander Bruy'
21+
__date__ = 'February 2016'
22+
__copyright__ = '(C) 2016, Alexander Bruy'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
from PyQt4.QtCore import Qt
29+
30+
from qgis.gui import QgsMapToolEmitPoint
31+
32+
33+
class PointMapTool(QgsMapToolEmitPoint):
34+
35+
def __init__(self, canvas):
36+
QgsMapToolEmitPoint.__init__(self, canvas)
37+
38+
self.canvas = canvas
39+
self.cursor = Qt.ArrowCursor
40+
41+
def activate(self):
42+
self.canvas.setCursor(self.cursor)
43+
44+
def canvasPressEvent(self, event):
45+
pnt = self.toMapCoordinates(event.pos())
46+
self.canvasClicked.emit(pnt, event.button())
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
PointSelectionPanel.py
6+
---------------------
7+
Date : February 2016
8+
Copyright : (C) 2016 by Alexander Bruy
9+
Email : alexander dot bruy at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Alexander Bruy'
21+
__date__ = 'February 2016'
22+
__copyright__ = '(C) 2016, Alexander Bruy'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
import os
29+
30+
from PyQt4 import uic
31+
from PyQt4.QtGui import QCursor
32+
33+
from qgis.gui import QgsMessageBar
34+
from qgis.utils import iface
35+
36+
from processing.gui.PointMapTool import PointMapTool
37+
38+
pluginPath = os.path.split(os.path.dirname(__file__))[0]
39+
WIDGET, BASE = uic.loadUiType(
40+
os.path.join(pluginPath, 'ui', 'widgetBaseSelector.ui'))
41+
42+
43+
class PointSelectionPanel(BASE, WIDGET):
44+
45+
def __init__(self, dialog, default=None):
46+
super(PointSelectionPanel, self).__init__(None)
47+
self.setupUi(self)
48+
49+
self.btnSelect.clicked.connect(self.selectOnCanvas)
50+
51+
self.dialog = dialog
52+
53+
canvas = iface.mapCanvas()
54+
self.prevMapTool = canvas.mapTool()
55+
56+
self.tool = PointMapTool(canvas)
57+
self.tool.canvasClicked.connect(self.updatePoint)
58+
59+
if default:
60+
tokens = unicode(default).split(',')
61+
if len(tokens) == 2:
62+
try:
63+
float(tokens[0])
64+
float(tokens[1])
65+
self.leText.setText(unicode(default))
66+
except:
67+
pass
68+
69+
def selectOnCanvas(self):
70+
canvas = iface.mapCanvas()
71+
canvas.setMapTool(self.tool)
72+
self.dialog.showMinimized()
73+
74+
def updatePoint(self, point, button):
75+
s = '{},{}'.format(point.x(), point.y())
76+
77+
self.leText.setText(s)
78+
canvas = iface.mapCanvas()
79+
canvas.setMapTool(self.prevMapTool)
80+
self.dialog.showNormal()
81+
self.dialog.raise_()
82+
self.dialog.activateWindow()
83+
84+
def getValue(self):
85+
if unicode(self.leText.text()).strip() != '':
86+
return unicode(self.leText.text())
87+
else:
88+
return None
89+
90+
def setPointFromString(self, s):
91+
self.leText.setText(s)

0 commit comments

Comments
 (0)
Please sign in to comment.