diff --git a/PyReconstruct/modules/calc/__init__.py b/PyReconstruct/modules/calc/__init__.py index 63f554ec3..403b43833 100644 --- a/PyReconstruct/modules/calc/__init__.py +++ b/PyReconstruct/modules/calc/__init__.py @@ -28,6 +28,9 @@ reducePoints, getExterior ) +from .polygon import ( + uncuttable_closed_traces +) from .image import ( getImgDims, point_list_2_pix diff --git a/PyReconstruct/modules/calc/grid.py b/PyReconstruct/modules/calc/grid.py index 45eb8aee2..42c1b2258 100644 --- a/PyReconstruct/modules/calc/grid.py +++ b/PyReconstruct/modules/calc/grid.py @@ -323,7 +323,10 @@ def cutTraces(trace_list, cut_trace, del_threshold=0.0, closed=True): Returns: list: The newly cut traces """ - if not trace_list or not cut_trace: + # A usable cut line needs at least two points; an empty or single-point + # cut (a knife click with no drag) would raise in shapely's LineString, + # so treat it as a no-op. + if not trace_list or not cut_trace or len(cut_trace) < 2: return trace_list if closed: diff --git a/PyReconstruct/modules/calc/polygon.py b/PyReconstruct/modules/calc/polygon.py index 12bcc575d..55e663e92 100644 --- a/PyReconstruct/modules/calc/polygon.py +++ b/PyReconstruct/modules/calc/polygon.py @@ -13,22 +13,65 @@ ) +def uncuttable_closed_traces(trace_list) -> List[int]: + """Return the indices of traces that cannot be cut as closed polygons. + + A closed cut is a polygon difference, so it needs a polygon shapely will + accept: at least three points, and an outline that does not cross itself. + Freehand tracing produces the second case often (a loop doubled back on, a + figure eight), and a two-point contour produces the first. + + Callers use this to decide *before* they change anything, because the + alternative is to find out afterwards. `cut_closed_traces` used to drop such + a trace from its result, and its one caller in the field deletes the + originals before it looks at what came back, so the object was deleted and + nothing replaced it. + + Params: + trace_list (list): traces, each a list of points + Returns: + (list): the indices in `trace_list` that cannot be cut + """ + uncuttable = [] + + for i, trace in enumerate(trace_list): + + if len(trace) < 3: # Polygon() raises below three points + uncuttable.append(i) + continue + + if not Polygon(trace).is_valid: + uncuttable.append(i) + + return uncuttable + + def cut_closed_traces(trace_list, cut_trace, del_threshold=0.0) -> List[Any]: """Cut closed polygons.""" # Convert cut_trace to a LineString cut_line = LineString(cut_trace) - + # Create a list to store resulting traces new_traces = [] - + # Process each trace for trace in trace_list: + # A trace shapely cannot use as a polygon is kept, not dropped. The + # caller in the field deletes the originals before it inspects the + # result, so dropping one here deleted the user's object and returned + # nothing to put in its place. Use `uncuttable_closed_traces` to refuse + # the operation up front; this is the floor under that check, so that no + # caller of this function can lose a trace by reaching it. + if len(trace) < 3: + new_traces.append(trace) + continue + poly = Polygon(trace) - # Skip invalid polygons if not poly.is_valid: + new_traces.append(trace) continue ## Determine trace area threshold diff --git a/PyReconstruct/modules/datatypes/default_settings.py b/PyReconstruct/modules/datatypes/default_settings.py index 7b151522d..c8b30b1ca 100644 --- a/PyReconstruct/modules/datatypes/default_settings.py +++ b/PyReconstruct/modules/datatypes/default_settings.py @@ -67,6 +67,7 @@ def get_username() -> str: "roll_knife_window": 10, "trace_mode": "combo", # combo, poly, scribble # MFO "knife_del_threshold": 1.0, # MFO + "knife_ignore_secondary_click": True, # MFO "grid": [1, 1, 1, 1, 1, 1], # MFO "sampling_frame_grid": True, # MFO "flag_name": "", # MFO diff --git a/PyReconstruct/modules/gui/dialog/all_options.py b/PyReconstruct/modules/gui/dialog/all_options.py index 0b00011cf..62e804ed9 100644 --- a/PyReconstruct/modules/gui/dialog/all_options.py +++ b/PyReconstruct/modules/gui/dialog/all_options.py @@ -204,13 +204,28 @@ def setOption(response): ["When using the knife, objects smaller than this percent"], ["of the original trace area will be automatically deleted."], None, - ["Knife delete threshold (%):", ("float", self.series.getOption("knife_del_threshold", use_defaults), (0, 100)), None] + ["Knife delete threshold (%):", ("float", self.series.getOption("knife_del_threshold", use_defaults), (0, 100)), None], + None, + ["A press of another mouse button while a cut is being drawn"], + ["can be ignored, so that a tablet's barrel button does not"], + ["cancel the cut and open the field menu over the object."], + None, + [ + ( + "check", + ( + "Ignore other mouse buttons while cutting", + self.series.getOption("knife_ignore_secondary_click", use_defaults) + ) + ) + ] ] - + def setOption(response): - + self.series.setOption("knife_del_threshold", response[0]) - + self.series.setOption("knife_ignore_secondary_click", response[1][0][1]) + self.addOptionWidget("knife", structure, setOption) ## 3D options diff --git a/PyReconstruct/modules/gui/main/field_widget.py b/PyReconstruct/modules/gui/main/field_widget.py index d7d9c598a..7bec5b877 100644 --- a/PyReconstruct/modules/gui/main/field_widget.py +++ b/PyReconstruct/modules/gui/main/field_widget.py @@ -201,6 +201,31 @@ def mousePressEvent(self, event): # check what was clicked self.lclick, self.mclick, self.rclick = get_clicked(event) + # A knife stroke in progress owns the gesture. + # + # A drawing tablet's barrel button, or any stray second press, used to + # fall through the "favor right click" branch below, which clears + # `current_trace` and drops `lclick`, and then raise the field context + # menu over the object being cut. The cut was lost and a menu nobody + # asked for appeared under a still-moving pen, one release away from + # "Delete selected". Ignoring the press keeps the stroke alive; + # `knifeRelease` still commits it when the pen comes up. + # + # `lclick` is forced back on because the press may report only the + # secondary button, and `knifeRelease` reads `lclick` to decide whether + # to cut. Same idea as `is_line_tracing` in `exclude_context` below: a + # gesture that is already underway is not a place for a context menu. + # The knife's right-click dialog turns this off for anyone who wants the + # old escape hatch. + if ( + self.rclick + and self.mouse_mode == KNIFE + and self.current_trace + and self.series.getOption("knife_ignore_secondary_click") + ): + self.lclick, self.rclick, self.mclick = True, False, False + return + # ignore middle clicks combined with other clicks if self.mclick and (self.lclick or self.rclick): if self.is_panzooming: diff --git a/PyReconstruct/modules/gui/main/field_widget_2_trace.py b/PyReconstruct/modules/gui/main/field_widget_2_trace.py index b34608a26..7b3080716 100644 --- a/PyReconstruct/modules/gui/main/field_widget_2_trace.py +++ b/PyReconstruct/modules/gui/main/field_widget_2_trace.py @@ -20,8 +20,9 @@ pixmapPointToField, getExterior, mergeTraces, - reducePoints, + reducePoints, cutTraces, + uncuttable_closed_traces, ) from PyReconstruct.modules.gui.table import ( CopyTableWidget, @@ -579,6 +580,24 @@ def cutTrace(self, scalpel_trace : list): ## Pixelize the selected traces traces_to_cut = [self.section_layer.traceToPix(t) for t in traces] + ## Refuse a trace that cannot be cut, rather than deleting it + # + # A closed cut is a polygon difference and needs a polygon shapely will + # accept. A freehand outline that crosses itself is not one, and + # `cut_closed_traces` used to drop it from the result, which this + # method only inspects after `deleteTraces` has already run, so the + # object was deleted, nothing replaced it, no message was shown and the + # method returned True. Decide before anything is committed, and refuse + # the whole selection rather than cutting part of it. + if example_trace.closed and uncuttable_closed_traces(traces_to_cut): + + notify( + "A selected trace crosses itself and cannot be cut.\n" + "The object was left unchanged." + ) + + return False + ## Smooth cut if requested if self.series.getOption("roll_knife_average"): @@ -598,6 +617,31 @@ def cutTrace(self, scalpel_trace : list): closed=example_trace.closed ) + ## A cut line of fewer than two points is no cut at all + # + # `cutTraces` returns the list it was given, unchanged and by identity, + # for a degenerate cut line. A knife click with no drag produces one. + # Leave the section alone instead of deleting every selected trace and + # recreating it, which logged a modification and consumed a palette + # increment for a gesture that did nothing. + if cut_traces is traces_to_cut: + return False + + ## Nothing to put back + # + # The last line of defense, for any path that returns no pieces at all + # (every piece under the delete threshold, an open trace shapely could + # not split). Deleting here is what loses an object, so refuse the whole + # operation rather than half-completing it. + if not cut_traces: + + notify( + "The cut could not be completed.\n" + "The object was left unchanged." + ) + + return False + ## Remove old traces self.section.deleteTraces(traces, log_event=False) diff --git a/PyReconstruct/modules/gui/main/main_window.py b/PyReconstruct/modules/gui/main/main_window.py index b03e24491..ec390eccf 100644 --- a/PyReconstruct/modules/gui/main/main_window.py +++ b/PyReconstruct/modules/gui/main/main_window.py @@ -1930,6 +1930,18 @@ def modifyKnife(self, event=None): [ ("check", ("Smooth cuts", self.series.getOption("roll_knife_average"))), ("int", self.series.getOption("roll_knife_window")) + ], + [ + "\nWhile a cut is being drawn:" + ], + [ + ( + "check", + ( + "Ignore the other mouse buttons", + self.series.getOption("knife_ignore_secondary_click") + ) + ) ] ] response, confirmed = QuickDialog.get(self, structure, "Knife") @@ -1939,7 +1951,8 @@ def modifyKnife(self, event=None): self.series.setOption("knife_del_threshold", response[0]) self.series.setOption("roll_knife_average", response[1][0][1]) self.series.setOption("roll_knife_window", response[2]) - + self.series.setOption("knife_ignore_secondary_click", response[3][0][1]) + self.seriesModified() def resetTracePalette(self):