Skip to content

Commit

Permalink
[processing] add ParameterPoint for selecting point on canvas (fix #5733
Browse files Browse the repository at this point in the history
)
  • Loading branch information
alexbruy committed Feb 29, 2016
1 parent 143f3d6 commit 58d40d1
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 4 deletions.
31 changes: 31 additions & 0 deletions python/plugins/processing/core/parameters.py
Expand Up @@ -215,6 +215,37 @@ def getAsScriptCode(self):
return '##' + self.name + '=extent'


class ParameterPoint(Parameter):

def __init__(self, name='', description='', default=None, optional=False):
Parameter.__init__(self, name, description, default, optional)
# The value is a string in the form "x, y"

def setValue(self, text):
if text is None:
if not self.optional:
return False
self.value = None
return True

tokens = unicode(text).split(',')
if len(tokens) != 2:
return False
try:
float(tokens[0])
float(tokens[1])
self.value = text
return True
except:
return False

def getValueAsCommandLineParameter(self):
return '"' + unicode(self.value) + '"'

def getAsScriptCode(self):
return '##' + self.name + '=point'


class ParameterFile(Parameter):

def __init__(self, name='', description='', isFolder=False, optional=True, ext=None):
Expand Down
13 changes: 10 additions & 3 deletions python/plugins/processing/gui/AlgorithmDialog.py
Expand Up @@ -26,8 +26,14 @@
__revision__ = '$Format:%H$'

from PyQt4.QtCore import Qt
from PyQt4.QtGui import QMessageBox, QApplication, QCursor, QColor, QPalette, QPushButton, QWidget,\
QVBoxLayout
from PyQt4.QtGui import (QMessageBox,
QApplication,
QCursor,
QColor,
QPalette,
QPushButton,
QWidget,
QVBoxLayout)

from qgis.core import *

Expand All @@ -54,6 +60,7 @@
from processing.core.parameters import ParameterNumber
from processing.core.parameters import ParameterFile
from processing.core.parameters import ParameterCrs
from processing.core.parameters import ParameterPoint
from processing.core.parameters import ParameterGeometryPredicate

from processing.core.outputs import OutputRaster
Expand Down Expand Up @@ -152,7 +159,7 @@ def setParamValue(self, param, widget, alg=None):
options = dataobjects.getVectorLayers([param.datatype], sorting=False)
return param.setValue([options[i] for i in widget.selectedoptions])
elif isinstance(param, (ParameterNumber, ParameterFile, ParameterCrs,
ParameterExtent)):
ParameterExtent, ParameterPoint)):
return param.setValue(widget.getValue())
elif isinstance(param, ParameterString):
if param.multiline:
Expand Down
4 changes: 4 additions & 0 deletions python/plugins/processing/gui/BatchPanel.py
Expand Up @@ -38,6 +38,7 @@
from processing.gui.CrsSelectionPanel import CrsSelectionPanel
from processing.gui.ExtentSelectionPanel import ExtentSelectionPanel
from processing.gui.FixedTablePanel import FixedTablePanel
from processing.gui.PointSelectionPanel import PointSelectionPanel
from processing.gui.BatchInputSelectionPanel import BatchInputSelectionPanel
from processing.gui.BatchOutputSelectionPanel import BatchOutputSelectionPanel
from processing.gui.GeometryPredicateSelectionPanel import GeometryPredicateSelectionPanel
Expand All @@ -48,6 +49,7 @@
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterExtent
from processing.core.parameters import ParameterCrs
from processing.core.parameters import ParameterPoint
from processing.core.parameters import ParameterBoolean
from processing.core.parameters import ParameterSelection
from processing.core.parameters import ParameterFixedTable
Expand Down Expand Up @@ -155,6 +157,8 @@ def getWidgetFromParameter(self, param, row, col):
item = FixedTablePanel(param)
elif isinstance(param, ParameterExtent):
item = ExtentSelectionPanel(self.parent, self.alg, param.default)
elif isinstance(param, ParameterPoint):
item = PointSelectionPanel(self.parent, param.default)
elif isinstance(param, ParameterCrs):
item = CrsSelectionPanel(param.default)
elif isinstance(param, ParameterFile):
Expand Down
18 changes: 17 additions & 1 deletion python/plugins/processing/gui/ParametersPanel.py
Expand Up @@ -36,7 +36,17 @@

from PyQt4 import uic
from PyQt4.QtCore import QCoreApplication, QVariant
from PyQt4.QtGui import QWidget, QLayout, QVBoxLayout, QHBoxLayout, QToolButton, QIcon, QLabel, QCheckBox, QComboBox, QLineEdit, QPlainTextEdit
from PyQt4.QtGui import (QWidget,
QLayout,
QVBoxLayout,
QHBoxLayout,
QToolButton,
QIcon,
QLabel,
QCheckBox,
QComboBox,
QLineEdit,
QPlainTextEdit)

from processing.core.ProcessingConfig import ProcessingConfig

