PointSelectionPanel.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. """
  2. ***************************************************************************
  3. PointSelectionPanel.py
  4. ---------------------
  5. Date : February 2016
  6. Copyright : (C) 2016 by Alexander Bruy
  7. Email : alexander dot bruy at gmail dot com
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Alexander Bruy'
  18. __date__ = 'February 2016'
  19. __copyright__ = '(C) 2016, Alexander Bruy'
  20. import os
  21. import warnings
  22. from qgis.core import (QgsProject,
  23. QgsReferencedPointXY,
  24. QgsPointXY)
  25. from qgis.PyQt import uic
  26. from qgis.utils import iface
  27. from processing.gui.PointMapTool import PointMapTool
  28. pluginPath = os.path.split(os.path.dirname(__file__))[0]
  29. with warnings.catch_warnings():
  30. warnings.filterwarnings("ignore", category=DeprecationWarning)
  31. WIDGET, BASE = uic.loadUiType(
  32. os.path.join(pluginPath, 'ui', 'widgetBaseSelector.ui'))
  33. class PointSelectionPanel(BASE, WIDGET):
  34. def __init__(self, dialog, default=None):
  35. super().__init__(None)
  36. self.setupUi(self)
  37. self.btnSelect.clicked.connect(self.selectOnCanvas)
  38. self.dialog = dialog
  39. self.crs = QgsProject.instance().crs()
  40. if iface is not None:
  41. canvas = iface.mapCanvas()
  42. self.prevMapTool = canvas.mapTool()
  43. self.tool = PointMapTool(canvas)
  44. self.tool.canvasClicked.connect(self.updatePoint)
  45. self.tool.complete.connect(self.pointPicked)
  46. else:
  47. self.prevMapTool = None
  48. self.tool = None
  49. if default:
  50. tokens = str(default).split(',')
  51. if len(tokens) == 2:
  52. try:
  53. float(tokens[0])
  54. float(tokens[1])
  55. self.leText.setText(str(default))
  56. except:
  57. pass
  58. def selectOnCanvas(self):
  59. canvas = iface.mapCanvas()
  60. canvas.setMapTool(self.tool)
  61. self.dialog.showMinimized()
  62. def updatePoint(self, point, button):
  63. s = f'{point.x()},{point.y()}'
  64. self.crs = QgsProject.instance().crs()
  65. if self.crs.isValid():
  66. s += ' [' + self.crs.authid() + ']'
  67. self.leText.setText(s)
  68. def pointPicked(self):
  69. canvas = iface.mapCanvas()
  70. canvas.setMapTool(self.prevMapTool)
  71. self.dialog.showNormal()
  72. self.dialog.raise_()
  73. self.dialog.activateWindow()
  74. def getValue(self):
  75. if str(self.leText.text()).strip() != '':
  76. return str(self.leText.text())
  77. else:
  78. return None
  79. def setPointFromString(self, s):
  80. self.leText.setText(s)