Skip to content

Commit 0683e4a

Browse files
committed
Allow using the mouse pixel value tracking tool in the recode pixel
table without needing to activate an editing tool
1 parent 152f891 commit 0683e4a

File tree

2 files changed

+46
-31
lines changed

2 files changed

+46
-31
lines changed

core/editing.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ def get_old_and_new_pixel_values(self, pixel):
169169
def highlight_value_in_recode_pixel_table(self, value_to_select):
170170
"""Highlight the current pixel value from mouse pointer on canvas"""
171171
from ThRasE.thrase import ThRasE
172+
if self.pixels is None:
173+
return
172174
if value_to_select is None:
173175
ThRasE.dialog.recodePixelTable.clearSelection()
174176
with block_signals_to(ThRasE.dialog.recodePixelTable):

gui/view_widget.py

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,12 @@ def setup_view_widget(self):
6060
"polygon": EditLog("polygon"),
6161
"freehand": EditLog("polygon")
6262
}
63-
# unselect items in recode pixel table
63+
# mouse pixel value tracking in recode pixel table
64+
self.mouse_pixel_value_tracking = False
6465
self.mousePixelValue2Table.clicked.connect(self.unhighlight_cells_in_recode_pixel_table)
66+
self.mousePixelValue2Table.toggled.connect(self.toggle_mouse_pixel_value_tracking)
67+
if self.mousePixelValue2Table.isChecked():
68+
self.toggle_mouse_pixel_value_tracking(True)
6569

6670
# picker pixel tool edit
6771
self.widget_EditingToolbar.setEnabled(False)
@@ -105,7 +109,7 @@ def setup_view_widget(self):
105109

106110
def trigger_auto_clear(self, picker_type="all"):
107111
"""Trigger auto-clear after edit if enabled with a delay
108-
112+
109113
Args:
110114
picker_type: Type of picker tool - "line", "polygon", "freehand", or "all"
111115
"""
@@ -121,7 +125,7 @@ def auto_clear():
121125
self.clear_all_freehand_drawn()
122126
self.render_widget.canvas.clearCache()
123127
self.render_widget.refresh()
124-
128+
125129
QTimer.singleShot(500, auto_clear)
126130

127131
@staticmethod
@@ -132,6 +136,43 @@ def unhighlight_cells_in_recode_pixel_table():
132136
[ThRasE.dialog.recodePixelTable.item(idx, 2).setBackground(Qt.white)
133137
for idx in range(len(LayerToEdit.current.pixels))]
134138

139+
def toggle_mouse_pixel_value_tracking(self, enabled):
140+
"""Connect or disconnect canvas tracking for pixel-value highlighting."""
141+
if enabled and not self.mouse_pixel_value_tracking:
142+
self.render_widget.canvas.xyCoordinates.connect(self.update_pixel_value_highlight)
143+
self.mouse_pixel_value_tracking = True
144+
return
145+
146+
if not enabled and self.mouse_pixel_value_tracking:
147+
try:
148+
self.render_widget.canvas.xyCoordinates.disconnect(self.update_pixel_value_highlight)
149+
except TypeError:
150+
pass
151+
self.mouse_pixel_value_tracking = False
152+
layer = LayerToEdit.current
153+
if layer and layer.pixels:
154+
layer.highlight_value_in_recode_pixel_table(None)
155+
156+
def update_pixel_value_highlight(self, point):
157+
"""Highlight the table row matching the pixel under the mouse pointer."""
158+
if not self.mousePixelValue2Table.isChecked():
159+
return
160+
161+
layer = LayerToEdit.current
162+
if layer is None or not layer.pixels:
163+
return
164+
165+
pixel_value = None
166+
if layer.check_point_inside_layer(point):
167+
try:
168+
identify_result = layer.data_provider.identify(point, QgsRaster.IdentifyFormatValue)
169+
if identify_result.isValid():
170+
pixel_value = identify_result.results().get(layer.band)
171+
except Exception:
172+
pixel_value = None
173+
174+
layer.highlight_value_in_recode_pixel_table(pixel_value)
175+
135176
def update(self):
136177
valid_layers = [layer_toolbar.layer for layer_toolbar in self.layer_toolbars if layer_toolbar.is_active]
137178
if len(valid_layers) > 0:
@@ -540,13 +581,6 @@ def canvasMoveEvent(self, event):
540581
ThRasE.dialog.map_coordinate.setText("Coordinate: {:.3f}, {:.3f} ({})".format(
541582
map_coordinate.x(), map_coordinate.y(), crs))
542583

