template_tools.txt 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. from PyQt5 import QtCore
  2. from PyQt5.QtCore import *
  3. from PyQt5.QtGui import *
  4. from PyQt5.QtWidgets import * # only used widgets can be listed here
  5. from qgis.core import *
  6. from qgis._gui import *
  7. from qgis.utils import iface
  8. # variables
  9. screen = QApplication.primaryScreen()
  10. size = screen.size()
  11. w, h = size.width(), size.height() # used in placing widget
  12. txt_selection = "Selected layer: {}\nSelected field: {}\nSelected values: {}"
  13. txt_no_selection = "No selection in layer {}"
  14. class PointTool(QgsMapToolEmitPoint):
  15. # point map tool
  16. # used as an example for easyPlugin
  17. def __init__(self, icon_action):
  18. self.canvas = iface.mapCanvas()
  19. self.icon_action = icon_action
  20. # hover circle
  21. self.rubberBandPointRound = QgsRubberBand(self.canvas, QgsWkbTypes.PointGeometry)
  22. self.rubberBandPointRound.setColor(QColor(55,50,100,50))
  23. self.rubberBandPointRound.setStrokeColor(QColor('red'))
  24. self.rubberBandPointRound.setWidth(35)
  25. self.rubberBandPointRound.reset()
  26. # click point
  27. self.rubberBandPoint = QgsRubberBand(self.canvas, QgsWkbTypes.PointGeometry)
  28. self.rubberBandPoint.setColor(QColor(150,150,200,255))
  29. self.rubberBandPoint.setWidth(15)
  30. self.rubberBandPoint.reset()
  31. self.anchor_point = None
  32. self.anchor_point_geom = None
  33. # initialize tool
  34. QgsMapToolEmitPoint.__init__(self, self.canvas)
  35. return
  36. def canvasMoveEvent(self, e):
  37. # listening to mouse move
  38. self.anchor_point = self.toMapCoordinates(e.pos())
  39. self.anchor_point_geom = QgsGeometry().fromPointXY(self.anchor_point)
  40. self.rubberBandPointRound.reset()
  41. self.rubberBandPointRound.setToGeometry(self.anchor_point_geom)
  42. return
  43. def canvasPressEvent(self, e):
  44. # listening to mouse click
  45. self.rubberBandPoint.reset()
  46. self.rubberBandPoint.setToGeometry(self.anchor_point_geom)
  47. return
  48. def deactivate(self):
  49. # deactivating tool
  50. self.rubberBandPoint.reset()
  51. self.rubberBandPointRound.reset()
  52. QgsMapTool.deactivate(self)
  53. self.deactivated.emit()
  54. self.icon_action.setChecked(False)
  55. return
  56. class SimpleGui(QWidget):
  57. # simple gui widget which check selected values and brings it into
  58. # notification
  59. # used as an example for easyPlugin
  60. def __init__(self, parent=None):
  61. super().__init__(parent)
  62. self.setWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
  63. self.setGeometry(int(w / 2 - 200), int(h / 2) - 50, 400, 100)
  64. self.grid = QGridLayout()
  65. self.grid.setSpacing(10)
  66. self.setWindowTitle("Simple GUI")
  67. # variables
  68. self.current_layer = None
  69. self.fields_data = []
  70. self.values_data = []
  71. all_layers = list(
  72. list(
  73. filter(
  74. lambda x: x.type() == QgsVectorLayer.VectorLayer
  75. and x.featureCount()
  76. and x.isValid(),
  77. QgsProject.instance().mapLayers().values(),
  78. )
  79. )
  80. )
  81. self.layers_dict = {l.name(): l for l in all_layers}
  82. # widgets
  83. self.combo_layers = QComboBox()
  84. self.combo_fields = QComboBox()
  85. self.check_value = QPushButton("Check value")
  86. self.combo_layers.addItems(list(self.layers_dict.keys()))
  87. # gui setup
  88. self.setLayout(self.grid)
  89. self.grid.addWidget(self.combo_layers, 1, 1, 1, 2)
  90. self.grid.addWidget(self.combo_fields, 1, 3, 1, 3)
  91. self.grid.addWidget(self.check_value, 2, 1, 1, 5)
  92. # actions
  93. self.combo_layers.currentIndexChanged.connect(self.update_fields)
  94. self.check_value.clicked.connect(self.check_selected_values)
  95. self.update_fields()
  96. self.show()
  97. def update_fields(self):
  98. # get current layer and fields
  99. if self.layers_dict:
  100. selected_text = self.combo_layers.currentText()
  101. self.current_layer = self.layers_dict[selected_text]
  102. self.combo_fields.clear()
  103. self.combo_fields.addItems(self.current_layer.fields().names())
  104. def check_selected_values(self):
  105. # get selected features and its values from selected fields
  106. if self.current_layer:
  107. selection = list(self.current_layer.getSelectedFeatures())
  108. if selection:
  109. current_field = self.combo_fields.currentText()
  110. unique_values = sorted(set([f[current_field] for f in selection]))
  111. message_text = txt_selection.format(
  112. self.current_layer.name(),
  113. current_field,
  114. ", ".join([str(w) for w in unique_values]),
  115. )
  116. self.warning_message(message_text)
  117. else:
  118. self.warning_message(txt_no_selection.format(self.current_layer.name()))
  119. else:
  120. self.warning_message("No vector layers in current project")
  121. def warning_message(self, text):
  122. mbox = QMessageBox()
  123. mbox.warning(self, "Notification", text)