dtmedianlinetool.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # -*- coding: utf-8 -*-
  2. """
  3. digitizemedianlinetool
  4. ``````````````````````
  5. """
  6. """
  7. Part of DigitizingTools, a QGIS plugin that
  8. subsumes different tools neded during digitizing sessions
  9. This vertex selection tool is adopted/adapted from:
  10. 'CadTools Plugin', Copyright (C) Stefan Ziegler
  11. * begin : 2013-08-15
  12. * copyright : (C) 2013 by Angelos Tzotsos
  13. * email : tzotsos@gmail.com
  14. This program is free software; you can redistribute it and/or modify
  15. it under the terms of the GNU General Public License as published by
  16. the Free Software Foundation; either version 2 of the License, or
  17. (at your option) any later version.
  18. """
  19. from qgis.PyQt import QtCore, QtGui
  20. from qgis.core import *
  21. from qgis.gui import *
  22. class DtMedianLineTool(QgsMapTool):
  23. finishedDigitizing = QtCore.pyqtSignal()
  24. vertexFound = QtCore.pyqtSignal(list)
  25. def __init__(self, parent):
  26. super().__init__(parent.canvas)
  27. self.canvas = parent.canvas
  28. self.parent = parent
  29. self.markers = []
  30. #custom cursor
  31. self.cursor = QtGui.QCursor(QtGui.QPixmap(["16 16 3 1",
  32. " c None",
  33. ". c #FF0000",
  34. "+ c #FFFFFF",
  35. " ",
  36. " +.+ ",
  37. " ++.++ ",
  38. " +.....+ ",
  39. " +. .+ ",
  40. " +. . .+ ",
  41. " +. . .+ ",
  42. " ++. . .++",
  43. " ... ...+... ...",
  44. " ++. . .++",
  45. " +. . .+ ",
  46. " +. . .+ ",
  47. " ++. .+ ",
  48. " ++.....+ ",
  49. " ++.++ ",
  50. " +.+ "]))
  51. def canvasPressEvent(self, event):
  52. pass
  53. def canvasMoveEvent(self, event):
  54. pass
  55. def canvasReleaseEvent(self, event):
  56. #Get the click
  57. x = event.pos().x()
  58. y = event.pos().y()
  59. if event.button() == QtCore.Qt.RightButton:
  60. self.finishedDigitizing.emit()
  61. return
  62. layer = self.canvas.currentLayer()
  63. if layer is not None:
  64. #the clicked point is our starting point
  65. startingPoint = QtCore.QPoint(x, y)
  66. #we need a snapper, so we use the MapCanvas snappingUtils (new in 2.8.x)
  67. snapper = self.canvas.snappingUtils()
  68. snapper.setCurrentLayer(layer)
  69. #snapType, snapTolerance, snapUnits = snapper.defaultSettings()
  70. # snapType = 0: no snap, 1 = vertex, 2 = segment, 3 = vertex & segment
  71. snapMatch = snapper.snapToCurrentLayer(startingPoint, QgsPointLocator.Vertex)
  72. #if we have found a vertex
  73. if snapMatch.isValid():
  74. # we like to mark the vertex that is choosen
  75. p = snapMatch.point()
  76. m = QgsVertexMarker(self.canvas)
  77. m.setIconType(1)
  78. modulo = self.parent.selected_points % 2
  79. if modulo == 0:
  80. m.setColor(QtGui.QColor(255, 0, 0))
  81. else:
  82. m.setColor(QtGui.QColor(0, 0, 255))
  83. m.setIconSize(12)
  84. m.setPenWidth(3)
  85. m.setCenter(p)
  86. self.markers.append(m)
  87. self.vertexFound.emit([p])
  88. else:
  89. pass
  90. def showSettingsWarning(self):
  91. m = QgsMessageViewer()
  92. m.setWindowTitle("Snap tolerance")
  93. m.setCheckBoxText("Don't show this message again")
  94. m.setCheckBoxVisible(True)
  95. m.setCheckBoxQSettingsLabel(settingsLabel)
  96. m.setMessageAsHtml("<p>Could not snap segment.</p><p>Have you set "
  97. "the tolerance in Settings > Project Properties > General?</p>")
  98. m.showMessage()
  99. def activate(self):
  100. self.canvas.setCursor(self.cursor)
  101. def deactivate(self):
  102. for m in self.markers:
  103. self.canvas.scene().removeItem(m)
  104. del self.markers[:]
  105. def isZoomTool(self):
  106. return False
  107. def isTransient(self):
  108. return False
  109. def isEditTool(self):
  110. return True