543-
# highlight the current pixel value from mouse picker
544-
if self.view_widget.mousePixelValue2Table.isChecked():
545-
point = self.view_widget.render_widget.canvas.getCoordinateTransform().toMapCoordinates(event.pos().x(), event.pos().y())
546-
pixel_value_to_select = \
547-
LayerToEdit.current.data_provider.identify(point, QgsRaster.IdentifyFormatValue).results()[LayerToEdit.current.band]
548-
LayerToEdit.current.highlight_value_in_recode_pixel_table(pixel_value_to_select)
549-
550584
def canvasPressEvent(self, event):
551585
# edit the pixel over pointer mouse on left-click
552586
if event.button() == Qt.LeftButton:
@@ -641,13 +675,6 @@ def canvasMoveEvent(self, event):
641675
crs = iface.mapCanvas().mapSettings().destinationCrs().authid()
642676
ThRasE.dialog.map_coordinate.setText("Coordinate: {:.3f}, {:.3f} ({})".format(
643677
map_coordinate.x(), map_coordinate.y(), crs))
644-
645-
# highlight the current pixel value from mouse picker
646-
if self.view_widget.mousePixelValue2Table.isChecked():
647-
point = self.view_widget.render_widget.canvas.getCoordinateTransform().toMapCoordinates(event.pos().x(), event.pos().y())
648-
pixel_value_to_select = \
649-
LayerToEdit.current.data_provider.identify(point, QgsRaster.IdentifyFormatValue).results()[LayerToEdit.current.band]
650-
LayerToEdit.current.highlight_value_in_recode_pixel_table(pixel_value_to_select)
651678
# draw the auxiliary line
652679
if self.aux_line is None:
653680
return
@@ -762,13 +789,6 @@ def canvasMoveEvent(self, event):
762789
crs = iface.mapCanvas().mapSettings().destinationCrs().authid()
763790
ThRasE.dialog.map_coordinate.setText("Coordinate: {:.3f}, {:.3f} ({})".format(
764791
map_coordinate.x(), map_coordinate.y(), crs))
765-
766-
# highlight the current pixel value from mouse picker
767-
if self.view_widget.mousePixelValue2Table.isChecked():
768-
point = self.view_widget.render_widget.canvas.getCoordinateTransform().toMapCoordinates(event.pos().x(), event.pos().y())
769-
pixel_value_to_select = \
770-
LayerToEdit.current.data_provider.identify(point, QgsRaster.IdentifyFormatValue).results()[LayerToEdit.current.band]
771-
LayerToEdit.current.highlight_value_in_recode_pixel_table(pixel_value_to_select)
772792
# draw the auxiliary rubber band
773793
if self.aux_rubber_band is None:
774794
return
@@ -883,13 +903,6 @@ def canvasMoveEvent(self, event):
883903
crs = iface.mapCanvas().mapSettings().destinationCrs().authid()
884904
ThRasE.dialog.map_coordinate.setText("Coordinate: {:.3f}, {:.3f} ({})".format(
885905
map_coordinate.x(), map_coordinate.y(), crs))
886-
887-
# highlight the current pixel value from mouse picker
888-
if self.view_widget.mousePixelValue2Table.isChecked():
889-
point = self.view_widget.render_widget.canvas.getCoordinateTransform().toMapCoordinates(event.pos().x(), event.pos().y())
890-
pixel_value_to_select = \
891-
LayerToEdit.current.data_provider.identify(point, QgsRaster.IdentifyFormatValue).results()[LayerToEdit.current.band]
892-
LayerToEdit.current.highlight_value_in_recode_pixel_table(pixel_value_to_select)
893906
# draw the auxiliary rubber band
894907
if not self.drawing or not self.rubber_band:
895908
return

0 commit comments

Comments
 (0)