-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmap_tools.py
More file actions
172 lines (137 loc) · 5.85 KB
/
map_tools.py
File metadata and controls
172 lines (137 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""
Map tools for interactive segmentation prompts.
"""
from qgis.PyQt.QtCore import Qt, pyqtSignal
from qgis.PyQt.QtGui import QColor
from qgis.core import QgsPointXY, QgsRectangle, QgsWkbTypes
from qgis.gui import QgsMapTool, QgsRubberBand, QgsVertexMarker
class PointPromptTool(QgsMapTool):
"""Map tool for adding point prompts."""
point_added = pyqtSignal(object, bool) # point, is_foreground
def __init__(self, canvas, plugin, batch_mode=False):
"""Initialize the point prompt tool.
Args:
canvas: The QGIS map canvas.
plugin: The parent plugin instance.
batch_mode: If True, adds points to batch list instead of regular list.
"""
super().__init__(canvas)
self.canvas = canvas
self.plugin = plugin
self.is_foreground = True
self.batch_mode = batch_mode
self.markers = []
# Set cursor
self.setCursor(Qt.CrossCursor)
def set_foreground(self, foreground):
"""Set whether we're adding foreground or background points."""
self.is_foreground = foreground
def canvasPressEvent(self, event):
"""Handle mouse press event."""
pass
def canvasReleaseEvent(self, event):
"""Handle mouse release event - add a point."""
if event.button() == Qt.LeftButton:
point = self.toMapCoordinates(event.pos())
# Add visual marker
marker = QgsVertexMarker(self.canvas)
marker.setCenter(point)
# Use blue for batch mode, green/red for regular mode
if self.batch_mode:
marker.setColor(QColor(0, 120, 255))
else:
marker.setColor(
QColor(0, 255, 0) if self.is_foreground else QColor(255, 0, 0)
)
marker.setIconType(QgsVertexMarker.ICON_CIRCLE)
marker.setIconSize(10)
marker.setPenWidth(3)
self.markers.append(marker)
# Add point to plugin
if self.batch_mode:
self.plugin.add_batch_point(point)
else:
self.plugin.add_point(point, self.is_foreground)
elif event.button() == Qt.RightButton:
# Right-click to finish
self.deactivate()
if self.batch_mode:
self.plugin.batch_add_point_btn.setChecked(False)
else:
self.plugin.add_fg_point_btn.setChecked(False)
self.plugin.add_bg_point_btn.setChecked(False)
def clear_markers(self):
"""Clear all markers from the canvas."""
for marker in self.markers:
self.canvas.scene().removeItem(marker)
self.markers = []
def deactivate(self):
"""Deactivate the tool."""
super().deactivate()
class BoxPromptTool(QgsMapTool):
"""Map tool for drawing box prompts."""
box_drawn = pyqtSignal(object) # QgsRectangle
def __init__(self, canvas, plugin):
"""Initialize the box prompt tool.
Args:
canvas: The QGIS map canvas.
plugin: The parent plugin instance.
"""
super().__init__(canvas)
self.canvas = canvas
self.plugin = plugin
# Rubber band for visual feedback
self.rubber_band = QgsRubberBand(canvas, QgsWkbTypes.PolygonGeometry)
self.rubber_band.setColor(QColor(0, 120, 255, 100))
self.rubber_band.setStrokeColor(QColor(0, 120, 255))
self.rubber_band.setWidth(2)
self.start_point = None
self.is_drawing = False
# Set cursor
self.setCursor(Qt.CrossCursor)
def canvasPressEvent(self, event):
"""Handle mouse press event - start drawing."""
if event.button() == Qt.LeftButton:
self.start_point = self.toMapCoordinates(event.pos())
self.is_drawing = True
self.rubber_band.reset(QgsWkbTypes.PolygonGeometry)
def canvasMoveEvent(self, event):
"""Handle mouse move event - update rubber band."""
if self.is_drawing and self.start_point is not None:
current_point = self.toMapCoordinates(event.pos())
self.update_rubber_band(self.start_point, current_point)
def canvasReleaseEvent(self, event):
"""Handle mouse release event - finish drawing."""
if event.button() == Qt.LeftButton and self.is_drawing:
end_point = self.toMapCoordinates(event.pos())
self.is_drawing = False
# Create rectangle
rect = QgsRectangle(self.start_point, end_point)
# Update rubber band with final position
self.update_rubber_band(self.start_point, end_point)
# Set box in plugin
self.plugin.set_box(rect)
# Deactivate tool
self.deactivate()
self.plugin.draw_box_btn.setChecked(False)
elif event.button() == Qt.RightButton:
# Right-click to cancel
self.is_drawing = False
self.rubber_band.reset(QgsWkbTypes.PolygonGeometry)
self.deactivate()
self.plugin.draw_box_btn.setChecked(False)
def update_rubber_band(self, start_point, end_point):
"""Update the rubber band rectangle."""
self.rubber_band.reset(QgsWkbTypes.PolygonGeometry)
# Create rectangle points
self.rubber_band.addPoint(QgsPointXY(start_point.x(), start_point.y()), False)
self.rubber_band.addPoint(QgsPointXY(end_point.x(), start_point.y()), False)
self.rubber_band.addPoint(QgsPointXY(end_point.x(), end_point.y()), False)
self.rubber_band.addPoint(QgsPointXY(start_point.x(), end_point.y()), True)
def clear_rubber_band(self):
"""Clear the rubber band."""
self.rubber_band.reset(QgsWkbTypes.PolygonGeometry)
def deactivate(self):
"""Deactivate the tool."""
self.is_drawing = False
super().deactivate()