Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions PyReconstruct/modules/backend/func/state_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,17 @@ def addState(self, section : Section, series : Series):
updated_ztraces
)

def dropStatesAfter(self, count : int):
"""Drop the undo states saved after the first `count`.

Folds an action that saved more than one state into one undo step.
The current state is kept: it already reflects the finished action.

Params:
count (int): the number of undo states to keep
"""
del self.undo_states[count:]

def undoState(self, section : Section, series : Series) -> set:
"""Restore an undo state on the section.

Expand Down
2 changes: 1 addition & 1 deletion PyReconstruct/modules/gui/dialog/all_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def setOption(response):
("Poly", trace_mode == "poly"),
("Combo", trace_mode == "combo")
)],
[("check", ("Automatically merge selected traces", self.series.getOption("auto_merge", use_defaults)))]
[("check", ("Automatically merge overlapping traces", self.series.getOption("auto_merge", use_defaults)))]
]
def setOption(response):
if response[0][0][1]:
Expand Down
7 changes: 5 additions & 2 deletions PyReconstruct/modules/gui/main/field_widget_2_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -1073,11 +1073,14 @@ def mergeTraces(
merge names the trace just drawn instead, so that the
palette the user is tracing with is what survives.
"""
if len(traces) < 2:
to_merge = restrict if restrict else traces

# auto merge restricts to the traces it found overlapping, and only
# the trace just drawn among them is sure to be selected
if len(to_merge) < 2:
notify("Please select two or more traces to merge.")
return False

to_merge = restrict if restrict else traces
first_trace = attrs_from if attrs_from is not None else to_merge[0]

# set attributes to be the first object selected
Expand Down
58 changes: 42 additions & 16 deletions PyReconstruct/modules/gui/main/field_widget_5_mouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,26 @@ def isSingleClicking(self):
return False

def autoMerge(self):
"""Automatically merge the selected traces of the same name."""
# merge with existing selected traces of the same name
"""Merge the trace just drawn with the same-name traces it overlaps."""
if not self.series.getOption("auto_merge"):
return
traces_to_merge = []
for t in self.section.selected_traces:
if t.name == self.tracing_trace.name and t.closed:
traces_to_merge.append(t)
if len(traces_to_merge) < 2:

# the trace just drawn: newTrace appends it to its contour and selects
# it, so a draw that was refused leaves nothing to merge
contour = self.section.contours.get(self.tracing_trace.name)
if not contour:
return
new_trace = contour[-1]
if not new_trace.closed or new_trace not in self.section.selected_traces:
return

# the trace just drawn: newTrace appends it to the selection
new_trace = traces_to_merge[-1]
# The candidates are the visible closed traces of the same name on this
# section, whether or not they are selected: tracing in polygon mode
# often leaves the existing trace unselected (issue #138).
candidates = [
t for t in contour
if t is not new_trace and t.closed and not t.hidden
]

# Only the traces the new stroke actually runs into are merged, and
# transitively: it runs into A, A runs into B.
Expand All @@ -146,7 +153,6 @@ def autoMerge(self):
# what is merely *selected*: the selection is a lineage that outlives
# each merge, since the merged trace is what stays selected.
group = [new_trace]
candidates = traces_to_merge[:-1]
growing = True
while growing:
growing = False
Expand All @@ -169,6 +175,18 @@ def autoMerge(self):

self.mergeTraces(restrict=group, attrs_from=attrs)

def autoMergeDrawnTrace(self):
"""Auto-merge the trace just drawn as part of the draw's undo step.

The draw has already saved an undo state and the merge saves another,
but to the user they are one action: one undo must restore the section
as it was before the draw, not leave the unmerged trace (issue #137).
"""
section_states = self.series_states[self.series.current_section]
n_states = len(section_states.undo_states)
self.autoMerge()
section_states.dropStatesAfter(n_states)

def pointerPress(self, event):
"""Called when mouse is pressed in pointer mode.

Expand Down Expand Up @@ -597,7 +615,7 @@ def pencilRelease(self, event):
)

if closed and len(self.current_trace) > 2:
self.autoMerge()
self.autoMergeDrawnTrace()

self.current_trace = []

Expand Down Expand Up @@ -633,8 +651,11 @@ def lineRelease(self, event=None, override=False, log_event=True):
# a no-op when the trace layer is hidden (@field_interaction) or
# when the retraced line collapses to < 2 points. The return
# value cannot be used here because it carries log_event, which
# is forced False while scissoring.
added_before = len(self.section.added_traces)
# is forced False while scissoring. Nor can added_traces: on a
# logged draw, the table refresh inside newTrace clears it
# (clearTracking) before newTrace returns. Count the contour.
name = self.tracing_trace.name
contour_before = len(self.section.contours.get(name, []))

self.newTrace(
current_trace_copy,
Expand All @@ -643,13 +664,18 @@ def lineRelease(self, event=None, override=False, log_event=True):
log_event=(log_event and (not self.is_scissoring))
)

recreated = len(self.section.added_traces) > added_before
recreated = len(self.section.contours.get(name, [])) > contour_before

if recreated and log_event and self.is_scissoring:
self.series.addLog(self.tracing_trace.name, self.section.n, "Modify trace(s)")

if recreated and closed and len(self.current_trace) > 2:
self.autoMerge()
# only a logged draw saved an undo state for the merge to join;
# a scissors edit is not a new draw and is not merged
if (
recreated and closed and log_event and not self.is_scissoring
and len(self.current_trace) > 2
):
self.autoMergeDrawnTrace()

self.current_trace = []

Expand Down
2 changes: 1 addition & 1 deletion PyReconstruct/modules/gui/main/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,7 @@ def changeTraceMode(self):
("Rectangle", current_shape == "rect"),
("Ellipse", current_shape == "circle")
)],
[("check", ("Automatically merge selected traces", self.series.getOption("auto_merge")))],
[("check", ("Automatically merge overlapping traces", self.series.getOption("auto_merge")))],
[("check", ("Apply rolling average while scribbling", self.series.getOption("roll_average"))),
("int", self.series.getOption("roll_window"))]
]
Expand Down
Loading