Expand All @@ -49,6 +59,7 @@
from processing.gui.ExtentSelectionPanel import ExtentSelectionPanel
from processing.gui.FileSelectionPanel import FileSelectionPanel
from processing.gui.CrsSelectionPanel import CrsSelectionPanel
from processing.gui.PointSelectionPanel import PointSelectionPanel
from processing.gui.GeometryPredicateSelectionPanel import \
GeometryPredicateSelectionPanel

Expand All @@ -66,6 +77,7 @@
from processing.core.parameters import ParameterFile
from processing.core.parameters import ParameterCrs
from processing.core.parameters import ParameterString
from processing.core.parameters import ParameterPoint
from processing.core.parameters import ParameterGeometryPredicate

from processing.core.outputs import OutputRaster
Expand Down Expand Up @@ -184,6 +196,8 @@ def initWidgets(self):
desc = param.description
if isinstance(param, ParameterExtent):
desc += self.tr(' (xmin, xmax, ymin, ymax)')
if isinstance(param, ParameterPoint):
desc += self.tr(' (x, y)')
try:
if param.optional:
desc += self.tr(' [optional]')
Expand Down Expand Up @@ -376,6 +390,8 @@ def getWidgetFromParameter(self, param):
param.isInteger)
elif isinstance(param, ParameterExtent):
item = ExtentSelectionPanel(self.parent, self.alg, param.default)
elif isinstance(param, ParameterPoint):
item = PointSelectionPanel(self.parent, param.default)
elif isinstance(param, ParameterCrs):
item = CrsSelectionPanel(param.default)
elif isinstance(param, ParameterString):
Expand Down
46 changes: 46 additions & 0 deletions python/plugins/processing/gui/PointMapTool.py
@@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
PointMapTool.py
---------------------
Date : February 2016
Copyright : (C) 2016 by Alexander Bruy
Email : alexander dot bruy at gmail dot com
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""

__author__ = 'Alexander Bruy'
__date__ = 'February 2016'
__copyright__ = '(C) 2016, Alexander Bruy'

# This will get replaced with a git SHA1 when you do a git archive

__revision__ = '$Format:%H$'

from PyQt4.QtCore import Qt

from qgis.gui import QgsMapToolEmitPoint


class PointMapTool(QgsMapToolEmitPoint):

def __init__(self, canvas):
QgsMapToolEmitPoint.__init__(self, canvas)

self.canvas = canvas
self.cursor = Qt.ArrowCursor

def activate(self):
self.canvas.setCursor(self.cursor)

def canvasPressEvent(self, event):
pnt = self.toMapCoordinates(event.pos())
self.canvasClicked.emit(pnt, event.button())
91 changes: 91 additions & 0 deletions python/plugins/processing/gui/PointSelectionPanel.py
@@ -0,0 +1,91 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
PointSelectionPanel.py
---------------------
Date : February 2016
Copyright : (C) 2016 by Alexander Bruy
Email : alexander dot bruy at gmail dot com
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""

__author__ = 'Alexander Bruy'
__date__ = 'February 2016'
__copyright__ = '(C) 2016, Alexander Bruy'

# This will get replaced with a git SHA1 when you do a git archive

__revision__ = '$Format:%H$'

import os

from PyQt4 import uic
from PyQt4.QtGui import QCursor

from qgis.gui import QgsMessageBar
from qgis.utils import iface

from processing.gui.PointMapTool import PointMapTool

pluginPath = os.path.split(os.path.dirname(__file__))[0]
WIDGET, BASE = uic.loadUiType(
os.path.join(pluginPath, 'ui', 'widgetBaseSelector.ui'))


class PointSelectionPanel(BASE, WIDGET):

def __init__(self, dialog, default=None):
super(PointSelectionPanel, self).__init__(None)
self.setupUi(self)

self.btnSelect.clicked.connect(self.selectOnCanvas)

self.dialog = dialog

canvas = iface.mapCanvas()
self.prevMapTool = canvas.mapTool()

self.tool = PointMapTool(canvas)
self.tool.canvasClicked.connect(self.updatePoint)

if default:
tokens = unicode(default).split(',')
if len(tokens) == 2:
try:
float(tokens[0])
float(tokens[1])
self.leText.setText(unicode(default))
except:
pass

def selectOnCanvas(self):
canvas = iface.mapCanvas()
canvas.setMapTool(self.tool)
self.dialog.showMinimized()

def updatePoint(self, point, button):
s = '{},{}'.format(point.x(), point.y())

self.leText.setText(s)
canvas = iface.mapCanvas()
canvas.setMapTool(self.prevMapTool)
self.dialog.showNormal()
self.dialog.raise_()
self.dialog.activateWindow()

def getValue(self):
if unicode(self.leText.text()).strip() != '':
return unicode(self.leText.text())
else:
return None

def setPointFromString(self, s):
self.leText.setText(s)

0 comments on commit 58d40d1

Please sign in to comment.